qemu-timer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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 "sysemu/cpu-timers.h"
  29. #include "sysemu/replay.h"
  30. #include "sysemu/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. /* Interrupt execution to force deadline recalculation. */
  356. if (icount_enabled() && timer_list->clock->type == QEMU_CLOCK_VIRTUAL) {
  357. icount_start_warp_timer();
  358. }
  359. timerlist_notify(timer_list);
  360. }
  361. /* stop a timer, but do not dealloc it */
  362. void timer_del(QEMUTimer *ts)
  363. {
  364. QEMUTimerList *timer_list = ts->timer_list;
  365. if (timer_list) {
  366. qemu_mutex_lock(&timer_list->active_timers_lock);
  367. timer_del_locked(timer_list, ts);
  368. qemu_mutex_unlock(&timer_list->active_timers_lock);
  369. }
  370. }
  371. /* modify the current timer so that it will be fired when current_time
  372. >= expire_time. The corresponding callback will be called. */
  373. void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
  374. {
  375. QEMUTimerList *timer_list = ts->timer_list;
  376. bool rearm;
  377. qemu_mutex_lock(&timer_list->active_timers_lock);
  378. timer_del_locked(timer_list, ts);
  379. rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
  380. qemu_mutex_unlock(&timer_list->active_timers_lock);
  381. if (rearm) {
  382. timerlist_rearm(timer_list);
  383. }
  384. }
  385. /* modify the current timer so that it will be fired when current_time
  386. >= expire_time or the current deadline, whichever comes earlier.
  387. The corresponding callback will be called. */
  388. void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
  389. {
  390. QEMUTimerList *timer_list = ts->timer_list;
  391. bool rearm = false;
  392. WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) {
  393. if (ts->expire_time == -1 || ts->expire_time > expire_time) {
  394. if (ts->expire_time != -1) {
  395. timer_del_locked(timer_list, ts);
  396. }
  397. rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
  398. } else {
  399. rearm = false;
  400. }
  401. }
  402. if (rearm) {
  403. timerlist_rearm(timer_list);
  404. }
  405. }
  406. void timer_mod(QEMUTimer *ts, int64_t expire_time)
  407. {
  408. timer_mod_ns(ts, expire_time * ts->scale);
  409. }
  410. void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
  411. {
  412. timer_mod_anticipate_ns(ts, expire_time * ts->scale);
  413. }
  414. bool timer_pending(QEMUTimer *ts)
  415. {
  416. return ts->expire_time >= 0;
  417. }
  418. bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
  419. {
  420. return timer_expired_ns(timer_head, current_time * timer_head->scale);
  421. }
  422. bool timerlist_run_timers(QEMUTimerList *timer_list)
  423. {
  424. QEMUTimer *ts;
  425. int64_t current_time;
  426. bool progress = false;
  427. QEMUTimerCB *cb;
  428. void *opaque;
  429. if (!qatomic_read(&timer_list->active_timers)) {
  430. return false;
  431. }
  432. qemu_event_reset(&timer_list->timers_done_ev);
  433. if (!timer_list->clock->enabled) {
  434. goto out;
  435. }
  436. switch (timer_list->clock->type) {
  437. case QEMU_CLOCK_REALTIME:
  438. break;
  439. default:
  440. case QEMU_CLOCK_VIRTUAL:
  441. break;
  442. case QEMU_CLOCK_HOST:
  443. if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST)) {
  444. goto out;
  445. }
  446. break;
  447. case QEMU_CLOCK_VIRTUAL_RT:
  448. if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) {
  449. goto out;
  450. }
  451. break;
  452. }
  453. /*
  454. * Extract expired timers from active timers list and process them.
  455. *
  456. * In rr mode we need "filtered" checkpointing for virtual clock. The
  457. * checkpoint must be recorded/replayed before processing any non-EXTERNAL timer,
  458. * and that must only be done once since the clock value stays the same. Because
  459. * non-EXTERNAL timers may appear in the timers list while it being processed,
  460. * the checkpoint can be issued at a time until no timers are left and we are
  461. * done".
  462. */
  463. current_time = qemu_clock_get_ns(timer_list->clock->type);
  464. qemu_mutex_lock(&timer_list->active_timers_lock);
  465. while ((ts = timer_list->active_timers)) {
  466. if (!timer_expired_ns(ts, current_time)) {
  467. /* No expired timers left. The checkpoint can be skipped
  468. * if no timers fired or they were all external.
  469. */
  470. break;
  471. }
  472. /* Checkpoint for virtual clock is redundant in cases where
  473. * it's being triggered with only non-EXTERNAL timers, because
  474. * these timers don't change guest state directly.
  475. */
  476. if (replay_mode != REPLAY_MODE_NONE
  477. && timer_list->clock->type == QEMU_CLOCK_VIRTUAL
  478. && !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL)
  479. && !replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) {
  480. qemu_mutex_unlock(&timer_list->active_timers_lock);
  481. goto out;
  482. }
  483. /* remove timer from the list before calling the callback */
  484. timer_list->active_timers = ts->next;
  485. ts->next = NULL;
  486. ts->expire_time = -1;
  487. cb = ts->cb;
  488. opaque = ts->opaque;
  489. /* run the callback (the timer list can be modified) */
  490. qemu_mutex_unlock(&timer_list->active_timers_lock);
  491. cb(opaque);
  492. qemu_mutex_lock(&timer_list->active_timers_lock);
  493. progress = true;
  494. }
  495. qemu_mutex_unlock(&timer_list->active_timers_lock);
  496. out:
  497. qemu_event_set(&timer_list->timers_done_ev);
  498. return progress;
  499. }
  500. bool qemu_clock_run_timers(QEMUClockType type)
  501. {
  502. return timerlist_run_timers(main_loop_tlg.tl[type]);
  503. }
  504. void timerlistgroup_init(QEMUTimerListGroup *tlg,
  505. QEMUTimerListNotifyCB *cb, void *opaque)
  506. {
  507. QEMUClockType type;
  508. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  509. tlg->tl[type] = timerlist_new(type, cb, opaque);
  510. }
  511. }
  512. void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
  513. {
  514. QEMUClockType type;
  515. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  516. timerlist_free(tlg->tl[type]);
  517. }
  518. }
  519. bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
  520. {
  521. QEMUClockType type;
  522. bool progress = false;
  523. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  524. progress |= timerlist_run_timers(tlg->tl[type]);
  525. }
  526. return progress;
  527. }
  528. int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
  529. {
  530. int64_t deadline = -1;
  531. QEMUClockType type;
  532. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  533. if (qemu_clock_use_for_deadline(type)) {
  534. deadline = qemu_soonest_timeout(deadline,
  535. timerlist_deadline_ns(tlg->tl[type]));
  536. }
  537. }
  538. return deadline;
  539. }
  540. int64_t qemu_clock_get_ns(QEMUClockType type)
  541. {
  542. switch (type) {
  543. case QEMU_CLOCK_REALTIME:
  544. return get_clock();
  545. default:
  546. case QEMU_CLOCK_VIRTUAL:
  547. return cpus_get_virtual_clock();
  548. case QEMU_CLOCK_HOST:
  549. return REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime());
  550. case QEMU_CLOCK_VIRTUAL_RT:
  551. return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock());
  552. }
  553. }
  554. static void qemu_virtual_clock_set_ns(int64_t time)
  555. {
  556. return cpus_set_virtual_clock(time);
  557. }
  558. void init_clocks(QEMUTimerListNotifyCB *notify_cb)
  559. {
  560. QEMUClockType type;
  561. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  562. qemu_clock_init(type, notify_cb);
  563. }
  564. #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
  565. prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
  566. #endif
  567. }
  568. uint64_t timer_expire_time_ns(QEMUTimer *ts)
  569. {
  570. return timer_pending(ts) ? ts->expire_time : -1;
  571. }
  572. bool qemu_clock_run_all_timers(void)
  573. {
  574. bool progress = false;
  575. QEMUClockType type;
  576. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  577. if (qemu_clock_use_for_deadline(type)) {
  578. progress |= qemu_clock_run_timers(type);
  579. }
  580. }
  581. return progress;
  582. }
  583. int64_t qemu_clock_advance_virtual_time(int64_t dest)
  584. {
  585. int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  586. AioContext *aio_context;
  587. int64_t deadline;
  588. aio_context = qemu_get_aio_context();
  589. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
  590. QEMU_TIMER_ATTR_ALL);
  591. /*
  592. * A deadline of < 0 indicates this timer is not enabled, so we
  593. * won't get far trying to run it forward.
  594. */
  595. while (deadline >= 0 && clock < dest) {
  596. int64_t warp = qemu_soonest_timeout(dest - clock, deadline);
  597. qemu_virtual_clock_set_ns(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + warp);
  598. qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL);
  599. timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]);
  600. clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  601. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
  602. QEMU_TIMER_ATTR_ALL);
  603. }
  604. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  605. return clock;
  606. }