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
193 void qemu_clock_enable(QEMUClockType type, bool enabled);
197 * @type: the clock type
199 * Warp a clock to a new value
201 void qemu_clock_warp(QEMUClockType type);
204 * qemu_clock_register_reset_notifier:
205 * @type: the clock type
206 * @notifier: the notifier function
208 * Register a notifier function to call when the clock
209 * concerned is reset.
211 void qemu_clock_register_reset_notifier(QEMUClockType type,
215 * qemu_clock_unregister_reset_notifier:
216 * @type: the clock type
217 * @notifier: the notifier function
219 * Unregister a notifier function to call when the clock
220 * concerned is reset.
222 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
226 * qemu_clock_run_timers:
227 * @type: clock on which to operate
229 * Run all the timers associated with the default timer list
232 * Returns: true if any timer ran.
234 bool qemu_clock_run_timers(QEMUClockType type);
237 * qemu_clock_run_all_timers:
239 * Run all the timers associated with the default timer list
242 * Returns: true if any timer ran.
244 bool qemu_clock_run_all_timers(void);
252 * @type: the clock type to associate with the timerlist
253 * @cb: the callback to call on notification
254 * @opaque: the opaque pointer to pass to the callback
256 * Create a new timerlist associated with the clock of
259 * Returns: a pointer to the QEMUTimerList created
261 QEMUTimerList *timerlist_new(QEMUClockType type,
262 QEMUTimerListNotifyCB *cb, void *opaque);
266 * @timer_list: the timer list to free
268 * Frees a timer_list. It must have no active timers.
270 void timerlist_free(QEMUTimerList *timer_list);
273 * timerlist_has_timers:
274 * @timer_list: the timer list to operate on
276 * Determine whether a timer list has active timers
278 * Note that this function should not be used when other threads also access
279 * the timer list. The return value may be outdated by the time it is acted
282 * Returns: true if the timer list has timers.
284 bool timerlist_has_timers(QEMUTimerList *timer_list);
288 * @timer_list: the timer list to operate on
290 * Determine whether a timer list has any timers which
293 * Returns: true if the timer list has timers which
296 bool timerlist_expired(QEMUTimerList *timer_list);
299 * timerlist_deadline_ns:
300 * @timer_list: the timer list to operate on
302 * Determine the deadline for a timer_list, i.e.
303 * the number of nanoseconds until the first timer
304 * expires. Return -1 if there are no timers.
306 * Returns: the number of nanoseconds until the earliest
307 * timer expires -1 if none
309 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
312 * timerlist_get_clock:
313 * @timer_list: the timer list to operate on
315 * Determine the clock type associated with a timer list.
317 * Returns: the clock type associated with the
320 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
323 * timerlist_run_timers:
324 * @timer_list: the timer list to use
326 * Call all expired timers associated with the timer list.
328 * Returns: true if any timer expired
330 bool timerlist_run_timers(QEMUTimerList *timer_list);
334 * @timer_list: the timer list to use
336 * call the notifier callback associated with the timer list.
338 void timerlist_notify(QEMUTimerList *timer_list);
345 * timerlistgroup_init:
346 * @tlg: the timer list group
347 * @cb: the callback to call when a notify is required
348 * @opaque: the opaque pointer to be passed to the callback.
350 * Initialise a timer list group. This must already be
351 * allocated in memory and zeroed. The notifier callback is
352 * called whenever a clock in the timer list group is
353 * reenabled or whenever a timer associated with any timer
354 * list is modified. If @cb is specified as null, qemu_notify()
357 void timerlistgroup_init(QEMUTimerListGroup *tlg,
358 QEMUTimerListNotifyCB *cb, void *opaque);
361 * timerlistgroup_deinit:
362 * @tlg: the timer list group
364 * Deinitialise a timer list group. This must already be
365 * initialised. Note the memory is not freed.
367 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
370 * timerlistgroup_run_timers:
371 * @tlg: the timer list group
373 * Run the timers associated with a timer list group.
374 * This will run timers on multiple clocks.
376 * Returns: true if any timer callback ran
378 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
381 * timerlistgroup_deadline_ns:
382 * @tlg: the timer list group
384 * Determine the deadline of the soonest timer to
385 * expire associated with any timer list linked to
386 * the timer list group. Only clocks suitable for
387 * deadline calculation are included.
389 * Returns: the deadline in nanoseconds or -1 if no
390 * timers are to expire.
392 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
400 * @ts: the timer to be initialised
401 * @timer_list: the timer list to attach the timer to
402 * @scale: the scale value for the tiemr
403 * @cb: the callback to be called when the timer expires
404 * @opaque: the opaque pointer to be passed to the callback
406 * Initialise a new timer and associate it with @timer_list.
407 * The caller is responsible for allocating the memory.
409 * You need not call an explicit deinit call. Simply make
410 * sure it is not on a list with timer_del.
412 void timer_init(QEMUTimer *ts,
413 QEMUTimerList *timer_list, int scale,
414 QEMUTimerCB *cb, void *opaque);
418 * @timer_list: the timer list to attach the timer to
419 * @scale: the scale value for the tiemr
420 * @cb: the callback to be called when the timer expires
421 * @opaque: the opaque pointer to be passed to the callback
423 * Creeate a new timer and associate it with @timer_list.
424 * The memory is allocated by the function.
426 * This is not the preferred interface unless you know you
427 * are going to call timer_free. Use timer_init instead.
429 * Returns: a pointer to the timer
431 static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
436 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
437 timer_init(ts, timer_list, scale, cb, opaque);
443 * @type: the clock type to use
444 * @scale: the scale value for the tiemr
445 * @cb: the callback to be called when the timer expires
446 * @opaque: the opaque pointer to be passed to the callback
448 * Creeate a new timer and associate it with the default
449 * timer list for the clock type @type.
451 * Returns: a pointer to the timer
453 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
454 QEMUTimerCB *cb, void *opaque)
456 return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
461 * @clock: the clock to associate with the timer
462 * @callback: the callback to call when the timer expires
463 * @opaque: the opaque pointer to pass to the callback
465 * Create a new timer with nanosecond scale on the default timer list
466 * associated with the clock.
468 * Returns: a pointer to the newly created timer
470 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
473 return timer_new(type, SCALE_NS, cb, opaque);
478 * @clock: the clock to associate with the timer
479 * @callback: the callback to call when the timer expires
480 * @opaque: the opaque pointer to pass to the callback
482 * Create a new timer with microsecond scale on the default timer list
483 * associated with the clock.
485 * Returns: a pointer to the newly created timer
487 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
490 return timer_new(type, SCALE_US, cb, opaque);
495 * @clock: the clock to associate with the timer
496 * @callback: the callback to call when the timer expires
497 * @opaque: the opaque pointer to pass to the callback
499 * Create a new timer with millisecond scale on the default timer list
500 * associated with the clock.
502 * Returns: a pointer to the newly created timer
504 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
507 return timer_new(type, SCALE_MS, cb, opaque);
514 * Free a timer (it must not be on the active list)
516 void timer_free(QEMUTimer *ts);
522 * Delete a timer from the active list.
524 * This function is thread-safe but the timer and its timer list must not be
525 * freed while this function is running.
527 void timer_del(QEMUTimer *ts);
532 * @expire_time: the expiry time in nanoseconds
534 * Modify a timer to expire at @expire_time
536 * This function is thread-safe but the timer and its timer list must not be
537 * freed while this function is running.
539 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
544 * @expire_time: the expire time in the units associated with the timer
546 * Modify a timer to expiry at @expire_time, taking into
547 * account the scale associated with the timer.
549 * This function is thread-safe but the timer and its timer list must not be
550 * freed while this function is running.
552 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
558 * Determines whether a timer is pending (i.e. is on the
559 * active list of timers, whether or not it has not yet expired).
561 * Returns: true if the timer is pending
563 bool timer_pending(QEMUTimer *ts);
569 * Determines whether a timer has expired.
571 * Returns: true if the timer has expired
573 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
576 * timer_expire_time_ns:
579 * Determine the expiry time of a timer
581 * Returns: the expiry time in nanoseconds
583 uint64_t timer_expire_time_ns(QEMUTimer *ts);
590 * Read a timer @ts from a file @f
592 void timer_get(QEMUFile *f, QEMUTimer *ts);
599 void timer_put(QEMUFile *f, QEMUTimer *ts);
602 * General utility functions
606 * qemu_timeout_ns_to_ms:
607 * @ns: nanosecond timeout value
609 * Convert a nanosecond timeout value (or -1) to
610 * a millisecond value (or -1), always rounding up.
612 * Returns: millisecond timeout value
614 int qemu_timeout_ns_to_ms(int64_t ns);
618 * @fds: Array of file descriptors
619 * @nfds: number of file descriptors
620 * @timeout: timeout in nanoseconds
622 * Perform a poll like g_poll but with a timeout in nanoseconds.
623 * See g_poll documentation for further details.
625 * Returns: number of fds ready
627 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
630 * qemu_soonest_timeout:
631 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
632 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
634 * Calculates the soonest of two timeout values. -1 means infinite, which
635 * is later than any other value.
637 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
639 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
641 /* we can abuse the fact that -1 (which means infinite) is a maximal
642 * value when cast to unsigned. As this is disgusting, it's kept in
643 * one inline function.
645 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
651 * Initialise the clock & timer infrastructure
653 void init_clocks(void);
655 int64_t cpu_get_ticks(void);
656 void cpu_enable_ticks(void);
657 void cpu_disable_ticks(void);
659 static inline int64_t get_ticks_per_sec(void)
665 * Low level clock functions
668 /* real time host monotonic timer */
669 static inline int64_t get_clock_realtime(void)
673 gettimeofday(&tv, NULL);
674 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
677 /* Warning: don't insert tracepoints into these functions, they are
678 also used by simpletrace backend and tracepoints would cause
679 an infinite recursion! */
681 extern int64_t clock_freq;
683 static inline int64_t get_clock(void)
686 QueryPerformanceCounter(&ti);
687 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
692 extern int use_rt_clock;
694 static inline int64_t get_clock(void)
696 #ifdef CLOCK_MONOTONIC
699 clock_gettime(CLOCK_MONOTONIC, &ts);
700 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
704 /* XXX: using gettimeofday leads to problems if the date
705 changes, so it should be avoided. */
706 return get_clock_realtime();
712 int64_t cpu_get_icount(void);
713 int64_t cpu_get_clock(void);
715 /*******************************************/
716 /* host CPU ticks (if available) */
718 #if defined(_ARCH_PPC)
720 static inline int64_t cpu_get_real_ticks(void)
724 /* This reads timebase in one 64bit go and includes Cell workaround from:
725 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
727 __asm__ __volatile__ ("mftb %0\n\t"
732 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
734 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
735 "mfspr %L0,268\n\t" /* mftb */
736 "mfspr %0,269\n\t" /* mftbu */
739 : "=r" (retval), "=r" (junk));
744 #elif defined(__i386__)
746 static inline int64_t cpu_get_real_ticks(void)
749 asm volatile ("rdtsc" : "=A" (val));
753 #elif defined(__x86_64__)
755 static inline int64_t cpu_get_real_ticks(void)
759 asm volatile("rdtsc" : "=a" (low), "=d" (high));
766 #elif defined(__hppa__)
768 static inline int64_t cpu_get_real_ticks(void)
771 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
775 #elif defined(__ia64)
777 static inline int64_t cpu_get_real_ticks(void)
780 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
784 #elif defined(__s390__)
786 static inline int64_t cpu_get_real_ticks(void)
789 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
793 #elif defined(__sparc__)
795 static inline int64_t cpu_get_real_ticks (void)
799 asm volatile("rd %%tick,%0" : "=r"(rval));
802 /* We need an %o or %g register for this. For recent enough gcc
803 there is an "h" constraint for that. Don't bother with that. */
811 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
812 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
817 #elif defined(__mips__) && \
818 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
820 * binutils wants to use rdhwr only on mips32r2
821 * but as linux kernel emulate it, it's fine
825 #define MIPS_RDHWR(rd, value) { \
826 __asm__ __volatile__ (".set push\n\t" \
827 ".set mips32r2\n\t" \
828 "rdhwr %0, "rd"\n\t" \
833 static inline int64_t cpu_get_real_ticks(void)
835 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
837 static uint32_t cyc_per_count = 0;
839 if (!cyc_per_count) {
840 MIPS_RDHWR("$3", cyc_per_count);
843 MIPS_RDHWR("$2", count);
844 return (int64_t)(count * cyc_per_count);
847 #elif defined(__alpha__)
849 static inline int64_t cpu_get_real_ticks(void)
854 asm volatile("rpcc %0" : "=r"(cc));
861 /* The host CPU doesn't have an easily accessible cycle counter.
862 Just return a monotonically increasing value. This will be
863 totally wrong, but hopefully better than nothing. */
864 static inline int64_t cpu_get_real_ticks (void)
866 static int64_t ticks = 0;
871 #ifdef CONFIG_PROFILER
872 static inline int64_t profile_getclock(void)
874 return cpu_get_real_ticks();
877 extern int64_t qemu_time, qemu_time_start;
878 extern int64_t tlb_flush_time;
879 extern int64_t dev_time;