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#define OOJSStartTimeLimiter() OOJSStartTimeLimiterWithTimeLimit(0.0)
49
50#ifndef NDEBUG
51#define OOJSStartTimeLimiterWithTimeLimit(limit) OOJSStartTimeLimiterWithTimeLimit_(limit, OOLOG_FILE_NAME, __LINE__)
52#define OOJSStopTimeLimiter() OOJSStopTimeLimiter_(OOLOG_FILE_NAME, __LINE__)
53void OOJSStartTimeLimiterWithTimeLimit_(OOTimeDelta limit, const char* file, unsigned line);
54void OOJSStopTimeLimiter_(const char* file, unsigned line);
55#else
57void OOJSStopTimeLimiter(void);
58#endif
59
60#define kOOJSLongTimeLimit (5.0)
61
62#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 OOJSCopyTimeLimiterNominalStartTime()
76 Copy the nominal start time for the time limiter. This is the actual time
77 with any time extensions (paused periods) added in.
78
79 OOJSResetTimeLimiter()
80 Set the time limiter start time to now.
81
82 OOJSGetTimeLimiterLimit()
83 OOJSSetTimeLimiterLimit()
84 Manipulate the timeout.
85*/
86
87@class OOTimeProfile, OOTimeProfileEntry;
88
89void OOJSBeginProfiling(BOOL trace);
90OOTimeProfile* OOJSEndProfiling(void);
91BOOL OOJSIsProfiling(void);
92
94
95void OOJSResetTimeLimiter(void);
98
99/*
100 NOTE: the profiler declarations that need to be visible to functions that
101 are profiled are found in OOJSEngineNativeWrappers.h.
102*/
103
104@interface OOTimeProfile : NSObject {
105@private
106 double _totalTime;
107 double _nativeTime;
108 double _extensionTime;
109#ifdef MOZ_TRACE_JSCALLS
110 double _javaScriptTime;
111#endif
112
113 double _profilerOverhead;
114
115 NSArray* _profileEntries;
116}
117
118- (double)totalTime;
119- (double)javaScriptTime;
120- (double)nativeTime;
121- (double)extensionTime;
122- (double)nonExtensionTime;
123- (double)profilerOverhead;
124
125- (NSArray*)profileEntries; // Array of OOTimeProfileEntry
126
127@end
128
129@interface OOTimeProfileEntry : NSObject {
130@private
131 NSString* _function;
132 unsigned long _hitCount;
133 double _totalTimeSum;
134 double _selfTimeSum;
135 double _totalTimeMax;
136 double _selfTimeMax;
137#ifdef MOZ_TRACE_JSCALLS
138 JSFunction* _jsFunction;
139#endif
140}
141
142- (NSString*)description;
143
144- (NSString*)function;
145- (NSUInteger)hitCount;
146- (double)totalTimeSum;
147- (double)selfTimeSum;
148- (double)totalTimeAverage;
149- (double)selfTimeAverage;
150- (double)totalTimeMax;
151- (double)selfTimeMax;
152- (BOOL)isJavaScriptFrame;
153
154- (NSComparisonResult)compareByTotalTime:(OOTimeProfileEntry*)other;
155- (NSComparisonResult)compareByTotalTimeReverse:(OOTimeProfileEntry*)other;
156- (NSComparisonResult)compareBySelfTime:(OOTimeProfileEntry*)other;
157- (NSComparisonResult)compareBySelfTimeReverse:(OOTimeProfileEntry*)other;
158
159@end
160
161#endif
162
163@class OOJavaScriptEngine;
164
165void OOJSTimeManagementInit(OOJavaScriptEngine* engine, JSRuntime* runtime);
#define OOJSStopTimeLimiter()
void OOJSStartTimeLimiterWithTimeLimit_(OOTimeDelta limit, const char *file, unsigned line)
void OOJSTimeManagementInit(OOJavaScriptEngine *engine, JSRuntime *runtime)
void OOJSStopTimeLimiter_(const char *file, unsigned line)
#define OOJSStartTimeLimiterWithTimeLimit(limit)
void OOJSResetTimeLimiter(void)
OOHighResTimeValue OOJSCopyTimeLimiterNominalStartTime(void)
OOTimeDelta OOJSGetTimeLimiterLimit(void)
void OOJSSetTimeLimiterLimit(OOTimeDelta limit)
uint64_t OOHighResTimeValue
double OOTimeDelta
Definition OOTypes.h:193