Oolite
Loading...
Searching...
No Matches
OOProfilingStopwatch.h
Go to the documentation of this file.
1/*
2
3OOProfilingStopwatch.h
4Oolite
5
6Testing utility to monitor elapsed times at high precision.
7
8
9Copyright (C) 2010-2013 Jens Ayton and contributors
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files (the "Software"), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28
29*/
30
31#ifndef OOSTOPWATCH_STANDALONE
32#import "OOFoundation.h"
34#import "OOTypes.h"
35#endif
36
37/* Platform-specific high-resolution timer:
38
39 OOHighResTimeValue is a time value. This could be a scalar, struct or pointer.
40 OOHighResTimeValue OOGetHighResTime(void) returns the current time.
41 OODisposeHighResTime() destroys an existing OOHighResTimeValue, if necessary.
42 It must do nothing if the value passed is zeroed out.
43 OOCopyHighResTime(x) returns a timer value equal to x.
44 OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
45 returns the difference between two time values, in seconds.
46*/
47
48#if OOLITE_MAC_OS_X
49
50// Mac OS X: always use MACH_ABSOLUTE_TIME.
51#define OO_PROFILING_STOPWATCH_MACH_ABSOLUTE_TIME 1
52#import <mach/mach_time.h>
53
54typedef uint64_t OOHighResTimeValue;
55
56#define OOGetHighResTime mach_absolute_time
57#define OODisposeHighResTime(time) \
58 do { \
59 (void)time; \
60 } while (0)
61#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
62
63#elif OOLITE_WINDOWS
64
65// Windows: if standalone, use timeGetTime...
66#if OOSTOPWATCH_STANDALONE
67#define OO_PROFILING_STOPWATCH_WINDOWS 1
68typedef DWORD OOHighResTimeValue; // Rolls over once every 50 days, but we can live with that.
69
70// Note: timeGetTime returns time in milliseconds. This results in lower time resolution in Windows, but at this stage I
71// don't think we need to do something about it. If we really need microseond precision, we might consider an implementation
72// based on the Win32 API QueryPerformanceCounter function - Nikos 20100615.
73#define OOGetHighResTime timeGetTime
74#define OODisposeHighResTime(time) \
75 do { \
76 (void)time; \
77 } while (0)
78#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
79
80#elif OOJS_QUICKJS
81/* ...with QuickJS, which has no JS_Now(), use the performance counter, so
82 the profiler can time short functions (timeGetTime() only counts whole
83 milliseconds)...
84*/
85#define OO_PROFILING_STOPWATCH_PERFORMANCE_COUNTER 1
86typedef int64_t OOHighResTimeValue;
87
89{
90 LARGE_INTEGER counter;
91 QueryPerformanceCounter(&counter);
92 return counter.QuadPart;
93}
94
95#define OODisposeHighResTime(time) \
96 do { \
97 (void)time; \
98 } while (0)
99#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
100
101#else
102/* ...otherwise, use JS_Now() for higher precision. The Windows implementation
103 does the messy work of calibrating performance counters against low-res
104 timers.
105*/
106#define OO_PROFILING_STOPWATCH_JS_NOW 1
107#endif
108
109#else
110
111// Other platforms (presumed unixy): use gettimeofday().
112
113#define OO_PROFILING_STOPWATCH_GETTIMEOFDAY 1
114#include <sys/time.h>
115
116typedef struct timeval OOHighResTimeValue;
117
119{
120 struct timeval tv;
121 gettimeofday(&tv, NULL);
122 return tv;
123}
124
125#define OODisposeHighResTime(time) \
126 do { \
127 (void)time; \
128 } while (0)
129#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
130
131#endif
132
133#if OO_PROFILING_STOPWATCH_JS_NOW
134#include <jsapi.h>
135typedef int64 OOHighResTimeValue;
136
137#define OOGetHighResTime JS_Now
138#define OODisposeHighResTime(time) \
139 do { \
140 (void)time; \
141 } while (0)
142#define OOCopyHighResTime(time) ((OOHighResTimeValue)time)
143#endif
144
146
147@interface OOProfilingStopwatch : NSObject {
148@private
152}
153
154+ (instancetype)stopwatch; // New stopwatch is initially started.
155
156- (void)start;
157- (void)stop;
158- (OOTimeDelta)currentTime; // Returns stop time - start time if stopped, or now - start time if running.
159
160/* Resets timer to zero, returning the current value. This is drift-free, i.e.
161 if it is called twice in a row while running the sum is an accurate time
162 since the timer started.
163*/
164- (OOTimeDelta)reset;
165
166@end
#define OOINLINE
OOTimeDelta OOHighResTimeDeltaInSeconds(OOHighResTimeValue startTime, OOHighResTimeValue endTime)
uint64_t OOHighResTimeValue
#define OOGetHighResTime
double OOTimeDelta
Definition OOTypes.h:193
OOHighResTimeValue _start