2
0

qemu-timer.c 16 KB

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