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 "qemu/main-loop.h"
26 #include "qemu/timer.h"
27 #include "sysemu/replay.h"
28 #include "sysemu/sysemu.h"
38 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
39 #include <sys/prctl.h>
42 /***********************************************************/
45 typedef struct QEMUClock
{
46 /* We rely on BQL to protect the timerlists */
47 QLIST_HEAD(, QEMUTimerList
) timerlists
;
49 NotifierList reset_notifiers
;
56 QEMUTimerListGroup main_loop_tlg
;
57 static QEMUClock qemu_clocks
[QEMU_CLOCK_MAX
];
59 /* A QEMUTimerList is a list of timers attached to a clock. More
60 * than one QEMUTimerList can be attached to each clock, for instance
61 * used by different AioContexts / threads. Each clock also has
62 * a list of the QEMUTimerLists associated with it, in order that
63 * reenabling the clock can call all the notifiers.
66 struct QEMUTimerList
{
68 QemuMutex active_timers_lock
;
69 QEMUTimer
*active_timers
;
70 QLIST_ENTRY(QEMUTimerList
) list
;
71 QEMUTimerListNotifyCB
*notify_cb
;
74 /* lightweight method to mark the end of timerlist's running */
75 QemuEvent timers_done_ev
;
80 * @type: type of clock
82 * Translate a clock type into a pointer to QEMUClock object.
84 * Returns: a pointer to the QEMUClock object
86 static inline QEMUClock
*qemu_clock_ptr(QEMUClockType type
)
88 return &qemu_clocks
[type
];
91 static bool timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
93 return timer_head
&& (timer_head
->expire_time
<= current_time
);
96 QEMUTimerList
*timerlist_new(QEMUClockType type
,
97 QEMUTimerListNotifyCB
*cb
,
100 QEMUTimerList
*timer_list
;
101 QEMUClock
*clock
= qemu_clock_ptr(type
);
103 timer_list
= g_malloc0(sizeof(QEMUTimerList
));
104 qemu_event_init(&timer_list
->timers_done_ev
, true);
105 timer_list
->clock
= clock
;
106 timer_list
->notify_cb
= cb
;
107 timer_list
->notify_opaque
= opaque
;
108 qemu_mutex_init(&timer_list
->active_timers_lock
);
109 QLIST_INSERT_HEAD(&clock
->timerlists
, timer_list
, list
);
113 void timerlist_free(QEMUTimerList
*timer_list
)
115 assert(!timerlist_has_timers(timer_list
));
116 if (timer_list
->clock
) {
117 QLIST_REMOVE(timer_list
, list
);
119 qemu_mutex_destroy(&timer_list
->active_timers_lock
);
123 static void qemu_clock_init(QEMUClockType type
)
125 QEMUClock
*clock
= qemu_clock_ptr(type
);
127 /* Assert that the clock of type TYPE has not been initialized yet. */
128 assert(main_loop_tlg
.tl
[type
] == NULL
);
131 clock
->enabled
= true;
132 clock
->last
= INT64_MIN
;
133 QLIST_INIT(&clock
->timerlists
);
134 notifier_list_init(&clock
->reset_notifiers
);
135 main_loop_tlg
.tl
[type
] = timerlist_new(type
, NULL
, NULL
);
138 bool qemu_clock_use_for_deadline(QEMUClockType type
)
140 return !(use_icount
&& (type
== QEMU_CLOCK_VIRTUAL
));
143 void qemu_clock_notify(QEMUClockType type
)
145 QEMUTimerList
*timer_list
;
146 QEMUClock
*clock
= qemu_clock_ptr(type
);
147 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
148 timerlist_notify(timer_list
);
152 /* Disabling the clock will wait for related timerlists to stop
153 * executing qemu_run_timers. Thus, this functions should not
154 * be used from the callback of a timer that is based on @clock.
155 * Doing so would cause a deadlock.
157 * Caller should hold BQL.
159 void qemu_clock_enable(QEMUClockType type
, bool enabled
)
161 QEMUClock
*clock
= qemu_clock_ptr(type
);
163 bool old
= clock
->enabled
;
164 clock
->enabled
= enabled
;
165 if (enabled
&& !old
) {
166 qemu_clock_notify(type
);
167 } else if (!enabled
&& old
) {
168 QLIST_FOREACH(tl
, &clock
->timerlists
, list
) {
169 qemu_event_wait(&tl
->timers_done_ev
);
174 bool timerlist_has_timers(QEMUTimerList
*timer_list
)
176 return !!timer_list
->active_timers
;
179 bool qemu_clock_has_timers(QEMUClockType type
)
181 return timerlist_has_timers(
182 main_loop_tlg
.tl
[type
]);
185 bool timerlist_expired(QEMUTimerList
*timer_list
)
189 qemu_mutex_lock(&timer_list
->active_timers_lock
);
190 if (!timer_list
->active_timers
) {
191 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
194 expire_time
= timer_list
->active_timers
->expire_time
;
195 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
197 return expire_time
< qemu_clock_get_ns(timer_list
->clock
->type
);
200 bool qemu_clock_expired(QEMUClockType type
)
202 return timerlist_expired(
203 main_loop_tlg
.tl
[type
]);
207 * As above, but return -1 for no deadline, and do not cap to 2^32
208 * as we know the result is always positive.
211 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
)
216 if (!timer_list
->clock
->enabled
) {
220 /* The active timers list may be modified before the caller uses our return
221 * value but ->notify_cb() is called when the deadline changes. Therefore
222 * the caller should notice the change and there is no race condition.
224 qemu_mutex_lock(&timer_list
->active_timers_lock
);
225 if (!timer_list
->active_timers
) {
226 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
229 expire_time
= timer_list
->active_timers
->expire_time
;
230 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
232 delta
= expire_time
- qemu_clock_get_ns(timer_list
->clock
->type
);
241 /* Calculate the soonest deadline across all timerlists attached
242 * to the clock. This is used for the icount timeout so we
243 * ignore whether or not the clock should be used in deadline
246 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
)
248 int64_t deadline
= -1;
249 QEMUTimerList
*timer_list
;
250 QEMUClock
*clock
= qemu_clock_ptr(type
);
251 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
252 deadline
= qemu_soonest_timeout(deadline
,
253 timerlist_deadline_ns(timer_list
));
258 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
)
260 return timer_list
->clock
->type
;
263 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
)
265 return main_loop_tlg
.tl
[type
];
268 void timerlist_notify(QEMUTimerList
*timer_list
)
270 if (timer_list
->notify_cb
) {
271 timer_list
->notify_cb(timer_list
->notify_opaque
);
277 /* Transition function to convert a nanosecond timeout to ms
278 * This is used where a system does not support ppoll
280 int qemu_timeout_ns_to_ms(int64_t ns
)
291 /* Always round up, because it's better to wait too long than to wait too
292 * little and effectively busy-wait
294 ms
= (ns
+ SCALE_MS
- 1) / SCALE_MS
;
296 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
297 if (ms
> (int64_t) INT32_MAX
) {
305 /* qemu implementation of g_poll which uses a nanosecond timeout but is
306 * otherwise identical to g_poll
308 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
)
312 return ppoll((struct pollfd
*)fds
, nfds
, NULL
, NULL
);
315 int64_t tvsec
= timeout
/ 1000000000LL;
316 /* Avoid possibly overflowing and specifying a negative number of
317 * seconds, which would turn a very long timeout into a busy-wait.
319 if (tvsec
> (int64_t)INT32_MAX
) {
323 ts
.tv_nsec
= timeout
% 1000000000LL;
324 return ppoll((struct pollfd
*)fds
, nfds
, &ts
, NULL
);
327 return g_poll(fds
, nfds
, qemu_timeout_ns_to_ms(timeout
));
332 void timer_init_tl(QEMUTimer
*ts
,
333 QEMUTimerList
*timer_list
, int scale
,
334 QEMUTimerCB
*cb
, void *opaque
)
336 ts
->timer_list
= timer_list
;
340 ts
->expire_time
= -1;
343 void timer_deinit(QEMUTimer
*ts
)
345 assert(ts
->expire_time
== -1);
346 ts
->timer_list
= NULL
;
349 void timer_free(QEMUTimer
*ts
)
354 static void timer_del_locked(QEMUTimerList
*timer_list
, QEMUTimer
*ts
)
358 ts
->expire_time
= -1;
359 pt
= &timer_list
->active_timers
;
372 static bool timer_mod_ns_locked(QEMUTimerList
*timer_list
,
373 QEMUTimer
*ts
, int64_t expire_time
)
377 /* add the timer in the sorted list */
378 pt
= &timer_list
->active_timers
;
381 if (!timer_expired_ns(t
, expire_time
)) {
386 ts
->expire_time
= MAX(expire_time
, 0);
390 return pt
== &timer_list
->active_timers
;
393 static void timerlist_rearm(QEMUTimerList
*timer_list
)
395 /* Interrupt execution to force deadline recalculation. */
396 qemu_clock_warp(timer_list
->clock
->type
);
397 timerlist_notify(timer_list
);
400 /* stop a timer, but do not dealloc it */
401 void timer_del(QEMUTimer
*ts
)
403 QEMUTimerList
*timer_list
= ts
->timer_list
;
406 qemu_mutex_lock(&timer_list
->active_timers_lock
);
407 timer_del_locked(timer_list
, ts
);
408 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
412 /* modify the current timer so that it will be fired when current_time
413 >= expire_time. The corresponding callback will be called. */
414 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
)
416 QEMUTimerList
*timer_list
= ts
->timer_list
;
419 qemu_mutex_lock(&timer_list
->active_timers_lock
);
420 timer_del_locked(timer_list
, ts
);
421 rearm
= timer_mod_ns_locked(timer_list
, ts
, expire_time
);
422 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
425 timerlist_rearm(timer_list
);
429 /* modify the current timer so that it will be fired when current_time
430 >= expire_time or the current deadline, whichever comes earlier.
431 The corresponding callback will be called. */
432 void timer_mod_anticipate_ns(QEMUTimer
*ts
, int64_t expire_time
)
434 QEMUTimerList
*timer_list
= ts
->timer_list
;
437 qemu_mutex_lock(&timer_list
->active_timers_lock
);
438 if (ts
->expire_time
== -1 || ts
->expire_time
> expire_time
) {
439 if (ts
->expire_time
!= -1) {
440 timer_del_locked(timer_list
, ts
);
442 rearm
= timer_mod_ns_locked(timer_list
, ts
, expire_time
);
446 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
449 timerlist_rearm(timer_list
);
453 void timer_mod(QEMUTimer
*ts
, int64_t expire_time
)
455 timer_mod_ns(ts
, expire_time
* ts
->scale
);
458 void timer_mod_anticipate(QEMUTimer
*ts
, int64_t expire_time
)
460 timer_mod_anticipate_ns(ts
, expire_time
* ts
->scale
);
463 bool timer_pending(QEMUTimer
*ts
)
465 return ts
->expire_time
>= 0;
468 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
470 return timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
473 bool timerlist_run_timers(QEMUTimerList
*timer_list
)
476 int64_t current_time
;
477 bool progress
= false;
481 qemu_event_reset(&timer_list
->timers_done_ev
);
482 if (!timer_list
->clock
->enabled
|| !timer_list
->active_timers
) {
486 switch (timer_list
->clock
->type
) {
487 case QEMU_CLOCK_REALTIME
:
490 case QEMU_CLOCK_VIRTUAL
:
491 if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL
)) {
495 case QEMU_CLOCK_HOST
:
496 if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST
)) {
500 case QEMU_CLOCK_VIRTUAL_RT
:
501 if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT
)) {
507 current_time
= qemu_clock_get_ns(timer_list
->clock
->type
);
509 qemu_mutex_lock(&timer_list
->active_timers_lock
);
510 ts
= timer_list
->active_timers
;
511 if (!timer_expired_ns(ts
, current_time
)) {
512 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
516 /* remove timer from the list before calling the callback */
517 timer_list
->active_timers
= ts
->next
;
519 ts
->expire_time
= -1;
522 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
524 /* run the callback (the timer list can be modified) */
530 qemu_event_set(&timer_list
->timers_done_ev
);
534 bool qemu_clock_run_timers(QEMUClockType type
)
536 return timerlist_run_timers(main_loop_tlg
.tl
[type
]);
539 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
540 QEMUTimerListNotifyCB
*cb
, void *opaque
)
543 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
544 tlg
->tl
[type
] = timerlist_new(type
, cb
, opaque
);
548 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
)
551 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
552 timerlist_free(tlg
->tl
[type
]);
556 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
)
559 bool progress
= false;
560 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
561 progress
|= timerlist_run_timers(tlg
->tl
[type
]);
566 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
)
568 int64_t deadline
= -1;
570 bool play
= replay_mode
== REPLAY_MODE_PLAY
;
571 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
572 if (qemu_clock_use_for_deadline(type
)) {
573 if (!play
|| type
== QEMU_CLOCK_REALTIME
) {
574 deadline
= qemu_soonest_timeout(deadline
,
575 timerlist_deadline_ns(tlg
->tl
[type
]));
577 /* Read clock from the replay file and
578 do not calculate the deadline, based on virtual clock. */
579 qemu_clock_get_ns(type
);
586 int64_t qemu_clock_get_ns(QEMUClockType type
)
589 QEMUClock
*clock
= qemu_clock_ptr(type
);
592 case QEMU_CLOCK_REALTIME
:
595 case QEMU_CLOCK_VIRTUAL
:
597 return cpu_get_icount();
599 return cpu_get_clock();
601 case QEMU_CLOCK_HOST
:
602 now
= REPLAY_CLOCK(REPLAY_CLOCK_HOST
, get_clock_realtime());
605 if (now
< last
|| now
> (last
+ get_max_clock_jump())) {
606 notifier_list_notify(&clock
->reset_notifiers
, &now
);
609 case QEMU_CLOCK_VIRTUAL_RT
:
610 return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT
, cpu_get_clock());
614 void qemu_clock_register_reset_notifier(QEMUClockType type
,
617 QEMUClock
*clock
= qemu_clock_ptr(type
);
618 notifier_list_add(&clock
->reset_notifiers
, notifier
);
621 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
624 notifier_remove(notifier
);
627 void init_clocks(void)
630 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
631 qemu_clock_init(type
);
634 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
635 prctl(PR_SET_TIMERSLACK
, 1, 0, 0, 0);
639 uint64_t timer_expire_time_ns(QEMUTimer
*ts
)
641 return timer_pending(ts
) ? ts
->expire_time
: -1;
644 bool qemu_clock_run_all_timers(void)
646 bool progress
= false;
649 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
650 progress
|= qemu_clock_run_timers(type
);