From 7c4aa69e9d2cd4b0d0c10ad03c7059a99d3cea62 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Tue, 8 Jul 2008 15:58:08 -0700 Subject: [PATCH] Bug 444023, Add JS functions to stop/start callgrind, r=sayrer --- config/autoconf.mk.in | 1 + configure.in | 12 +++++ dom/src/base/nsJSEnvironment.cpp | 14 ++++++ js/src/Makefile.ref | 3 ++ js/src/js.cpp | 10 +++++ js/src/jsdbgapi.cpp | 44 +++++++++++++++++++ js/src/jsdbgapi.h | 16 +++++++ .../xpconnect/loader/mozJSComponentLoader.cpp | 7 ++- js/src/xpconnect/shell/Makefile.in | 4 +- js/src/xpconnect/shell/xpcshell.cpp | 7 ++- 10 files changed, 115 insertions(+), 3 deletions(-) diff --git a/config/autoconf.mk.in b/config/autoconf.mk.in index fe7afa2858b..e01cf550ae1 100644 --- a/config/autoconf.mk.in +++ b/config/autoconf.mk.in @@ -103,6 +103,7 @@ MOZ_LEAKY = @MOZ_LEAKY@ MOZ_MEMORY = @MOZ_MEMORY@ MOZ_JPROF = @MOZ_JPROF@ MOZ_SHARK = @MOZ_SHARK@ +MOZ_CALLGRIND = @MOZ_CALLGRIND@ DEHYDRA_PATH = @DEHYDRA_PATH@ MOZ_XPCTOOLS = @MOZ_XPCTOOLS@ diff --git a/configure.in b/configure.in index 7a262c8c90d..4f0f0cf1223 100644 --- a/configure.in +++ b/configure.in @@ -6409,6 +6409,17 @@ if test -n "$MOZ_SHARK"; then AC_DEFINE(MOZ_SHARK) fi +dnl ======================================================== +dnl callgrind +dnl ======================================================== +MOZ_ARG_ENABLE_BOOL(callgrind, +[ --enable-callgrind Enable callgrind profiling], + MOZ_CALLGRIND=1, + MOZ_CALLGRIND= ) +if test -n "$MOZ_CALLGRIND"; then + AC_DEFINE(MOZ_CALLGRIND) +fi + dnl ======================================================== dnl = Enable static checking using gcc-dehydra dnl ======================================================== @@ -7801,6 +7812,7 @@ AC_SUBST(MOZ_LOG_REFCNT) AC_SUBST(MOZ_LEAKY) AC_SUBST(MOZ_JPROF) AC_SUBST(MOZ_SHARK) +AC_SUBST(MOZ_CALLGRIND) AC_SUBST(MOZ_XPCTOOLS) AC_SUBST(MOZ_JSLOADER) AC_SUBST(MOZ_USE_NATIVE_UCONV) diff --git a/dom/src/base/nsJSEnvironment.cpp b/dom/src/base/nsJSEnvironment.cpp index da644b1e816..8766aef20e8 100644 --- a/dom/src/base/nsJSEnvironment.cpp +++ b/dom/src/base/nsJSEnvironment.cpp @@ -3107,6 +3107,15 @@ static JSFunctionSpec SharkFunctions[] = { }; #endif +#ifdef MOZ_CALLGRIND +static JSFunctionSpec CallgrindFunctions[] = { + {"startCallgrind", js_StartCallgrind, 0, 0, 0}, + {"stopCallgrind", js_StopCallgrind, 0, 0, 0}, + {"dumpCallgrind", js_DumpCallgrind, 1, 0, 0}, + {nsnull, nsnull, 0, 0, 0} +}; +#endif + nsresult nsJSContext::InitClasses(void *aGlobalObj) { @@ -3144,6 +3153,11 @@ nsJSContext::InitClasses(void *aGlobalObj) ::JS_DefineFunctions(mContext, globalObj, SharkFunctions); #endif +#ifdef MOZ_CALLGRIND + // Attempt to initialize Callgrind functions + ::JS_DefineFunctions(mContext, globalObj, CallgrindFunctions); +#endif + JSOptionChangedCallback(js_options_dot_str, this); return rv; diff --git a/js/src/Makefile.ref b/js/src/Makefile.ref index fb14d1611fa..0b52e3e8437 100644 --- a/js/src/Makefile.ref +++ b/js/src/Makefile.ref @@ -94,6 +94,9 @@ DEFINES += -DMOZ_SHARK CFLAGS += -F/System/Library/PrivateFrameworks LDFLAGS += -F/System/Library/PrivateFrameworks -framework CHUD endif +ifdef MOZ_CALLGRIND +DEFINES += -DMOZ_CALLGRIND +endif ifndef NO_LIBM LDFLAGS += -lm diff --git a/js/src/js.cpp b/js/src/js.cpp index f43ff3e1fca..3d7d6b97ece 100644 --- a/js/src/js.cpp +++ b/js/src/js.cpp @@ -2824,6 +2824,11 @@ static JSFunctionSpec shell_functions[] = { JS_FS("connectShark", js_ConnectShark, 0,0,0), JS_FS("disconnectShark", js_DisconnectShark, 0,0,0), #endif +#ifdef MOZ_CALLGRIND + JS_FS("startCallgrind", js_StartCallgrind, 0,0,0), + JS_FS("stopCallgrind", js_StopCallgrind, 0,0,0), + JS_FS("dumpCallgrind", js_DumpCallgrind, 1,0,0), +#endif #ifdef DEBUG_ARRAYS JS_FS("arrayInfo", js_ArrayInfo, 1,0,0), #endif @@ -2901,6 +2906,11 @@ static const char *const shell_help_messages[] = { " The -k switch does this automatically.", "disconnectShark() Disconnect from Shark.", #endif +#ifdef MOZ_CALLGRIND +"startCallgrind() Start callgrind instrumentation.\n", +"stopCallgrind() Stop callgrind instumentation.", +"dumpCallgrind() Dump callgrind counters.\n", +#endif #ifdef DEBUG_ARRAYS "arrayInfo(a1, a2, ...) Report statistics about arrays.", #endif diff --git a/js/src/jsdbgapi.cpp b/js/src/jsdbgapi.cpp index 7309ebbabf5..7a51b0d7506 100644 --- a/js/src/jsdbgapi.cpp +++ b/js/src/jsdbgapi.cpp @@ -1799,3 +1799,47 @@ js_DisconnectShark(JSContext *cx, JSObject *obj, } #endif /* MOZ_SHARK */ + +#ifdef MOZ_CALLGRIND + +#include + +JS_FRIEND_API(JSBool) +js_StartCallgrind(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + CALLGRIND_START_INSTRUMENTATION; + CALLGRIND_ZERO_STATS; + return JS_TRUE; +} + +JS_FRIEND_API(JSBool) +js_StopCallgrind(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + CALLGRIND_STOP_INSTRUMENTATION; + return JS_TRUE; +} + +JS_FRIEND_API(JSBool) +js_DumpCallgrind(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + JSString *str; + char *cstr; + + if (argc > 0 && JSVAL_IS_STRING(argv[0])) { + str = JSVAL_TO_STRING(argv[0]); + cstr = js_DeflateString(cx, JSSTRING_CHARS(str), JSSTRING_LENGTH(str)); + if (cstr) { + CALLGRIND_DUMP_STATS_AT(cstr); + JS_free(cx, cstr); + return JS_TRUE; + } + } + CALLGRIND_DUMP_STATS; + + return JS_TRUE; +} + +#endif /* MOZ_CALLGRIND */ diff --git a/js/src/jsdbgapi.h b/js/src/jsdbgapi.h index 2b0ebac8328..ffbcd09d7a8 100644 --- a/js/src/jsdbgapi.h +++ b/js/src/jsdbgapi.h @@ -459,6 +459,22 @@ js_DisconnectShark(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, #endif /* MOZ_SHARK */ +#ifdef MOZ_CALLGRIND + +extern JS_FRIEND_API(JSBool) +js_StopCallgrind(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +extern JS_FRIEND_API(JSBool) +js_StartCallgrind(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +extern JS_FRIEND_API(JSBool) +js_DumpCallgrind(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +#endif /* MOZ_CALLGRIND */ + JS_END_EXTERN_C #endif /* jsdbgapi_h___ */ diff --git a/js/src/xpconnect/loader/mozJSComponentLoader.cpp b/js/src/xpconnect/loader/mozJSComponentLoader.cpp index f4cb868e604..2535e331c5c 100644 --- a/js/src/xpconnect/loader/mozJSComponentLoader.cpp +++ b/js/src/xpconnect/loader/mozJSComponentLoader.cpp @@ -83,7 +83,7 @@ #include "prmem.h" #include "plbase64.h" -#ifdef MOZ_SHARK +#if defined(MOZ_SHARK) || defined(MOZ_CALLGRIND) #include "jsdbgapi.h" #endif @@ -281,6 +281,11 @@ static JSFunctionSpec gGlobalFun[] = { {"stopShark", js_StopShark, 0,0,0}, {"connectShark", js_ConnectShark, 0,0,0}, {"disconnectShark", js_DisconnectShark,0,0,0}, +#endif +#ifdef MOZ_CALLGRIND + {"startCallgrind", js_StartCallgrind, 0,0,0}, + {"stopCallgrind", js_StopCallgrind, 0,0,0}, + {"dumpCallgrind", js_DumpCallgrind, 1,0,0}, #endif {nsnull,nsnull,0,0,0} }; diff --git a/js/src/xpconnect/shell/Makefile.in b/js/src/xpconnect/shell/Makefile.in index 360c1c5a089..58111ccea56 100644 --- a/js/src/xpconnect/shell/Makefile.in +++ b/js/src/xpconnect/shell/Makefile.in @@ -74,7 +74,9 @@ DEFINES += -DMOZ_SHARK CFLAGS += -F/System/Library/PrivateFrameworks LDFLAGS += -F/System/Library/PrivateFrameworks -framework CHUD endif - +ifdef MOZ_CALLGRIND +DEFINES += -DMOZ_CALLGRIND +endif # # Line editing support. If your OS supplies the readline library, define # JS_READLINE to get line editing in the xpcshell. diff --git a/js/src/xpconnect/shell/xpcshell.cpp b/js/src/xpconnect/shell/xpcshell.cpp index eb0e8e2f943..69a4d65e922 100644 --- a/js/src/xpconnect/shell/xpcshell.cpp +++ b/js/src/xpconnect/shell/xpcshell.cpp @@ -84,7 +84,7 @@ #include "nsIJSContextStack.h" -#ifdef MOZ_SHARK +#if defined(MOZ_SHARK) || defined(MOZ_CALLGRIND) #include "jsdbgapi.h" #endif @@ -438,6 +438,11 @@ static JSFunctionSpec glob_functions[] = { {"stopShark", js_StopShark, 0,0,0}, {"connectShark", js_ConnectShark, 0,0,0}, {"disconnectShark", js_DisconnectShark, 0,0,0}, +#endif +#ifdef MOZ_CALLGRIND + {"startCallgrind", js_StartCallgrind, 0,0,0}, + {"stopCallgrind", js_StopCallgrind, 0,0,0}, + {"dumpCallgrind", js_DumpCallgrind, 1,0,0}, #endif {nsnull,nsnull,0,0,0} };