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 * Note that this function should not be used when other threads also access
119 * the timer list. The return value may be outdated by the time it is acted
122 * Returns: true if the clock's default timer list
123 * has timers attached
125 bool qemu_clock_has_timers(QEMUClockType type
);
128 * qemu_clock_expired:
129 * @type: the clock type
131 * Determines whether a clock's default timer list
132 * has an expired clock.
134 * Returns: true if the clock's default timer list has
137 bool qemu_clock_expired(QEMUClockType type
);
140 * qemu_clock_use_for_deadline:
141 * @type: the clock type
143 * Determine whether a clock should be used for deadline
144 * calculations. Some clocks, for instance vm_clock with
145 * use_icount set, do not count in nanoseconds. Such clocks
146 * are not used for deadline calculations, and are presumed
147 * to interrupt any poll using qemu_notify/aio_notify
150 * Returns: true if the clock runs in nanoseconds and
151 * should be used for a deadline.
153 bool qemu_clock_use_for_deadline(QEMUClockType type
);
156 * qemu_clock_deadline_ns_all:
157 * @type: the clock type
159 * Calculate the deadline across all timer lists associated
160 * with a clock (as opposed to just the default one)
161 * in nanoseconds, or -1 if no timer is set to expire.
163 * Returns: time until expiry in nanoseconds or -1
165 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
);
168 * qemu_clock_get_main_loop_timerlist:
169 * @type: the clock type
171 * Return the default timer list assocatiated with a clock.
173 * Returns: the default timer list
175 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
);
179 * @type: the clock type
181 * Call the notifier callback connected with the default timer
182 * list linked to the clock, or qemu_notify() if none.
184 void qemu_clock_notify(QEMUClockType type
);
188 * @type: the clock type
189 * @enabled: true to enable, false to disable
191 * Enable or disable a clock
192 * Disabling the clock will wait for related timerlists to stop
193 * executing qemu_run_timers. Thus, this functions should not
194 * be used from the callback of a timer that is based on @clock.
195 * Doing so would cause a deadlock.
197 * Caller should hold BQL.
199 void qemu_clock_enable(QEMUClockType type
, bool enabled
);
203 * @type: the clock type
205 * Warp a clock to a new value
207 void qemu_clock_warp(QEMUClockType type
);
210 * qemu_clock_register_reset_notifier:
211 * @type: the clock type
212 * @notifier: the notifier function
214 * Register a notifier function to call when the clock
215 * concerned is reset.
217 void qemu_clock_register_reset_notifier(QEMUClockType type
,
221 * qemu_clock_unregister_reset_notifier:
222 * @type: the clock type
223 * @notifier: the notifier function
225 * Unregister a notifier function to call when the clock
226 * concerned is reset.
228 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
232 * qemu_clock_run_timers:
233 * @type: clock on which to operate
235 * Run all the timers associated with the default timer list
238 * Returns: true if any timer ran.
240 bool qemu_clock_run_timers(QEMUClockType type
);
243 * qemu_clock_run_all_timers:
245 * Run all the timers associated with the default timer list
248 * Returns: true if any timer ran.
250 bool qemu_clock_run_all_timers(void);
258 * @type: the clock type to associate with the timerlist
259 * @cb: the callback to call on notification
260 * @opaque: the opaque pointer to pass to the callback
262 * Create a new timerlist associated with the clock of
265 * Returns: a pointer to the QEMUTimerList created
267 QEMUTimerList
*timerlist_new(QEMUClockType type
,
268 QEMUTimerListNotifyCB
*cb
, void *opaque
);
272 * @timer_list: the timer list to free
274 * Frees a timer_list. It must have no active timers.
276 void timerlist_free(QEMUTimerList
*timer_list
);
279 * timerlist_has_timers:
280 * @timer_list: the timer list to operate on
282 * Determine whether a timer list has active timers
284 * Note that this function should not be used when other threads also access
285 * the timer list. The return value may be outdated by the time it is acted
288 * Returns: true if the timer list has timers.
290 bool timerlist_has_timers(QEMUTimerList
*timer_list
);
294 * @timer_list: the timer list to operate on
296 * Determine whether a timer list has any timers which
299 * Returns: true if the timer list has timers which
302 bool timerlist_expired(QEMUTimerList
*timer_list
);
305 * timerlist_deadline_ns:
306 * @timer_list: the timer list to operate on
308 * Determine the deadline for a timer_list, i.e.
309 * the number of nanoseconds until the first timer
310 * expires. Return -1 if there are no timers.
312 * Returns: the number of nanoseconds until the earliest
313 * timer expires -1 if none
315 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
);
318 * timerlist_get_clock:
319 * @timer_list: the timer list to operate on
321 * Determine the clock type associated with a timer list.
323 * Returns: the clock type associated with the
326 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
);
329 * timerlist_run_timers:
330 * @timer_list: the timer list to use
332 * Call all expired timers associated with the timer list.
334 * Returns: true if any timer expired
336 bool timerlist_run_timers(QEMUTimerList
*timer_list
);
340 * @timer_list: the timer list to use
342 * call the notifier callback associated with the timer list.
344 void timerlist_notify(QEMUTimerList
*timer_list
);
351 * timerlistgroup_init:
352 * @tlg: the timer list group
353 * @cb: the callback to call when a notify is required
354 * @opaque: the opaque pointer to be passed to the callback.
356 * Initialise a timer list group. This must already be
357 * allocated in memory and zeroed. The notifier callback is
358 * called whenever a clock in the timer list group is
359 * reenabled or whenever a timer associated with any timer
360 * list is modified. If @cb is specified as null, qemu_notify()
363 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
364 QEMUTimerListNotifyCB
*cb
, void *opaque
);
367 * timerlistgroup_deinit:
368 * @tlg: the timer list group
370 * Deinitialise a timer list group. This must already be
371 * initialised. Note the memory is not freed.
373 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
);
376 * timerlistgroup_run_timers:
377 * @tlg: the timer list group
379 * Run the timers associated with a timer list group.
380 * This will run timers on multiple clocks.
382 * Returns: true if any timer callback ran
384 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
);
387 * timerlistgroup_deadline_ns:
388 * @tlg: the timer list group
390 * Determine the deadline of the soonest timer to
391 * expire associated with any timer list linked to
392 * the timer list group. Only clocks suitable for
393 * deadline calculation are included.
395 * Returns: the deadline in nanoseconds or -1 if no
396 * timers are to expire.
398 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
);
406 * @ts: the timer to be initialised
407 * @timer_list: the timer list to attach the timer to
408 * @scale: the scale value for the timer
409 * @cb: the callback to be called when the timer expires
410 * @opaque: the opaque pointer to be passed to the callback
412 * Initialise a new timer and associate it with @timer_list.
413 * The caller is responsible for allocating the memory.
415 * You need not call an explicit deinit call. Simply make
416 * sure it is not on a list with timer_del.
418 void timer_init(QEMUTimer
*ts
,
419 QEMUTimerList
*timer_list
, int scale
,
420 QEMUTimerCB
*cb
, void *opaque
);
424 * @timer_list: the timer list to attach the timer to
425 * @scale: the scale value for the timer
426 * @cb: the callback to be called when the timer expires
427 * @opaque: the opaque pointer to be passed to the callback
429 * Creeate a new timer and associate it with @timer_list.
430 * The memory is allocated by the function.
432 * This is not the preferred interface unless you know you
433 * are going to call timer_free. Use timer_init instead.
435 * Returns: a pointer to the timer
437 static inline QEMUTimer
*timer_new_tl(QEMUTimerList
*timer_list
,
442 QEMUTimer
*ts
= g_malloc0(sizeof(QEMUTimer
));
443 timer_init(ts
, timer_list
, scale
, cb
, opaque
);
449 * @type: the clock type to use
450 * @scale: the scale value for the timer
451 * @cb: the callback to be called when the timer expires
452 * @opaque: the opaque pointer to be passed to the callback
454 * Creeate a new timer and associate it with the default
455 * timer list for the clock type @type.
457 * Returns: a pointer to the timer
459 static inline QEMUTimer
*timer_new(QEMUClockType type
, int scale
,
460 QEMUTimerCB
*cb
, void *opaque
)
462 return timer_new_tl(main_loop_tlg
.tl
[type
], scale
, cb
, opaque
);
467 * @clock: the clock to associate with the timer
468 * @callback: the callback to call when the timer expires
469 * @opaque: the opaque pointer to pass to the callback
471 * Create a new timer with nanosecond scale on the default timer list
472 * associated with the clock.
474 * Returns: a pointer to the newly created timer
476 static inline QEMUTimer
*timer_new_ns(QEMUClockType type
, QEMUTimerCB
*cb
,
479 return timer_new(type
, SCALE_NS
, cb
, opaque
);
484 * @clock: the clock to associate with the timer
485 * @callback: the callback to call when the timer expires
486 * @opaque: the opaque pointer to pass to the callback
488 * Create a new timer with microsecond scale on the default timer list
489 * associated with the clock.
491 * Returns: a pointer to the newly created timer
493 static inline QEMUTimer
*timer_new_us(QEMUClockType type
, QEMUTimerCB
*cb
,
496 return timer_new(type
, SCALE_US
, cb
, opaque
);
501 * @clock: the clock to associate with the timer
502 * @callback: the callback to call when the timer expires
503 * @opaque: the opaque pointer to pass to the callback
505 * Create a new timer with millisecond scale on the default timer list
506 * associated with the clock.
508 * Returns: a pointer to the newly created timer
510 static inline QEMUTimer
*timer_new_ms(QEMUClockType type
, QEMUTimerCB
*cb
,
513 return timer_new(type
, SCALE_MS
, cb
, opaque
);
520 * Free a timer (it must not be on the active list)
522 void timer_free(QEMUTimer
*ts
);
528 * Delete a timer from the active list.
530 * This function is thread-safe but the timer and its timer list must not be
531 * freed while this function is running.
533 void timer_del(QEMUTimer
*ts
);
538 * @expire_time: the expiry time in nanoseconds
540 * Modify a timer to expire at @expire_time
542 * This function is thread-safe but the timer and its timer list must not be
543 * freed while this function is running.
545 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
);
548 * timer_mod_anticipate_ns:
550 * @expire_time: the expiry time in nanoseconds
552 * Modify a timer to expire at @expire_time or the current time,
553 * whichever comes earlier.
555 * This function is thread-safe but the timer and its timer list must not be
556 * freed while this function is running.
558 void timer_mod_anticipate_ns(QEMUTimer
*ts
, int64_t expire_time
);
563 * @expire_time: the expire time in the units associated with the timer
565 * Modify a timer to expiry at @expire_time, taking into
566 * account the scale associated with the timer.
568 * This function is thread-safe but the timer and its timer list must not be
569 * freed while this function is running.
571 void timer_mod(QEMUTimer
*ts
, int64_t expire_timer
);
574 * timer_mod_anticipate:
576 * @expire_time: the expiry time in nanoseconds
578 * Modify a timer to expire at @expire_time or the current time, whichever
579 * comes earlier, taking into account the scale associated with the timer.
581 * This function is thread-safe but the timer and its timer list must not be
582 * freed while this function is running.
584 void timer_mod_anticipate(QEMUTimer
*ts
, int64_t expire_time
);
590 * Determines whether a timer is pending (i.e. is on the
591 * active list of timers, whether or not it has not yet expired).
593 * Returns: true if the timer is pending
595 bool timer_pending(QEMUTimer
*ts
);
601 * Determines whether a timer has expired.
603 * Returns: true if the timer has expired
605 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
608 * timer_expire_time_ns:
611 * Determine the expiry time of a timer
613 * Returns: the expiry time in nanoseconds
615 uint64_t timer_expire_time_ns(QEMUTimer
*ts
);
622 * Read a timer @ts from a file @f
624 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
);
631 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
);
634 * General utility functions
638 * qemu_timeout_ns_to_ms:
639 * @ns: nanosecond timeout value
641 * Convert a nanosecond timeout value (or -1) to
642 * a millisecond value (or -1), always rounding up.
644 * Returns: millisecond timeout value
646 int qemu_timeout_ns_to_ms(int64_t ns
);
650 * @fds: Array of file descriptors
651 * @nfds: number of file descriptors
652 * @timeout: timeout in nanoseconds
654 * Perform a poll like g_poll but with a timeout in nanoseconds.
655 * See g_poll documentation for further details.
657 * Returns: number of fds ready
659 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
);
662 * qemu_soonest_timeout:
663 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
664 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
666 * Calculates the soonest of two timeout values. -1 means infinite, which
667 * is later than any other value.
669 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
671 static inline int64_t qemu_soonest_timeout(int64_t timeout1
, int64_t timeout2
)
673 /* we can abuse the fact that -1 (which means infinite) is a maximal
674 * value when cast to unsigned. As this is disgusting, it's kept in
675 * one inline function.
677 return ((uint64_t) timeout1
< (uint64_t) timeout2
) ? timeout1
: timeout2
;
683 * Initialise the clock & timer infrastructure
685 void init_clocks(void);
687 int64_t cpu_get_ticks(void);
688 /* Caller must hold BQL */
689 void cpu_enable_ticks(void);
690 /* Caller must hold BQL */
691 void cpu_disable_ticks(void);
693 static inline int64_t get_ticks_per_sec(void)
699 * Low level clock functions
702 /* real time host monotonic timer */
703 static inline int64_t get_clock_realtime(void)
707 gettimeofday(&tv
, NULL
);
708 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
711 /* Warning: don't insert tracepoints into these functions, they are
712 also used by simpletrace backend and tracepoints would cause
713 an infinite recursion! */
715 extern int64_t clock_freq
;
717 static inline int64_t get_clock(void)
720 QueryPerformanceCounter(&ti
);
721 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
726 extern int use_rt_clock
;
728 static inline int64_t get_clock(void)
730 #ifdef CLOCK_MONOTONIC
733 clock_gettime(CLOCK_MONOTONIC
, &ts
);
734 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
738 /* XXX: using gettimeofday leads to problems if the date
739 changes, so it should be avoided. */
740 return get_clock_realtime();
746 int64_t cpu_get_icount(void);
747 int64_t cpu_get_clock(void);
748 int64_t cpu_get_clock_offset(void);
749 int64_t cpu_icount_to_ns(int64_t icount
);
751 /*******************************************/
752 /* host CPU ticks (if available) */
754 #if defined(_ARCH_PPC)
756 static inline int64_t cpu_get_real_ticks(void)
760 /* This reads timebase in one 64bit go and includes Cell workaround from:
761 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
763 __asm__
__volatile__ ("mftb %0\n\t"
768 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
770 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
771 "mfspr %L0,268\n\t" /* mftb */
772 "mfspr %0,269\n\t" /* mftbu */
775 : "=r" (retval
), "=r" (junk
));
780 #elif defined(__i386__)
782 static inline int64_t cpu_get_real_ticks(void)
785 asm volatile ("rdtsc" : "=A" (val
));
789 #elif defined(__x86_64__)
791 static inline int64_t cpu_get_real_ticks(void)
795 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
802 #elif defined(__hppa__)
804 static inline int64_t cpu_get_real_ticks(void)
807 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
811 #elif defined(__ia64)
813 static inline int64_t cpu_get_real_ticks(void)
816 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
820 #elif defined(__s390__)
822 static inline int64_t cpu_get_real_ticks(void)
825 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
829 #elif defined(__sparc__)
831 static inline int64_t cpu_get_real_ticks (void)
835 asm volatile("rd %%tick,%0" : "=r"(rval
));
838 /* We need an %o or %g register for this. For recent enough gcc
839 there is an "h" constraint for that. Don't bother with that. */
847 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
848 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
853 #elif defined(__mips__) && \
854 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
856 * binutils wants to use rdhwr only on mips32r2
857 * but as linux kernel emulate it, it's fine
861 #define MIPS_RDHWR(rd, value) { \
862 __asm__ __volatile__ (".set push\n\t" \
863 ".set mips32r2\n\t" \
864 "rdhwr %0, "rd"\n\t" \
869 static inline int64_t cpu_get_real_ticks(void)
871 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
873 static uint32_t cyc_per_count
= 0;
875 if (!cyc_per_count
) {
876 MIPS_RDHWR("$3", cyc_per_count
);
879 MIPS_RDHWR("$2", count
);
880 return (int64_t)(count
* cyc_per_count
);
883 #elif defined(__alpha__)
885 static inline int64_t cpu_get_real_ticks(void)
890 asm volatile("rpcc %0" : "=r"(cc
));
897 /* The host CPU doesn't have an easily accessible cycle counter.
898 Just return a monotonically increasing value. This will be
899 totally wrong, but hopefully better than nothing. */
900 static inline int64_t cpu_get_real_ticks (void)
902 static int64_t ticks
= 0;
907 #ifdef CONFIG_PROFILER
908 static inline int64_t profile_getclock(void)
910 return cpu_get_real_ticks();
913 extern int64_t qemu_time
, qemu_time_start
;
914 extern int64_t tlb_flush_time
;
915 extern int64_t dev_time
;