Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 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 | |
Peter Maydell | 1ac0206 | 2015-01-20 16:16:40 +0000 | [diff] [blame] | 25 | #include "qemu/main-loop.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 26 | #include "qemu/timer.h" |
Pavel Dovgalyuk | 8eda206 | 2015-09-17 19:24:28 +0300 | [diff] [blame] | 27 | #include "sysemu/replay.h" |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 28 | #include "sysemu/sysemu.h" |
Peter Maydell | 1ac0206 | 2015-01-20 16:16:40 +0000 | [diff] [blame] | 29 | |
Anthony Liguori | 30ea833 | 2012-11-02 16:12:53 -0500 | [diff] [blame] | 30 | #ifdef CONFIG_POSIX |
| 31 | #include <pthread.h> |
| 32 | #endif |
Stefan Weil | bff9f8b | 2012-04-20 10:27:06 +0200 | [diff] [blame] | 33 | |
Alex Bligh | 4e0c652 | 2013-08-21 16:02:43 +0100 | [diff] [blame] | 34 | #ifdef CONFIG_PPOLL |
| 35 | #include <poll.h> |
| 36 | #endif |
| 37 | |
Alex Bligh | cd758dd | 2013-08-21 16:02:44 +0100 | [diff] [blame] | 38 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
| 39 | #include <sys/prctl.h> |
| 40 | #endif |
| 41 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 42 | /***********************************************************/ |
| 43 | /* timers */ |
| 44 | |
Alex Bligh | b4049b7 | 2013-08-21 16:03:09 +0100 | [diff] [blame] | 45 | typedef struct QEMUClock { |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 46 | /* We rely on BQL to protect the timerlists */ |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 47 | QLIST_HEAD(, QEMUTimerList) timerlists; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 48 | |
| 49 | NotifierList reset_notifiers; |
| 50 | int64_t last; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 51 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 52 | QEMUClockType type; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 53 | bool enabled; |
Alex Bligh | b4049b7 | 2013-08-21 16:03:09 +0100 | [diff] [blame] | 54 | } QEMUClock; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 55 | |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 56 | QEMUTimerListGroup main_loop_tlg; |
Stefan Weil | fbdb664 | 2014-05-03 08:12:15 +0200 | [diff] [blame] | 57 | static QEMUClock qemu_clocks[QEMU_CLOCK_MAX]; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 58 | |
| 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 | |
| 66 | struct QEMUTimerList { |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 67 | QEMUClock *clock; |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 68 | QemuMutex active_timers_lock; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 69 | QEMUTimer *active_timers; |
| 70 | QLIST_ENTRY(QEMUTimerList) list; |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 71 | QEMUTimerListNotifyCB *notify_cb; |
| 72 | void *notify_opaque; |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 73 | |
| 74 | /* lightweight method to mark the end of timerlist's running */ |
| 75 | QemuEvent timers_done_ev; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 78 | /** |
| 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 Bligh | b4049b7 | 2013-08-21 16:03:09 +0100 | [diff] [blame] | 86 | static inline QEMUClock *qemu_clock_ptr(QEMUClockType type) |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 87 | { |
| 88 | return &qemu_clocks[type]; |
| 89 | } |
| 90 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 91 | static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time) |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 92 | { |
| 93 | return timer_head && (timer_head->expire_time <= current_time); |
| 94 | } |
| 95 | |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 96 | QEMUTimerList *timerlist_new(QEMUClockType type, |
| 97 | QEMUTimerListNotifyCB *cb, |
| 98 | void *opaque) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 99 | { |
| 100 | QEMUTimerList *timer_list; |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 101 | QEMUClock *clock = qemu_clock_ptr(type); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 102 | |
| 103 | timer_list = g_malloc0(sizeof(QEMUTimerList)); |
Paolo Bonzini | e4efd8a | 2015-07-21 16:07:48 +0200 | [diff] [blame] | 104 | qemu_event_init(&timer_list->timers_done_ev, true); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 105 | timer_list->clock = clock; |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 106 | timer_list->notify_cb = cb; |
| 107 | timer_list->notify_opaque = opaque; |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 108 | qemu_mutex_init(&timer_list->active_timers_lock); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 109 | QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list); |
| 110 | return timer_list; |
| 111 | } |
| 112 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 113 | void 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 Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 118 | } |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 119 | qemu_mutex_destroy(&timer_list->active_timers_lock); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 120 | g_free(timer_list); |
| 121 | } |
| 122 | |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 123 | static void qemu_clock_init(QEMUClockType type) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 124 | { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 125 | QEMUClock *clock = qemu_clock_ptr(type); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 126 | |
Kirill Batuzov | 02ce232 | 2014-05-06 16:59:53 +0400 | [diff] [blame] | 127 | /* Assert that the clock of type TYPE has not been initialized yet. */ |
| 128 | assert(main_loop_tlg.tl[type] == NULL); |
| 129 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 130 | clock->type = type; |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 131 | clock->enabled = true; |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 132 | clock->last = INT64_MIN; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 133 | QLIST_INIT(&clock->timerlists); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 134 | notifier_list_init(&clock->reset_notifiers); |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 135 | main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 138 | bool qemu_clock_use_for_deadline(QEMUClockType type) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 139 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 140 | return !(use_icount && (type == QEMU_CLOCK_VIRTUAL)); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 143 | void qemu_clock_notify(QEMUClockType type) |
Alex Bligh | b1bbfe7 | 2013-08-21 16:02:55 +0100 | [diff] [blame] | 144 | { |
| 145 | QEMUTimerList *timer_list; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 146 | QEMUClock *clock = qemu_clock_ptr(type); |
Alex Bligh | b1bbfe7 | 2013-08-21 16:02:55 +0100 | [diff] [blame] | 147 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { |
| 148 | timerlist_notify(timer_list); |
| 149 | } |
| 150 | } |
| 151 | |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 152 | /* 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 Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 159 | void qemu_clock_enable(QEMUClockType type, bool enabled) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 160 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 161 | QEMUClock *clock = qemu_clock_ptr(type); |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 162 | QEMUTimerList *tl; |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 163 | bool old = clock->enabled; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 164 | clock->enabled = enabled; |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 165 | if (enabled && !old) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 166 | qemu_clock_notify(type); |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 167 | } else if (!enabled && old) { |
| 168 | QLIST_FOREACH(tl, &clock->timerlists, list) { |
| 169 | qemu_event_wait(&tl->timers_done_ev); |
| 170 | } |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 171 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 174 | bool timerlist_has_timers(QEMUTimerList *timer_list) |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 175 | { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 176 | return !!timer_list->active_timers; |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 179 | bool qemu_clock_has_timers(QEMUClockType type) |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 180 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 181 | return timerlist_has_timers( |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 182 | main_loop_tlg.tl[type]); |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 183 | } |
| 184 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 185 | bool timerlist_expired(QEMUTimerList *timer_list) |
| 186 | { |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 187 | 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 Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 198 | } |
| 199 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 200 | bool qemu_clock_expired(QEMUClockType type) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 201 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 202 | return timerlist_expired( |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 203 | main_loop_tlg.tl[type]); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 206 | /* |
| 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 Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 211 | int64_t timerlist_deadline_ns(QEMUTimerList *timer_list) |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 212 | { |
| 213 | int64_t delta; |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 214 | int64_t expire_time; |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 215 | |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 216 | if (!timer_list->clock->enabled) { |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 220 | /* 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 Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 233 | |
| 234 | if (delta <= 0) { |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | return delta; |
| 239 | } |
| 240 | |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 241 | /* 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 Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 246 | int64_t qemu_clock_deadline_ns_all(QEMUClockType type) |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 247 | { |
| 248 | int64_t deadline = -1; |
| 249 | QEMUTimerList *timer_list; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 250 | QEMUClock *clock = qemu_clock_ptr(type); |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 251 | 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 Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 258 | QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 259 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 260 | return timer_list->clock->type; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 263 | QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 264 | { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 265 | return main_loop_tlg.tl[type]; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 266 | } |
| 267 | |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 268 | void 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 Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 277 | /* Transition function to convert a nanosecond timeout to ms |
| 278 | * This is used where a system does not support ppoll |
| 279 | */ |
| 280 | int 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 Bligh | 4e0c652 | 2013-08-21 16:02:43 +0100 | [diff] [blame] | 305 | /* qemu implementation of g_poll which uses a nanosecond timeout but is |
| 306 | * otherwise identical to g_poll |
| 307 | */ |
| 308 | int 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 Maydell | 490309f | 2014-11-25 18:21:45 +0000 | [diff] [blame] | 315 | 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 Bligh | 4e0c652 | 2013-08-21 16:02:43 +0100 | [diff] [blame] | 323 | 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 Bonzini | f186aa9 | 2014-12-23 21:54:14 +0100 | [diff] [blame] | 332 | void timer_init_tl(QEMUTimer *ts, |
| 333 | QEMUTimerList *timer_list, int scale, |
| 334 | QEMUTimerCB *cb, void *opaque) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 335 | { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 336 | ts->timer_list = timer_list; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 337 | ts->cb = cb; |
| 338 | ts->opaque = opaque; |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 339 | ts->scale = scale; |
Paolo Bonzini | 3db1ee7 | 2013-09-12 11:02:20 +0200 | [diff] [blame] | 340 | ts->expire_time = -1; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Paolo Bonzini | cd1bd53 | 2014-12-24 10:57:04 +0100 | [diff] [blame] | 343 | void timer_deinit(QEMUTimer *ts) |
| 344 | { |
| 345 | assert(ts->expire_time == -1); |
| 346 | ts->timer_list = NULL; |
| 347 | } |
| 348 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 349 | void timer_free(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 350 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 351 | g_free(ts); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 352 | } |
| 353 | |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 354 | static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 355 | { |
| 356 | QEMUTimer **pt, *t; |
| 357 | |
Paolo Bonzini | 3db1ee7 | 2013-09-12 11:02:20 +0200 | [diff] [blame] | 358 | ts->expire_time = -1; |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 359 | pt = &timer_list->active_timers; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 360 | 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 Bonzini | 0f809e5 | 2013-10-03 15:06:39 +0200 | [diff] [blame] | 372 | static 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 | |
| 393 | static 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 Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 400 | /* stop a timer, but do not dealloc it */ |
| 401 | void timer_del(QEMUTimer *ts) |
| 402 | { |
| 403 | QEMUTimerList *timer_list = ts->timer_list; |
| 404 | |
Paolo Bonzini | cd1bd53 | 2014-12-24 10:57:04 +0100 | [diff] [blame] | 405 | 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 Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 412 | /* modify the current timer so that it will be fired when current_time |
| 413 | >= expire_time. The corresponding callback will be called. */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 414 | void timer_mod_ns(QEMUTimer *ts, int64_t expire_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 415 | { |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 416 | QEMUTimerList *timer_list = ts->timer_list; |
Paolo Bonzini | 0f809e5 | 2013-10-03 15:06:39 +0200 | [diff] [blame] | 417 | bool rearm; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 418 | |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 419 | qemu_mutex_lock(&timer_list->active_timers_lock); |
| 420 | timer_del_locked(timer_list, ts); |
Paolo Bonzini | 0f809e5 | 2013-10-03 15:06:39 +0200 | [diff] [blame] | 421 | rearm = timer_mod_ns_locked(timer_list, ts, expire_time); |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 422 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 423 | |
Paolo Bonzini | 0f809e5 | 2013-10-03 15:06:39 +0200 | [diff] [blame] | 424 | if (rearm) { |
| 425 | timerlist_rearm(timer_list); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Paolo Bonzini | add40e9 | 2013-10-03 15:11:43 +0200 | [diff] [blame] | 429 | /* 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. */ |
| 432 | void 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 Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 453 | void timer_mod(QEMUTimer *ts, int64_t expire_time) |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 454 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 455 | timer_mod_ns(ts, expire_time * ts->scale); |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 456 | } |
| 457 | |
Paolo Bonzini | add40e9 | 2013-10-03 15:11:43 +0200 | [diff] [blame] | 458 | void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time) |
| 459 | { |
| 460 | timer_mod_anticipate_ns(ts, expire_time * ts->scale); |
| 461 | } |
| 462 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 463 | bool timer_pending(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 464 | { |
Paolo Bonzini | 3db1ee7 | 2013-09-12 11:02:20 +0200 | [diff] [blame] | 465 | return ts->expire_time >= 0; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 466 | } |
| 467 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 468 | bool timer_expired(QEMUTimer *timer_head, int64_t current_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 469 | { |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 470 | return timer_expired_ns(timer_head, current_time * timer_head->scale); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 471 | } |
| 472 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 473 | bool timerlist_run_timers(QEMUTimerList *timer_list) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 474 | { |
Paolo Bonzini | 144b97c | 2012-09-19 15:52:44 +0200 | [diff] [blame] | 475 | QEMUTimer *ts; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 476 | int64_t current_time; |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 477 | bool progress = false; |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 478 | QEMUTimerCB *cb; |
| 479 | void *opaque; |
| 480 | |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 481 | qemu_event_reset(&timer_list->timers_done_ev); |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 482 | if (!timer_list->clock->enabled || !timer_list->active_timers) { |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 483 | goto out; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 484 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 485 | |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 486 | 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 Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 507 | current_time = qemu_clock_get_ns(timer_list->clock->type); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 508 | for(;;) { |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 509 | qemu_mutex_lock(&timer_list->active_timers_lock); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 510 | ts = timer_list->active_timers; |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 511 | if (!timer_expired_ns(ts, current_time)) { |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 512 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 513 | break; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 514 | } |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 515 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 516 | /* remove timer from the list before calling the callback */ |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 517 | timer_list->active_timers = ts->next; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 518 | ts->next = NULL; |
Paolo Bonzini | 3db1ee7 | 2013-09-12 11:02:20 +0200 | [diff] [blame] | 519 | ts->expire_time = -1; |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 520 | cb = ts->cb; |
| 521 | opaque = ts->opaque; |
| 522 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 523 | |
| 524 | /* run the callback (the timer list can be modified) */ |
Stefan Hajnoczi | 978f220 | 2013-09-12 11:02:19 +0200 | [diff] [blame] | 525 | cb(opaque); |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 526 | progress = true; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 527 | } |
Liu Ping Fan | 3c05341 | 2013-09-25 14:21:00 +0800 | [diff] [blame] | 528 | |
| 529 | out: |
| 530 | qemu_event_set(&timer_list->timers_done_ev); |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 531 | return progress; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 534 | bool qemu_clock_run_timers(QEMUClockType type) |
| 535 | { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 536 | return timerlist_run_timers(main_loop_tlg.tl[type]); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 539 | void timerlistgroup_init(QEMUTimerListGroup *tlg, |
| 540 | QEMUTimerListNotifyCB *cb, void *opaque) |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 541 | { |
| 542 | QEMUClockType type; |
| 543 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 544 | tlg->tl[type] = timerlist_new(type, cb, opaque); |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | void 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 | |
| 556 | bool 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 | |
| 566 | int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg) |
| 567 | { |
| 568 | int64_t deadline = -1; |
| 569 | QEMUClockType type; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 570 | bool play = replay_mode == REPLAY_MODE_PLAY; |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 571 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 572 | 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 Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | return deadline; |
| 584 | } |
| 585 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 586 | int64_t qemu_clock_get_ns(QEMUClockType type) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 587 | { |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 588 | int64_t now, last; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 589 | QEMUClock *clock = qemu_clock_ptr(type); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 590 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 591 | switch (type) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 592 | 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 Dovgalyuk | 8eda206 | 2015-09-17 19:24:28 +0300 | [diff] [blame] | 602 | now = REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime()); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 603 | last = clock->last; |
| 604 | clock->last = now; |
Pavel Dovgalyuk | 8bd7f71 | 2015-09-17 19:24:44 +0300 | [diff] [blame] | 605 | if (now < last || now > (last + get_max_clock_jump())) { |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 606 | notifier_list_notify(&clock->reset_notifiers, &now); |
| 607 | } |
| 608 | return now; |
Pavel Dovgalyuk | 4e7fa73 | 2014-11-26 13:40:50 +0300 | [diff] [blame] | 609 | case QEMU_CLOCK_VIRTUAL_RT: |
Pavel Dovgalyuk | 8eda206 | 2015-09-17 19:24:28 +0300 | [diff] [blame] | 610 | return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock()); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 614 | void qemu_clock_register_reset_notifier(QEMUClockType type, |
| 615 | Notifier *notifier) |
| 616 | { |
| 617 | QEMUClock *clock = qemu_clock_ptr(type); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 618 | notifier_list_add(&clock->reset_notifiers, notifier); |
| 619 | } |
| 620 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 621 | void qemu_clock_unregister_reset_notifier(QEMUClockType type, |
| 622 | Notifier *notifier) |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 623 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 624 | notifier_remove(notifier); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 625 | } |
| 626 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 627 | void init_clocks(void) |
| 628 | { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 629 | QEMUClockType type; |
| 630 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 631 | qemu_clock_init(type); |
Paolo Bonzini | 744ca8e | 2012-10-29 15:26:28 +0100 | [diff] [blame] | 632 | } |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 633 | |
Alex Bligh | cd758dd | 2013-08-21 16:02:44 +0100 | [diff] [blame] | 634 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
| 635 | prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); |
| 636 | #endif |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 637 | } |
| 638 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 639 | uint64_t timer_expire_time_ns(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 640 | { |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 641 | return timer_pending(ts) ? ts->expire_time : -1; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 642 | } |
| 643 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 644 | bool qemu_clock_run_all_timers(void) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 645 | { |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 646 | bool progress = false; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 647 | QEMUClockType type; |
Alex Bligh | 6d32717 | 2013-08-21 16:02:59 +0100 | [diff] [blame] | 648 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 649 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 650 | progress |= qemu_clock_run_timers(type); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 651 | } |
Peter Portante | 158fd3c | 2012-04-05 11:00:45 -0400 | [diff] [blame] | 652 | |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 653 | return progress; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 654 | } |