]> git.cworth.org Git - apitrace/blob - common/os_time.hpp
Merge remote-tracking branch 'github/master' into profile-gui
[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 #elif defined(__linux__)
37 #include <time.h>
38 #elif defined(__APPLE__)
39 #include <mach/mach_time.h>
40 #else
41 #include <sys/time.h>
42 #endif
43
44
45 namespace os {
46
47     // OS dependent time frequency
48 #if defined(_WIN32) || defined(__APPLE__)
49     // runtime variable on Windows and MacOSX
50     extern long long timeFrequency;
51 #elif defined(__linux__)
52     // nanoseconds on Linux
53     static const long long timeFrequency = 1000000000LL;
54 #else
55     // microseconds on Unices
56     static const long long timeFrequency = 1000000LL;
57 #endif
58
59     // Time from an unknown base in a unit determined by timeFrequency
60     inline long long
61     getTime(void) {
62 #if defined(_WIN32)
63         if (!timeFrequency) {
64             LARGE_INTEGER frequency;
65             QueryPerformanceFrequency(&frequency);
66             timeFrequency = frequency.QuadPart;
67         }
68         LARGE_INTEGER counter;
69         QueryPerformanceCounter(&counter);
70         return counter.QuadPart;
71 #elif defined(__linux__)
72         struct timespec tp;
73         if (clock_gettime(CLOCK_REALTIME, &tp) == -1) {
74             return 0;
75         }
76         return tp.tv_sec * 1000000000LL + tp.tv_nsec;
77 #elif defined(__APPLE__)
78         if (!timeFrequency) {
79             mach_timebase_info_data_t timebaseInfo;
80             mach_timebase_info(&timebaseInfo);
81             timeFrequency = 1000000000LL * timebaseInfo.denom / timebaseInfo.numer;
82         }
83         return mach_absolute_time();
84 #else
85         struct timeval tv;
86         gettimeofday(&tv, NULL);
87         return tv.tv_sec * 1000000LL + tv.tv_usec;
88 #endif
89     }
90
91
92 } /* namespace os */
93
94 #endif /* _OS_TIME_HPP_ */