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 /***********************************************************/
47 typedef struct QEMUClock
{
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 QemuMutex active_timers_lock
;
70 QEMUTimer
*active_timers
;
71 QLIST_ENTRY(QEMUTimerList
) list
;
72 QEMUTimerListNotifyCB
*notify_cb
;
78 * @type: type of clock
80 * Translate a clock type into a pointer to QEMUClock object.
82 * Returns: a pointer to the QEMUClock object
84 static inline QEMUClock
*qemu_clock_ptr(QEMUClockType type
)
86 return &qemu_clocks
[type
];
89 static bool timer_expired_ns(QEMUTimer
*timer_head
, int64_t current_time
)
91 return timer_head
&& (timer_head
->expire_time
<= current_time
);
94 QEMUTimerList
*timerlist_new(QEMUClockType type
,
95 QEMUTimerListNotifyCB
*cb
,
98 QEMUTimerList
*timer_list
;
99 QEMUClock
*clock
= qemu_clock_ptr(type
);
101 timer_list
= g_malloc0(sizeof(QEMUTimerList
));
102 timer_list
->clock
= clock
;
103 timer_list
->notify_cb
= cb
;
104 timer_list
->notify_opaque
= opaque
;
105 qemu_mutex_init(&timer_list
->active_timers_lock
);
106 QLIST_INSERT_HEAD(&clock
->timerlists
, timer_list
, list
);
110 void timerlist_free(QEMUTimerList
*timer_list
)
112 assert(!timerlist_has_timers(timer_list
));
113 if (timer_list
->clock
) {
114 QLIST_REMOVE(timer_list
, list
);
116 qemu_mutex_destroy(&timer_list
->active_timers_lock
);
120 static void qemu_clock_init(QEMUClockType type
)
122 QEMUClock
*clock
= qemu_clock_ptr(type
);
125 clock
->enabled
= true;
126 clock
->last
= INT64_MIN
;
127 QLIST_INIT(&clock
->timerlists
);
128 notifier_list_init(&clock
->reset_notifiers
);
129 main_loop_tlg
.tl
[type
] = timerlist_new(type
, NULL
, NULL
);
132 bool qemu_clock_use_for_deadline(QEMUClockType type
)
134 return !(use_icount
&& (type
== QEMU_CLOCK_VIRTUAL
));
137 void qemu_clock_notify(QEMUClockType type
)
139 QEMUTimerList
*timer_list
;
140 QEMUClock
*clock
= qemu_clock_ptr(type
);
141 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
142 timerlist_notify(timer_list
);
146 void qemu_clock_enable(QEMUClockType type
, bool enabled
)
148 QEMUClock
*clock
= qemu_clock_ptr(type
);
149 bool old
= clock
->enabled
;
150 clock
->enabled
= enabled
;
151 if (enabled
&& !old
) {
152 qemu_clock_notify(type
);
156 bool timerlist_has_timers(QEMUTimerList
*timer_list
)
158 return !!timer_list
->active_timers
;
161 bool qemu_clock_has_timers(QEMUClockType type
)
163 return timerlist_has_timers(
164 main_loop_tlg
.tl
[type
]);
167 bool timerlist_expired(QEMUTimerList
*timer_list
)
171 qemu_mutex_lock(&timer_list
->active_timers_lock
);
172 if (!timer_list
->active_timers
) {
173 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
176 expire_time
= timer_list
->active_timers
->expire_time
;
177 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
179 return expire_time
< qemu_clock_get_ns(timer_list
->clock
->type
);
182 bool qemu_clock_expired(QEMUClockType type
)
184 return timerlist_expired(
185 main_loop_tlg
.tl
[type
]);
189 * As above, but return -1 for no deadline, and do not cap to 2^32
190 * as we know the result is always positive.
193 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
)
198 if (!timer_list
->clock
->enabled
) {
202 /* The active timers list may be modified before the caller uses our return
203 * value but ->notify_cb() is called when the deadline changes. Therefore
204 * the caller should notice the change and there is no race condition.
206 qemu_mutex_lock(&timer_list
->active_timers_lock
);
207 if (!timer_list
->active_timers
) {
208 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
211 expire_time
= timer_list
->active_timers
->expire_time
;
212 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
214 delta
= expire_time
- qemu_clock_get_ns(timer_list
->clock
->type
);
223 /* Calculate the soonest deadline across all timerlists attached
224 * to the clock. This is used for the icount timeout so we
225 * ignore whether or not the clock should be used in deadline
228 int64_t qemu_clock_deadline_ns_all(QEMUClockType type
)
230 int64_t deadline
= -1;
231 QEMUTimerList
*timer_list
;
232 QEMUClock
*clock
= qemu_clock_ptr(type
);
233 QLIST_FOREACH(timer_list
, &clock
->timerlists
, list
) {
234 deadline
= qemu_soonest_timeout(deadline
,
235 timerlist_deadline_ns(timer_list
));
240 QEMUClockType
timerlist_get_clock(QEMUTimerList
*timer_list
)
242 return timer_list
->clock
->type
;
245 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClockType type
)
247 return main_loop_tlg
.tl
[type
];
250 void timerlist_notify(QEMUTimerList
*timer_list
)
252 if (timer_list
->notify_cb
) {
253 timer_list
->notify_cb(timer_list
->notify_opaque
);
259 /* Transition function to convert a nanosecond timeout to ms
260 * This is used where a system does not support ppoll
262 int qemu_timeout_ns_to_ms(int64_t ns
)
273 /* Always round up, because it's better to wait too long than to wait too
274 * little and effectively busy-wait
276 ms
= (ns
+ SCALE_MS
- 1) / SCALE_MS
;
278 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
279 if (ms
> (int64_t) INT32_MAX
) {
287 /* qemu implementation of g_poll which uses a nanosecond timeout but is
288 * otherwise identical to g_poll
290 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
)
294 return ppoll((struct pollfd
*)fds
, nfds
, NULL
, NULL
);
297 ts
.tv_sec
= timeout
/ 1000000000LL;
298 ts
.tv_nsec
= timeout
% 1000000000LL;
299 return ppoll((struct pollfd
*)fds
, nfds
, &ts
, NULL
);
302 return g_poll(fds
, nfds
, qemu_timeout_ns_to_ms(timeout
));
307 void timer_init(QEMUTimer
*ts
,
308 QEMUTimerList
*timer_list
, int scale
,
309 QEMUTimerCB
*cb
, void *opaque
)
311 ts
->timer_list
= timer_list
;
315 ts
->expire_time
= -1;
318 void timer_free(QEMUTimer
*ts
)
323 static void timer_del_locked(QEMUTimerList
*timer_list
, QEMUTimer
*ts
)
327 ts
->expire_time
= -1;
328 pt
= &timer_list
->active_timers
;
341 /* stop a timer, but do not dealloc it */
342 void timer_del(QEMUTimer
*ts
)
344 QEMUTimerList
*timer_list
= ts
->timer_list
;
346 qemu_mutex_lock(&timer_list
->active_timers_lock
);
347 timer_del_locked(timer_list
, ts
);
348 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
351 /* modify the current timer so that it will be fired when current_time
352 >= expire_time. The corresponding callback will be called. */
353 void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
)
355 QEMUTimerList
*timer_list
= ts
->timer_list
;
358 qemu_mutex_lock(&timer_list
->active_timers_lock
);
359 timer_del_locked(timer_list
, ts
);
361 /* add the timer in the sorted list */
362 pt
= &timer_list
->active_timers
;
365 if (!timer_expired_ns(t
, expire_time
)) {
370 ts
->expire_time
= MAX(expire_time
, 0);
373 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
375 /* Rearm if necessary */
376 if (pt
== &timer_list
->active_timers
) {
377 /* Interrupt execution to force deadline recalculation. */
378 qemu_clock_warp(timer_list
->clock
->type
);
379 timerlist_notify(timer_list
);
383 void timer_mod(QEMUTimer
*ts
, int64_t expire_time
)
385 timer_mod_ns(ts
, expire_time
* ts
->scale
);
388 bool timer_pending(QEMUTimer
*ts
)
390 return ts
->expire_time
>= 0;
393 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
395 return timer_expired_ns(timer_head
, current_time
* timer_head
->scale
);
398 bool timerlist_run_timers(QEMUTimerList
*timer_list
)
401 int64_t current_time
;
402 bool progress
= false;
406 if (!timer_list
->clock
->enabled
) {
410 current_time
= qemu_clock_get_ns(timer_list
->clock
->type
);
412 qemu_mutex_lock(&timer_list
->active_timers_lock
);
413 ts
= timer_list
->active_timers
;
414 if (!timer_expired_ns(ts
, current_time
)) {
415 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
419 /* remove timer from the list before calling the callback */
420 timer_list
->active_timers
= ts
->next
;
422 ts
->expire_time
= -1;
425 qemu_mutex_unlock(&timer_list
->active_timers_lock
);
427 /* run the callback (the timer list can be modified) */
434 bool qemu_clock_run_timers(QEMUClockType type
)
436 return timerlist_run_timers(main_loop_tlg
.tl
[type
]);
439 void timerlistgroup_init(QEMUTimerListGroup
*tlg
,
440 QEMUTimerListNotifyCB
*cb
, void *opaque
)
443 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
444 tlg
->tl
[type
] = timerlist_new(type
, cb
, opaque
);
448 void timerlistgroup_deinit(QEMUTimerListGroup
*tlg
)
451 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
452 timerlist_free(tlg
->tl
[type
]);
456 bool timerlistgroup_run_timers(QEMUTimerListGroup
*tlg
)
459 bool progress
= false;
460 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
461 progress
|= timerlist_run_timers(tlg
->tl
[type
]);
466 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup
*tlg
)
468 int64_t deadline
= -1;
470 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
471 if (qemu_clock_use_for_deadline(tlg
->tl
[type
]->clock
->type
)) {
472 deadline
= qemu_soonest_timeout(deadline
,
473 timerlist_deadline_ns(
480 int64_t qemu_clock_get_ns(QEMUClockType type
)
483 QEMUClock
*clock
= qemu_clock_ptr(type
);
486 case QEMU_CLOCK_REALTIME
:
489 case QEMU_CLOCK_VIRTUAL
:
491 return cpu_get_icount();
493 return cpu_get_clock();
495 case QEMU_CLOCK_HOST
:
496 now
= get_clock_realtime();
500 notifier_list_notify(&clock
->reset_notifiers
, &now
);
506 void qemu_clock_register_reset_notifier(QEMUClockType type
,
509 QEMUClock
*clock
= qemu_clock_ptr(type
);
510 notifier_list_add(&clock
->reset_notifiers
, notifier
);
513 void qemu_clock_unregister_reset_notifier(QEMUClockType type
,
516 notifier_remove(notifier
);
519 void init_clocks(void)
522 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
523 qemu_clock_init(type
);
526 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
527 prctl(PR_SET_TIMERSLACK
, 1, 0, 0, 0);
531 uint64_t timer_expire_time_ns(QEMUTimer
*ts
)
533 return timer_pending(ts
) ? ts
->expire_time
: -1;
536 bool qemu_clock_run_all_timers(void)
538 bool progress
= false;
541 for (type
= 0; type
< QEMU_CLOCK_MAX
; type
++) {
542 progress
|= qemu_clock_run_timers(type
);