blob: f16e422837dd996c7dbc56140abfba9501996b7e [file] [log] [blame]
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001/*
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
Peter Maydell1ac02062015-01-20 16:16:40 +000025#include "qemu/main-loop.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/timer.h"
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +030027#include "sysemu/replay.h"
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +030028#include "sysemu/sysemu.h"
Peter Maydell1ac02062015-01-20 16:16:40 +000029
Anthony Liguori30ea8332012-11-02 16:12:53 -050030#ifdef CONFIG_POSIX
31#include <pthread.h>
32#endif
Stefan Weilbff9f8b2012-04-20 10:27:06 +020033
Alex Bligh4e0c6522013-08-21 16:02:43 +010034#ifdef CONFIG_PPOLL
35#include <poll.h>
36#endif
37
Alex Blighcd758dd2013-08-21 16:02:44 +010038#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
39#include <sys/prctl.h>
40#endif
41
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010042/***********************************************************/
43/* timers */
44
Alex Blighb4049b72013-08-21 16:03:09 +010045typedef struct QEMUClock {
Liu Ping Fan3c053412013-09-25 14:21:00 +080046 /* We rely on BQL to protect the timerlists */
Alex Blighff83c662013-08-21 16:02:46 +010047 QLIST_HEAD(, QEMUTimerList) timerlists;
Jan Kiszka691a0c92011-06-20 14:06:27 +020048
49 NotifierList reset_notifiers;
50 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020051
Alex Blighff83c662013-08-21 16:02:46 +010052 QEMUClockType type;
Stefan Weil9a14b292012-04-20 11:51:58 +020053 bool enabled;
Alex Blighb4049b72013-08-21 16:03:09 +010054} QEMUClock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010055
Alex Bligh754d6a52013-08-21 16:02:48 +010056QEMUTimerListGroup main_loop_tlg;
Stefan Weilfbdb6642014-05-03 08:12:15 +020057static QEMUClock qemu_clocks[QEMU_CLOCK_MAX];
Alex Blighff83c662013-08-21 16:02:46 +010058
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.
64 */
65
66struct QEMUTimerList {
Stefan Weil9a14b292012-04-20 11:51:58 +020067 QEMUClock *clock;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +020068 QemuMutex active_timers_lock;
Alex Blighff83c662013-08-21 16:02:46 +010069 QEMUTimer *active_timers;
70 QLIST_ENTRY(QEMUTimerList) list;
Alex Blighd5541d82013-08-21 16:02:50 +010071 QEMUTimerListNotifyCB *notify_cb;
72 void *notify_opaque;
Liu Ping Fan3c053412013-09-25 14:21:00 +080073
74 /* lightweight method to mark the end of timerlist's running */
75 QemuEvent timers_done_ev;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010076};
77
Alex Bligh7bf8fbd2013-08-21 16:03:03 +010078/**
79 * qemu_clock_ptr:
80 * @type: type of clock
81 *
82 * Translate a clock type into a pointer to QEMUClock object.
83 *
84 * Returns: a pointer to the QEMUClock object
85 */
Alex Blighb4049b72013-08-21 16:03:09 +010086static inline QEMUClock *qemu_clock_ptr(QEMUClockType type)
Alex Bligh7bf8fbd2013-08-21 16:03:03 +010087{
88 return &qemu_clocks[type];
89}
90
Alex Blighe93379b2013-08-21 16:02:39 +010091static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010092{
93 return timer_head && (timer_head->expire_time <= current_time);
94}
95
Alex Bligh7bf8fbd2013-08-21 16:03:03 +010096QEMUTimerList *timerlist_new(QEMUClockType type,
97 QEMUTimerListNotifyCB *cb,
98 void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +010099{
100 QEMUTimerList *timer_list;
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100101 QEMUClock *clock = qemu_clock_ptr(type);
Alex Blighff83c662013-08-21 16:02:46 +0100102
103 timer_list = g_malloc0(sizeof(QEMUTimerList));
Paolo Bonzinie4efd8a2015-07-21 16:07:48 +0200104 qemu_event_init(&timer_list->timers_done_ev, true);
Alex Blighff83c662013-08-21 16:02:46 +0100105 timer_list->clock = clock;
Alex Blighd5541d82013-08-21 16:02:50 +0100106 timer_list->notify_cb = cb;
107 timer_list->notify_opaque = opaque;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200108 qemu_mutex_init(&timer_list->active_timers_lock);
Alex Blighff83c662013-08-21 16:02:46 +0100109 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
110 return timer_list;
111}
112
Alex Blighff83c662013-08-21 16:02:46 +0100113void timerlist_free(QEMUTimerList *timer_list)
114{
115 assert(!timerlist_has_timers(timer_list));
116 if (timer_list->clock) {
117 QLIST_REMOVE(timer_list, list);
Alex Blighff83c662013-08-21 16:02:46 +0100118 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200119 qemu_mutex_destroy(&timer_list->active_timers_lock);
Alex Blighff83c662013-08-21 16:02:46 +0100120 g_free(timer_list);
121}
122
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100123static void qemu_clock_init(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100124{
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100125 QEMUClock *clock = qemu_clock_ptr(type);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200126
Kirill Batuzov02ce2322014-05-06 16:59:53 +0400127 /* Assert that the clock of type TYPE has not been initialized yet. */
128 assert(main_loop_tlg.tl[type] == NULL);
129
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100130 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200131 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200132 clock->last = INT64_MIN;
Alex Blighff83c662013-08-21 16:02:46 +0100133 QLIST_INIT(&clock->timerlists);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200134 notifier_list_init(&clock->reset_notifiers);
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100135 main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100136}
137
Alex Bligh40daca52013-08-21 16:03:02 +0100138bool qemu_clock_use_for_deadline(QEMUClockType type)
Alex Blighff83c662013-08-21 16:02:46 +0100139{
Alex Bligh40daca52013-08-21 16:03:02 +0100140 return !(use_icount && (type == QEMU_CLOCK_VIRTUAL));
Alex Blighff83c662013-08-21 16:02:46 +0100141}
142
Alex Bligh40daca52013-08-21 16:03:02 +0100143void qemu_clock_notify(QEMUClockType type)
Alex Blighb1bbfe72013-08-21 16:02:55 +0100144{
145 QEMUTimerList *timer_list;
Alex Bligh40daca52013-08-21 16:03:02 +0100146 QEMUClock *clock = qemu_clock_ptr(type);
Alex Blighb1bbfe72013-08-21 16:02:55 +0100147 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
148 timerlist_notify(timer_list);
149 }
150}
151
Liu Ping Fan3c053412013-09-25 14:21:00 +0800152/* 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.
156 *
157 * Caller should hold BQL.
158 */
Alex Bligh40daca52013-08-21 16:03:02 +0100159void qemu_clock_enable(QEMUClockType type, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100160{
Alex Bligh40daca52013-08-21 16:03:02 +0100161 QEMUClock *clock = qemu_clock_ptr(type);
Liu Ping Fan3c053412013-09-25 14:21:00 +0800162 QEMUTimerList *tl;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200163 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100164 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200165 if (enabled && !old) {
Alex Bligh40daca52013-08-21 16:03:02 +0100166 qemu_clock_notify(type);
Liu Ping Fan3c053412013-09-25 14:21:00 +0800167 } else if (!enabled && old) {
168 QLIST_FOREACH(tl, &clock->timerlists, list) {
169 qemu_event_wait(&tl->timers_done_ev);
170 }
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200171 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100172}
173
Alex Blighff83c662013-08-21 16:02:46 +0100174bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200175{
Alex Blighff83c662013-08-21 16:02:46 +0100176 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200177}
178
Alex Bligh40daca52013-08-21 16:03:02 +0100179bool qemu_clock_has_timers(QEMUClockType type)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200180{
Alex Bligh40daca52013-08-21 16:03:02 +0100181 return timerlist_has_timers(
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100182 main_loop_tlg.tl[type]);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200183}
184
Alex Blighff83c662013-08-21 16:02:46 +0100185bool timerlist_expired(QEMUTimerList *timer_list)
186{
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200187 int64_t expire_time;
188
189 qemu_mutex_lock(&timer_list->active_timers_lock);
190 if (!timer_list->active_timers) {
191 qemu_mutex_unlock(&timer_list->active_timers_lock);
192 return false;
193 }
194 expire_time = timer_list->active_timers->expire_time;
195 qemu_mutex_unlock(&timer_list->active_timers_lock);
196
197 return expire_time < qemu_clock_get_ns(timer_list->clock->type);
Alex Blighff83c662013-08-21 16:02:46 +0100198}
199
Alex Bligh40daca52013-08-21 16:03:02 +0100200bool qemu_clock_expired(QEMUClockType type)
Alex Blighff83c662013-08-21 16:02:46 +0100201{
Alex Bligh40daca52013-08-21 16:03:02 +0100202 return timerlist_expired(
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100203 main_loop_tlg.tl[type]);
Alex Blighff83c662013-08-21 16:02:46 +0100204}
205
Alex Bligh02a03a92013-08-21 16:02:41 +0100206/*
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.
209 */
210
Alex Blighff83c662013-08-21 16:02:46 +0100211int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100212{
213 int64_t delta;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200214 int64_t expire_time;
Alex Bligh02a03a92013-08-21 16:02:41 +0100215
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200216 if (!timer_list->clock->enabled) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100217 return -1;
218 }
219
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200220 /* 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.
223 */
224 qemu_mutex_lock(&timer_list->active_timers_lock);
225 if (!timer_list->active_timers) {
226 qemu_mutex_unlock(&timer_list->active_timers_lock);
227 return -1;
228 }
229 expire_time = timer_list->active_timers->expire_time;
230 qemu_mutex_unlock(&timer_list->active_timers_lock);
231
232 delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
Alex Bligh02a03a92013-08-21 16:02:41 +0100233
234 if (delta <= 0) {
235 return 0;
236 }
237
238 return delta;
239}
240
Alex Blighac70aaf2013-08-21 16:02:57 +0100241/* 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
244 * calculations.
245 */
Alex Bligh40daca52013-08-21 16:03:02 +0100246int64_t qemu_clock_deadline_ns_all(QEMUClockType type)
Alex Blighac70aaf2013-08-21 16:02:57 +0100247{
248 int64_t deadline = -1;
249 QEMUTimerList *timer_list;
Alex Bligh40daca52013-08-21 16:03:02 +0100250 QEMUClock *clock = qemu_clock_ptr(type);
Alex Blighac70aaf2013-08-21 16:02:57 +0100251 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
252 deadline = qemu_soonest_timeout(deadline,
253 timerlist_deadline_ns(timer_list));
254 }
255 return deadline;
256}
257
Alex Bligh40daca52013-08-21 16:03:02 +0100258QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list)
Alex Blighff83c662013-08-21 16:02:46 +0100259{
Alex Bligh40daca52013-08-21 16:03:02 +0100260 return timer_list->clock->type;
Alex Blighff83c662013-08-21 16:02:46 +0100261}
262
Alex Bligh40daca52013-08-21 16:03:02 +0100263QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
Alex Blighff83c662013-08-21 16:02:46 +0100264{
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100265 return main_loop_tlg.tl[type];
Alex Blighff83c662013-08-21 16:02:46 +0100266}
267
Alex Blighd5541d82013-08-21 16:02:50 +0100268void timerlist_notify(QEMUTimerList *timer_list)
269{
270 if (timer_list->notify_cb) {
271 timer_list->notify_cb(timer_list->notify_opaque);
272 } else {
273 qemu_notify_event();
274 }
275}
276
Alex Bligh02a03a92013-08-21 16:02:41 +0100277/* Transition function to convert a nanosecond timeout to ms
278 * This is used where a system does not support ppoll
279 */
280int qemu_timeout_ns_to_ms(int64_t ns)
281{
282 int64_t ms;
283 if (ns < 0) {
284 return -1;
285 }
286
287 if (!ns) {
288 return 0;
289 }
290
291 /* Always round up, because it's better to wait too long than to wait too
292 * little and effectively busy-wait
293 */
294 ms = (ns + SCALE_MS - 1) / SCALE_MS;
295
296 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
297 if (ms > (int64_t) INT32_MAX) {
298 ms = INT32_MAX;
299 }
300
301 return (int) ms;
302}
303
304
Alex Bligh4e0c6522013-08-21 16:02:43 +0100305/* qemu implementation of g_poll which uses a nanosecond timeout but is
306 * otherwise identical to g_poll
307 */
308int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
309{
310#ifdef CONFIG_PPOLL
311 if (timeout < 0) {
312 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
313 } else {
314 struct timespec ts;
Peter Maydell490309f2014-11-25 18:21:45 +0000315 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.
318 */
319 if (tvsec > (int64_t)INT32_MAX) {
320 tvsec = INT32_MAX;
321 }
322 ts.tv_sec = tvsec;
Alex Bligh4e0c6522013-08-21 16:02:43 +0100323 ts.tv_nsec = timeout % 1000000000LL;
324 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
325 }
326#else
327 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
328#endif
329}
330
331
Paolo Bonzinif186aa92014-12-23 21:54:14 +0100332void timer_init_tl(QEMUTimer *ts,
333 QEMUTimerList *timer_list, int scale,
334 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100335{
Alex Blighff83c662013-08-21 16:02:46 +0100336 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100337 ts->cb = cb;
338 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100339 ts->scale = scale;
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200340 ts->expire_time = -1;
Alex Blighff83c662013-08-21 16:02:46 +0100341}
342
Paolo Bonzinicd1bd532014-12-24 10:57:04 +0100343void timer_deinit(QEMUTimer *ts)
344{
345 assert(ts->expire_time == -1);
346 ts->timer_list = NULL;
347}
348
Alex Bligh40daca52013-08-21 16:03:02 +0100349void timer_free(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100350{
Anthony Liguori7267c092011-08-20 22:09:37 -0500351 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100352}
353
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200354static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100355{
356 QEMUTimer **pt, *t;
357
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200358 ts->expire_time = -1;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200359 pt = &timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100360 for(;;) {
361 t = *pt;
362 if (!t)
363 break;
364 if (t == ts) {
365 *pt = t->next;
366 break;
367 }
368 pt = &t->next;
369 }
370}
371
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200372static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
373 QEMUTimer *ts, int64_t expire_time)
374{
375 QEMUTimer **pt, *t;
376
377 /* add the timer in the sorted list */
378 pt = &timer_list->active_timers;
379 for (;;) {
380 t = *pt;
381 if (!timer_expired_ns(t, expire_time)) {
382 break;
383 }
384 pt = &t->next;
385 }
386 ts->expire_time = MAX(expire_time, 0);
387 ts->next = *pt;
388 *pt = ts;
389
390 return pt == &timer_list->active_timers;
391}
392
393static void timerlist_rearm(QEMUTimerList *timer_list)
394{
395 /* Interrupt execution to force deadline recalculation. */
396 qemu_clock_warp(timer_list->clock->type);
397 timerlist_notify(timer_list);
398}
399
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200400/* stop a timer, but do not dealloc it */
401void timer_del(QEMUTimer *ts)
402{
403 QEMUTimerList *timer_list = ts->timer_list;
404
Paolo Bonzinicd1bd532014-12-24 10:57:04 +0100405 if (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);
409 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200410}
411
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100412/* modify the current timer so that it will be fired when current_time
413 >= expire_time. The corresponding callback will be called. */
Alex Bligh40daca52013-08-21 16:03:02 +0100414void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100415{
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200416 QEMUTimerList *timer_list = ts->timer_list;
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200417 bool rearm;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100418
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200419 qemu_mutex_lock(&timer_list->active_timers_lock);
420 timer_del_locked(timer_list, ts);
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200421 rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200422 qemu_mutex_unlock(&timer_list->active_timers_lock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100423
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200424 if (rearm) {
425 timerlist_rearm(timer_list);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100426 }
427}
428
Paolo Bonziniadd40e92013-10-03 15:11:43 +0200429/* 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. */
432void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
433{
434 QEMUTimerList *timer_list = ts->timer_list;
435 bool rearm;
436
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);
441 }
442 rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
443 } else {
444 rearm = false;
445 }
446 qemu_mutex_unlock(&timer_list->active_timers_lock);
447
448 if (rearm) {
449 timerlist_rearm(timer_list);
450 }
451}
452
Alex Bligh40daca52013-08-21 16:03:02 +0100453void timer_mod(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzini4a998742011-03-11 16:33:58 +0100454{
Alex Bligh40daca52013-08-21 16:03:02 +0100455 timer_mod_ns(ts, expire_time * ts->scale);
Paolo Bonzini4a998742011-03-11 16:33:58 +0100456}
457
Paolo Bonziniadd40e92013-10-03 15:11:43 +0200458void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
459{
460 timer_mod_anticipate_ns(ts, expire_time * ts->scale);
461}
462
Alex Blighe93379b2013-08-21 16:02:39 +0100463bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100464{
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200465 return ts->expire_time >= 0;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466}
467
Alex Blighe93379b2013-08-21 16:02:39 +0100468bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100469{
Alex Blighe93379b2013-08-21 16:02:39 +0100470 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100471}
472
Alex Blighff83c662013-08-21 16:02:46 +0100473bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100474{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200475 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100476 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100477 bool progress = false;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200478 QEMUTimerCB *cb;
479 void *opaque;
480
Liu Ping Fan3c053412013-09-25 14:21:00 +0800481 qemu_event_reset(&timer_list->timers_done_ev);
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300482 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Liu Ping Fan3c053412013-09-25 14:21:00 +0800483 goto out;
Alex Blighff83c662013-08-21 16:02:46 +0100484 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100485
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300486 switch (timer_list->clock->type) {
487 case QEMU_CLOCK_REALTIME:
488 break;
489 default:
490 case QEMU_CLOCK_VIRTUAL:
491 if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) {
492 goto out;
493 }
494 break;
495 case QEMU_CLOCK_HOST:
496 if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST)) {
497 goto out;
498 }
499 break;
500 case QEMU_CLOCK_VIRTUAL_RT:
501 if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) {
502 goto out;
503 }
504 break;
505 }
506
Alex Bligh40daca52013-08-21 16:03:02 +0100507 current_time = qemu_clock_get_ns(timer_list->clock->type);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100508 for(;;) {
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200509 qemu_mutex_lock(&timer_list->active_timers_lock);
Alex Blighff83c662013-08-21 16:02:46 +0100510 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100511 if (!timer_expired_ns(ts, current_time)) {
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200512 qemu_mutex_unlock(&timer_list->active_timers_lock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100513 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100514 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200515
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100516 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100517 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100518 ts->next = NULL;
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200519 ts->expire_time = -1;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200520 cb = ts->cb;
521 opaque = ts->opaque;
522 qemu_mutex_unlock(&timer_list->active_timers_lock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100523
524 /* run the callback (the timer list can be modified) */
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200525 cb(opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100526 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100527 }
Liu Ping Fan3c053412013-09-25 14:21:00 +0800528
529out:
530 qemu_event_set(&timer_list->timers_done_ev);
Alex Blighf9a976b2013-08-21 16:02:45 +0100531 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100532}
533
Alex Bligh40daca52013-08-21 16:03:02 +0100534bool qemu_clock_run_timers(QEMUClockType type)
535{
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100536 return timerlist_run_timers(main_loop_tlg.tl[type]);
Alex Bligh40daca52013-08-21 16:03:02 +0100537}
538
Alex Blighd5541d82013-08-21 16:02:50 +0100539void timerlistgroup_init(QEMUTimerListGroup *tlg,
540 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Bligh754d6a52013-08-21 16:02:48 +0100541{
542 QEMUClockType type;
543 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Blighd5541d82013-08-21 16:02:50 +0100544 tlg->tl[type] = timerlist_new(type, cb, opaque);
Alex Bligh754d6a52013-08-21 16:02:48 +0100545 }
546}
547
548void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
549{
550 QEMUClockType type;
551 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
552 timerlist_free(tlg->tl[type]);
553 }
554}
555
556bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
557{
558 QEMUClockType type;
559 bool progress = false;
560 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
561 progress |= timerlist_run_timers(tlg->tl[type]);
562 }
563 return progress;
564}
565
566int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
567{
568 int64_t deadline = -1;
569 QEMUClockType type;
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300570 bool play = replay_mode == REPLAY_MODE_PLAY;
Alex Bligh754d6a52013-08-21 16:02:48 +0100571 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300572 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]));
576 } else {
577 /* Read clock from the replay file and
578 do not calculate the deadline, based on virtual clock. */
579 qemu_clock_get_ns(type);
580 }
Alex Bligh754d6a52013-08-21 16:02:48 +0100581 }
582 }
583 return deadline;
584}
585
Alex Bligh40daca52013-08-21 16:03:02 +0100586int64_t qemu_clock_get_ns(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100587{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200588 int64_t now, last;
Alex Bligh40daca52013-08-21 16:03:02 +0100589 QEMUClock *clock = qemu_clock_ptr(type);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200590
Alex Bligh40daca52013-08-21 16:03:02 +0100591 switch (type) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100592 case QEMU_CLOCK_REALTIME:
593 return get_clock();
594 default:
595 case QEMU_CLOCK_VIRTUAL:
596 if (use_icount) {
597 return cpu_get_icount();
598 } else {
599 return cpu_get_clock();
600 }
601 case QEMU_CLOCK_HOST:
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300602 now = REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime());
Jan Kiszka691a0c92011-06-20 14:06:27 +0200603 last = clock->last;
604 clock->last = now;
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300605 if (now < last || now > (last + get_max_clock_jump())) {
Jan Kiszka691a0c92011-06-20 14:06:27 +0200606 notifier_list_notify(&clock->reset_notifiers, &now);
607 }
608 return now;
Pavel Dovgalyuk4e7fa732014-11-26 13:40:50 +0300609 case QEMU_CLOCK_VIRTUAL_RT:
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300610 return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100611 }
612}
613
Alex Bligh40daca52013-08-21 16:03:02 +0100614void qemu_clock_register_reset_notifier(QEMUClockType type,
615 Notifier *notifier)
616{
617 QEMUClock *clock = qemu_clock_ptr(type);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200618 notifier_list_add(&clock->reset_notifiers, notifier);
619}
620
Alex Bligh40daca52013-08-21 16:03:02 +0100621void qemu_clock_unregister_reset_notifier(QEMUClockType type,
622 Notifier *notifier)
Jan Kiszka691a0c92011-06-20 14:06:27 +0200623{
Paolo Bonzini31552522012-01-13 17:34:01 +0100624 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200625}
626
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100627void init_clocks(void)
628{
Alex Blighff83c662013-08-21 16:02:46 +0100629 QEMUClockType type;
630 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100631 qemu_clock_init(type);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100632 }
Alex Blighff83c662013-08-21 16:02:46 +0100633
Alex Blighcd758dd2013-08-21 16:02:44 +0100634#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
635 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
636#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100637}
638
Alex Blighe93379b2013-08-21 16:02:39 +0100639uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100640{
Alex Blighe93379b2013-08-21 16:02:39 +0100641 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100642}
643
Alex Bligh40daca52013-08-21 16:03:02 +0100644bool qemu_clock_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100645{
Alex Blighf9a976b2013-08-21 16:02:45 +0100646 bool progress = false;
Alex Blighff83c662013-08-21 16:02:46 +0100647 QEMUClockType type;
Alex Bligh6d327172013-08-21 16:02:59 +0100648
Alex Blighff83c662013-08-21 16:02:46 +0100649 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Bligh40daca52013-08-21 16:03:02 +0100650 progress |= qemu_clock_run_timers(type);
Alex Blighff83c662013-08-21 16:02:46 +0100651 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400652
Alex Blighf9a976b2013-08-21 16:02:45 +0100653 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100654}