]>
git.proxmox.com Git - qemu.git/blob - include/qemu/timer.h
e4a64791378294c1736277c94e26594867d6d0e7
4 #include "qemu-common.h"
5 #include "qemu/main-loop.h"
6 #include "qemu/notify.h"
10 #define SCALE_MS 1000000
14 #define QEMU_CLOCK_REALTIME 0
15 #define QEMU_CLOCK_VIRTUAL 1
16 #define QEMU_CLOCK_HOST 2
18 typedef struct QEMUClock QEMUClock
;
19 typedef void QEMUTimerCB(void *opaque
);
21 /* The real time clock should be used only for stuff which does not
22 change the virtual machine state, as it is run even if the virtual
23 machine is stopped. The real time clock has a frequency of 1000
25 extern QEMUClock
*rt_clock
;
27 /* The virtual clock is only run during the emulation. It is stopped
28 when the virtual machine is stopped. Virtual timers use a high
29 precision clock, usually cpu cycles (use ticks_per_sec). */
30 extern QEMUClock
*vm_clock
;
32 /* The host clock should be use for device models that emulate accurate
33 real time sources. It will continue to run when the virtual machine
34 is suspended, and it will reflect system time changes the host may
35 undergo (e.g. due to NTP). The host clock has the same precision as
37 extern QEMUClock
*host_clock
;
39 int64_t qemu_get_clock_ns(QEMUClock
*clock
);
40 int64_t qemu_clock_has_timers(QEMUClock
*clock
);
41 int64_t qemu_clock_expired(QEMUClock
*clock
);
42 int64_t qemu_clock_deadline(QEMUClock
*clock
);
45 * qemu_clock_deadline_ns:
46 * @clock: the clock to operate on
48 * Calculate the timeout of the earliest expiring timer
49 * in nanoseconds, or -1 if no timer is set to expire.
51 * Returns: time until expiry in nanoseconds or -1
53 int64_t qemu_clock_deadline_ns(QEMUClock
*clock
);
56 * qemu_timeout_ns_to_ms:
57 * @ns: nanosecond timeout value
59 * Convert a nanosecond timeout value (or -1) to
60 * a millisecond value (or -1), always rounding up.
62 * Returns: millisecond timeout value
64 int qemu_timeout_ns_to_ms(int64_t ns
);
68 * @fds: Array of file descriptors
69 * @nfds: number of file descriptors
70 * @timeout: timeout in nanoseconds
72 * Perform a poll like g_poll but with a timeout in nanoseconds.
73 * See g_poll documentation for further details.
75 * Returns: number of fds ready
77 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
);
78 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
);
79 void qemu_clock_warp(QEMUClock
*clock
);
81 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
);
82 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
,
85 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
86 QEMUTimerCB
*cb
, void *opaque
);
87 void qemu_free_timer(QEMUTimer
*ts
);
88 void qemu_del_timer(QEMUTimer
*ts
);
89 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
);
90 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
);
91 bool timer_pending(QEMUTimer
*ts
);
92 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
93 uint64_t timer_expire_time_ns(QEMUTimer
*ts
);
95 void qemu_run_timers(QEMUClock
*clock
);
96 void qemu_run_all_timers(void);
97 void configure_alarms(char const *opt
);
98 void init_clocks(void);
99 int init_timer_alarm(void);
101 int64_t cpu_get_ticks(void);
102 void cpu_enable_ticks(void);
103 void cpu_disable_ticks(void);
106 * qemu_soonest_timeout:
107 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
108 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
110 * Calculates the soonest of two timeout values. -1 means infinite, which
111 * is later than any other value.
113 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
115 static inline int64_t qemu_soonest_timeout(int64_t timeout1
, int64_t timeout2
)
117 /* we can abuse the fact that -1 (which means infinite) is a maximal
118 * value when cast to unsigned. As this is disgusting, it's kept in
119 * one inline function.
121 return ((uint64_t) timeout1
< (uint64_t) timeout2
) ? timeout1
: timeout2
;
124 static inline QEMUTimer
*qemu_new_timer_ns(QEMUClock
*clock
, QEMUTimerCB
*cb
,
127 return qemu_new_timer(clock
, SCALE_NS
, cb
, opaque
);
130 static inline QEMUTimer
*qemu_new_timer_ms(QEMUClock
*clock
, QEMUTimerCB
*cb
,
133 return qemu_new_timer(clock
, SCALE_MS
, cb
, opaque
);
136 static inline int64_t qemu_get_clock_ms(QEMUClock
*clock
)
138 return qemu_get_clock_ns(clock
) / SCALE_MS
;
141 static inline int64_t get_ticks_per_sec(void)
146 /* real time host monotonic timer */
147 static inline int64_t get_clock_realtime(void)
151 gettimeofday(&tv
, NULL
);
152 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
155 /* Warning: don't insert tracepoints into these functions, they are
156 also used by simpletrace backend and tracepoints would cause
157 an infinite recursion! */
159 extern int64_t clock_freq
;
161 static inline int64_t get_clock(void)
164 QueryPerformanceCounter(&ti
);
165 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
170 extern int use_rt_clock
;
172 static inline int64_t get_clock(void)
174 #ifdef CLOCK_MONOTONIC
177 clock_gettime(CLOCK_MONOTONIC
, &ts
);
178 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
182 /* XXX: using gettimeofday leads to problems if the date
183 changes, so it should be avoided. */
184 return get_clock_realtime();
189 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
);
190 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
);
193 int64_t cpu_get_icount(void);
194 int64_t cpu_get_clock(void);
196 /*******************************************/
197 /* host CPU ticks (if available) */
199 #if defined(_ARCH_PPC)
201 static inline int64_t cpu_get_real_ticks(void)
205 /* This reads timebase in one 64bit go and includes Cell workaround from:
206 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
208 __asm__
__volatile__ ("mftb %0\n\t"
213 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
215 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
216 "mfspr %L0,268\n\t" /* mftb */
217 "mfspr %0,269\n\t" /* mftbu */
220 : "=r" (retval
), "=r" (junk
));
225 #elif defined(__i386__)
227 static inline int64_t cpu_get_real_ticks(void)
230 asm volatile ("rdtsc" : "=A" (val
));
234 #elif defined(__x86_64__)
236 static inline int64_t cpu_get_real_ticks(void)
240 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
247 #elif defined(__hppa__)
249 static inline int64_t cpu_get_real_ticks(void)
252 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
256 #elif defined(__ia64)
258 static inline int64_t cpu_get_real_ticks(void)
261 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
265 #elif defined(__s390__)
267 static inline int64_t cpu_get_real_ticks(void)
270 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
274 #elif defined(__sparc__)
276 static inline int64_t cpu_get_real_ticks (void)
280 asm volatile("rd %%tick,%0" : "=r"(rval
));
283 /* We need an %o or %g register for this. For recent enough gcc
284 there is an "h" constraint for that. Don't bother with that. */
292 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
293 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
298 #elif defined(__mips__) && \
299 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
301 * binutils wants to use rdhwr only on mips32r2
302 * but as linux kernel emulate it, it's fine
306 #define MIPS_RDHWR(rd, value) { \
307 __asm__ __volatile__ (".set push\n\t" \
308 ".set mips32r2\n\t" \
309 "rdhwr %0, "rd"\n\t" \
314 static inline int64_t cpu_get_real_ticks(void)
316 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
318 static uint32_t cyc_per_count
= 0;
320 if (!cyc_per_count
) {
321 MIPS_RDHWR("$3", cyc_per_count
);
324 MIPS_RDHWR("$2", count
);
325 return (int64_t)(count
* cyc_per_count
);
328 #elif defined(__alpha__)
330 static inline int64_t cpu_get_real_ticks(void)
335 asm volatile("rpcc %0" : "=r"(cc
));
342 /* The host CPU doesn't have an easily accessible cycle counter.
343 Just return a monotonically increasing value. This will be
344 totally wrong, but hopefully better than nothing. */
345 static inline int64_t cpu_get_real_ticks (void)
347 static int64_t ticks
= 0;
352 #ifdef CONFIG_PROFILER
353 static inline int64_t profile_getclock(void)
355 return cpu_get_real_ticks();
358 extern int64_t qemu_time
, qemu_time_start
;
359 extern int64_t tlb_flush_time
;
360 extern int64_t dev_time
;