]> git.proxmox.com Git - mirror_qemu.git/blame - softmmu/icount.c
bsd-user: Implement getrlimit(2) and setrlimit(2)
[mirror_qemu.git] / softmmu / icount.c
CommitLineData
740b1759
CF
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
25#include "qemu/osdep.h"
740b1759
CF
26#include "qemu/cutils.h"
27#include "migration/vmstate.h"
28#include "qapi/error.h"
29#include "qemu/error-report.h"
30#include "exec/exec-all.h"
31#include "sysemu/cpus.h"
32#include "sysemu/qtest.h"
33#include "qemu/main-loop.h"
34#include "qemu/option.h"
35#include "qemu/seqlock.h"
36#include "sysemu/replay.h"
37#include "sysemu/runstate.h"
38#include "hw/core/cpu.h"
39#include "sysemu/cpu-timers.h"
40#include "sysemu/cpu-throttle.h"
41#include "timers-state.h"
42
43/*
44 * ICOUNT: Instruction Counter
45 *
46 * this module is split off from cpu-timers because the icount part
47 * is TCG-specific, and does not need to be built for other accels.
48 */
49static bool icount_sleep = true;
50/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
51#define MAX_ICOUNT_SHIFT 10
52
53/*
54 * 0 = Do not count executed instructions.
55 * 1 = Fixed conversion of insn to ns via "shift" option
56 * 2 = Runtime adaptive algorithm to compute shift
57 */
58int use_icount;
59
60static void icount_enable_precise(void)
61{
62 use_icount = 1;
63}
64
65static void icount_enable_adaptive(void)
66{
67 use_icount = 2;
68}
69
70/*
71 * The current number of executed instructions is based on what we
72 * originally budgeted minus the current state of the decrementing
73 * icount counters in extra/u16.low.
74 */
8191d368 75static int64_t icount_get_executed(CPUState *cpu)
740b1759
CF
76{
77 return (cpu->icount_budget -
78 (cpu_neg(cpu)->icount_decr.u16.low + cpu->icount_extra));
79}
80
81/*
82 * Update the global shared timer_state.qemu_icount to take into
83 * account executed instructions. This is done by the TCG vCPU
84 * thread so the main-loop can see time has moved forward.
85 */
8191d368 86static void icount_update_locked(CPUState *cpu)
740b1759 87{
8191d368 88 int64_t executed = icount_get_executed(cpu);
740b1759
CF
89 cpu->icount_budget -= executed;
90
91 qatomic_set_i64(&timers_state.qemu_icount,
92 timers_state.qemu_icount + executed);
93}
94
95/*
96 * Update the global shared timer_state.qemu_icount to take into
97 * account executed instructions. This is done by the TCG vCPU
98 * thread so the main-loop can see time has moved forward.
99 */
8191d368 100void icount_update(CPUState *cpu)
740b1759
CF
101{
102 seqlock_write_lock(&timers_state.vm_clock_seqlock,
103 &timers_state.vm_clock_lock);
8191d368 104 icount_update_locked(cpu);
740b1759
CF
105 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
106 &timers_state.vm_clock_lock);
107}
108
8191d368 109static int64_t icount_get_raw_locked(void)
740b1759
CF
110{
111 CPUState *cpu = current_cpu;
112
113 if (cpu && cpu->running) {
114 if (!cpu->can_do_io) {
115 error_report("Bad icount read");
116 exit(1);
117 }
118 /* Take into account what has run */
8191d368 119 icount_update_locked(cpu);
740b1759
CF
120 }
121 /* The read is protected by the seqlock, but needs atomic64 to avoid UB */
122 return qatomic_read_i64(&timers_state.qemu_icount);
123}
124
8191d368 125static int64_t icount_get_locked(void)
740b1759 126{
8191d368 127 int64_t icount = icount_get_raw_locked();
740b1759 128 return qatomic_read_i64(&timers_state.qemu_icount_bias) +
8191d368 129 icount_to_ns(icount);
740b1759
CF
130}
131
8191d368 132int64_t icount_get_raw(void)
740b1759
CF
133{
134 int64_t icount;
135 unsigned start;
136
137 do {
138 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
8191d368 139 icount = icount_get_raw_locked();
740b1759
CF
140 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
141
142 return icount;
143}
144
145/* Return the virtual CPU time, based on the instruction counter. */
8191d368 146int64_t icount_get(void)
740b1759
CF
147{
148 int64_t icount;
149 unsigned start;
150
151 do {
152 start = seqlock_read_begin(&timers_state.vm_clock_seqlock);
8191d368 153 icount = icount_get_locked();
740b1759
CF
154 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, start));
155
156 return icount;
157}
158
8191d368 159int64_t icount_to_ns(int64_t icount)
740b1759
CF
160{
161 return icount << qatomic_read(&timers_state.icount_time_shift);
162}
163
164/*
165 * Correlation between real and virtual time is always going to be
166 * fairly approximate, so ignore small variation.
167 * When the guest is idle real and virtual time will be aligned in
168 * the IO wait loop.
169 */
170#define ICOUNT_WOBBLE (NANOSECONDS_PER_SECOND / 10)
171
172static void icount_adjust(void)
173{
174 int64_t cur_time;
175 int64_t cur_icount;
176 int64_t delta;
177
740b1759
CF
178 /* If the VM is not running, then do nothing. */
179 if (!runstate_is_running()) {
180 return;
181 }
182
183 seqlock_write_lock(&timers_state.vm_clock_seqlock,
184 &timers_state.vm_clock_lock);
185 cur_time = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
186 cpu_get_clock_locked());
8191d368 187 cur_icount = icount_get_locked();
740b1759
CF
188
189 delta = cur_icount - cur_time;
190 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
191 if (delta > 0
fe852ac2 192 && timers_state.last_delta + ICOUNT_WOBBLE < delta * 2
740b1759
CF
193 && timers_state.icount_time_shift > 0) {
194 /* The guest is getting too far ahead. Slow time down. */
195 qatomic_set(&timers_state.icount_time_shift,
196 timers_state.icount_time_shift - 1);
197 }
198 if (delta < 0
fe852ac2 199 && timers_state.last_delta - ICOUNT_WOBBLE > delta * 2
740b1759
CF
200 && timers_state.icount_time_shift < MAX_ICOUNT_SHIFT) {
201 /* The guest is getting too far behind. Speed time up. */
202 qatomic_set(&timers_state.icount_time_shift,
203 timers_state.icount_time_shift + 1);
204 }
fe852ac2 205 timers_state.last_delta = delta;
740b1759
CF
206 qatomic_set_i64(&timers_state.qemu_icount_bias,
207 cur_icount - (timers_state.qemu_icount
208 << timers_state.icount_time_shift));
209 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
210 &timers_state.vm_clock_lock);
211}
212
213static void icount_adjust_rt(void *opaque)
214{
215 timer_mod(timers_state.icount_rt_timer,
216 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
217 icount_adjust();
218}
219
220static void icount_adjust_vm(void *opaque)
221{
222 timer_mod(timers_state.icount_vm_timer,
223 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
224 NANOSECONDS_PER_SECOND / 10);
225 icount_adjust();
226}
227
8191d368 228int64_t icount_round(int64_t count)
740b1759
CF
229{
230 int shift = qatomic_read(&timers_state.icount_time_shift);
231 return (count + (1 << shift) - 1) >> shift;
232}
233
234static void icount_warp_rt(void)
235{
236 unsigned seq;
237 int64_t warp_start;
238
239 /*
240 * The icount_warp_timer is rescheduled soon after vm_clock_warp_start
241 * changes from -1 to another value, so the race here is okay.
242 */
243 do {
244 seq = seqlock_read_begin(&timers_state.vm_clock_seqlock);
245 warp_start = timers_state.vm_clock_warp_start;
246 } while (seqlock_read_retry(&timers_state.vm_clock_seqlock, seq));
247
248 if (warp_start == -1) {
249 return;
250 }
251
252 seqlock_write_lock(&timers_state.vm_clock_seqlock,
253 &timers_state.vm_clock_lock);
254 if (runstate_is_running()) {
255 int64_t clock = REPLAY_CLOCK_LOCKED(REPLAY_CLOCK_VIRTUAL_RT,
256 cpu_get_clock_locked());
257 int64_t warp_delta;
258
259 warp_delta = clock - timers_state.vm_clock_warp_start;
260 if (icount_enabled() == 2) {
261 /*
67f85346
NP
262 * In adaptive mode, do not let QEMU_CLOCK_VIRTUAL run too far
263 * ahead of real time (it might already be ahead so careful not
264 * to go backwards).
740b1759 265 */
8191d368 266 int64_t cur_icount = icount_get_locked();
740b1759 267 int64_t delta = clock - cur_icount;
67f85346
NP
268
269 if (delta < 0) {
270 delta = 0;
271 }
740b1759
CF
272 warp_delta = MIN(warp_delta, delta);
273 }
274 qatomic_set_i64(&timers_state.qemu_icount_bias,
275 timers_state.qemu_icount_bias + warp_delta);
276 }
277 timers_state.vm_clock_warp_start = -1;
278 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
279 &timers_state.vm_clock_lock);
280
281 if (qemu_clock_expired(QEMU_CLOCK_VIRTUAL)) {
282 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
283 }
284}
285
286static void icount_timer_cb(void *opaque)
287{
288 /*
289 * No need for a checkpoint because the timer already synchronizes
290 * with CHECKPOINT_CLOCK_VIRTUAL_RT.
291 */
292 icount_warp_rt();
293}
294
8191d368 295void icount_start_warp_timer(void)
740b1759
CF
296{
297 int64_t clock;
298 int64_t deadline;
299
300 assert(icount_enabled());
301
302 /*
303 * Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
304 * do not fire, so computing the deadline does not make sense.
305 */
306 if (!runstate_is_running()) {
307 return;
308 }
309
310 if (replay_mode != REPLAY_MODE_PLAY) {
311 if (!all_cpu_threads_idle()) {
312 return;
313 }
314
315 if (qtest_enabled()) {
316 /* When testing, qtest commands advance icount. */
317 return;
318 }
319
320 replay_checkpoint(CHECKPOINT_CLOCK_WARP_START);
321 } else {
322 /* warp clock deterministically in record/replay mode */
323 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_START)) {
324 /*
325 * vCPU is sleeping and warp can't be started.
326 * It is probably a race condition: notification sent
327 * to vCPU was processed in advance and vCPU went to sleep.
669dcb60 328 * Therefore we have to wake it up for doing something.
740b1759 329 */
60618e2d 330 if (replay_has_event()) {
740b1759
CF
331 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
332 }
333 return;
334 }
335 }
336
337 /* We want to use the earliest deadline from ALL vm_clocks */
338 clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
339 deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL,
340 ~QEMU_TIMER_ATTR_EXTERNAL);
341 if (deadline < 0) {
342 static bool notified;
343 if (!icount_sleep && !notified) {
344 warn_report("icount sleep disabled and no active timers");
345 notified = true;
346 }
347 return;
348 }
349
350 if (deadline > 0) {
351 /*
352 * Ensure QEMU_CLOCK_VIRTUAL proceeds even when the virtual CPU goes to
353 * sleep. Otherwise, the CPU might be waiting for a future timer
354 * interrupt to wake it up, but the interrupt never comes because
355 * the vCPU isn't running any insns and thus doesn't advance the
356 * QEMU_CLOCK_VIRTUAL.
357 */
358 if (!icount_sleep) {
359 /*
360 * We never let VCPUs sleep in no sleep icount mode.
361 * If there is a pending QEMU_CLOCK_VIRTUAL timer we just advance
362 * to the next QEMU_CLOCK_VIRTUAL event and notify it.
363 * It is useful when we want a deterministic execution time,
364 * isolated from host latencies.
365 */
366 seqlock_write_lock(&timers_state.vm_clock_seqlock,
367 &timers_state.vm_clock_lock);
368 qatomic_set_i64(&timers_state.qemu_icount_bias,
369 timers_state.qemu_icount_bias + deadline);
370 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
371 &timers_state.vm_clock_lock);
372 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
373 } else {
374 /*
375 * We do stop VCPUs and only advance QEMU_CLOCK_VIRTUAL after some
376 * "real" time, (related to the time left until the next event) has
377 * passed. The QEMU_CLOCK_VIRTUAL_RT clock will do this.
378 * This avoids that the warps are visible externally; for example,
379 * you will not be sending network packets continuously instead of
380 * every 100ms.
381 */
382 seqlock_write_lock(&timers_state.vm_clock_seqlock,
383 &timers_state.vm_clock_lock);
384 if (timers_state.vm_clock_warp_start == -1
385 || timers_state.vm_clock_warp_start > clock) {
386 timers_state.vm_clock_warp_start = clock;
387 }
388 seqlock_write_unlock(&timers_state.vm_clock_seqlock,
389 &timers_state.vm_clock_lock);
390 timer_mod_anticipate(timers_state.icount_warp_timer,
391 clock + deadline);
392 }
393 } else if (deadline == 0) {
394 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
395 }
396}
397
8191d368 398void icount_account_warp_timer(void)
740b1759 399{
45e077d7 400 if (!icount_sleep) {
740b1759
CF
401 return;
402 }
403
404 /*
405 * Nothing to do if the VM is stopped: QEMU_CLOCK_VIRTUAL timers
406 * do not fire, so computing the deadline does not make sense.
407 */
408 if (!runstate_is_running()) {
409 return;
410 }
411
60618e2d
PD
412 replay_async_events();
413
740b1759
CF
414 /* warp clock deterministically in record/replay mode */
415 if (!replay_checkpoint(CHECKPOINT_CLOCK_WARP_ACCOUNT)) {
416 return;
417 }
418
419 timer_del(timers_state.icount_warp_timer);
420 icount_warp_rt();
421}
422
8191d368 423void icount_configure(QemuOpts *opts, Error **errp)
740b1759
CF
424{
425 const char *option = qemu_opt_get(opts, "shift");
426 bool sleep = qemu_opt_get_bool(opts, "sleep", true);
427 bool align = qemu_opt_get_bool(opts, "align", false);
428 long time_shift = -1;
429
430 if (!option) {
431 if (qemu_opt_get(opts, "align") != NULL) {
432 error_setg(errp, "Please specify shift option when using align");
433 }
434 return;
435 }
436
437 if (align && !sleep) {
438 error_setg(errp, "align=on and sleep=off are incompatible");
439 return;
440 }
441
442 if (strcmp(option, "auto") != 0) {
443 if (qemu_strtol(option, NULL, 0, &time_shift) < 0
444 || time_shift < 0 || time_shift > MAX_ICOUNT_SHIFT) {
445 error_setg(errp, "icount: Invalid shift value");
446 return;
447 }
448 } else if (icount_align_option) {
449 error_setg(errp, "shift=auto and align=on are incompatible");
450 return;
451 } else if (!icount_sleep) {
452 error_setg(errp, "shift=auto and sleep=off are incompatible");
453 return;
454 }
455
456 icount_sleep = sleep;
457 if (icount_sleep) {
458 timers_state.icount_warp_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL_RT,
459 icount_timer_cb, NULL);
460 }
461
462 icount_align_option = align;
463
464 if (time_shift >= 0) {
465 timers_state.icount_time_shift = time_shift;
466 icount_enable_precise();
467 return;
468 }
469
470 icount_enable_adaptive();
471
472 /*
473 * 125MIPS seems a reasonable initial guess at the guest speed.
474 * It will be corrected fairly quickly anyway.
475 */
476 timers_state.icount_time_shift = 3;
477
478 /*
479 * Have both realtime and virtual time triggers for speed adjustment.
480 * The realtime trigger catches emulated time passing too slowly,
481 * the virtual time trigger catches emulated time passing too fast.
482 * Realtime triggers occur even when idle, so use them less frequently
483 * than VM triggers.
484 */
485 timers_state.vm_clock_warp_start = -1;
486 timers_state.icount_rt_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL_RT,
487 icount_adjust_rt, NULL);
488 timer_mod(timers_state.icount_rt_timer,
489 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL_RT) + 1000);
490 timers_state.icount_vm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
491 icount_adjust_vm, NULL);
492 timer_mod(timers_state.icount_vm_timer,
493 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
494 NANOSECONDS_PER_SECOND / 10);
495}
75bbe5e5
PD
496
497void icount_notify_exit(void)
498{
499 if (icount_enabled() && current_cpu) {
500 qemu_cpu_kick(current_cpu);
501 qemu_clock_notify(QEMU_CLOCK_VIRTUAL);
502 }
503}