2
0

timer.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. #ifndef QEMU_TIMER_H
  2. #define QEMU_TIMER_H
  3. #include "qemu-common.h"
  4. #include "qemu/notify.h"
  5. #include "qemu/host-utils.h"
  6. #define NANOSECONDS_PER_SECOND 1000000000LL
  7. /* timers */
  8. #define SCALE_MS 1000000
  9. #define SCALE_US 1000
  10. #define SCALE_NS 1
  11. /**
  12. * QEMUClockType:
  13. *
  14. * The following clock types are available:
  15. *
  16. * @QEMU_CLOCK_REALTIME: Real time clock
  17. *
  18. * The real time clock should be used only for stuff which does not
  19. * change the virtual machine state, as it runs even if the virtual
  20. * machine is stopped.
  21. *
  22. * @QEMU_CLOCK_VIRTUAL: virtual clock
  23. *
  24. * The virtual clock only runs during the emulation. It stops
  25. * when the virtual machine is stopped.
  26. *
  27. * @QEMU_CLOCK_HOST: host clock
  28. *
  29. * The host clock should be used for device models that emulate accurate
  30. * real time sources. It will continue to run when the virtual machine
  31. * is suspended, and it will reflect system time changes the host may
  32. * undergo (e.g. due to NTP).
  33. *
  34. * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
  35. *
  36. * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
  37. * In icount mode, this clock counts nanoseconds while the virtual
  38. * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
  39. * while the CPUs are sleeping and thus not executing instructions.
  40. */
  41. typedef enum {
  42. QEMU_CLOCK_REALTIME = 0,
  43. QEMU_CLOCK_VIRTUAL = 1,
  44. QEMU_CLOCK_HOST = 2,
  45. QEMU_CLOCK_VIRTUAL_RT = 3,
  46. QEMU_CLOCK_MAX
  47. } QEMUClockType;
  48. typedef struct QEMUTimerList QEMUTimerList;
  49. struct QEMUTimerListGroup {
  50. QEMUTimerList *tl[QEMU_CLOCK_MAX];
  51. };
  52. typedef void QEMUTimerCB(void *opaque);
  53. typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
  54. struct QEMUTimer {
  55. int64_t expire_time; /* in nanoseconds */
  56. QEMUTimerList *timer_list;
  57. QEMUTimerCB *cb;
  58. void *opaque;
  59. QEMUTimer *next;
  60. int scale;
  61. };
  62. extern QEMUTimerListGroup main_loop_tlg;
  63. /*
  64. * qemu_clock_get_ns;
  65. * @type: the clock type
  66. *
  67. * Get the nanosecond value of a clock with
  68. * type @type
  69. *
  70. * Returns: the clock value in nanoseconds
  71. */
  72. int64_t qemu_clock_get_ns(QEMUClockType type);
  73. /**
  74. * qemu_clock_get_ms;
  75. * @type: the clock type
  76. *
  77. * Get the millisecond value of a clock with
  78. * type @type
  79. *
  80. * Returns: the clock value in milliseconds
  81. */
  82. static inline int64_t qemu_clock_get_ms(QEMUClockType type)
  83. {
  84. return qemu_clock_get_ns(type) / SCALE_MS;
  85. }
  86. /**
  87. * qemu_clock_get_us;
  88. * @type: the clock type
  89. *
  90. * Get the microsecond value of a clock with
  91. * type @type
  92. *
  93. * Returns: the clock value in microseconds
  94. */
  95. static inline int64_t qemu_clock_get_us(QEMUClockType type)
  96. {
  97. return qemu_clock_get_ns(type) / SCALE_US;
  98. }
  99. /**
  100. * qemu_clock_has_timers:
  101. * @type: the clock type
  102. *
  103. * Determines whether a clock's default timer list
  104. * has timers attached
  105. *
  106. * Note that this function should not be used when other threads also access
  107. * the timer list. The return value may be outdated by the time it is acted
  108. * upon.
  109. *
  110. * Returns: true if the clock's default timer list
  111. * has timers attached
  112. */
  113. bool qemu_clock_has_timers(QEMUClockType type);
  114. /**
  115. * qemu_clock_expired:
  116. * @type: the clock type
  117. *
  118. * Determines whether a clock's default timer list
  119. * has an expired timer.
  120. *
  121. * Returns: true if the clock's default timer list has
  122. * an expired timer
  123. */
  124. bool qemu_clock_expired(QEMUClockType type);
  125. /**
  126. * qemu_clock_use_for_deadline:
  127. * @type: the clock type
  128. *
  129. * Determine whether a clock should be used for deadline
  130. * calculations. Some clocks, for instance vm_clock with
  131. * use_icount set, do not count in nanoseconds. Such clocks
  132. * are not used for deadline calculations, and are presumed
  133. * to interrupt any poll using qemu_notify/aio_notify
  134. * etc.
  135. *
  136. * Returns: true if the clock runs in nanoseconds and
  137. * should be used for a deadline.
  138. */
  139. bool qemu_clock_use_for_deadline(QEMUClockType type);
  140. /**
  141. * qemu_clock_deadline_ns_all:
  142. * @type: the clock type
  143. *
  144. * Calculate the deadline across all timer lists associated
  145. * with a clock (as opposed to just the default one)
  146. * in nanoseconds, or -1 if no timer is set to expire.
  147. *
  148. * Returns: time until expiry in nanoseconds or -1
  149. */
  150. int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
  151. /**
  152. * qemu_clock_get_main_loop_timerlist:
  153. * @type: the clock type
  154. *
  155. * Return the default timer list associated with a clock.
  156. *
  157. * Returns: the default timer list
  158. */
  159. QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
  160. /**
  161. * qemu_clock_nofify:
  162. * @type: the clock type
  163. *
  164. * Call the notifier callback connected with the default timer
  165. * list linked to the clock, or qemu_notify() if none.
  166. */
  167. void qemu_clock_notify(QEMUClockType type);
  168. /**
  169. * qemu_clock_enable:
  170. * @type: the clock type
  171. * @enabled: true to enable, false to disable
  172. *
  173. * Enable or disable a clock
  174. * Disabling the clock will wait for related timerlists to stop
  175. * executing qemu_run_timers. Thus, this functions should not
  176. * be used from the callback of a timer that is based on @clock.
  177. * Doing so would cause a deadlock.
  178. *
  179. * Caller should hold BQL.
  180. */
  181. void qemu_clock_enable(QEMUClockType type, bool enabled);
  182. /**
  183. * qemu_start_warp_timer:
  184. *
  185. * Starts a timer for virtual clock update
  186. */
  187. void qemu_start_warp_timer(void);
  188. /**
  189. * qemu_clock_register_reset_notifier:
  190. * @type: the clock type
  191. * @notifier: the notifier function
  192. *
  193. * Register a notifier function to call when the clock
  194. * concerned is reset.
  195. */
  196. void qemu_clock_register_reset_notifier(QEMUClockType type,
  197. Notifier *notifier);
  198. /**
  199. * qemu_clock_unregister_reset_notifier:
  200. * @type: the clock type
  201. * @notifier: the notifier function
  202. *
  203. * Unregister a notifier function to call when the clock
  204. * concerned is reset.
  205. */
  206. void qemu_clock_unregister_reset_notifier(QEMUClockType type,
  207. Notifier *notifier);
  208. /**
  209. * qemu_clock_run_timers:
  210. * @type: clock on which to operate
  211. *
  212. * Run all the timers associated with the default timer list
  213. * of a clock.
  214. *
  215. * Returns: true if any timer ran.
  216. */
  217. bool qemu_clock_run_timers(QEMUClockType type);
  218. /**
  219. * qemu_clock_run_all_timers:
  220. *
  221. * Run all the timers associated with the default timer list
  222. * of every clock.
  223. *
  224. * Returns: true if any timer ran.
  225. */
  226. bool qemu_clock_run_all_timers(void);
  227. /**
  228. * qemu_clock_get_last:
  229. *
  230. * Returns last clock query time.
  231. */
  232. uint64_t qemu_clock_get_last(QEMUClockType type);
  233. /**
  234. * qemu_clock_set_last:
  235. *
  236. * Sets last clock query time.
  237. */
  238. void qemu_clock_set_last(QEMUClockType type, uint64_t last);
  239. /*
  240. * QEMUTimerList
  241. */
  242. /**
  243. * timerlist_new:
  244. * @type: the clock type to associate with the timerlist
  245. * @cb: the callback to call on notification
  246. * @opaque: the opaque pointer to pass to the callback
  247. *
  248. * Create a new timerlist associated with the clock of
  249. * type @type.
  250. *
  251. * Returns: a pointer to the QEMUTimerList created
  252. */
  253. QEMUTimerList *timerlist_new(QEMUClockType type,
  254. QEMUTimerListNotifyCB *cb, void *opaque);
  255. /**
  256. * timerlist_free:
  257. * @timer_list: the timer list to free
  258. *
  259. * Frees a timer_list. It must have no active timers.
  260. */
  261. void timerlist_free(QEMUTimerList *timer_list);
  262. /**
  263. * timerlist_has_timers:
  264. * @timer_list: the timer list to operate on
  265. *
  266. * Determine whether a timer list has active timers
  267. *
  268. * Note that this function should not be used when other threads also access
  269. * the timer list. The return value may be outdated by the time it is acted
  270. * upon.
  271. *
  272. * Returns: true if the timer list has timers.
  273. */
  274. bool timerlist_has_timers(QEMUTimerList *timer_list);
  275. /**
  276. * timerlist_expired:
  277. * @timer_list: the timer list to operate on
  278. *
  279. * Determine whether a timer list has any timers which
  280. * are expired.
  281. *
  282. * Returns: true if the timer list has timers which
  283. * have expired.
  284. */
  285. bool timerlist_expired(QEMUTimerList *timer_list);
  286. /**
  287. * timerlist_deadline_ns:
  288. * @timer_list: the timer list to operate on
  289. *
  290. * Determine the deadline for a timer_list, i.e.
  291. * the number of nanoseconds until the first timer
  292. * expires. Return -1 if there are no timers.
  293. *
  294. * Returns: the number of nanoseconds until the earliest
  295. * timer expires -1 if none
  296. */
  297. int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
  298. /**
  299. * timerlist_get_clock:
  300. * @timer_list: the timer list to operate on
  301. *
  302. * Determine the clock type associated with a timer list.
  303. *
  304. * Returns: the clock type associated with the
  305. * timer list.
  306. */
  307. QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
  308. /**
  309. * timerlist_run_timers:
  310. * @timer_list: the timer list to use
  311. *
  312. * Call all expired timers associated with the timer list.
  313. *
  314. * Returns: true if any timer expired
  315. */
  316. bool timerlist_run_timers(QEMUTimerList *timer_list);
  317. /**
  318. * timerlist_notify:
  319. * @timer_list: the timer list to use
  320. *
  321. * call the notifier callback associated with the timer list.
  322. */
  323. void timerlist_notify(QEMUTimerList *timer_list);
  324. /*
  325. * QEMUTimerListGroup
  326. */
  327. /**
  328. * timerlistgroup_init:
  329. * @tlg: the timer list group
  330. * @cb: the callback to call when a notify is required
  331. * @opaque: the opaque pointer to be passed to the callback.
  332. *
  333. * Initialise a timer list group. This must already be
  334. * allocated in memory and zeroed. The notifier callback is
  335. * called whenever a clock in the timer list group is
  336. * reenabled or whenever a timer associated with any timer
  337. * list is modified. If @cb is specified as null, qemu_notify()
  338. * is used instead.
  339. */
  340. void timerlistgroup_init(QEMUTimerListGroup *tlg,
  341. QEMUTimerListNotifyCB *cb, void *opaque);
  342. /**
  343. * timerlistgroup_deinit:
  344. * @tlg: the timer list group
  345. *
  346. * Deinitialise a timer list group. This must already be
  347. * initialised. Note the memory is not freed.
  348. */
  349. void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
  350. /**
  351. * timerlistgroup_run_timers:
  352. * @tlg: the timer list group
  353. *
  354. * Run the timers associated with a timer list group.
  355. * This will run timers on multiple clocks.
  356. *
  357. * Returns: true if any timer callback ran
  358. */
  359. bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
  360. /**
  361. * timerlistgroup_deadline_ns:
  362. * @tlg: the timer list group
  363. *
  364. * Determine the deadline of the soonest timer to
  365. * expire associated with any timer list linked to
  366. * the timer list group. Only clocks suitable for
  367. * deadline calculation are included.
  368. *
  369. * Returns: the deadline in nanoseconds or -1 if no
  370. * timers are to expire.
  371. */
  372. int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
  373. /*
  374. * QEMUTimer
  375. */
  376. /**
  377. * timer_init_tl:
  378. * @ts: the timer to be initialised
  379. * @timer_list: the timer list to attach the timer to
  380. * @scale: the scale value for the timer
  381. * @cb: the callback to be called when the timer expires
  382. * @opaque: the opaque pointer to be passed to the callback
  383. *
  384. * Initialise a new timer and associate it with @timer_list.
  385. * The caller is responsible for allocating the memory.
  386. *
  387. * You need not call an explicit deinit call. Simply make
  388. * sure it is not on a list with timer_del.
  389. */
  390. void timer_init_tl(QEMUTimer *ts,
  391. QEMUTimerList *timer_list, int scale,
  392. QEMUTimerCB *cb, void *opaque);
  393. /**
  394. * timer_init:
  395. * @ts: the timer to be initialised
  396. * @type: the clock to associate with the timer
  397. * @scale: the scale value for the timer
  398. * @cb: the callback to call when the timer expires
  399. * @opaque: the opaque pointer to pass to the callback
  400. *
  401. * Initialize a timer with the given scale on the default timer list
  402. * associated with the clock.
  403. *
  404. * You need not call an explicit deinit call. Simply make
  405. * sure it is not on a list with timer_del.
  406. */
  407. static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
  408. QEMUTimerCB *cb, void *opaque)
  409. {
  410. timer_init_tl(ts, main_loop_tlg.tl[type], scale, cb, opaque);
  411. }
  412. /**
  413. * timer_init_ns:
  414. * @ts: the timer to be initialised
  415. * @type: the clock to associate with the timer
  416. * @cb: the callback to call when the timer expires
  417. * @opaque: the opaque pointer to pass to the callback
  418. *
  419. * Initialize a timer with nanosecond scale on the default timer list
  420. * associated with the clock.
  421. *
  422. * You need not call an explicit deinit call. Simply make
  423. * sure it is not on a list with timer_del.
  424. */
  425. static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
  426. QEMUTimerCB *cb, void *opaque)
  427. {
  428. timer_init(ts, type, SCALE_NS, cb, opaque);
  429. }
  430. /**
  431. * timer_init_us:
  432. * @ts: the timer to be initialised
  433. * @type: the clock to associate with the timer
  434. * @cb: the callback to call when the timer expires
  435. * @opaque: the opaque pointer to pass to the callback
  436. *
  437. * Initialize a timer with microsecond scale on the default timer list
  438. * associated with the clock.
  439. *
  440. * You need not call an explicit deinit call. Simply make
  441. * sure it is not on a list with timer_del.
  442. */
  443. static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
  444. QEMUTimerCB *cb, void *opaque)
  445. {
  446. timer_init(ts, type, SCALE_US, cb, opaque);
  447. }
  448. /**
  449. * timer_init_ms:
  450. * @ts: the timer to be initialised
  451. * @type: the clock to associate with the timer
  452. * @cb: the callback to call when the timer expires
  453. * @opaque: the opaque pointer to pass to the callback
  454. *
  455. * Initialize a timer with millisecond scale on the default timer list
  456. * associated with the clock.
  457. *
  458. * You need not call an explicit deinit call. Simply make
  459. * sure it is not on a list with timer_del.
  460. */
  461. static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
  462. QEMUTimerCB *cb, void *opaque)
  463. {
  464. timer_init(ts, type, SCALE_MS, cb, opaque);
  465. }
  466. /**
  467. * timer_new_tl:
  468. * @timer_list: the timer list to attach the timer to
  469. * @scale: the scale value for the timer
  470. * @cb: the callback to be called when the timer expires
  471. * @opaque: the opaque pointer to be passed to the callback
  472. *
  473. * Create a new timer and associate it with @timer_list.
  474. * The memory is allocated by the function.
  475. *
  476. * This is not the preferred interface unless you know you
  477. * are going to call timer_free. Use timer_init instead.
  478. *
  479. * Returns: a pointer to the timer
  480. */
  481. static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
  482. int scale,
  483. QEMUTimerCB *cb,
  484. void *opaque)
  485. {
  486. QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
  487. timer_init_tl(ts, timer_list, scale, cb, opaque);
  488. return ts;
  489. }
  490. /**
  491. * timer_new:
  492. * @type: the clock type to use
  493. * @scale: the scale value for the timer
  494. * @cb: the callback to be called when the timer expires
  495. * @opaque: the opaque pointer to be passed to the callback
  496. *
  497. * Create a new timer and associate it with the default
  498. * timer list for the clock type @type.
  499. *
  500. * The default timer list has one special feature: in icount mode,
  501. * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
  502. * not true of other timer lists, which are typically associated
  503. * with an AioContext---each of them runs its timer callbacks in its own
  504. * AioContext thread.
  505. *
  506. * Returns: a pointer to the timer
  507. */
  508. static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
  509. QEMUTimerCB *cb, void *opaque)
  510. {
  511. return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
  512. }
  513. /**
  514. * timer_new_ns:
  515. * @type: the clock type to associate with the timer
  516. * @cb: the callback to call when the timer expires
  517. * @opaque: the opaque pointer to pass to the callback
  518. *
  519. * Create a new timer with nanosecond scale on the default timer list
  520. * associated with the clock.
  521. *
  522. * The default timer list has one special feature: in icount mode,
  523. * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
  524. * not true of other timer lists, which are typically associated
  525. * with an AioContext---each of them runs its timer callbacks in its own
  526. * AioContext thread.
  527. *
  528. * Returns: a pointer to the newly created timer
  529. */
  530. static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
  531. void *opaque)
  532. {
  533. return timer_new(type, SCALE_NS, cb, opaque);
  534. }
  535. /**
  536. * timer_new_us:
  537. * @type: the clock type to associate with the timer
  538. * @cb: the callback to call when the timer expires
  539. * @opaque: the opaque pointer to pass to the callback
  540. *
  541. * The default timer list has one special feature: in icount mode,
  542. * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
  543. * not true of other timer lists, which are typically associated
  544. * with an AioContext---each of them runs its timer callbacks in its own
  545. * AioContext thread.
  546. *
  547. * Create a new timer with microsecond scale on the default timer list
  548. * associated with the clock.
  549. *
  550. * Returns: a pointer to the newly created timer
  551. */
  552. static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
  553. void *opaque)
  554. {
  555. return timer_new(type, SCALE_US, cb, opaque);
  556. }
  557. /**
  558. * timer_new_ms:
  559. * @type: the clock type to associate with the timer
  560. * @cb: the callback to call when the timer expires
  561. * @opaque: the opaque pointer to pass to the callback
  562. *
  563. * The default timer list has one special feature: in icount mode,
  564. * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
  565. * not true of other timer lists, which are typically associated
  566. * with an AioContext---each of them runs its timer callbacks in its own
  567. * AioContext thread.
  568. *
  569. * Create a new timer with millisecond scale on the default timer list
  570. * associated with the clock.
  571. *
  572. * Returns: a pointer to the newly created timer
  573. */
  574. static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
  575. void *opaque)
  576. {
  577. return timer_new(type, SCALE_MS, cb, opaque);
  578. }
  579. /**
  580. * timer_deinit:
  581. * @ts: the timer to be de-initialised
  582. *
  583. * Deassociate the timer from any timerlist. You should
  584. * call timer_del before. After this call, any further
  585. * timer_del call cannot cause dangling pointer accesses
  586. * even if the previously used timerlist is freed.
  587. */
  588. void timer_deinit(QEMUTimer *ts);
  589. /**
  590. * timer_free:
  591. * @ts: the timer
  592. *
  593. * Free a timer (it must not be on the active list)
  594. */
  595. static inline void timer_free(QEMUTimer *ts)
  596. {
  597. g_free(ts);
  598. }
  599. /**
  600. * timer_del:
  601. * @ts: the timer
  602. *
  603. * Delete a timer from the active list.
  604. *
  605. * This function is thread-safe but the timer and its timer list must not be
  606. * freed while this function is running.
  607. */
  608. void timer_del(QEMUTimer *ts);
  609. /**
  610. * timer_mod_ns:
  611. * @ts: the timer
  612. * @expire_time: the expiry time in nanoseconds
  613. *
  614. * Modify a timer to expire at @expire_time
  615. *
  616. * This function is thread-safe but the timer and its timer list must not be
  617. * freed while this function is running.
  618. */
  619. void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
  620. /**
  621. * timer_mod_anticipate_ns:
  622. * @ts: the timer
  623. * @expire_time: the expiry time in nanoseconds
  624. *
  625. * Modify a timer to expire at @expire_time or the current time,
  626. * whichever comes earlier.
  627. *
  628. * This function is thread-safe but the timer and its timer list must not be
  629. * freed while this function is running.
  630. */
  631. void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
  632. /**
  633. * timer_mod:
  634. * @ts: the timer
  635. * @expire_time: the expire time in the units associated with the timer
  636. *
  637. * Modify a timer to expiry at @expire_time, taking into
  638. * account the scale associated with the timer.
  639. *
  640. * This function is thread-safe but the timer and its timer list must not be
  641. * freed while this function is running.
  642. */
  643. void timer_mod(QEMUTimer *ts, int64_t expire_timer);
  644. /**
  645. * timer_mod_anticipate:
  646. * @ts: the timer
  647. * @expire_time: the expiry time in nanoseconds
  648. *
  649. * Modify a timer to expire at @expire_time or the current time, whichever
  650. * comes earlier, taking into account the scale associated with the timer.
  651. *
  652. * This function is thread-safe but the timer and its timer list must not be
  653. * freed while this function is running.
  654. */
  655. void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
  656. /**
  657. * timer_pending:
  658. * @ts: the timer
  659. *
  660. * Determines whether a timer is pending (i.e. is on the
  661. * active list of timers, whether or not it has not yet expired).
  662. *
  663. * Returns: true if the timer is pending
  664. */
  665. bool timer_pending(QEMUTimer *ts);
  666. /**
  667. * timer_expired:
  668. * @ts: the timer
  669. * @current_time: the current time
  670. *
  671. * Determines whether a timer has expired.
  672. *
  673. * Returns: true if the timer has expired
  674. */
  675. bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
  676. /**
  677. * timer_expire_time_ns:
  678. * @ts: the timer
  679. *
  680. * Determine the expiry time of a timer
  681. *
  682. * Returns: the expiry time in nanoseconds
  683. */
  684. uint64_t timer_expire_time_ns(QEMUTimer *ts);
  685. /**
  686. * timer_get:
  687. * @f: the file
  688. * @ts: the timer
  689. *
  690. * Read a timer @ts from a file @f
  691. */
  692. void timer_get(QEMUFile *f, QEMUTimer *ts);
  693. /**
  694. * timer_put:
  695. * @f: the file
  696. * @ts: the timer
  697. */
  698. void timer_put(QEMUFile *f, QEMUTimer *ts);
  699. /*
  700. * General utility functions
  701. */
  702. /**
  703. * qemu_timeout_ns_to_ms:
  704. * @ns: nanosecond timeout value
  705. *
  706. * Convert a nanosecond timeout value (or -1) to
  707. * a millisecond value (or -1), always rounding up.
  708. *
  709. * Returns: millisecond timeout value
  710. */
  711. int qemu_timeout_ns_to_ms(int64_t ns);
  712. /**
  713. * qemu_poll_ns:
  714. * @fds: Array of file descriptors
  715. * @nfds: number of file descriptors
  716. * @timeout: timeout in nanoseconds
  717. *
  718. * Perform a poll like g_poll but with a timeout in nanoseconds.
  719. * See g_poll documentation for further details.
  720. *
  721. * Returns: number of fds ready
  722. */
  723. int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
  724. /**
  725. * qemu_soonest_timeout:
  726. * @timeout1: first timeout in nanoseconds (or -1 for infinite)
  727. * @timeout2: second timeout in nanoseconds (or -1 for infinite)
  728. *
  729. * Calculates the soonest of two timeout values. -1 means infinite, which
  730. * is later than any other value.
  731. *
  732. * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
  733. */
  734. static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
  735. {
  736. /* we can abuse the fact that -1 (which means infinite) is a maximal
  737. * value when cast to unsigned. As this is disgusting, it's kept in
  738. * one inline function.
  739. */
  740. return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
  741. }
  742. /**
  743. * initclocks:
  744. *
  745. * Initialise the clock & timer infrastructure
  746. */
  747. void init_clocks(QEMUTimerListNotifyCB *notify_cb);
  748. int64_t cpu_get_ticks(void);
  749. /* Caller must hold BQL */
  750. void cpu_enable_ticks(void);
  751. /* Caller must hold BQL */
  752. void cpu_disable_ticks(void);
  753. static inline int64_t get_max_clock_jump(void)
  754. {
  755. /* This should be small enough to prevent excessive interrupts from being
  756. * generated by the RTC on clock jumps, but large enough to avoid frequent
  757. * unnecessary resets in idle VMs.
  758. */
  759. return 60 * NANOSECONDS_PER_SECOND;
  760. }
  761. /*
  762. * Low level clock functions
  763. */
  764. /* get host real time in nanosecond */
  765. static inline int64_t get_clock_realtime(void)
  766. {
  767. struct timeval tv;
  768. gettimeofday(&tv, NULL);
  769. return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
  770. }
  771. /* Warning: don't insert tracepoints into these functions, they are
  772. also used by simpletrace backend and tracepoints would cause
  773. an infinite recursion! */
  774. #ifdef _WIN32
  775. extern int64_t clock_freq;
  776. static inline int64_t get_clock(void)
  777. {
  778. LARGE_INTEGER ti;
  779. QueryPerformanceCounter(&ti);
  780. return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
  781. }
  782. #else
  783. extern int use_rt_clock;
  784. static inline int64_t get_clock(void)
  785. {
  786. #ifdef CLOCK_MONOTONIC
  787. if (use_rt_clock) {
  788. struct timespec ts;
  789. clock_gettime(CLOCK_MONOTONIC, &ts);
  790. return ts.tv_sec * 1000000000LL + ts.tv_nsec;
  791. } else
  792. #endif
  793. {
  794. /* XXX: using gettimeofday leads to problems if the date
  795. changes, so it should be avoided. */
  796. return get_clock_realtime();
  797. }
  798. }
  799. #endif
  800. /* icount */
  801. int64_t cpu_get_icount_raw(void);
  802. int64_t cpu_get_icount(void);
  803. int64_t cpu_get_clock(void);
  804. int64_t cpu_icount_to_ns(int64_t icount);
  805. void cpu_update_icount(CPUState *cpu);
  806. /*******************************************/
  807. /* host CPU ticks (if available) */
  808. #if defined(_ARCH_PPC)
  809. static inline int64_t cpu_get_host_ticks(void)
  810. {
  811. int64_t retval;
  812. #ifdef _ARCH_PPC64
  813. /* This reads timebase in one 64bit go and includes Cell workaround from:
  814. http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
  815. */
  816. __asm__ __volatile__ ("mftb %0\n\t"
  817. "cmpwi %0,0\n\t"
  818. "beq- $-8"
  819. : "=r" (retval));
  820. #else
  821. /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
  822. unsigned long junk;
  823. __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
  824. "mfspr %L0,268\n\t" /* mftb */
  825. "mfspr %0,269\n\t" /* mftbu */
  826. "cmpw %0,%1\n\t"
  827. "bne $-16"
  828. : "=r" (retval), "=r" (junk));
  829. #endif
  830. return retval;
  831. }
  832. #elif defined(__i386__)
  833. static inline int64_t cpu_get_host_ticks(void)
  834. {
  835. int64_t val;
  836. asm volatile ("rdtsc" : "=A" (val));
  837. return val;
  838. }
  839. #elif defined(__x86_64__)
  840. static inline int64_t cpu_get_host_ticks(void)
  841. {
  842. uint32_t low,high;
  843. int64_t val;
  844. asm volatile("rdtsc" : "=a" (low), "=d" (high));
  845. val = high;
  846. val <<= 32;
  847. val |= low;
  848. return val;
  849. }
  850. #elif defined(__hppa__)
  851. static inline int64_t cpu_get_host_ticks(void)
  852. {
  853. int val;
  854. asm volatile ("mfctl %%cr16, %0" : "=r"(val));
  855. return val;
  856. }
  857. #elif defined(__s390__)
  858. static inline int64_t cpu_get_host_ticks(void)
  859. {
  860. int64_t val;
  861. asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
  862. return val;
  863. }
  864. #elif defined(__sparc__)
  865. static inline int64_t cpu_get_host_ticks (void)
  866. {
  867. #if defined(_LP64)
  868. uint64_t rval;
  869. asm volatile("rd %%tick,%0" : "=r"(rval));
  870. return rval;
  871. #else
  872. /* We need an %o or %g register for this. For recent enough gcc
  873. there is an "h" constraint for that. Don't bother with that. */
  874. union {
  875. uint64_t i64;
  876. struct {
  877. uint32_t high;
  878. uint32_t low;
  879. } i32;
  880. } rval;
  881. asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
  882. : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
  883. return rval.i64;
  884. #endif
  885. }
  886. #elif defined(__mips__) && \
  887. ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
  888. /*
  889. * binutils wants to use rdhwr only on mips32r2
  890. * but as linux kernel emulate it, it's fine
  891. * to use it.
  892. *
  893. */
  894. #define MIPS_RDHWR(rd, value) { \
  895. __asm__ __volatile__ (".set push\n\t" \
  896. ".set mips32r2\n\t" \
  897. "rdhwr %0, "rd"\n\t" \
  898. ".set pop" \
  899. : "=r" (value)); \
  900. }
  901. static inline int64_t cpu_get_host_ticks(void)
  902. {
  903. /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
  904. uint32_t count;
  905. static uint32_t cyc_per_count = 0;
  906. if (!cyc_per_count) {
  907. MIPS_RDHWR("$3", cyc_per_count);
  908. }
  909. MIPS_RDHWR("$2", count);
  910. return (int64_t)(count * cyc_per_count);
  911. }
  912. #elif defined(__alpha__)
  913. static inline int64_t cpu_get_host_ticks(void)
  914. {
  915. uint64_t cc;
  916. uint32_t cur, ofs;
  917. asm volatile("rpcc %0" : "=r"(cc));
  918. cur = cc;
  919. ofs = cc >> 32;
  920. return cur - ofs;
  921. }
  922. #else
  923. /* The host CPU doesn't have an easily accessible cycle counter.
  924. Just return a monotonically increasing value. This will be
  925. totally wrong, but hopefully better than nothing. */
  926. static inline int64_t cpu_get_host_ticks(void)
  927. {
  928. return get_clock();
  929. }
  930. #endif
  931. #ifdef CONFIG_PROFILER
  932. static inline int64_t profile_getclock(void)
  933. {
  934. return get_clock();
  935. }
  936. extern int64_t tcg_time;
  937. extern int64_t dev_time;
  938. #endif
  939. #endif