Bug 409109: using the new operation counting JS API for monitoring long-running scripts. r=brendan,jst

This commit is contained in:
igor@mir2.org 2008-01-09 07:20:39 -08:00
Родитель f25955ac69
Коммит 04e836d754
5 изменённых файлов: 80 добавлений и 63 удалений

Просмотреть файл

@ -782,19 +782,19 @@ PrintWinCodebase(nsGlobalWindow *win)
}
#endif
// The number of branch callbacks between calls to JS_MaybeGC
#define MAYBE_GC_BRANCH_COUNT_MASK 0x00000fff // 4095
// The number of operation callbacks between calls to JS_MaybeGC
#define MAYBE_GC_OPERATION_COUNT_MASK 0x00000fff // 4095
// The number of branch callbacks before we even check if our start
// The number of operation callbacks before we even check if our start
// timestamp is initialized. This is a fairly low number as we want to
// initialize the timestamp early enough to not waste much time before
// we get there, but we don't want to bother doing this too early as
// it's not generally necessary.
#define INITIALIZE_TIME_BRANCH_COUNT_MASK 0x000000ff // 255
#define INITIALIZE_TIME_OPERATION_COUNT_MASK 0x000000ff // 255
// This function is called after each JS branch execution
JSBool JS_DLL_CALLBACK
nsJSContext::DOMBranchCallback(JSContext *cx, JSScript *script)
nsJSContext::DOMOperationCallback(JSContext *cx)
{
// Get the native context
nsJSContext *ctx = static_cast<nsJSContext *>(::JS_GetContextPrivate(cx));
@ -804,17 +804,17 @@ nsJSContext::DOMBranchCallback(JSContext *cx, JSScript *script)
return JS_TRUE;
}
PRUint32 callbackCount = ++ctx->mBranchCallbackCount;
PRUint32 callbackCount = ++ctx->mOperationCallbackCount;
if (callbackCount & INITIALIZE_TIME_BRANCH_COUNT_MASK) {
if (callbackCount & INITIALIZE_TIME_OPERATION_COUNT_MASK) {
return JS_TRUE;
}
if (callbackCount == INITIALIZE_TIME_BRANCH_COUNT_MASK + 1 &&
LL_IS_ZERO(ctx->mBranchCallbackTime)) {
// Initialize mBranchCallbackTime to start timing how long the
if (callbackCount == INITIALIZE_TIME_OPERATION_COUNT_MASK + 1 &&
LL_IS_ZERO(ctx->mOperationCallbackTime)) {
// Initialize mOperationCallbackTime to start timing how long the
// script has run
ctx->mBranchCallbackTime = PR_Now();
ctx->mOperationCallbackTime = PR_Now();
ctx->mIsTrackingChromeCodeTime =
::JS_IsSystemObject(cx, ::JS_GetGlobalObject(cx));
@ -822,22 +822,22 @@ nsJSContext::DOMBranchCallback(JSContext *cx, JSScript *script)
return JS_TRUE;
}
if (callbackCount & MAYBE_GC_BRANCH_COUNT_MASK) {
if (callbackCount & MAYBE_GC_OPERATION_COUNT_MASK) {
return JS_TRUE;
}
// XXX Save the branch callback time so we can restore it after the GC,
// XXX Save the operation callback time so we can restore it after the GC,
// because GCing can cause JS to run on our context, causing our
// ScriptEvaluated to be called, and clearing our branch callback time and
// count. See bug 302333.
PRTime callbackTime = ctx->mBranchCallbackTime;
// ScriptEvaluated to be called, and clearing our operation callback time
// and count. See bug 302333.
PRTime callbackTime = ctx->mOperationCallbackTime;
// Run the GC if we get this far.
JS_MaybeGC(cx);
// Now restore the callback time and count, in case they got reset.
ctx->mBranchCallbackTime = callbackTime;
ctx->mBranchCallbackCount = callbackCount;
ctx->mOperationCallbackTime = callbackTime;
ctx->mOperationCallbackCount = callbackCount;
PRTime now = PR_Now();
@ -871,7 +871,9 @@ nsJSContext::DOMBranchCallback(JSContext *cx, JSScript *script)
nsresult rv;
// Check if we should offer the option to debug
PRBool debugPossible = (cx->debugHooks->debuggerHandler != nsnull);
JSStackFrame* fp = ::JS_GetScriptedCaller(cx, NULL);
PRBool debugPossible = (fp != nsnull &&
cx->debugHooks->debuggerHandler != nsnull);
#ifdef MOZ_JSDEBUGGER
// Get the debugger service if necessary.
if (debugPossible) {
@ -939,6 +941,7 @@ nsJSContext::DOMBranchCallback(JSContext *cx, JSScript *script)
}
// Append file and line number information, if available
JSScript *script = fp ? ::JS_GetFrameScript(cx, fp) : nsnull;
if (script) {
const char *filename = ::JS_GetScriptFilename(cx, script);
if (filename) {
@ -994,17 +997,18 @@ nsJSContext::DOMBranchCallback(JSContext *cx, JSScript *script)
}
}
ctx->mBranchCallbackTime = PR_Now();
ctx->mOperationCallbackTime = PR_Now();
return JS_TRUE;
}
else if ((buttonPressed == 2) && debugPossible) {
// Debug the script
jsval rval;
switch(cx->debugHooks->debuggerHandler(cx, script, cx->fp->pc, &rval,
switch(cx->debugHooks->debuggerHandler(cx, script, ::JS_GetFramePC(cx, fp),
&rval,
cx->debugHooks->
debuggerHandlerData)) {
case JSTRAP_RETURN:
cx->fp->rval = rval;
fp->rval = rval;
return JS_TRUE;
case JSTRAP_ERROR:
cx->throwing = JS_FALSE;
@ -1092,9 +1096,7 @@ nsJSContext::nsJSContext(JSRuntime *aRuntime) : mGCOnDestruction(PR_TRUE)
++sContextCount;
mDefaultJSOptions = JSOPTION_PRIVATE_IS_NSISUPPORTS
| JSOPTION_NATIVE_BRANCH_CALLBACK
| JSOPTION_ANONFUNFIX;
mDefaultJSOptions = JSOPTION_PRIVATE_IS_NSISUPPORTS | JSOPTION_ANONFUNFIX;
// Let xpconnect resync its JSContext tracker. We do this before creating
// a new JSContext just in case the heap manager recycles the JSContext
@ -1113,7 +1115,8 @@ nsJSContext::nsJSContext(JSRuntime *aRuntime) : mGCOnDestruction(PR_TRUE)
JSOptionChangedCallback,
this);
::JS_SetBranchCallback(mContext, DOMBranchCallback);
::JS_SetOperationCallback(mContext, DOMOperationCallback,
JS_OPERATION_WEIGHT_BASE);
static JSLocaleCallbacks localeCallbacks =
{
@ -1129,8 +1132,8 @@ nsJSContext::nsJSContext(JSRuntime *aRuntime) : mGCOnDestruction(PR_TRUE)
mNumEvaluations = 0;
mTerminations = nsnull;
mScriptsEnabled = PR_TRUE;
mBranchCallbackCount = 0;
mBranchCallbackTime = LL_ZERO;
mOperationCallbackCount = 0;
mOperationCallbackTime = LL_ZERO;
mProcessingScriptTag = PR_FALSE;
mIsTrackingChromeCodeTime = PR_FALSE;
}
@ -1167,8 +1170,8 @@ nsJSContext::Unlink()
// Clear our entry in the JSContext, bugzilla bug 66413
::JS_SetContextPrivate(mContext, nsnull);
// Clear the branch callback, bugzilla bug 238218
::JS_SetBranchCallback(mContext, nsnull);
// Clear the operation callback, bugzilla bug 238218
::JS_ClearOperationCallback(mContext);
// Unregister our "javascript.options.*" pref-changed callback.
nsContentUtils::UnregisterPrefCallback(js_options_dot_str,
@ -1476,9 +1479,11 @@ nsJSContext::EvaluateString(const nsAString& aScript,
}
// The result of evaluation, used only if there were no errors. This need
// not be a GC root currently, provided we run the GC only from the branch
// callback or from ScriptEvaluated. TODO: use JS_Begin/EndRequest to keep
// the GC from racing with JS execution on any thread.
// not be a GC root currently, provided we run the GC only from the
// operation callback or from ScriptEvaluated.
//
// TODO: use JS_Begin/EndRequest to keep the GC from racing with JS
// execution on any thread.
jsval val;
nsJSContext::TerminationFuncHolder holder(this);
@ -1684,9 +1689,8 @@ nsJSContext::ExecuteScript(void *aScriptObject,
}
// The result of evaluation, used only if there were no errors. This need
// not be a GC root currently, provided we run the GC only from the branch
// callback or from ScriptEvaluated. TODO: use JS_Begin/EndRequest to keep
// the GC from racing with JS execution on any thread.
// not be a GC root currently, provided we run the GC only from the
// operation callback or from ScriptEvaluated.
jsval val;
JSBool ok;
@ -3225,8 +3229,8 @@ nsJSContext::ScriptEvaluated(PRBool aTerminated)
}
#endif
mBranchCallbackCount = 0;
mBranchCallbackTime = LL_ZERO;
mOperationCallbackCount = 0;
mOperationCallbackTime = LL_ZERO;
}
nsresult

Просмотреть файл

@ -279,8 +279,8 @@ private:
PRPackedBool mProcessingScriptTag;
PRPackedBool mIsTrackingChromeCodeTime;
PRUint32 mBranchCallbackCount;
PRTime mBranchCallbackTime;
PRUint32 mOperationCallbackCount;
PRTime mOperationCallbackTime;
PRUint32 mDefaultJSOptions;
// mGlobalWrapperRef is used only to hold a strong reference to the
@ -292,8 +292,7 @@ private:
static int PR_CALLBACK JSOptionChangedCallback(const char *pref, void *data);
static JSBool JS_DLL_CALLBACK DOMBranchCallback(JSContext *cx,
JSScript *script);
static JSBool JS_DLL_CALLBACK DOMOperationCallback(JSContext *cx);
};
class nsIJSRuntimeService;

Просмотреть файл

@ -2105,14 +2105,19 @@ JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, uintN argc,
*/
#define JS_MAX_OPERATION_LIMIT ((uint32) 0x7FFFFFFF)
#define JS_OPERATION_WEIGHT_BASE 4096
/*
* Set the operation callback that the engine calls periodically after
* the internal operation count reaches the specified limit.
*
* When operationLimit is 4096, the callback will be called at least after
* each backward jump in the interpreter. To minimize the overhead of the
* callback invocation we suggest at least 100*4096 as a value for
* operationLimit.
* When operationLimit is JS_OPERATION_WEIGHT_BASE, the callback will be
* called at least after each backward jump in the interpreter. To minimize
* the overhead of the callback invocation we suggest at least
*
* 100 * JS_OPERATION_WEIGHT_BASE
*
* as a value for operationLimit.
*/
extern JS_PUBLIC_API(void)
JS_SetOperationCallback(JSContext *cx, JSOperationCallback callback,

Просмотреть файл

@ -1075,8 +1075,8 @@ extern JSErrorFormatString js_ErrorFormatString[JSErr_Limit];
#define JSOW_SET_PROPERTY 20
#define JSOW_NEW_PROPERTY 200
#define JSOW_DELETE_PROPERTY 30
#define JSOW_ENTER_SHARP 4096
#define JSOW_SCRIPT_JUMP 4096
#define JSOW_ENTER_SHARP JS_OPERATION_WEIGHT_BASE
#define JSOW_SCRIPT_JUMP JS_OPERATION_WEIGHT_BASE
/*
* Reset the operation count and call the operation callback assuming that the

Просмотреть файл

@ -3354,11 +3354,9 @@ public:
NS_DECL_ISUPPORTS
private:
static JSBool JS_DLL_CALLBACK ContextHolderBranchCallback(JSContext *cx,
JSScript *script);
static JSBool JS_DLL_CALLBACK ContextHolderOperationCallback(JSContext *cx);
XPCAutoJSContext mJSContext;
JSBranchCallback mOrigBranchCallback;
JSContext* mOrigCx;
};
@ -3366,38 +3364,49 @@ NS_IMPL_ISUPPORTS0(ContextHolder)
ContextHolder::ContextHolder(JSContext *aOuterCx, JSObject *aSandbox)
: mJSContext(JS_NewContext(JS_GetRuntime(aOuterCx), 1024), JS_FALSE),
mOrigBranchCallback(nsnull),
mOrigCx(aOuterCx)
{
if (mJSContext) {
if(mJSContext)
{
JS_SetOptions(mJSContext,
JSOPTION_DONT_REPORT_UNCAUGHT |
JSOPTION_PRIVATE_IS_NSISUPPORTS);
JS_SetGlobalObject(mJSContext, aSandbox);
JS_SetContextPrivate(mJSContext, this);
// Now cache the original branch callback
mOrigBranchCallback = JS_SetBranchCallback(aOuterCx, nsnull);
JS_SetBranchCallback(aOuterCx, mOrigBranchCallback);
if (mOrigBranchCallback) {
JS_SetBranchCallback(mJSContext, ContextHolderBranchCallback);
if(JS_GetOperationCallback(aOuterCx))
{
JS_SetOperationCallback(mJSContext, ContextHolderOperationCallback,
JS_GetOperationLimit(aOuterCx));
}
}
}
JSBool JS_DLL_CALLBACK
ContextHolder::ContextHolderBranchCallback(JSContext *cx, JSScript *script)
ContextHolder::ContextHolderOperationCallback(JSContext *cx)
{
ContextHolder* thisObject =
static_cast<ContextHolder*>(JS_GetContextPrivate(cx));
NS_ASSERTION(thisObject, "How did that happen?");
if (thisObject->mOrigBranchCallback) {
return (thisObject->mOrigBranchCallback)(thisObject->mOrigCx, script);
JSContext *origCx = thisObject->mOrigCx;
JSOperationCallback callback = JS_GetOperationCallback(origCx);
JSBool ok = JS_TRUE;
if(callback)
{
ok = callback(origCx);
callback = JS_GetOperationCallback(origCx);
if(callback)
{
// If the callback is still set in the original context, reflect
// a possibly updated operation limit into cx.
JS_SetOperationLimit(cx, JS_GetOperationLimit(origCx));
return ok;
}
}
return JS_TRUE;
JS_ClearOperationCallback(cx);
return ok;
}
/***************************************************************************/