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