]>
git.proxmox.com Git - qemu.git/blob - include/qemu/timer.h
4 #include "qemu-common.h"
5 #include "qemu/main-loop.h"
6 #include "qemu/notify.h"
10 #define SCALE_MS 1000000
14 typedef struct QEMUClock QEMUClock
;
15 typedef void QEMUTimerCB(void *opaque
);
17 /* The real time clock should be used only for stuff which does not
18 change the virtual machine state, as it is run even if the virtual
19 machine is stopped. The real time clock has a frequency of 1000
21 extern QEMUClock
*rt_clock
;
23 /* The virtual clock is only run during the emulation. It is stopped
24 when the virtual machine is stopped. Virtual timers use a high
25 precision clock, usually cpu cycles (use ticks_per_sec). */
26 extern QEMUClock
*vm_clock
;
28 /* The host clock should be use for device models that emulate accurate
29 real time sources. It will continue to run when the virtual machine
30 is suspended, and it will reflect system time changes the host may
31 undergo (e.g. due to NTP). The host clock has the same precision as
33 extern QEMUClock
*host_clock
;
35 int64_t qemu_get_clock_ns(QEMUClock
*clock
);
36 int64_t qemu_clock_has_timers(QEMUClock
*clock
);
37 int64_t qemu_clock_expired(QEMUClock
*clock
);
38 int64_t qemu_clock_deadline(QEMUClock
*clock
);
39 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
);
40 void qemu_clock_warp(QEMUClock
*clock
);
42 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
);
43 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
,
46 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
47 QEMUTimerCB
*cb
, void *opaque
);
48 void qemu_free_timer(QEMUTimer
*ts
);
49 void qemu_del_timer(QEMUTimer
*ts
);
50 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
);
51 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
);
52 bool qemu_timer_pending(QEMUTimer
*ts
);
53 bool qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
54 uint64_t qemu_timer_expire_time_ns(QEMUTimer
*ts
);
56 void qemu_run_timers(QEMUClock
*clock
);
57 void qemu_run_all_timers(void);
58 void configure_alarms(char const *opt
);
59 void init_clocks(void);
60 int init_timer_alarm(void);
62 int64_t cpu_get_ticks(void);
63 void cpu_enable_ticks(void);
64 void cpu_disable_ticks(void);
66 static inline QEMUTimer
*qemu_new_timer_ns(QEMUClock
*clock
, QEMUTimerCB
*cb
,
69 return qemu_new_timer(clock
, SCALE_NS
, cb
, opaque
);
72 static inline QEMUTimer
*qemu_new_timer_ms(QEMUClock
*clock
, QEMUTimerCB
*cb
,
75 return qemu_new_timer(clock
, SCALE_MS
, cb
, opaque
);
78 static inline int64_t qemu_get_clock_ms(QEMUClock
*clock
)
80 return qemu_get_clock_ns(clock
) / SCALE_MS
;
83 static inline int64_t get_ticks_per_sec(void)
88 /* real time host monotonic timer */
89 static inline int64_t get_clock_realtime(void)
93 gettimeofday(&tv
, NULL
);
94 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
97 /* Warning: don't insert tracepoints into these functions, they are
98 also used by simpletrace backend and tracepoints would cause
99 an infinite recursion! */
101 extern int64_t clock_freq
;
103 static inline int64_t get_clock(void)
106 QueryPerformanceCounter(&ti
);
107 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
112 extern int use_rt_clock
;
114 static inline int64_t get_clock(void)
116 #ifdef CLOCK_MONOTONIC
119 clock_gettime(CLOCK_MONOTONIC
, &ts
);
120 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
124 /* XXX: using gettimeofday leads to problems if the date
125 changes, so it should be avoided. */
126 return get_clock_realtime();
131 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
);
132 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
);
135 int64_t cpu_get_icount(void);
136 int64_t cpu_get_clock(void);
138 /*******************************************/
139 /* host CPU ticks (if available) */
141 #if defined(_ARCH_PPC)
143 static inline int64_t cpu_get_real_ticks(void)
147 /* This reads timebase in one 64bit go and includes Cell workaround from:
148 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
150 __asm__
__volatile__ ("mftb %0\n\t"
155 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
157 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
158 "mfspr %L0,268\n\t" /* mftb */
159 "mfspr %0,269\n\t" /* mftbu */
162 : "=r" (retval
), "=r" (junk
));
167 #elif defined(__i386__)
169 static inline int64_t cpu_get_real_ticks(void)
172 asm volatile ("rdtsc" : "=A" (val
));
176 #elif defined(__x86_64__)
178 static inline int64_t cpu_get_real_ticks(void)
182 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
189 #elif defined(__hppa__)
191 static inline int64_t cpu_get_real_ticks(void)
194 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
198 #elif defined(__ia64)
200 static inline int64_t cpu_get_real_ticks(void)
203 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
207 #elif defined(__s390__)
209 static inline int64_t cpu_get_real_ticks(void)
212 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
216 #elif defined(__sparc__)
218 static inline int64_t cpu_get_real_ticks (void)
222 asm volatile("rd %%tick,%0" : "=r"(rval
));
225 /* We need an %o or %g register for this. For recent enough gcc
226 there is an "h" constraint for that. Don't bother with that. */
234 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
235 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
240 #elif defined(__mips__) && \
241 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
243 * binutils wants to use rdhwr only on mips32r2
244 * but as linux kernel emulate it, it's fine
248 #define MIPS_RDHWR(rd, value) { \
249 __asm__ __volatile__ (".set push\n\t" \
250 ".set mips32r2\n\t" \
251 "rdhwr %0, "rd"\n\t" \
256 static inline int64_t cpu_get_real_ticks(void)
258 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
260 static uint32_t cyc_per_count
= 0;
262 if (!cyc_per_count
) {
263 MIPS_RDHWR("$3", cyc_per_count
);
266 MIPS_RDHWR("$2", count
);
267 return (int64_t)(count
* cyc_per_count
);
270 #elif defined(__alpha__)
272 static inline int64_t cpu_get_real_ticks(void)
277 asm volatile("rpcc %0" : "=r"(cc
));
284 /* The host CPU doesn't have an easily accessible cycle counter.
285 Just return a monotonically increasing value. This will be
286 totally wrong, but hopefully better than nothing. */
287 static inline int64_t cpu_get_real_ticks (void)
289 static int64_t ticks
= 0;
294 #ifdef CONFIG_PROFILER
295 static inline int64_t profile_getclock(void)
297 return cpu_get_real_ticks();
300 extern int64_t qemu_time
, qemu_time_start
;
301 extern int64_t tlb_flush_time
;
302 extern int64_t dev_time
;