2
0

qemu-timer.c 16 KB

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