blob: d548d3c1ada836b398a59ae810e55e7aaf3b735c [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 Maydelld38ea872016-01-29 17:50:05 +000025#include "qemu/osdep.h"
Peter Maydell1ac02062015-01-20 16:16:40 +000026#include "qemu/main-loop.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Stefan Hajnoczi3284c3d2020-03-16 11:09:56 +000028#include "qemu/lockable.h"
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +030029#include "sysemu/replay.h"
Paolo Bonzinid2528bd2017-03-03 12:01:16 +010030#include "sysemu/cpus.h"
Peter Maydell1ac02062015-01-20 16:16:40 +000031
Anthony Liguori30ea8332012-11-02 16:12:53 -050032#ifdef CONFIG_POSIX
33#include <pthread.h>
34#endif
Stefan Weilbff9f8b2012-04-20 10:27:06 +020035
Alex Bligh4e0c6522013-08-21 16:02:43 +010036#ifdef CONFIG_PPOLL
37#include <poll.h>
38#endif
39
Alex Blighcd758dd2013-08-21 16:02:44 +010040#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
41#include <sys/prctl.h>
42#endif
43
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010044/***********************************************************/
45/* timers */
46
Alex Blighb4049b72013-08-21 16:03:09 +010047typedef struct QEMUClock {
Liu Ping Fan3c053412013-09-25 14:21:00 +080048 /* We rely on BQL to protect the timerlists */
Alex Blighff83c662013-08-21 16:02:46 +010049 QLIST_HEAD(, QEMUTimerList) timerlists;
Jan Kiszka691a0c92011-06-20 14:06:27 +020050
Alex Blighff83c662013-08-21 16:02:46 +010051 QEMUClockType type;
Stefan Weil9a14b292012-04-20 11:51:58 +020052 bool enabled;
Alex Blighb4049b72013-08-21 16:03:09 +010053} QEMUClock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010054
Alex Bligh754d6a52013-08-21 16:02:48 +010055QEMUTimerListGroup main_loop_tlg;
Stefan Weilfbdb6642014-05-03 08:12:15 +020056static QEMUClock qemu_clocks[QEMU_CLOCK_MAX];
Alex Blighff83c662013-08-21 16:02:46 +010057
58/* A QEMUTimerList is a list of timers attached to a clock. More
59 * than one QEMUTimerList can be attached to each clock, for instance
60 * used by different AioContexts / threads. Each clock also has
61 * a list of the QEMUTimerLists associated with it, in order that
62 * reenabling the clock can call all the notifiers.
63 */
64
65struct QEMUTimerList {
Stefan Weil9a14b292012-04-20 11:51:58 +020066 QEMUClock *clock;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +020067 QemuMutex active_timers_lock;
Alex Blighff83c662013-08-21 16:02:46 +010068 QEMUTimer *active_timers;
69 QLIST_ENTRY(QEMUTimerList) list;
Alex Blighd5541d82013-08-21 16:02:50 +010070 QEMUTimerListNotifyCB *notify_cb;
71 void *notify_opaque;
Liu Ping Fan3c053412013-09-25 14:21:00 +080072
73 /* lightweight method to mark the end of timerlist's running */
74 QemuEvent timers_done_ev;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010075};
76
Alex Bligh7bf8fbd2013-08-21 16:03:03 +010077/**
78 * qemu_clock_ptr:
79 * @type: type of clock
80 *
81 * Translate a clock type into a pointer to QEMUClock object.
82 *
83 * Returns: a pointer to the QEMUClock object
84 */
Alex Blighb4049b72013-08-21 16:03:09 +010085static inline QEMUClock *qemu_clock_ptr(QEMUClockType type)
Alex Bligh7bf8fbd2013-08-21 16:03:03 +010086{
87 return &qemu_clocks[type];
88}
89
Alex Blighe93379b2013-08-21 16:02:39 +010090static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010091{
92 return timer_head && (timer_head->expire_time <= current_time);
93}
94
Alex Bligh7bf8fbd2013-08-21 16:03:03 +010095QEMUTimerList *timerlist_new(QEMUClockType type,
96 QEMUTimerListNotifyCB *cb,
97 void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +010098{
99 QEMUTimerList *timer_list;
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100100 QEMUClock *clock = qemu_clock_ptr(type);
Alex Blighff83c662013-08-21 16:02:46 +0100101
102 timer_list = g_malloc0(sizeof(QEMUTimerList));
Paolo Bonzinie4efd8a2015-07-21 16:07:48 +0200103 qemu_event_init(&timer_list->timers_done_ev, true);
Alex Blighff83c662013-08-21 16:02:46 +0100104 timer_list->clock = clock;
Alex Blighd5541d82013-08-21 16:02:50 +0100105 timer_list->notify_cb = cb;
106 timer_list->notify_opaque = opaque;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200107 qemu_mutex_init(&timer_list->active_timers_lock);
Alex Blighff83c662013-08-21 16:02:46 +0100108 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
109 return timer_list;
110}
111
Alex Blighff83c662013-08-21 16:02:46 +0100112void timerlist_free(QEMUTimerList *timer_list)
113{
114 assert(!timerlist_has_timers(timer_list));
115 if (timer_list->clock) {
116 QLIST_REMOVE(timer_list, list);
Alex Blighff83c662013-08-21 16:02:46 +0100117 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200118 qemu_mutex_destroy(&timer_list->active_timers_lock);
Alex Blighff83c662013-08-21 16:02:46 +0100119 g_free(timer_list);
120}
121
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100122static void qemu_clock_init(QEMUClockType type, QEMUTimerListNotifyCB *notify_cb)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100123{
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100124 QEMUClock *clock = qemu_clock_ptr(type);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200125
Kirill Batuzov02ce2322014-05-06 16:59:53 +0400126 /* Assert that the clock of type TYPE has not been initialized yet. */
127 assert(main_loop_tlg.tl[type] == NULL);
128
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100129 clock->type = type;
Gonglei3fdd0ee2016-08-09 15:49:15 +0800130 clock->enabled = (type == QEMU_CLOCK_VIRTUAL ? false : true);
Alex Blighff83c662013-08-21 16:02:46 +0100131 QLIST_INIT(&clock->timerlists);
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100132 main_loop_tlg.tl[type] = timerlist_new(type, notify_cb, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100133}
134
Alex Bligh40daca52013-08-21 16:03:02 +0100135bool qemu_clock_use_for_deadline(QEMUClockType type)
Alex Blighff83c662013-08-21 16:02:46 +0100136{
Alex Bligh40daca52013-08-21 16:03:02 +0100137 return !(use_icount && (type == QEMU_CLOCK_VIRTUAL));
Alex Blighff83c662013-08-21 16:02:46 +0100138}
139
Alex Bligh40daca52013-08-21 16:03:02 +0100140void qemu_clock_notify(QEMUClockType type)
Alex Blighb1bbfe72013-08-21 16:02:55 +0100141{
142 QEMUTimerList *timer_list;
Alex Bligh40daca52013-08-21 16:03:02 +0100143 QEMUClock *clock = qemu_clock_ptr(type);
Alex Blighb1bbfe72013-08-21 16:02:55 +0100144 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
145 timerlist_notify(timer_list);
146 }
147}
148
Liu Ping Fan3c053412013-09-25 14:21:00 +0800149/* Disabling the clock will wait for related timerlists to stop
150 * executing qemu_run_timers. Thus, this functions should not
151 * be used from the callback of a timer that is based on @clock.
152 * Doing so would cause a deadlock.
153 *
154 * Caller should hold BQL.
155 */
Alex Bligh40daca52013-08-21 16:03:02 +0100156void qemu_clock_enable(QEMUClockType type, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100157{
Alex Bligh40daca52013-08-21 16:03:02 +0100158 QEMUClock *clock = qemu_clock_ptr(type);
Liu Ping Fan3c053412013-09-25 14:21:00 +0800159 QEMUTimerList *tl;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200160 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100161 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200162 if (enabled && !old) {
Alex Bligh40daca52013-08-21 16:03:02 +0100163 qemu_clock_notify(type);
Liu Ping Fan3c053412013-09-25 14:21:00 +0800164 } else if (!enabled && old) {
165 QLIST_FOREACH(tl, &clock->timerlists, list) {
166 qemu_event_wait(&tl->timers_done_ev);
167 }
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200168 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100169}
170
Alex Blighff83c662013-08-21 16:02:46 +0100171bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200172{
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100173 return !!atomic_read(&timer_list->active_timers);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200174}
175
Alex Bligh40daca52013-08-21 16:03:02 +0100176bool qemu_clock_has_timers(QEMUClockType type)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200177{
Alex Bligh40daca52013-08-21 16:03:02 +0100178 return timerlist_has_timers(
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100179 main_loop_tlg.tl[type]);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200180}
181
Alex Blighff83c662013-08-21 16:02:46 +0100182bool timerlist_expired(QEMUTimerList *timer_list)
183{
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200184 int64_t expire_time;
185
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100186 if (!atomic_read(&timer_list->active_timers)) {
187 return false;
188 }
189
Stefan Hajnoczi3284c3d2020-03-16 11:09:56 +0000190 WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
191 if (!timer_list->active_timers) {
192 return false;
193 }
194 expire_time = timer_list->active_timers->expire_time;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200195 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200196
Paolo Bonzini33bef0b2017-03-03 11:37:57 +0100197 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
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100216 if (!atomic_read(&timer_list->active_timers)) {
217 return -1;
218 }
219
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200220 if (!timer_list->clock->enabled) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100221 return -1;
222 }
223
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200224 /* The active timers list may be modified before the caller uses our return
225 * value but ->notify_cb() is called when the deadline changes. Therefore
226 * the caller should notice the change and there is no race condition.
227 */
Stefan Hajnoczi3284c3d2020-03-16 11:09:56 +0000228 WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
229 if (!timer_list->active_timers) {
230 return -1;
231 }
232 expire_time = timer_list->active_timers->expire_time;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200233 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200234
235 delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
Alex Bligh02a03a92013-08-21 16:02:41 +0100236
237 if (delta <= 0) {
238 return 0;
239 }
240
241 return delta;
242}
243
Alex Blighac70aaf2013-08-21 16:02:57 +0100244/* Calculate the soonest deadline across all timerlists attached
245 * to the clock. This is used for the icount timeout so we
246 * ignore whether or not the clock should be used in deadline
247 * calculations.
248 */
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300249int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask)
Alex Blighac70aaf2013-08-21 16:02:57 +0100250{
251 int64_t deadline = -1;
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300252 int64_t delta;
253 int64_t expire_time;
254 QEMUTimer *ts;
Alex Blighac70aaf2013-08-21 16:02:57 +0100255 QEMUTimerList *timer_list;
Alex Bligh40daca52013-08-21 16:03:02 +0100256 QEMUClock *clock = qemu_clock_ptr(type);
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300257
258 if (!clock->enabled) {
259 return -1;
260 }
261
Alex Blighac70aaf2013-08-21 16:02:57 +0100262 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
Pavel Dovgalyukdcb15782019-07-25 11:44:26 +0300263 qemu_mutex_lock(&timer_list->active_timers_lock);
264 ts = timer_list->active_timers;
265 /* Skip all external timers */
266 while (ts && (ts->attributes & ~attr_mask)) {
267 ts = ts->next;
268 }
269 if (!ts) {
270 qemu_mutex_unlock(&timer_list->active_timers_lock);
271 continue;
272 }
273 expire_time = ts->expire_time;
274 qemu_mutex_unlock(&timer_list->active_timers_lock);
275
276 delta = expire_time - qemu_clock_get_ns(type);
277 if (delta <= 0) {
278 delta = 0;
279 }
280 deadline = qemu_soonest_timeout(deadline, delta);
Alex Blighac70aaf2013-08-21 16:02:57 +0100281 }
282 return deadline;
283}
284
Alex Bligh40daca52013-08-21 16:03:02 +0100285QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list)
Alex Blighff83c662013-08-21 16:02:46 +0100286{
Alex Bligh40daca52013-08-21 16:03:02 +0100287 return timer_list->clock->type;
Alex Blighff83c662013-08-21 16:02:46 +0100288}
289
Alex Bligh40daca52013-08-21 16:03:02 +0100290QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
Alex Blighff83c662013-08-21 16:02:46 +0100291{
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100292 return main_loop_tlg.tl[type];
Alex Blighff83c662013-08-21 16:02:46 +0100293}
294
Alex Blighd5541d82013-08-21 16:02:50 +0100295void timerlist_notify(QEMUTimerList *timer_list)
296{
297 if (timer_list->notify_cb) {
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100298 timer_list->notify_cb(timer_list->notify_opaque, timer_list->clock->type);
Alex Blighd5541d82013-08-21 16:02:50 +0100299 } else {
300 qemu_notify_event();
301 }
302}
303
Alex Bligh02a03a92013-08-21 16:02:41 +0100304/* Transition function to convert a nanosecond timeout to ms
305 * This is used where a system does not support ppoll
306 */
307int qemu_timeout_ns_to_ms(int64_t ns)
308{
309 int64_t ms;
310 if (ns < 0) {
311 return -1;
312 }
313
314 if (!ns) {
315 return 0;
316 }
317
318 /* Always round up, because it's better to wait too long than to wait too
319 * little and effectively busy-wait
320 */
Laurent Vivier5029b962016-05-31 18:36:04 +0200321 ms = DIV_ROUND_UP(ns, SCALE_MS);
Alex Bligh02a03a92013-08-21 16:02:41 +0100322
323 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
Frediano Ziglio5bd34352019-10-23 13:26:52 +0100324 return MIN(ms, INT32_MAX);
Alex Bligh02a03a92013-08-21 16:02:41 +0100325}
326
327
Alex Bligh4e0c6522013-08-21 16:02:43 +0100328/* qemu implementation of g_poll which uses a nanosecond timeout but is
329 * otherwise identical to g_poll
330 */
331int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
332{
333#ifdef CONFIG_PPOLL
334 if (timeout < 0) {
335 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
336 } else {
337 struct timespec ts;
Peter Maydell490309f2014-11-25 18:21:45 +0000338 int64_t tvsec = timeout / 1000000000LL;
339 /* Avoid possibly overflowing and specifying a negative number of
340 * seconds, which would turn a very long timeout into a busy-wait.
341 */
342 if (tvsec > (int64_t)INT32_MAX) {
343 tvsec = INT32_MAX;
344 }
345 ts.tv_sec = tvsec;
Alex Bligh4e0c6522013-08-21 16:02:43 +0100346 ts.tv_nsec = timeout % 1000000000LL;
347 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
348 }
349#else
350 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
351#endif
352}
353
354
Artem Pisarenko89a603a2018-10-17 14:24:19 +0600355void timer_init_full(QEMUTimer *ts,
356 QEMUTimerListGroup *timer_list_group, QEMUClockType type,
357 int scale, int attributes,
358 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100359{
Artem Pisarenko89a603a2018-10-17 14:24:19 +0600360 if (!timer_list_group) {
361 timer_list_group = &main_loop_tlg;
362 }
363 ts->timer_list = timer_list_group->tl[type];
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100364 ts->cb = cb;
365 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100366 ts->scale = scale;
Artem Pisarenko89a603a2018-10-17 14:24:19 +0600367 ts->attributes = attributes;
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200368 ts->expire_time = -1;
Alex Blighff83c662013-08-21 16:02:46 +0100369}
370
Paolo Bonzinicd1bd532014-12-24 10:57:04 +0100371void timer_deinit(QEMUTimer *ts)
372{
373 assert(ts->expire_time == -1);
374 ts->timer_list = NULL;
375}
376
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200377static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100378{
379 QEMUTimer **pt, *t;
380
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200381 ts->expire_time = -1;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200382 pt = &timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100383 for(;;) {
384 t = *pt;
385 if (!t)
386 break;
387 if (t == ts) {
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100388 atomic_set(pt, t->next);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100389 break;
390 }
391 pt = &t->next;
392 }
393}
394
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200395static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
396 QEMUTimer *ts, int64_t expire_time)
397{
398 QEMUTimer **pt, *t;
399
400 /* add the timer in the sorted list */
401 pt = &timer_list->active_timers;
402 for (;;) {
403 t = *pt;
404 if (!timer_expired_ns(t, expire_time)) {
405 break;
406 }
407 pt = &t->next;
408 }
409 ts->expire_time = MAX(expire_time, 0);
410 ts->next = *pt;
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100411 atomic_set(pt, ts);
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200412
413 return pt == &timer_list->active_timers;
414}
415
416static void timerlist_rearm(QEMUTimerList *timer_list)
417{
418 /* Interrupt execution to force deadline recalculation. */
Pavel Dovgalyuke76d1792016-03-10 14:56:09 +0300419 if (timer_list->clock->type == QEMU_CLOCK_VIRTUAL) {
420 qemu_start_warp_timer();
421 }
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200422 timerlist_notify(timer_list);
423}
424
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200425/* stop a timer, but do not dealloc it */
426void timer_del(QEMUTimer *ts)
427{
428 QEMUTimerList *timer_list = ts->timer_list;
429
Paolo Bonzinicd1bd532014-12-24 10:57:04 +0100430 if (timer_list) {
431 qemu_mutex_lock(&timer_list->active_timers_lock);
432 timer_del_locked(timer_list, ts);
433 qemu_mutex_unlock(&timer_list->active_timers_lock);
434 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200435}
436
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100437/* modify the current timer so that it will be fired when current_time
438 >= expire_time. The corresponding callback will be called. */
Alex Bligh40daca52013-08-21 16:03:02 +0100439void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100440{
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200441 QEMUTimerList *timer_list = ts->timer_list;
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200442 bool rearm;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100443
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200444 qemu_mutex_lock(&timer_list->active_timers_lock);
445 timer_del_locked(timer_list, ts);
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200446 rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200447 qemu_mutex_unlock(&timer_list->active_timers_lock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100448
Paolo Bonzini0f809e52013-10-03 15:06:39 +0200449 if (rearm) {
450 timerlist_rearm(timer_list);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100451 }
452}
453
Paolo Bonziniadd40e92013-10-03 15:11:43 +0200454/* modify the current timer so that it will be fired when current_time
455 >= expire_time or the current deadline, whichever comes earlier.
456 The corresponding callback will be called. */
457void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
458{
459 QEMUTimerList *timer_list = ts->timer_list;
460 bool rearm;
461
462 qemu_mutex_lock(&timer_list->active_timers_lock);
463 if (ts->expire_time == -1 || ts->expire_time > expire_time) {
464 if (ts->expire_time != -1) {
465 timer_del_locked(timer_list, ts);
466 }
467 rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
468 } else {
469 rearm = false;
470 }
471 qemu_mutex_unlock(&timer_list->active_timers_lock);
472
473 if (rearm) {
474 timerlist_rearm(timer_list);
475 }
476}
477
Alex Bligh40daca52013-08-21 16:03:02 +0100478void timer_mod(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzini4a998742011-03-11 16:33:58 +0100479{
Alex Bligh40daca52013-08-21 16:03:02 +0100480 timer_mod_ns(ts, expire_time * ts->scale);
Paolo Bonzini4a998742011-03-11 16:33:58 +0100481}
482
Paolo Bonziniadd40e92013-10-03 15:11:43 +0200483void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
484{
485 timer_mod_anticipate_ns(ts, expire_time * ts->scale);
486}
487
Alex Blighe93379b2013-08-21 16:02:39 +0100488bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100489{
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200490 return ts->expire_time >= 0;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100491}
492
Alex Blighe93379b2013-08-21 16:02:39 +0100493bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100494{
Alex Blighe93379b2013-08-21 16:02:39 +0100495 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100496}
497
Alex Blighff83c662013-08-21 16:02:46 +0100498bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100499{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200500 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100501 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100502 bool progress = false;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200503 QEMUTimerCB *cb;
504 void *opaque;
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600505 bool need_replay_checkpoint = false;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200506
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100507 if (!atomic_read(&timer_list->active_timers)) {
508 return false;
509 }
510
Liu Ping Fan3c053412013-09-25 14:21:00 +0800511 qemu_event_reset(&timer_list->timers_done_ev);
Paolo Bonzini8caa05d2016-12-01 09:58:02 +0100512 if (!timer_list->clock->enabled) {
Liu Ping Fan3c053412013-09-25 14:21:00 +0800513 goto out;
Alex Blighff83c662013-08-21 16:02:46 +0100514 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100515
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300516 switch (timer_list->clock->type) {
517 case QEMU_CLOCK_REALTIME:
518 break;
519 default:
520 case QEMU_CLOCK_VIRTUAL:
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600521 if (replay_mode != REPLAY_MODE_NONE) {
522 /* Checkpoint for virtual clock is redundant in cases where
523 * it's being triggered with only non-EXTERNAL timers, because
524 * these timers don't change guest state directly.
525 * Since it has conditional dependence on specific timers, it is
526 * subject to race conditions and requires special handling.
527 * See below.
528 */
529 need_replay_checkpoint = true;
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300530 }
531 break;
532 case QEMU_CLOCK_HOST:
533 if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST)) {
534 goto out;
535 }
536 break;
537 case QEMU_CLOCK_VIRTUAL_RT:
538 if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) {
539 goto out;
540 }
541 break;
542 }
543
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600544 /*
545 * Extract expired timers from active timers list and and process them.
546 *
547 * In rr mode we need "filtered" checkpointing for virtual clock. The
548 * checkpoint must be recorded/replayed before processing any non-EXTERNAL timer,
549 * and that must only be done once since the clock value stays the same. Because
550 * non-EXTERNAL timers may appear in the timers list while it being processed,
551 * the checkpoint can be issued at a time until no timers are left and we are
552 * done".
553 */
Alex Bligh40daca52013-08-21 16:03:02 +0100554 current_time = qemu_clock_get_ns(timer_list->clock->type);
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600555 qemu_mutex_lock(&timer_list->active_timers_lock);
556 while ((ts = timer_list->active_timers)) {
Alex Blighe93379b2013-08-21 16:02:39 +0100557 if (!timer_expired_ns(ts, current_time)) {
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600558 /* No expired timers left. The checkpoint can be skipped
559 * if no timers fired or they were all external.
560 */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100561 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100562 }
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600563 if (need_replay_checkpoint
564 && !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL)) {
565 /* once we got here, checkpoint clock only once */
566 need_replay_checkpoint = false;
567 qemu_mutex_unlock(&timer_list->active_timers_lock);
568 if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) {
569 goto out;
570 }
571 qemu_mutex_lock(&timer_list->active_timers_lock);
572 /* The lock was released; start over again in case the list was
573 * modified.
574 */
575 continue;
576 }
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200577
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100578 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100579 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100580 ts->next = NULL;
Paolo Bonzini3db1ee72013-09-12 11:02:20 +0200581 ts->expire_time = -1;
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200582 cb = ts->cb;
583 opaque = ts->opaque;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100584
585 /* run the callback (the timer list can be modified) */
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600586 qemu_mutex_unlock(&timer_list->active_timers_lock);
Stefan Hajnoczi978f2202013-09-12 11:02:19 +0200587 cb(opaque);
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600588 qemu_mutex_lock(&timer_list->active_timers_lock);
589
Alex Blighf9a976b2013-08-21 16:02:45 +0100590 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100591 }
Artem Pisarenkoe81f8672018-10-17 14:24:20 +0600592 qemu_mutex_unlock(&timer_list->active_timers_lock);
Liu Ping Fan3c053412013-09-25 14:21:00 +0800593
594out:
595 qemu_event_set(&timer_list->timers_done_ev);
Alex Blighf9a976b2013-08-21 16:02:45 +0100596 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100597}
598
Alex Bligh40daca52013-08-21 16:03:02 +0100599bool qemu_clock_run_timers(QEMUClockType type)
600{
Alex Bligh7bf8fbd2013-08-21 16:03:03 +0100601 return timerlist_run_timers(main_loop_tlg.tl[type]);
Alex Bligh40daca52013-08-21 16:03:02 +0100602}
603
Alex Blighd5541d82013-08-21 16:02:50 +0100604void timerlistgroup_init(QEMUTimerListGroup *tlg,
605 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Bligh754d6a52013-08-21 16:02:48 +0100606{
607 QEMUClockType type;
608 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Blighd5541d82013-08-21 16:02:50 +0100609 tlg->tl[type] = timerlist_new(type, cb, opaque);
Alex Bligh754d6a52013-08-21 16:02:48 +0100610 }
611}
612
613void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
614{
615 QEMUClockType type;
616 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
617 timerlist_free(tlg->tl[type]);
618 }
619}
620
621bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
622{
623 QEMUClockType type;
624 bool progress = false;
625 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
626 progress |= timerlist_run_timers(tlg->tl[type]);
627 }
628 return progress;
629}
630
631int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
632{
633 int64_t deadline = -1;
634 QEMUClockType type;
635 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Pavel Dovgalyuk8bd7f712015-09-17 19:24:44 +0300636 if (qemu_clock_use_for_deadline(type)) {
Pavel Dovgalyuke4dab942018-07-25 15:15:26 +0300637 deadline = qemu_soonest_timeout(deadline,
638 timerlist_deadline_ns(tlg->tl[type]));
Alex Bligh754d6a52013-08-21 16:02:48 +0100639 }
640 }
641 return deadline;
642}
643
Alex Bligh40daca52013-08-21 16:03:02 +0100644int64_t qemu_clock_get_ns(QEMUClockType type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100645{
Alex Bligh40daca52013-08-21 16:03:02 +0100646 switch (type) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100647 case QEMU_CLOCK_REALTIME:
648 return get_clock();
649 default:
650 case QEMU_CLOCK_VIRTUAL:
651 if (use_icount) {
652 return cpu_get_icount();
653 } else {
654 return cpu_get_clock();
655 }
656 case QEMU_CLOCK_HOST:
Dr. David Alan Gilbert3c2d4c82019-07-24 12:58:23 +0100657 return REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime());
Pavel Dovgalyuk4e7fa732014-11-26 13:40:50 +0300658 case QEMU_CLOCK_VIRTUAL_RT:
Pavel Dovgalyuk8eda2062015-09-17 19:24:28 +0300659 return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100660 }
661}
662
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100663void init_clocks(QEMUTimerListNotifyCB *notify_cb)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100664{
Alex Blighff83c662013-08-21 16:02:46 +0100665 QEMUClockType type;
666 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Paolo Bonzini3f53bc62017-03-03 11:50:29 +0100667 qemu_clock_init(type, notify_cb);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100668 }
Alex Blighff83c662013-08-21 16:02:46 +0100669
Alex Blighcd758dd2013-08-21 16:02:44 +0100670#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
671 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
672#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100673}
674
Alex Blighe93379b2013-08-21 16:02:39 +0100675uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100676{
Alex Blighe93379b2013-08-21 16:02:39 +0100677 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100678}
679
Alex Bligh40daca52013-08-21 16:03:02 +0100680bool qemu_clock_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100681{
Alex Blighf9a976b2013-08-21 16:02:45 +0100682 bool progress = false;
Alex Blighff83c662013-08-21 16:02:46 +0100683 QEMUClockType type;
Alex Bligh6d327172013-08-21 16:02:59 +0100684
Alex Blighff83c662013-08-21 16:02:46 +0100685 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Paolo Bonzini6b8f0182017-03-02 19:56:40 +0100686 if (qemu_clock_use_for_deadline(type)) {
687 progress |= qemu_clock_run_timers(type);
688 }
Alex Blighff83c662013-08-21 16:02:46 +0100689 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400690
Alex Blighf9a976b2013-08-21 16:02:45 +0100691 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100692}