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 QEMUTimerList
*main_loop_timerlist
;
49 QLIST_HEAD(, QEMUTimerList
) timerlists
;
51 NotifierList reset_notifiers
;
58 QEMUTimerListGroup main_loop_tlg
;
59 QEMUClock
*qemu_clocks
[QEMU_CLOCK_MAX
];
61 /* A QEMUTimerList is a list of timers attached to a clock. More
62 * than one QEMUTimerList can be attached to each clock, for instance
63 * used by different AioContexts / threads. Each clock also has
64 * a list of the QEMUTimerLists associated with it, in order that
65 * reenabling the clock can call all the notifiers.
68 struct QEMUTimerList
{
70 QEMUTimer
*active_timers
;
71 QLIST_ENTRY(QEMUTimerList
) list
;
72 QEMUTimerListNotifyCB
*notify_cb
;
76 static bool timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
78 return timer_head
&& (timer_head
->expire_time
<= current_time
);
81 static QEMUTimerList
*timerlist_new_from_clock(QEMUClock
*clock
,
82 QEMUTimerListNotifyCB
*cb
,
85 QEMUTimerList
*timer_list
;
87 /* Assert if we do not have a clock. If you see this
88 * assertion in means that the clocks have not been
89 * initialised before a timerlist is needed. This
90 * normally happens if an AioContext is used before
91 * init_clocks() is called within main().
95 timer_list
= g_malloc0(sizeof(QEMUTimerList
));
96 timer_list
->clock
= clock
;
97 timer_list
->notify_cb
= cb
;
98 timer_list
->notify_opaque
= opaque
;
99 QLIST_INSERT_HEAD(&clock
->timerlists
, timer_list
, list
);
103 QEMUTimerList
*timerlist_new(QEMUClockType type
,
104 QEMUTimerListNotifyCB
*cb
, void *opaque
)
106 return timerlist_new_from_clock(qemu_clock_ptr(type
), cb
, opaque
);
109 void timerlist_free(QEMUTimerList
*timer_list
)
111 assert(!timerlist_has_timers(timer_list
));
112 if (timer_list
->clock
) {
113 QLIST_REMOVE(timer_list
, list
);
114 if (timer_list
->clock
->main_loop_timerlist
== timer_list
) {
115 timer_list
->clock
->main_loop_timerlist
= NULL
;
121 static QEMUClock
*qemu_clock_new(QEMUClockType type
)
125 clock
= g_malloc0(sizeof(QEMUClock
));
127 clock
->enabled
= true;
128 clock
->last
= INT64_MIN
;
129 QLIST_INIT(&clock
->timerlists
);
130 notifier_list_init(&clock
->reset_notifiers
);
131 clock
->main_loop_timerlist
= timerlist_new_from_clock(clock
, NULL
, NULL
);
135 bool qemu_clock_use_for_deadline(QEMUClockType type
)
137 return !(use_icount
&& (type
== QEMU_CLOCK_VIRTUAL
));
140 void qemu_clock_notify(QEMUClockType type
)
142 QEMUTimerList
*timer_list
;
143 QEMUClock
*clock
= qemu_clock_ptr(type
);
144 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
145 timerlist_notify(timer_list
);
149 void qemu_clock_enable(QEMUClockType type
, bool enabled
)
151 QEMUClock
*clock
= qemu_clock_ptr(type
);
152 bool old
= clock
->enabled
;
153 clock
->enabled
= enabled
;
154 if (enabled
&& !old
) {
155 qemu_clock_notify(type
);
159 bool timerlist_has_timers(QEMUTimerList
*timer_list
)
161 return !!timer_list
->active_timers
;
164 bool qemu_clock_has_timers(QEMUClockType type
)
166 return timerlist_has_timers(
167 qemu_clock_ptr(type
)->main_loop_timerlist
);
170 bool timerlist_expired(QEMUTimerList
*timer_list
)
172 return (timer_list
->active_timers
&&
173 timer_list
->active_timers
->expire_time
<
174 qemu_clock_get_ns(timer_list
->clock
->type
));
177 bool qemu_clock_expired(QEMUClockType type
)
179 return timerlist_expired(
180 qemu_clock_ptr(type
)->main_loop_timerlist
);
184 * As above, but return -1 for no deadline, and do not cap to 2^32
185 * as we know the result is always positive.
188 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
)
192 if (!timer_list
->clock
->enabled
|| !timer_list
->active_timers
) {
196 delta
= timer_list
->active_timers
->expire_time
-
197 qemu_clock_get_ns(timer_list
->clock
->type
);
206 /* Calculate the soonest deadline across all timerlists attached
207 * to the clock. This is used for the icount timeout so we
208 * ignore whether or not the clock should be used in deadline
211 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
)
213 int64_t deadline
= -1;
214 QEMUTimerList
*timer_list
;
215 QEMUClock
*clock
= qemu_clock_ptr(type
);
216 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
217 deadline
= qemu_soonest_timeout(deadline
,
218 timerlist_deadline_ns(timer_list
));
223 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
)
225 return timer_list
->clock
->type
;
228 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
)
230 return qemu_clock_ptr(type
)->main_loop_timerlist
;
233 void timerlist_notify(QEMUTimerList
*timer_list
)
235 if (timer_list
->notify_cb
) {
236 timer_list
->notify_cb(timer_list
->notify_opaque
);
242 /* Transition function to convert a nanosecond timeout to ms
243 * This is used where a system does not support ppoll
245 int qemu_timeout_ns_to_ms(int64_t ns
)
256 /* Always round up, because it's better to wait too long than to wait too
257 * little and effectively busy-wait
259 ms
= (ns
+ SCALE_MS
- 1) / SCALE_MS
;
261 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
262 if (ms
> (int64_t) INT32_MAX
) {
270 /* qemu implementation of g_poll which uses a nanosecond timeout but is
271 * otherwise identical to g_poll
273 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
)
277 return ppoll((struct pollfd
*)fds
, nfds
, NULL
, NULL
);
280 ts
.tv_sec
= timeout
/ 1000000000LL;
281 ts
.tv_nsec
= timeout
% 1000000000LL;
282 return ppoll((struct pollfd
*)fds
, nfds
, &ts
, NULL
);
285 return g_poll(fds
, nfds
, qemu_timeout_ns_to_ms(timeout
));
290 void timer_init(QEMUTimer
*ts
,
291 QEMUTimerList
*timer_list
, int scale
,
292 QEMUTimerCB
*cb
, void *opaque
)
294 ts
->timer_list
= timer_list
;
300 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
301 QEMUTimerCB
*cb
, void *opaque
)
303 return timer_new_tl(clock
->main_loop_timerlist
,
307 void timer_free(QEMUTimer
*ts
)
312 /* stop a timer, but do not dealloc it */
313 void timer_del(QEMUTimer
*ts
)
317 /* NOTE: this code must be signal safe because
318 timer_expired() can be called from a signal. */
319 pt
= &ts
->timer_list
->active_timers
;
332 /* modify the current timer so that it will be fired when current_time
333 >= expire_time. The corresponding callback will be called. */
334 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
)
340 /* add the timer in the sorted list */
341 /* NOTE: this code must be signal safe because
342 timer_expired() can be called from a signal. */
343 pt
= &ts
->timer_list
->active_timers
;
346 if (!timer_expired_ns(t
, expire_time
)) {
351 ts
->expire_time
= expire_time
;
355 /* Rearm if necessary */
356 if (pt
== &ts
->timer_list
->active_timers
) {
357 /* Interrupt execution to force deadline recalculation. */
358 qemu_clock_warp(ts
->timer_list
->clock
->type
);
359 timerlist_notify(ts
->timer_list
);
363 void timer_mod(QEMUTimer
*ts
, int64_t expire_time
)
365 timer_mod_ns(ts
, expire_time
* ts
->scale
);
368 bool timer_pending(QEMUTimer
*ts
)
371 for (t
= ts
->timer_list
->active_timers
; t
!= NULL
; t
= t
->next
) {
379 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
381 return timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
384 bool timerlist_run_timers(QEMUTimerList
*timer_list
)
387 int64_t current_time
;
388 bool progress
= false;
390 if (!timer_list
->clock
->enabled
) {
394 current_time
= qemu_clock_get_ns(timer_list
->clock
->type
);
396 ts
= timer_list
->active_timers
;
397 if (!timer_expired_ns(ts
, current_time
)) {
400 /* remove timer from the list before calling the callback */
401 timer_list
->active_timers
= ts
->next
;
404 /* run the callback (the timer list can be modified) */
411 bool qemu_clock_run_timers(QEMUClockType type
)
413 return timerlist_run_timers(qemu_clock_ptr(type
)->main_loop_timerlist
);
416 bool qemu_run_timers(QEMUClock
*clock
)
418 return qemu_clock_run_timers(clock
->type
);
421 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
422 QEMUTimerListNotifyCB
*cb
, void *opaque
)
425 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
426 tlg
->tl
[type
] = timerlist_new(type
, cb
, opaque
);
430 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
)
433 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
434 timerlist_free(tlg
->tl
[type
]);
438 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
)
441 bool progress
= false;
442 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
443 progress
|= timerlist_run_timers(tlg
->tl
[type
]);
448 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
)
450 int64_t deadline
= -1;
452 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
453 if (qemu_clock_use_for_deadline(tlg
->tl
[type
]->clock
->type
)) {
454 deadline
= qemu_soonest_timeout(deadline
,
455 timerlist_deadline_ns(
462 int64_t qemu_clock_get_ns(QEMUClockType type
)
465 QEMUClock
*clock
= qemu_clock_ptr(type
);
468 case QEMU_CLOCK_REALTIME
:
471 case QEMU_CLOCK_VIRTUAL
:
473 return cpu_get_icount();
475 return cpu_get_clock();
477 case QEMU_CLOCK_HOST
:
478 now
= get_clock_realtime();
482 notifier_list_notify(&clock
->reset_notifiers
, &now
);
488 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
490 return qemu_clock_get_ns(clock
->type
);
493 void qemu_clock_register_reset_notifier(QEMUClockType type
,
496 QEMUClock
*clock
= qemu_clock_ptr(type
);
497 notifier_list_add(&clock
->reset_notifiers
, notifier
);
500 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
503 notifier_remove(notifier
);
506 void qemu_register_clock_reset_notifier(QEMUClock
*clock
,
509 qemu_clock_register_reset_notifier(clock
->type
, notifier
);
512 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
,
515 qemu_clock_unregister_reset_notifier(clock
->type
, notifier
);
518 void init_clocks(void)
521 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
522 if (!qemu_clocks
[type
]) {
523 qemu_clocks
[type
] = qemu_clock_new(type
);
524 main_loop_tlg
.tl
[type
] = qemu_clocks
[type
]->main_loop_timerlist
;
528 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
529 prctl(PR_SET_TIMERSLACK
, 1, 0, 0, 0);
533 uint64_t timer_expire_time_ns(QEMUTimer
*ts
)
535 return timer_pending(ts
) ? ts
->expire_time
: -1;
538 bool qemu_clock_run_all_timers(void)
540 bool progress
= false;
543 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
544 progress
|= qemu_clock_run_timers(type
);