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