]> git.proxmox.com Git - mirror_qemu.git/blame - include/qemu/timer.h
Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2018-12-17' into...
[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"
89a603a0 5#include "qemu/bitops.h"
1de7afc9 6#include "qemu/notify.h"
49caffe0 7#include "qemu/host-utils.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
89a603a0
AP
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 *
e81f8679
AP
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.
89a603a0
AP
70 */
71
e81f8679
AP
72#define QEMU_TIMER_ATTR_EXTERNAL BIT(0)
73
ff83c66e 74typedef struct QEMUTimerList QEMUTimerList;
754d6a54
AB
75
76struct QEMUTimerListGroup {
77 QEMUTimerList *tl[QEMU_CLOCK_MAX];
78};
79
87ecb68b 80typedef void QEMUTimerCB(void *opaque);
3f53bc61 81typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
87ecb68b 82
ff83c66e
AB
83struct QEMUTimer {
84 int64_t expire_time; /* in nanoseconds */
85 QEMUTimerList *timer_list;
86 QEMUTimerCB *cb;
87 void *opaque;
88 QEMUTimer *next;
89a603a0 89 int attributes;
ff83c66e
AB
90 int scale;
91};
92
754d6a54 93extern QEMUTimerListGroup main_loop_tlg;
87ecb68b 94
b4049b74 95/*
54904d2a
AB
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 */
40daca54 104int64_t qemu_clock_get_ns(QEMUClockType type);
54904d2a 105
55a197da
AB
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 */
115static 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 */
129static inline int64_t qemu_clock_get_us(QEMUClockType type)
130{
131 return qemu_clock_get_ns(type) / SCALE_US;
132}
133
54904d2a
AB
134/**
135 * qemu_clock_has_timers:
40daca54 136 * @type: the clock type
54904d2a
AB
137 *
138 * Determines whether a clock's default timer list
139 * has timers attached
140 *
978f2205
SH
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 *
54904d2a
AB
145 * Returns: true if the clock's default timer list
146 * has timers attached
147 */
40daca54 148bool qemu_clock_has_timers(QEMUClockType type);
54904d2a
AB
149
150/**
151 * qemu_clock_expired:
40daca54 152 * @type: the clock type
54904d2a
AB
153 *
154 * Determines whether a clock's default timer list
45241cf9 155 * has an expired timer.
54904d2a
AB
156 *
157 * Returns: true if the clock's default timer list has
158 * an expired timer
159 */
40daca54 160bool qemu_clock_expired(QEMUClockType type);
02a03a9f 161
ff83c66e
AB
162/**
163 * qemu_clock_use_for_deadline:
40daca54 164 * @type: the clock type
ff83c66e
AB
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 */
40daca54 176bool qemu_clock_use_for_deadline(QEMUClockType type);
ff83c66e 177
ac70aafc 178/**
40daca54
AB
179 * qemu_clock_deadline_ns_all:
180 * @type: the clock type
ac70aafc
AB
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 */
40daca54 188int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
ac70aafc 189
ff83c66e
AB
190/**
191 * qemu_clock_get_main_loop_timerlist:
40daca54 192 * @type: the clock type
ff83c66e 193 *
083b96e2 194 * Return the default timer list associated with a clock.
ff83c66e
AB
195 *
196 * Returns: the default timer list
197 */
40daca54 198QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
ff83c66e 199
b1bbfe72
AB
200/**
201 * qemu_clock_nofify:
40daca54 202 * @type: the clock type
b1bbfe72
AB
203 *
204 * Call the notifier callback connected with the default timer
205 * list linked to the clock, or qemu_notify() if none.
206 */
40daca54
AB
207void 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
3c053411
LPF
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.
40daca54
AB
221 */
222void qemu_clock_enable(QEMUClockType type, bool enabled);
223
224/**
e76d1798 225 * qemu_start_warp_timer:
40daca54 226 *
e76d1798 227 * Starts a timer for virtual clock update
40daca54 228 */
e76d1798 229void qemu_start_warp_timer(void);
40daca54
AB
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 */
239void 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 */
250void 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 */
262bool 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 */
272bool qemu_clock_run_all_timers(void);
273
4b930d26
PD
274/**
275 * qemu_clock_get_last:
276 *
277 * Returns last clock query time.
278 */
279uint64_t qemu_clock_get_last(QEMUClockType type);
280/**
281 * qemu_clock_set_last:
282 *
283 * Sets last clock query time.
284 */
285void qemu_clock_set_last(QEMUClockType type, uint64_t last);
286
287
40daca54
AB
288/*
289 * QEMUTimerList
290 */
b1bbfe72 291
ff83c66e
AB
292/**
293 * timerlist_new:
294 * @type: the clock type to associate with the timerlist
d5541d86
AB
295 * @cb: the callback to call on notification
296 * @opaque: the opaque pointer to pass to the callback
ff83c66e
AB
297 *
298 * Create a new timerlist associated with the clock of
299 * type @type.
300 *
301 * Returns: a pointer to the QEMUTimerList created
302 */
d5541d86
AB
303QEMUTimerList *timerlist_new(QEMUClockType type,
304 QEMUTimerListNotifyCB *cb, void *opaque);
ff83c66e
AB
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 */
312void 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 *
978f2205
SH
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 *
ff83c66e
AB
324 * Returns: true if the timer list has timers.
325 */
326bool 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 */
338bool timerlist_expired(QEMUTimerList *timer_list);
ff83c66e
AB
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 */
351int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
352
353/**
40daca54 354 * timerlist_get_clock:
ff83c66e
AB
355 * @timer_list: the timer list to operate on
356 *
40daca54 357 * Determine the clock type associated with a timer list.
ff83c66e 358 *
40daca54
AB
359 * Returns: the clock type associated with the
360 * timer list.
ff83c66e 361 */
40daca54 362QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
ff83c66e
AB
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 */
372bool timerlist_run_timers(QEMUTimerList *timer_list);
373
d5541d86
AB
374/**
375 * timerlist_notify:
376 * @timer_list: the timer list to use
377 *
378 * call the notifier callback associated with the timer list.
379 */
380void timerlist_notify(QEMUTimerList *timer_list);
381
40daca54
AB
382/*
383 * QEMUTimerListGroup
384 */
385
754d6a54
AB
386/**
387 * timerlistgroup_init:
388 * @tlg: the timer list group
d5541d86
AB
389 * @cb: the callback to call when a notify is required
390 * @opaque: the opaque pointer to be passed to the callback.
754d6a54
AB
391 *
392 * Initialise a timer list group. This must already be
d5541d86
AB
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.
754d6a54 398 */
d5541d86
AB
399void timerlistgroup_init(QEMUTimerListGroup *tlg,
400 QEMUTimerListNotifyCB *cb, void *opaque);
754d6a54
AB
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 */
409void 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 */
420bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
421
422/**
54904d2a 423 * timerlistgroup_deadline_ns:
754d6a54
AB
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 */
434int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
435
40daca54
AB
436/*
437 * QEMUTimer
54904d2a 438 */
ff83c66e
AB
439
440/**
89a603a0 441 * timer_init_full:
ff83c66e 442 * @ts: the timer to be initialised
89a603a0
AP
443 * @timer_list_group: (optional) the timer list group to attach the timer to
444 * @type: the clock type to use
dc9fc1ca 445 * @scale: the scale value for the timer
89a603a0 446 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
ff83c66e
AB
447 * @cb: the callback to be called when the timer expires
448 * @opaque: the opaque pointer to be passed to the callback
449 *
89a603a0
AP
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).
ff83c66e
AB
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 */
89a603a0
AP
458void timer_init_full(QEMUTimer *ts,
459 QEMUTimerListGroup *timer_list_group, QEMUClockType type,
460 int scale, int attributes,
461 QEMUTimerCB *cb, void *opaque);
ff83c66e 462
65a81af8
PB
463/**
464 * timer_init:
04ecbb78 465 * @ts: the timer to be initialised
65a81af8
PB
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.
89a603a0 473 * See timer_init_full for details.
65a81af8
PB
474 */
475static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
476 QEMUTimerCB *cb, void *opaque)
477{
89a603a0 478 timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
65a81af8
PB
479}
480
481/**
482 * timer_init_ns:
04ecbb78 483 * @ts: the timer to be initialised
65a81af8
PB
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.
89a603a0 490 * See timer_init_full for details.
65a81af8
PB
491 */
492static 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:
04ecbb78 500 * @ts: the timer to be initialised
65a81af8
PB
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.
89a603a0 507 * See timer_init_full for details.
65a81af8
PB
508 */
509static 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:
04ecbb78 517 * @ts: the timer to be initialised
65a81af8
PB
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.
89a603a0 524 * See timer_init_full for details.
65a81af8
PB
525 */
526static 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
ff83c66e 532/**
89a603a0
AP
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
dc9fc1ca 536 * @scale: the scale value for the timer
89a603a0 537 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
ff83c66e
AB
538 * @cb: the callback to be called when the timer expires
539 * @opaque: the opaque pointer to be passed to the callback
540 *
89a603a0
AP
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).
ff83c66e
AB
544 * The memory is allocated by the function.
545 *
546 * This is not the preferred interface unless you know you
89a603a0
AP
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.
ff83c66e
AB
554 *
555 * Returns: a pointer to the timer
556 */
89a603a0
AP
557static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
558 QEMUClockType type,
559 int scale, int attributes,
560 QEMUTimerCB *cb, void *opaque)
ff83c66e
AB
561{
562 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
89a603a0 563 timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
ff83c66e
AB
564 return ts;
565}
566
a3a726ae
AB
567/**
568 * timer_new:
569 * @type: the clock type to use
dc9fc1ca 570 * @scale: the scale value for the timer
a3a726ae
AB
571 * @cb: the callback to be called when the timer expires
572 * @opaque: the opaque pointer to be passed to the callback
573 *
89a603a0
AP
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.
6b8f0187 577 *
a3a726ae
AB
578 * Returns: a pointer to the timer
579 */
580static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
581 QEMUTimerCB *cb, void *opaque)
582{
89a603a0 583 return timer_new_full(NULL, type, scale, 0, cb, opaque);
a3a726ae
AB
584}
585
54904d2a 586/**
40daca54 587 * timer_new_ns:
04ecbb78
C
588 * @type: the clock type to associate with the timer
589 * @cb: the callback to call when the timer expires
40daca54 590 * @opaque: the opaque pointer to pass to the callback
54904d2a 591 *
40daca54
AB
592 * Create a new timer with nanosecond scale on the default timer list
593 * associated with the clock.
89a603a0 594 * See timer_new_full for details.
6b8f0187 595 *
40daca54 596 * Returns: a pointer to the newly created timer
ff83c66e 597 */
40daca54
AB
598static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
599 void *opaque)
ff83c66e 600{
40daca54 601 return timer_new(type, SCALE_NS, cb, opaque);
ff83c66e
AB
602}
603
54904d2a 604/**
40daca54 605 * timer_new_us:
04ecbb78
C
606 * @type: the clock type to associate with the timer
607 * @cb: the callback to call when the timer expires
40daca54 608 * @opaque: the opaque pointer to pass to the callback
54904d2a 609 *
40daca54
AB
610 * Create a new timer with microsecond scale on the default timer list
611 * associated with the clock.
89a603a0 612 * See timer_new_full for details.
54904d2a 613 *
40daca54 614 * Returns: a pointer to the newly created timer
54904d2a 615 */
40daca54
AB
616static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
617 void *opaque)
618{
619 return timer_new(type, SCALE_US, cb, opaque);
620}
54904d2a 621
ff83c66e 622/**
40daca54 623 * timer_new_ms:
04ecbb78
C
624 * @type: the clock type to associate with the timer
625 * @cb: the callback to call when the timer expires
40daca54 626 * @opaque: the opaque pointer to pass to the callback
ff83c66e 627 *
40daca54
AB
628 * Create a new timer with millisecond scale on the default timer list
629 * associated with the clock.
89a603a0 630 * See timer_new_full for details.
40daca54
AB
631 *
632 * Returns: a pointer to the newly created timer
ff83c66e 633 */
40daca54
AB
634static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
635 void *opaque)
ff83c66e 636{
40daca54 637 return timer_new(type, SCALE_MS, cb, opaque);
ff83c66e
AB
638}
639
cd1bd53a
PB
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 */
649void timer_deinit(QEMUTimer *ts);
650
54904d2a 651/**
40daca54
AB
652 * timer_free:
653 * @ts: the timer
54904d2a 654 *
40daca54 655 * Free a timer (it must not be on the active list)
54904d2a 656 */
e703dcba
MAL
657static inline void timer_free(QEMUTimer *ts)
658{
659 g_free(ts);
660}
54904d2a 661
ff83c66e 662/**
40daca54
AB
663 * timer_del:
664 * @ts: the timer
ff83c66e 665 *
40daca54 666 * Delete a timer from the active list.
978f2205
SH
667 *
668 * This function is thread-safe but the timer and its timer list must not be
669 * freed while this function is running.
ff83c66e 670 */
40daca54 671void timer_del(QEMUTimer *ts);
ff83c66e 672
54904d2a 673/**
40daca54
AB
674 * timer_mod_ns:
675 * @ts: the timer
676 * @expire_time: the expiry time in nanoseconds
54904d2a 677 *
40daca54 678 * Modify a timer to expire at @expire_time
978f2205
SH
679 *
680 * This function is thread-safe but the timer and its timer list must not be
681 * freed while this function is running.
54904d2a 682 */
40daca54 683void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
54904d2a 684
add40e97
PB
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 */
696void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
697
ff83c66e
AB
698/**
699 * timer_mod:
40daca54
AB
700 * @ts: the timer
701 * @expire_time: the expire time in the units associated with the timer
ff83c66e 702 *
40daca54
AB
703 * Modify a timer to expiry at @expire_time, taking into
704 * account the scale associated with the timer.
978f2205
SH
705 *
706 * This function is thread-safe but the timer and its timer list must not be
707 * freed while this function is running.
ff83c66e 708 */
40daca54 709void timer_mod(QEMUTimer *ts, int64_t expire_timer);
ff83c66e 710
add40e97
PB
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 */
722void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
723
54904d2a
AB
724/**
725 * timer_pending:
40daca54 726 * @ts: the timer
54904d2a
AB
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 */
733bool timer_pending(QEMUTimer *ts);
734
735/**
736 * timer_expired:
40daca54 737 * @ts: the timer
04ecbb78 738 * @current_time: the current time
54904d2a
AB
739 *
740 * Determines whether a timer has expired.
741 *
742 * Returns: true if the timer has expired
743 */
744bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
745
746/**
747 * timer_expire_time_ns:
40daca54 748 * @ts: the timer
54904d2a 749 *
40daca54 750 * Determine the expiry time of a timer
54904d2a 751 *
40daca54 752 * Returns: the expiry time in nanoseconds
54904d2a
AB
753 */
754uint64_t timer_expire_time_ns(QEMUTimer *ts);
755
f9a976b7 756/**
40daca54
AB
757 * timer_get:
758 * @f: the file
759 * @ts: the timer
f9a976b7 760 *
40daca54 761 * Read a timer @ts from a file @f
f9a976b7 762 */
40daca54 763void timer_get(QEMUFile *f, QEMUTimer *ts);
f9a976b7
AB
764
765/**
40daca54
AB
766 * timer_put:
767 * @f: the file
768 * @ts: the timer
769 */
770void 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
f9a976b7 779 *
40daca54
AB
780 * Convert a nanosecond timeout value (or -1) to
781 * a millisecond value (or -1), always rounding up.
f9a976b7 782 *
40daca54 783 * Returns: millisecond timeout value
f9a976b7 784 */
40daca54 785int qemu_timeout_ns_to_ms(int64_t ns);
f9a976b7 786
54904d2a 787/**
40daca54
AB
788 * qemu_poll_ns:
789 * @fds: Array of file descriptors
790 * @nfds: number of file descriptors
791 * @timeout: timeout in nanoseconds
54904d2a 792 *
40daca54
AB
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
54904d2a 797 */
40daca54 798int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
70c3b557 799
02a03a9f
AB
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 */
810static 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
ff83c66e 819/**
40daca54 820 * initclocks:
ff83c66e 821 *
40daca54
AB
822 * Initialise the clock & timer infrastructure
823 */
3f53bc61 824void init_clocks(QEMUTimerListNotifyCB *notify_cb);
40daca54
AB
825
826int64_t cpu_get_ticks(void);
cb365646 827/* Caller must hold BQL */
40daca54 828void cpu_enable_ticks(void);
cb365646 829/* Caller must hold BQL */
40daca54
AB
830void cpu_disable_ticks(void);
831
fb1a3a05
PD
832static 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 */
73bcb24d 838 return 60 * NANOSECONDS_PER_SECOND;
fb1a3a05
PD
839}
840
40daca54
AB
841/*
842 * Low level clock functions
843 */
87ecb68b 844
3224e878 845/* get host real time in nanosecond */
c57c846a
BS
846static 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
858extern int64_t clock_freq;
859
860static inline int64_t get_clock(void)
861{
862 LARGE_INTEGER ti;
863 QueryPerformanceCounter(&ti);
73bcb24d 864 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
c57c846a
BS
865}
866
867#else
868
869extern int use_rt_clock;
870
871static inline int64_t get_clock(void)
872{
d05ef160 873#ifdef CLOCK_MONOTONIC
c57c846a
BS
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
db1a4972 887
29e922b6 888/* icount */
2a62914b 889int64_t cpu_get_icount_raw(void);
29e922b6 890int64_t cpu_get_icount(void);
946fb27c 891int64_t cpu_get_clock(void);
3f031313 892int64_t cpu_icount_to_ns(int64_t icount);
512d3c80 893void cpu_update_icount(CPUState *cpu);
29e922b6
BS
894
895/*******************************************/
896/* host CPU ticks (if available) */
897
898#if defined(_ARCH_PPC)
899
4a7428c5 900static inline int64_t cpu_get_host_ticks(void)
29e922b6
BS
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;
4a9590f3
AG
914 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
915 "mfspr %L0,268\n\t" /* mftb */
916 "mfspr %0,269\n\t" /* mftbu */
29e922b6
BS
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
4a7428c5 926static inline int64_t cpu_get_host_ticks(void)
29e922b6
BS
927{
928 int64_t val;
929 asm volatile ("rdtsc" : "=A" (val));
930 return val;
931}
932
933#elif defined(__x86_64__)
934
4a7428c5 935static inline int64_t cpu_get_host_ticks(void)
29e922b6
BS
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
4a7428c5 948static inline int64_t cpu_get_host_ticks(void)
29e922b6
BS
949{
950 int val;
951 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
952 return val;
953}
954
29e922b6
BS
955#elif defined(__s390__)
956
4a7428c5 957static inline int64_t cpu_get_host_ticks(void)
29e922b6
BS
958{
959 int64_t val;
960 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
961 return val;
962}
963
9b9c37c3 964#elif defined(__sparc__)
29e922b6 965
4a7428c5 966static inline int64_t cpu_get_host_ticks (void)
29e922b6
BS
967{
968#if defined(_LP64)
969 uint64_t rval;
970 asm volatile("rd %%tick,%0" : "=r"(rval));
971 return rval;
972#else
9b9c37c3
RH
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. */
29e922b6
BS
975 union {
976 uint64_t i64;
977 struct {
978 uint32_t high;
979 uint32_t low;
980 } i32;
981 } rval;
9b9c37c3
RH
982 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
983 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
29e922b6
BS
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
4a7428c5 1004static inline int64_t cpu_get_host_ticks(void)
29e922b6
BS
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
14a6063a
RH
1018#elif defined(__alpha__)
1019
4a7428c5 1020static inline int64_t cpu_get_host_ticks(void)
14a6063a
RH
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
29e922b6
BS
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. */
d1bb099f 1035static inline int64_t cpu_get_host_ticks(void)
29e922b6 1036{
d1bb099f 1037 return get_clock();
29e922b6
BS
1038}
1039#endif
1040
2d8ebcf9
RH
1041#ifdef CONFIG_PROFILER
1042static inline int64_t profile_getclock(void)
1043{
89d5cbdd 1044 return get_clock();
2d8ebcf9
RH
1045}
1046
2d8ebcf9
RH
1047extern int64_t dev_time;
1048#endif
1049
87ecb68b 1050#endif