From dacf41392f8a2129caeed3d4c139f3b7bc6c05da Mon Sep 17 00:00:00 2001 From: "syd%netscape.com" Date: Thu, 13 Jul 2000 05:19:22 +0000 Subject: [PATCH] Add support to JS profiler for collecting min, max, and average execution times of functions. Doesn't yet handle rentrancy. Create Makefile.in files so this stuff will build on *nix. r=jband, a=leaf --- js/src/Makefile.in | 2 +- js/src/xpconnect/Makefile.in | 5 ++ js/src/xpconnect/src/Makefile.in | 6 +- js/src/xpconnect/tools/Makefile.in | 44 ++++++++++++ js/src/xpconnect/tools/idl/Makefile.in | 48 +++++++++++++ js/src/xpconnect/tools/src/Makefile.in | 72 +++++++++++++++++++ .../tools/src/nsXPCToolsProfiler.cpp | 47 +++++++++--- js/src/xpconnect/tools/src/xpctools_private.h | 23 ++++++ 8 files changed, 235 insertions(+), 12 deletions(-) create mode 100644 js/src/xpconnect/tools/Makefile.in create mode 100644 js/src/xpconnect/tools/idl/Makefile.in create mode 100644 js/src/xpconnect/tools/src/Makefile.in diff --git a/js/src/Makefile.in b/js/src/Makefile.in index 358452be917e..253655613b39 100644 --- a/js/src/Makefile.in +++ b/js/src/Makefile.in @@ -180,7 +180,7 @@ JSJAVA_CFLAGS = \ include $(topsrcdir)/config/rules.mk -DEFINES += -DEXPORT_JS_API +DEFINES += -DEXPORT_JS_API INCLUDES += -I$(srcdir) diff --git a/js/src/xpconnect/Makefile.in b/js/src/xpconnect/Makefile.in index 2707c55ba5a8..4ac86f244849 100644 --- a/js/src/xpconnect/Makefile.in +++ b/js/src/xpconnect/Makefile.in @@ -44,5 +44,10 @@ ifdef ENABLE_TESTS DIRS += tests shell endif +ifdef MOZ_XPC_TOOLS_SUPPORT +DIRS += tools +endif + + include $(topsrcdir)/config/rules.mk diff --git a/js/src/xpconnect/src/Makefile.in b/js/src/xpconnect/src/Makefile.in index b1bda1e67456..6b353eae90ab 100644 --- a/js/src/xpconnect/src/Makefile.in +++ b/js/src/xpconnect/src/Makefile.in @@ -89,5 +89,9 @@ EXTRA_DSO_LDOPTS += \ include $(topsrcdir)/config/rules.mk -DEFINES += -DJSFILE -DJS_THREADSAFE +DEFINES += -DJSFILE -DJS_THREADSAFE + +ifdef MOZ_XPC_TOOLS_SUPPORT +DEFINES += -DXPC_TOOLS_SUPPORT +endif diff --git a/js/src/xpconnect/tools/Makefile.in b/js/src/xpconnect/tools/Makefile.in new file mode 100644 index 000000000000..77be140c88fd --- /dev/null +++ b/js/src/xpconnect/tools/Makefile.in @@ -0,0 +1,44 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is Mozilla Communicator client code, released +# March 31, 1998. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the +# terms of the GNU Public License (the "GPL"), in which case the +# provisions of the GPL are applicable instead of those above. +# If you wish to allow use of your version of this file only +# under the terms of the GPL and not to allow others to use your +# version of this file under the NPL, indicate your decision by +# deleting the provisions above and replace them with the notice +# and other provisions required by the GPL. If you do not delete +# the provisions above, a recipient may use your version of this +# file under either the NPL or the GPL. +# + +DEPTH = ../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +DIRS = idl src + +include $(topsrcdir)/config/rules.mk + diff --git a/js/src/xpconnect/tools/idl/Makefile.in b/js/src/xpconnect/tools/idl/Makefile.in new file mode 100644 index 000000000000..11b82af96f1d --- /dev/null +++ b/js/src/xpconnect/tools/idl/Makefile.in @@ -0,0 +1,48 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is Mozilla Communicator client code, released +# March 31, 1998. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1999 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# +# Alternatively, the contents of this file may be used under the +# terms of the GNU Public License (the "GPL"), in which case the +# provisions of the GPL are applicable instead of those above. +# If you wish to allow use of your version of this file only +# under the terms of the GPL and not to allow others to use your +# version of this file under the NPL, indicate your decision by +# deleting the provisions above and replace them with the notice +# and other provisions required by the GPL. If you do not delete +# the provisions above, a recipient may use your version of this +# file under either the NPL or the GPL. +# + +DEPTH = ../../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = xpctools + +XPIDLSRCS = \ + nsIXPCToolsCompiler.idl \ + nsIXPCToolsProfiler.idl \ + $(NULL) + +include $(topsrcdir)/config/rules.mk diff --git a/js/src/xpconnect/tools/src/Makefile.in b/js/src/xpconnect/tools/src/Makefile.in new file mode 100644 index 000000000000..261e93bb74fe --- /dev/null +++ b/js/src/xpconnect/tools/src/Makefile.in @@ -0,0 +1,72 @@ +# +# The contents of this file are subject to the Netscape Public +# License Version 1.1 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.mozilla.org/NPL/ +# +# Software distributed under the License is distributed on an "AS +# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr +# implied. See the License for the specific language governing +# rights and limitations under the License. +# +# The Original Code is Mozilla Communicator client code, released +# March 31, 1998. +# +# The Initial Developer of the Original Code is Netscape +# Communications Corporation. Portions created by Netscape are +# Copyright (C) 1998 Netscape Communications Corporation. All +# Rights Reserved. +# +# Contributor(s): +# John Bandhauer +# +# Alternatively, the contents of this file may be used under the +# terms of the GNU Public License (the "GPL"), in which case the +# provisions of the GPL are applicable instead of those above. +# If you wish to allow use of your version of this file only +# under the terms of the GPL and not to allow others to use your +# version of this file under the NPL, indicate your decision by +# deleting the provisions above and replace them with the notice +# and other provisions required by the GPL. If you do not delete +# the provisions above, a recipient may use your version of this +# file under either the NPL or the GPL. +# + +DEPTH = ../../../../.. +topsrcdir = @top_srcdir@ +srcdir = @srcdir@ +VPATH = @srcdir@ + +include $(DEPTH)/config/autoconf.mk + +MODULE = xpctools +LIBRARY_NAME = xpctools +SHORT_LIBNAME = xpctools + +CPPSRCS = \ + nsXPCToolsCompiler.o \ + nsXPCToolsProfiler.o \ + nsXPCToolsModule.o \ + $(NULL) + +include $(topsrcdir)/config/config.mk + +IS_COMPONENT = 1 + +# this is automatically discovered under BeOS +ifneq ($(OS_ARCH),BeOS) +LIBS += \ + $(MOZ_JS_LIBS) \ + $(MOZ_COMPONENT_LIBS) \ + $(NULL) +endif + +EXTRA_DSO_LDOPTS += \ + $(MOZ_COMPONENT_LIBS) \ + $(MOZ_JS_LIBS) \ + $(NULL) + +include $(topsrcdir)/config/rules.mk + +DEFINES += -DJSFILE -DJS_THREADSAFE + diff --git a/js/src/xpconnect/tools/src/nsXPCToolsProfiler.cpp b/js/src/xpconnect/tools/src/nsXPCToolsProfiler.cpp index e7b95a271f80..f1bb1db63d98 100644 --- a/js/src/xpconnect/tools/src/nsXPCToolsProfiler.cpp +++ b/js/src/xpconnect/tools/src/nsXPCToolsProfiler.cpp @@ -111,7 +111,11 @@ ProfilerFunction::ProfilerFunction(const char* name, mLineExtent(extent), mFile(file), mCallCount(0), - mCompileCount(0) + mCompileCount(0), + mQuickTime((PRUint32) -1), + mLongTime(0), + mStartTime(0), + mSum(0) { // empty } @@ -241,7 +245,6 @@ xpctools_InterpreterHook(JSContext *cx, JSStackFrame *fp, JSBool before, JSBool *ok, void *closure) { // ignore returns - NS_ASSERTION(before, "engine should not do this cuz we return nsnull!"); NS_ASSERTION(fp, "bad frame pointer!"); JSScript* script = fp->script; @@ -255,10 +258,20 @@ xpctools_InterpreterHook(JSContext *cx, JSStackFrame *fp, JSBool before, ProfilerFunction* fun = (ProfilerFunction*) self->mScriptTable->Get(&scriptkey); if(fun) - fun->IncrementCallCount(); + { + if(before == PR_TRUE) + { + fun->IncrementCallCount(); + fun->SetStartTime(); + } + else + { + fun->SetEndTime(); + } + } } } - return nsnull; + return closure; } /***************************************************************************/ @@ -308,15 +321,29 @@ xpctools_FuncionNamePrinter(nsHashKey *aKey, void *aData, void* closure) ProfilerFunction* fun = (ProfilerFunction*) aData; FILE* out = (FILE*) closure; const char* name = fun->GetName(); + PRUint32 average; + PRUint32 count; + + count = fun->GetCallCount(); + if (count != 0) + average = fun->GetSum() / count; if(!name) name = ""; fprintf(out, - " [%lu,%lu] %s() {%d-%d}\n", - (unsigned long) fun->GetCompileCount(), - (unsigned long) fun->GetCallCount(), - name, - (int) fun->GetBaseLineNumber(), - (int)(fun->GetBaseLineNumber()+fun->GetLineExtent()-1)); + " [%lu,%lu] %s() {%d-%d} ", + (unsigned long) fun->GetCompileCount(), + (unsigned long) fun->GetCallCount(), + name, + (int) fun->GetBaseLineNumber(), + (int)(fun->GetBaseLineNumber()+fun->GetLineExtent()-1)); + if(count != 0) + fprintf(out, + "{min %lu, max %lu avg %lu}\n", + (unsigned long) fun->GetQuickTime(), + (unsigned long) fun->GetLongTime(), + (unsigned long) average); + else + fprintf(out, "\n" ); return PR_TRUE; } diff --git a/js/src/xpconnect/tools/src/xpctools_private.h b/js/src/xpconnect/tools/src/xpctools_private.h index 0696048448d0..19dec258dc27 100644 --- a/js/src/xpconnect/tools/src/xpctools_private.h +++ b/js/src/xpconnect/tools/src/xpctools_private.h @@ -52,6 +52,7 @@ #include "jsapi.h" #include "jshash.h" #include "jsprf.h" +#include "jslong.h" #include "jsinterp.h" #include "jscntxt.h" #include "jsdbgapi.h" @@ -114,8 +115,26 @@ public: uintN GetLineExtent() const {return mLineExtent;} void IncrementCallCount() {++mCallCount;} PRUint32 GetCallCount() {return mCallCount;} + PRUint32 GetSum() {return mSum;} void IncrementCompileCount() {++mCompileCount;} PRUint32 GetCompileCount() {return mCompileCount;} + PRUint32 NowInMilliSecs() + {PRUint64 now64; + PRUint32 now32; + JSLL_DIV(now64,PR_Now(),JSLL_INIT(0,1000)); + JSLL_L2UI(now32, now64); + return now32; + } + void SetStartTime() {mStartTime = NowInMilliSecs();} + void SetEndTime() + {PRUint32 delta = NowInMilliSecs() - mStartTime; + if(delta < mQuickTime) + mQuickTime = delta; + if (delta > mLongTime) + mLongTime = delta; + mSum += delta;} + PRUint32 GetQuickTime() {return mQuickTime;} + PRUint32 GetLongTime() {return mLongTime;} ProfilerFunction(); // not implemented @@ -126,6 +145,10 @@ private: ProfilerFile* mFile; PRUint32 mCallCount; PRUint32 mCompileCount; + PRUint32 mQuickTime; // quickest delta in msec + PRUint32 mLongTime; // longest delta in msec + PRUint32 mStartTime; // time on enter + PRUint32 mSum; };