icount-common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/cutils.h"
  26. #include "migration/vmstate.h"
  27. #include "qapi/error.h"
  28. #include "qemu/error-report.h"
  29. #include "system/cpus.h"
  30. #include "system/qtest.h"
  31. #include "qemu/main-loop.h"
  32. #include "qemu/option.h"
  33. #include "qemu/seqlock.h"
  34. #include "system/replay.h"
  35. #include "system/runstate.h"
  36. #include "hw/core/cpu.h"
  37. #include "system/cpu-timers.h"
  38. #include "system/cpu-timers-internal.h"
  39. /*
  40. * ICOUNT: Instruction Counter
  41. *
  42. * this module is split off from cpu-timers because the icount part
  43. * is TCG-specific, and does not need to be built for other accels.
  44. */
  45. static bool icount_sleep = true;
  46. /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
  47. #define MAX_ICOUNT_SHIFT 10
  48. /* Do not count executed instructions */
  49. ICountMode use_icount = ICOUNT_DISABLED;
  50. static void icount_enable_precise(void)
  51. {
  52. /* Fixed conversion of insn to ns via "shift" option */
  53. use_icount = ICOUNT_PRECISE;
  54. }
  55. static void icount_enable_adaptive(void)
  56. {
  57. /* Runtime adaptive algorithm to compute shift */
  58. use_icount = ICOUNT_ADAPTATIVE;
  59. }
  60. /*
  61. * The current number of executed instructions is based on what we
  62. * originally budgeted minus the current state of the decrementing
  63. * icount counters in extra/u16.low.
  64. */
  65. static int64_t icount_get_executed(CPUState *cpu)
  66. {
  67. return (cpu->icount_budget -
  68. (cpu->neg.icount_decr.u16.low + cpu->icount_extra));
  69. }
  70. /*
  71. * Update the global shared timer_state.qemu_icount to take into
  72. * account executed instructions. This is done by the TCG vCPU
  73. * thread so the main-loop can see time has moved forward.
  74. */
  75. static void icount_update_locked(CPUState *cpu)
  76. {
  77. int64_t executed = icount_get_executed(cpu);
  78. cpu->icount_budget -= executed;
  79. qatomic_set_i64(&timers_state.qemu_icount,
  80. timers_state.qemu_icount + executed);
  81. }
  82. /*
  83. * Update the global shared timer_state.qemu_icount to take into
  84. * account executed instructions. This is done by the TCG vCPU
  85. * thread so the main-loop can see time has moved forward.
  86. */
  87. void icount_update(CPUState *cpu)
  88. {
  89. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  90. &timers_state.vm_clock_lock);
  91. icount_update_locked(cpu);
  92. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  93. &timers_state.vm_clock_lock);
  94. }
  95. static int64_t icount_get_raw_locked(void)
  96. {
  97. CPUState *cpu = current_cpu;
  98. if (cpu && cpu->running) {
  99. if (!cpu->neg.can_do_io) {
  100. error_report("Bad icount read");
  101. exit(1);
  102. }
  103. /* Take into account what has run */
  104. icount_update_locked(cpu);
  105. }
  106. /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
  107. return qatomic_read_i64(&timers_state.qemu_icount);
  108. }
  109. static int64_t icount_get_locked(void)
  110. {
  111. int64_t icount = icount_get_raw_locked();
  112. return qatomic_read_i64(&timers_state.qemu_icount_bias) +
  113. icount_to_ns(icount);
  114. }
  115. int64_t icount_get_raw(void)
  116. {
  117. int64_t icount;
  118. unsigned start;
  119. do {
  120. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  121. icount = icount_get_raw_locked();
  122. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  123. return icount;
  124. }
  125. /* Return the virtual CPU time, based on the instruction counter. */
  126. int64_t icount_get(void)
  127. {
  128. int64_t icount;
  129. unsigned start;
  130. do {
  131. start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  132. icount = icount_get_locked();
  133. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
  134. return icount;
  135. }
  136. int64_t icount_to_ns(int64_t icount)
  137. {
  138. return icount << qatomic_read(&timers_state.icount_time_shift);
  139. }
  140. /*
  141. * Correlation between real and virtual time is always going to be
  142. * fairly approximate, so ignore small variation.
  143. * When the guest is idle real and virtual time will be aligned in
  144. * the IO wait loop.
  145. */
  146. #define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
  147. static void icount_adjust(void)
  148. {
  149. int64_t cur_time;
  150. int64_t cur_icount;
  151. int64_t delta;
  152. /* If the VM is not running, then do nothing. */
  153. if (!runstate_is_running()) {
  154. return;
  155. }
  156. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  157. &timers_state.vm_clock_lock);
  158. cur_time = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
  159. cpu_get_clock_locked());
  160. cur_icount = icount_get_locked();
  161. delta = cur_icount - cur_time;
  162. /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
  163. if (delta > 0
  164. && timers_state.last_delta + ICOUNT_WOBBLE < delta * 2
  165. && timers_state.icount_time_shift > 0) {
  166. /* The guest is getting too far ahead. Slow time down. */
  167. qatomic_set(&timers_state.icount_time_shift,
  168. timers_state.icount_time_shift - 1);
  169. }
  170. if (delta < 0
  171. && timers_state.last_delta - ICOUNT_WOBBLE > delta * 2
  172. && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
  173. /* The guest is getting too far behind. Speed time up. */
  174. qatomic_set(&timers_state.icount_time_shift,
  175. timers_state.icount_time_shift + 1);
  176. }
  177. timers_state.last_delta = delta;
  178. qatomic_set_i64(&timers_state.qemu_icount_bias,
  179. cur_icount - (timers_state.qemu_icount
  180. << timers_state.icount_time_shift));
  181. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  182. &timers_state.vm_clock_lock);
  183. }
  184. static void icount_adjust_rt(void *opaque)
  185. {
  186. timer_mod(timers_state.icount_rt_timer,
  187. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
  188. icount_adjust();
  189. }
  190. static void icount_adjust_vm(void *opaque)
  191. {
  192. timer_mod(timers_state.icount_vm_timer,
  193. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  194. NANOSECONDS_PER_SECOND / 10);
  195. icount_adjust();
  196. }
  197. int64_t icount_round(int64_t count)
  198. {
  199. int shift = qatomic_read(&timers_state.icount_time_shift);
  200. return (count + (1 << shift) - 1) >> shift;
  201. }
  202. static void icount_warp_rt(void)
  203. {
  204. unsigned seq;
  205. int64_t warp_start;
  206. /*
  207. * The icount_warp_timer is rescheduled soon after vm_clock_warp_start
  208. * changes from -1 to another value, so the race here is okay.
  209. */
  210. do {
  211. seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
  212. warp_start = timers_state.vm_clock_warp_start;
  213. } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
  214. if (warp_start == -1) {
  215. return;
  216. }
  217. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  218. &timers_state.vm_clock_lock);
  219. if (runstate_is_running()) {
  220. int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
  221. cpu_get_clock_locked());
  222. int64_t warp_delta;
  223. warp_delta = clock - timers_state.vm_clock_warp_start;
  224. if (icount_enabled() == ICOUNT_ADAPTATIVE) {
  225. /*
  226. * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too far
  227. * ahead of real time (it might already be ahead so careful not
  228. * to go backwards).
  229. */
  230. int64_t cur_icount = icount_get_locked();
  231. int64_t delta = clock - cur_icount;
  232. if (delta < 0) {
  233. delta = 0;
  234. }
  235. warp_delta = MIN(warp_delta, delta);
  236. }
  237. qatomic_set_i64(&timers_state.qemu_icount_bias,
  238. timers_state.qemu_icount_bias + warp_delta);
  239. }
  240. timers_state.vm_clock_warp_start = -1;
  241. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  242. &timers_state.vm_clock_lock);
  243. if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
  244. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  245. }
  246. }
  247. static void icount_timer_cb(void *opaque)
  248. {
  249. /*
  250. * No need for a checkpoint because the timer already synchronizes
  251. * with CHECKPOINT_CLOCK_VIRTUAL_RT.
  252. */
  253. icount_warp_rt();
  254. }
  255. void icount_start_warp_timer(void)
  256. {
  257. int64_t clock;
  258. int64_t deadline;
  259. assert(icount_enabled());
  260. /*
  261. * Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
  262. * do not fire, so computing the deadline does not make sense.
  263. */
  264. if (!runstate_is_running()) {
  265. return;
  266. }
  267. if (replay_mode != REPLAY_MODE_PLAY) {
  268. if (!all_cpu_threads_idle()) {
  269. return;
  270. }
  271. if (qtest_enabled()) {
  272. /* When testing, qtest commands advance icount. */
  273. return;
  274. }
  275. replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
  276. } else {
  277. /* warp clock deterministically in record/replay mode */
  278. if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
  279. /*
  280. * vCPU is sleeping and warp can't be started.
  281. * It is probably a race condition: notification sent
  282. * to vCPU was processed in advance and vCPU went to sleep.
  283. * Therefore we have to wake it up for doing something.
  284. */
  285. if (replay_has_event()) {
  286. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  287. }
  288. return;
  289. }
  290. }
  291. /* We want to use the earliest deadline from ALL vm_clocks */
  292. clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
  293. deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
  294. ~QEMU_TIMER_ATTR_EXTERNAL);
  295. if (deadline < 0) {
  296. if (!icount_sleep) {
  297. warn_report_once("icount sleep disabled and no active timers");
  298. }
  299. return;
  300. }
  301. if (deadline > 0) {
  302. /*
  303. * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
  304. * sleep. Otherwise, the CPU might be waiting for a future timer
  305. * interrupt to wake it up, but the interrupt never comes because
  306. * the vCPU isn't running any insns and thus doesn't advance the
  307. * QEMU_CLOCK_VIRTUAL.
  308. */
  309. if (!icount_sleep) {
  310. /*
  311. * We never let VCPUs sleep in no sleep icount mode.
  312. * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
  313. * to the next QEMU_CLOCK_VIRTUAL event and notify it.
  314. * It is useful when we want a deterministic execution time,
  315. * isolated from host latencies.
  316. */
  317. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  318. &timers_state.vm_clock_lock);
  319. qatomic_set_i64(&timers_state.qemu_icount_bias,
  320. timers_state.qemu_icount_bias + deadline);
  321. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  322. &timers_state.vm_clock_lock);
  323. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  324. } else {
  325. /*
  326. * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
  327. * "real" time, (related to the time left until the next event) has
  328. * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
  329. * This avoids that the warps are visible externally; for example,
  330. * you will not be sending network packets continuously instead of
  331. * every 100ms.
  332. */
  333. seqlock_write_lock(&timers_state.vm_clock_seqlock,
  334. &timers_state.vm_clock_lock);
  335. if (timers_state.vm_clock_warp_start == -1
  336. || timers_state.vm_clock_warp_start > clock) {
  337. timers_state.vm_clock_warp_start = clock;
  338. }
  339. seqlock_write_unlock(&timers_state.vm_clock_seqlock,
  340. &timers_state.vm_clock_lock);
  341. timer_mod_anticipate(timers_state.icount_warp_timer,
  342. clock + deadline);
  343. }
  344. } else if (deadline == 0) {
  345. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  346. }
  347. }
  348. void icount_account_warp_timer(void)
  349. {
  350. if (!icount_sleep) {
  351. return;
  352. }
  353. /*
  354. * Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
  355. * do not fire, so computing the deadline does not make sense.
  356. */
  357. if (!runstate_is_running()) {
  358. return;
  359. }
  360. replay_async_events();
  361. /* warp clock deterministically in record/replay mode */
  362. if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
  363. return;
  364. }
  365. timer_del(timers_state.icount_warp_timer);
  366. icount_warp_rt();
  367. }
  368. bool icount_configure(QemuOpts *opts, Error **errp)
  369. {
  370. const char *option = qemu_opt_get(opts, "shift");
  371. bool sleep = qemu_opt_get_bool(opts, "sleep", true);
  372. bool align = qemu_opt_get_bool(opts, "align", false);
  373. long time_shift = -1;
  374. if (!option) {
  375. if (qemu_opt_get(opts, "align") != NULL) {
  376. error_setg(errp, "Please specify shift option when using align");
  377. return false;
  378. }
  379. return true;
  380. }
  381. if (align && !sleep) {
  382. error_setg(errp, "align=on and sleep=off are incompatible");
  383. return false;
  384. }
  385. if (strcmp(option, "auto") != 0) {
  386. if (qemu_strtol(option, NULL, 0, &time_shift) < 0
  387. || time_shift < 0 || time_shift > MAX_ICOUNT_SHIFT) {
  388. error_setg(errp, "icount: Invalid shift value");
  389. return false;
  390. }
  391. } else if (icount_align_option) {
  392. error_setg(errp, "shift=auto and align=on are incompatible");
  393. return false;
  394. } else if (!icount_sleep) {
  395. error_setg(errp, "shift=auto and sleep=off are incompatible");
  396. return false;
  397. }
  398. icount_sleep = sleep;
  399. if (icount_sleep) {
  400. timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
  401. icount_timer_cb, NULL);
  402. }
  403. icount_align_option = align;
  404. if (time_shift >= 0) {
  405. timers_state.icount_time_shift = time_shift;
  406. icount_enable_precise();
  407. return true;
  408. }
  409. icount_enable_adaptive();
  410. /*
  411. * 125MIPS seems a reasonable initial guess at the guest speed.
  412. * It will be corrected fairly quickly anyway.
  413. */
  414. timers_state.icount_time_shift = 3;
  415. /*
  416. * Have both realtime and virtual time triggers for speed adjustment.
  417. * The realtime trigger catches emulated time passing too slowly,
  418. * the virtual time trigger catches emulated time passing too fast.
  419. * Realtime triggers occur even when idle, so use them less frequently
  420. * than VM triggers.
  421. */
  422. timers_state.vm_clock_warp_start = -1;
  423. timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
  424. icount_adjust_rt, NULL);
  425. timer_mod(timers_state.icount_rt_timer,
  426. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
  427. timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  428. icount_adjust_vm, NULL);
  429. timer_mod(timers_state.icount_vm_timer,
  430. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  431. NANOSECONDS_PER_SECOND / 10);
  432. return true;
  433. }
  434. void icount_notify_exit(void)
  435. {
  436. assert(icount_enabled());
  437. if (current_cpu) {
  438. qemu_cpu_kick(current_cpu);
  439. qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
  440. }
  441. }