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
40 * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
42 * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
43 * In icount mode, this clock counts nanoseconds while the virtual
44 * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
45 * while the CPUs are sleeping and thus not executing instructions.
49 QEMU_CLOCK_REALTIME
= 0,
50 QEMU_CLOCK_VIRTUAL
= 1,
52 QEMU_CLOCK_VIRTUAL_RT
= 3,
56 typedef struct QEMUTimerList QEMUTimerList
;
58 struct QEMUTimerListGroup
{
59 QEMUTimerList
*tl
[QEMU_CLOCK_MAX
];
62 typedef void QEMUTimerCB(void *opaque
);
63 typedef void QEMUTimerListNotifyCB(void *opaque
);
66 int64_t expire_time
; /* in nanoseconds */
67 QEMUTimerList
*timer_list
;
74 extern QEMUTimerListGroup main_loop_tlg
;
82 * @type: the clock type
84 * Get the nanosecond value of a clock with
87 * Returns: the clock value in nanoseconds
89 int64_t qemu_clock_get_ns(QEMUClockType type
);
93 * @type: the clock type
95 * Get the millisecond value of a clock with
98 * Returns: the clock value in milliseconds
100 static inline int64_t qemu_clock_get_ms(QEMUClockType type
)
102 return qemu_clock_get_ns(type
) / SCALE_MS
;
107 * @type: the clock type
109 * Get the microsecond value of a clock with
112 * Returns: the clock value in microseconds
114 static inline int64_t qemu_clock_get_us(QEMUClockType type
)
116 return qemu_clock_get_ns(type
) / SCALE_US
;
120 * qemu_clock_has_timers:
121 * @type: the clock type
123 * Determines whether a clock's default timer list
124 * has timers attached
126 * Note that this function should not be used when other threads also access
127 * the timer list. The return value may be outdated by the time it is acted
130 * Returns: true if the clock's default timer list
131 * has timers attached
133 bool qemu_clock_has_timers(QEMUClockType type
);
136 * qemu_clock_expired:
137 * @type: the clock type
139 * Determines whether a clock's default timer list
140 * has an expired clock.
142 * Returns: true if the clock's default timer list has
145 bool qemu_clock_expired(QEMUClockType type
);
148 * qemu_clock_use_for_deadline:
149 * @type: the clock type
151 * Determine whether a clock should be used for deadline
152 * calculations. Some clocks, for instance vm_clock with
153 * use_icount set, do not count in nanoseconds. Such clocks
154 * are not used for deadline calculations, and are presumed
155 * to interrupt any poll using qemu_notify/aio_notify
158 * Returns: true if the clock runs in nanoseconds and
159 * should be used for a deadline.
161 bool qemu_clock_use_for_deadline(QEMUClockType type
);
164 * qemu_clock_deadline_ns_all:
165 * @type: the clock type
167 * Calculate the deadline across all timer lists associated
168 * with a clock (as opposed to just the default one)
169 * in nanoseconds, or -1 if no timer is set to expire.
171 * Returns: time until expiry in nanoseconds or -1
173 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
);
176 * qemu_clock_get_main_loop_timerlist:
177 * @type: the clock type
179 * Return the default timer list assocatiated with a clock.
181 * Returns: the default timer list
183 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
);
187 * @type: the clock type
189 * Call the notifier callback connected with the default timer
190 * list linked to the clock, or qemu_notify() if none.
192 void qemu_clock_notify(QEMUClockType type
);
196 * @type: the clock type
197 * @enabled: true to enable, false to disable
199 * Enable or disable a clock
200 * Disabling the clock will wait for related timerlists to stop
201 * executing qemu_run_timers. Thus, this functions should not
202 * be used from the callback of a timer that is based on @clock.
203 * Doing so would cause a deadlock.
205 * Caller should hold BQL.
207 void qemu_clock_enable(QEMUClockType type
, bool enabled
);
211 * @type: the clock type
213 * Warp a clock to a new value
215 void qemu_clock_warp(QEMUClockType type
);
218 * qemu_clock_register_reset_notifier:
219 * @type: the clock type
220 * @notifier: the notifier function
222 * Register a notifier function to call when the clock
223 * concerned is reset.
225 void qemu_clock_register_reset_notifier(QEMUClockType type
,
229 * qemu_clock_unregister_reset_notifier:
230 * @type: the clock type
231 * @notifier: the notifier function
233 * Unregister a notifier function to call when the clock
234 * concerned is reset.
236 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
240 * qemu_clock_run_timers:
241 * @type: clock on which to operate
243 * Run all the timers associated with the default timer list
246 * Returns: true if any timer ran.
248 bool qemu_clock_run_timers(QEMUClockType type
);
251 * qemu_clock_run_all_timers:
253 * Run all the timers associated with the default timer list
256 * Returns: true if any timer ran.
258 bool qemu_clock_run_all_timers(void);
266 * @type: the clock type to associate with the timerlist
267 * @cb: the callback to call on notification
268 * @opaque: the opaque pointer to pass to the callback
270 * Create a new timerlist associated with the clock of
273 * Returns: a pointer to the QEMUTimerList created
275 QEMUTimerList
*timerlist_new(QEMUClockType type
,
276 QEMUTimerListNotifyCB
*cb
, void *opaque
);
280 * @timer_list: the timer list to free
282 * Frees a timer_list. It must have no active timers.
284 void timerlist_free(QEMUTimerList
*timer_list
);
287 * timerlist_has_timers:
288 * @timer_list: the timer list to operate on
290 * Determine whether a timer list has active timers
292 * Note that this function should not be used when other threads also access
293 * the timer list. The return value may be outdated by the time it is acted
296 * Returns: true if the timer list has timers.
298 bool timerlist_has_timers(QEMUTimerList
*timer_list
);
302 * @timer_list: the timer list to operate on
304 * Determine whether a timer list has any timers which
307 * Returns: true if the timer list has timers which
310 bool timerlist_expired(QEMUTimerList
*timer_list
);
313 * timerlist_deadline_ns:
314 * @timer_list: the timer list to operate on
316 * Determine the deadline for a timer_list, i.e.
317 * the number of nanoseconds until the first timer
318 * expires. Return -1 if there are no timers.
320 * Returns: the number of nanoseconds until the earliest
321 * timer expires -1 if none
323 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
);
326 * timerlist_get_clock:
327 * @timer_list: the timer list to operate on
329 * Determine the clock type associated with a timer list.
331 * Returns: the clock type associated with the
334 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
);
337 * timerlist_run_timers:
338 * @timer_list: the timer list to use
340 * Call all expired timers associated with the timer list.
342 * Returns: true if any timer expired
344 bool timerlist_run_timers(QEMUTimerList
*timer_list
);
348 * @timer_list: the timer list to use
350 * call the notifier callback associated with the timer list.
352 void timerlist_notify(QEMUTimerList
*timer_list
);
359 * timerlistgroup_init:
360 * @tlg: the timer list group
361 * @cb: the callback to call when a notify is required
362 * @opaque: the opaque pointer to be passed to the callback.
364 * Initialise a timer list group. This must already be
365 * allocated in memory and zeroed. The notifier callback is
366 * called whenever a clock in the timer list group is
367 * reenabled or whenever a timer associated with any timer
368 * list is modified. If @cb is specified as null, qemu_notify()
371 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
372 QEMUTimerListNotifyCB
*cb
, void *opaque
);
375 * timerlistgroup_deinit:
376 * @tlg: the timer list group
378 * Deinitialise a timer list group. This must already be
379 * initialised. Note the memory is not freed.
381 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
);
384 * timerlistgroup_run_timers:
385 * @tlg: the timer list group
387 * Run the timers associated with a timer list group.
388 * This will run timers on multiple clocks.
390 * Returns: true if any timer callback ran
392 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
);
395 * timerlistgroup_deadline_ns:
396 * @tlg: the timer list group
398 * Determine the deadline of the soonest timer to
399 * expire associated with any timer list linked to
400 * the timer list group. Only clocks suitable for
401 * deadline calculation are included.
403 * Returns: the deadline in nanoseconds or -1 if no
404 * timers are to expire.
406 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
);
414 * @ts: the timer to be initialised
415 * @timer_list: the timer list to attach the timer to
416 * @scale: the scale value for the timer
417 * @cb: the callback to be called when the timer expires
418 * @opaque: the opaque pointer to be passed to the callback
420 * Initialise a new timer and associate it with @timer_list.
421 * The caller is responsible for allocating the memory.
423 * You need not call an explicit deinit call. Simply make
424 * sure it is not on a list with timer_del.
426 void timer_init_tl(QEMUTimer
*ts
,
427 QEMUTimerList
*timer_list
, int scale
,
428 QEMUTimerCB
*cb
, void *opaque
);
432 * @type: the clock to associate with the timer
433 * @scale: the scale value for the timer
434 * @cb: the callback to call when the timer expires
435 * @opaque: the opaque pointer to pass to the callback
437 * Initialize a timer with the given scale on the default timer list
438 * associated with the clock.
440 * You need not call an explicit deinit call. Simply make
441 * sure it is not on a list with timer_del.
443 static inline void timer_init(QEMUTimer
*ts
, QEMUClockType type
, int scale
,
444 QEMUTimerCB
*cb
, void *opaque
)
446 timer_init_tl(ts
, main_loop_tlg
.tl
[type
], scale
, cb
, opaque
);
451 * @type: the clock to associate with the timer
452 * @cb: the callback to call when the timer expires
453 * @opaque: the opaque pointer to pass to the callback
455 * Initialize a timer with nanosecond scale on the default timer list
456 * associated with the clock.
458 * You need not call an explicit deinit call. Simply make
459 * sure it is not on a list with timer_del.
461 static inline void timer_init_ns(QEMUTimer
*ts
, QEMUClockType type
,
462 QEMUTimerCB
*cb
, void *opaque
)
464 timer_init(ts
, type
, SCALE_NS
, cb
, opaque
);
469 * @type: the clock to associate with the timer
470 * @cb: the callback to call when the timer expires
471 * @opaque: the opaque pointer to pass to the callback
473 * Initialize a timer with microsecond scale on the default timer list
474 * associated with the clock.
476 * You need not call an explicit deinit call. Simply make
477 * sure it is not on a list with timer_del.
479 static inline void timer_init_us(QEMUTimer
*ts
, QEMUClockType type
,
480 QEMUTimerCB
*cb
, void *opaque
)
482 timer_init(ts
, type
, SCALE_US
, cb
, opaque
);
487 * @type: the clock to associate with the timer
488 * @cb: the callback to call when the timer expires
489 * @opaque: the opaque pointer to pass to the callback
491 * Initialize a timer with millisecond scale on the default timer list
492 * associated with the clock.
494 * You need not call an explicit deinit call. Simply make
495 * sure it is not on a list with timer_del.
497 static inline void timer_init_ms(QEMUTimer
*ts
, QEMUClockType type
,
498 QEMUTimerCB
*cb
, void *opaque
)
500 timer_init(ts
, type
, SCALE_MS
, cb
, opaque
);
505 * @timer_list: the timer list to attach the timer to
506 * @scale: the scale value for the timer
507 * @cb: the callback to be called when the timer expires
508 * @opaque: the opaque pointer to be passed to the callback
510 * Creeate a new timer and associate it with @timer_list.
511 * The memory is allocated by the function.
513 * This is not the preferred interface unless you know you
514 * are going to call timer_free. Use timer_init instead.
516 * Returns: a pointer to the timer
518 static inline QEMUTimer
*timer_new_tl(QEMUTimerList
*timer_list
,
523 QEMUTimer
*ts
= g_malloc0(sizeof(QEMUTimer
));
524 timer_init_tl(ts
, timer_list
, scale
, cb
, opaque
);
530 * @type: the clock type to use
531 * @scale: the scale value for the timer
532 * @cb: the callback to be called when the timer expires
533 * @opaque: the opaque pointer to be passed to the callback
535 * Creeate a new timer and associate it with the default
536 * timer list for the clock type @type.
538 * Returns: a pointer to the timer
540 static inline QEMUTimer
*timer_new(QEMUClockType type
, int scale
,
541 QEMUTimerCB
*cb
, void *opaque
)
543 return timer_new_tl(main_loop_tlg
.tl
[type
], scale
, cb
, opaque
);
548 * @clock: the clock to associate with the timer
549 * @callback: the callback to call when the timer expires
550 * @opaque: the opaque pointer to pass to the callback
552 * Create a new timer with nanosecond scale on the default timer list
553 * associated with the clock.
555 * Returns: a pointer to the newly created timer
557 static inline QEMUTimer
*timer_new_ns(QEMUClockType type
, QEMUTimerCB
*cb
,
560 return timer_new(type
, SCALE_NS
, cb
, opaque
);
565 * @clock: the clock to associate with the timer
566 * @callback: the callback to call when the timer expires
567 * @opaque: the opaque pointer to pass to the callback
569 * Create a new timer with microsecond scale on the default timer list
570 * associated with the clock.
572 * Returns: a pointer to the newly created timer
574 static inline QEMUTimer
*timer_new_us(QEMUClockType type
, QEMUTimerCB
*cb
,
577 return timer_new(type
, SCALE_US
, cb
, opaque
);
582 * @clock: the clock to associate with the timer
583 * @callback: the callback to call when the timer expires
584 * @opaque: the opaque pointer to pass to the callback
586 * Create a new timer with millisecond scale on the default timer list
587 * associated with the clock.
589 * Returns: a pointer to the newly created timer
591 static inline QEMUTimer
*timer_new_ms(QEMUClockType type
, QEMUTimerCB
*cb
,
594 return timer_new(type
, SCALE_MS
, cb
, opaque
);
599 * @ts: the timer to be de-initialised
601 * Deassociate the timer from any timerlist. You should
602 * call timer_del before. After this call, any further
603 * timer_del call cannot cause dangling pointer accesses
604 * even if the previously used timerlist is freed.
606 void timer_deinit(QEMUTimer
*ts
);
612 * Free a timer (it must not be on the active list)
614 void timer_free(QEMUTimer
*ts
);
620 * Delete a timer from the active list.
622 * This function is thread-safe but the timer and its timer list must not be
623 * freed while this function is running.
625 void timer_del(QEMUTimer
*ts
);
630 * @expire_time: the expiry time in nanoseconds
632 * Modify a timer to expire at @expire_time
634 * This function is thread-safe but the timer and its timer list must not be
635 * freed while this function is running.
637 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
);
640 * timer_mod_anticipate_ns:
642 * @expire_time: the expiry time in nanoseconds
644 * Modify a timer to expire at @expire_time or the current time,
645 * whichever comes earlier.
647 * This function is thread-safe but the timer and its timer list must not be
648 * freed while this function is running.
650 void timer_mod_anticipate_ns(QEMUTimer
*ts
, int64_t expire_time
);
655 * @expire_time: the expire time in the units associated with the timer
657 * Modify a timer to expiry at @expire_time, taking into
658 * account the scale associated with the timer.
660 * This function is thread-safe but the timer and its timer list must not be
661 * freed while this function is running.
663 void timer_mod(QEMUTimer
*ts
, int64_t expire_timer
);
666 * timer_mod_anticipate:
668 * @expire_time: the expiry time in nanoseconds
670 * Modify a timer to expire at @expire_time or the current time, whichever
671 * comes earlier, taking into account the scale associated with the timer.
673 * This function is thread-safe but the timer and its timer list must not be
674 * freed while this function is running.
676 void timer_mod_anticipate(QEMUTimer
*ts
, int64_t expire_time
);
682 * Determines whether a timer is pending (i.e. is on the
683 * active list of timers, whether or not it has not yet expired).
685 * Returns: true if the timer is pending
687 bool timer_pending(QEMUTimer
*ts
);
693 * Determines whether a timer has expired.
695 * Returns: true if the timer has expired
697 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
700 * timer_expire_time_ns:
703 * Determine the expiry time of a timer
705 * Returns: the expiry time in nanoseconds
707 uint64_t timer_expire_time_ns(QEMUTimer
*ts
);
714 * Read a timer @ts from a file @f
716 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
);
723 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
);
726 * General utility functions
730 * qemu_timeout_ns_to_ms:
731 * @ns: nanosecond timeout value
733 * Convert a nanosecond timeout value (or -1) to
734 * a millisecond value (or -1), always rounding up.
736 * Returns: millisecond timeout value
738 int qemu_timeout_ns_to_ms(int64_t ns
);
742 * @fds: Array of file descriptors
743 * @nfds: number of file descriptors
744 * @timeout: timeout in nanoseconds
746 * Perform a poll like g_poll but with a timeout in nanoseconds.
747 * See g_poll documentation for further details.
749 * Returns: number of fds ready
751 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
);
754 * qemu_soonest_timeout:
755 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
756 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
758 * Calculates the soonest of two timeout values. -1 means infinite, which
759 * is later than any other value.
761 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
763 static inline int64_t qemu_soonest_timeout(int64_t timeout1
, int64_t timeout2
)
765 /* we can abuse the fact that -1 (which means infinite) is a maximal
766 * value when cast to unsigned. As this is disgusting, it's kept in
767 * one inline function.
769 return ((uint64_t) timeout1
< (uint64_t) timeout2
) ? timeout1
: timeout2
;
775 * Initialise the clock & timer infrastructure
777 void init_clocks(void);
779 int64_t cpu_get_ticks(void);
780 /* Caller must hold BQL */
781 void cpu_enable_ticks(void);
782 /* Caller must hold BQL */
783 void cpu_disable_ticks(void);
785 static inline int64_t get_ticks_per_sec(void)
791 * Low level clock functions
794 /* real time host monotonic timer */
795 static inline int64_t get_clock_realtime(void)
799 gettimeofday(&tv
, NULL
);
800 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
803 /* Warning: don't insert tracepoints into these functions, they are
804 also used by simpletrace backend and tracepoints would cause
805 an infinite recursion! */
807 extern int64_t clock_freq
;
809 static inline int64_t get_clock(void)
812 QueryPerformanceCounter(&ti
);
813 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
818 extern int use_rt_clock
;
820 static inline int64_t get_clock(void)
822 #ifdef CLOCK_MONOTONIC
825 clock_gettime(CLOCK_MONOTONIC
, &ts
);
826 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
830 /* XXX: using gettimeofday leads to problems if the date
831 changes, so it should be avoided. */
832 return get_clock_realtime();
838 int64_t cpu_get_icount_raw(void);
839 int64_t cpu_get_icount(void);
840 int64_t cpu_get_clock(void);
841 int64_t cpu_icount_to_ns(int64_t icount
);
843 /*******************************************/
844 /* host CPU ticks (if available) */
846 #if defined(_ARCH_PPC)
848 static inline int64_t cpu_get_real_ticks(void)
852 /* This reads timebase in one 64bit go and includes Cell workaround from:
853 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
855 __asm__
__volatile__ ("mftb %0\n\t"
860 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
862 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
863 "mfspr %L0,268\n\t" /* mftb */
864 "mfspr %0,269\n\t" /* mftbu */
867 : "=r" (retval
), "=r" (junk
));
872 #elif defined(__i386__)
874 static inline int64_t cpu_get_real_ticks(void)
877 asm volatile ("rdtsc" : "=A" (val
));
881 #elif defined(__x86_64__)
883 static inline int64_t cpu_get_real_ticks(void)
887 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
894 #elif defined(__hppa__)
896 static inline int64_t cpu_get_real_ticks(void)
899 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
903 #elif defined(__ia64)
905 static inline int64_t cpu_get_real_ticks(void)
908 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
912 #elif defined(__s390__)
914 static inline int64_t cpu_get_real_ticks(void)
917 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
921 #elif defined(__sparc__)
923 static inline int64_t cpu_get_real_ticks (void)
927 asm volatile("rd %%tick,%0" : "=r"(rval
));
930 /* We need an %o or %g register for this. For recent enough gcc
931 there is an "h" constraint for that. Don't bother with that. */
939 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
940 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
945 #elif defined(__mips__) && \
946 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
948 * binutils wants to use rdhwr only on mips32r2
949 * but as linux kernel emulate it, it's fine
953 #define MIPS_RDHWR(rd, value) { \
954 __asm__ __volatile__ (".set push\n\t" \
955 ".set mips32r2\n\t" \
956 "rdhwr %0, "rd"\n\t" \
961 static inline int64_t cpu_get_real_ticks(void)
963 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
965 static uint32_t cyc_per_count
= 0;
967 if (!cyc_per_count
) {
968 MIPS_RDHWR("$3", cyc_per_count
);
971 MIPS_RDHWR("$2", count
);
972 return (int64_t)(count
* cyc_per_count
);
975 #elif defined(__alpha__)
977 static inline int64_t cpu_get_real_ticks(void)
982 asm volatile("rpcc %0" : "=r"(cc
));
989 /* The host CPU doesn't have an easily accessible cycle counter.
990 Just return a monotonically increasing value. This will be
991 totally wrong, but hopefully better than nothing. */
992 static inline int64_t cpu_get_real_ticks (void)
994 static int64_t ticks
= 0;
999 #ifdef CONFIG_PROFILER
1000 static inline int64_t profile_getclock(void)
1005 extern int64_t tcg_time
;
1006 extern int64_t dev_time
;