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