Use only trace_event_get_state_backends() to check trace enablement
cover Use only trace_event_get_state_backends() to check trace enablement
Currently docs/devel/tracing.txt recommends checking the TRACE_FOO_ENABLED macro to see if the FOO event is enabled, for the purpose of skipping expensive computations that are only used in trace output.
This is no longer the best way to do this; instead we should prefer trace_event_get_state_backends(), which checks both whether the event is compile-time disabled and also whether it is run-time disabled.
Update the documentation, and also the 5 uses of the old style in the tree.
thanks
-- PMM
Peter Maydell (3):
docs/devel/tracing.txt: Recommend only
trace_event_get_state_backends()
memory.c: Use trace_event_get_state_backends() hw/display/qxl.c: Use trace_event_get_state_backends()
docs/devel/tracing.txt | 12 +++++------- hw/display/qxl.c | 2 +- memory.c | 8 ++++---- 3 files changed, 10 insertions(+), 12 deletions(-)
Signature · 1 line
2.20.1
PATCH 1/3 docs/devel/tracing.txt: Recommend only trace_event_get_state_backends()
Instead of recommending checking the TRACE_FOO_ENABLED macro to skip expensive computations needed only for tracing, recommend only using trace_event_get_state_backends(). This works for both compile-time and run-time disabling of events, and has no extra performance impact if the event is compile-time disabled.
--- docs/devel/tracing.txt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
docs/devel/tracing.txt +5 -7
@@ -342,8 +342,10 @@ edit the "trace-events-all" file). In addition, there might be cases where relatively complex computations must be performed to generate values that are only used as arguments for a trace -function. In these cases you can use the macro 'TRACE_${EVENT_NAME}_ENABLED' to -guard such computations and avoid its compilation when the event is disabled: +function. In these cases you can use 'trace_event_get_state_backends()' to +guard such computations, so they are skipped if the event has been either +compile-time disabled or run-time disabled. If the event is compile-time +disabled, this check will have no performance impact. #include "trace.h" /* needed for trace event prototype */
@@ -356,7 +358,7 @@ guard such computations and avoid its compilation when the event is disabled: align = getpagesize(); } ptr = qemu_memalign(align, size); - if (TRACE_QEMU_VMALLOC_ENABLED) { /* preprocessor macro */ + if (trace_event_get_state_backends(TRACE_QEMU_VMALLOC)) { void *complex; /* some complex computations to produce the 'complex' value */ trace_qemu_vmalloc(size, ptr, complex);
@@ -364,10 +366,6 @@ guard such computations and avoid its compilation when the event is disabled: return ptr; } -You can check both if the event has been disabled and is dynamically enabled at -the same time using the 'trace_event_get_state_backends' routine (see header -"trace/control.h" for more information). - === "tcg" === Guest code generated by TCG can be traced by defining an event with the "tcg"
Signature · 1 line
2.20.1
PATCH 2/3 memory.c: Use trace_event_get_state_backends()
In reply to Peter MaydellThe preferred way to test whether a trace event is enabled is to use trace_event_get_state_backends(), because this will give the correct answer (allowing expensive computations to be skipped) whether the trace event is compile-time or run-time disabled. Convert the four old-style direct uses of TRACE_FOO_ENABLED in memory.c.
--- memory.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
memory.c +4 -4
@@ -434,7 +434,7 @@ 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 (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) { + }else if (trace_event_get_state_backends (TRACE_MEMORY_REGION_OPS_READ )) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size); }
@@ -456,7 +456,7 @@ 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 (TRACE_MEMORY_REGION_OPS_READ_ENABLED ) { + }else if (trace_event_get_state_backends (TRACE_MEMORY_REGION_OPS_READ )) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_read (get_cpu_index (), mr, abs_addr, tmp, size); }
@@ -476,7 +476,7 @@ static MemTxResult memory_region_write_accessor(MemoryRegion *mr,if (mr->subpage) {trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); - }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) { + }else if (trace_event_get_state_backends (TRACE_MEMORY_REGION_OPS_WRITE )) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size); }
@@ -496,7 +496,7 @@ static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,if (mr->subpage) {trace_memory_region_subpage_write (get_cpu_index (), mr, addr, tmp, size); - }else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED ) { + }else if (trace_event_get_state_backends (TRACE_MEMORY_REGION_OPS_WRITE )) {hwaddr abs_addr =memory_region_to_absolute_addr (mr, addr);trace_memory_region_ops_write (get_cpu_index (), mr, abs_addr, tmp, size); }
Signature · 1 line
2.20.1
PATCH 3/3 hw/display/qxl.c: Use trace_event_get_state_backends()
In reply to Peter MaydellThe preferred way to test whether a trace event is enabled is to use trace_event_get_state_backends(), because this will give the correct answer (allowing expensive computations to be skipped) whether the trace event is compile-time or run-time disabled. Convert the old-style direct use of TRACE_FOO_ENABLED.
--- hw/display/qxl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/qxl.c +1 -1
@@ -1764,7 +1764,7 @@ async_common:qxl_set_mode (d, val,0 );break ;case QXL_IO_LOG : -if (TRACE_QXL_IO_LOG_ENABLED || d->guestdebug) { +if (trace_event_get_state_backends (TRACE_QXL_IO_LOG ) || d->guestdebug) {/* We cannot trust the guest to NUL terminate d->ram->log_buf */ char *log_buf =g_strndup ((const char *)d->ram->log_buf,sizeof (d->ram->log_buf));
Signature · 1 line
2.20.1
Re: [PATCH 3/3] hw/display/qxl.c: Use trace_event_get_state_backends()
Quoted 16 lines · Peter Maydell
The preferred way to test whether a trace event is enabled is to use
trace_event_get_state_backends(), because this will give the correct answer (allowing expensive computations to be skipped) whether the trace event is compile-time or run-time disabled. Convert the old-style direct use ofTRACE_FOO_ENABLED.Signed-off-by Peter Maydell <[email protected]>--- hw/display/qxl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/display/qxl.c +0
-0
hw/display/qxl.c async_common: · line 1767
Quoted 5 lines
hw/display/qxl.c +1
-1@@ -1764,7 +1764,7 @@ async_common:qxl_set_mode (d, val,0 );break ;case QXL_IO_LOG : -if (TRACE_QXL_IO_LOG_ENABLED || d->guestdebug) { +if (trace_event_get_state_backends (TRACE_QXL_IO_LOG ) || d->guestdebug) {
Quoted 6 lines
hw/display/qxl.c +0
-0/* We cannot trust the guest to NUL terminate d->ram->log_buf */ char *log_buf =g_strndup ((const char *)d->ram->log_buf,sizeof (d->ram->log_buf));Signature · 1 line
2.20.1
Stefan Hajnoczi
AppliedQuoted 22 lines · Peter Maydell
Currently
docs/devel/tracing.txtrecommends checking theTRACE_FOO_ENABLEDmacro to see if the FOO event is enabled, for the purpose of skipping expensive computations that are only used in trace output.This is no longer the best way to do this; instead we should prefer
trace_event_get_state_backends(), which checks both whether the event is compile-time disabled and also whether it is run-time disabled.Update the documentation, and also the 5 uses of the old style in the tree.
thanks
-- PMMPeter Maydell (3):
docs/devel/tracing.txt: Recommend only
trace_event_get_state_backends()
memory.c: Usetrace_event_get_state_backends()hw/display/qxl.c: Usetrace_event_get_state_backends()
Quoted 9 lines
docs/devel/tracing.txt | 12 +++++------- hw/display/qxl.c | 2 +- memory.c | 8 ++++---- 3 files changed, 10 insertions(+), 12 deletions(-)Signature · 1 line
2.20.1
Thanks, applied to my tracing tree:
https://github.com/stefanha/qemu/commits/tracing
Stefan