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