trace: include CPU index in trace_memory_region_*()
PATCH v2 1/2 trace: include CPU index in trace_memory_region_*()
Knowing which CPU performed an action is essential for understanding SMP guest behavior.
However, cpu_physical_memory_rw() may be executed by a machine init function, before any VCPUs are running, when there is no CPU running ('current_cpu' is NULL). In this case, store -1 in the trace record as the CPU index. Trace analysis tools may need to be aware of this special case.
---
v2: use get_cpu_index() helper function
--- memory.c | 32 ++++++++++++++++++++------------ trace-events | 8 ++++---- 2 files changed, 24 insertions(+), 16 deletions(-)
memory.c +20 -12
@@ -386,6 +386,14 @@ static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)return abs_addr; } +static int get_cpu_index (void ) +{ +if (current_cpu) { +return current_cpu->cpu_index; + } +return -1 ; +} + static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr, hwaddr addr, uint64_t *value,
@@ -398,10 +406,10 @@ static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr, tmp = mr->ops->old_mmio.read[ctz32 (size)](mr->opaque, addr);if (mr->subpage) { -trace_memory_region_subpage_read (mr, addr, tmp, size); +trace_memory_region_subpage_read (get_cpu_index (), mr, addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr); -trace_memory_region_ops_read (mr, abs_addr, tmp, size); +trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size); } *value |= (tmp & mask) << shift;return MEMTX_OK ;
@@ -419,10 +427,10 @@ static MemTxResult memory_region_read_accessor(MemoryRegion *mr, tmp = mr->ops->read (mr->opaque, addr, size);if (mr->subpage) { -trace_memory_region_subpage_read (mr, addr, tmp, size); +trace_memory_region_subpage_read (get_cpu_index (), mr, addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr); -trace_memory_region_ops_read (mr, abs_addr, tmp, size); +trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size); } *value |= (tmp & mask) << shift;return MEMTX_OK ;
@@ -441,10 +449,10 @@ static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr, r = mr->ops->read_with_attrs (mr->opaque, addr, &tmp, size, attrs);if (mr->subpage) { -trace_memory_region_subpage_read (mr, addr, tmp, size); +trace_memory_region_subpage_read (get_cpu_index (), mr, addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr); -trace_memory_region_ops_read (mr, abs_addr, tmp, size); +trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size); } *value |= (tmp & mask) << shift;return r;
@@ -462,10 +470,10 @@ static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;if (mr->subpage) { -trace_memory_region_subpage_write (mr, addr, tmp, size); +trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr); -trace_memory_region_ops_write (mr, abs_addr, tmp, size); +trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size); } mr->ops->old_mmio.write[ctz32 (size)](mr->opaque, addr, tmp);return MEMTX_OK ;
@@ -483,10 +491,10 @@ static MemTxResult memory_region_write_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;if (mr->subpage) { -trace_memory_region_subpage_write (mr, addr, tmp, size); +trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr); -trace_memory_region_ops_write (mr, abs_addr, tmp, size); +trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size); } mr->ops->write (mr->opaque, addr, tmp, size);return MEMTX_OK ;
@@ -504,10 +512,10 @@ static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;if (mr->subpage) { -trace_memory_region_subpage_write (mr, addr, tmp, size); +trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr); -trace_memory_region_ops_write (mr, abs_addr, tmp, size); +trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size); }return mr->ops->write_with_attrs (mr->opaque, addr, tmp, size, attrs); }
trace-events +4 -4
@@ -1620,10 +1620,10 @@ disable exec_tb_exit(void *next_tb, unsigned int flags) "tb:%p flags=%x" translate_block(void *tb, uintptr_t pc, uint8_t *tb_code) "tb:%p, pc:0x%"PRIxPTR", tb_code:%p" # memory.c -memory_region_ops_read(void *mr, uint64_t addr, uint64_t value, unsigned size) "mr %p addr %#"PRIx64" value %#"PRIx64" size %u" -memory_region_ops_write(void *mr, uint64_t addr, uint64_t value, unsigned size) "mr %p addr %#"PRIx64" value %#"PRIx64" size %u" -memory_region_subpage_read(void *mr, uint64_t offset, uint64_t value, unsigned size) "mr %p offset %#"PRIx64" value %#"PRIx64" size %u" -memory_region_subpage_write(void *mr, uint64_t offset, uint64_t value, unsigned size) "mr %p offset %#"PRIx64" value %#"PRIx64" size %u" +memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr %#"PRIx64" value %#"PRIx64" size %u" +memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr %#"PRIx64" value %#"PRIx64" size %u" +memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset %#"PRIx64" value %#"PRIx64" size %u" +memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset %#"PRIx64" value %#"PRIx64" size %u" # qom/object.c object_dynamic_cast_assert(const char *type, const char *target, const char *file, int line, const char *func) "%s->%s (%s:%d:%s)"
Signature · 1 line
1.9.1
PATCH v2 2/2 trace: separate MMIO tracepoints from TB-access tracepoints
Memory accesses to code which has previously been translated into a TB show up in the MMIO path, so that they may invalidate the TB. It's extremely confusing to mix those in with device MMIOs, so split them into their own tracepoint.
---
It took many hours to figure out why some RAM accesses were coming through the MMIO path instead of being handled inline in the TBs.
On IRC, Paolo expressed some concern about performance, but ultimately agreed that adding one conditional to an already heavy codepath wouldn't have much impact.
v2: rename trace_memory_region_ops_tb_read/write to trace_memory_region_tb_read/write
--- memory.c | 30 ++++++++++++++++++++++++++++++ trace-events | 2 ++ 2 files changed, 32 insertions(+)
memory.c +30 -0
@@ -407,6 +407,11 @@ static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr, tmp = mr->ops->old_mmio.read[ctz32 (size)](mr->opaque, addr);if (mr->subpage) {trace_memory_region_subpage_read (get_cpu_index (), mr, addr, tmp, size); + }else if (mr == &io_mem_notdirty) { +/* Accesses to code which has previously been translated into a TB show +* up in the MMIO path, as accesses to the io_mem_notdirty +* MemoryRegion. */ +trace_memory_region_tb_read (get_cpu_index (), addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size);
@@ -428,6 +433,11 @@ static MemTxResult memory_region_read_accessor(MemoryRegion *mr, tmp = mr->ops->read (mr->opaque, addr, size);if (mr->subpage) {trace_memory_region_subpage_read (get_cpu_index (), mr, addr, tmp, size); + }else if (mr == &io_mem_notdirty) { +/* Accesses to code which has previously been translated into a TB show +* up in the MMIO path, as accesses to the io_mem_notdirty +* MemoryRegion. */ +trace_memory_region_tb_read (get_cpu_index (), addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size);
@@ -450,6 +460,11 @@ static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr, r = mr->ops->read_with_attrs (mr->opaque, addr, &tmp, size, attrs);if (mr->subpage) {trace_memory_region_subpage_read (get_cpu_index (), mr, addr, tmp, size); + }else if (mr == &io_mem_notdirty) { +/* Accesses to code which has previously been translated into a TB show +* up in the MMIO path, as accesses to the io_mem_notdirty +* MemoryRegion. */ +trace_memory_region_tb_read (get_cpu_index (), addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size);
@@ -471,6 +486,11 @@ static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;if (mr->subpage) {trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); + }else if (mr == &io_mem_notdirty) { +/* Accesses to code which has previously been translated into a TB show +* up in the MMIO path, as accesses to the io_mem_notdirty +* MemoryRegion. */ +trace_memory_region_tb_write (get_cpu_index (), addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size);
@@ -492,6 +512,11 @@ static MemTxResult memory_region_write_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;if (mr->subpage) {trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); + }else if (mr == &io_mem_notdirty) { +/* Accesses to code which has previously been translated into a TB show +* up in the MMIO path, as accesses to the io_mem_notdirty +* MemoryRegion. */ +trace_memory_region_tb_write (get_cpu_index (), addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size);
@@ -513,6 +538,11 @@ static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr, tmp = (*value >> shift) & mask;if (mr->subpage) {trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); + }else if (mr == &io_mem_notdirty) { +/* Accesses to code which has previously been translated into a TB show +* up in the MMIO path, as accesses to the io_mem_notdirty +* MemoryRegion. */ +trace_memory_region_tb_write (get_cpu_index (), addr, tmp, size); }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size);
trace-events +2 -0
@@ -1624,6 +1624,8 @@ memory_region_ops_read(int cpu_index, void *mr, uint64_t addr, uint64_t value, u memory_region_ops_write(int cpu_index, void *mr, uint64_t addr, uint64_t value, unsigned size) "cpu %d mr %p addr %#"PRIx64" value %#"PRIx64" size %u" memory_region_subpage_read(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset %#"PRIx64" value %#"PRIx64" size %u" memory_region_subpage_write(int cpu_index, void *mr, uint64_t offset, uint64_t value, unsigned size) "cpu %d mr %p offset %#"PRIx64" value %#"PRIx64" size %u" +memory_region_tb_read(int cpu_index, uint64_t addr, uint64_t value, unsigned size) "cpu %d addr %#"PRIx64" value %#"PRIx64" size %u" +memory_region_tb_write(int cpu_index, uint64_t addr, uint64_t value, unsigned size) "cpu %d addr %#"PRIx64" value %#"PRIx64" size %u" # qom/object.c object_dynamic_cast_assert(const char *type, const char *target, const char *file, int line, const char *func) "%s->%s (%s:%d:%s)"
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH v2 2/2] trace: separate MMIO tracepoints from TB-access tracepoints
Quoted 14 lines · Hollis Blanchard
Memory accesses to code which has previously been translated into a TB show up
in the MMIO path, so that they may invalidate the TB. It's extremely confusing
to mix those in with device MMIOs, so split them into their own tracepoint.Signed-off-by Hollis Blanchard <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>---
It took many hours to figure out why some RAM accesses were coming through the
MMIO path instead of being handled inline in the TBs.
Quoted 9 lines
On IRC, Paolo expressed some concern about performance, but ultimately agreed
that adding one conditional to an already heavy codepath wouldn't have much
impact.v2: rename trace_memory_region_ops_tb_read/write to trace_memory_region_tb_read/write
Stefan, any further comment here? You sent a Reviewed-by on the original, but unlike the "include CPU index" patch, you didn't mention adding this one to https://github.com/stefanha/qemu/commits/tracing.
--
Hollis Blanchard <[email protected]>
Mentor Graphics Emulation Division
Stefan Hajnoczi
AppliedQuoted 8 lines · Hollis Blanchard
Knowing which CPU performed an action is essential for understanding SMP guest behavior.
However,
cpu_physical_memory_rw()may be executed by a machine init function, before any VCPUs are running, when there is no CPU running ('current_cpu' is NULL). In this case, store -1 in the trace record as the CPU index. Trace analysis tools may need to be aware of this special case.
Quoted 7 lines
Signed-off-by Hollis Blanchard <[email protected]>---
v2: useget_cpu_index()helper function--- memory.c | 32 ++++++++++++++++++++------------ trace-events | 8 ++++---- 2 files changed, 24 insertions(+), 16 deletions(-)
Thanks, applied to my tracing tree:
https://github.com/stefanha/qemu/commits/tracing
Stefan