xref: /qemu/util/qemu-timer-common.c (revision c57c846a80f9306aa2c6cf7efdef45ed42723fac)
1*c57c846aSBlue Swirl /*
2*c57c846aSBlue Swirl  * QEMU System Emulator
3*c57c846aSBlue Swirl  *
4*c57c846aSBlue Swirl  * Copyright (c) 2003-2008 Fabrice Bellard
5*c57c846aSBlue Swirl  *
6*c57c846aSBlue Swirl  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c57c846aSBlue Swirl  * of this software and associated documentation files (the "Software"), to deal
8*c57c846aSBlue Swirl  * in the Software without restriction, including without limitation the rights
9*c57c846aSBlue Swirl  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10*c57c846aSBlue Swirl  * copies of the Software, and to permit persons to whom the Software is
11*c57c846aSBlue Swirl  * furnished to do so, subject to the following conditions:
12*c57c846aSBlue Swirl  *
13*c57c846aSBlue Swirl  * The above copyright notice and this permission notice shall be included in
14*c57c846aSBlue Swirl  * all copies or substantial portions of the Software.
15*c57c846aSBlue Swirl  *
16*c57c846aSBlue Swirl  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c57c846aSBlue Swirl  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c57c846aSBlue Swirl  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*c57c846aSBlue Swirl  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c57c846aSBlue Swirl  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c57c846aSBlue Swirl  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22*c57c846aSBlue Swirl  * THE SOFTWARE.
23*c57c846aSBlue Swirl  */
24*c57c846aSBlue Swirl #include "qemu-timer.h"
25*c57c846aSBlue Swirl 
26*c57c846aSBlue Swirl /***********************************************************/
27*c57c846aSBlue Swirl /* real time host monotonic timer */
28*c57c846aSBlue Swirl 
29*c57c846aSBlue Swirl #ifdef _WIN32
30*c57c846aSBlue Swirl 
31*c57c846aSBlue Swirl int64_t clock_freq;
32*c57c846aSBlue Swirl 
33*c57c846aSBlue Swirl static void __attribute__((constructor)) init_get_clock(void)
34*c57c846aSBlue Swirl {
35*c57c846aSBlue Swirl     LARGE_INTEGER freq;
36*c57c846aSBlue Swirl     int ret;
37*c57c846aSBlue Swirl     ret = QueryPerformanceFrequency(&freq);
38*c57c846aSBlue Swirl     if (ret == 0) {
39*c57c846aSBlue Swirl         fprintf(stderr, "Could not calibrate ticks\n");
40*c57c846aSBlue Swirl         exit(1);
41*c57c846aSBlue Swirl     }
42*c57c846aSBlue Swirl     clock_freq = freq.QuadPart;
43*c57c846aSBlue Swirl }
44*c57c846aSBlue Swirl 
45*c57c846aSBlue Swirl #else
46*c57c846aSBlue Swirl 
47*c57c846aSBlue Swirl int use_rt_clock;
48*c57c846aSBlue Swirl 
49*c57c846aSBlue Swirl static void __attribute__((constructor)) init_get_clock(void)
50*c57c846aSBlue Swirl {
51*c57c846aSBlue Swirl     use_rt_clock = 0;
52*c57c846aSBlue Swirl #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
53*c57c846aSBlue Swirl     || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
54*c57c846aSBlue Swirl     {
55*c57c846aSBlue Swirl         struct timespec ts;
56*c57c846aSBlue Swirl         if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
57*c57c846aSBlue Swirl             use_rt_clock = 1;
58*c57c846aSBlue Swirl         }
59*c57c846aSBlue Swirl     }
60*c57c846aSBlue Swirl #endif
61*c57c846aSBlue Swirl }
62*c57c846aSBlue Swirl #endif
63