xref: /openbmc/qemu/util/qemu-timer.c (revision eb7f80fd26d73e7e1af105431da58971b1dba517)
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 
25 #include "qemu/osdep.h"
26 #include "qemu/main-loop.h"
27 #include "qemu/timer.h"
28 #include "qemu/lockable.h"
29 #include "sysemu/cpu-timers.h"
30 #include "sysemu/replay.h"
31 #include "sysemu/cpus.h"
32 #include "sysemu/qtest.h"
33 
34 #ifdef CONFIG_POSIX
35 #include <pthread.h>
36 #endif
37 
38 #ifdef CONFIG_PPOLL
39 #include <poll.h>
40 #endif
41 
42 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
43 #include <sys/prctl.h>
44 #endif
45 
46 /***********************************************************/
47 /* timers */
48 
49 typedef struct QEMUClock {
50     /* We rely on BQL to protect the timerlists */
51     QLIST_HEAD(, QEMUTimerList) timerlists;
52 
53     QEMUClockType type;
54     bool enabled;
55 } QEMUClock;
56 
57 QEMUTimerListGroup main_loop_tlg;
58 static QEMUClock qemu_clocks[QEMU_CLOCK_MAX];
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 
67 struct QEMUTimerList {
68     QEMUClock *clock;
69     QemuMutex active_timers_lock;
70     QEMUTimer *active_timers;
71     QLIST_ENTRY(QEMUTimerList) list;
72     QEMUTimerListNotifyCB *notify_cb;
73     void *notify_opaque;
74 
75     /* lightweight method to mark the end of timerlist's running */
76     QemuEvent timers_done_ev;
77 };
78 
79 /**
80  * qemu_clock_ptr:
81  * @type: type of clock
82  *
83  * Translate a clock type into a pointer to QEMUClock object.
84  *
85  * Returns: a pointer to the QEMUClock object
86  */
87 static inline QEMUClock *qemu_clock_ptr(QEMUClockType type)
88 {
89     return &qemu_clocks[type];
90 }
91 
92 static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
93 {
94     return timer_head && (timer_head->expire_time <= current_time);
95 }
96 
97 QEMUTimerList *timerlist_new(QEMUClockType type,
98                              QEMUTimerListNotifyCB *cb,
99                              void *opaque)
100 {
101     QEMUTimerList *timer_list;
102     QEMUClock *clock = qemu_clock_ptr(type);
103 
104     timer_list = g_malloc0(sizeof(QEMUTimerList));
105     qemu_event_init(&timer_list->timers_done_ev, true);
106     timer_list->clock = clock;
107     timer_list->notify_cb = cb;
108     timer_list->notify_opaque = opaque;
109     qemu_mutex_init(&timer_list->active_timers_lock);
110     QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list);
111     return timer_list;
112 }
113 
114 void timerlist_free(QEMUTimerList *timer_list)
115 {
116     assert(!timerlist_has_timers(timer_list));
117     if (timer_list->clock) {
118         QLIST_REMOVE(timer_list, list);
119     }
120     qemu_mutex_destroy(&timer_list->active_timers_lock);
121     g_free(timer_list);
122 }
123 
124 static void qemu_clock_init(QEMUClockType type, QEMUTimerListNotifyCB *notify_cb)
125 {
126     QEMUClock *clock = qemu_clock_ptr(type);
127 
128     /* Assert that the clock of type TYPE has not been initialized yet. */
129     assert(main_loop_tlg.tl[type] == NULL);
130 
131     clock->type = type;
132     clock->enabled = (type == QEMU_CLOCK_VIRTUAL ? false : true);
133     QLIST_INIT(&clock->timerlists);
134     main_loop_tlg.tl[type] = timerlist_new(type, notify_cb, NULL);
135 }
136 
137 bool qemu_clock_use_for_deadline(QEMUClockType type)
138 {
139     return !(icount_enabled() && (type == QEMU_CLOCK_VIRTUAL));
140 }
141 
142 void qemu_clock_notify(QEMUClockType type)
143 {
144     QEMUTimerList *timer_list;
145     QEMUClock *clock = qemu_clock_ptr(type);
146     QLIST_FOREACH(timer_list, &clock->timerlists, list) {
147         timerlist_notify(timer_list);
148     }
149 }
150 
151 /* Disabling the clock will wait for related timerlists to stop
152  * executing qemu_run_timers.  Thus, this functions should not
153  * be used from the callback of a timer that is based on @clock.
154  * Doing so would cause a deadlock.
155  *
156  * Caller should hold BQL.
157  */
158 void qemu_clock_enable(QEMUClockType type, bool enabled)
159 {
160     QEMUClock *clock = qemu_clock_ptr(type);
161     QEMUTimerList *tl;
162     bool old = clock->enabled;
163     clock->enabled = enabled;
164     if (enabled && !old) {
165         qemu_clock_notify(type);
166     } else if (!enabled && old) {
167         QLIST_FOREACH(tl, &clock->timerlists, list) {
168             qemu_event_wait(&tl->timers_done_ev);
169         }
170     }
171 }
172 
173 bool timerlist_has_timers(QEMUTimerList *timer_list)
174 {
175     return !!qatomic_read(&timer_list->active_timers);
176 }
177 
178 bool qemu_clock_has_timers(QEMUClockType type)
179 {
180     return timerlist_has_timers(
181         main_loop_tlg.tl[type]);
182 }
183 
184 bool timerlist_expired(QEMUTimerList *timer_list)
185 {
186     int64_t expire_time;
187 
188     if (!qatomic_read(&timer_list->active_timers)) {
189         return false;
190     }
191 
192     WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
193         if (!timer_list->active_timers) {
194             return false;
195         }
196         expire_time = timer_list->active_timers->expire_time;
197     }
198 
199     return expire_time <= qemu_clock_get_ns(timer_list->clock->type);
200 }
201 
202 bool qemu_clock_expired(QEMUClockType type)
203 {
204     return timerlist_expired(
205         main_loop_tlg.tl[type]);
206 }
207 
208 /*
209  * As above, but return -1 for no deadline, and do not cap to 2^32
210  * as we know the result is always positive.
211  */
212 
213 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list)
214 {
215     int64_t delta;
216     int64_t expire_time;
217 
218     if (!qatomic_read(&timer_list->active_timers)) {
219         return -1;
220     }
221 
222     if (!timer_list->clock->enabled) {
223         return -1;
224     }
225 
226     /* The active timers list may be modified before the caller uses our return
227      * value but ->notify_cb() is called when the deadline changes.  Therefore
228      * the caller should notice the change and there is no race condition.
229      */
230     WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
231         if (!timer_list->active_timers) {
232             return -1;
233         }
234         expire_time = timer_list->active_timers->expire_time;
235     }
236 
237     delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
238 
239     if (delta <= 0) {
240         return 0;
241     }
242 
243     return delta;
244 }
245 
246 /*
247  * Returns the time remaining for the deadline, in ms.
248  */
249 int64_t timer_deadline_ms(QEMUTimer *timer)
250 {
251     if (timer_pending(timer)) {
252         return qemu_timeout_ns_to_ms(timer->expire_time) -
253                qemu_clock_get_ms(timer->timer_list->clock->type);
254     }
255 
256     return 0;
257 }
258 
259 /* Calculate the soonest deadline across all timerlists attached
260  * to the clock. This is used for the icount timeout so we
261  * ignore whether or not the clock should be used in deadline
262  * calculations.
263  */
264 int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask)
265 {
266     int64_t deadline = -1;
267     int64_t delta;
268     int64_t expire_time;
269     QEMUTimer *ts;
270     QEMUTimerList *timer_list;
271     QEMUClock *clock = qemu_clock_ptr(type);
272 
273     if (!clock->enabled) {
274         return -1;
275     }
276 
277     QLIST_FOREACH(timer_list, &clock->timerlists, list) {
278         qemu_mutex_lock(&timer_list->active_timers_lock);
279         ts = timer_list->active_timers;
280         /* Skip all external timers */
281         while (ts && (ts->attributes & ~attr_mask)) {
282             ts = ts->next;
283         }
284         if (!ts) {
285             qemu_mutex_unlock(&timer_list->active_timers_lock);
286             continue;
287         }
288         expire_time = ts->expire_time;
289         qemu_mutex_unlock(&timer_list->active_timers_lock);
290 
291         delta = expire_time - qemu_clock_get_ns(type);
292         if (delta <= 0) {
293             delta = 0;
294         }
295         deadline = qemu_soonest_timeout(deadline, delta);
296     }
297     return deadline;
298 }
299 
300 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list)
301 {
302     return timer_list->clock->type;
303 }
304 
305 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
306 {
307     return main_loop_tlg.tl[type];
308 }
309 
310 void timerlist_notify(QEMUTimerList *timer_list)
311 {
312     if (timer_list->notify_cb) {
313         timer_list->notify_cb(timer_list->notify_opaque, timer_list->clock->type);
314     } else {
315         qemu_notify_event();
316     }
317 }
318 
319 /* Transition function to convert a nanosecond timeout to ms
320  * This is used where a system does not support ppoll
321  */
322 int qemu_timeout_ns_to_ms(int64_t ns)
323 {
324     int64_t ms;
325     if (ns < 0) {
326         return -1;
327     }
328 
329     if (!ns) {
330         return 0;
331     }
332 
333     /* Always round up, because it's better to wait too long than to wait too
334      * little and effectively busy-wait
335      */
336     ms = DIV_ROUND_UP(ns, SCALE_MS);
337 
338     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
339     return MIN(ms, INT32_MAX);
340 }
341 
342 
343 /* qemu implementation of g_poll which uses a nanosecond timeout but is
344  * otherwise identical to g_poll
345  */
346 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
347 {
348 #ifdef CONFIG_PPOLL
349     if (timeout < 0) {
350         return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
351     } else {
352         struct timespec ts;
353         int64_t tvsec = timeout / 1000000000LL;
354         /* Avoid possibly overflowing and specifying a negative number of
355          * seconds, which would turn a very long timeout into a busy-wait.
356          */
357         if (tvsec > (int64_t)INT32_MAX) {
358             tvsec = INT32_MAX;
359         }
360         ts.tv_sec = tvsec;
361         ts.tv_nsec = timeout % 1000000000LL;
362         return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
363     }
364 #else
365     return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
366 #endif
367 }
368 
369 
370 void timer_init_full(QEMUTimer *ts,
371                      QEMUTimerListGroup *timer_list_group, QEMUClockType type,
372                      int scale, int attributes,
373                      QEMUTimerCB *cb, void *opaque)
374 {
375     if (!timer_list_group) {
376         timer_list_group = &main_loop_tlg;
377     }
378     ts->timer_list = timer_list_group->tl[type];
379     ts->cb = cb;
380     ts->opaque = opaque;
381     ts->scale = scale;
382     ts->attributes = attributes;
383     ts->expire_time = -1;
384 }
385 
386 void timer_deinit(QEMUTimer *ts)
387 {
388     assert(ts->expire_time == -1);
389     ts->timer_list = NULL;
390 }
391 
392 static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
393 {
394     QEMUTimer **pt, *t;
395 
396     ts->expire_time = -1;
397     pt = &timer_list->active_timers;
398     for(;;) {
399         t = *pt;
400         if (!t)
401             break;
402         if (t == ts) {
403             qatomic_set(pt, t->next);
404             break;
405         }
406         pt = &t->next;
407     }
408 }
409 
410 static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
411                                 QEMUTimer *ts, int64_t expire_time)
412 {
413     QEMUTimer **pt, *t;
414 
415     /* add the timer in the sorted list */
416     pt = &timer_list->active_timers;
417     for (;;) {
418         t = *pt;
419         if (!timer_expired_ns(t, expire_time)) {
420             break;
421         }
422         pt = &t->next;
423     }
424     ts->expire_time = MAX(expire_time, 0);
425     ts->next = *pt;
426     qatomic_set(pt, ts);
427 
428     return pt == &timer_list->active_timers;
429 }
430 
431 static void timerlist_rearm(QEMUTimerList *timer_list)
432 {
433     /* Interrupt execution to force deadline recalculation.  */
434     if (icount_enabled() && timer_list->clock->type == QEMU_CLOCK_VIRTUAL) {
435         icount_start_warp_timer();
436     }
437     timerlist_notify(timer_list);
438 }
439 
440 /* stop a timer, but do not dealloc it */
441 void timer_del(QEMUTimer *ts)
442 {
443     QEMUTimerList *timer_list = ts->timer_list;
444 
445     if (timer_list) {
446         qemu_mutex_lock(&timer_list->active_timers_lock);
447         timer_del_locked(timer_list, ts);
448         qemu_mutex_unlock(&timer_list->active_timers_lock);
449     }
450 }
451 
452 /* modify the current timer so that it will be fired when current_time
453    >= expire_time. The corresponding callback will be called. */
454 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
455 {
456     QEMUTimerList *timer_list = ts->timer_list;
457     bool rearm;
458 
459     qemu_mutex_lock(&timer_list->active_timers_lock);
460     timer_del_locked(timer_list, ts);
461     rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
462     qemu_mutex_unlock(&timer_list->active_timers_lock);
463 
464     if (rearm) {
465         timerlist_rearm(timer_list);
466     }
467 }
468 
469 /* modify the current timer so that it will be fired when current_time
470    >= expire_time or the current deadline, whichever comes earlier.
471    The corresponding callback will be called. */
472 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
473 {
474     QEMUTimerList *timer_list = ts->timer_list;
475     bool rearm;
476 
477     WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
478         if (ts->expire_time == -1 || ts->expire_time > expire_time) {
479             if (ts->expire_time != -1) {
480                 timer_del_locked(timer_list, ts);
481             }
482             rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
483         } else {
484             rearm = false;
485         }
486     }
487     if (rearm) {
488         timerlist_rearm(timer_list);
489     }
490 }
491 
492 void timer_mod(QEMUTimer *ts, int64_t expire_time)
493 {
494     timer_mod_ns(ts, expire_time * ts->scale);
495 }
496 
497 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
498 {
499     timer_mod_anticipate_ns(ts, expire_time * ts->scale);
500 }
501 
502 bool timer_pending(QEMUTimer *ts)
503 {
504     return ts->expire_time >= 0;
505 }
506 
507 bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
508 {
509     return timer_expired_ns(timer_head, current_time * timer_head->scale);
510 }
511 
512 bool timerlist_run_timers(QEMUTimerList *timer_list)
513 {
514     QEMUTimer *ts;
515     int64_t current_time;
516     bool progress = false;
517     QEMUTimerCB *cb;
518     void *opaque;
519 
520     if (!qatomic_read(&timer_list->active_timers)) {
521         return false;
522     }
523 
524     qemu_event_reset(&timer_list->timers_done_ev);
525     if (!timer_list->clock->enabled) {
526         goto out;
527     }
528 
529     switch (timer_list->clock->type) {
530     case QEMU_CLOCK_REALTIME:
531         break;
532     default:
533     case QEMU_CLOCK_VIRTUAL:
534         break;
535     case QEMU_CLOCK_HOST:
536         if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST)) {
537             goto out;
538         }
539         break;
540     case QEMU_CLOCK_VIRTUAL_RT:
541         if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) {
542             goto out;
543         }
544         break;
545     }
546 
547     /*
548      * Extract expired timers from active timers list and process them.
549      *
550      * In rr mode we need "filtered" checkpointing for virtual clock.  The
551      * checkpoint must be recorded/replayed before processing any non-EXTERNAL timer,
552      * and that must only be done once since the clock value stays the same. Because
553      * non-EXTERNAL timers may appear in the timers list while it being processed,
554      * the checkpoint can be issued at a time until no timers are left and we are
555      * done".
556      */
557     current_time = qemu_clock_get_ns(timer_list->clock->type);
558     qemu_mutex_lock(&timer_list->active_timers_lock);
559     while ((ts = timer_list->active_timers)) {
560         if (!timer_expired_ns(ts, current_time)) {
561             /* No expired timers left.  The checkpoint can be skipped
562              * if no timers fired or they were all external.
563              */
564             break;
565         }
566         /* Checkpoint for virtual clock is redundant in cases where
567          * it's being triggered with only non-EXTERNAL timers, because
568          * these timers don't change guest state directly.
569          */
570         if (replay_mode != REPLAY_MODE_NONE
571             && timer_list->clock->type == QEMU_CLOCK_VIRTUAL
572             && !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL)
573             && !replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) {
574             qemu_mutex_unlock(&timer_list->active_timers_lock);
575             goto out;
576         }
577 
578         /* remove timer from the list before calling the callback */
579         timer_list->active_timers = ts->next;
580         ts->next = NULL;
581         ts->expire_time = -1;
582         cb = ts->cb;
583         opaque = ts->opaque;
584 
585         /* run the callback (the timer list can be modified) */
586         qemu_mutex_unlock(&timer_list->active_timers_lock);
587         cb(opaque);
588         qemu_mutex_lock(&timer_list->active_timers_lock);
589 
590         progress = true;
591     }
592     qemu_mutex_unlock(&timer_list->active_timers_lock);
593 
594 out:
595     qemu_event_set(&timer_list->timers_done_ev);
596     return progress;
597 }
598 
599 bool qemu_clock_run_timers(QEMUClockType type)
600 {
601     return timerlist_run_timers(main_loop_tlg.tl[type]);
602 }
603 
604 void timerlistgroup_init(QEMUTimerListGroup *tlg,
605                          QEMUTimerListNotifyCB *cb, void *opaque)
606 {
607     QEMUClockType type;
608     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
609         tlg->tl[type] = timerlist_new(type, cb, opaque);
610     }
611 }
612 
613 void 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 
621 bool 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 
631 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
632 {
633     int64_t deadline = -1;
634     QEMUClockType type;
635     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
636         if (qemu_clock_use_for_deadline(type)) {
637             deadline = qemu_soonest_timeout(deadline,
638                                             timerlist_deadline_ns(tlg->tl[type]));
639         }
640     }
641     return deadline;
642 }
643 
644 int64_t qemu_clock_get_ns(QEMUClockType type)
645 {
646     switch (type) {
647     case QEMU_CLOCK_REALTIME:
648         return get_clock();
649     default:
650     case QEMU_CLOCK_VIRTUAL:
651         return cpus_get_virtual_clock();
652     case QEMU_CLOCK_HOST:
653         return REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime());
654     case QEMU_CLOCK_VIRTUAL_RT:
655         return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock());
656     }
657 }
658 
659 void init_clocks(QEMUTimerListNotifyCB *notify_cb)
660 {
661     QEMUClockType type;
662     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
663         qemu_clock_init(type, notify_cb);
664     }
665 
666 #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
667     prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
668 #endif
669 }
670 
671 uint64_t timer_expire_time_ns(QEMUTimer *ts)
672 {
673     return timer_pending(ts) ? ts->expire_time : -1;
674 }
675 
676 bool qemu_clock_run_all_timers(void)
677 {
678     bool progress = false;
679     QEMUClockType type;
680 
681     for (type = 0; type < QEMU_CLOCK_MAX; type++) {
682         if (qemu_clock_use_for_deadline(type)) {
683             progress |= qemu_clock_run_timers(type);
684         }
685     }
686 
687     return progress;
688 }
689