xref: /qemu/util/qemu-timer-common.c (revision 8814b1327c0070d440ec1480888b77eb27af43f8)
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  */
24aafd7584SPeter Maydell #include "qemu/osdep.h"
251de7afc9SPaolo Bonzini #include "qemu/timer.h"
26c57c846aSBlue Swirl 
27c57c846aSBlue Swirl /***********************************************************/
28c57c846aSBlue Swirl /* real time host monotonic timer */
29c57c846aSBlue Swirl 
30*4d834039SKeith Packard int64_t clock_start;
31*4d834039SKeith Packard 
32c57c846aSBlue Swirl #ifdef _WIN32
33c57c846aSBlue Swirl 
34c57c846aSBlue Swirl int64_t clock_freq;
35c57c846aSBlue Swirl 
init_get_clock(void)36c57c846aSBlue Swirl static void __attribute__((constructor)) init_get_clock(void)
37c57c846aSBlue Swirl {
38c57c846aSBlue Swirl     LARGE_INTEGER freq;
39c57c846aSBlue Swirl     int ret;
40c57c846aSBlue Swirl     ret = QueryPerformanceFrequency(&freq);
41c57c846aSBlue Swirl     if (ret == 0) {
42c57c846aSBlue Swirl         fprintf(stderr, "Could not calibrate ticks\n");
43c57c846aSBlue Swirl         exit(1);
44c57c846aSBlue Swirl     }
45c57c846aSBlue Swirl     clock_freq = freq.QuadPart;
46*4d834039SKeith Packard     clock_start = get_clock();
47c57c846aSBlue Swirl }
48c57c846aSBlue Swirl 
49c57c846aSBlue Swirl #else
50c57c846aSBlue Swirl 
51c57c846aSBlue Swirl int use_rt_clock;
52c57c846aSBlue Swirl 
init_get_clock(void)53c57c846aSBlue Swirl static void __attribute__((constructor)) init_get_clock(void)
54c57c846aSBlue Swirl {
55c57c846aSBlue Swirl     struct timespec ts;
56a284f798SPeter Maydell 
57a284f798SPeter Maydell     use_rt_clock = 0;
58c57c846aSBlue Swirl     if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
59c57c846aSBlue Swirl         use_rt_clock = 1;
60c57c846aSBlue Swirl     }
61*4d834039SKeith Packard     clock_start = get_clock();
62c57c846aSBlue Swirl }
63c57c846aSBlue Swirl #endif
64