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