Oolite
Loading...
Searching...
No Matches
OOJSEngineTimeManagement.m
Go to the documentation of this file.
1/*
2
3OOJSEngineTimeManagement.h
4
5
6Copyright (C) 2010-2013 Jens Ayton
7
8Permission is hereby granted, free of charge, to any person obtaining a copy
9of this software and associated documentation files (the "Software"), to deal
10in the Software without restriction, including without limitation the rights
11to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12copies of the Software, and to permit persons to whom the Software is
13furnished to do so, subject to the following conditions:
14
15The above copyright notice and this permission notice shall be included in all
16copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24SOFTWARE.
25
26*/
27
30#import "OOJSScript.h"
31#import "OOLoggingExtended.h"
33#include <jsdbgapi.h>
34
35#if OOLITE_LINUX
36// Workaround for clang/glibc incompatibility.
37#ifdef __block
38#undef __block
39#endif
40#define __block __glibc_block
41#endif
42#include <unistd.h>
43
44#if OOLITE_LINUX
45#undef __block
46#endif
47
48#if OO_DEBUG
49#define OOJS_DEBUG_LIMITER 1
50#else
51#define OOJS_DEBUG_LIMITER 0
52#endif
53
54static unsigned sLimiterStartDepth;
58static double sLimiterTimeLimit;
59
60#if OOJS_DEBUG_LIMITER
61#define OOJS_TIME_LIMIT (0.2) // seconds
62#else
63#define OOJS_TIME_LIMIT (1) // seconds
64#endif
65
66static BOOL sStop;
67
68#ifndef NDEBUG
69static const char* sLastStartedFile;
70static unsigned sLastStartedLine;
71static const char* sLastStoppedFile;
72static unsigned sLastStoppedLine;
73#endif
74
75#if OOJS_PROFILE && defined(MOZ_TRACE_JSCALLS)
76static void FunctionCallback(JSFunction* function, JSScript* script, JSContext* context, int entering);
77#endif
78
79#ifndef NDEBUG
80void OOJSStartTimeLimiterWithTimeLimit_(OOTimeDelta limit, const char* file, unsigned line)
81#else
83#endif
84{
85#if OOJS_DEBUG_LIMITER
86 OOLog(@"script.javaScript.timeLimit.debug", @"Limiter starting: %u => %u", sLimiterStartDepth, sLimiterStartDepth + 1);
87#endif
88 if (sLimiterStartDepth++ == 0) {
89 if (limit <= 0.0)
90 limit = OOJS_TIME_LIMIT;
91 sLimiterTimeLimit = limit;
93
96 }
97
98#ifndef NDEBUG
99 sLastStartedFile = file;
100 sLastStartedLine = line;
101#endif
102}
103
104#ifndef NDEBUG
105void OOJSStopTimeLimiter_(const char* file, unsigned line)
106#else
107void OOJSStopTimeLimiter(void)
108#endif
109{
110#ifndef NDEBUG
111 if (sLimiterStartDepth == 0) {
112 OOLog(@"bug.javaScript.limiterDepth", @"Attempt to stop JavaScript time limiter while it is already fully stopped. This is an internal bug, please report it. (Last start: %@:%u, last valid stop: %@:%u, this stop attempt: %@:%u.)", OOLogAbbreviatedFileName(sLastStartedFile), sLastStartedLine, OOLogAbbreviatedFileName(sLastStoppedFile), sLastStoppedLine, OOLogAbbreviatedFileName(file), line);
113 return;
114 }
115
116 sLastStoppedFile = file;
117 sLastStoppedLine = line;
118
119#if OOJS_DEBUG_LIMITER
120 OOLog(@"script.javaScript.timeLimit.debug", @"Limiter ending: %u <= %u", sLimiterStartDepth - 1, sLimiterStartDepth);
121#endif
122
123#endif
124
125 if (--sLimiterStartDepth == 0)
126 sLimiterTimeLimit = 0.0;
127}
128
136
138{
139 if (--sLimiterPauseDepth == 0)
140
141 {
145
146 sLimiterTimeLimit += elapsed;
147 }
148}
149
150#ifndef NDEBUG
155
163
168
170{
171 sLimiterTimeLimit = limit;
172}
173#endif
174
175@implementation OOJavaScriptEngine (WatchdogTimer)
176
178{
179 for (;;) {
180#if OOLITE_WINDOWS
181 Sleep(OOJS_TIME_LIMIT * 1000);
182#else
183 usleep(OOJS_TIME_LIMIT * 1000000);
184#endif
185
187 continue; // Most of the time, a script isn't running.
188
189 // Note: if you add logging here, you need a manual autorelease pool.
190
194
195 if (EXPECT_NOT(elapsed > sLimiterTimeLimit)) {
196 sStop = YES;
197 JS_TriggerAllOperationCallbacks(_runtime);
198 }
199 }
200}
201
202@end
203
204static JSBool OperationCallback(JSContext* context)
205{
206 if (!sStop)
207 return YES;
208
209 JS_ClearPendingException(context);
210
214
215 if (elapsed <= sLimiterTimeLimit)
216 return YES;
217
218 OOLogERR(@"script.javaScript.timeLimit", @"Script \"%@\" ran for %g seconds and has been terminated.", [[OOJSScript currentlyRunningScript] name], elapsed);
219#ifdef OO_DEBUG_MONITOR_ENABLED
220 OOJSDumpStack(context);
221#endif
222
223 // FIXME: we really should put something in the JS log here, but since that's implemented in JS there are complications.
224
225 return NO;
226}
227
228static JSBool ContextCallback(JSContext* context, uintN contextOp)
229{
230 if (contextOp == JSCONTEXT_NEW) {
231 JS_SetOperationCallback(context, OperationCallback);
232
233#if OOJS_PROFILE && defined(MOZ_TRACE_JSCALLS)
234 JS_SetFunctionCallback(context, (JSFunctionCallback)FunctionCallback); // Naughtily casts away consts, because const JSContexts and JSFunctions are useless.
235#endif
236 }
237 return YES;
238}
239
240void OOJSTimeManagementInit(OOJavaScriptEngine* engine, JSRuntime* runtime)
241{
242 [NSThread detachNewThreadSelector:@selector(watchdogTimerThread)
243 toTarget:engine
244 withObject:nil];
245
246 JS_SetContextCallback(runtime, ContextCallback);
247}
248
249#if OOJS_PROFILE
250
251#ifndef MOZ_TRACE_JSCALLS
252#warning Profiling is enabled, but MOZ_TRACE_JSCALLS is disabled, so only native functions will be profiled.
253#endif
254
255static BOOL sProfiling = NO;
256static BOOL sTracing = NO;
257static OOJSProfileStackFrame* sProfileStack = NULL;
258static NSMapTable* sProfileInfo;
259static double sProfilerOverhead;
260static double sProfilerTotalNativeTime;
261static double sProfilerTotalJavaScriptTime;
262static double sProfilerEntryTimeLimit;
263static OOHighResTimeValue sProfilerStartTime;
264
265@interface OOTimeProfile (Private)
266
267- (void)setTotalTime:(double)value;
268- (void)setNativeTime:(double)value;
269#ifdef MOZ_TRACE_JSCALLS
270- (void)setJavaScriptTime:(double)value;
271#endif
272- (void)setProfilerOverhead:(double)value;
273- (void)setExtensionTime:(double)value;
274- (void)setProfileEntries:(NSArray*)value;
275
276- (NSDictionary*)propertyListRepresentation;
277
278@end
279
280@interface OOTimeProfileEntry (Private)
281
282- (id)initWithCName:(const char*)name;
283#ifdef MOZ_TRACE_JSCALLS
284- (id)initWithJSFunction:(JSFunction*)function context:(JSContext*)context;
285#endif
286
287- (void)addSampleWithTotalTime:(OOTimeDelta)totalTime selfTime:(OOTimeDelta)selfTime;
288
289- (NSDictionary*)propertyListRepresentation;
290
291@end
292
293void OOJSBeginProfiling(BOOL trace)
294{
295 assert(sProfiling == NO);
296 sProfiling = YES;
297 sTracing = trace;
298 sProfileInfo = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSObjectMapValueCallBacks, 100);
299 sProfilerOverhead = 0.0;
300 sProfilerTotalNativeTime = 0.0;
301 sProfilerTotalJavaScriptTime = 0.0;
302 sProfilerEntryTimeLimit = OOJSGetTimeLimiterLimit();
303
304 // This should be last for precision.
305 sProfilerStartTime = OOGetHighResTime();
306
307 if (trace) {
308 OOLog(@"script.javaScript.trace", @"%@", @">>>> Beginning trace.");
309 OOLogIndent();
310 }
311}
312
313OOTimeProfile* OOJSEndProfiling(void)
314{
315 // This should be at the top for precision.
317 // Time limiter should be as close to outermost as practical.
319
320 assert(sProfiling && sProfileStack == NULL);
321
322 sProfiling = NO;
323
324 OOTimeProfile* result = [[OOTimeProfile alloc] init];
325
326 [result setTotalTime:OOHighResTimeDeltaInSeconds(sProfilerStartTime, now)];
327 [result setNativeTime:sProfilerTotalNativeTime];
328#ifdef MOZ_TRACE_JSCALLS
329 [result setJavaScriptTime:sProfilerTotalJavaScriptTime];
330#endif
331 [result setProfilerOverhead:sProfilerOverhead];
332
333 double currentTimeLimit = OOJSGetTimeLimiterLimit();
334 [result setExtensionTime:currentTimeLimit - sProfilerEntryTimeLimit];
335
336 [result setProfileEntries:[NSAllMapTableValues(sProfileInfo) sortedArrayUsingSelector:@selector(compareBySelfTimeReverse:)]];
337
338 if (sTracing) {
339 OOLogOutdent();
340 OOLog(@"script.javaScript.trace", @"%@", @"<<<< End of trace.");
341 sTracing = NO;
342 }
343
344 // Clean up.
345 NSFreeMapTable(sProfileInfo);
346 OODisposeHighResTime(sProfilerStartTime);
347
349
351 return result;
352}
353
354BOOL OOJSIsProfiling(void)
355{
356 return sProfiling;
357}
358
359void OOJSBeginTracing(void);
360void OOJSEndTracing(void);
361BOOL OOJSIsTracing(void);
362
363static void UpdateProfileForFrame(OOHighResTimeValue now, OOJSProfileStackFrame* frame);
364
365#ifdef MOZ_TRACE_JSCALLS
366static void CleanUpJSFrame(OOJSProfileStackFrame* frame)
367{
368 free(frame);
369}
370
371static void TraceEnterJSFunction(JSContext* context, JSFunction* function, OOTimeProfileEntry* profileEntry)
372{
373 NSMutableString* name = [NSMutableString stringWithFormat:@"%@(", [profileEntry function]];
374 BOOL isNative = JS_GetFunctionNative(context, function) != NULL;
375 NSString* frameTag = nil;
376 NSString* logMsgClass = nil;
377
378 if (!isNative) {
379 // Get stack frame and find arguments.
380 JSStackFrame* frame = NULL;
381 BOOL first = YES;
382 jsval this;
383 JSObject* scope;
384 JSPropertyDescArray properties = { 0, NULL };
385 unsigned i;
386
387 // Temporarily disable profiling as we'll call out to profiled functions to get value descriptions.
388 sProfiling = NO;
389
390 if (JS_FrameIterator(context, &frame) != NULL) {
391 if (JS_IsConstructorFrame(context, frame)) {
392 [name insertString:@"new " atIndex:0];
393 }
394
395 if (JS_GetFrameThis(context, frame, &this)) {
396 [name appendFormat:@"this: %@", OOJSDescribeValue(context, this, YES)];
397 first = NO;
398 }
399
400 scope = JS_GetFrameScopeChain(context, frame);
401 if (scope != NULL && JS_GetPropertyDescArray(context, scope, &properties)) {
402 for (i = 0; i < properties.length; i++) {
403 JSPropertyDesc* prop = &properties.array[i];
404 if (prop->flags & JSPD_ARGUMENT) {
405 if (!first)
406 [name appendFormat:@", "];
407 else
408 first = NO;
409
410 [name appendFormat:@"%@: %@", OOStringFromJSValueEvenIfNull(context, prop->id), OOJSDescribeValue(context, prop->value, YES)];
411 }
412 }
413 }
414 }
415
416 sProfiling = YES;
417
418 frameTag = @"JS"; // JavaScript
419 logMsgClass = @"script.javaScript.trace.JS";
420 } else {
421 frameTag = @"NW"; // Native Wrapper
422 logMsgClass = @"script.javaScript.trace.NW";
423 }
424
425 [name appendString:@")"];
426 OOLog(logMsgClass, @">> %@ [%@]", name, frameTag);
427 OOLogIndent();
428}
429
430static void FunctionCallback(JSFunction* function, JSScript* script, JSContext* context, int entering)
431{
432 if (EXPECT(!sProfiling))
433 return;
434 if (EXPECT_NOT(function == NULL))
435 return;
436
437 // Ignore native functions. Ours get their own entries anyway, SpiderMonkey's are elided.
438 if (!sTracing && JS_GetFunctionNative(context, function) != NULL)
439 return;
440
442
443 NSAutoreleasePool* pool = [NSAutoreleasePool new];
444
445 if (entering > 0) {
446 // Create profile entry up front so we can shove the JS function in it.
447 OOTimeProfileEntry* entry = NSMapGet(sProfileInfo, function);
448 if (entry == nil) {
449 entry = [[OOTimeProfileEntry alloc] initWithJSFunction:function context:context];
450 NSMapInsertKnownAbsent(sProfileInfo, function, entry);
451 [entry release];
452 }
453
454 if (EXPECT_NOT(sTracing)) {
455 // We use EXPECT_NOT here because profiles are time-critical and traces are not.
456 TraceEnterJSFunction(context, function, entry);
457 }
458
459 // Make a stack frame on the heap.
460 OOJSProfileStackFrame* frame = malloc(sizeof(OOJSProfileStackFrame));
461 assert(frame != NULL);
462
463 *frame = (OOJSProfileStackFrame) {
464 .back = sProfileStack,
465 .key = function,
466 .startTime = start,
467 .subTime = 0.0,
468 .total = &sProfilerTotalJavaScriptTime,
469 .cleanup = CleanUpJSFrame
470 };
471
472 sProfileStack = frame;
473 } else {
474 // Exiting.
475 assert(sProfileStack != NULL && sProfileStack->cleanup == CleanUpJSFrame);
476
477 UpdateProfileForFrame(start, sProfileStack);
478 }
479
480 [pool release];
481
483 double currentOverhead = OOHighResTimeDeltaInSeconds(start, end);
484 sProfilerOverhead += currentOverhead;
487}
488#endif
489
490void OOJSProfileEnter(OOJSProfileStackFrame* frame, const char* function)
491{
492 if (EXPECT(!sProfiling))
493 return;
494 if (EXPECT_NOT(sTracing)) {
495 // We use EXPECT_NOT here because profiles are time-critical and traces are not.
496 OOLog(@"script.javaScript.trace.ON", @">> %s [ON]", function);
497 OOLogIndent();
498 }
499
500 *frame = (OOJSProfileStackFrame) {
501 .back = sProfileStack,
502 .key = function,
503 .function = function,
504 .startTime = OOGetHighResTime(),
505 .total = &sProfilerTotalNativeTime
506 };
507 sProfileStack = frame;
508}
509
510void OOJSProfileExit(OOJSProfileStackFrame* frame)
511{
512 if (EXPECT(!sProfiling))
513 return;
514
516 NSAutoreleasePool* pool = [NSAutoreleasePool new];
517 BOOL done = NO;
518
519 /*
520 It's possible there could be JavaScript frames on top of this frame if
521 a JS native returned false. Or possibly not. The semantics of
522 JS_SetFunctionCallback() aren't specified in detail.
523 -- Ahruman 2011-01-16
524 */
525 for (;;) {
526 assert(sProfileStack != NULL);
527
528 done = (sProfileStack == frame);
529 UpdateProfileForFrame(now, sProfileStack);
530 if (EXPECT(done))
531 break;
532 }
533
534 [pool release];
535
536 OODisposeHighResTime(frame->startTime);
537
539 double currentOverhead = OOHighResTimeDeltaInSeconds(now, end);
540 sProfilerOverhead += currentOverhead;
541
542 /* Equivalent of pausing/resuming time limiter, except that it guarantees
543 excluded time will match profiler overhead if there are no other
544 pauses happening.
545 */
546 if (sLimiterPauseDepth == 0)
547 sLimiterTimeLimit += currentOverhead;
548
551}
552
553static void UpdateProfileForFrame(OOHighResTimeValue now, OOJSProfileStackFrame* frame)
554{
555 sProfileStack = frame->back;
556
557 OOTimeProfileEntry* entry = NSMapGet(sProfileInfo, frame->key);
558 if (entry == nil) {
559 entry = [[OOTimeProfileEntry alloc] initWithCName:frame->function];
560 NSMapInsertKnownAbsent(sProfileInfo, frame->key, entry);
561 [entry release];
562 }
563
564 OOTimeDelta time = OOHighResTimeDeltaInSeconds(frame->startTime, now);
565 OOTimeDelta selfTime = time - frame->subTime;
566 [entry addSampleWithTotalTime:time selfTime:selfTime];
567
568 *(frame->total) += selfTime;
569 if (sProfileStack != NULL)
570 sProfileStack->subTime += time;
571
572 if (frame->cleanup != NULL)
573 frame->cleanup(frame);
574
575 if (EXPECT_NOT(sTracing))
576 OOLogOutdent();
577}
578
579@implementation OOTimeProfile
580
581- (void)dealloc
582{
583 DESTROY(_profileEntries);
584
585 [super dealloc];
586}
587
588- (NSString*)description
589{
590 double totalTime = [self totalTime];
591
592 NSMutableString* result = [NSMutableString stringWithFormat:
593 @"Total time: %g ms\n"
594 "JavaScript: %g ms, native: %g ms\n"
595 "Counted towards limit: %g ms, excluded: %g ms\n"
596 "Profiler overhead: %g ms",
597 totalTime * 1000.0,
598 [self javaScriptTime] * 1000.0, [self nativeTime] * 1000.0,
599 [self nonExtensionTime] * 1000.0, [self extensionTime] * 1000.0,
600 [self profilerOverhead] * 1000.0];
601
602 NSArray* profileEntries = [self profileEntries];
603 NSUInteger i, count = [profileEntries count];
604 if (count != 0) {
605 [result appendString:@"\n NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX"];
606 for (i = 0; i < count; i++) {
607 // [result appendFormat:@"\n %@", [_profileEntries objectAtIndex:i]];
608
609 OOTimeProfileEntry* entry = [profileEntries objectAtIndex:i];
610
611 double totalPc = [entry totalTimeSum] * 100.0 / totalTime;
612 double selfPc = [entry selfTimeSum] * 100.0 / totalTime;
613
614 [result appendFormat:@"\n%60s %c%7lu %8.2f %8.2f %5.1f %5.1f %8.2f",
615 [[entry function] UTF8String],
616 [entry isJavaScriptFrame] ? 'J' : 'N',
617 (unsigned long)[entry hitCount], [entry totalTimeSum] * 1000.0, [entry selfTimeSum] * 1000.0, totalPc, selfPc, [entry selfTimeMax] * 1000.0];
618 }
619 }
620
621 return result;
622}
623
624- (double)totalTime
625{
626 return _totalTime;
627}
628
629- (void)setTotalTime:(double)value
630{
631 _totalTime = value;
632}
633
634- (double)javaScriptTime
635{
636#ifdef MOZ_TRACE_JSCALLS
637 return _javaScriptTime;
638#else
639 return _totalTime - _nativeTime;
640#endif
641}
642
643#ifdef MOZ_TRACE_JSCALLS
644- (void)setJavaScriptTime:(double)value
645{
646 _javaScriptTime = value;
647}
648#endif
649
650- (double)nativeTime
651{
652 return _nativeTime;
653}
654
655- (void)setNativeTime:(double)value
656{
657 _nativeTime = value;
658}
659
660- (double)extensionTime
661{
662 return _extensionTime;
663}
664
665- (void)setExtensionTime:(double)value
666{
667 _extensionTime = value;
668}
669
670- (double)nonExtensionTime
671{
672 return _totalTime - _extensionTime;
673}
674
675- (double)profilerOverhead
676{
677 return _profilerOverhead;
678}
679
680- (void)setProfilerOverhead:(double)value
681{
682 _profilerOverhead = value;
683}
684
685- (NSArray*)profileEntries
686{
687 return _profileEntries;
688}
689
690- (void)setProfileEntries:(NSArray*)value
691{
692 if (_profileEntries != value) {
693 [_profileEntries release];
694 _profileEntries = [value retain];
695 }
696}
697
698- (jsval)oo_jsValueInContext:(JSContext*)context
699{
700 return OOJSValueFromNativeObject(context, [self propertyListRepresentation]);
701}
702
703- (NSDictionary*)propertyListRepresentation
704{
705 NSArray* profileEntries = [self profileEntries];
706 NSMutableArray* convertedEntries = [NSMutableArray arrayWithCapacity:[profileEntries count]];
707 OOTimeProfileEntry* entry = nil;
708 foreach (entry, profileEntries) {
709 [convertedEntries addObject:[entry propertyListRepresentation]];
710 }
711
712 return [NSDictionary dictionaryWithObjectsAndKeys:
713 profileEntries, @"profiles",
714 [NSNumber numberWithDouble:[self totalTime]], @"totalTime",
715 [NSNumber numberWithDouble:[self javaScriptTime]], @"javaScriptTime",
716 [NSNumber numberWithDouble:[self nativeTime]], @"nativeTime",
717 [NSNumber numberWithDouble:[self extensionTime]], @"extensionTime",
718 [NSNumber numberWithDouble:[self nonExtensionTime]], @"nonExtensionTime",
719 [NSNumber numberWithDouble:[self profilerOverhead]], @"profilerOverhead",
720 nil];
721}
722
723@end
724
725@implementation OOTimeProfileEntry
726
727- (id)initWithCName:(const char*)name
728{
729 NSAssert(sProfiling, @"Can't create profile entries while not profiling.");
730
731 if ((self = [super init])) {
732 if (name != NULL) {
733 _function = [[NSString stringWithUTF8String:name] retain];
734 }
735 }
736
737 return self;
738}
739
740#if MOZ_TRACE_JSCALLS
741- (id)initWithJSFunction:(JSFunction*)function context:(JSContext*)context
742{
743 if ((self = [self initWithCName:NULL])) {
744 // Temporarily disable profiling so we don't profile the profiler while it's profiling the profilee.
745 sProfiling = NO;
746 _jsFunction = function;
747
748 NSString* funcName = nil;
749 JSString* jsName = JS_GetFunctionId(_jsFunction);
750 if (jsName != NULL)
751 funcName = [OOStringFromJSString(context, jsName) retain];
752 else
753 funcName = @"<anonymous>";
754
755 // If it's a non-native function, get its source location.
756 NSString* location = nil;
757 if (JS_GetFunctionNative(context, function) == NULL) {
758 JSStackFrame* frame = NULL;
759 if (JS_FrameIterator(context, &frame) != NULL) {
760 location = OOJSDescribeLocation(context, frame);
761 }
762 }
763
764 if (location != nil) {
765 _function = [[NSString alloc] initWithFormat:@"(%@) %@", location, funcName];
766 } else
767 _function = [funcName retain];
768
769 sProfiling = YES;
770 }
771
772 return self;
773}
774#endif
775
776- (void)dealloc
777{
778 DESTROY(_function);
779
780 [super dealloc];
781}
782
783- (void)addSampleWithTotalTime:(OOTimeDelta)totalTime selfTime:(OOTimeDelta)selfTime
784{
785 _hitCount++;
786 _totalTimeSum += totalTime;
787 _selfTimeSum += selfTime;
788 _totalTimeMax = fmax(_totalTimeMax, totalTime);
789 _selfTimeMax = fmax(_selfTimeMax, selfTime);
790}
791
792- (NSString*)description
793{
794 if (_hitCount == 0)
795 return [NSString stringWithFormat:@"%@: --", _function];
796
797 // Convert everything to milliseconds.
798 float totalTimeSum = _totalTimeSum * 1000.0;
799 float selfTimeSum = _selfTimeSum * 1000.0;
800 float totalTimeMax = _totalTimeMax * 1000.0;
801 float selfTimeMax = _selfTimeMax * 1000.0;
802
803 if (totalTimeSum == selfTimeSum && totalTimeMax == selfTimeMax) {
804 if (_hitCount == 1) {
805 return [NSString stringWithFormat:@"%@: 1 time, %g ms", _function, totalTimeSum];
806 } else {
807 return [NSString stringWithFormat:@"%@: %lu times, total %g ms, avg %g ms, max %g ms", _function, _hitCount, totalTimeSum, totalTimeSum / _hitCount, totalTimeMax];
808 }
809 } else {
810 if (_hitCount == 1) {
811 return [NSString stringWithFormat:@"%@: 1 time, %g ms (self %g ms)", _function, totalTimeSum, selfTimeSum];
812 } else {
813 return [NSString stringWithFormat:@"%@: %lu times, total %g ms (self %g ms), avg %g ms (self %g ms), max %g ms, max self %g ms", _function, _hitCount, totalTimeSum, selfTimeSum, totalTimeSum / _hitCount, selfTimeSum / _hitCount, totalTimeMax, selfTimeMax];
814 }
815 }
816}
817
818- (NSString*)function
819{
820 return _function;
821}
822
823- (NSUInteger)hitCount
824{
825 return _hitCount;
826}
827
828- (double)totalTimeSum
829{
830 return _totalTimeSum;
831}
832
833- (double)selfTimeSum
834{
835 return _selfTimeSum;
836}
837
838- (double)totalTimeAverage
839{
840 return _hitCount ? (_totalTimeSum / _hitCount) : 0.0;
841}
842
843- (double)selfTimeAverage
844{
845 return _hitCount ? (_selfTimeSum / _hitCount) : 0.0;
846}
847
848- (double)totalTimeMax
849{
850 return _totalTimeMax;
851}
852
853- (double)selfTimeMax
854{
855 return _selfTimeMax;
856}
857
858- (BOOL)isJavaScriptFrame
859{
860#if MOZ_TRACE_JSCALLS
861 return _jsFunction != NULL;
862#else
863 return NO;
864#endif
865}
866
867- (NSComparisonResult)compareByTotalTime:(OOTimeProfileEntry*)other
868{
869 return -[self compareByTotalTimeReverse:other];
870}
871
872- (NSComparisonResult)compareByTotalTimeReverse:(OOTimeProfileEntry*)other
873{
874 double selfTotal = [self totalTimeSum];
875 double otherTotal = [other totalTimeSum];
876
877 if (selfTotal < otherTotal)
878 return NSOrderedDescending;
879 if (selfTotal > otherTotal)
880 return NSOrderedAscending;
881 return NSOrderedSame;
882}
883
884- (NSComparisonResult)compareBySelfTime:(OOTimeProfileEntry*)other
885{
886 return -[self compareBySelfTimeReverse:other];
887}
888
889- (NSComparisonResult)compareBySelfTimeReverse:(OOTimeProfileEntry*)other
890{
891 double selfTotal = [self selfTimeSum];
892 double otherTotal = [other selfTimeSum];
893
894 if (selfTotal < otherTotal)
895 return NSOrderedDescending;
896 if (selfTotal > otherTotal)
897 return NSOrderedAscending;
898 return NSOrderedSame;
899}
900
901- (jsval)oo_jsValueInContext:(JSContext*)context
902{
903 return OOJSValueFromNativeObject(context, [self propertyListRepresentation]);
904}
905
906- (NSDictionary*)propertyListRepresentation
907{
908 return [NSDictionary dictionaryWithObjectsAndKeys:
909 _function, @"name",
910 [NSNumber numberWithUnsignedInteger:[self hitCount]], @"hitCount",
911 [NSNumber numberWithDouble:[self totalTimeSum]], @"totalTimeSum",
912 [NSNumber numberWithDouble:[self selfTimeSum]], @"selfTimeSum",
913 [NSNumber numberWithDouble:[self totalTimeAverage]], @"totalTimeAverage",
914 [NSNumber numberWithDouble:[self selfTimeAverage]], @"selfTimeAverage",
915 [NSNumber numberWithDouble:[self totalTimeMax]], @"totalTimeMax",
916 [NSNumber numberWithDouble:[self selfTimeMax]], @"selfTimeMax",
917 [NSNumber numberWithBool:[self isJavaScriptFrame]], @"isJavaScriptFrame",
918 nil];
919}
920
921@end
922
923#endif
#define DESTROY(x)
#define EXPECT_NOT(x)
#define EXPECT(x)
#define OOJSProfileExit(frame)
#define OOJSProfileEnter(frame, function)
#define OOJSStopTimeLimiter()
void OOJSTimeManagementInit(OOJavaScriptEngine *engine, JSRuntime *runtime)
#define OOJSStartTimeLimiterWithTimeLimit(limit)
void OOJSPauseTimeLimiter(void)
void OOJSResetTimeLimiter(void)
static unsigned sLastStoppedLine
static const char * sLastStoppedFile
#define OOJS_TIME_LIMIT
static BOOL sStop
static JSBool OperationCallback(JSContext *context)
static const char * sLastStartedFile
OOHighResTimeValue OOJSCopyTimeLimiterNominalStartTime(void)
static OOHighResTimeValue sLimiterPauseStart
static double sLimiterTimeLimit
OOTimeDelta OOJSGetTimeLimiterLimit(void)
static int sLimiterPauseDepth
void OOJSStartTimeLimiterWithTimeLimit_(OOTimeDelta limit, const char *file, unsigned line)
static unsigned sLimiterStartDepth
void OOJSStopTimeLimiter_(const char *file, unsigned line)
void OOJSResumeTimeLimiter(void)
static OOHighResTimeValue sLimiterStart
static unsigned sLastStartedLine
static JSBool ContextCallback(JSContext *context, uintN contextOp)
void OOJSSetTimeLimiterLimit(OOTimeDelta limit)
void OOJSPauseTimeLimiter(void)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define OOJSDumpStack(cx)
#define OOJSDescribeLocation(cx, frame)
void OOJSResumeTimeLimiter(void)
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
void OOLogOutdent(void)
Definition OOLogging.m:354
#define OOLog(class, format,...)
Definition OOLogging.h:84
void OOLogIndent(void)
Definition OOLogging.m:345
NSString * OOLogAbbreviatedFileName(const char *inName)
Definition OOLogging.m:762
return self
unsigned count
return nil
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
uint64_t OOHighResTimeValue
#define OOGetHighResTime
#define OODisposeHighResTime(time)
double OOTimeDelta
Definition OOTypes.h:193