1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#include "nsTimelineService.h"
53
static PRIntervalTime initInterval = 0;
56
static PRFileDesc *timelineFD = PR_STDERR;
57
static PRBool gTimelineDisabled = PR_TRUE;
59
// Notes about threading:
60
// We avoid locks as we always use thread-local-storage.
61
// This means every other thread has its own private copy of
62
// data, and this thread can't re-enter (as our implemenation
63
// doesn't call back out anywhere). Thus, we can avoid locks!
65
static const PRUintn BAD_TLS_INDEX = (PRUintn) -1;
66
static PRUintn gTLSIndex = BAD_TLS_INDEX;
68
class TimelineThreadData {
70
TimelineThreadData() : initTime(0), indent(0),
71
disabled(PR_TRUE), timers(nsnull) {}
72
~TimelineThreadData() {if (timers) PL_HashTableDestroy(timers);}
79
/* Implementation file */
80
NS_IMPL_THREADSAFE_ISUPPORTS1(nsTimelineService, nsITimelineService)
82
static PRTime Now(void);
85
* Timer structure stored in a hash table to keep track of named
88
class nsTimelineServiceTimer {
90
nsTimelineServiceTimer();
91
~nsTimelineServiceTimer();
95
* Caller passes in "now" rather than having us calculate it so
96
* that we can avoid including timer overhead in the time being
99
void stop(PRTime now);
102
PRTime getAccum(PRTime now);
108
PRThread *mOwnerThread; // only used for asserts - could be #if MOZ_DEBUG
111
#define TIMER_CHECK_OWNER() \
112
NS_ABORT_IF_FALSE(PR_GetCurrentThread() == mOwnerThread, \
113
"Timer used by non-owning thread")
116
nsTimelineServiceTimer::nsTimelineServiceTimer()
117
: mAccum(LL_ZERO), mStart(LL_ZERO), mRunning(0),
118
mOwnerThread(PR_GetCurrentThread())
122
nsTimelineServiceTimer::~nsTimelineServiceTimer()
126
void nsTimelineServiceTimer::start()
135
void nsTimelineServiceTimer::stop(PRTime now)
141
LL_SUB(delta, now, mStart);
142
LL_ADD(accum, mAccum, delta);
147
void nsTimelineServiceTimer::reset()
154
PRTime nsTimelineServiceTimer::getAccum()
163
LL_SUB(delta, Now(), mStart);
164
LL_ADD(accum, mAccum, delta);
169
PRTime nsTimelineServiceTimer::getAccum(PRTime now)
178
LL_SUB(delta, now, mStart);
179
LL_ADD(accum, mAccum, delta);
186
* PR_Now() on the Mac only gives us a resolution of seconds. Using
187
* PR_IntervalNow() gives us better resolution. with the drawback that
188
* the timeline is only good for about six hours.
190
* PR_IntervalNow() occasionally exhibits discontinuities on Windows,
191
* so we only use it on the Mac. Bleah!
193
static PRTime Now(void)
195
PRIntervalTime numTicks = PR_IntervalNow() - initInterval;
197
LL_ADD(now, initTime, PR_IntervalToMilliseconds(numTicks) * 1000);
201
static PRTime Now(void)
207
static TimelineThreadData *GetThisThreadData()
209
NS_ABORT_IF_FALSE(gTLSIndex!=BAD_TLS_INDEX, "Our TLS not initialized");
210
TimelineThreadData *new_data = nsnull;
211
TimelineThreadData *data = (TimelineThreadData *)PR_GetThreadPrivate(gTLSIndex);
212
if (data == nsnull) {
213
// First request for this thread - allocate it.
214
new_data = new TimelineThreadData();
219
new_data->timers = PL_NewHashTable(100, PL_HashString, PL_CompareStrings,
220
PL_CompareValues, NULL, NULL);
221
if (new_data->timers==NULL)
223
new_data->initTime = PR_Now();
224
NS_WARN_IF_FALSE(!gTimelineDisabled,
225
"Why are we creating new state when disabled?");
226
new_data->disabled = PR_FALSE;
229
PR_SetThreadPrivate(gTLSIndex, data);
232
if (new_data) // eeek - error during creation!
234
NS_WARN_IF_FALSE(data, "TimelineService could not get thread-local data");
239
static void ThreadDestruct (void *data);
240
static PRStatus TimelineInit(void);
243
void ThreadDestruct( void *data )
246
delete (TimelineThreadData *)data;
250
* PRCallOnceFN that initializes stuff for the timing service.
252
static PRCallOnceType initonce;
254
PRStatus TimelineInit(void)
262
PRStatus status = PR_NewThreadPrivateIndex( &gTLSIndex, ThreadDestruct );
263
NS_WARN_IF_FALSE(status==0, "TimelineService could not allocate TLS storage.");
265
timeStr = PR_GetEnv("NS_TIMELINE_INIT_TIME");
267
initInterval = PR_IntervalNow();
269
// NS_TIMELINE_INIT_TIME only makes sense for the main thread, so if it
270
// exists, set it there. If not, let normal thread management code take
271
// care of setting the init time.
272
if (timeStr != NULL && 2 == PR_sscanf(timeStr, "%d.%d", &secs, &msecs)) {
273
PRTime &initTime = GetThisThreadData()->initTime;
274
LL_MUL(tmp1, (PRInt64)secs, 1000000);
275
LL_MUL(tmp2, (PRInt64)msecs, 1000);
276
LL_ADD(initTime, tmp1, tmp2);
278
initInterval -= PR_MicrosecondsToInterval(
279
(PRUint32)(PR_Now() - initTime));
284
fileName = "timeline.txt";
286
fileName = PR_GetEnv("NS_TIMELINE_LOG_FILE");
289
&& (fd = PR_Open(fileName, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE,
293
"NOTE: due to asynchrony, the indentation that you see does"
294
" not necessarily correspond to nesting in the code.\n\n");
297
// Runtime disable of timeline
298
if (PR_GetEnv("NS_TIMELINE_ENABLE"))
299
gTimelineDisabled = PR_FALSE;
303
static void ParseTime(PRTime tm, PRInt32& secs, PRInt32& msecs)
305
PRTime llsecs, llmsecs, tmp;
307
LL_DIV(llsecs, tm, 1000000);
308
LL_MOD(tmp, tm, 1000000);
309
LL_DIV(llmsecs, tmp, 1000);
311
LL_L2I(secs, llsecs);
312
LL_L2I(msecs, llmsecs);
315
static char *Indent(char *buf)
317
int &indent = GetThisThreadData()->indent;
319
if (amount > MAXINDENT) {
325
PR_Write(timelineFD, "indent underflow!\n", 18);
333
static void PrintTime(PRTime tm, const char *text, va_list args)
336
char pbuf[550], *pc, tbuf[550];
338
ParseTime(tm, secs, msecs);
340
// snprintf/write rather than fprintf because we don't want
341
// messages from multiple threads to garble one another.
343
PR_vsnprintf(pc, sizeof pbuf - (pc - pbuf), text, args);
344
PR_snprintf(tbuf, sizeof tbuf, "%05d.%03d (%08p): %s\n",
345
secs, msecs, PR_GetCurrentThread(), pbuf);
346
PR_Write(timelineFD, tbuf, strlen(tbuf));
350
* Make this public if we need it.
352
static nsresult NS_TimelineMarkV(const char *text, va_list args)
356
PR_CallOnce(&initonce, TimelineInit);
358
TimelineThreadData *thread = GetThisThreadData();
361
LL_SUB(elapsed, tmp, thread->initTime);
363
PrintTime(elapsed, text, args);
368
PR_IMPLEMENT(nsresult) NS_TimelineForceMark(const char *text, ...)
371
va_start(args, text);
372
NS_TimelineMarkV(text, args);
377
PR_IMPLEMENT(nsresult) NS_TimelineMark(const char *text, ...)
380
va_start(args, text);
382
PR_CallOnce(&initonce, TimelineInit);
384
if (gTimelineDisabled)
385
return NS_ERROR_NOT_AVAILABLE;
387
TimelineThreadData *thread = GetThisThreadData();
389
if (thread->disabled)
390
return NS_ERROR_NOT_AVAILABLE;
392
NS_TimelineMarkV(text, args);
397
PR_IMPLEMENT(nsresult) NS_TimelineStartTimer(const char *timerName)
399
PR_CallOnce(&initonce, TimelineInit);
401
if (gTimelineDisabled)
402
return NS_ERROR_NOT_AVAILABLE;
404
TimelineThreadData *thread = GetThisThreadData();
406
if (thread->timers == NULL)
407
return NS_ERROR_FAILURE;
408
if (thread->disabled)
409
return NS_ERROR_NOT_AVAILABLE;
411
nsTimelineServiceTimer *timer
412
= (nsTimelineServiceTimer *)PL_HashTableLookup(thread->timers, timerName);
414
timer = new nsTimelineServiceTimer;
416
return NS_ERROR_OUT_OF_MEMORY;
418
PL_HashTableAdd(thread->timers, timerName, timer);
424
PR_IMPLEMENT(nsresult) NS_TimelineStopTimer(const char *timerName)
426
if (gTimelineDisabled)
427
return NS_ERROR_NOT_AVAILABLE;
429
* Strange-looking now/timer->stop() interaction is to avoid
430
* including time spent in TLS and PL_HashTableLookup in the
435
TimelineThreadData *thread = GetThisThreadData();
436
if (thread->timers == NULL)
437
return NS_ERROR_FAILURE;
438
if (thread->disabled)
439
return NS_ERROR_NOT_AVAILABLE;
440
nsTimelineServiceTimer *timer
441
= (nsTimelineServiceTimer *)PL_HashTableLookup(thread->timers, timerName);
443
return NS_ERROR_FAILURE;
451
PR_IMPLEMENT(nsresult) NS_TimelineMarkTimer(const char *timerName, const char *str)
453
PR_CallOnce(&initonce, TimelineInit);
455
if (gTimelineDisabled)
456
return NS_ERROR_NOT_AVAILABLE;
458
TimelineThreadData *thread = GetThisThreadData();
459
if (thread->timers == NULL)
460
return NS_ERROR_FAILURE;
461
if (thread->disabled)
462
return NS_ERROR_NOT_AVAILABLE;
463
nsTimelineServiceTimer *timer
464
= (nsTimelineServiceTimer *)PL_HashTableLookup(thread->timers, timerName);
466
return NS_ERROR_FAILURE;
468
PRTime accum = timer->getAccum();
472
ParseTime(accum, sec, msec);
474
PR_snprintf(buf, sizeof buf, "%s total: %d.%03d",
475
timerName, sec, msec);
477
PR_snprintf(buf, sizeof buf, "%s total: %d.%03d (%s)",
478
timerName, sec, msec, str);
479
NS_TimelineMark(buf);
484
PR_IMPLEMENT(nsresult) NS_TimelineResetTimer(const char *timerName)
486
if (gTimelineDisabled)
487
return NS_ERROR_NOT_AVAILABLE;
489
TimelineThreadData *thread = GetThisThreadData();
490
if (thread->timers == NULL)
491
return NS_ERROR_FAILURE;
492
if (thread->disabled)
493
return NS_ERROR_NOT_AVAILABLE;
494
nsTimelineServiceTimer *timer
495
= (nsTimelineServiceTimer *)PL_HashTableLookup(thread->timers, timerName);
497
return NS_ERROR_FAILURE;
504
PR_IMPLEMENT(nsresult) NS_TimelineIndent()
506
if (gTimelineDisabled)
507
return NS_ERROR_NOT_AVAILABLE;
509
TimelineThreadData *thread = GetThisThreadData();
510
if (thread->disabled)
511
return NS_ERROR_NOT_AVAILABLE;
516
PR_IMPLEMENT(nsresult) NS_TimelineOutdent()
518
if (gTimelineDisabled)
519
return NS_ERROR_NOT_AVAILABLE;
521
TimelineThreadData *thread = GetThisThreadData();
522
if (thread->disabled)
523
return NS_ERROR_NOT_AVAILABLE;
528
PR_IMPLEMENT(nsresult) NS_TimelineEnter(const char *text)
530
nsresult rv = NS_TimelineMark("%s...", text);
534
return NS_TimelineIndent();
537
PR_IMPLEMENT(nsresult) NS_TimelineLeave(const char *text)
539
nsresult rv = NS_TimelineOutdent();
543
return NS_TimelineMark("...%s", text);
546
nsTimelineService::nsTimelineService()
548
/* member initializers and constructor code */
551
/* void mark (in string text); */
552
NS_IMETHODIMP nsTimelineService::Mark(const char *text)
554
return NS_TimelineMark(text);
557
/* void startTimer (in string timerName); */
558
NS_IMETHODIMP nsTimelineService::StartTimer(const char *timerName)
560
return NS_TimelineStartTimer(timerName);
563
/* void stopTimer (in string timerName); */
564
NS_IMETHODIMP nsTimelineService::StopTimer(const char *timerName)
566
return NS_TimelineStopTimer(timerName);
569
/* void markTimer (in string timerName); */
570
NS_IMETHODIMP nsTimelineService::MarkTimer(const char *timerName)
572
return NS_TimelineMarkTimer(timerName);
575
/* void markTimerWithComment(in string timerName, in string comment); */
576
NS_IMETHODIMP nsTimelineService::MarkTimerWithComment(const char *timerName, const char *comment)
578
return NS_TimelineMarkTimer(timerName, comment);
581
/* void resetTimer (in string timerName); */
582
NS_IMETHODIMP nsTimelineService::ResetTimer(const char *timerName)
584
return NS_TimelineResetTimer(timerName);
587
/* void indent (); */
588
NS_IMETHODIMP nsTimelineService::Indent()
590
return NS_TimelineIndent();
593
/* void outdent (); */
594
NS_IMETHODIMP nsTimelineService::Outdent()
596
return NS_TimelineOutdent();
599
/* void enter (in string text); */
600
NS_IMETHODIMP nsTimelineService::Enter(const char *text)
602
return NS_TimelineEnter(text);
605
/* void leave (in string text); */
606
NS_IMETHODIMP nsTimelineService::Leave(const char *text)
608
return NS_TimelineLeave(text);
611
#endif /* MOZ_TIMELINE */