40#define __block __glibc_block
49#define OOJS_DEBUG_LIMITER 1
51#define OOJS_DEBUG_LIMITER 0
61#define OOJS_TIME_LIMIT (0.2)
63#define OOJS_TIME_LIMIT (1)
75#if OOJS_PROFILE && defined(MOZ_TRACE_JSCALLS)
76static void FunctionCallback(JSFunction*
function, JSScript* script, JSContext* context,
int entering);
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);
119#if OOJS_DEBUG_LIMITER
175@implementation OOJavaScriptEngine (WatchdogTimer)
197 JS_TriggerAllOperationCallbacks(_runtime);
209 JS_ClearPendingException(context);
218 OOLogERR(
@"script.javaScript.timeLimit",
@"Script \"%@\
" ran for %g seconds and has been terminated.", [[
OOJSScript currentlyRunningScript] name], elapsed);
219#ifdef OO_DEBUG_MONITOR_ENABLED
230 if (contextOp == JSCONTEXT_NEW) {
233#if OOJS_PROFILE && defined(MOZ_TRACE_JSCALLS)
234 JS_SetFunctionCallback(context, (JSFunctionCallback)FunctionCallback);
242 [NSThread detachNewThreadSelector:@selector(watchdogTimerThread)
251#ifndef MOZ_TRACE_JSCALLS
252#warning Profiling is enabled, but MOZ_TRACE_JSCALLS is disabled, so only native functions will be profiled.
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;
265@interface OOTimeProfile (Private)
267- (void)setTotalTime:(
double)value;
268- (void)setNativeTime:(
double)value;
269#ifdef MOZ_TRACE_JSCALLS
270- (void)setJavaScriptTime:(
double)value;
272- (void)setProfilerOverhead:(
double)value;
273- (void)setExtensionTime:(
double)value;
274- (void)setProfileEntries:(NSArray*)value;
276- (NSDictionary*)propertyListRepresentation;
280@interface OOTimeProfileEntry (Private)
282- (id)initWithCName:(const
char*)name;
283#ifdef MOZ_TRACE_JSCALLS
284- (id)initWithJSFunction:(JSFunction*)function context:(JSContext*)context;
289- (NSDictionary*)propertyListRepresentation;
293void OOJSBeginProfiling(BOOL trace)
295 assert(sProfiling == NO);
298 sProfileInfo = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSObjectMapValueCallBacks, 100);
299 sProfilerOverhead = 0.0;
300 sProfilerTotalNativeTime = 0.0;
301 sProfilerTotalJavaScriptTime = 0.0;
308 OOLog(
@"script.javaScript.trace",
@"%@",
@">>>> Beginning trace.");
313OOTimeProfile* OOJSEndProfiling(
void)
320 assert(sProfiling && sProfileStack == NULL);
324 OOTimeProfile* result = [[OOTimeProfile alloc] init];
326 [result setTotalTime:OOHighResTimeDeltaInSeconds(sProfilerStartTime, now)];
327 [result setNativeTime:sProfilerTotalNativeTime];
328#ifdef MOZ_TRACE_JSCALLS
329 [result setJavaScriptTime:sProfilerTotalJavaScriptTime];
331 [result setProfilerOverhead:sProfilerOverhead];
334 [result setExtensionTime:currentTimeLimit - sProfilerEntryTimeLimit];
336 [result setProfileEntries:[NSAllMapTableValues(sProfileInfo) sortedArrayUsingSelector:@selector(compareBySelfTimeReverse:)]];
340 OOLog(
@"script.javaScript.trace",
@"%@",
@"<<<< End of trace.");
345 NSFreeMapTable(sProfileInfo);
354BOOL OOJSIsProfiling(
void)
359void OOJSBeginTracing(
void);
360void OOJSEndTracing(
void);
361BOOL OOJSIsTracing(
void);
363static void UpdateProfileForFrame(
OOHighResTimeValue now, OOJSProfileStackFrame* frame);
365#ifdef MOZ_TRACE_JSCALLS
366static void CleanUpJSFrame(OOJSProfileStackFrame* frame)
371static void TraceEnterJSFunction(JSContext* context, JSFunction*
function, OOTimeProfileEntry* profileEntry)
373 NSMutableString* name = [NSMutableString stringWithFormat:@"%@(", [profileEntry function]];
374 BOOL isNative = JS_GetFunctionNative(context,
function) != NULL;
375 NSString* frameTag =
nil;
376 NSString* logMsgClass =
nil;
380 JSStackFrame* frame = NULL;
384 JSPropertyDescArray properties = { 0, NULL };
390 if (JS_FrameIterator(context, &frame) != NULL) {
391 if (JS_IsConstructorFrame(context, frame)) {
392 [name insertString:@"new " atIndex:0];
395 if (JS_GetFrameThis(context, frame, &
this)) {
396 [name appendFormat:@"this: %@", OOJSDescribeValue(context, this, YES)];
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) {
406 [name appendFormat:
@", "];
410 [name appendFormat:
@"%@: %@", OOStringFromJSValueEvenIfNull(context, prop->id), OOJSDescribeValue(context, prop->value, YES)];
419 logMsgClass =
@"script.javaScript.trace.JS";
422 logMsgClass =
@"script.javaScript.trace.NW";
425 [name appendString:@")"];
426 OOLog(logMsgClass,
@">> %@ [%@]", name, frameTag);
430static void FunctionCallback(JSFunction*
function, JSScript* script, JSContext* context,
int entering)
438 if (!sTracing && JS_GetFunctionNative(context,
function) != NULL)
443 NSAutoreleasePool* pool = [NSAutoreleasePool new];
447 OOTimeProfileEntry* entry = NSMapGet(sProfileInfo,
function);
449 entry = [[OOTimeProfileEntry alloc] initWithJSFunction:function context:context];
450 NSMapInsertKnownAbsent(sProfileInfo,
function, entry);
456 TraceEnterJSFunction(context,
function, entry);
460 OOJSProfileStackFrame* frame = malloc(
sizeof(OOJSProfileStackFrame));
461 assert(frame != NULL);
463 *frame = (OOJSProfileStackFrame) {
464 .back = sProfileStack,
468 .total = &sProfilerTotalJavaScriptTime,
469 .cleanup = CleanUpJSFrame
472 sProfileStack = frame;
475 assert(sProfileStack != NULL && sProfileStack->cleanup == CleanUpJSFrame);
477 UpdateProfileForFrame(start, sProfileStack);
484 sProfilerOverhead += currentOverhead;
496 OOLog(
@"script.javaScript.trace.ON",
@">> %s [ON]",
function);
500 *frame = (OOJSProfileStackFrame) {
501 .back = sProfileStack,
503 .function =
function,
505 .total = &sProfilerTotalNativeTime
507 sProfileStack = frame;
516 NSAutoreleasePool* pool = [NSAutoreleasePool new];
526 assert(sProfileStack != NULL);
528 done = (sProfileStack == frame);
529 UpdateProfileForFrame(now, sProfileStack);
540 sProfilerOverhead += currentOverhead;
553static void UpdateProfileForFrame(
OOHighResTimeValue now, OOJSProfileStackFrame* frame)
555 sProfileStack = frame->back;
557 OOTimeProfileEntry* entry = NSMapGet(sProfileInfo, frame->key);
559 entry = [[OOTimeProfileEntry alloc] initWithCName:frame->function];
560 NSMapInsertKnownAbsent(sProfileInfo, frame->key, entry);
566 [entry addSampleWithTotalTime:time selfTime:selfTime];
568 *(frame->total) += selfTime;
569 if (sProfileStack != NULL)
570 sProfileStack->subTime += time;
572 if (frame->cleanup != NULL)
573 frame->cleanup(frame);
579@implementation OOTimeProfile
588- (NSString*)description
590 double totalTime = [
self totalTime];
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",
598 [
self javaScriptTime] * 1000.0, [
self nativeTime] * 1000.0,
599 [
self nonExtensionTime] * 1000.0, [
self extensionTime] * 1000.0,
600 [
self profilerOverhead] * 1000.0];
602 NSArray* profileEntries = [
self profileEntries];
603 NSUInteger i,
count = [profileEntries count];
605 [result appendString:@"\n NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX"];
606 for (i = 0; i <
count; i++) {
609 OOTimeProfileEntry* entry = [profileEntries objectAtIndex:i];
611 double totalPc = [entry totalTimeSum] * 100.0 / totalTime;
612 double selfPc = [entry selfTimeSum] * 100.0 / totalTime;
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];
629- (void)setTotalTime:(
double)value
634- (double)javaScriptTime
636#ifdef MOZ_TRACE_JSCALLS
637 return _javaScriptTime;
639 return _totalTime - _nativeTime;
643#ifdef MOZ_TRACE_JSCALLS
644- (void)setJavaScriptTime:(
double)value
646 _javaScriptTime = value;
655- (void)setNativeTime:(
double)value
660- (double)extensionTime
662 return _extensionTime;
665- (void)setExtensionTime:(
double)value
667 _extensionTime = value;
670- (double)nonExtensionTime
672 return _totalTime - _extensionTime;
675- (double)profilerOverhead
677 return _profilerOverhead;
680- (void)setProfilerOverhead:(
double)value
682 _profilerOverhead = value;
685- (NSArray*)profileEntries
687 return _profileEntries;
690- (void)setProfileEntries:(NSArray*)value
692 if (_profileEntries != value) {
693 [_profileEntries release];
694 _profileEntries = [value retain];
698- (jsval)oo_jsValueInContext:(JSContext*)context
703- (NSDictionary*)propertyListRepresentation
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]];
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",
725@implementation OOTimeProfileEntry
727- (id)initWithCName:(const
char*)name
729 NSAssert(sProfiling,
@"Can't create profile entries while not profiling.");
731 if ((
self = [super init])) {
733 _function = [[NSString stringWithUTF8String:name] retain];
741- (id)initWithJSFunction:(JSFunction*)function context:(JSContext*)context
743 if ((
self = [
self initWithCName:NULL])) {
746 _jsFunction =
function;
748 NSString* funcName =
nil;
749 JSString* jsName = JS_GetFunctionId(_jsFunction);
751 funcName = [OOStringFromJSString(context, jsName) retain];
753 funcName =
@"<anonymous>";
756 NSString* location =
nil;
757 if (JS_GetFunctionNative(context,
function) == NULL) {
758 JSStackFrame* frame = NULL;
759 if (JS_FrameIterator(context, &frame) != NULL) {
764 if (location !=
nil) {
765 _function = [[NSString alloc] initWithFormat:@"(%@) %@", location, funcName];
767 _function = [funcName retain];
786 _totalTimeSum += totalTime;
787 _selfTimeSum += selfTime;
788 _totalTimeMax = fmax(_totalTimeMax, totalTime);
789 _selfTimeMax = fmax(_selfTimeMax, selfTime);
792- (NSString*)description
795 return [NSString stringWithFormat:@"%@: --", _function];
798 float totalTimeSum = _totalTimeSum * 1000.0;
799 float selfTimeSum = _selfTimeSum * 1000.0;
800 float totalTimeMax = _totalTimeMax * 1000.0;
801 float selfTimeMax = _selfTimeMax * 1000.0;
803 if (totalTimeSum == selfTimeSum && totalTimeMax == selfTimeMax) {
804 if (_hitCount == 1) {
805 return [NSString stringWithFormat:@"%@: 1 time, %g ms", _function, totalTimeSum];
807 return [NSString stringWithFormat:@"%@: %lu times, total %g ms, avg %g ms, max %g ms", _function, _hitCount, totalTimeSum, totalTimeSum / _hitCount, totalTimeMax];
810 if (_hitCount == 1) {
811 return [NSString stringWithFormat:@"%@: 1 time, %g ms (self %g ms)", _function, totalTimeSum, selfTimeSum];
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];
823- (NSUInteger)hitCount
828- (double)totalTimeSum
830 return _totalTimeSum;
838- (double)totalTimeAverage
840 return _hitCount ? (_totalTimeSum / _hitCount) : 0.0;
843- (double)selfTimeAverage
845 return _hitCount ? (_selfTimeSum / _hitCount) : 0.0;
848- (double)totalTimeMax
850 return _totalTimeMax;
858- (BOOL)isJavaScriptFrame
861 return _jsFunction != NULL;
867- (NSComparisonResult)compareByTotalTime:(OOTimeProfileEntry*)other
869 return -[
self compareByTotalTimeReverse:other];
872- (NSComparisonResult)compareByTotalTimeReverse:(OOTimeProfileEntry*)other
874 double selfTotal = [
self totalTimeSum];
875 double otherTotal = [other totalTimeSum];
877 if (selfTotal < otherTotal)
878 return NSOrderedDescending;
879 if (selfTotal > otherTotal)
880 return NSOrderedAscending;
881 return NSOrderedSame;
884- (NSComparisonResult)compareBySelfTime:(OOTimeProfileEntry*)other
886 return -[
self compareBySelfTimeReverse:other];
889- (NSComparisonResult)compareBySelfTimeReverse:(OOTimeProfileEntry*)other
891 double selfTotal = [
self selfTimeSum];
892 double otherTotal = [other selfTimeSum];
894 if (selfTotal < otherTotal)
895 return NSOrderedDescending;
896 if (selfTotal > otherTotal)
897 return NSOrderedAscending;
898 return NSOrderedSame;
901- (jsval)oo_jsValueInContext:(JSContext*)context
906- (NSDictionary*)propertyListRepresentation
908 return [NSDictionary dictionaryWithObjectsAndKeys:
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",
#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
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,...)
#define OOLog(class, format,...)
NSString * OOLogAbbreviatedFileName(const char *inName)
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
uint64_t OOHighResTimeValue
#define OODisposeHighResTime(time)
void watchdogTimerThread()