Revision c57c846a qemu-timer.c

b/qemu-timer.c
64 64
static QEMUTimer *icount_rt_timer;
65 65
static QEMUTimer *icount_vm_timer;
66 66

  
67

  
68
/***********************************************************/
69
/* real time host monotonic timer */
70

  
71

  
72
static int64_t get_clock_realtime(void)
73
{
74
    struct timeval tv;
75

  
76
    gettimeofday(&tv, NULL);
77
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
78
}
79

  
80
#ifdef WIN32
81

  
82
static int64_t clock_freq;
83

  
84
static void init_get_clock(void)
85
{
86
    LARGE_INTEGER freq;
87
    int ret;
88
    ret = QueryPerformanceFrequency(&freq);
89
    if (ret == 0) {
90
        fprintf(stderr, "Could not calibrate ticks\n");
91
        exit(1);
92
    }
93
    clock_freq = freq.QuadPart;
94
}
95

  
96
static int64_t get_clock(void)
97
{
98
    LARGE_INTEGER ti;
99
    QueryPerformanceCounter(&ti);
100
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
101
}
102

  
103
#else
104

  
105
static int use_rt_clock;
106

  
107
static void init_get_clock(void)
108
{
109
    use_rt_clock = 0;
110
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
111
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
112
    {
113
        struct timespec ts;
114
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
115
            use_rt_clock = 1;
116
        }
117
    }
118
#endif
119
}
120

  
121
static int64_t get_clock(void)
122
{
123
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
124
	|| defined(__DragonFly__) || defined(__FreeBSD_kernel__)
125
    if (use_rt_clock) {
126
        struct timespec ts;
127
        clock_gettime(CLOCK_MONOTONIC, &ts);
128
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
129
    } else
130
#endif
131
    {
132
        /* XXX: using gettimeofday leads to problems if the date
133
           changes, so it should be avoided. */
134
        return get_clock_realtime();
135
    }
136
}
137
#endif
138

  
139 67
/***********************************************************/
140 68
/* guest cycle counter */
141 69

  
......
614 542

  
615 543
void init_clocks(void)
616 544
{
617
    init_get_clock();
618 545
    rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
619 546
    vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
620 547
    host_clock = qemu_new_clock(QEMU_CLOCK_HOST);

Also available in: Unified diff