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