qemu-timer.c 19 KB

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