4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "sysemu/sysemu.h"
26 #include "monitor/monitor.h"
27 #include "ui/console.h"
31 #include "qemu/timer.h"
40 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
41 #include <sys/prctl.h>
44 /***********************************************************/
48 QLIST_HEAD(, QEMUTimerList
) timerlists
;
50 NotifierList reset_notifiers
;
57 QEMUTimerListGroup main_loop_tlg
;
58 QEMUClock qemu_clocks
[QEMU_CLOCK_MAX
];
60 /* A QEMUTimerList is a list of timers attached to a clock. More
61 * than one QEMUTimerList can be attached to each clock, for instance
62 * used by different AioContexts / threads. Each clock also has
63 * a list of the QEMUTimerLists associated with it, in order that
64 * reenabling the clock can call all the notifiers.
67 struct QEMUTimerList
{
69 QEMUTimer
*active_timers
;
70 QLIST_ENTRY(QEMUTimerList
) list
;
71 QEMUTimerListNotifyCB
*notify_cb
;
77 * @type: type of clock
79 * Translate a clock type into a pointer to QEMUClock object.
81 * Returns: a pointer to the QEMUClock object
83 QEMUClock
*qemu_clock_ptr(QEMUClockType type
)
85 return &qemu_clocks
[type
];
88 static bool timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
90 return timer_head
&& (timer_head
->expire_time
<= current_time
);
93 QEMUTimerList
*timerlist_new(QEMUClockType type
,
94 QEMUTimerListNotifyCB
*cb
,
97 QEMUTimerList
*timer_list
;
98 QEMUClock
*clock
= qemu_clock_ptr(type
);
100 timer_list
= g_malloc0(sizeof(QEMUTimerList
));
101 timer_list
->clock
= clock
;
102 timer_list
->notify_cb
= cb
;
103 timer_list
->notify_opaque
= opaque
;
104 QLIST_INSERT_HEAD(&clock
->timerlists
, timer_list
, list
);
108 void timerlist_free(QEMUTimerList
*timer_list
)
110 assert(!timerlist_has_timers(timer_list
));
111 if (timer_list
->clock
) {
112 QLIST_REMOVE(timer_list
, list
);
117 static void qemu_clock_init(QEMUClockType type
)
119 QEMUClock
*clock
= qemu_clock_ptr(type
);
122 clock
->enabled
= true;
123 clock
->last
= INT64_MIN
;
124 QLIST_INIT(&clock
->timerlists
);
125 notifier_list_init(&clock
->reset_notifiers
);
126 main_loop_tlg
.tl
[type
] = timerlist_new(type
, NULL
, NULL
);
129 bool qemu_clock_use_for_deadline(QEMUClockType type
)
131 return !(use_icount
&& (type
== QEMU_CLOCK_VIRTUAL
));
134 void qemu_clock_notify(QEMUClockType type
)
136 QEMUTimerList
*timer_list
;
137 QEMUClock
*clock
= qemu_clock_ptr(type
);
138 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
139 timerlist_notify(timer_list
);
143 void qemu_clock_enable(QEMUClockType type
, bool enabled
)
145 QEMUClock
*clock
= qemu_clock_ptr(type
);
146 bool old
= clock
->enabled
;
147 clock
->enabled
= enabled
;
148 if (enabled
&& !old
) {
149 qemu_clock_notify(type
);
153 bool timerlist_has_timers(QEMUTimerList
*timer_list
)
155 return !!timer_list
->active_timers
;
158 bool qemu_clock_has_timers(QEMUClockType type
)
160 return timerlist_has_timers(
161 main_loop_tlg
.tl
[type
]);
164 bool timerlist_expired(QEMUTimerList
*timer_list
)
166 return (timer_list
->active_timers
&&
167 timer_list
->active_timers
->expire_time
<
168 qemu_clock_get_ns(timer_list
->clock
->type
));
171 bool qemu_clock_expired(QEMUClockType type
)
173 return timerlist_expired(
174 main_loop_tlg
.tl
[type
]);
178 * As above, but return -1 for no deadline, and do not cap to 2^32
179 * as we know the result is always positive.
182 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
)
186 if (!timer_list
->clock
->enabled
|| !timer_list
->active_timers
) {
190 delta
= timer_list
->active_timers
->expire_time
-
191 qemu_clock_get_ns(timer_list
->clock
->type
);
200 /* Calculate the soonest deadline across all timerlists attached
201 * to the clock. This is used for the icount timeout so we
202 * ignore whether or not the clock should be used in deadline
205 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
)
207 int64_t deadline
= -1;
208 QEMUTimerList
*timer_list
;
209 QEMUClock
*clock
= qemu_clock_ptr(type
);
210 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
211 deadline
= qemu_soonest_timeout(deadline
,
212 timerlist_deadline_ns(timer_list
));
217 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
)
219 return timer_list
->clock
->type
;
222 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
)
224 return main_loop_tlg
.tl
[type
];
227 void timerlist_notify(QEMUTimerList
*timer_list
)
229 if (timer_list
->notify_cb
) {
230 timer_list
->notify_cb(timer_list
->notify_opaque
);
236 /* Transition function to convert a nanosecond timeout to ms
237 * This is used where a system does not support ppoll
239 int qemu_timeout_ns_to_ms(int64_t ns
)
250 /* Always round up, because it's better to wait too long than to wait too
251 * little and effectively busy-wait
253 ms
= (ns
+ SCALE_MS
- 1) / SCALE_MS
;
255 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
256 if (ms
> (int64_t) INT32_MAX
) {
264 /* qemu implementation of g_poll which uses a nanosecond timeout but is
265 * otherwise identical to g_poll
267 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
)
271 return ppoll((struct pollfd
*)fds
, nfds
, NULL
, NULL
);
274 ts
.tv_sec
= timeout
/ 1000000000LL;
275 ts
.tv_nsec
= timeout
% 1000000000LL;
276 return ppoll((struct pollfd
*)fds
, nfds
, &ts
, NULL
);
279 return g_poll(fds
, nfds
, qemu_timeout_ns_to_ms(timeout
));
284 void timer_init(QEMUTimer
*ts
,
285 QEMUTimerList
*timer_list
, int scale
,
286 QEMUTimerCB
*cb
, void *opaque
)
288 ts
->timer_list
= timer_list
;
294 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
295 QEMUTimerCB
*cb
, void *opaque
)
297 return timer_new_tl(main_loop_tlg
.tl
[clock
->type
],
301 void timer_free(QEMUTimer
*ts
)
306 /* stop a timer, but do not dealloc it */
307 void timer_del(QEMUTimer
*ts
)
311 /* NOTE: this code must be signal safe because
312 timer_expired() can be called from a signal. */
313 pt
= &ts
->timer_list
->active_timers
;
326 /* modify the current timer so that it will be fired when current_time
327 >= expire_time. The corresponding callback will be called. */
328 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
)
334 /* add the timer in the sorted list */
335 /* NOTE: this code must be signal safe because
336 timer_expired() can be called from a signal. */
337 pt
= &ts
->timer_list
->active_timers
;
340 if (!timer_expired_ns(t
, expire_time
)) {
345 ts
->expire_time
= expire_time
;
349 /* Rearm if necessary */
350 if (pt
== &ts
->timer_list
->active_timers
) {
351 /* Interrupt execution to force deadline recalculation. */
352 qemu_clock_warp(ts
->timer_list
->clock
->type
);
353 timerlist_notify(ts
->timer_list
);
357 void timer_mod(QEMUTimer
*ts
, int64_t expire_time
)
359 timer_mod_ns(ts
, expire_time
* ts
->scale
);
362 bool timer_pending(QEMUTimer
*ts
)
365 for (t
= ts
->timer_list
->active_timers
; t
!= NULL
; t
= t
->next
) {
373 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
375 return timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
378 bool timerlist_run_timers(QEMUTimerList
*timer_list
)
381 int64_t current_time
;
382 bool progress
= false;
384 if (!timer_list
->clock
->enabled
) {
388 current_time
= qemu_clock_get_ns(timer_list
->clock
->type
);
390 ts
= timer_list
->active_timers
;
391 if (!timer_expired_ns(ts
, current_time
)) {
394 /* remove timer from the list before calling the callback */
395 timer_list
->active_timers
= ts
->next
;
398 /* run the callback (the timer list can be modified) */
405 bool qemu_clock_run_timers(QEMUClockType type
)
407 return timerlist_run_timers(main_loop_tlg
.tl
[type
]);
410 bool qemu_run_timers(QEMUClock
*clock
)
412 return qemu_clock_run_timers(clock
->type
);
415 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
416 QEMUTimerListNotifyCB
*cb
, void *opaque
)
419 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
420 tlg
->tl
[type
] = timerlist_new(type
, cb
, opaque
);
424 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
)
427 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
428 timerlist_free(tlg
->tl
[type
]);
432 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
)
435 bool progress
= false;
436 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
437 progress
|= timerlist_run_timers(tlg
->tl
[type
]);
442 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
)
444 int64_t deadline
= -1;
446 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
447 if (qemu_clock_use_for_deadline(tlg
->tl
[type
]->clock
->type
)) {
448 deadline
= qemu_soonest_timeout(deadline
,
449 timerlist_deadline_ns(
456 int64_t qemu_clock_get_ns(QEMUClockType type
)
459 QEMUClock
*clock
= qemu_clock_ptr(type
);
462 case QEMU_CLOCK_REALTIME
:
465 case QEMU_CLOCK_VIRTUAL
:
467 return cpu_get_icount();
469 return cpu_get_clock();
471 case QEMU_CLOCK_HOST
:
472 now
= get_clock_realtime();
476 notifier_list_notify(&clock
->reset_notifiers
, &now
);
482 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
484 return qemu_clock_get_ns(clock
->type
);
487 void qemu_clock_register_reset_notifier(QEMUClockType type
,
490 QEMUClock
*clock
= qemu_clock_ptr(type
);
491 notifier_list_add(&clock
->reset_notifiers
, notifier
);
494 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
497 notifier_remove(notifier
);
500 void qemu_register_clock_reset_notifier(QEMUClock
*clock
,
503 qemu_clock_register_reset_notifier(clock
->type
, notifier
);
506 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
,
509 qemu_clock_unregister_reset_notifier(clock
->type
, notifier
);
512 void init_clocks(void)
515 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
516 qemu_clock_init(type
);
519 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
520 prctl(PR_SET_TIMERSLACK
, 1, 0, 0, 0);
524 uint64_t timer_expire_time_ns(QEMUTimer
*ts
)
526 return timer_pending(ts
) ? ts
->expire_time
: -1;
529 bool qemu_clock_run_all_timers(void)
531 bool progress
= false;
534 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
535 progress
|= qemu_clock_run_timers(type
);