2
0

qemu-timer.c 19 KB

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