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