qemu-timer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 "sysemu/sysemu.h"
  25. #include "monitor/monitor.h"
  26. #include "ui/console.h"
  27. #include "hw/hw.h"
  28. #include "qemu/timer.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, false);
  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. ts.tv_sec = timeout / 1000000000LL;
  270. ts.tv_nsec = timeout % 1000000000LL;
  271. return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
  272. }
  273. #else
  274. return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
  275. #endif
  276. }
  277. void timer_init(QEMUTimer *ts,
  278. QEMUTimerList *timer_list, int scale,
  279. QEMUTimerCB *cb, void *opaque)
  280. {
  281. ts->timer_list = timer_list;
  282. ts->cb = cb;
  283. ts->opaque = opaque;
  284. ts->scale = scale;
  285. ts->expire_time = -1;
  286. }
  287. void timer_free(QEMUTimer *ts)
  288. {
  289. g_free(ts);
  290. }
  291. static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts)
  292. {
  293. QEMUTimer **pt, *t;
  294. ts->expire_time = -1;
  295. pt = &timer_list->active_timers;
  296. for(;;) {
  297. t = *pt;
  298. if (!t)
  299. break;
  300. if (t == ts) {
  301. *pt = t->next;
  302. break;
  303. }
  304. pt = &t->next;
  305. }
  306. }
  307. static bool timer_mod_ns_locked(QEMUTimerList *timer_list,
  308. QEMUTimer *ts, int64_t expire_time)
  309. {
  310. QEMUTimer **pt, *t;
  311. /* add the timer in the sorted list */
  312. pt = &timer_list->active_timers;
  313. for (;;) {
  314. t = *pt;
  315. if (!timer_expired_ns(t, expire_time)) {
  316. break;
  317. }
  318. pt = &t->next;
  319. }
  320. ts->expire_time = MAX(expire_time, 0);
  321. ts->next = *pt;
  322. *pt = ts;
  323. return pt == &timer_list->active_timers;
  324. }
  325. static void timerlist_rearm(QEMUTimerList *timer_list)
  326. {
  327. /* Interrupt execution to force deadline recalculation. */
  328. qemu_clock_warp(timer_list->clock->type);
  329. timerlist_notify(timer_list);
  330. }
  331. /* stop a timer, but do not dealloc it */
  332. void timer_del(QEMUTimer *ts)
  333. {
  334. QEMUTimerList *timer_list = ts->timer_list;
  335. qemu_mutex_lock(&timer_list->active_timers_lock);
  336. timer_del_locked(timer_list, ts);
  337. qemu_mutex_unlock(&timer_list->active_timers_lock);
  338. }
  339. /* modify the current timer so that it will be fired when current_time
  340. >= expire_time. The corresponding callback will be called. */
  341. void timer_mod_ns(QEMUTimer *ts, int64_t expire_time)
  342. {
  343. QEMUTimerList *timer_list = ts->timer_list;
  344. bool rearm;
  345. qemu_mutex_lock(&timer_list->active_timers_lock);
  346. timer_del_locked(timer_list, ts);
  347. rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
  348. qemu_mutex_unlock(&timer_list->active_timers_lock);
  349. if (rearm) {
  350. timerlist_rearm(timer_list);
  351. }
  352. }
  353. /* modify the current timer so that it will be fired when current_time
  354. >= expire_time or the current deadline, whichever comes earlier.
  355. The corresponding callback will be called. */
  356. void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time)
  357. {
  358. QEMUTimerList *timer_list = ts->timer_list;
  359. bool rearm;
  360. qemu_mutex_lock(&timer_list->active_timers_lock);
  361. if (ts->expire_time == -1 || ts->expire_time > expire_time) {
  362. if (ts->expire_time != -1) {
  363. timer_del_locked(timer_list, ts);
  364. }
  365. rearm = timer_mod_ns_locked(timer_list, ts, expire_time);
  366. } else {
  367. rearm = false;
  368. }
  369. qemu_mutex_unlock(&timer_list->active_timers_lock);
  370. if (rearm) {
  371. timerlist_rearm(timer_list);
  372. }
  373. }
  374. void timer_mod(QEMUTimer *ts, int64_t expire_time)
  375. {
  376. timer_mod_ns(ts, expire_time * ts->scale);
  377. }
  378. void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time)
  379. {
  380. timer_mod_anticipate_ns(ts, expire_time * ts->scale);
  381. }
  382. bool timer_pending(QEMUTimer *ts)
  383. {
  384. return ts->expire_time >= 0;
  385. }
  386. bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
  387. {
  388. return timer_expired_ns(timer_head, current_time * timer_head->scale);
  389. }
  390. bool timerlist_run_timers(QEMUTimerList *timer_list)
  391. {
  392. QEMUTimer *ts;
  393. int64_t current_time;
  394. bool progress = false;
  395. QEMUTimerCB *cb;
  396. void *opaque;
  397. qemu_event_reset(&timer_list->timers_done_ev);
  398. if (!timer_list->clock->enabled) {
  399. goto out;
  400. }
  401. current_time = qemu_clock_get_ns(timer_list->clock->type);
  402. for(;;) {
  403. qemu_mutex_lock(&timer_list->active_timers_lock);
  404. ts = timer_list->active_timers;
  405. if (!timer_expired_ns(ts, current_time)) {
  406. qemu_mutex_unlock(&timer_list->active_timers_lock);
  407. break;
  408. }
  409. /* remove timer from the list before calling the callback */
  410. timer_list->active_timers = ts->next;
  411. ts->next = NULL;
  412. ts->expire_time = -1;
  413. cb = ts->cb;
  414. opaque = ts->opaque;
  415. qemu_mutex_unlock(&timer_list->active_timers_lock);
  416. /* run the callback (the timer list can be modified) */
  417. cb(opaque);
  418. progress = true;
  419. }
  420. out:
  421. qemu_event_set(&timer_list->timers_done_ev);
  422. return progress;
  423. }
  424. bool qemu_clock_run_timers(QEMUClockType type)
  425. {
  426. return timerlist_run_timers(main_loop_tlg.tl[type]);
  427. }
  428. void timerlistgroup_init(QEMUTimerListGroup *tlg,
  429. QEMUTimerListNotifyCB *cb, void *opaque)
  430. {
  431. QEMUClockType type;
  432. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  433. tlg->tl[type] = timerlist_new(type, cb, opaque);
  434. }
  435. }
  436. void timerlistgroup_deinit(QEMUTimerListGroup *tlg)
  437. {
  438. QEMUClockType type;
  439. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  440. timerlist_free(tlg->tl[type]);
  441. }
  442. }
  443. bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg)
  444. {
  445. QEMUClockType type;
  446. bool progress = false;
  447. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  448. progress |= timerlist_run_timers(tlg->tl[type]);
  449. }
  450. return progress;
  451. }
  452. int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg)
  453. {
  454. int64_t deadline = -1;
  455. QEMUClockType type;
  456. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  457. if (qemu_clock_use_for_deadline(tlg->tl[type]->clock->type)) {
  458. deadline = qemu_soonest_timeout(deadline,
  459. timerlist_deadline_ns(
  460. tlg->tl[type]));
  461. }
  462. }
  463. return deadline;
  464. }
  465. int64_t qemu_clock_get_ns(QEMUClockType type)
  466. {
  467. int64_t now, last;
  468. QEMUClock *clock = qemu_clock_ptr(type);
  469. switch (type) {
  470. case QEMU_CLOCK_REALTIME:
  471. return get_clock();
  472. default:
  473. case QEMU_CLOCK_VIRTUAL:
  474. if (use_icount) {
  475. return cpu_get_icount();
  476. } else {
  477. return cpu_get_clock();
  478. }
  479. case QEMU_CLOCK_HOST:
  480. now = get_clock_realtime();
  481. last = clock->last;
  482. clock->last = now;
  483. if (now < last) {
  484. notifier_list_notify(&clock->reset_notifiers, &now);
  485. }
  486. return now;
  487. }
  488. }
  489. void qemu_clock_register_reset_notifier(QEMUClockType type,
  490. Notifier *notifier)
  491. {
  492. QEMUClock *clock = qemu_clock_ptr(type);
  493. notifier_list_add(&clock->reset_notifiers, notifier);
  494. }
  495. void qemu_clock_unregister_reset_notifier(QEMUClockType type,
  496. Notifier *notifier)
  497. {
  498. notifier_remove(notifier);
  499. }
  500. void init_clocks(void)
  501. {
  502. QEMUClockType type;
  503. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  504. qemu_clock_init(type);
  505. }
  506. #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
  507. prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
  508. #endif
  509. }
  510. uint64_t timer_expire_time_ns(QEMUTimer *ts)
  511. {
  512. return timer_pending(ts) ? ts->expire_time : -1;
  513. }
  514. bool qemu_clock_run_all_timers(void)
  515. {
  516. bool progress = false;
  517. QEMUClockType type;
  518. for (type = 0; type < QEMU_CLOCK_MAX; type++) {
  519. progress |= qemu_clock_run_timers(type);
  520. }
  521. return progress;
  522. }