From ec10e9a4f41eb909e78444d1ebc35f25e0491c94 Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Tue, 29 Jul 2008 15:19:26 -0700 Subject: [PATCH] Bug 444845, js hooks to control vtune, r=sayrer --- config/autoconf.mk.in | 1 + configure.in | 14 +- dom/src/base/nsJSEnvironment.cpp | 15 +++ js/src/Makefile.in | 6 + js/src/Makefile.ref | 5 + js/src/js.cpp | 14 +- js/src/jsdbgapi.cpp | 120 ++++++++++++++++++ js/src/jsdbgapi.h | 20 +++ .../xpconnect/loader/mozJSComponentLoader.cpp | 8 +- js/src/xpconnect/shell/Makefile.in | 5 + 10 files changed, 205 insertions(+), 3 deletions(-) diff --git a/config/autoconf.mk.in b/config/autoconf.mk.in index 9bd830c336ff..2aef5ff16437 100644 --- a/config/autoconf.mk.in +++ b/config/autoconf.mk.in @@ -106,6 +106,7 @@ MOZ_MEMORY = @MOZ_MEMORY@ MOZ_JPROF = @MOZ_JPROF@ MOZ_SHARK = @MOZ_SHARK@ MOZ_CALLGRIND = @MOZ_CALLGRIND@ +MOZ_VTUNE = @MOZ_VTUNE@ DEHYDRA_PATH = @DEHYDRA_PATH@ MOZ_XPCTOOLS = @MOZ_XPCTOOLS@ diff --git a/configure.in b/configure.in index 6614483b807c..9c3d44059725 100644 --- a/configure.in +++ b/configure.in @@ -6459,13 +6459,24 @@ dnl ======================================================== dnl callgrind dnl ======================================================== MOZ_ARG_ENABLE_BOOL(callgrind, -[ --enable-callgrind Enable callgrind profiling], +[ --enable-callgrind Enable callgrind profiling], MOZ_CALLGRIND=1, MOZ_CALLGRIND= ) if test -n "$MOZ_CALLGRIND"; then AC_DEFINE(MOZ_CALLGRIND) fi +dnl ======================================================== +dnl vtune +dnl ======================================================== +MOZ_ARG_ENABLE_BOOL(vtune, +[ --enable-vtune Enable vtune profiling], + MOZ_VTUNE=1, + MOZ_VTUNE= ) +if test -n "$MOZ_VTUNE"; then + AC_DEFINE(MOZ_VTUNE) +fi + dnl ======================================================== dnl = Enable static checking using gcc-dehydra dnl ======================================================== @@ -7861,6 +7872,7 @@ AC_SUBST(MOZ_LEAKY) AC_SUBST(MOZ_JPROF) AC_SUBST(MOZ_SHARK) AC_SUBST(MOZ_CALLGRIND) +AC_SUBST(MOZ_VTUNE) 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 deef33adbc8a..6cb7d4452a25 100644 --- a/dom/src/base/nsJSEnvironment.cpp +++ b/dom/src/base/nsJSEnvironment.cpp @@ -3114,6 +3114,16 @@ static JSFunctionSpec CallgrindFunctions[] = { }; #endif +#ifdef MOZ_VTUNE +static JSFunctionSpec VtuneFunctions[] = { + {"startVtune", js_StartVtune, 1, 0, 0}, + {"stopVtune", js_StopVtune, 0, 0, 0}, + {"pauseVtune", js_PauseVtune, 0, 0, 0}, + {"resumeVtune", js_ResumeVtune, 0, 0, 0}, + {nsnull, nsnull, 0, 0, 0} +}; +#endif + nsresult nsJSContext::InitClasses(void *aGlobalObj) { @@ -3156,6 +3166,11 @@ nsJSContext::InitClasses(void *aGlobalObj) ::JS_DefineFunctions(mContext, globalObj, CallgrindFunctions); #endif +#ifdef MOZ_VTUNE + // Attempt to initialize Vtune functions + ::JS_DefineFunctions(mContext, globalObj, VtuneFunctions); +#endif + JSOptionChangedCallback(js_options_dot_str, this); return rv; diff --git a/js/src/Makefile.in b/js/src/Makefile.in index 8cb0aa45b15c..f2401209a594 100644 --- a/js/src/Makefile.in +++ b/js/src/Makefile.in @@ -308,6 +308,12 @@ CXXFLAGS += -F/System/Library/PrivateFrameworks LDFLAGS += -F/System/Library/PrivateFrameworks -framework CHUD endif +ifdef MOZ_VTUNE +CXXFLAGS += -IC:/Program\ Files/Intel/VTune/Analyzer/Include +EXTRA_DSO_LDOPTS += C:/Program\ Files/Intel/VTune/Analyzer/Lib/VtuneApi.lib +LIBS += C:/Program\ Files/Intel/VTune/Analyzer/Lib/VtuneApi.lib +endif + LDFLAGS += $(pathsubst -l%,$(NSPR_STATIC_PATH)/%.a,$(NSPR_LIBS)) # BeOS and HP-UX do not require the extra linking of "-lm" diff --git a/js/src/Makefile.ref b/js/src/Makefile.ref index 2f3843c7a0e5..4cf51435b06c 100644 --- a/js/src/Makefile.ref +++ b/js/src/Makefile.ref @@ -97,6 +97,11 @@ endif ifdef MOZ_CALLGRIND DEFINES += -DMOZ_CALLGRIND endif +ifdef MOZ_VTUNE +DEFINES += -DMOZ_VTUNE +CXXFLAGS += -IC:/Program\ Files/Intel/VTune/Analyzer/Include +OTHER_LIBS += C:/Program\ Files/Intel/VTune/Analyzer/Lib/VtuneApi.lib +endif ifndef NO_LIBM LDFLAGS += -lm diff --git a/js/src/js.cpp b/js/src/js.cpp index e3921a9bafc5..fcab85c6e41a 100644 --- a/js/src/js.cpp +++ b/js/src/js.cpp @@ -2834,6 +2834,12 @@ static JSFunctionSpec shell_functions[] = { JS_FS("stopCallgrind", js_StopCallgrind, 0,0,0), JS_FS("dumpCallgrind", js_DumpCallgrind, 1,0,0), #endif +#ifdef MOZ_VTUNE + JS_FS("startVtune", js_StartVtune, 1,0,0), + JS_FS("stopVtune", js_StopVtune, 0,0,0), + JS_FS("pauseVtune", js_PauseVtune, 0,0,0), + JS_FS("resumeVtune", js_ResumeVtune, 0,0,0), +#endif #ifdef DEBUG_ARRAYS JS_FS("arrayInfo", js_ArrayInfo, 1,0,0), #endif @@ -2914,7 +2920,13 @@ static const char *const shell_help_messages[] = { #ifdef MOZ_CALLGRIND "startCallgrind() Start callgrind instrumentation.\n", "stopCallgrind() Stop callgrind instumentation.", -"dumpCallgrind() Dump callgrind counters.\n", +"dumpCallgrind([name]) Dump callgrind counters.\n", +#endif +#ifdef MOZ_VTUNE +"startVtune([filename]) Start vtune instrumentation.\n", +"stopVtune() Stop vtune instumentation.", +"pauseVtune() Pause vtune collection.\n", +"resumeVtune() Resume vtune collection.\n", #endif #ifdef DEBUG_ARRAYS "arrayInfo(a1, a2, ...) Report statistics about arrays.", diff --git a/js/src/jsdbgapi.cpp b/js/src/jsdbgapi.cpp index 7a51b0d75062..065012974d8f 100644 --- a/js/src/jsdbgapi.cpp +++ b/js/src/jsdbgapi.cpp @@ -1843,3 +1843,123 @@ js_DumpCallgrind(JSContext *cx, JSObject *obj, } #endif /* MOZ_CALLGRIND */ + +#ifdef MOZ_VTUNE +#include + +static const char *vtuneErrorMessages[] = { + "unknown, error #0", + "invalid 'max samples' field", + "invalid 'samples per buffer' field", + "invalid 'sample interval' field", + "invalid path", + "sample file in use", + "invalid 'number of events' field", + "unknown, error #7", + "internal error", + "bad event name", + "VTStopSampling called without calling VTStartSampling", + "no events selected for event-based sampling", + "events selected cannot be run together", + "no sampling parameters", + "sample database already exists", + "sampling already started", + "time-based sampling not supported", + "invalid 'sampling parameters size' field", + "invalid 'event size' field", + "sampling file already bound", + "invalid event path", + "invalid license", + "invalid 'global options' field", + +}; + +JS_FRIEND_API(JSBool) +js_StartVtune(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + VTUNE_EVENT events[] = { + { 1000000, 0, 0, 0, "CPU_CLK_UNHALTED.CORE" }, + { 1000000, 0, 0, 0, "INST_RETIRED.ANY" }, + }; + + U32 n_events = sizeof(events) / sizeof(VTUNE_EVENT); + char *default_filename = "mozilla-vtune.tb5"; + JSString *str; + U32 status; + + VTUNE_SAMPLING_PARAMS params = { + sizeof(VTUNE_SAMPLING_PARAMS), + sizeof(VTUNE_EVENT), + 0, 0, /* Reserved fields */ + 1, /* Initialize in "paused" state */ + 0, /* Max samples, or 0 for "continuous" */ + 4096, /* Samples per buffer */ + 0.1, /* Sampling interval in ms */ + 1, /* 1 for event-based sampling, 0 for time-based */ + + n_events, + events, + default_filename, + }; + + if (argc > 0 && JSVAL_IS_STRING(argv[0])) { + str = JSVAL_TO_STRING(argv[0]); + params.tb5Filename = js_DeflateString(cx, + JSSTRING_CHARS(str), + JSSTRING_LENGTH(str)); + } + + status = VTStartSampling(¶ms); + + if (params.tb5Filename != default_filename) + JS_free(cx, params.tb5Filename); + + if (status != 0) { + if (status == VTAPI_MULTIPLE_RUNS) + VTStopSampling(0); + if (status < sizeof(vtuneErrorMessages)) + JS_ReportError(cx, "Vtune setup error: %s", + vtuneErrorMessages[status]); + else + JS_ReportError(cx, "Vtune setup error: %d", + status); + return JS_FALSE; + } + return JS_TRUE; +} + +JS_FRIEND_API(JSBool) +js_StopVtune(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + U32 status = VTStopSampling(1); + if (status) { + if (status < sizeof(vtuneErrorMessages)) + JS_ReportError(cx, "Vtune shutdown error: %s", + vtuneErrorMessages[status]); + else + JS_ReportError(cx, "Vtune shutdown error: %d", + status); + return JS_FALSE; + } + return JS_TRUE; +} + +JS_FRIEND_API(JSBool) +js_PauseVtune(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + VTPause(); + return JS_TRUE; +} + +JS_FRIEND_API(JSBool) +js_ResumeVtune(JSContext *cx, JSObject *obj, + uintN argc, jsval *argv, jsval *rval) +{ + VTResume(); + return JS_TRUE; +} + +#endif /* MOZ_VTUNE */ diff --git a/js/src/jsdbgapi.h b/js/src/jsdbgapi.h index ffbcd09d7a83..88f5bb1581a5 100644 --- a/js/src/jsdbgapi.h +++ b/js/src/jsdbgapi.h @@ -475,6 +475,26 @@ js_DumpCallgrind(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, #endif /* MOZ_CALLGRIND */ +#ifdef MOZ_VTUNE + +extern JS_FRIEND_API(JSBool) +js_StartVtune(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +extern JS_FRIEND_API(JSBool) +js_StopVtune(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +extern JS_FRIEND_API(JSBool) +js_PauseVtune(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +extern JS_FRIEND_API(JSBool) +js_ResumeVtune(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, + jsval *rval); + +#endif /* MOZ_VTUNE */ + JS_END_EXTERN_C #endif /* jsdbgapi_h___ */ diff --git a/js/src/xpconnect/loader/mozJSComponentLoader.cpp b/js/src/xpconnect/loader/mozJSComponentLoader.cpp index 976e632d8df2..cd1ba4c36a39 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" -#if defined(MOZ_SHARK) || defined(MOZ_CALLGRIND) +#if defined(MOZ_SHARK) || defined(MOZ_CALLGRIND) || defined(MOZ_VTUNE) #include "jsdbgapi.h" #endif @@ -286,6 +286,12 @@ static JSFunctionSpec gGlobalFun[] = { {"startCallgrind", js_StartCallgrind, 0,0,0}, {"stopCallgrind", js_StopCallgrind, 0,0,0}, {"dumpCallgrind", js_DumpCallgrind, 1,0,0}, +#endif +#ifdef MOZ_VTUNE + {"startVtune", js_StartVtune, 1,0,0}, + {"stopVtune", js_StopVtune, 0,0,0}, + {"pauseVtune", js_PauseVtune, 0,0,0}, + {"resumeVtune", js_ResumeVtune, 0,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 58111ccea569..bfff6c3cd9a9 100644 --- a/js/src/xpconnect/shell/Makefile.in +++ b/js/src/xpconnect/shell/Makefile.in @@ -77,6 +77,11 @@ endif ifdef MOZ_CALLGRIND DEFINES += -DMOZ_CALLGRIND endif +ifdef MOZ_VTUNE +DEFINES += -DMOZ_VTUNE +CXXFLAGS += -IC:/Program\ Files/Intel/VTune/Analyzer/Include +LIBS += C:/Program\ Files/Intel/VTune/Analyzer/Lib/VtuneApi.lib +endif # # Line editing support. If your OS supplies the readline library, define # JS_READLINE to get line editing in the xpcshell.