]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-timer.c
aio / timers: Add qemu_clock_get_ms and qemu_clock_get_ms
[mirror_qemu.git] / qemu-timer.c
CommitLineData
db1a4972
PB
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
9c17d615 25#include "sysemu/sysemu.h"
83c9089e 26#include "monitor/monitor.h"
28ecbaee 27#include "ui/console.h"
db1a4972
PB
28
29#include "hw/hw.h"
30
1de7afc9 31#include "qemu/timer.h"
30ea8339
AL
32#ifdef CONFIG_POSIX
33#include <pthread.h>
34#endif
bff9f8bf 35
4e0c6529
AB
36#ifdef CONFIG_PPOLL
37#include <poll.h>
38#endif
39
cd758dd0
AB
40#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
41#include <sys/prctl.h>
42#endif
43
db1a4972
PB
44/***********************************************************/
45/* timers */
46
db1a4972 47struct QEMUClock {
ff83c66e
AB
48 QEMUTimerList *main_loop_timerlist;
49 QLIST_HEAD(, QEMUTimerList) timerlists;
691a0c9c
JK
50
51 NotifierList reset_notifiers;
52 int64_t last;
9a14b298 53
ff83c66e 54 QEMUClockType type;
9a14b298 55 bool enabled;
db1a4972
PB
56};
57
754d6a54 58QEMUTimerListGroup main_loop_tlg;
ff83c66e
AB
59QEMUClock *qemu_clocks[QEMU_CLOCK_MAX];
60
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.
66 */
67
68struct QEMUTimerList {
9a14b298 69 QEMUClock *clock;
ff83c66e
AB
70 QEMUTimer *active_timers;
71 QLIST_ENTRY(QEMUTimerList) list;
d5541d86
AB
72 QEMUTimerListNotifyCB *notify_cb;
73 void *notify_opaque;
db1a4972
PB
74};
75
e93379b0 76static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
45c7b37f
SW
77{
78 return timer_head && (timer_head->expire_time <= current_time);
79}
80
d5541d86
AB
81static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock,
82 QEMUTimerListNotifyCB *cb,
83 void *opaque)
ff83c66e
AB
84{
85 QEMUTimerList *timer_list;
86
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().
92 */
93 assert(clock);
94
95 timer_list = g_malloc0(sizeof(QEMUTimerList));
96 timer_list->clock = clock;
d5541d86
AB
97 timer_list->notify_cb = cb;
98 timer_list->notify_opaque = opaque;
ff83c66e
AB
99 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
100 return timer_list;
101}
102
d5541d86
AB
103QEMUTimerList *timerlist_new(QEMUClockType type,
104 QEMUTimerListNotifyCB *cb, void *opaque)
ff83c66e 105{
d5541d86 106 return timerlist_new_from_clock(qemu_clock_ptr(type), cb, opaque);
ff83c66e 107}
db1a4972 108
ff83c66e
AB
109void timerlist_free(QEMUTimerList *timer_list)
110{
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;
116 }
117 }
118 g_free(timer_list);
119}
120
121static QEMUClock *qemu_clock_new(QEMUClockType type)
db1a4972
PB
122{
123 QEMUClock *clock;
691a0c9c 124
7267c094 125 clock = g_malloc0(sizeof(QEMUClock));
db1a4972 126 clock->type = type;
5e1ec7b2 127 clock->enabled = true;
2ff68d07 128 clock->last = INT64_MIN;
ff83c66e 129 QLIST_INIT(&clock->timerlists);
691a0c9c 130 notifier_list_init(&clock->reset_notifiers);
d5541d86 131 clock->main_loop_timerlist = timerlist_new_from_clock(clock, NULL, NULL);
db1a4972
PB
132 return clock;
133}
134
ff83c66e
AB
135bool qemu_clock_use_for_deadline(QEMUClock *clock)
136{
137 return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL));
138}
139
b1bbfe72
AB
140void qemu_clock_notify(QEMUClock *clock)
141{
142 QEMUTimerList *timer_list;
143 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
144 timerlist_notify(timer_list);
145 }
146}
147
5e1ec7b2 148void qemu_clock_enable(QEMUClock *clock, bool enabled)
db1a4972 149{
fbdc14eb 150 bool old = clock->enabled;
db1a4972 151 clock->enabled = enabled;
fbdc14eb 152 if (enabled && !old) {
b1bbfe72 153 qemu_clock_notify(clock);
fbdc14eb 154 }
db1a4972
PB
155}
156
ff83c66e 157bool timerlist_has_timers(QEMUTimerList *timer_list)
dc2dfcf0 158{
ff83c66e 159 return !!timer_list->active_timers;
dc2dfcf0
PB
160}
161
ff83c66e 162bool qemu_clock_has_timers(QEMUClock *clock)
dc2dfcf0 163{
ff83c66e 164 return timerlist_has_timers(clock->main_loop_timerlist);
dc2dfcf0
PB
165}
166
ff83c66e
AB
167bool timerlist_expired(QEMUTimerList *timer_list)
168{
169 return (timer_list->active_timers &&
170 timer_list->active_timers->expire_time <
171 qemu_get_clock_ns(timer_list->clock));
172}
173
174bool qemu_clock_expired(QEMUClock *clock)
175{
176 return timerlist_expired(clock->main_loop_timerlist);
177}
178
02a03a9f
AB
179/*
180 * As above, but return -1 for no deadline, and do not cap to 2^32
181 * as we know the result is always positive.
182 */
183
ff83c66e 184int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
02a03a9f
AB
185{
186 int64_t delta;
187
ff83c66e 188 if (!timer_list->clock->enabled || !timer_list->active_timers) {
02a03a9f
AB
189 return -1;
190 }
191
ff83c66e
AB
192 delta = timer_list->active_timers->expire_time -
193 qemu_get_clock_ns(timer_list->clock);
02a03a9f
AB
194
195 if (delta <= 0) {
196 return 0;
197 }
198
199 return delta;
200}
201
ff83c66e
AB
202int64_t qemu_clock_deadline_ns(QEMUClock *clock)
203{
204 return timerlist_deadline_ns(clock->main_loop_timerlist);
205}
206
ac70aafc
AB
207/* Calculate the soonest deadline across all timerlists attached
208 * to the clock. This is used for the icount timeout so we
209 * ignore whether or not the clock should be used in deadline
210 * calculations.
211 */
212int64_t qemu_clock_deadline_ns_all(QEMUClock *clock)
213{
214 int64_t deadline = -1;
215 QEMUTimerList *timer_list;
216 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
217 deadline = qemu_soonest_timeout(deadline,
218 timerlist_deadline_ns(timer_list));
219 }
220 return deadline;
221}
222
ff83c66e
AB
223QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
224{
225 return timer_list->clock;
226}
227
228QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
229{
230 return clock->main_loop_timerlist;
231}
232
d5541d86
AB
233void timerlist_notify(QEMUTimerList *timer_list)
234{
235 if (timer_list->notify_cb) {
236 timer_list->notify_cb(timer_list->notify_opaque);
237 } else {
238 qemu_notify_event();
239 }
240}
241
02a03a9f
AB
242/* Transition function to convert a nanosecond timeout to ms
243 * This is used where a system does not support ppoll
244 */
245int qemu_timeout_ns_to_ms(int64_t ns)
246{
247 int64_t ms;
248 if (ns < 0) {
249 return -1;
250 }
251
252 if (!ns) {
253 return 0;
254 }
255
256 /* Always round up, because it's better to wait too long than to wait too
257 * little and effectively busy-wait
258 */
259 ms = (ns + SCALE_MS - 1) / SCALE_MS;
260
261 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
262 if (ms > (int64_t) INT32_MAX) {
263 ms = INT32_MAX;
264 }
265
266 return (int) ms;
267}
268
269
4e0c6529
AB
270/* qemu implementation of g_poll which uses a nanosecond timeout but is
271 * otherwise identical to g_poll
272 */
273int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
274{
275#ifdef CONFIG_PPOLL
276 if (timeout < 0) {
277 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
278 } else {
279 struct timespec ts;
280 ts.tv_sec = timeout / 1000000000LL;
281 ts.tv_nsec = timeout % 1000000000LL;
282 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
283 }
284#else
285 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
286#endif
287}
288
289
ff83c66e
AB
290void timer_init(QEMUTimer *ts,
291 QEMUTimerList *timer_list, int scale,
292 QEMUTimerCB *cb, void *opaque)
db1a4972 293{
ff83c66e 294 ts->timer_list = timer_list;
db1a4972
PB
295 ts->cb = cb;
296 ts->opaque = opaque;
4a998740 297 ts->scale = scale;
ff83c66e
AB
298}
299
300QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
301 QEMUTimerCB *cb, void *opaque)
302{
303 return timer_new_tl(clock->main_loop_timerlist,
304 scale, cb, opaque);
db1a4972
PB
305}
306
307void qemu_free_timer(QEMUTimer *ts)
308{
7267c094 309 g_free(ts);
db1a4972
PB
310}
311
312/* stop a timer, but do not dealloc it */
313void qemu_del_timer(QEMUTimer *ts)
314{
315 QEMUTimer **pt, *t;
316
317 /* NOTE: this code must be signal safe because
e93379b0 318 timer_expired() can be called from a signal. */
ff83c66e 319 pt = &ts->timer_list->active_timers;
db1a4972
PB
320 for(;;) {
321 t = *pt;
322 if (!t)
323 break;
324 if (t == ts) {
325 *pt = t->next;
326 break;
327 }
328 pt = &t->next;
329 }
330}
331
332/* modify the current timer so that it will be fired when current_time
333 >= expire_time. The corresponding callback will be called. */
2ff68d07 334void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
db1a4972
PB
335{
336 QEMUTimer **pt, *t;
337
338 qemu_del_timer(ts);
339
340 /* add the timer in the sorted list */
341 /* NOTE: this code must be signal safe because
e93379b0 342 timer_expired() can be called from a signal. */
ff83c66e 343 pt = &ts->timer_list->active_timers;
db1a4972
PB
344 for(;;) {
345 t = *pt;
e93379b0 346 if (!timer_expired_ns(t, expire_time)) {
db1a4972 347 break;
45c7b37f 348 }
db1a4972
PB
349 pt = &t->next;
350 }
351 ts->expire_time = expire_time;
352 ts->next = *pt;
353 *pt = ts;
354
355 /* Rearm if necessary */
ff83c66e 356 if (pt == &ts->timer_list->active_timers) {
db1a4972 357 /* Interrupt execution to force deadline recalculation. */
ff83c66e 358 qemu_clock_warp(ts->timer_list->clock);
b1bbfe72 359 timerlist_notify(ts->timer_list);
db1a4972
PB
360 }
361}
362
4a998740
PB
363void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
364{
365 qemu_mod_timer_ns(ts, expire_time * ts->scale);
366}
367
e93379b0 368bool timer_pending(QEMUTimer *ts)
db1a4972
PB
369{
370 QEMUTimer *t;
ff83c66e 371 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
5e1ec7b2
SW
372 if (t == ts) {
373 return true;
374 }
db1a4972 375 }
5e1ec7b2 376 return false;
db1a4972
PB
377}
378
e93379b0 379bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
db1a4972 380{
e93379b0 381 return timer_expired_ns(timer_head, current_time * timer_head->scale);
db1a4972
PB
382}
383
ff83c66e 384bool timerlist_run_timers(QEMUTimerList *timer_list)
db1a4972 385{
144b97c2 386 QEMUTimer *ts;
db1a4972 387 int64_t current_time;
f9a976b7 388 bool progress = false;
db1a4972 389
ff83c66e 390 if (!timer_list->clock->enabled) {
f9a976b7 391 return progress;
ff83c66e 392 }
db1a4972 393
ff83c66e 394 current_time = qemu_get_clock_ns(timer_list->clock);
db1a4972 395 for(;;) {
ff83c66e 396 ts = timer_list->active_timers;
e93379b0 397 if (!timer_expired_ns(ts, current_time)) {
db1a4972 398 break;
45c7b37f 399 }
db1a4972 400 /* remove timer from the list before calling the callback */
ff83c66e 401 timer_list->active_timers = ts->next;
db1a4972
PB
402 ts->next = NULL;
403
404 /* run the callback (the timer list can be modified) */
405 ts->cb(ts->opaque);
f9a976b7 406 progress = true;
db1a4972 407 }
f9a976b7 408 return progress;
db1a4972
PB
409}
410
ff83c66e
AB
411bool qemu_run_timers(QEMUClock *clock)
412{
413 return timerlist_run_timers(clock->main_loop_timerlist);
414}
415
d5541d86
AB
416void timerlistgroup_init(QEMUTimerListGroup *tlg,
417 QEMUTimerListNotifyCB *cb, void *opaque)
754d6a54
AB
418{
419 QEMUClockType type;
420 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
d5541d86 421 tlg->tl[type] = timerlist_new(type, cb, opaque);
754d6a54
AB
422 }
423}
424
425void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
426{
427 QEMUClockType type;
428 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
429 timerlist_free(tlg->tl[type]);
430 }
431}
432
433bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
434{
435 QEMUClockType type;
436 bool progress = false;
437 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
438 progress |= timerlist_run_timers(tlg->tl[type]);
439 }
440 return progress;
441}
442
443int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
444{
445 int64_t deadline = -1;
446 QEMUClockType type;
447 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
448 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) {
449 deadline = qemu_soonest_timeout(deadline,
450 timerlist_deadline_ns(
451 tlg->tl[type]));
452 }
453 }
454 return deadline;
455}
456
db1a4972
PB
457int64_t qemu_get_clock_ns(QEMUClock *clock)
458{
691a0c9c
JK
459 int64_t now, last;
460
db1a4972
PB
461 switch(clock->type) {
462 case QEMU_CLOCK_REALTIME:
463 return get_clock();
464 default:
465 case QEMU_CLOCK_VIRTUAL:
466 if (use_icount) {
467 return cpu_get_icount();
468 } else {
469 return cpu_get_clock();
470 }
471 case QEMU_CLOCK_HOST:
691a0c9c
JK
472 now = get_clock_realtime();
473 last = clock->last;
474 clock->last = now;
475 if (now < last) {
476 notifier_list_notify(&clock->reset_notifiers, &now);
477 }
478 return now;
db1a4972
PB
479 }
480}
481
691a0c9c
JK
482void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
483{
484 notifier_list_add(&clock->reset_notifiers, notifier);
485}
486
487void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
488{
31552529 489 notifier_remove(notifier);
691a0c9c
JK
490}
491
db1a4972
PB
492void init_clocks(void)
493{
ff83c66e
AB
494 QEMUClockType type;
495 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
496 if (!qemu_clocks[type]) {
497 qemu_clocks[type] = qemu_clock_new(type);
754d6a54 498 main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist;
ff83c66e 499 }
744ca8e3 500 }
ff83c66e 501
cd758dd0
AB
502#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
503 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
504#endif
db1a4972
PB
505}
506
e93379b0 507uint64_t timer_expire_time_ns(QEMUTimer *ts)
db1a4972 508{
e93379b0 509 return timer_pending(ts) ? ts->expire_time : -1;
db1a4972
PB
510}
511
f9a976b7 512bool qemu_run_all_timers(void)
db1a4972 513{
f9a976b7 514 bool progress = false;
ff83c66e 515 QEMUClockType type;
6d327171 516
ff83c66e
AB
517 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
518 progress |= qemu_run_timers(qemu_clock_ptr(type));
519 }
158fd3ce 520
f9a976b7 521 return progress;
db1a4972 522}