54static BOOL sProfiling = NO;
55static BOOL sTracing = NO;
56static OOJSProfileStackFrame* sProfileStack = NULL;
57static NSMapTable* sProfileInfo;
58static double sProfilerOverhead;
59static double sProfilerTotalNativeTime;
60static double sProfilerEntryTimeLimit;
63#if OOJS_QUICKJS_CALL_HOOK
64static double sProfilerTotalJavaScriptTime;
71static JSValue* sProfiledFunctions;
72static NSUInteger sProfiledFunctionCount;
73static NSUInteger sProfiledFunctionCapacity;
75static void FunctionCallHook(JSContext* context, JSValueConst
function,
const void* code, JSValueConst
this,
int argc, JSValueConst* argv,
bool entering,
void* opaque);
78@interface OOTimeProfile (Private)
80- (void)setTotalTime:(
double)value;
81- (void)setNativeTime:(
double)value;
82- (void)setJavaScriptTime:(
double)value;
83- (void)setProfilerOverhead:(
double)value;
84- (void)setExtensionTime:(
double)value;
85- (void)setProfileEntries:(NSArray*)value;
87- (NSDictionary*)propertyListRepresentation;
91@interface OOTimeProfileEntry (Private)
93- (id)initWithCName:(const
char*)name;
94#if OOJS_QUICKJS_CALL_HOOK
95- (id)initWithJSFunction:(JSValueConst)function context:(JSContext*)context;
100- (NSDictionary*)propertyListRepresentation;
104void OOJSBeginProfiling(BOOL trace)
106 assert(sProfiling == NO);
109 sProfileInfo = NSCreateMapTable(NSNonOwnedPointerMapKeyCallBacks, NSObjectMapValueCallBacks, 100);
110 sProfilerOverhead = 0.0;
111 sProfilerTotalNativeTime = 0.0;
114#if OOJS_QUICKJS_CALL_HOOK
115 sProfilerTotalJavaScriptTime = 0.0;
116 sProfiledFunctionCount = 0;
118 JS_SetFunctionCallHook(JS_GetRuntime(context), FunctionCallHook, NULL);
126 OOLog(
@"script.javaScript.trace",
@"%@",
@">>>> Beginning trace.");
131OOTimeProfile* OOJSEndProfiling(
void)
138 assert(sProfiling && sProfileStack == NULL);
142 OOTimeProfile* result = [[OOTimeProfile alloc] init];
144 [result setTotalTime:OOHighResTimeDeltaInSeconds(sProfilerStartTime, now)];
145 [result setNativeTime:sProfilerTotalNativeTime];
146#if OOJS_QUICKJS_CALL_HOOK
147 [result setJavaScriptTime:sProfilerTotalJavaScriptTime];
149 [result setJavaScriptTime:[result totalTime] - sProfilerTotalNativeTime];
151 [result setProfilerOverhead:sProfilerOverhead];
154 [result setExtensionTime:currentTimeLimit - sProfilerEntryTimeLimit];
156 [result setProfileEntries:[NSAllMapTableValues(sProfileInfo) sortedArrayUsingSelector:@selector(compareBySelfTimeReverse:)]];
160 OOLog(
@"script.javaScript.trace",
@"%@",
@"<<<< End of trace.");
165#if OOJS_QUICKJS_CALL_HOOK
167 JS_SetFunctionCallHook(JS_GetRuntime(context), NULL, NULL);
168 for (NSUInteger i = 0; i < sProfiledFunctionCount; i++) {
169 JS_FreeValue(context, sProfiledFunctions[i]);
171 sProfiledFunctionCount = 0;
175 NSFreeMapTable(sProfileInfo);
181 return [result autorelease];
184BOOL OOJSIsProfiling(
void)
189static void UpdateProfileForFrame(
OOHighResTimeValue now, OOJSProfileStackFrame* frame);
191#if OOJS_QUICKJS_CALL_HOOK
192static void CleanUpJSFrame(OOJSProfileStackFrame* frame)
198static void HoldProfiledFunction(JSContext* context, JSValueConst
function)
200 if (sProfiledFunctionCount == sProfiledFunctionCapacity) {
201 sProfiledFunctionCapacity = sProfiledFunctionCapacity ? sProfiledFunctionCapacity * 2 : 64;
202 sProfiledFunctions = realloc(sProfiledFunctions, sProfiledFunctionCapacity *
sizeof *sProfiledFunctions);
203 assert(sProfiledFunctions != NULL);
205 sProfiledFunctions[sProfiledFunctionCount++] = JS_DupValue(context,
function);
208static void TraceEnterJSFunction(JSContext* context, JSValueConst
this,
int argc, JSValueConst* argv, OOTimeProfileEntry* profileEntry)
210 NSMutableString* name = [NSMutableString stringWithFormat:@"%@(", [profileEntry function]];
216 if (!JS_IsUndefined(
this)) {
217 [name appendFormat:@"this: %@", OOJSDescribeValue(context, this, YES)];
220 for (
int i = 0; i < argc; i++) {
222 [name appendString:
@", "];
225 [name appendString:OOJSDescribeValue(context, argv[i], YES)];
230 [name appendString:@")"];
231 OOLog(
@"script.javaScript.trace.JS",
@">> %@ [JS]", name);
235static void FunctionCallHook(JSContext* context, JSValueConst
function,
const void* code, JSValueConst
this,
int argc, JSValueConst* argv,
bool entering,
void* opaque)
241 BOOL startOwnedByFrame = NO;
243 NSAutoreleasePool* pool = [NSAutoreleasePool new];
247 OOTimeProfileEntry* entry = NSMapGet(sProfileInfo, code);
249 entry = [[OOTimeProfileEntry alloc] initWithJSFunction:function context:context];
250 NSMapInsertKnownAbsent(sProfileInfo, code, entry);
252 HoldProfiledFunction(context,
function);
257 TraceEnterJSFunction(context,
this, argc, argv, entry);
261 OOJSProfileStackFrame* frame = malloc(
sizeof(OOJSProfileStackFrame));
262 assert(frame != NULL);
264 *frame = (OOJSProfileStackFrame) {
265 .back = sProfileStack,
269 .total = &sProfilerTotalJavaScriptTime,
270 .cleanup = CleanUpJSFrame
273 sProfileStack = frame;
274 startOwnedByFrame = YES;
275 }
else if (sProfileStack != NULL && sProfileStack->cleanup == CleanUpJSFrame && sProfileStack->key == code) {
277 UpdateProfileForFrame(start, sProfileStack);
284 sProfilerOverhead += currentOverhead;
285 OOJSExcludeTimeFromLimiter(currentOverhead);
286 if (!startOwnedByFrame)
298 OOLog(
@"script.javaScript.trace.ON",
@">> %s [ON]",
function);
302 *frame = (OOJSProfileStackFrame) {
303 .back = sProfileStack,
305 .function =
function,
307 .total = &sProfilerTotalNativeTime
309 sProfileStack = frame;
320 OOJSProfileStackFrame* search = sProfileStack;
321 while (search != NULL && search != frame)
322 search = search->back;
327 NSAutoreleasePool* pool = [NSAutoreleasePool new];
332 assert(sProfileStack != NULL);
334 done = (sProfileStack == frame);
335 UpdateProfileForFrame(now, sProfileStack);
346 sProfilerOverhead += currentOverhead;
347 OOJSExcludeTimeFromLimiter(currentOverhead);
353static void UpdateProfileForFrame(
OOHighResTimeValue now, OOJSProfileStackFrame* frame)
355 sProfileStack = frame->back;
357 OOTimeProfileEntry* entry = NSMapGet(sProfileInfo, frame->key);
359 entry = [[OOTimeProfileEntry alloc] initWithCName:frame->function];
360 NSMapInsertKnownAbsent(sProfileInfo, frame->key, entry);
366 [entry addSampleWithTotalTime:time selfTime:selfTime];
368 *(frame->total) += selfTime;
369 if (sProfileStack != NULL)
370 sProfileStack->subTime += time;
372 if (frame->cleanup != NULL)
373 frame->cleanup(frame);
379@implementation OOTimeProfile
388- (NSString*)description
390 double totalTime = [
self totalTime];
392 NSMutableString* result = [NSMutableString stringWithFormat:
393 @"Total time: %g ms\n"
394 "JavaScript: %g ms, native: %g ms\n"
395 "Counted towards limit: %g ms, excluded: %g ms\n"
396 "Profiler overhead: %g ms",
398 [
self javaScriptTime] * 1000.0, [
self nativeTime] * 1000.0,
399 [
self nonExtensionTime] * 1000.0, [
self extensionTime] * 1000.0,
400 [
self profilerOverhead] * 1000.0];
402 NSArray* profileEntries = [
self profileEntries];
403 NSUInteger i,
count = [profileEntries count];
405 [result appendString:@"\n NAME T COUNT TOTAL SELF TOTAL% SELF% SELFMAX"];
406 for (i = 0; i <
count; i++) {
407 OOTimeProfileEntry* entry = [profileEntries objectAtIndex:i];
409 double totalPc = [entry totalTimeSum] * 100.0 / totalTime;
410 double selfPc = [entry selfTimeSum] * 100.0 / totalTime;
412 [result appendFormat:@"\n%60s %c%7lu %8.2f %8.2f %5.1f %5.1f %8.2f",
413 [[entry function] UTF8String],
414 [entry isJavaScriptFrame] ? 'J' : 'N',
415 (unsigned long)[entry hitCount], [entry totalTimeSum] * 1000.0, [entry selfTimeSum] * 1000.0, totalPc, selfPc, [entry selfTimeMax] * 1000.0];
427- (void)setTotalTime:(
double)value
432- (double)javaScriptTime
434 return _javaScriptTime;
437- (void)setJavaScriptTime:(
double)value
439 _javaScriptTime = value;
447- (void)setNativeTime:(
double)value
452- (double)extensionTime
454 return _extensionTime;
457- (void)setExtensionTime:(
double)value
459 _extensionTime = value;
462- (double)nonExtensionTime
464 return _totalTime - _extensionTime;
467- (double)profilerOverhead
469 return _profilerOverhead;
472- (void)setProfilerOverhead:(
double)value
474 _profilerOverhead = value;
477- (NSArray*)profileEntries
479 return _profileEntries;
482- (void)setProfileEntries:(NSArray*)value
484 if (_profileEntries != value) {
485 [_profileEntries release];
486 _profileEntries = [value retain];
490- (JSValue)oo_jsValueInContext:(JSContext*)context
495- (NSDictionary*)propertyListRepresentation
497 NSArray* profileEntries = [
self profileEntries];
498 NSMutableArray* convertedEntries = [NSMutableArray arrayWithCapacity:[profileEntries count]];
499 OOTimeProfileEntry* entry =
nil;
500 foreach (entry, profileEntries) {
501 [convertedEntries addObject:[entry propertyListRepresentation]];
504 return [NSDictionary dictionaryWithObjectsAndKeys:
505 convertedEntries, @"profiles",
506 [NSNumber numberWithDouble:[
self totalTime]], @"totalTime",
507 [NSNumber numberWithDouble:[
self javaScriptTime]], @"javaScriptTime",
508 [NSNumber numberWithDouble:[
self nativeTime]], @"nativeTime",
509 [NSNumber numberWithDouble:[
self extensionTime]], @"extensionTime",
510 [NSNumber numberWithDouble:[
self nonExtensionTime]], @"nonExtensionTime",
511 [NSNumber numberWithDouble:[
self profilerOverhead]], @"profilerOverhead",
517@implementation OOTimeProfileEntry
519- (id)initWithCName:(const
char*)name
521 NSAssert(sProfiling,
@"Can't create profile entries while not profiling.");
523 if ((
self = [super init])) {
525 _function = [[NSString stringWithUTF8String:name] retain];
532#if OOJS_QUICKJS_CALL_HOOK
533- (id)initWithJSFunction:(JSValueConst)function context:(JSContext*)context
535 if ((
self = [
self initWithCName:NULL])) {
538 _isJavaScriptFrame = YES;
540 JSAtom nameAtom, fileAtom;
542 NSString* funcName =
nil;
543 NSString* location =
nil;
544 if (JS_GetFunctionInfo(context,
function, &nameAtom, &fileAtom, &line)) {
545 if (nameAtom != JS_ATOM_NULL) {
547 JS_FreeAtom(context, nameAtom);
549 if (fileAtom != JS_ATOM_NULL) {
550 location = [NSString stringWithFormat:@"%@:%i", OOStringFromJSID(fileAtom), line];
551 JS_FreeAtom(context, fileAtom);
554 if ([funcName length] == 0)
555 funcName =
@"<anonymous>";
557 if (location !=
nil) {
558 _function = [[NSString alloc] initWithFormat:@"(%@) %@", location, funcName];
560 _function = [funcName retain];
579 _totalTimeSum += totalTime;
580 _selfTimeSum += selfTime;
581 _totalTimeMax = fmax(_totalTimeMax, totalTime);
582 _selfTimeMax = fmax(_selfTimeMax, selfTime);
585- (NSString*)description
588 return [NSString stringWithFormat:@"%@: --", _function];
591 float totalTimeSum = _totalTimeSum * 1000.0;
592 float selfTimeSum = _selfTimeSum * 1000.0;
593 float totalTimeMax = _totalTimeMax * 1000.0;
594 float selfTimeMax = _selfTimeMax * 1000.0;
596 if (totalTimeSum == selfTimeSum && totalTimeMax == selfTimeMax) {
597 if (_hitCount == 1) {
598 return [NSString stringWithFormat:@"%@: 1 time, %g ms", _function, totalTimeSum];
600 return [NSString stringWithFormat:@"%@: %lu times, total %g ms, avg %g ms, max %g ms", _function, _hitCount, totalTimeSum, totalTimeSum / _hitCount, totalTimeMax];
603 if (_hitCount == 1) {
604 return [NSString stringWithFormat:@"%@: 1 time, %g ms (self %g ms)", _function, totalTimeSum, selfTimeSum];
606 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];
616- (NSUInteger)hitCount
621- (double)totalTimeSum
623 return _totalTimeSum;
631- (double)totalTimeAverage
633 return _hitCount ? (_totalTimeSum / _hitCount) : 0.0;
636- (double)selfTimeAverage
638 return _hitCount ? (_selfTimeSum / _hitCount) : 0.0;
641- (double)totalTimeMax
643 return _totalTimeMax;
651- (BOOL)isJavaScriptFrame
653 return _isJavaScriptFrame;
656- (NSComparisonResult)compareByTotalTime:(OOTimeProfileEntry*)other
658 return -[
self compareByTotalTimeReverse:other];
661- (NSComparisonResult)compareByTotalTimeReverse:(OOTimeProfileEntry*)other
663 double selfTotal = [
self totalTimeSum];
664 double otherTotal = [other totalTimeSum];
666 if (selfTotal < otherTotal)
667 return NSOrderedDescending;
668 if (selfTotal > otherTotal)
669 return NSOrderedAscending;
670 return NSOrderedSame;
673- (NSComparisonResult)compareBySelfTime:(OOTimeProfileEntry*)other
675 return -[
self compareBySelfTimeReverse:other];
678- (NSComparisonResult)compareBySelfTimeReverse:(OOTimeProfileEntry*)other
680 double selfTotal = [
self selfTimeSum];
681 double otherTotal = [other selfTimeSum];
683 if (selfTotal < otherTotal)
684 return NSOrderedDescending;
685 if (selfTotal > otherTotal)
686 return NSOrderedAscending;
687 return NSOrderedSame;
690- (JSValue)oo_jsValueInContext:(JSContext*)context
695- (NSDictionary*)propertyListRepresentation
697 return [NSDictionary dictionaryWithObjectsAndKeys:
699 [NSNumber numberWithUnsignedInteger:[
self hitCount]], @"hitCount",
700 [NSNumber numberWithDouble:[
self totalTimeSum]], @"totalTimeSum",
701 [NSNumber numberWithDouble:[
self selfTimeSum]], @"selfTimeSum",
702 [NSNumber numberWithDouble:[
self totalTimeAverage]], @"totalTimeAverage",
703 [NSNumber numberWithDouble:[
self selfTimeAverage]], @"selfTimeAverage",
704 [NSNumber numberWithDouble:[
self totalTimeMax]], @"totalTimeMax",
705 [NSNumber numberWithDouble:[
self selfTimeMax]], @"selfTimeMax",
706 [NSNumber numberWithBool:[
self isJavaScriptFrame]], @"isJavaScriptFrame",
#define OOJSProfileExit(frame)
#define OOJSProfileEnter(frame, function)
OOTimeDelta OOJSGetTimeLimiterLimit(void)
void OOJSPauseTimeLimiter(void)
NSString * OOStringFromJSID(jsid propID)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
void OOJSResumeTimeLimiter(void)
#define OOLog(class, format,...)
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
uint64_t OOHighResTimeValue
#define OODisposeHighResTime(time)
void OOJSTimeManagementInit(OOJavaScriptEngine *engine, JSRuntime *runtime)