xref: /qemu/include/qemu/timer.h (revision 3cf42b8b)
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
3 
4 #include "qemu-common.h"
5 #include "qemu/notify.h"
6 #include "qemu/host-utils.h"
7 
8 #define NANOSECONDS_PER_SECOND 1000000000LL
9 
10 /* timers */
11 
12 #define SCALE_MS 1000000
13 #define SCALE_US 1000
14 #define SCALE_NS 1
15 
16 /**
17  * QEMUClockType:
18  *
19  * The following clock types are available:
20  *
21  * @QEMU_CLOCK_REALTIME: Real time clock
22  *
23  * The real time clock should be used only for stuff which does not
24  * change the virtual machine state, as it runs even if the virtual
25  * machine is stopped.
26  *
27  * @QEMU_CLOCK_VIRTUAL: virtual clock
28  *
29  * The virtual clock only runs during the emulation. It stops
30  * when the virtual machine is stopped.
31  *
32  * @QEMU_CLOCK_HOST: host clock
33  *
34  * The host clock should be used 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).
38  *
39  * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
40  *
41  * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
42  * In icount mode, this clock counts nanoseconds while the virtual
43  * machine is running.  It is used to increase @QEMU_CLOCK_VIRTUAL
44  * while the CPUs are sleeping and thus not executing instructions.
45  */
46 
47 typedef enum {
48     QEMU_CLOCK_REALTIME = 0,
49     QEMU_CLOCK_VIRTUAL = 1,
50     QEMU_CLOCK_HOST = 2,
51     QEMU_CLOCK_VIRTUAL_RT = 3,
52     QEMU_CLOCK_MAX
53 } QEMUClockType;
54 
55 typedef struct QEMUTimerList QEMUTimerList;
56 
57 struct QEMUTimerListGroup {
58     QEMUTimerList *tl[QEMU_CLOCK_MAX];
59 };
60 
61 typedef void QEMUTimerCB(void *opaque);
62 typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
63 
64 struct QEMUTimer {
65     int64_t expire_time;        /* in nanoseconds */
66     QEMUTimerList *timer_list;
67     QEMUTimerCB *cb;
68     void *opaque;
69     QEMUTimer *next;
70     int scale;
71 };
72 
73 extern QEMUTimerListGroup main_loop_tlg;
74 
75 /*
76  * qemu_clock_get_ns;
77  * @type: the clock type
78  *
79  * Get the nanosecond value of a clock with
80  * type @type
81  *
82  * Returns: the clock value in nanoseconds
83  */
84 int64_t qemu_clock_get_ns(QEMUClockType type);
85 
86 /**
87  * qemu_clock_get_ms;
88  * @type: the clock type
89  *
90  * Get the millisecond value of a clock with
91  * type @type
92  *
93  * Returns: the clock value in milliseconds
94  */
95 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
96 {
97     return qemu_clock_get_ns(type) / SCALE_MS;
98 }
99 
100 /**
101  * qemu_clock_get_us;
102  * @type: the clock type
103  *
104  * Get the microsecond value of a clock with
105  * type @type
106  *
107  * Returns: the clock value in microseconds
108  */
109 static inline int64_t qemu_clock_get_us(QEMUClockType type)
110 {
111     return qemu_clock_get_ns(type) / SCALE_US;
112 }
113 
114 /**
115  * qemu_clock_has_timers:
116  * @type: the clock type
117  *
118  * Determines whether a clock's default timer list
119  * has timers attached
120  *
121  * Note that this function should not be used when other threads also access
122  * the timer list.  The return value may be outdated by the time it is acted
123  * upon.
124  *
125  * Returns: true if the clock's default timer list
126  * has timers attached
127  */
128 bool qemu_clock_has_timers(QEMUClockType type);
129 
130 /**
131  * qemu_clock_expired:
132  * @type: the clock type
133  *
134  * Determines whether a clock's default timer list
135  * has an expired timer.
136  *
137  * Returns: true if the clock's default timer list has
138  * an expired timer
139  */
140 bool qemu_clock_expired(QEMUClockType type);
141 
142 /**
143  * qemu_clock_use_for_deadline:
144  * @type: the clock type
145  *
146  * Determine whether a clock should be used for deadline
147  * calculations. Some clocks, for instance vm_clock with
148  * use_icount set, do not count in nanoseconds. Such clocks
149  * are not used for deadline calculations, and are presumed
150  * to interrupt any poll using qemu_notify/aio_notify
151  * etc.
152  *
153  * Returns: true if the clock runs in nanoseconds and
154  * should be used for a deadline.
155  */
156 bool qemu_clock_use_for_deadline(QEMUClockType type);
157 
158 /**
159  * qemu_clock_deadline_ns_all:
160  * @type: the clock type
161  *
162  * Calculate the deadline across all timer lists associated
163  * with a clock (as opposed to just the default one)
164  * in nanoseconds, or -1 if no timer is set to expire.
165  *
166  * Returns: time until expiry in nanoseconds or -1
167  */
168 int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
169 
170 /**
171  * qemu_clock_get_main_loop_timerlist:
172  * @type: the clock type
173  *
174  * Return the default timer list associated with a clock.
175  *
176  * Returns: the default timer list
177  */
178 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
179 
180 /**
181  * qemu_clock_nofify:
182  * @type: the clock type
183  *
184  * Call the notifier callback connected with the default timer
185  * list linked to the clock, or qemu_notify() if none.
186  */
187 void qemu_clock_notify(QEMUClockType type);
188 
189 /**
190  * qemu_clock_enable:
191  * @type: the clock type
192  * @enabled: true to enable, false to disable
193  *
194  * Enable or disable a clock
195  * Disabling the clock will wait for related timerlists to stop
196  * executing qemu_run_timers.  Thus, this functions should not
197  * be used from the callback of a timer that is based on @clock.
198  * Doing so would cause a deadlock.
199  *
200  * Caller should hold BQL.
201  */
202 void qemu_clock_enable(QEMUClockType type, bool enabled);
203 
204 /**
205  * qemu_start_warp_timer:
206  *
207  * Starts a timer for virtual clock update
208  */
209 void qemu_start_warp_timer(void);
210 
211 /**
212  * qemu_clock_register_reset_notifier:
213  * @type: the clock type
214  * @notifier: the notifier function
215  *
216  * Register a notifier function to call when the clock
217  * concerned is reset.
218  */
219 void qemu_clock_register_reset_notifier(QEMUClockType type,
220                                         Notifier *notifier);
221 
222 /**
223  * qemu_clock_unregister_reset_notifier:
224  * @type: the clock type
225  * @notifier: the notifier function
226  *
227  * Unregister a notifier function to call when the clock
228  * concerned is reset.
229  */
230 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
231                                           Notifier *notifier);
232 
233 /**
234  * qemu_clock_run_timers:
235  * @type: clock on which to operate
236  *
237  * Run all the timers associated with the default timer list
238  * of a clock.
239  *
240  * Returns: true if any timer ran.
241  */
242 bool qemu_clock_run_timers(QEMUClockType type);
243 
244 /**
245  * qemu_clock_run_all_timers:
246  *
247  * Run all the timers associated with the default timer list
248  * of every clock.
249  *
250  * Returns: true if any timer ran.
251  */
252 bool qemu_clock_run_all_timers(void);
253 
254 /**
255  * qemu_clock_get_last:
256  *
257  * Returns last clock query time.
258  */
259 uint64_t qemu_clock_get_last(QEMUClockType type);
260 /**
261  * qemu_clock_set_last:
262  *
263  * Sets last clock query time.
264  */
265 void qemu_clock_set_last(QEMUClockType type, uint64_t last);
266 
267 
268 /*
269  * QEMUTimerList
270  */
271 
272 /**
273  * timerlist_new:
274  * @type: the clock type to associate with the timerlist
275  * @cb: the callback to call on notification
276  * @opaque: the opaque pointer to pass to the callback
277  *
278  * Create a new timerlist associated with the clock of
279  * type @type.
280  *
281  * Returns: a pointer to the QEMUTimerList created
282  */
283 QEMUTimerList *timerlist_new(QEMUClockType type,
284                              QEMUTimerListNotifyCB *cb, void *opaque);
285 
286 /**
287  * timerlist_free:
288  * @timer_list: the timer list to free
289  *
290  * Frees a timer_list. It must have no active timers.
291  */
292 void timerlist_free(QEMUTimerList *timer_list);
293 
294 /**
295  * timerlist_has_timers:
296  * @timer_list: the timer list to operate on
297  *
298  * Determine whether a timer list has active timers
299  *
300  * Note that this function should not be used when other threads also access
301  * the timer list.  The return value may be outdated by the time it is acted
302  * upon.
303  *
304  * Returns: true if the timer list has timers.
305  */
306 bool timerlist_has_timers(QEMUTimerList *timer_list);
307 
308 /**
309  * timerlist_expired:
310  * @timer_list: the timer list to operate on
311  *
312  * Determine whether a timer list has any timers which
313  * are expired.
314  *
315  * Returns: true if the timer list has timers which
316  * have expired.
317  */
318 bool timerlist_expired(QEMUTimerList *timer_list);
319 
320 /**
321  * timerlist_deadline_ns:
322  * @timer_list: the timer list to operate on
323  *
324  * Determine the deadline for a timer_list, i.e.
325  * the number of nanoseconds until the first timer
326  * expires. Return -1 if there are no timers.
327  *
328  * Returns: the number of nanoseconds until the earliest
329  * timer expires -1 if none
330  */
331 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
332 
333 /**
334  * timerlist_get_clock:
335  * @timer_list: the timer list to operate on
336  *
337  * Determine the clock type associated with a timer list.
338  *
339  * Returns: the clock type associated with the
340  * timer list.
341  */
342 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
343 
344 /**
345  * timerlist_run_timers:
346  * @timer_list: the timer list to use
347  *
348  * Call all expired timers associated with the timer list.
349  *
350  * Returns: true if any timer expired
351  */
352 bool timerlist_run_timers(QEMUTimerList *timer_list);
353 
354 /**
355  * timerlist_notify:
356  * @timer_list: the timer list to use
357  *
358  * call the notifier callback associated with the timer list.
359  */
360 void timerlist_notify(QEMUTimerList *timer_list);
361 
362 /*
363  * QEMUTimerListGroup
364  */
365 
366 /**
367  * timerlistgroup_init:
368  * @tlg: the timer list group
369  * @cb: the callback to call when a notify is required
370  * @opaque: the opaque pointer to be passed to the callback.
371  *
372  * Initialise a timer list group. This must already be
373  * allocated in memory and zeroed. The notifier callback is
374  * called whenever a clock in the timer list group is
375  * reenabled or whenever a timer associated with any timer
376  * list is modified. If @cb is specified as null, qemu_notify()
377  * is used instead.
378  */
379 void timerlistgroup_init(QEMUTimerListGroup *tlg,
380                          QEMUTimerListNotifyCB *cb, void *opaque);
381 
382 /**
383  * timerlistgroup_deinit:
384  * @tlg: the timer list group
385  *
386  * Deinitialise a timer list group. This must already be
387  * initialised. Note the memory is not freed.
388  */
389 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
390 
391 /**
392  * timerlistgroup_run_timers:
393  * @tlg: the timer list group
394  *
395  * Run the timers associated with a timer list group.
396  * This will run timers on multiple clocks.
397  *
398  * Returns: true if any timer callback ran
399  */
400 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
401 
402 /**
403  * timerlistgroup_deadline_ns:
404  * @tlg: the timer list group
405  *
406  * Determine the deadline of the soonest timer to
407  * expire associated with any timer list linked to
408  * the timer list group. Only clocks suitable for
409  * deadline calculation are included.
410  *
411  * Returns: the deadline in nanoseconds or -1 if no
412  * timers are to expire.
413  */
414 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
415 
416 /*
417  * QEMUTimer
418  */
419 
420 /**
421  * timer_init_tl:
422  * @ts: the timer to be initialised
423  * @timer_list: the timer list to attach the timer to
424  * @scale: the scale value for the timer
425  * @cb: the callback to be called when the timer expires
426  * @opaque: the opaque pointer to be passed to the callback
427  *
428  * Initialise a new timer and associate it with @timer_list.
429  * The caller is responsible for allocating the memory.
430  *
431  * You need not call an explicit deinit call. Simply make
432  * sure it is not on a list with timer_del.
433  */
434 void timer_init_tl(QEMUTimer *ts,
435                    QEMUTimerList *timer_list, int scale,
436                    QEMUTimerCB *cb, void *opaque);
437 
438 /**
439  * timer_init:
440  * @ts: the timer to be initialised
441  * @type: the clock to associate with the timer
442  * @scale: the scale value for the timer
443  * @cb: the callback to call when the timer expires
444  * @opaque: the opaque pointer to pass to the callback
445  *
446  * Initialize a timer with the given scale on the default timer list
447  * associated with the clock.
448  *
449  * You need not call an explicit deinit call. Simply make
450  * sure it is not on a list with timer_del.
451  */
452 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
453                               QEMUTimerCB *cb, void *opaque)
454 {
455     timer_init_tl(ts, main_loop_tlg.tl[type], scale, cb, opaque);
456 }
457 
458 /**
459  * timer_init_ns:
460  * @ts: the timer to be initialised
461  * @type: the clock to associate with the timer
462  * @cb: the callback to call when the timer expires
463  * @opaque: the opaque pointer to pass to the callback
464  *
465  * Initialize a timer with nanosecond scale on the default timer list
466  * associated with the clock.
467  *
468  * You need not call an explicit deinit call. Simply make
469  * sure it is not on a list with timer_del.
470  */
471 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
472                                  QEMUTimerCB *cb, void *opaque)
473 {
474     timer_init(ts, type, SCALE_NS, cb, opaque);
475 }
476 
477 /**
478  * timer_init_us:
479  * @ts: the timer to be initialised
480  * @type: the clock to associate with the timer
481  * @cb: the callback to call when the timer expires
482  * @opaque: the opaque pointer to pass to the callback
483  *
484  * Initialize a timer with microsecond scale on the default timer list
485  * associated with the clock.
486  *
487  * You need not call an explicit deinit call. Simply make
488  * sure it is not on a list with timer_del.
489  */
490 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
491                                  QEMUTimerCB *cb, void *opaque)
492 {
493     timer_init(ts, type, SCALE_US, cb, opaque);
494 }
495 
496 /**
497  * timer_init_ms:
498  * @ts: the timer to be initialised
499  * @type: the clock to associate with the timer
500  * @cb: the callback to call when the timer expires
501  * @opaque: the opaque pointer to pass to the callback
502  *
503  * Initialize a timer with millisecond scale on the default timer list
504  * associated with the clock.
505  *
506  * You need not call an explicit deinit call. Simply make
507  * sure it is not on a list with timer_del.
508  */
509 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
510                                  QEMUTimerCB *cb, void *opaque)
511 {
512     timer_init(ts, type, SCALE_MS, cb, opaque);
513 }
514 
515 /**
516  * timer_new_tl:
517  * @timer_list: the timer list to attach the timer to
518  * @scale: the scale value for the timer
519  * @cb: the callback to be called when the timer expires
520  * @opaque: the opaque pointer to be passed to the callback
521  *
522  * Create a new timer and associate it with @timer_list.
523  * The memory is allocated by the function.
524  *
525  * This is not the preferred interface unless you know you
526  * are going to call timer_free. Use timer_init instead.
527  *
528  * Returns: a pointer to the timer
529  */
530 static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
531                                       int scale,
532                                       QEMUTimerCB *cb,
533                                       void *opaque)
534 {
535     QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
536     timer_init_tl(ts, timer_list, scale, cb, opaque);
537     return ts;
538 }
539 
540 /**
541  * timer_new:
542  * @type: the clock type to use
543  * @scale: the scale value for the timer
544  * @cb: the callback to be called when the timer expires
545  * @opaque: the opaque pointer to be passed to the callback
546  *
547  * Create a new timer and associate it with the default
548  * timer list for the clock type @type.
549  *
550  * The default timer list has one special feature: in icount mode,
551  * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
552  * not true of other timer lists, which are typically associated
553  * with an AioContext---each of them runs its timer callbacks in its own
554  * AioContext thread.
555  *
556  * Returns: a pointer to the timer
557  */
558 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
559                                    QEMUTimerCB *cb, void *opaque)
560 {
561     return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
562 }
563 
564 /**
565  * timer_new_ns:
566  * @type: the clock type to associate with the timer
567  * @cb: the callback to call when the timer expires
568  * @opaque: the opaque pointer to pass to the callback
569  *
570  * Create a new timer with nanosecond scale on the default timer list
571  * associated with the clock.
572  *
573  * The default timer list has one special feature: in icount mode,
574  * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
575  * not true of other timer lists, which are typically associated
576  * with an AioContext---each of them runs its timer callbacks in its own
577  * AioContext thread.
578  *
579  * Returns: a pointer to the newly created timer
580  */
581 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
582                                       void *opaque)
583 {
584     return timer_new(type, SCALE_NS, cb, opaque);
585 }
586 
587 /**
588  * timer_new_us:
589  * @type: the clock type to associate with the timer
590  * @cb: the callback to call when the timer expires
591  * @opaque: the opaque pointer to pass to the callback
592  *
593  * The default timer list has one special feature: in icount mode,
594  * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
595  * not true of other timer lists, which are typically associated
596  * with an AioContext---each of them runs its timer callbacks in its own
597  * AioContext thread.
598  *
599  * Create a new timer with microsecond scale on the default timer list
600  * associated with the clock.
601  *
602  * Returns: a pointer to the newly created timer
603  */
604 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
605                                       void *opaque)
606 {
607     return timer_new(type, SCALE_US, cb, opaque);
608 }
609 
610 /**
611  * timer_new_ms:
612  * @type: the clock type to associate with the timer
613  * @cb: the callback to call when the timer expires
614  * @opaque: the opaque pointer to pass to the callback
615  *
616  * The default timer list has one special feature: in icount mode,
617  * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread.  This is
618  * not true of other timer lists, which are typically associated
619  * with an AioContext---each of them runs its timer callbacks in its own
620  * AioContext thread.
621  *
622  * Create a new timer with millisecond scale on the default timer list
623  * associated with the clock.
624  *
625  * Returns: a pointer to the newly created timer
626  */
627 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
628                                       void *opaque)
629 {
630     return timer_new(type, SCALE_MS, cb, opaque);
631 }
632 
633 /**
634  * timer_deinit:
635  * @ts: the timer to be de-initialised
636  *
637  * Deassociate the timer from any timerlist.  You should
638  * call timer_del before.  After this call, any further
639  * timer_del call cannot cause dangling pointer accesses
640  * even if the previously used timerlist is freed.
641  */
642 void timer_deinit(QEMUTimer *ts);
643 
644 /**
645  * timer_free:
646  * @ts: the timer
647  *
648  * Free a timer (it must not be on the active list)
649  */
650 static inline void timer_free(QEMUTimer *ts)
651 {
652     g_free(ts);
653 }
654 
655 /**
656  * timer_del:
657  * @ts: the timer
658  *
659  * Delete a timer from the active list.
660  *
661  * This function is thread-safe but the timer and its timer list must not be
662  * freed while this function is running.
663  */
664 void timer_del(QEMUTimer *ts);
665 
666 /**
667  * timer_mod_ns:
668  * @ts: the timer
669  * @expire_time: the expiry time in nanoseconds
670  *
671  * Modify a timer to expire at @expire_time
672  *
673  * This function is thread-safe but the timer and its timer list must not be
674  * freed while this function is running.
675  */
676 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
677 
678 /**
679  * timer_mod_anticipate_ns:
680  * @ts: the timer
681  * @expire_time: the expiry time in nanoseconds
682  *
683  * Modify a timer to expire at @expire_time or the current time,
684  * whichever comes earlier.
685  *
686  * This function is thread-safe but the timer and its timer list must not be
687  * freed while this function is running.
688  */
689 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
690 
691 /**
692  * timer_mod:
693  * @ts: the timer
694  * @expire_time: the expire time in the units associated with the timer
695  *
696  * Modify a timer to expiry at @expire_time, taking into
697  * account the scale associated with the timer.
698  *
699  * This function is thread-safe but the timer and its timer list must not be
700  * freed while this function is running.
701  */
702 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
703 
704 /**
705  * timer_mod_anticipate:
706  * @ts: the timer
707  * @expire_time: the expiry time in nanoseconds
708  *
709  * Modify a timer to expire at @expire_time or the current time, whichever
710  * comes earlier, taking into account the scale associated with the timer.
711  *
712  * This function is thread-safe but the timer and its timer list must not be
713  * freed while this function is running.
714  */
715 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
716 
717 /**
718  * timer_pending:
719  * @ts: the timer
720  *
721  * Determines whether a timer is pending (i.e. is on the
722  * active list of timers, whether or not it has not yet expired).
723  *
724  * Returns: true if the timer is pending
725  */
726 bool timer_pending(QEMUTimer *ts);
727 
728 /**
729  * timer_expired:
730  * @ts: the timer
731  * @current_time: the current time
732  *
733  * Determines whether a timer has expired.
734  *
735  * Returns: true if the timer has expired
736  */
737 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
738 
739 /**
740  * timer_expire_time_ns:
741  * @ts: the timer
742  *
743  * Determine the expiry time of a timer
744  *
745  * Returns: the expiry time in nanoseconds
746  */
747 uint64_t timer_expire_time_ns(QEMUTimer *ts);
748 
749 /**
750  * timer_get:
751  * @f: the file
752  * @ts: the timer
753  *
754  * Read a timer @ts from a file @f
755  */
756 void timer_get(QEMUFile *f, QEMUTimer *ts);
757 
758 /**
759  * timer_put:
760  * @f: the file
761  * @ts: the timer
762  */
763 void timer_put(QEMUFile *f, QEMUTimer *ts);
764 
765 /*
766  * General utility functions
767  */
768 
769 /**
770  * qemu_timeout_ns_to_ms:
771  * @ns: nanosecond timeout value
772  *
773  * Convert a nanosecond timeout value (or -1) to
774  * a millisecond value (or -1), always rounding up.
775  *
776  * Returns: millisecond timeout value
777  */
778 int qemu_timeout_ns_to_ms(int64_t ns);
779 
780 /**
781  * qemu_poll_ns:
782  * @fds: Array of file descriptors
783  * @nfds: number of file descriptors
784  * @timeout: timeout in nanoseconds
785  *
786  * Perform a poll like g_poll but with a timeout in nanoseconds.
787  * See g_poll documentation for further details.
788  *
789  * Returns: number of fds ready
790  */
791 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
792 
793 /**
794  * qemu_soonest_timeout:
795  * @timeout1: first timeout in nanoseconds (or -1 for infinite)
796  * @timeout2: second timeout in nanoseconds (or -1 for infinite)
797  *
798  * Calculates the soonest of two timeout values. -1 means infinite, which
799  * is later than any other value.
800  *
801  * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
802  */
803 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
804 {
805     /* we can abuse the fact that -1 (which means infinite) is a maximal
806      * value when cast to unsigned. As this is disgusting, it's kept in
807      * one inline function.
808      */
809     return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
810 }
811 
812 /**
813  * initclocks:
814  *
815  * Initialise the clock & timer infrastructure
816  */
817 void init_clocks(QEMUTimerListNotifyCB *notify_cb);
818 
819 int64_t cpu_get_ticks(void);
820 /* Caller must hold BQL */
821 void cpu_enable_ticks(void);
822 /* Caller must hold BQL */
823 void cpu_disable_ticks(void);
824 
825 static inline int64_t get_max_clock_jump(void)
826 {
827     /* This should be small enough to prevent excessive interrupts from being
828      * generated by the RTC on clock jumps, but large enough to avoid frequent
829      * unnecessary resets in idle VMs.
830      */
831     return 60 * NANOSECONDS_PER_SECOND;
832 }
833 
834 /*
835  * Low level clock functions
836  */
837 
838 /* get host real time in nanosecond */
839 static inline int64_t get_clock_realtime(void)
840 {
841     struct timeval tv;
842 
843     gettimeofday(&tv, NULL);
844     return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
845 }
846 
847 /* Warning: don't insert tracepoints into these functions, they are
848    also used by simpletrace backend and tracepoints would cause
849    an infinite recursion! */
850 #ifdef _WIN32
851 extern int64_t clock_freq;
852 
853 static inline int64_t get_clock(void)
854 {
855     LARGE_INTEGER ti;
856     QueryPerformanceCounter(&ti);
857     return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
858 }
859 
860 #else
861 
862 extern int use_rt_clock;
863 
864 static inline int64_t get_clock(void)
865 {
866 #ifdef CLOCK_MONOTONIC
867     if (use_rt_clock) {
868         struct timespec ts;
869         clock_gettime(CLOCK_MONOTONIC, &ts);
870         return ts.tv_sec * 1000000000LL + ts.tv_nsec;
871     } else
872 #endif
873     {
874         /* XXX: using gettimeofday leads to problems if the date
875            changes, so it should be avoided. */
876         return get_clock_realtime();
877     }
878 }
879 #endif
880 
881 /* icount */
882 int64_t cpu_get_icount_raw(void);
883 int64_t cpu_get_icount(void);
884 int64_t cpu_get_clock(void);
885 int64_t cpu_icount_to_ns(int64_t icount);
886 void    cpu_update_icount(CPUState *cpu);
887 
888 /*******************************************/
889 /* host CPU ticks (if available) */
890 
891 #if defined(_ARCH_PPC)
892 
893 static inline int64_t cpu_get_host_ticks(void)
894 {
895     int64_t retval;
896 #ifdef _ARCH_PPC64
897     /* This reads timebase in one 64bit go and includes Cell workaround from:
898        http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
899     */
900     __asm__ __volatile__ ("mftb    %0\n\t"
901                           "cmpwi   %0,0\n\t"
902                           "beq-    $-8"
903                           : "=r" (retval));
904 #else
905     /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
906     unsigned long junk;
907     __asm__ __volatile__ ("mfspr   %1,269\n\t"  /* mftbu */
908                           "mfspr   %L0,268\n\t" /* mftb */
909                           "mfspr   %0,269\n\t"  /* mftbu */
910                           "cmpw    %0,%1\n\t"
911                           "bne     $-16"
912                           : "=r" (retval), "=r" (junk));
913 #endif
914     return retval;
915 }
916 
917 #elif defined(__i386__)
918 
919 static inline int64_t cpu_get_host_ticks(void)
920 {
921     int64_t val;
922     asm volatile ("rdtsc" : "=A" (val));
923     return val;
924 }
925 
926 #elif defined(__x86_64__)
927 
928 static inline int64_t cpu_get_host_ticks(void)
929 {
930     uint32_t low,high;
931     int64_t val;
932     asm volatile("rdtsc" : "=a" (low), "=d" (high));
933     val = high;
934     val <<= 32;
935     val |= low;
936     return val;
937 }
938 
939 #elif defined(__hppa__)
940 
941 static inline int64_t cpu_get_host_ticks(void)
942 {
943     int val;
944     asm volatile ("mfctl %%cr16, %0" : "=r"(val));
945     return val;
946 }
947 
948 #elif defined(__s390__)
949 
950 static inline int64_t cpu_get_host_ticks(void)
951 {
952     int64_t val;
953     asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
954     return val;
955 }
956 
957 #elif defined(__sparc__)
958 
959 static inline int64_t cpu_get_host_ticks (void)
960 {
961 #if defined(_LP64)
962     uint64_t        rval;
963     asm volatile("rd %%tick,%0" : "=r"(rval));
964     return rval;
965 #else
966     /* We need an %o or %g register for this.  For recent enough gcc
967        there is an "h" constraint for that.  Don't bother with that.  */
968     union {
969         uint64_t i64;
970         struct {
971             uint32_t high;
972             uint32_t low;
973         }       i32;
974     } rval;
975     asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
976                  : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
977     return rval.i64;
978 #endif
979 }
980 
981 #elif defined(__mips__) && \
982     ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
983 /*
984  * binutils wants to use rdhwr only on mips32r2
985  * but as linux kernel emulate it, it's fine
986  * to use it.
987  *
988  */
989 #define MIPS_RDHWR(rd, value) {                         \
990         __asm__ __volatile__ (".set   push\n\t"         \
991                               ".set mips32r2\n\t"       \
992                               "rdhwr  %0, "rd"\n\t"     \
993                               ".set   pop"              \
994                               : "=r" (value));          \
995     }
996 
997 static inline int64_t cpu_get_host_ticks(void)
998 {
999     /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
1000     uint32_t count;
1001     static uint32_t cyc_per_count = 0;
1002 
1003     if (!cyc_per_count) {
1004         MIPS_RDHWR("$3", cyc_per_count);
1005     }
1006 
1007     MIPS_RDHWR("$2", count);
1008     return (int64_t)(count * cyc_per_count);
1009 }
1010 
1011 #elif defined(__alpha__)
1012 
1013 static inline int64_t cpu_get_host_ticks(void)
1014 {
1015     uint64_t cc;
1016     uint32_t cur, ofs;
1017 
1018     asm volatile("rpcc %0" : "=r"(cc));
1019     cur = cc;
1020     ofs = cc >> 32;
1021     return cur - ofs;
1022 }
1023 
1024 #else
1025 /* The host CPU doesn't have an easily accessible cycle counter.
1026    Just return a monotonically increasing value.  This will be
1027    totally wrong, but hopefully better than nothing.  */
1028 static inline int64_t cpu_get_host_ticks(void)
1029 {
1030     return get_clock();
1031 }
1032 #endif
1033 
1034 #ifdef CONFIG_PROFILER
1035 static inline int64_t profile_getclock(void)
1036 {
1037     return get_clock();
1038 }
1039 
1040 extern int64_t tcg_time;
1041 extern int64_t dev_time;
1042 #endif
1043 
1044 #endif
1045