Revision c57c846a qemu-timer.h
b/qemu-timer.h | ||
---|---|---|
2 | 2 |
#define QEMU_TIMER_H |
3 | 3 |
|
4 | 4 |
#include "qemu-common.h" |
5 |
#include <time.h> |
|
6 |
#include <sys/time.h> |
|
7 |
|
|
8 |
#ifdef _WIN32 |
|
9 |
#include <windows.h> |
|
10 |
#include <mmsystem.h> |
|
11 |
#endif |
|
5 | 12 |
|
6 | 13 |
/* timers */ |
7 | 14 |
|
... | ... | |
52 | 59 |
return 1000000000LL; |
53 | 60 |
} |
54 | 61 |
|
62 |
/* compute with 96 bit intermediate result: (a*b)/c */ |
|
63 |
static inline uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
|
64 |
{ |
|
65 |
union { |
|
66 |
uint64_t ll; |
|
67 |
struct { |
|
68 |
#ifdef HOST_WORDS_BIGENDIAN |
|
69 |
uint32_t high, low; |
|
70 |
#else |
|
71 |
uint32_t low, high; |
|
72 |
#endif |
|
73 |
} l; |
|
74 |
} u, res; |
|
75 |
uint64_t rl, rh; |
|
76 |
|
|
77 |
u.ll = a; |
|
78 |
rl = (uint64_t)u.l.low * (uint64_t)b; |
|
79 |
rh = (uint64_t)u.l.high * (uint64_t)b; |
|
80 |
rh += (rl >> 32); |
|
81 |
res.l.high = rh / c; |
|
82 |
res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
|
83 |
return res.ll; |
|
84 |
} |
|
85 |
|
|
86 |
/* real time host monotonic timer */ |
|
87 |
static inline int64_t get_clock_realtime(void) |
|
88 |
{ |
|
89 |
struct timeval tv; |
|
90 |
|
|
91 |
gettimeofday(&tv, NULL); |
|
92 |
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
|
93 |
} |
|
94 |
|
|
95 |
/* Warning: don't insert tracepoints into these functions, they are |
|
96 |
also used by simpletrace backend and tracepoints would cause |
|
97 |
an infinite recursion! */ |
|
98 |
#ifdef _WIN32 |
|
99 |
extern int64_t clock_freq; |
|
100 |
|
|
101 |
static inline int64_t get_clock(void) |
|
102 |
{ |
|
103 |
LARGE_INTEGER ti; |
|
104 |
QueryPerformanceCounter(&ti); |
|
105 |
return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq); |
|
106 |
} |
|
107 |
|
|
108 |
#else |
|
109 |
|
|
110 |
extern int use_rt_clock; |
|
111 |
|
|
112 |
static inline int64_t get_clock(void) |
|
113 |
{ |
|
114 |
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \ |
|
115 |
|| defined(__DragonFly__) || defined(__FreeBSD_kernel__) |
|
116 |
if (use_rt_clock) { |
|
117 |
struct timespec ts; |
|
118 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
|
119 |
return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
|
120 |
} else |
|
121 |
#endif |
|
122 |
{ |
|
123 |
/* XXX: using gettimeofday leads to problems if the date |
|
124 |
changes, so it should be avoided. */ |
|
125 |
return get_clock_realtime(); |
|
126 |
} |
|
127 |
} |
|
128 |
#endif |
|
55 | 129 |
|
56 | 130 |
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts); |
57 | 131 |
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts); |
Also available in: Unified diff