blob: 8ea8211873fc65d53de1eabf7b9a796144c32e53 [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
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/sysemu.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010026#include "monitor/monitor.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010028
29#include "hw/hw.h"
30
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010031#include "qemu/timer.h"
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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010047struct QEMUClock {
Alex Blighff83c662013-08-21 16:02:46 +010048 QEMUTimerList *main_loop_timerlist;
49 QLIST_HEAD(, QEMUTimerList) timerlists;
Jan Kiszka691a0c92011-06-20 14:06:27 +020050
51 NotifierList reset_notifiers;
52 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020053
Alex Blighff83c662013-08-21 16:02:46 +010054 QEMUClockType type;
Stefan Weil9a14b292012-04-20 11:51:58 +020055 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010056};
57
Alex Bligh754d6a52013-08-21 16:02:48 +010058QEMUTimerListGroup main_loop_tlg;
Alex Blighff83c662013-08-21 16:02:46 +010059QEMUClock *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 {
Stefan Weil9a14b292012-04-20 11:51:58 +020069 QEMUClock *clock;
Alex Blighff83c662013-08-21 16:02:46 +010070 QEMUTimer *active_timers;
71 QLIST_ENTRY(QEMUTimerList) list;
Alex Blighd5541d82013-08-21 16:02:50 +010072 QEMUTimerListNotifyCB *notify_cb;
73 void *notify_opaque;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010074};
75
Alex Blighe93379b2013-08-21 16:02:39 +010076static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010077{
78 return timer_head && (timer_head->expire_time <= current_time);
79}
80
Alex Blighd5541d82013-08-21 16:02:50 +010081static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock,
82 QEMUTimerListNotifyCB *cb,
83 void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +010084{
85 QEMUTimerList *timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010086
Alex Blighff83c662013-08-21 16:02:46 +010087 /* 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;
Alex Blighd5541d82013-08-21 16:02:50 +010097 timer_list->notify_cb = cb;
98 timer_list->notify_opaque = opaque;
Alex Blighff83c662013-08-21 16:02:46 +010099 QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
100 return timer_list;
101}
102
Alex Blighd5541d82013-08-21 16:02:50 +0100103QEMUTimerList *timerlist_new(QEMUClockType type,
104 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Blighff83c662013-08-21 16:02:46 +0100105{
Alex Blighd5541d82013-08-21 16:02:50 +0100106 return timerlist_new_from_clock(qemu_clock_ptr(type), cb, opaque);
Alex Blighff83c662013-08-21 16:02:46 +0100107}
108
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)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100122{
123 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200124
Anthony Liguori7267c092011-08-20 22:09:37 -0500125 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100126 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200127 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200128 clock->last = INT64_MIN;
Alex Blighff83c662013-08-21 16:02:46 +0100129 QLIST_INIT(&clock->timerlists);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200130 notifier_list_init(&clock->reset_notifiers);
Alex Blighd5541d82013-08-21 16:02:50 +0100131 clock->main_loop_timerlist = timerlist_new_from_clock(clock, NULL, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100132 return clock;
133}
134
Alex Blighff83c662013-08-21 16:02:46 +0100135bool qemu_clock_use_for_deadline(QEMUClock *clock)
136{
137 return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL));
138}
139
Alex Blighb1bbfe72013-08-21 16:02:55 +0100140void 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
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200148void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100149{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200150 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100151 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200152 if (enabled && !old) {
Alex Blighb1bbfe72013-08-21 16:02:55 +0100153 qemu_clock_notify(clock);
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200154 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100155}
156
Alex Blighff83c662013-08-21 16:02:46 +0100157bool timerlist_has_timers(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200158{
Alex Blighff83c662013-08-21 16:02:46 +0100159 return !!timer_list->active_timers;
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200160}
161
Alex Blighff83c662013-08-21 16:02:46 +0100162bool qemu_clock_has_timers(QEMUClock *clock)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200163{
Alex Blighff83c662013-08-21 16:02:46 +0100164 return timerlist_has_timers(clock->main_loop_timerlist);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200165}
166
Alex Blighff83c662013-08-21 16:02:46 +0100167bool 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
179int64_t timerlist_deadline(QEMUTimerList *timer_list)
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200180{
181 /* To avoid problems with overflow limit this to 2^32. */
182 int64_t delta = INT32_MAX;
183
Alex Blighff83c662013-08-21 16:02:46 +0100184 if (timer_list->clock->enabled && timer_list->active_timers) {
185 delta = timer_list->active_timers->expire_time -
186 qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200187 }
188 if (delta < 0) {
189 delta = 0;
190 }
191 return delta;
192}
193
Alex Blighff83c662013-08-21 16:02:46 +0100194int64_t qemu_clock_deadline(QEMUClock *clock)
195{
196 return timerlist_deadline(clock->main_loop_timerlist);
197}
198
Alex Bligh02a03a92013-08-21 16:02:41 +0100199/*
200 * As above, but return -1 for no deadline, and do not cap to 2^32
201 * as we know the result is always positive.
202 */
203
Alex Blighff83c662013-08-21 16:02:46 +0100204int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
Alex Bligh02a03a92013-08-21 16:02:41 +0100205{
206 int64_t delta;
207
Alex Blighff83c662013-08-21 16:02:46 +0100208 if (!timer_list->clock->enabled || !timer_list->active_timers) {
Alex Bligh02a03a92013-08-21 16:02:41 +0100209 return -1;
210 }
211
Alex Blighff83c662013-08-21 16:02:46 +0100212 delta = timer_list->active_timers->expire_time -
213 qemu_get_clock_ns(timer_list->clock);
Alex Bligh02a03a92013-08-21 16:02:41 +0100214
215 if (delta <= 0) {
216 return 0;
217 }
218
219 return delta;
220}
221
Alex Blighff83c662013-08-21 16:02:46 +0100222int64_t qemu_clock_deadline_ns(QEMUClock *clock)
223{
224 return timerlist_deadline_ns(clock->main_loop_timerlist);
225}
226
Alex Blighac70aaf2013-08-21 16:02:57 +0100227/* Calculate the soonest deadline across all timerlists attached
228 * to the clock. This is used for the icount timeout so we
229 * ignore whether or not the clock should be used in deadline
230 * calculations.
231 */
232int64_t qemu_clock_deadline_ns_all(QEMUClock *clock)
233{
234 int64_t deadline = -1;
235 QEMUTimerList *timer_list;
236 QLIST_FOREACH(timer_list, &clock->timerlists, list) {
237 deadline = qemu_soonest_timeout(deadline,
238 timerlist_deadline_ns(timer_list));
239 }
240 return deadline;
241}
242
Alex Blighff83c662013-08-21 16:02:46 +0100243QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list)
244{
245 return timer_list->clock;
246}
247
248QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock)
249{
250 return clock->main_loop_timerlist;
251}
252
Alex Blighd5541d82013-08-21 16:02:50 +0100253void timerlist_notify(QEMUTimerList *timer_list)
254{
255 if (timer_list->notify_cb) {
256 timer_list->notify_cb(timer_list->notify_opaque);
257 } else {
258 qemu_notify_event();
259 }
260}
261
Alex Bligh02a03a92013-08-21 16:02:41 +0100262/* Transition function to convert a nanosecond timeout to ms
263 * This is used where a system does not support ppoll
264 */
265int qemu_timeout_ns_to_ms(int64_t ns)
266{
267 int64_t ms;
268 if (ns < 0) {
269 return -1;
270 }
271
272 if (!ns) {
273 return 0;
274 }
275
276 /* Always round up, because it's better to wait too long than to wait too
277 * little and effectively busy-wait
278 */
279 ms = (ns + SCALE_MS - 1) / SCALE_MS;
280
281 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
282 if (ms > (int64_t) INT32_MAX) {
283 ms = INT32_MAX;
284 }
285
286 return (int) ms;
287}
288
289
Alex Bligh4e0c6522013-08-21 16:02:43 +0100290/* qemu implementation of g_poll which uses a nanosecond timeout but is
291 * otherwise identical to g_poll
292 */
293int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
294{
295#ifdef CONFIG_PPOLL
296 if (timeout < 0) {
297 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
298 } else {
299 struct timespec ts;
300 ts.tv_sec = timeout / 1000000000LL;
301 ts.tv_nsec = timeout % 1000000000LL;
302 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
303 }
304#else
305 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
306#endif
307}
308
309
Alex Blighff83c662013-08-21 16:02:46 +0100310void timer_init(QEMUTimer *ts,
311 QEMUTimerList *timer_list, int scale,
312 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100313{
Alex Blighff83c662013-08-21 16:02:46 +0100314 ts->timer_list = timer_list;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100315 ts->cb = cb;
316 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100317 ts->scale = scale;
Alex Blighff83c662013-08-21 16:02:46 +0100318}
319
320QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
321 QEMUTimerCB *cb, void *opaque)
322{
323 return timer_new_tl(clock->main_loop_timerlist,
324 scale, cb, opaque);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100325}
326
327void qemu_free_timer(QEMUTimer *ts)
328{
Anthony Liguori7267c092011-08-20 22:09:37 -0500329 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100330}
331
332/* stop a timer, but do not dealloc it */
333void qemu_del_timer(QEMUTimer *ts)
334{
335 QEMUTimer **pt, *t;
336
337 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100338 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100339 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100340 for(;;) {
341 t = *pt;
342 if (!t)
343 break;
344 if (t == ts) {
345 *pt = t->next;
346 break;
347 }
348 pt = &t->next;
349 }
350}
351
352/* modify the current timer so that it will be fired when current_time
353 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200354void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100355{
356 QEMUTimer **pt, *t;
357
358 qemu_del_timer(ts);
359
360 /* add the timer in the sorted list */
361 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100362 timer_expired() can be called from a signal. */
Alex Blighff83c662013-08-21 16:02:46 +0100363 pt = &ts->timer_list->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100364 for(;;) {
365 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100366 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100367 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100368 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100369 pt = &t->next;
370 }
371 ts->expire_time = expire_time;
372 ts->next = *pt;
373 *pt = ts;
374
375 /* Rearm if necessary */
Alex Blighff83c662013-08-21 16:02:46 +0100376 if (pt == &ts->timer_list->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100377 /* Interrupt execution to force deadline recalculation. */
Alex Blighff83c662013-08-21 16:02:46 +0100378 qemu_clock_warp(ts->timer_list->clock);
Alex Blighb1bbfe72013-08-21 16:02:55 +0100379 timerlist_notify(ts->timer_list);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100380 }
381}
382
Paolo Bonzini4a998742011-03-11 16:33:58 +0100383void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
384{
385 qemu_mod_timer_ns(ts, expire_time * ts->scale);
386}
387
Alex Blighe93379b2013-08-21 16:02:39 +0100388bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100389{
390 QEMUTimer *t;
Alex Blighff83c662013-08-21 16:02:46 +0100391 for (t = ts->timer_list->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200392 if (t == ts) {
393 return true;
394 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100395 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200396 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100397}
398
Alex Blighe93379b2013-08-21 16:02:39 +0100399bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100400{
Alex Blighe93379b2013-08-21 16:02:39 +0100401 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100402}
403
Alex Blighff83c662013-08-21 16:02:46 +0100404bool timerlist_run_timers(QEMUTimerList *timer_list)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100405{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200406 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100407 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100408 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100409
Alex Blighff83c662013-08-21 16:02:46 +0100410 if (!timer_list->clock->enabled) {
Alex Blighf9a976b2013-08-21 16:02:45 +0100411 return progress;
Alex Blighff83c662013-08-21 16:02:46 +0100412 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100413
Alex Blighff83c662013-08-21 16:02:46 +0100414 current_time = qemu_get_clock_ns(timer_list->clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100415 for(;;) {
Alex Blighff83c662013-08-21 16:02:46 +0100416 ts = timer_list->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100417 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100418 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100419 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100420 /* remove timer from the list before calling the callback */
Alex Blighff83c662013-08-21 16:02:46 +0100421 timer_list->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100422 ts->next = NULL;
423
424 /* run the callback (the timer list can be modified) */
425 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100426 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100427 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100428 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100429}
430
Alex Blighff83c662013-08-21 16:02:46 +0100431bool qemu_run_timers(QEMUClock *clock)
432{
433 return timerlist_run_timers(clock->main_loop_timerlist);
434}
435
Alex Blighd5541d82013-08-21 16:02:50 +0100436void timerlistgroup_init(QEMUTimerListGroup *tlg,
437 QEMUTimerListNotifyCB *cb, void *opaque)
Alex Bligh754d6a52013-08-21 16:02:48 +0100438{
439 QEMUClockType type;
440 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
Alex Blighd5541d82013-08-21 16:02:50 +0100441 tlg->tl[type] = timerlist_new(type, cb, opaque);
Alex Bligh754d6a52013-08-21 16:02:48 +0100442 }
443}
444
445void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
446{
447 QEMUClockType type;
448 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
449 timerlist_free(tlg->tl[type]);
450 }
451}
452
453bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
454{
455 QEMUClockType type;
456 bool progress = false;
457 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
458 progress |= timerlist_run_timers(tlg->tl[type]);
459 }
460 return progress;
461}
462
463int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
464{
465 int64_t deadline = -1;
466 QEMUClockType type;
467 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
468 if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) {
469 deadline = qemu_soonest_timeout(deadline,
470 timerlist_deadline_ns(
471 tlg->tl[type]));
472 }
473 }
474 return deadline;
475}
476
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100477int64_t qemu_get_clock_ns(QEMUClock *clock)
478{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200479 int64_t now, last;
480
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100481 switch(clock->type) {
482 case QEMU_CLOCK_REALTIME:
483 return get_clock();
484 default:
485 case QEMU_CLOCK_VIRTUAL:
486 if (use_icount) {
487 return cpu_get_icount();
488 } else {
489 return cpu_get_clock();
490 }
491 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200492 now = get_clock_realtime();
493 last = clock->last;
494 clock->last = now;
495 if (now < last) {
496 notifier_list_notify(&clock->reset_notifiers, &now);
497 }
498 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100499 }
500}
501
Jan Kiszka691a0c92011-06-20 14:06:27 +0200502void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
503{
504 notifier_list_add(&clock->reset_notifiers, notifier);
505}
506
507void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
508{
Paolo Bonzini31552522012-01-13 17:34:01 +0100509 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200510}
511
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100512void init_clocks(void)
513{
Alex Blighff83c662013-08-21 16:02:46 +0100514 QEMUClockType type;
515 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
516 if (!qemu_clocks[type]) {
517 qemu_clocks[type] = qemu_clock_new(type);
Alex Bligh754d6a52013-08-21 16:02:48 +0100518 main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist;
Alex Blighff83c662013-08-21 16:02:46 +0100519 }
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100520 }
Alex Blighff83c662013-08-21 16:02:46 +0100521
Alex Blighcd758dd2013-08-21 16:02:44 +0100522#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
523 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
524#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100525}
526
Alex Blighe93379b2013-08-21 16:02:39 +0100527uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528{
Alex Blighe93379b2013-08-21 16:02:39 +0100529 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100530}
531
Alex Blighf9a976b2013-08-21 16:02:45 +0100532bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533{
Alex Blighf9a976b2013-08-21 16:02:45 +0100534 bool progress = false;
Alex Blighff83c662013-08-21 16:02:46 +0100535 QEMUClockType type;
Alex Bligh6d327172013-08-21 16:02:59 +0100536
Alex Blighff83c662013-08-21 16:02:46 +0100537 for (type = 0; type < QEMU_CLOCK_MAX; type++) {
538 progress |= qemu_run_timers(qemu_clock_ptr(type));
539 }
Peter Portante158fd3c2012-04-05 11:00:45 -0400540
Alex Blighf9a976b2013-08-21 16:02:45 +0100541 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100542}