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(QEMUTimer
*ts
,
427 QEMUTimerList
*timer_list
, int scale
,
428 QEMUTimerCB
*cb
, void *opaque
);
432 * @timer_list: the timer list to attach the timer to
433 * @scale: the scale value for the timer
434 * @cb: the callback to be called when the timer expires
435 * @opaque: the opaque pointer to be passed to the callback
437 * Creeate a new timer and associate it with @timer_list.
438 * The memory is allocated by the function.
440 * This is not the preferred interface unless you know you
441 * are going to call timer_free. Use timer_init instead.
443 * Returns: a pointer to the timer
445 static inline QEMUTimer
*timer_new_tl(QEMUTimerList
*timer_list
,
450 QEMUTimer
*ts
= g_malloc0(sizeof(QEMUTimer
));
451 timer_init(ts
, timer_list
, scale
, cb
, opaque
);
457 * @type: the clock type to use
458 * @scale: the scale value for the timer
459 * @cb: the callback to be called when the timer expires
460 * @opaque: the opaque pointer to be passed to the callback
462 * Creeate a new timer and associate it with the default
463 * timer list for the clock type @type.
465 * Returns: a pointer to the timer
467 static inline QEMUTimer
*timer_new(QEMUClockType type
, int scale
,
468 QEMUTimerCB
*cb
, void *opaque
)
470 return timer_new_tl(main_loop_tlg
.tl
[type
], scale
, cb
, opaque
);
475 * @clock: the clock to associate with the timer
476 * @callback: the callback to call when the timer expires
477 * @opaque: the opaque pointer to pass to the callback
479 * Create a new timer with nanosecond scale on the default timer list
480 * associated with the clock.
482 * Returns: a pointer to the newly created timer
484 static inline QEMUTimer
*timer_new_ns(QEMUClockType type
, QEMUTimerCB
*cb
,
487 return timer_new(type
, SCALE_NS
, cb
, opaque
);
492 * @clock: the clock to associate with the timer
493 * @callback: the callback to call when the timer expires
494 * @opaque: the opaque pointer to pass to the callback
496 * Create a new timer with microsecond scale on the default timer list
497 * associated with the clock.
499 * Returns: a pointer to the newly created timer
501 static inline QEMUTimer
*timer_new_us(QEMUClockType type
, QEMUTimerCB
*cb
,
504 return timer_new(type
, SCALE_US
, cb
, opaque
);
509 * @clock: the clock to associate with the timer
510 * @callback: the callback to call when the timer expires
511 * @opaque: the opaque pointer to pass to the callback
513 * Create a new timer with millisecond scale on the default timer list
514 * associated with the clock.
516 * Returns: a pointer to the newly created timer
518 static inline QEMUTimer
*timer_new_ms(QEMUClockType type
, QEMUTimerCB
*cb
,
521 return timer_new(type
, SCALE_MS
, cb
, opaque
);
528 * Free a timer (it must not be on the active list)
530 void timer_free(QEMUTimer
*ts
);
536 * Delete a timer from the active list.
538 * This function is thread-safe but the timer and its timer list must not be
539 * freed while this function is running.
541 void timer_del(QEMUTimer
*ts
);
546 * @expire_time: the expiry time in nanoseconds
548 * Modify a timer to expire at @expire_time
550 * This function is thread-safe but the timer and its timer list must not be
551 * freed while this function is running.
553 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
);
556 * timer_mod_anticipate_ns:
558 * @expire_time: the expiry time in nanoseconds
560 * Modify a timer to expire at @expire_time or the current time,
561 * whichever comes earlier.
563 * This function is thread-safe but the timer and its timer list must not be
564 * freed while this function is running.
566 void timer_mod_anticipate_ns(QEMUTimer
*ts
, int64_t expire_time
);
571 * @expire_time: the expire time in the units associated with the timer
573 * Modify a timer to expiry at @expire_time, taking into
574 * account the scale associated with the timer.
576 * This function is thread-safe but the timer and its timer list must not be
577 * freed while this function is running.
579 void timer_mod(QEMUTimer
*ts
, int64_t expire_timer
);
582 * timer_mod_anticipate:
584 * @expire_time: the expiry time in nanoseconds
586 * Modify a timer to expire at @expire_time or the current time, whichever
587 * comes earlier, taking into account the scale associated with the timer.
589 * This function is thread-safe but the timer and its timer list must not be
590 * freed while this function is running.
592 void timer_mod_anticipate(QEMUTimer
*ts
, int64_t expire_time
);
598 * Determines whether a timer is pending (i.e. is on the
599 * active list of timers, whether or not it has not yet expired).
601 * Returns: true if the timer is pending
603 bool timer_pending(QEMUTimer
*ts
);
609 * Determines whether a timer has expired.
611 * Returns: true if the timer has expired
613 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
616 * timer_expire_time_ns:
619 * Determine the expiry time of a timer
621 * Returns: the expiry time in nanoseconds
623 uint64_t timer_expire_time_ns(QEMUTimer
*ts
);
630 * Read a timer @ts from a file @f
632 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
);
639 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
);
642 * General utility functions
646 * qemu_timeout_ns_to_ms:
647 * @ns: nanosecond timeout value
649 * Convert a nanosecond timeout value (or -1) to
650 * a millisecond value (or -1), always rounding up.
652 * Returns: millisecond timeout value
654 int qemu_timeout_ns_to_ms(int64_t ns
);
658 * @fds: Array of file descriptors
659 * @nfds: number of file descriptors
660 * @timeout: timeout in nanoseconds
662 * Perform a poll like g_poll but with a timeout in nanoseconds.
663 * See g_poll documentation for further details.
665 * Returns: number of fds ready
667 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
);
670 * qemu_soonest_timeout:
671 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
672 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
674 * Calculates the soonest of two timeout values. -1 means infinite, which
675 * is later than any other value.
677 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
679 static inline int64_t qemu_soonest_timeout(int64_t timeout1
, int64_t timeout2
)
681 /* we can abuse the fact that -1 (which means infinite) is a maximal
682 * value when cast to unsigned. As this is disgusting, it's kept in
683 * one inline function.
685 return ((uint64_t) timeout1
< (uint64_t) timeout2
) ? timeout1
: timeout2
;
691 * Initialise the clock & timer infrastructure
693 void init_clocks(void);
695 int64_t cpu_get_ticks(void);
696 /* Caller must hold BQL */
697 void cpu_enable_ticks(void);
698 /* Caller must hold BQL */
699 void cpu_disable_ticks(void);
701 static inline int64_t get_ticks_per_sec(void)
707 * Low level clock functions
710 /* real time host monotonic timer */
711 static inline int64_t get_clock_realtime(void)
715 gettimeofday(&tv
, NULL
);
716 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
719 /* Warning: don't insert tracepoints into these functions, they are
720 also used by simpletrace backend and tracepoints would cause
721 an infinite recursion! */
723 extern int64_t clock_freq
;
725 static inline int64_t get_clock(void)
728 QueryPerformanceCounter(&ti
);
729 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
734 extern int use_rt_clock
;
736 static inline int64_t get_clock(void)
738 #ifdef CLOCK_MONOTONIC
741 clock_gettime(CLOCK_MONOTONIC
, &ts
);
742 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
746 /* XXX: using gettimeofday leads to problems if the date
747 changes, so it should be avoided. */
748 return get_clock_realtime();
754 int64_t cpu_get_icount_raw(void);
755 int64_t cpu_get_icount(void);
756 int64_t cpu_get_clock(void);
757 int64_t cpu_get_clock_offset(void);
758 int64_t cpu_icount_to_ns(int64_t icount
);
760 /*******************************************/
761 /* host CPU ticks (if available) */
763 #if defined(_ARCH_PPC)
765 static inline int64_t cpu_get_real_ticks(void)
769 /* This reads timebase in one 64bit go and includes Cell workaround from:
770 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
772 __asm__
__volatile__ ("mftb %0\n\t"
777 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
779 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
780 "mfspr %L0,268\n\t" /* mftb */
781 "mfspr %0,269\n\t" /* mftbu */
784 : "=r" (retval
), "=r" (junk
));
789 #elif defined(__i386__)
791 static inline int64_t cpu_get_real_ticks(void)
794 asm volatile ("rdtsc" : "=A" (val
));
798 #elif defined(__x86_64__)
800 static inline int64_t cpu_get_real_ticks(void)
804 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
811 #elif defined(__hppa__)
813 static inline int64_t cpu_get_real_ticks(void)
816 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
820 #elif defined(__ia64)
822 static inline int64_t cpu_get_real_ticks(void)
825 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
829 #elif defined(__s390__)
831 static inline int64_t cpu_get_real_ticks(void)
834 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
838 #elif defined(__sparc__)
840 static inline int64_t cpu_get_real_ticks (void)
844 asm volatile("rd %%tick,%0" : "=r"(rval
));
847 /* We need an %o or %g register for this. For recent enough gcc
848 there is an "h" constraint for that. Don't bother with that. */
856 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
857 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
862 #elif defined(__mips__) && \
863 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
865 * binutils wants to use rdhwr only on mips32r2
866 * but as linux kernel emulate it, it's fine
870 #define MIPS_RDHWR(rd, value) { \
871 __asm__ __volatile__ (".set push\n\t" \
872 ".set mips32r2\n\t" \
873 "rdhwr %0, "rd"\n\t" \
878 static inline int64_t cpu_get_real_ticks(void)
880 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
882 static uint32_t cyc_per_count
= 0;
884 if (!cyc_per_count
) {
885 MIPS_RDHWR("$3", cyc_per_count
);
888 MIPS_RDHWR("$2", count
);
889 return (int64_t)(count
* cyc_per_count
);
892 #elif defined(__alpha__)
894 static inline int64_t cpu_get_real_ticks(void)
899 asm volatile("rpcc %0" : "=r"(cc
));
906 /* The host CPU doesn't have an easily accessible cycle counter.
907 Just return a monotonically increasing value. This will be
908 totally wrong, but hopefully better than nothing. */
909 static inline int64_t cpu_get_real_ticks (void)
911 static int64_t ticks
= 0;
916 #ifdef CONFIG_PROFILER
917 static inline int64_t profile_getclock(void)
919 return cpu_get_real_ticks();
922 extern int64_t qemu_time
, qemu_time_start
;
923 extern int64_t tlb_flush_time
;
924 extern int64_t dev_time
;