changeset: 28924:22f1d0260b34 tag: qtip tag: tip tag: taras_component.diff user: Taras Glek date: Thu Jun 25 15:19:38 2009 -0700 summary: >diff --git a/dom/src/base/nsJSEnvironment.cpp b/dom/src/base/nsJSEnvironment.cpp diff --git a/xpcom/glue/Makefile.in b/xpcom/glue/Makefile.in --- a/xpcom/glue/Makefile.in +++ b/xpcom/glue/Makefile.in @@ -103,6 +103,7 @@ SDK_HEADERS = \ nsIWeakReferenceUtils.h \ nsInterfaceHashtable.h \ nsMemory.h \ + measureroftime.h \ nsQuickSort.h \ nsRefPtrHashtable.h \ nsServiceManagerUtils.h \ diff --git a/xpcom/glue/measureroftime.cpp b/xpcom/glue/measureroftime.cpp new file mode 100644 --- /dev/null +++ b/xpcom/glue/measureroftime.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include "measureroftime.h" +#include "prtypes.h" +#include "prthread.h" +#include "nsDebug.h" + + +static const PRUintn BAD_TLS_INDEX = (PRUintn) -1; +static PRUintn gTLSIndex = BAD_TLS_INDEX; + +static void ThreadDestruct( void *data ) +{ + if (data) + delete (MeasurerOfTime::TLS *)data; +} + +static MeasurerOfTime::TLS *GetTLS() +{ + if (gTLSIndex == BAD_TLS_INDEX) { + PRStatus status = PR_NewThreadPrivateIndex( &gTLSIndex, ThreadDestruct ); + NS_ABORT_IF_FALSE(status==0, "MeasurerOfTime could not allocate TLS storage."); + } + MeasurerOfTime::TLS *data = (MeasurerOfTime::TLS*)PR_GetThreadPrivate(gTLSIndex); + if (data == nsnull) { + // First request for this thread - allocate it. + data = new MeasurerOfTime::TLS(); + PR_SetThreadPrivate(gTLSIndex, data); + } + return data; +} + +MeasurerOfTime::MeasurerOfTime(const char *what, bool autodump): + what(what), tls(GetTLS()), autodump(autodump) +{ + gettimeofday(&startTime, NULL); + ++(tls->indent); +} + +#ifdef _X86_ +#define INTERVAL 0.01 +#else +#define INTERVAL 0.1 +#endif +//adds cpu + system time to get real time +void MeasurerOfTime::dumpAndReset() { + struct timeval endTime; + gettimeofday(&endTime, NULL); + float diff = (endTime.tv_usec - startTime.tv_usec + + static_cast(endTime.tv_sec - startTime.tv_sec)*1000*1000)/1000; + + if (autodump || diff >= INTERVAL) { + struct rusage usage; + getrusage(RUSAGE_SELF,&usage); + // get total clock time from start of execution + unsigned long msSinceStart = (usage.ru_utime.tv_sec + usage.ru_stime.tv_sec)*1000 + + (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec)/1000; + + char buf[20]; + const unsigned int len = PR_MIN (sizeof(buf)-1, tls->indent); + for(unsigned int i =0;iindent); +} diff --git a/xpcom/glue/measureroftime.h b/xpcom/glue/measureroftime.h new file mode 100644 --- /dev/null +++ b/xpcom/glue/measureroftime.h @@ -0,0 +1,22 @@ +#ifndef MEASURER_OF_TIME +#define MEASURER_OF_TIME +#include + +/* measures in ms */ +struct MeasurerOfTime{ + struct TLS { + TLS(): indent(0) {} + unsigned int indent; + }; + + MeasurerOfTime(const char *what, bool autodump = false); + void dumpAndReset(); + ~MeasurerOfTime(); + + + struct timeval startTime; + const char *what; + TLS *tls; + bool autodump; +}; +#endif diff --git a/xpcom/glue/objs.mk b/xpcom/glue/objs.mk --- a/xpcom/glue/objs.mk +++ b/xpcom/glue/objs.mk @@ -55,6 +55,7 @@ XPCOM_GLUE_SRC_LCPPSRCS = \ nsISupportsImpl.cpp \ nsMemory.cpp \ nsWeakReference.cpp \ + measureroftime.cpp \ nsGREGlue.cpp \ nsVersionComparator.cpp \ nsTHashtable.cpp \