2
0

qemu-timer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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/sysemu.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. NotifierList reset_notifiers;
  44. int64_t last;
  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_malloc0(sizeof(QEMUTimerList));
  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)
  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 = true;
  113. clock->last = INT64_MIN;
  114. QLIST_INIT(&clock->timerlists);
  115. notifier_list_init(&clock->reset_notifiers);
  116. main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL);
  117. }
  118. bool qemu_clock_use_for_deadline(QEMUClockType type)
  119. {
  120. return !(use_icount && (type == QEMU_CLOCK_VIRTUAL));
  121. }
  122. void qemu_clock_notify(QEMUClockType type)
  123. {
  124. QEMUTimerList *timer_list;
  125. QEMUClock *clock = qemu_clock_ptr(type);
  126. QLIST_FOREACH(timer_list, &clock->timerlists, list) {
  127. timerlist_notify(timer_list);
  128. }
  129. }
  130. /* Disabling the clock will wait for related timerlists to stop
  131. * executing qemu_run_timers. Thus, this functions should not
  132. * be used from the callback of a timer that is based on @clock.
  133. * Doing so would cause a deadlock.
  134. *
  135. * Caller should hold BQL.
  136. */
  137. void qemu_clock_enable(QEMUClockType type, bool enabled)
  138. {
  139. QEMUClock *clock = qemu_clock_ptr(type);
  140. QEMUTimerList *tl;
  141. bool old = clock->enabled;
  142. clock->enabled = enabled;
  143. if (enabled && !old) {
  144. qemu_clock_notify(type);
  145. } else if (!enabled && old) {
  146. QLIST_FOREACH(tl, &clock->timerlists, list) {
  147. qemu_event_wait(&tl->timers_done_ev);
  148. }
  149. }
  150. }
  151. bool timerlist_has_timers(QEMUTimerList *timer_list)
  152. {
  153. return !!timer_list->active_timers;
  154. }
  155. bool qemu_clock_has_timers(QEMUClockType type)
  156. {
  157. return timerlist_has_timers(
  158. main_loop_tlg.tl[type]);
  159. }
  160. bool timerlist_expired(QEMUTimerList *timer_list)
  161. {
  162. int64_t expire_time;
  163. qemu_mutex_lock(&timer_list->active_timers_lock);
  164. if (!timer_list->active_timers) {
  165. qemu_mutex_unlock(&timer_list->active_timers_lock);
  166. return false;
  167. }
  168. expire_time = timer_list->active_timers->expire_time;
  169. qemu_mutex_unlock(&timer_list->active_timers_lock);
  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;
  185. if (!timer_list->clock->enabled) {
  186. return -1;
  187. }
  188. /* The active timers list may be modified before the caller uses our return
  189. * value but ->notify_cb() is called when the deadline changes. Therefore
  190. * the caller should notice the change and there is no race condition.
  191. */
  192. qemu_mutex_lock(&timer_list->active_timers_lock);
  193. if (!timer_list->active_timers) {
  194. qemu_mutex_unlock(&timer_list->active_timers_lock);
  195. return -1;
  196. }
  197. expire_time = timer_list->active_timers->expire_time;
  198. qemu_mutex_unlock(&timer_list->active_timers_lock);
  199. delta = expire_time - qemu_clock_get_ns(timer_list->clock->type);
  200. if (delta <= 0) {
  201. return 0;
  202. }
  203. return delta;
  204. }
  205. /* Calculate the soonest deadline across all timerlists attached
  206. * to the clock. This is used for the icount timeout so we
  207. * ignore whether or not the clock should be used in deadline
  208. * calculations.
  209. */
  210. int64_t qemu_clock_deadline_ns_all(QEMUClockType type)
  211. {
  212. int64_t deadline = -1;
  213. QEMUTimerList *timer_list;
  214. QEMUClock *clock = qemu_clock_ptr(type);
  215. QLIST_FOREACH(timer_list, &clock->timerlists, list) {
  216. deadline = qemu_soonest_timeout(deadline,
  217. timerlist_deadline_ns(timer_list));
  218. }
  219. return deadline;
  220. }
  221. QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list)
  222. {
  223. return timer_list->clock->type;
  224. }
  225. QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type)
  226. {
  227. return main_loop_tlg.tl[type];
  228. }
  229. void timerlist_notify(QEMUTimerList *timer_list)
  230. {
  231. if (timer_list->notify_cb) {
  232. timer_list->notify_cb(timer_list->notify_opaque);
  233. } else {
  234. qemu_notify_event();
  235. }
  236. }
  237. /* Transition function to convert a nanosecond timeout to ms
  238. * This is used where a system does not support ppoll
  239. */
  240. int qemu_timeout_ns_to_ms(int64_t ns)
  241. {
  242. int64_t ms;
  243. if (ns < 0) {
  244. return -1;
  245. }
  246. if (!ns) {
  247. return 0;
  248. }
  249. /* Always round up, because it's better to wait too long than to wait too
  250. * little and effectively busy-wait
  251. */
  252. ms = (ns + SCALE_MS - 1) / SCALE_MS;
  253. /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
  254. if (ms > (int64_t) INT32_MAX) {
  255. ms = INT32_MAX;
  256. }
  257. return (int) ms;
  258. }
  259. /* qemu implementation of g_poll which uses a nanosecond timeout but is
  260. * otherwise identical to g_poll
  261. */
  262. int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
  263. {
  264. #ifdef CONFIG_PPOLL
  265. if (timeout < 0) {
  266. return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
  267. } else {
  268. struct timespec ts;
  269. int64_t tvsec = timeout / 1000000000LL;
  270. /* Avoid possibly overflowing and specifying a negative number of
  271. * seconds, which would turn a very long timeout into a busy-wait.
  272. */
  273. if (tvsec > (int64_t)INT32_MAX) {
  274. tvsec = INT32_MAX;
  275. }
  276. ts.tv_sec = tvsec;
  277. ts.tv_nsec = timeout % 1000000000LL;
  278. return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
  279. }
  280. #else
  281. return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
  282. #endif
  283. }
  284. void timer_init_tl(QEMUTimer *ts,
  285. QEMUTimerList *timer_list, int scale,
  286. QEMUTimerCB *cb, void *opaque)
  287. {
  288. ts->timer_list = timer_list;
  289. ts->cb = cb;
  290. ts->opaque = opaque;
  291. ts->scale = scale;
  292. ts->expire_time = -1;
  293. }
  294. void timer_deinit(QEMUTimer *ts)
  295. {
  296. assert(ts->expire_time == -1);
  297. ts->timer_list = NULL;
  298. }
  299. void timer_free(QEMUTimer *ts)
  300. {
  301. g_free(ts);
  302. }
  303. static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
  304. {
  305. QEMUTimer **pt, *t;
  306. ts->expire_time = -1;
  307. pt = &timer_list->active_timers;
  308. for(;;) {
  309. t = *pt;
  310. if (!t)
  311. break;
  312. if (t == ts) {
  313. *pt = t->next;
  314. break;
  315. }
  316. pt = &t->next;
  317. }
  318. }
  319. static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
  320. QEMUTimer *ts, int64_t expire_time)
  321. {
  322. QEMUTimer **pt, *t;
  323. /* add the timer in the sorted list */
  324. pt = &timer_list->active_timers;
  325. for (;;) {
  326. t = *pt;
  327. if (!timer_expired_ns(t, expire_time)) {
  328. break;
  329. }
  330. pt = &t->next;
  331. }
  332. ts->expire_time = MAX(expire_time, 0);
  333. ts->next = *pt;
  334. *pt = ts;
  335. return pt == &timer_list->active_timers;
  336. }
  337. static void timerlist_rearm(QEMUTimerList *timer_list)
  338. {
  339. /* Interrupt execution to force deadline recalculation. */
  340. qemu_clock_warp(timer_list->clock->type);
  341. timerlist_notify(timer_list);
  342. }
  343. /* stop a timer, but do not dealloc it */
  344. void timer_del(QEMUTimer *ts)
  345. {
  346. QEMUTimerList *timer_list = ts->timer_list;
  347. if (timer_list) {
  348. qemu_mutex_lock(&timer_list->active_timers_lock);
  349. timer_del_locked(timer_list, ts);
  350. qemu_mutex_unlock(&timer_list->active_timers_lock);
  351. }
  352. }
  353. /* modify the current timer so that it will be fired when current_time
  354. >= expire_time. The corresponding callback will be called. */
  355. void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
  356. {
  357. QEMUTimerList *timer_list = ts->timer_list;
  358. bool rearm;
  359. qemu_mutex_lock(&timer_list->active_timers_lock);
  360. timer_del_locked(timer_list, ts);
  361. rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
  362. qemu_mutex_unlock(&timer_list->active_timers_lock);
  363. if (rearm) {
  364. timerlist_rearm(timer_list);
  365. }
  366. }
  367. /* modify the current timer so that it will be fired when current_time
  368. >= expire_time or the current deadline, whichever comes earlier.
  369. The corresponding callback will be called. */
  370. void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
  371. {
  372. QEMUTimerList *timer_list = ts->timer_list;
  373. bool rearm;
  374. qemu_mutex_lock(&timer_list->active_timers_lock);
  375. if (ts->expire_time == -1 || ts->expire_time > expire_time) {
  376. if (ts->expire_time != -1) {
  377. timer_del_locked(timer_list, ts);
  378. }
  379. rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
  380. } else {
  381. rearm = false;
  382. }
  383. qemu_mutex_unlock(&timer_list->active_timers_lock);
  384. if (rearm) {
  385. timerlist_rearm(timer_list);
  386. }
  387. }
  388. void timer_mod(QEMUTimer *ts, int64_t expire_time)
  389. {
  390. timer_mod_ns(ts, expire_time * ts->scale);
  391. }
  392. void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
  393. {
  394. timer_mod_anticipate_ns(ts, expire_time * ts->scale);
  395. }
  396. bool timer_pending(QEMUTimer *ts)
  397. {
  398. return ts->expire_time >= 0;
  399. }
  400. bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
  401. {
  402. return timer_expired_ns(timer_head, current_time * timer_head->scale);
  403. }
  404. bool timerlist_run_timers(QEMUTimerList *timer_list)
  405. {
  406. QEMUTimer *ts;
  407. int64_t current_time;
  408. bool progress = false;
  409. QEMUTimerCB *cb;
  410. void *opaque;
  411. qemu_event_reset(&timer_list->timers_done_ev);
  412. if (!timer_list->clock->enabled || !timer_list->active_timers) {
  413. goto out;
  414. }
  415. switch (timer_list->clock->type) {
  416. case QEMU_CLOCK_REALTIME:
  417. break;
  418. default:
  419. case QEMU_CLOCK_VIRTUAL:
  420. if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) {
  421. goto out;
  422. }
  423. break;
  424. case QEMU_CLOCK_HOST:
  425. if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST)) {
  426. goto out;
  427. }
  428. break;
  429. case QEMU_CLOCK_VIRTUAL_RT:
  430. if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) {
  431. goto out;
  432. }
  433. break;
  434. }
  435. current_time = qemu_clock_get_ns(timer_list->clock->type);
  436. for(;;) {
  437. qemu_mutex_lock(&timer_list->active_timers_lock);
  438. ts = timer_list->active_timers;
  439. if (!timer_expired_ns(ts, current_time)) {
  440. qemu_mutex_unlock(&timer_list->active_timers_lock);
  441. break;
  442. }
  443. /* remove timer from the list before calling the callback */
  444. timer_list->active_timers = ts->next;
  445. ts->next = NULL;
  446. ts->expire_time = -1;
  447. cb = ts->cb;
  448. opaque = ts->opaque;
  449. qemu_mutex_unlock(&timer_list->active_timers_lock);
  450. /* run the callback (the timer list can be modified) */
  451. cb(opaque);
  452. progress = true;
  453. }
  454. out:
  455. qemu_event_set(&timer_list->timers_done_ev);
  456. return progress;
  457. }
  458. bool qemu_clock_run_timers(QEMUClockType type)
  459. {
  460. return timerlist_run_timers(main_loop_tlg.tl[type]);
  461. }
  462. void timerlistgroup_init(QEMUTimerListGroup *tlg,
  463. QEMUTimerListNotifyCB *cb, void *opaque)
  464. {
  465. QEMUClockType type;
  466. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  467. tlg->tl[type] = timerlist_new(type, cb, opaque);
  468. }
  469. }
  470. void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
  471. {
  472. QEMUClockType type;
  473. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  474. timerlist_free(tlg->tl[type]);
  475. }
  476. }
  477. bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
  478. {
  479. QEMUClockType type;
  480. bool progress = false;
  481. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  482. progress |= timerlist_run_timers(tlg->tl[type]);
  483. }
  484. return progress;
  485. }
  486. int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
  487. {
  488. int64_t deadline = -1;
  489. QEMUClockType type;
  490. bool play = replay_mode == REPLAY_MODE_PLAY;
  491. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  492. if (qemu_clock_use_for_deadline(type)) {
  493. if (!play || type == QEMU_CLOCK_REALTIME) {
  494. deadline = qemu_soonest_timeout(deadline,
  495. timerlist_deadline_ns(tlg->tl[type]));
  496. } else {
  497. /* Read clock from the replay file and
  498. do not calculate the deadline, based on virtual clock. */
  499. qemu_clock_get_ns(type);
  500. }
  501. }
  502. }
  503. return deadline;
  504. }
  505. int64_t qemu_clock_get_ns(QEMUClockType type)
  506. {
  507. int64_t now, last;
  508. QEMUClock *clock = qemu_clock_ptr(type);
  509. switch (type) {
  510. case QEMU_CLOCK_REALTIME:
  511. return get_clock();
  512. default:
  513. case QEMU_CLOCK_VIRTUAL:
  514. if (use_icount) {
  515. return cpu_get_icount();
  516. } else {
  517. return cpu_get_clock();
  518. }
  519. case QEMU_CLOCK_HOST:
  520. now = REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime());
  521. last = clock->last;
  522. clock->last = now;
  523. if (now < last || now > (last + get_max_clock_jump())) {
  524. notifier_list_notify(&clock->reset_notifiers, &now);
  525. }
  526. return now;
  527. case QEMU_CLOCK_VIRTUAL_RT:
  528. return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock());
  529. }
  530. }
  531. void qemu_clock_register_reset_notifier(QEMUClockType type,
  532. Notifier *notifier)
  533. {
  534. QEMUClock *clock = qemu_clock_ptr(type);
  535. notifier_list_add(&clock->reset_notifiers, notifier);
  536. }
  537. void qemu_clock_unregister_reset_notifier(QEMUClockType type,
  538. Notifier *notifier)
  539. {
  540. notifier_remove(notifier);
  541. }
  542. void init_clocks(void)
  543. {
  544. QEMUClockType type;
  545. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  546. qemu_clock_init(type);
  547. }
  548. #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
  549. prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
  550. #endif
  551. }
  552. uint64_t timer_expire_time_ns(QEMUTimer *ts)
  553. {
  554. return timer_pending(ts) ? ts->expire_time : -1;
  555. }
  556. bool qemu_clock_run_all_timers(void)
  557. {
  558. bool progress = false;
  559. QEMUClockType type;
  560. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  561. progress |= qemu_clock_run_timers(type);
  562. }
  563. return progress;
  564. }