]> git.cworth.org Git - apitrace/blob - common/os_time.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / common / os_time.hpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 /*
27  * Simple OS time measurement abstraction.
28  */
29
30 #ifndef _OS_TIME_HPP_
31 #define _OS_TIME_HPP_
32
33
34 #if defined(_WIN32)
35 #  include <windows.h>
36 #else
37 #  if defined(__linux__)
38 #    include <time.h>
39 #  elif defined(__APPLE__)
40 #    include <mach/mach_time.h>
41 #  else
42 #    include <sys/time.h>
43 #  endif
44 #  include <unistd.h>
45 #endif
46
47
48 namespace os {
49
50     // OS dependent time frequency
51 #if defined(_WIN32) || defined(__APPLE__)
52     // runtime variable on Windows and MacOSX
53     extern long long timeFrequency;
54 #elif defined(__linux__)
55     // nanoseconds on Linux
56     static const long long timeFrequency = 1000000000LL;
57 #else
58     // microseconds on Unices
59     static const long long timeFrequency = 1000000LL;
60 #endif
61
62     // Time from an unknown base in a unit determined by timeFrequency
63     inline long long
64     getTime(void) {
65 #if defined(_WIN32)
66         if (!timeFrequency) {
67             LARGE_INTEGER frequency;
68             QueryPerformanceFrequency(&frequency);
69             timeFrequency = frequency.QuadPart;
70         }
71         LARGE_INTEGER counter;
72         QueryPerformanceCounter(&counter);
73         return counter.QuadPart;
74 #elif defined(__linux__)
75         struct timespec tp;
76         if (clock_gettime(CLOCK_MONOTONIC, &tp) == -1) {
77             return 0;
78         }
79         return tp.tv_sec * 1000000000LL + tp.tv_nsec;
80 #elif defined(__APPLE__)
81         if (!timeFrequency) {
82             mach_timebase_info_data_t timebaseInfo;
83             mach_timebase_info(&timebaseInfo);
84             timeFrequency = 1000000000LL * timebaseInfo.denom / timebaseInfo.numer;
85         }
86         return mach_absolute_time();
87 #else
88         struct timeval tv;
89         gettimeofday(&tv, NULL);
90         return tv.tv_sec * 1000000LL + tv.tv_usec;
91 #endif
92     }
93
94     // Suspend execution
95     inline void
96     sleep(unsigned long usecs) {
97 #if defined(_WIN32)
98         Sleep((usecs + 999) / 1000);
99 #else
100         usleep(usecs);
101 #endif
102     }
103
104 } /* namespace os */
105
106 #endif /* _OS_TIME_HPP_ */