4 #include "qemu/typedefs.h"
5 #include "qemu-common.h"
6 #include "qemu/notify.h"
10 #define SCALE_MS 1000000
17 * The following clock types are available:
19 * @QEMU_CLOCK_REALTIME: Real time clock
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
26 * @QEMU_CLOCK_VIRTUAL: virtual clock
28 * The virtual clock is only run during the emulation. It is stopped
29 * when the virtual machine is stopped. Virtual timers use a high
30 * precision clock, usually cpu cycles (use ticks_per_sec).
32 * @QEMU_CLOCK_HOST: host clock
34 * The host clock should be use for device models that emulate accurate
35 * real time sources. It will continue to run when the virtual machine
36 * is suspended, and it will reflect system time changes the host may
37 * undergo (e.g. due to NTP). The host clock has the same precision as
42 QEMU_CLOCK_REALTIME
= 0,
43 QEMU_CLOCK_VIRTUAL
= 1,
48 typedef struct QEMUTimerList QEMUTimerList
;
50 struct QEMUTimerListGroup
{
51 QEMUTimerList
*tl
[QEMU_CLOCK_MAX
];
54 typedef void QEMUTimerCB(void *opaque
);
55 typedef void QEMUTimerListNotifyCB(void *opaque
);
58 int64_t expire_time
; /* in nanoseconds */
59 QEMUTimerList
*timer_list
;
66 extern QEMUTimerListGroup main_loop_tlg
;
74 * @type: the clock type
76 * Get the nanosecond value of a clock with
79 * Returns: the clock value in nanoseconds
81 int64_t qemu_clock_get_ns(QEMUClockType type
);
85 * @type: the clock type
87 * Get the millisecond value of a clock with
90 * Returns: the clock value in milliseconds
92 static inline int64_t qemu_clock_get_ms(QEMUClockType type
)
94 return qemu_clock_get_ns(type
) / SCALE_MS
;
99 * @type: the clock type
101 * Get the microsecond value of a clock with
104 * Returns: the clock value in microseconds
106 static inline int64_t qemu_clock_get_us(QEMUClockType type
)
108 return qemu_clock_get_ns(type
) / SCALE_US
;
112 * qemu_clock_has_timers:
113 * @type: the clock type
115 * Determines whether a clock's default timer list
116 * has timers attached
118 * Returns: true if the clock's default timer list
119 * has timers attached
121 bool qemu_clock_has_timers(QEMUClockType type
);
124 * qemu_clock_expired:
125 * @type: the clock type
127 * Determines whether a clock's default timer list
128 * has an expired clock.
130 * Returns: true if the clock's default timer list has
133 bool qemu_clock_expired(QEMUClockType type
);
136 * qemu_clock_use_for_deadline:
137 * @type: the clock type
139 * Determine whether a clock should be used for deadline
140 * calculations. Some clocks, for instance vm_clock with
141 * use_icount set, do not count in nanoseconds. Such clocks
142 * are not used for deadline calculations, and are presumed
143 * to interrupt any poll using qemu_notify/aio_notify
146 * Returns: true if the clock runs in nanoseconds and
147 * should be used for a deadline.
149 bool qemu_clock_use_for_deadline(QEMUClockType type
);
152 * qemu_clock_deadline_ns_all:
153 * @type: the clock type
155 * Calculate the deadline across all timer lists associated
156 * with a clock (as opposed to just the default one)
157 * in nanoseconds, or -1 if no timer is set to expire.
159 * Returns: time until expiry in nanoseconds or -1
161 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
);
164 * qemu_clock_get_main_loop_timerlist:
165 * @type: the clock type
167 * Return the default timer list assocatiated with a clock.
169 * Returns: the default timer list
171 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
);
175 * @type: the clock type
177 * Call the notifier callback connected with the default timer
178 * list linked to the clock, or qemu_notify() if none.
180 void qemu_clock_notify(QEMUClockType type
);
184 * @type: the clock type
185 * @enabled: true to enable, false to disable
187 * Enable or disable a clock
189 void qemu_clock_enable(QEMUClockType type
, bool enabled
);
193 * @type: the clock type
195 * Warp a clock to a new value
197 void qemu_clock_warp(QEMUClockType type
);
200 * qemu_clock_register_reset_notifier:
201 * @type: the clock type
202 * @notifier: the notifier function
204 * Register a notifier function to call when the clock
205 * concerned is reset.
207 void qemu_clock_register_reset_notifier(QEMUClockType type
,
211 * qemu_clock_unregister_reset_notifier:
212 * @type: the clock type
213 * @notifier: the notifier function
215 * Unregister a notifier function to call when the clock
216 * concerned is reset.
218 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
222 * qemu_clock_run_timers:
223 * @type: clock on which to operate
225 * Run all the timers associated with the default timer list
228 * Returns: true if any timer ran.
230 bool qemu_clock_run_timers(QEMUClockType type
);
233 * qemu_clock_run_all_timers:
235 * Run all the timers associated with the default timer list
238 * Returns: true if any timer ran.
240 bool qemu_clock_run_all_timers(void);
248 * @type: the clock type to associate with the timerlist
249 * @cb: the callback to call on notification
250 * @opaque: the opaque pointer to pass to the callback
252 * Create a new timerlist associated with the clock of
255 * Returns: a pointer to the QEMUTimerList created
257 QEMUTimerList
*timerlist_new(QEMUClockType type
,
258 QEMUTimerListNotifyCB
*cb
, void *opaque
);
262 * @timer_list: the timer list to free
264 * Frees a timer_list. It must have no active timers.
266 void timerlist_free(QEMUTimerList
*timer_list
);
269 * timerlist_has_timers:
270 * @timer_list: the timer list to operate on
272 * Determine whether a timer list has active timers
274 * Returns: true if the timer list has timers.
276 bool timerlist_has_timers(QEMUTimerList
*timer_list
);
280 * @timer_list: the timer list to operate on
282 * Determine whether a timer list has any timers which
285 * Returns: true if the timer list has timers which
288 bool timerlist_expired(QEMUTimerList
*timer_list
);
291 * timerlist_deadline_ns:
292 * @timer_list: the timer list to operate on
294 * Determine the deadline for a timer_list, i.e.
295 * the number of nanoseconds until the first timer
296 * expires. Return -1 if there are no timers.
298 * Returns: the number of nanoseconds until the earliest
299 * timer expires -1 if none
301 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
);
304 * timerlist_get_clock:
305 * @timer_list: the timer list to operate on
307 * Determine the clock type associated with a timer list.
309 * Returns: the clock type associated with the
312 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
);
315 * timerlist_run_timers:
316 * @timer_list: the timer list to use
318 * Call all expired timers associated with the timer list.
320 * Returns: true if any timer expired
322 bool timerlist_run_timers(QEMUTimerList
*timer_list
);
326 * @timer_list: the timer list to use
328 * call the notifier callback associated with the timer list.
330 void timerlist_notify(QEMUTimerList
*timer_list
);
337 * timerlistgroup_init:
338 * @tlg: the timer list group
339 * @cb: the callback to call when a notify is required
340 * @opaque: the opaque pointer to be passed to the callback.
342 * Initialise a timer list group. This must already be
343 * allocated in memory and zeroed. The notifier callback is
344 * called whenever a clock in the timer list group is
345 * reenabled or whenever a timer associated with any timer
346 * list is modified. If @cb is specified as null, qemu_notify()
349 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
350 QEMUTimerListNotifyCB
*cb
, void *opaque
);
353 * timerlistgroup_deinit:
354 * @tlg: the timer list group
356 * Deinitialise a timer list group. This must already be
357 * initialised. Note the memory is not freed.
359 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
);
362 * timerlistgroup_run_timers:
363 * @tlg: the timer list group
365 * Run the timers associated with a timer list group.
366 * This will run timers on multiple clocks.
368 * Returns: true if any timer callback ran
370 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
);
373 * timerlistgroup_deadline_ns:
374 * @tlg: the timer list group
376 * Determine the deadline of the soonest timer to
377 * expire associated with any timer list linked to
378 * the timer list group. Only clocks suitable for
379 * deadline calculation are included.
381 * Returns: the deadline in nanoseconds or -1 if no
382 * timers are to expire.
384 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
);
392 * @ts: the timer to be initialised
393 * @timer_list: the timer list to attach the timer to
394 * @scale: the scale value for the tiemr
395 * @cb: the callback to be called when the timer expires
396 * @opaque: the opaque pointer to be passed to the callback
398 * Initialise a new timer and associate it with @timer_list.
399 * The caller is responsible for allocating the memory.
401 * You need not call an explicit deinit call. Simply make
402 * sure it is not on a list with timer_del.
404 void timer_init(QEMUTimer
*ts
,
405 QEMUTimerList
*timer_list
, int scale
,
406 QEMUTimerCB
*cb
, void *opaque
);
410 * @timer_list: the timer list to attach the timer to
411 * @scale: the scale value for the tiemr
412 * @cb: the callback to be called when the timer expires
413 * @opaque: the opaque pointer to be passed to the callback
415 * Creeate a new timer and associate it with @timer_list.
416 * The memory is allocated by the function.
418 * This is not the preferred interface unless you know you
419 * are going to call timer_free. Use timer_init instead.
421 * Returns: a pointer to the timer
423 static inline QEMUTimer
*timer_new_tl(QEMUTimerList
*timer_list
,
428 QEMUTimer
*ts
= g_malloc0(sizeof(QEMUTimer
));
429 timer_init(ts
, timer_list
, scale
, cb
, opaque
);
435 * @type: the clock type to use
436 * @scale: the scale value for the tiemr
437 * @cb: the callback to be called when the timer expires
438 * @opaque: the opaque pointer to be passed to the callback
440 * Creeate a new timer and associate it with the default
441 * timer list for the clock type @type.
443 * Returns: a pointer to the timer
445 static inline QEMUTimer
*timer_new(QEMUClockType type
, int scale
,
446 QEMUTimerCB
*cb
, void *opaque
)
448 return timer_new_tl(main_loop_tlg
.tl
[type
], scale
, cb
, opaque
);
453 * @clock: the clock to associate with the timer
454 * @callback: the callback to call when the timer expires
455 * @opaque: the opaque pointer to pass to the callback
457 * Create a new timer with nanosecond scale on the default timer list
458 * associated with the clock.
460 * Returns: a pointer to the newly created timer
462 static inline QEMUTimer
*timer_new_ns(QEMUClockType type
, QEMUTimerCB
*cb
,
465 return timer_new(type
, SCALE_NS
, cb
, opaque
);
470 * @clock: the clock to associate with the timer
471 * @callback: the callback to call when the timer expires
472 * @opaque: the opaque pointer to pass to the callback
474 * Create a new timer with microsecond scale on the default timer list
475 * associated with the clock.
477 * Returns: a pointer to the newly created timer
479 static inline QEMUTimer
*timer_new_us(QEMUClockType type
, QEMUTimerCB
*cb
,
482 return timer_new(type
, SCALE_US
, cb
, opaque
);
487 * @clock: the clock to associate with the timer
488 * @callback: the callback to call when the timer expires
489 * @opaque: the opaque pointer to pass to the callback
491 * Create a new timer with millisecond scale on the default timer list
492 * associated with the clock.
494 * Returns: a pointer to the newly created timer
496 static inline QEMUTimer
*timer_new_ms(QEMUClockType type
, QEMUTimerCB
*cb
,
499 return timer_new(type
, SCALE_MS
, cb
, opaque
);
506 * Free a timer (it must not be on the active list)
508 void timer_free(QEMUTimer
*ts
);
514 * Delete a timer from the active list.
516 void timer_del(QEMUTimer
*ts
);
521 * @expire_time: the expiry time in nanoseconds
523 * Modify a timer to expire at @expire_time
525 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
);
530 * @expire_time: the expire time in the units associated with the timer
532 * Modify a timer to expiry at @expire_time, taking into
533 * account the scale associated with the timer.
535 void timer_mod(QEMUTimer
*ts
, int64_t expire_timer
);
541 * Determines whether a timer is pending (i.e. is on the
542 * active list of timers, whether or not it has not yet expired).
544 * Returns: true if the timer is pending
546 bool timer_pending(QEMUTimer
*ts
);
552 * Determines whether a timer has expired.
554 * Returns: true if the timer has expired
556 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
559 * timer_expire_time_ns:
562 * Determine the expiry time of a timer
564 * Returns: the expiry time in nanoseconds
566 uint64_t timer_expire_time_ns(QEMUTimer
*ts
);
573 * Read a timer @ts from a file @f
575 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
);
582 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
);
585 * General utility functions
589 * qemu_timeout_ns_to_ms:
590 * @ns: nanosecond timeout value
592 * Convert a nanosecond timeout value (or -1) to
593 * a millisecond value (or -1), always rounding up.
595 * Returns: millisecond timeout value
597 int qemu_timeout_ns_to_ms(int64_t ns
);
601 * @fds: Array of file descriptors
602 * @nfds: number of file descriptors
603 * @timeout: timeout in nanoseconds
605 * Perform a poll like g_poll but with a timeout in nanoseconds.
606 * See g_poll documentation for further details.
608 * Returns: number of fds ready
610 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
);
613 * qemu_soonest_timeout:
614 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
615 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
617 * Calculates the soonest of two timeout values. -1 means infinite, which
618 * is later than any other value.
620 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
622 static inline int64_t qemu_soonest_timeout(int64_t timeout1
, int64_t timeout2
)
624 /* we can abuse the fact that -1 (which means infinite) is a maximal
625 * value when cast to unsigned. As this is disgusting, it's kept in
626 * one inline function.
628 return ((uint64_t) timeout1
< (uint64_t) timeout2
) ? timeout1
: timeout2
;
634 * Initialise the clock & timer infrastructure
636 void init_clocks(void);
638 int64_t cpu_get_ticks(void);
639 void cpu_enable_ticks(void);
640 void cpu_disable_ticks(void);
642 static inline int64_t get_ticks_per_sec(void)
648 * Low level clock functions
651 /* real time host monotonic timer */
652 static inline int64_t get_clock_realtime(void)
656 gettimeofday(&tv
, NULL
);
657 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
660 /* Warning: don't insert tracepoints into these functions, they are
661 also used by simpletrace backend and tracepoints would cause
662 an infinite recursion! */
664 extern int64_t clock_freq
;
666 static inline int64_t get_clock(void)
669 QueryPerformanceCounter(&ti
);
670 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
675 extern int use_rt_clock
;
677 static inline int64_t get_clock(void)
679 #ifdef CLOCK_MONOTONIC
682 clock_gettime(CLOCK_MONOTONIC
, &ts
);
683 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
687 /* XXX: using gettimeofday leads to problems if the date
688 changes, so it should be avoided. */
689 return get_clock_realtime();
695 int64_t cpu_get_icount(void);
696 int64_t cpu_get_clock(void);
698 /*******************************************/
699 /* host CPU ticks (if available) */
701 #if defined(_ARCH_PPC)
703 static inline int64_t cpu_get_real_ticks(void)
707 /* This reads timebase in one 64bit go and includes Cell workaround from:
708 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
710 __asm__
__volatile__ ("mftb %0\n\t"
715 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
717 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
718 "mfspr %L0,268\n\t" /* mftb */
719 "mfspr %0,269\n\t" /* mftbu */
722 : "=r" (retval
), "=r" (junk
));
727 #elif defined(__i386__)
729 static inline int64_t cpu_get_real_ticks(void)
732 asm volatile ("rdtsc" : "=A" (val
));
736 #elif defined(__x86_64__)
738 static inline int64_t cpu_get_real_ticks(void)
742 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
749 #elif defined(__hppa__)
751 static inline int64_t cpu_get_real_ticks(void)
754 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
758 #elif defined(__ia64)
760 static inline int64_t cpu_get_real_ticks(void)
763 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
767 #elif defined(__s390__)
769 static inline int64_t cpu_get_real_ticks(void)
772 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
776 #elif defined(__sparc__)
778 static inline int64_t cpu_get_real_ticks (void)
782 asm volatile("rd %%tick,%0" : "=r"(rval
));
785 /* We need an %o or %g register for this. For recent enough gcc
786 there is an "h" constraint for that. Don't bother with that. */
794 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
795 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
800 #elif defined(__mips__) && \
801 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
803 * binutils wants to use rdhwr only on mips32r2
804 * but as linux kernel emulate it, it's fine
808 #define MIPS_RDHWR(rd, value) { \
809 __asm__ __volatile__ (".set push\n\t" \
810 ".set mips32r2\n\t" \
811 "rdhwr %0, "rd"\n\t" \
816 static inline int64_t cpu_get_real_ticks(void)
818 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
820 static uint32_t cyc_per_count
= 0;
822 if (!cyc_per_count
) {
823 MIPS_RDHWR("$3", cyc_per_count
);
826 MIPS_RDHWR("$2", count
);
827 return (int64_t)(count
* cyc_per_count
);
830 #elif defined(__alpha__)
832 static inline int64_t cpu_get_real_ticks(void)
837 asm volatile("rpcc %0" : "=r"(cc
));
844 /* The host CPU doesn't have an easily accessible cycle counter.
845 Just return a monotonically increasing value. This will be
846 totally wrong, but hopefully better than nothing. */
847 static inline int64_t cpu_get_real_ticks (void)
849 static int64_t ticks
= 0;
854 #ifdef CONFIG_PROFILER
855 static inline int64_t profile_getclock(void)
857 return cpu_get_real_ticks();
860 extern int64_t qemu_time
, qemu_time_start
;
861 extern int64_t tlb_flush_time
;
862 extern int64_t dev_time
;