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