icount and misc MTTCG fixes for 2.9-rc4
v3 cover icount and misc MTTCG fixes for 2.9-rc4
Hi,
This doesn't change much from v2 earlier this week:
https://lists.nongnu.org/archive/html/qemu-devel/2017-04/msg00661.html
There was a minor improvement to the cpu_update_icount to not use a potentially expensive atomic_add (which is CST) when you can get away with weaker semantics with a single writer.
I've also fixed a compile error on 32bit guests. It only falls back to non-atomic code if CONFIG_ATOMIC64 is not set which isn't the case for ARMv7 as it can use load/store-exclusive pair to do the operation. This does require using the __no_check atomic variants though.
I still plan to roll a pull-request on Monday and get it sent to Peter in good time for cutting -rc4.
Cheers,
Alex Bennée (12):
scripts/qemugdb/mtree.py: fix up mtree dump scripts/qemu-gdb/timers.py: new helper to dump timer state scripts/replay-dump.py: replay log dumper target/i386/misc_helper: wrap BQL around another IRQ generator
cpus: remove icount handling from qemu_tcg_cpu_thread_fn
cpus: check cpu->running in cpu_get_icount_raw()
cpus: move icount preparation out of tcg_exec_cpu
cpus: don't credit executed instructions before they have run
cpus: introduce cpu_update_icount helper
cpu-exec: update icount after each TB_EXIT
cpus: call cpu_update_icount on read
replay: assert time only goes forward
cpu-exec.c | 14 +-- cpus.c | 109 ++++++++++++++----- include/qemu/timer.h | 1 + include/qom/cpu.h | 1 + replay/replay-internal.c | 4 + replay/replay.c | 4 + scripts/qemu-gdb.py | 3 +- scripts/qemugdb/mtree.py | 12 +- scripts/qemugdb/timers.py | 54 +++++++++ scripts/replay-dump.py | 272 ++++++++++++++++++++++++++++++++++++++++++++++ target/i386/misc_helper.c | 3 + 11 files changed, 437 insertions(+), 40 deletions(-) create mode 100644 scripts/qemugdb/timers.py create mode 100755 scripts/replay-dump.py
Signature · 1 line
2.11.0
PATCH v3 1/12 scripts/qemugdb/mtree.py: fix up mtree dump
Since QEMU has been able to build with native Int128 support this was broken as it attempts to fish values out of the non-existent structure. Also the alias print was trying to make a %x out of gdb.ValueType directly which didn't seem to work.
--- scripts/qemugdb/mtree.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
scripts/qemugdb/mtree.py +10 -2
@@ -21,7 +21,15 @@ def isnull(ptr): return ptr == gdb.Value(0).cast(ptr.type) def int128(p): - return int(p['lo']) + (int(p['hi']) << 64) + '''Read an Int128 type to a python integer. + + QEMU can be built with native Int128 support so we need to detect + if the value is a structure or the native type. + ''' + if p.type.code == gdb.TYPE_CODE_STRUCT: + return int(p['lo']) + (int(p['hi']) << 64) + else: + return int(("%s" % p), 16) class MtreeCommand(gdb.Command): '''Display the memory tree hierarchy'''
@@ -69,7 +77,7 @@ class MtreeCommand(gdb.Command): gdb.write('%s alias: %s@%016x (@ %s)\n' % (' ' * level, alias['name'].string(), - ptr['alias_offset'], + int(ptr['alias_offset']), alias, ), gdb.STDOUT)
Signature · 1 line
2.11.0
PATCH v3 2/12 scripts/qemu-gdb/timers.py: new helper to dump timer state
In reply to Alex BennéeThis introduces the qemu-gdb command "qemu timers" which will dump the state of the main timers in the system.
--- scripts/qemu-gdb.py | 3 ++- scripts/qemugdb/timers.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 scripts/qemugdb/timers.py
scripts/qemu-gdb.py +2 -1
@@ -26,7 +26,7 @@ import os, sys sys.path.append(os.path.dirname(__file__)) -from qemugdb import aio, mtree, coroutine +from qemugdb import aio, mtree, coroutine, timers class QemuCommand(gdb.Command): '''Prefix for QEMU debug support commands'''
@@ -38,6 +38,7 @@ QemuCommand() coroutine.CoroutineCommand() mtree.MtreeCommand() aio.HandlersCommand() +timers.TimersCommand() coroutine.CoroutineSPFunction() coroutine.CoroutinePCFunction()
scripts/qemugdb/timers.py new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH v3 3/12 scripts/replay-dump.py: replay log dumper
In reply to Alex BennéeThis script is a debugging tool for looking through the contents of a replay log file. It is incomplete but should fail gracefully at events it doesn't understand.
It currently understands two different log formats as the audio record/replay support was merged during since MTTCG. It was written to help debug what has caused the BQL changes to break replay support.
--- scripts/replay-dump.py | 272 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100755 scripts/replay-dump.py
scripts/replay-dump.py new file +272 -0
@@ -0,0 +1,272 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# Dump the contents of a recorded execution stream +# +# Copyright (c) 2017 Alex Bennée <[email protected]> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, see <http://www.gnu.org/licenses/>. + +import argparse +import struct +from collections import namedtuple + +# This mirrors some of the global replay state which some of the +# stream loading refers to. Some decoders may read the next event so +# we need handle that case. Calling reuse_event will ensure the next +# event is read from the cache rather than advancing the file. + +class ReplayState(object): + def __init__(self): + self.event = -1 + self.event_count = 0 + self.already_read = False + self.current_checkpoint = 0 + self.checkpoint = 0 + + def set_event(self, ev): + self.event = ev + self.event_count += 1 + + def get_event(self): + self.already_read = False + return self.event + + def reuse_event(self, ev): + self.event = ev + self.already_read = True + + def set_checkpoint(self): + self.checkpoint = self.event - self.checkpoint_start + + def get_checkpoint(self): + return self.checkpoint + +replay_state = ReplayState() + +# Simple read functions that mirror replay-internal.c +# The file-stream is big-endian and manually written out a byte at a time. + +def read_byte(fin): + "Read a single byte" + return struct.unpack('>B', fin.read(1))[0] + +def read_event(fin): + "Read a single byte event, but save some state" + if replay_state.already_read: + return replay_state.get_event() + else: + replay_state.set_event(read_byte(fin)) + return replay_state.event + +def read_word(fin): + "Read a 16 bit word" + return struct.unpack('>H', fin.read(2))[0] + +def read_dword(fin): + "Read a 32 bit word" + return struct.unpack('>I', fin.read(4))[0] + +def read_qword(fin): + "Read a 64 bit word" + return struct.unpack('>Q', fin.read(8))[0] + +# Generic decoder structure +Decoder = namedtuple("Decoder", "eid name fn") + +def call_decode(table, index, dumpfile): + "Search decode table for next step" + decoder = next((d for d in table if d.eid == index), None) + if not decoder: + print "Could not decode index: %d" % (index) + print "Entry is: %s" % (decoder) + print "Decode Table is:\n%s" % (table) + return False + else: + return decoder.fn(decoder.eid, decoder.name, dumpfile) + +# Print event +def print_event(eid, name, string=None, event_count=None): + "Print event with count" + if not event_count: + event_count = replay_state.event_count + + if string: + print "%d:%s(%d) %s" % (event_count, name, eid, string) + else: + print "%d:%s(%d)" % (event_count, name, eid) + + +# Decoders for each event type + +def decode_unimp(eid, name, _unused_dumpfile): + "Unimplimented decoder, will trigger exit" + print "%s not handled - will now stop" % (name) + return False + +# Checkpoint decoder +def swallow_async_qword(eid, name, dumpfile): + "Swallow a qword of data without looking at it" + step_id = read_qword(dumpfile) + print " %s(%d) @ %d" % (name, eid, step_id) + return True + +async_decode_table = [ Decoder(0, "REPLAY_ASYNC_EVENT_BH", swallow_async_qword), + Decoder(1, "REPLAY_ASYNC_INPUT", decode_unimp), + Decoder(2, "REPLAY_ASYNC_INPUT_SYNC", decode_unimp), + Decoder(3, "REPLAY_ASYNC_CHAR_READ", decode_unimp), + Decoder(4, "REPLAY_ASYNC_EVENT_BLOCK", decode_unimp), + Decoder(5, "REPLAY_ASYNC_EVENT_NET", decode_unimp), +] +# See replay_read_events/replay_read_event +def decode_async(eid, name, dumpfile): + """Decode an ASYNC event""" + + print_event(eid, name) + + async_event_kind = read_byte(dumpfile) + async_event_checkpoint = read_byte(dumpfile) + + if async_event_checkpoint != replay_state.current_checkpoint: + print " mismatch between checkpoint %d and async data %d" % ( + replay_state.current_checkpoint, async_event_checkpoint) + return True + + return call_decode(async_decode_table, async_event_kind, dumpfile) + + +def decode_instruction(eid, name, dumpfile): + ins_diff = read_dword(dumpfile) + print_event(eid, name, "0x%x" % (ins_diff)) + return True + +def decode_audio_out(eid, name, dumpfile): + audio_data = read_dword(dumpfile) + print_event(eid, name, "%d" % (audio_data)) + return True + +def decode_checkpoint(eid, name, dumpfile): + """Decode a checkpoint. + + Checkpoints contain a series of async events with their own specific data. + """ + replay_state.set_checkpoint() + # save event count as we peek ahead + event_number = replay_state.event_count + next_event = read_event(dumpfile) + + # if the next event is EVENT_ASYNC there are a bunch of + # async events to read, otherwise we are done + if next_event != 3: + print_event(eid, name, "no additional data", event_number) + else: + print_event(eid, name, "more data follows", event_number) + + replay_state.reuse_event(next_event) + return True + +def decode_checkpoint_init(eid, name, dumpfile): + print_event(eid, name) + return True + +def decode_interrupt(eid, name, dumpfile): + print_event(eid, name) + return True + +def decode_clock(eid, name, dumpfile): + clock_data = read_qword(dumpfile) + print_event(eid, name, "0x%x" % (clock_data)) + return True + + +# pre-MTTCG merge +v5_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction), + Decoder(1, "EVENT_INTERRUPT", decode_interrupt), + Decoder(2, "EVENT_EXCEPTION", decode_unimp), + Decoder(3, "EVENT_ASYNC", decode_async), + Decoder(4, "EVENT_SHUTDOWN", decode_unimp), + Decoder(5, "EVENT_CHAR_WRITE", decode_unimp), + Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp), + Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp), + Decoder(8, "EVENT_CLOCK_HOST", decode_clock), + Decoder(9, "EVENT_CLOCK_VIRTUAL_RT", decode_clock), + Decoder(10, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint), + Decoder(11, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint), + Decoder(12, "EVENT_CP_RESET_REQUESTED", decode_checkpoint), + Decoder(13, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint), + Decoder(14, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint), + Decoder(15, "EVENT_CP_CLOCK_HOST", decode_checkpoint), + Decoder(16, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint), + Decoder(17, "EVENT_CP_INIT", decode_checkpoint_init), + Decoder(18, "EVENT_CP_RESET", decode_checkpoint), +] + +# post-MTTCG merge, AUDIO support added +v6_event_table = [Decoder(0, "EVENT_INSTRUCTION", decode_instruction), + Decoder(1, "EVENT_INTERRUPT", decode_interrupt), + Decoder(2, "EVENT_EXCEPTION", decode_unimp), + Decoder(3, "EVENT_ASYNC", decode_async), + Decoder(4, "EVENT_SHUTDOWN", decode_unimp), + Decoder(5, "EVENT_CHAR_WRITE", decode_unimp), + Decoder(6, "EVENT_CHAR_READ_ALL", decode_unimp), + Decoder(7, "EVENT_CHAR_READ_ALL_ERROR", decode_unimp), + Decoder(8, "EVENT_AUDIO_OUT", decode_audio_out), + Decoder(9, "EVENT_AUDIO_IN", decode_unimp), + Decoder(10, "EVENT_CLOCK_HOST", decode_clock), + Decoder(11, "EVENT_CLOCK_VIRTUAL_RT", decode_clock), + Decoder(12, "EVENT_CP_CLOCK_WARP_START", decode_checkpoint), + Decoder(13, "EVENT_CP_CLOCK_WARP_ACCOUNT", decode_checkpoint), + Decoder(14, "EVENT_CP_RESET_REQUESTED", decode_checkpoint), + Decoder(15, "EVENT_CP_SUSPEND_REQUESTED", decode_checkpoint), + Decoder(16, "EVENT_CP_CLOCK_VIRTUAL", decode_checkpoint), + Decoder(17, "EVENT_CP_CLOCK_HOST", decode_checkpoint), + Decoder(18, "EVENT_CP_CLOCK_VIRTUAL_RT", decode_checkpoint), + Decoder(19, "EVENT_CP_INIT", decode_checkpoint_init), + Decoder(20, "EVENT_CP_RESET", decode_checkpoint), +] + +def parse_arguments(): + "Grab arguments for script" + parser = argparse.ArgumentParser() + parser.add_argument("-f", "--file", help='record/replay dump to read from', + required=True) + return parser.parse_args() + +def decode_file(filename): + "Decode a record/replay dump" + dumpfile = open(filename, "rb") + + # read and throwaway the header + version = read_dword(dumpfile) + junk = read_qword(dumpfile) + + print "HEADER: version 0x%x" % (version) + if version == 0xe02006: + event_decode_table = v6_event_table + replay_state.checkpoint_start = 12 + else: + event_decode_table = v5_event_table + replay_state.checkpoint_start = 10 + + try: + decode_ok = True + while decode_ok: + event = read_event(dumpfile) + decode_ok = call_decode(event_decode_table, event, dumpfile) + finally: + dumpfile.close() + +if __name__ == "__main__": + args = parse_arguments() + decode_file(args.file)
Signature · 1 line
2.11.0
PATCH v3 4/12 target/i386/misc_helper: wrap BQL around another IRQ generator
In reply to Alex BennéeAnything that calls into HW emulation must be protected by the BQL.
--- target/i386/misc_helper.c | 3 +++ 1 file changed, 3 insertions(+)
target/i386/misc_helper.c +3 -0
@@ -18,6 +18,7 @@*/ #include "qemu/osdep.h" +#include "qemu/main-loop.h" #include "cpu.h" #include "exec/helper-proto.h" #include "exec/exec-all.h"
@@ -156,7 +157,9 @@ void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)break ;case 8 :if (!(env->hflags2 &HF2_VINTR_MASK )) { +qemu_mutex_lock_iothread ();cpu_set_apic_tpr (x86_env_get_cpu (env)->apic_state, t0); +qemu_mutex_unlock_iothread (); } env->v_tpr = t0 &0x0f ;break ;
Signature · 1 line
2.11.0
PATCH v3 5/12 cpus: remove icount handling from qemu_tcg_cpu_thread_fn
In reply to Alex BennéeWe should never be running in multi-threaded mode with icount enabled. There is no point calling handle_icount_deadline here so remove it and assert !use_icount.
--- cpus.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
cpus.c +2 -2
@@ -1392,6 +1392,8 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) {CPUState *cpu = arg; +g_assert (!use_icount); +rcu_register_thread ();qemu_mutex_lock_iothread ();
@@ -1434,8 +1436,6 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) } } -handle_icount_deadline (); -atomic_mb_set (&cpu->exit_request,0 );qemu_tcg_wait_io_event (cpu); }
Signature · 1 line
2.11.0
PATCH v3 6/12 cpus: check cpu->running in cpu_get_icount_raw()
In reply to Alex BennéeThe lifetime of current_cpu is now the lifetime of the vCPU thread. However get_icount_raw() can apply a fudge factor if called while code is running to take into account the current executed instruction count.
To ensure this is always the case we also check cpu->running.
--- cpus.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
cpus.c +1 -1
@@ -229,7 +229,7 @@ int64_t cpu_get_icount_raw(void)CPUState *cpu = current_cpu; icount = timers_state.qemu_icount; -if (cpu) { +if (cpu && cpu->running) {if (!cpu->can_do_io) {fprintf (stderr,"Bad icount read\n" );exit (1 );
Signature · 1 line
2.11.0
PATCH v3 7/12 cpus: move icount preparation out of tcg_exec_cpu
In reply to Alex BennéeAs icount is only supported for single-threaded execution due to the requirement for determinism let's remove it from the common tcg_exec_cpu path.
Also remove the additional fiddling which shouldn't be required as the icount counters should all be rectified as you enter the loop.
---
v2
- only clear u16.low
- drop BQL assert
--- cpus.c | 67 ++++++++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 22 deletions(-)
cpus.c +45 -22
@@ -1179,47 +1179,64 @@ static void handle_icount_deadline(void) } } -static int tcg_cpu_exec (CPUState *cpu) +static void prepare_icount_for_run (CPUState *cpu) { -int ret; -#ifdef CONFIG_PROFILER -int64_t ti; -#endif - -#ifdef CONFIG_PROFILER - ti =profile_getclock (); -#endif if (use_icount) {int64_t count;int decr; - timers_state.qemu_icount -= (cpu->icount_decr.u16.low - + cpu->icount_extra); - cpu->icount_decr.u16.low =0 ; - cpu->icount_extra =0 ; + +/* These should always be cleared by process_icount_data after +* each vCPU execution. However u16.high can be raised +* asynchronously by cpu_exit/cpu_interrupt/tcg_handle_interrupt +*/ +g_assert (cpu->icount_decr.u16.low ==0 ); +g_assert (cpu->icount_extra ==0 ); + + count =tcg_get_icount_limit (); + timers_state.qemu_icount += count; decr = (count >0xffff ) ?0xffff : count; count -= decr; cpu->icount_decr.u16.low = decr; cpu->icount_extra = count; } -qemu_mutex_unlock_iothread (); -cpu_exec_start (cpu); - ret =cpu_exec (cpu); -cpu_exec_end (cpu); -qemu_mutex_lock_iothread (); -#ifdef CONFIG_PROFILER - tcg_time +=profile_getclock () - ti; -#endif +} + +static void process_icount_data (CPUState *cpu) +{if (use_icount) {/* Fold pending instructions back into the instruction counter, and clear the interrupt flag. */ timers_state.qemu_icount -= (cpu->icount_decr.u16.low + cpu->icount_extra); - cpu->icount_decr.u32 =0 ; + +/* Reset the counters */ + cpu->icount_decr.u16.low =0 ; cpu->icount_extra =0 ;replay_account_executed_instructions (); } +} + + +static int tcg_cpu_exec (CPUState *cpu) +{ +int ret; +#ifdef CONFIG_PROFILER +int64_t ti; +#endif + +#ifdef CONFIG_PROFILER + ti =profile_getclock (); +#endif +qemu_mutex_unlock_iothread (); +cpu_exec_start (cpu); + ret =cpu_exec (cpu); +cpu_exec_end (cpu); +qemu_mutex_lock_iothread (); +#ifdef CONFIG_PROFILER + tcg_time +=profile_getclock () - ti; +#endif return ret; }
@@ -1306,7 +1323,13 @@ static void *qemu_tcg_rr_cpu_thread_fn(void *arg)if (cpu_can_run (cpu)) {int r; + +prepare_icount_for_run (cpu); + r =tcg_cpu_exec (cpu); + +process_icount_data (cpu); +if (r ==EXCP_DEBUG ) {cpu_handle_guest_debug (cpu);break ;
Signature · 1 line
2.11.0
PATCH v3 8/12 cpus: don't credit executed instructions before they have run
In reply to Alex BennéeOutside of the vCPU thread icount time will only be tracked against timers_state.qemu_icount. We no longer credit cycles until they have completed the run. Inside the vCPU thread we adjust for passage of time by looking at how many have run so far. This is only valid inside the vCPU thread while it is running.
--- cpus.c | 25 +++++++++++++++++++------ include/qom/cpu.h | 1 + 2 files changed, 20 insertions(+), 6 deletions(-)
cpus.c +19 -6
@@ -223,6 +223,15 @@ void qemu_tcg_configure(QemuOpts *opts, Error **errp) } } +/* The current number of executed instructions is based on what we +* originally budgeted minus the current state of the decrementing +* icount counters in extra/u16.low. +*/ +static int64_t cpu_get_icount_executed (CPUState *cpu) +{ +return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra); +} +int64_t cpu_get_icount_raw (void ) {int64_t icount;
@@ -234,7 +243,8 @@ int64_t cpu_get_icount_raw(void)fprintf (stderr,"Bad icount read\n" );exit (1 ); } - icount -= (cpu->icount_decr.u16.low + cpu->icount_extra); +/* Take into account what has run */ + icount +=cpu_get_icount_executed (cpu); }return icount; }
@@ -1195,7 +1205,10 @@ static void prepare_icount_for_run(CPUState *cpu) count =tcg_get_icount_limit (); - timers_state.qemu_icount += count; +/* To calculate what we have executed so far we need to know +* what we originally budgeted to run this cycle */ + cpu->icount_budget = count; + decr = (count >0xffff ) ?0xffff : count; count -= decr; cpu->icount_decr.u16.low = decr;
@@ -1206,14 +1219,14 @@ static void prepare_icount_for_run(CPUState *cpu)static void process_icount_data (CPUState *cpu) {if (use_icount) { -/* Fold pending instructions back into the -instruction counter, and clear the interrupt flag. */ - timers_state.qemu_icount -= (cpu->icount_decr.u16.low - + cpu->icount_extra); +/* Account for executed instructions */ + timers_state.qemu_icount +=cpu_get_icount_executed (cpu);/* Reset the counters */ cpu->icount_decr.u16.low =0 ; cpu->icount_extra =0 ; + cpu->icount_budget =0 ; +replay_account_executed_instructions (); } }
include/qom/cpu.h +1 -0
@@ -332,6 +332,7 @@ struct CPUState {/* updates protected by BQL */ uint32_t interrupt_request;int singlestep_enabled; +int64_t icount_budget;int64_t icount_extra;sigjmp_buf jmp_env;
Signature · 1 line
2.11.0
PATCH v3 9/12 cpus: introduce cpu_update_icount helper
In reply to Alex BennéeBy holding off updates to timer_state.qemu_icount we can run into trouble when the non-vCPU thread needs to know the time. This helper ensures we atomically update timers_state.qemu_icount based on what has been currently executed.
--- cpus.c | 23 +++++++++++++++++++++-- include/qemu/timer.h | 1 + 2 files changed, 22 insertions(+), 2 deletions(-)
cpus.c +21 -2
@@ -232,12 +232,31 @@ static int64_t cpu_get_icount_executed(CPUState *cpu)return cpu->icount_budget - (cpu->icount_decr.u16.low + cpu->icount_extra); } +/* +* Update the global shared timer_state.qemu_icount to take into +* account executed instructions. This is done by the TCG vCPU +* thread so the main-loop can see time has moved forward. +*/ +void cpu_update_icount (CPUState *cpu) +{ +int64_t executed =cpu_get_icount_executed (cpu); + cpu->icount_budget -= executed; + +#ifdef CONFIG_ATOMIC64 +atomic_set__nocheck (&timers_state.qemu_icount, +atomic_read__nocheck (&timers_state.qemu_icount) + + executed); +#else /* FIXME: we need 64bit atomics to do this safely */ + timers_state.qemu_icount += executed; +#endif +} +int64_t cpu_get_icount_raw (void ) {int64_t icount;CPUState *cpu = current_cpu; - icount = timers_state.qemu_icount; + icount =atomic_read (&timers_state.qemu_icount);if (cpu && cpu->running) {if (!cpu->can_do_io) {fprintf (stderr,"Bad icount read\n" );
@@ -1220,7 +1239,7 @@ static void process_icount_data(CPUState *cpu) {if (use_icount) {/* Account for executed instructions */ - timers_state.qemu_icount +=cpu_get_icount_executed (cpu); +cpu_update_icount (cpu);/* Reset the counters */ cpu->icount_decr.u16.low =0 ;
include/qemu/timer.h +1 -0
@@ -869,6 +869,7 @@ int64_t cpu_get_icount_raw(void);int64_t cpu_get_icount (void );int64_t cpu_get_clock (void );int64_t cpu_icount_to_ns (int64_t icount); +void cpu_update_icount (CPUState *cpu);/*******************************************/ /* host CPU ticks (if available) */
Signature · 1 line
2.11.0
PATCH v3 10/12 cpu-exec: update icount after each TB_EXIT
In reply to Alex BennéeThere is no particular reason we shouldn't update the global system icount time as we exit each TranslationBlock run. This ensures the main-loop doesn't have to wait until we exit to the outer loop for executed instructions to be credited to timer_state.
The prepare_icount_for_run function is slightly tweaked to match the logic we run in cpu_loop_exec_tb.
Based on Paolo's original suggestion.
--- cpu-exec.c | 14 +++++++------- cpus.c | 18 +++++------------- 2 files changed, 12 insertions(+), 20 deletions(-)
cpu-exec.c +7 -7
@@ -600,13 +600,13 @@ static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,/* Instruction counter expired. */ assert (use_icount);#ifndef CONFIG_USER_ONLY -if (cpu->icount_extra) { -/* Refill decrementer and continue execution. */ - cpu->icount_extra += insns_left; - insns_left =MIN (0xffff , cpu->icount_extra); - cpu->icount_extra -= insns_left; - cpu->icount_decr.u16.low = insns_left; - }else { +/* Ensure global icount has gone forward */ +cpu_update_icount (cpu); +/* Refill decrementer and continue execution. */ + insns_left =MIN (0xffff , cpu->icount_budget); + cpu->icount_decr.u16.low = insns_left; + cpu->icount_extra = cpu->icount_budget - insns_left; +if (!cpu->icount_extra) {/* Execute any remaining instructions, then let the main loop * handle the next event. */
cpus.c +5 -13
@@ -1211,8 +1211,7 @@ static void handle_icount_deadline(void)static void prepare_icount_for_run (CPUState *cpu) {if (use_icount) { -int64_t count; -int decr; +int insns_left;/* These should always be cleared by process_icount_data after * each vCPU execution. However u16.high can be raised
@@ -1221,17 +1220,10 @@ static void prepare_icount_for_run(CPUState *cpu)g_assert (cpu->icount_decr.u16.low ==0 );g_assert (cpu->icount_extra ==0 ); - - count =tcg_get_icount_limit (); - -/* To calculate what we have executed so far we need to know -* what we originally budgeted to run this cycle */ - cpu->icount_budget = count; - - decr = (count >0xffff ) ?0xffff : count; - count -= decr; - cpu->icount_decr.u16.low = decr; - cpu->icount_extra = count; + cpu->icount_budget =tcg_get_icount_limit (); + insns_left =MIN (0xffff , cpu->icount_budget); + cpu->icount_decr.u16.low = insns_left; + cpu->icount_extra = cpu->icount_budget - insns_left; } }
Signature · 1 line
2.11.0
PATCH v3 11/12 cpus: call cpu_update_icount on read
In reply to Alex BennéeThis ensures each time the vCPU thread reads the icount we update the master timer_state.qemu_icount field. This way as long as updates are in BQL protected sections (which they should be) the main-loop can never come to update the log and find time has gone backwards.
--- cpus.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
cpus.c +6 -4
@@ -253,19 +253,21 @@ void cpu_update_icount(CPUState *cpu)int64_t cpu_get_icount_raw (void ) { -int64_t icount;CPUState *cpu = current_cpu; - icount =atomic_read (&timers_state.qemu_icount);if (cpu && cpu->running) {if (!cpu->can_do_io) {fprintf (stderr,"Bad icount read\n" );exit (1 ); }/* Take into account what has run */ - icount +=cpu_get_icount_executed (cpu); +cpu_update_icount (cpu); } -return icount; +#ifdef CONFIG_ATOMIC64 +return atomic_read__nocheck (&timers_state.qemu_icount); +#else /* FIXME: we need 64bit atomics to do this safely */ +return timers_state.qemu_icount; +#endif }/* Return the virtual CPU time, based on the instruction counter. */
Signature · 1 line
2.11.0
PATCH v3 12/12 replay: assert time only goes forward
In reply to Alex BennéeIf we find ourselves trying to add an event to the log where time has gone backwards it is because a vCPU event has occurred and the main-loop is not yet aware of time moving forward. This should not happen and if it does its better to fail early than generate a log that will have weird behaviour.
--- replay/replay-internal.c | 4 ++++ replay/replay.c | 4 ++++ 2 files changed, 8 insertions(+)
replay/replay-internal.c +4 -0
@@ -195,6 +195,10 @@ void replay_save_instructions(void)if (replay_file && replay_mode ==REPLAY_MODE_RECORD ) {replay_mutex_lock ();int diff = (int )(replay_get_current_step () - replay_state.current_step); + +/* Time can only go forward */ +assert (diff >=0 ); +if (diff >0 ) {replay_put_event (EVENT_INSTRUCTION );replay_put_dword (diff);
replay/replay.c +4 -0
@@ -84,6 +84,10 @@ void replay_account_executed_instructions(void)if (replay_state.instructions_count >0 ) {int count = (int )(replay_get_current_step () - replay_state.current_step); + +/* Time can only go forward */ +assert (count >=0 ); + replay_state.instructions_count -= count; replay_state.current_step += count;if (replay_state.instructions_count ==0 ) {
Signature · 1 line
2.11.0