qemu-timer.c 16 KB

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