Oolite
Loading...
Searching...
No Matches
OOJSEngineTimeManagement.h
Go to the documentation of this file.
1/*
2
3OOJSEngineTimeManagement.h
4
5Functionality related to time limiting and profiling of JavaScript code.
6
7
8Copyright (C) 2010-2013 Jens Ayton
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27
28*/
29
31
32/* Time Limiter
33
34 The time limiter stops scripts from running an arbitrarily long time.
35
36 The time limiter must be started before calling into JavaScript code.
37 Calls to OOJSStartTimeLimiter() and OOJSStopTimeLimiter() must be balanced,
38 and may be nested.
39 OOJSStartTimeLimiterWithTimeLimit() is like OOJSStartTimeLimiter(), but
40 specifies a custom time limit. This limit is only used if the limiter is
41 actually stopped.
42
43 The time limiter can be paused and resumed for native operations that are
44 known to be slow. OOJSPauseTimeLimiter() and OOJSResumeTimeLimiter() must
45 be balanced and can be nested, but the nest count may be negative - it is
46 valid to call OOJSResumeTimeLimiter() followed by OOJSPauseTimeLimiter().
47
48 With QuickJS, the limiter functions (OOJSStartTimeLimiterWithTimeLimit(),
49 OOJSStartTimeLimiter(), OOJSStopTimeLimiter(), OOJSPauseTimeLimiter(),
50 OOJSResumeTimeLimiter(), kOOJSLongTimeLimit and, in debug builds,
51 OOJSResetTimeLimiter(), OOJSGetTimeLimiterLimit() and
52 OOJSSetTimeLimiterLimit()) are declared in OOJavaScriptEngine.h and
53 implemented in OOJavaScriptEngine.m. QuickJS polls an interrupt handler
54 while running JS code, so no watchdog thread or operation callback is
55 needed.
56
57 Not ported: the debug-build variants that record the file and line of
58 the last start/stop (OOJSStartTimeLimiterWithTimeLimit_() and
59 OOJSStopTimeLimiter_()), which were only used to diagnose unbalanced
60 calls.
61*/
62
63#if OOJS_PROFILE
64
65/*
66 Profiling support.
67
68 OOJSBeginProfiling(trace), OOJSEndProfiling(), OOJSIsProfiling()
69 Start, stop, and query profiling mode. It is a hard error to start
70 profiling while already profiling.
71
72 If trace is set, all profileable functions will be logged. The actual
73 profile will be of little use in this case because of logging overhead.
74
75 Natives bracketed by OOJS_NATIVE_ENTER()/OOJS_PROFILE_ENTER are
76 profiled, and so are JavaScript functions, through the function call
77 hook Oolite's QuickJS-ng patch adds (the equivalent of SpiderMonkey's
78 MOZ_TRACE_JSCALLS).
79
80 OOJSExcludeTimeFromLimiter()
81 Extend the current time limit by the given time, unless the limiter is
82 paused. Used to keep profiler overhead from counting towards the limit.
83
84 NOTE: the profiler declarations that need to be visible to functions that
85 are profiled are found in OOJavaScriptEngine.h.
86*/
87
88@class OOTimeProfile, OOTimeProfileEntry;
89
90void OOJSBeginProfiling(BOOL trace);
91OOTimeProfile* OOJSEndProfiling(void);
92BOOL OOJSIsProfiling(void);
93
94void OOJSExcludeTimeFromLimiter(OOTimeDelta time);
95
96@interface OOTimeProfile : NSObject {
97@private
98 double _totalTime;
99 double _nativeTime;
100 double _extensionTime;
101 double _javaScriptTime;
102 double _profilerOverhead;
103
104 NSArray* _profileEntries;
105}
106
107- (double)totalTime;
108- (double)javaScriptTime;
109- (double)nativeTime;
110- (double)extensionTime;
111- (double)nonExtensionTime;
112- (double)profilerOverhead;
113
114- (NSArray*)profileEntries; // Array of OOTimeProfileEntry
115
116@end
117
118@interface OOTimeProfileEntry : NSObject {
119@private
120 NSString* _function;
121 unsigned long _hitCount;
122 double _totalTimeSum;
123 double _selfTimeSum;
124 double _totalTimeMax;
125 double _selfTimeMax;
126 BOOL _isJavaScriptFrame;
127}
128
129- (NSString*)function;
130- (NSUInteger)hitCount;
131- (double)totalTimeSum;
132- (double)selfTimeSum;
133- (double)totalTimeAverage;
134- (double)selfTimeAverage;
135- (double)totalTimeMax;
136- (double)selfTimeMax;
137- (BOOL)isJavaScriptFrame;
138
139- (NSComparisonResult)compareByTotalTime:(OOTimeProfileEntry*)other;
140- (NSComparisonResult)compareByTotalTimeReverse:(OOTimeProfileEntry*)other;
141- (NSComparisonResult)compareBySelfTime:(OOTimeProfileEntry*)other;
142- (NSComparisonResult)compareBySelfTimeReverse:(OOTimeProfileEntry*)other;
143
144@end
145
146#endif
147
148@class OOJavaScriptEngine;
149
150/* Formerly installed the watchdog thread and operation callback. The QuickJS
151 interrupt handler is installed by -[OOJavaScriptEngine init], so this
152 does nothing; it is kept for source compatibility.
153*/
154void OOJSTimeManagementInit(OOJavaScriptEngine* engine, JSRuntime* runtime);
void OOJSTimeManagementInit(OOJavaScriptEngine *engine, JSRuntime *runtime)
double OOTimeDelta
Definition OOTypes.h:193