Perf inject for ETM trace
Subsystems (5)
v2 cover Perf inject for ETM trace
Hi,
These patches add support for using perf inject to generate branch events, instruction events and branch stacks from CoreSight ETM traces.
They apply to linus's tree with the memory cleanup fix from
https://lkml.org/lkml/2018/1/25/432
Changes since v1:
* Split documentation update into separate patch
* Added null pointer check
* Moved some changes from patch 2 to patch 1
Regards
Rob Walker
Robert Walker (3):
perf tools: inject capabilitity for CoreSight traces perf inject: Emit instruction records on ETM trace discontinuity
coresight: Update documentation for perf usage
Documentation/trace/coresight.txt | 51 +++ tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 74 +++- tools/perf/util/cs-etm-decoder/cs-etm-decoder.h | 2 + tools/perf/util/cs-etm.c | 472 +++++++++++++++++++++--- 4 files changed, 533 insertions(+), 66 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/3 perf tools: inject capabilitity for CoreSight traces
Added user space perf functionality to translate CoreSight traces into instruction events with branch stack.
To invoke the new functionality, use the perf inject tool with --itrace=il. For example, to translate the ETM trace from perf.data into last branch records in a new inj.data file:
$ perf inject --itrace=i100000il128 -i perf.data -o perf.data.new
The 'i' parameter to itrace generates periodic instruction events. The period between instruction events can be specified as a number of instructions suffixed by i (default 100000).
The parameter to 'l' specifies the number of entries in the branch stack attached to instruction events.
The 'b' parameter to itrace generates events on taken branches.
This patch also fixes the contents of the branch events used in perf report
- previously branch events were generated for each contiguous range of instructions executed. These are fixed to generate branch events between the last address of a range ending in an executed branch instruction and the start address of the next range.
Based on patches by Sebastian Pop <[email protected]> with additional fixes and support for specifying the instruction period.
--- tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 65 +++- tools/perf/util/cs-etm-decoder/cs-etm-decoder.h | 1 + tools/perf/util/cs-etm.c | 434 +++++++++++++++++++++--- 3 files changed, 436 insertions(+), 64 deletions(-)
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +49 -16
@@ -78,6 +78,8 @@ int cs_etm_decoder__reset(struct cs_etm_decoder *decoder) {ocsd_datapath_resp_t dp_ret; + decoder->prev_return =OCSD_RESP_CONT ; + dp_ret =ocsd_dt_process_data (decoder->dcd_tree,OCSD_OP_RESET ,0 ,0 ,NULL ,NULL );if (OCSD_DATA_RESP_IS_FATAL (dp_ret))
@@ -253,16 +255,16 @@ static void cs_etm_decoder__clear_buffer(struct cs_etm_decoder *decoder) decoder->packet_count =0 ;for (i =0 ; i <MAX_BUFFER ; i++) { decoder->packet_buffer[i].start_addr =0xdeadbeefdeadbeefUL ; - decoder->packet_buffer[i].end_addr =0xdeadbeefdeadbeefUL ; - decoder->packet_buffer[i].exc = false; - decoder->packet_buffer[i].exc_ret = false; - decoder->packet_buffer[i].cpu =INT_MIN ; + decoder->packet_buffer[i].end_addr =0xdeadbeefdeadbeefUL ; + decoder->packet_buffer[i].last_instr_taken_branch = false; + decoder->packet_buffer[i].exc = false; + decoder->packet_buffer[i].exc_ret = false; + decoder->packet_buffer[i].cpu =INT_MIN ; } }static ocsd_datapath_resp_t cs_etm_decoder__buffer_packet (struct cs_etm_decoder *decoder, -const ocsd_generic_trace_elem *elem,const u8 trace_chan_id,enum cs_etm_sample_type sample_type) {
@@ -278,18 +280,16 @@ cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,return OCSD_RESP_FATAL_SYS_ERR ; et = decoder->tail; + et = (et +1 ) & (MAX_BUFFER -1 ); + decoder->tail = et; + decoder->packet_count++; + decoder->packet_buffer[et].sample_type = sample_type; - decoder->packet_buffer[et].start_addr = elem->st_addr; - decoder->packet_buffer[et].end_addr = elem->en_addr; decoder->packet_buffer[et].exc = false; decoder->packet_buffer[et].exc_ret = false; decoder->packet_buffer[et].cpu = *((int *)inode->priv); - -/* Wrap around if need be */ - et = (et +1 ) & (MAX_BUFFER -1 ); - - decoder->tail = et; - decoder->packet_count++; + decoder->packet_buffer[et].start_addr =0xdeadbeefdeadbeefUL ; + decoder->packet_buffer[et].end_addr =0xdeadbeefdeadbeefUL ;if (decoder->packet_count ==MAX_BUFFER -1 )return OCSD_RESP_WAIT ;
@@ -297,6 +297,40 @@ cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,return OCSD_RESP_CONT ; } +static ocsd_datapath_resp_t +cs_etm_decoder__buffer_range (struct cs_etm_decoder *decoder, +const ocsd_generic_trace_elem *elem, +const uint8_t trace_chan_id) +{ +int ret =0 ; +struct cs_etm_packet *packet; + + ret =cs_etm_decoder__buffer_packet (decoder, trace_chan_id, +CS_ETM_RANGE ); +if (ret !=OCSD_RESP_CONT && ret !=OCSD_RESP_WAIT ) +return ret; + + packet = &decoder->packet_buffer[decoder->tail]; + + packet->start_addr = elem->st_addr; + packet->end_addr = elem->en_addr; +switch (elem->last_i_type) { +case OCSD_INSTR_BR : +case OCSD_INSTR_BR_INDIRECT : + packet->last_instr_taken_branch = elem->last_instr_exec; +break ; +case OCSD_INSTR_ISB : +case OCSD_INSTR_DSB_DMB : +case OCSD_INSTR_OTHER : +default : + packet->last_instr_taken_branch = false; +break ; + } + +return ret; + +} + static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( const void *context, const ocsd_trc_index_t indx __maybe_unused,
@@ -316,9 +350,8 @@ static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( decoder->trace_on = true;break ;case OCSD_GEN_TRC_ELEM_INSTR_RANGE : - resp =cs_etm_decoder__buffer_packet (decoder, elem, - trace_chan_id, -CS_ETM_RANGE ); + resp =cs_etm_decoder__buffer_range (decoder, elem, + trace_chan_id);break ;case OCSD_GEN_TRC_ELEM_EXCEPTION : decoder->packet_buffer[decoder->tail].exc = true;
tools/perf/util/cs-etm-decoder/cs-etm-decoder.h +1 -0
@@ -30,6 +30,7 @@ struct cs_etm_packet {enum cs_etm_sample_type sample_type;u64 start_addr;u64 end_addr; +u8 last_instr_taken_branch;u8 exc;u8 exc_ret;int cpu;
tools/perf/util/cs-etm.c +386 -48
550 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Robert Walker
merge bot[tip:perf/core] perf cs-etm: Inject capabilitity for CoreSight traces
perf cs-etm: Inject capabilitity for CoreSight traces
Commit-ID: e573e978fb12e16094c0b39fad3dc4e6b4803c2c Gitweb: https://git.kernel.org/tip/e573e978fb12e16094c0b39fad3dc4e6b4803c2c Author: Robert Walker <[email protected]> AuthorDate: Wed, 14 Feb 2018 11:24:39 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 16 Feb 2018 14:55:44 -0300
perf cs-etm: Inject capabilitity for CoreSight traces
Added user space perf functionality to translate CoreSight traces into instruction events with branch stack.
To invoke the new functionality, use the perf inject tool with --itrace=il. For example, to translate the ETM trace from perf.data into last branch records in a new inj.data file:
$ perf inject --itrace=i100000il128 -i perf.data -o perf.data.new
The 'i' parameter to itrace generates periodic instruction events. The period between instruction events can be specified as a number of instructions suffixed by i (default 100000).
The parameter to 'l' specifies the number of entries in the branch stack attached to instruction events.
The 'b' parameter to itrace generates events on taken branches.
This patch also fixes the contents of the branch events used in perf report - previously branch events were generated for each contiguous range of instructions executed. These are fixed to generate branch events between the last address of a range ending in an executed branch instruction and the start address of the next range.
Based on patches by Sebastian Pop <[email protected]> with additional fixes and support for specifying the instruction period.
--- tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 65 +++- tools/perf/util/cs-etm-decoder/cs-etm-decoder.h | 1 + tools/perf/util/cs-etm.c | 434 +++++++++++++++++++++--- 3 files changed, 436 insertions(+), 64 deletions(-)
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +49 -16
@@ -78,6 +78,8 @@ int cs_etm_decoder__reset(struct cs_etm_decoder *decoder) {ocsd_datapath_resp_t dp_ret; + decoder->prev_return =OCSD_RESP_CONT ; + dp_ret =ocsd_dt_process_data (decoder->dcd_tree,OCSD_OP_RESET ,0 ,0 ,NULL ,NULL );if (OCSD_DATA_RESP_IS_FATAL (dp_ret))
@@ -253,16 +255,16 @@ static void cs_etm_decoder__clear_buffer(struct cs_etm_decoder *decoder) decoder->packet_count =0 ;for (i =0 ; i <MAX_BUFFER ; i++) { decoder->packet_buffer[i].start_addr =0xdeadbeefdeadbeefUL ; - decoder->packet_buffer[i].end_addr =0xdeadbeefdeadbeefUL ; - decoder->packet_buffer[i].exc = false; - decoder->packet_buffer[i].exc_ret = false; - decoder->packet_buffer[i].cpu =INT_MIN ; + decoder->packet_buffer[i].end_addr =0xdeadbeefdeadbeefUL ; + decoder->packet_buffer[i].last_instr_taken_branch = false; + decoder->packet_buffer[i].exc = false; + decoder->packet_buffer[i].exc_ret = false; + decoder->packet_buffer[i].cpu =INT_MIN ; } }static ocsd_datapath_resp_t cs_etm_decoder__buffer_packet (struct cs_etm_decoder *decoder, -const ocsd_generic_trace_elem *elem,const u8 trace_chan_id,enum cs_etm_sample_type sample_type) {
@@ -278,18 +280,16 @@ cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,return OCSD_RESP_FATAL_SYS_ERR ; et = decoder->tail; + et = (et +1 ) & (MAX_BUFFER -1 ); + decoder->tail = et; + decoder->packet_count++; + decoder->packet_buffer[et].sample_type = sample_type; - decoder->packet_buffer[et].start_addr = elem->st_addr; - decoder->packet_buffer[et].end_addr = elem->en_addr; decoder->packet_buffer[et].exc = false; decoder->packet_buffer[et].exc_ret = false; decoder->packet_buffer[et].cpu = *((int *)inode->priv); - -/* Wrap around if need be */ - et = (et +1 ) & (MAX_BUFFER -1 ); - - decoder->tail = et; - decoder->packet_count++; + decoder->packet_buffer[et].start_addr =0xdeadbeefdeadbeefUL ; + decoder->packet_buffer[et].end_addr =0xdeadbeefdeadbeefUL ;if (decoder->packet_count ==MAX_BUFFER -1 )return OCSD_RESP_WAIT ;
@@ -297,6 +297,40 @@ cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,return OCSD_RESP_CONT ; } +static ocsd_datapath_resp_t +cs_etm_decoder__buffer_range (struct cs_etm_decoder *decoder, +const ocsd_generic_trace_elem *elem, +const uint8_t trace_chan_id) +{ +int ret =0 ; +struct cs_etm_packet *packet; + + ret =cs_etm_decoder__buffer_packet (decoder, trace_chan_id, +CS_ETM_RANGE ); +if (ret !=OCSD_RESP_CONT && ret !=OCSD_RESP_WAIT ) +return ret; + + packet = &decoder->packet_buffer[decoder->tail]; + + packet->start_addr = elem->st_addr; + packet->end_addr = elem->en_addr; +switch (elem->last_i_type) { +case OCSD_INSTR_BR : +case OCSD_INSTR_BR_INDIRECT : + packet->last_instr_taken_branch = elem->last_instr_exec; +break ; +case OCSD_INSTR_ISB : +case OCSD_INSTR_DSB_DMB : +case OCSD_INSTR_OTHER : +default : + packet->last_instr_taken_branch = false; +break ; + } + +return ret; + +} + static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( const void *context, const ocsd_trc_index_t indx __maybe_unused,
@@ -316,9 +350,8 @@ static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( decoder->trace_on = true;break ;case OCSD_GEN_TRC_ELEM_INSTR_RANGE : - resp =cs_etm_decoder__buffer_packet (decoder, elem, - trace_chan_id, -CS_ETM_RANGE ); + resp =cs_etm_decoder__buffer_range (decoder, elem, + trace_chan_id);break ;case OCSD_GEN_TRC_ELEM_EXCEPTION : decoder->packet_buffer[decoder->tail].exc = true;
tools/perf/util/cs-etm-decoder/cs-etm-decoder.h +1 -0
@@ -30,6 +30,7 @@ struct cs_etm_packet {enum cs_etm_sample_type sample_type;u64 start_addr;u64 end_addr; +u8 last_instr_taken_branch;u8 exc;u8 exc_ret;int cpu;
tools/perf/util/cs-etm.c +386 -48
550 lines not on this page · raw message
PATCH v2 2/3 perf inject: Emit instruction records on ETM trace discontinuity
In reply to Robert WalkerThere may be discontinuities in the ETM trace stream due to overflows or ETM configuration for selective trace. This patch emits an instruction sample with the pending branch stack when a TRACE ON packet occurs indicating a discontinuity in the trace data.
A new packet type CS_ETM_TRACE_ON is added, which is emitted by the low level decoder when a TRACE ON occurs. The higher level decoder flushes the branch stack when this packet is emitted.
--- tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 9 +++ tools/perf/util/cs-etm-decoder/cs-etm-decoder.h | 1 + tools/perf/util/cs-etm.c | 80 ++++++++++++++++++------- 3 files changed, 67 insertions(+), 23 deletions(-)
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +9 -0
@@ -328,7 +328,14 @@ cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder, }return ret; +} +static ocsd_datapath_resp_t +cs_etm_decoder__buffer_trace_on (struct cs_etm_decoder *decoder, +const uint8_t trace_chan_id) +{ +return cs_etm_decoder__buffer_packet (decoder, trace_chan_id, +CS_ETM_TRACE_ON ); } static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
@@ -347,6 +354,8 @@ static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( decoder->trace_on = false;break ;case OCSD_GEN_TRC_ELEM_TRACE_ON : + resp =cs_etm_decoder__buffer_trace_on (decoder, + trace_chan_id); decoder->trace_on = true;break ;case OCSD_GEN_TRC_ELEM_INSTR_RANGE :
tools/perf/util/cs-etm-decoder/cs-etm-decoder.h +1 -0
@@ -24,6 +24,7 @@ struct cs_etm_buffer {enum cs_etm_sample_type {CS_ETM_RANGE =1 <<0 , +CS_ETM_TRACE_ON =1 <<1 , };struct cs_etm_packet {
tools/perf/util/cs-etm.c +57 -23
101 lines not on this page · raw message
Signature · 1 line
2.7.4
tip-bot for Robert Walker
merge bot[tip:perf/core] perf inject: Emit instruction records on ETM trace discontinuity
perf inject: Emit instruction records on ETM trace discontinuity
Commit-ID: 256e751cac78739a4de2232450d3681b68b5845e Gitweb: https://git.kernel.org/tip/256e751cac78739a4de2232450d3681b68b5845e Author: Robert Walker <[email protected]> AuthorDate: Wed, 14 Feb 2018 11:24:40 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 16 Feb 2018 14:55:45 -0300
perf inject: Emit instruction records on ETM trace discontinuity
There may be discontinuities in the ETM trace stream due to overflows or ETM configuration for selective trace. This patch emits an instruction sample with the pending branch stack when a TRACE ON packet occurs indicating a discontinuity in the trace data.
A new packet type CS_ETM_TRACE_ON is added, which is emitted by the low level decoder when a TRACE ON occurs. The higher level decoder flushes the branch stack when this packet is emitted.
--- tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 9 +++ tools/perf/util/cs-etm-decoder/cs-etm-decoder.h | 1 + tools/perf/util/cs-etm.c | 80 ++++++++++++++++++------- 3 files changed, 67 insertions(+), 23 deletions(-)
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +9 -0
@@ -328,7 +328,14 @@ cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder, }return ret; +} +static ocsd_datapath_resp_t +cs_etm_decoder__buffer_trace_on (struct cs_etm_decoder *decoder, +const uint8_t trace_chan_id) +{ +return cs_etm_decoder__buffer_packet (decoder, trace_chan_id, +CS_ETM_TRACE_ON ); } static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
@@ -347,6 +354,8 @@ static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( decoder->trace_on = false;break ;case OCSD_GEN_TRC_ELEM_TRACE_ON : + resp =cs_etm_decoder__buffer_trace_on (decoder, + trace_chan_id); decoder->trace_on = true;break ;case OCSD_GEN_TRC_ELEM_INSTR_RANGE :
tools/perf/util/cs-etm-decoder/cs-etm-decoder.h +1 -0
@@ -24,6 +24,7 @@ struct cs_etm_buffer {enum cs_etm_sample_type {CS_ETM_RANGE =1 <<0 , +CS_ETM_TRACE_ON =1 <<1 , };struct cs_etm_packet {
tools/perf/util/cs-etm.c +57 -23
101 lines not on this page · raw message
PATCH v2 3/3 coresight: Update documentation for perf usage
In reply to Robert WalkerAdd notes on using perf to collect and analyze CoreSight trace
--- Documentation/trace/coresight.txt | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
Documentation/trace/coresight.txt +51 -0
@@ -330,3 +330,54 @@ Details on how to use the generic STM API can be found here [2]. [1]. Documentation/ABI/testing/sysfs-bus-coresight-devices-stm [2]. Documentation/trace/stm.txt + + +Using perf tools +---------------- + +perf can be used to record and analyze trace of programs. + +Execution can be recorded using perf record with the cs_etm event, +specifying the name of the sink to record to, e.g: + + perf record -e cs_etm/@20070000.etr/u --per-thread + +The perf report and script commands can be used to analyze execution, +synthesizing instruction and branch events from the instruction trace. perf +inject can be used to replace the trace data with the synthesized events. +The --itrace option controls the type and frequency of synthesized events +(see perf documentation). + +Note that only 64-bit programs are currently supported - further work is +required to support instruction decode of 32-bit Arm programs. + + +Generating coverage files for Feedback Directed Optimization: AutoFDO +--------------------------------------------------------------------- + +perf inject accepts the --itrace option in which case tracing data is +removed and replaced with the synthesized events. e.g. + + perf inject --itrace --strip -i perf.data -o perf.data.new + +Below is an example of using ARM ETM for autoFDO. It requires autofdo +(https://github.com/google/autofdo) and gcc version 5. The bubble +sort example is from the AutoFDO tutorial (https://gcc.gnu.org/wiki/AutoFDO/Tutorial). + + $ gcc-5 -O3 sort.c -o sort + $ taskset -c 2 ./sort + Bubble sorting array of 30000 elements + 5910 ms + + $ perf record -e cs_etm/@20070000.etr/u --per-thread taskset -c 2 ./sort + Bubble sorting array of 30000 elements + 12543 ms + [ perf record: Woken up 35 times to write data ] + [ perf record: Captured and wrote 69.640 MB perf.data ] + + $ perf inject -i perf.data -o inj.data --itrace=il64 --strip + $ create_gcov --binary=./sort --profile=inj.data --gcov=sort.gcov -gcov_version=1 + $ gcc-5 -O3 -fauto-profile=sort.gcov sort.c -o sort_autofdo + $ taskset -c 2 ./sort_autofdo + Bubble sorting array of 30000 elements + 5806 ms
Signature · 1 line
2.7.4
tip-bot for Robert Walker
merge bot[tip:perf/core] coresight: Update documentation for perf usage
coresight: Update documentation for perf usage
Commit-ID: 6673016f874adee171690f95540ead5efddb5351 Gitweb: https://git.kernel.org/tip/6673016f874adee171690f95540ead5efddb5351 Author: Robert Walker <[email protected]> AuthorDate: Wed, 14 Feb 2018 11:24:41 +0000 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Fri, 16 Feb 2018 14:55:46 -0300
coresight: Update documentation for perf usage
Add notes on using perf to collect and analyze CoreSight trace
--- Documentation/trace/coresight.txt | 51 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
Documentation/trace/coresight.txt +51 -0
@@ -330,3 +330,54 @@ Details on how to use the generic STM API can be found here [2]. [1]. Documentation/ABI/testing/sysfs-bus-coresight-devices-stm [2]. Documentation/trace/stm.txt + + +Using perf tools +---------------- + +perf can be used to record and analyze trace of programs. + +Execution can be recorded using 'perf record' with the cs_etm event, +specifying the name of the sink to record to, e.g: + + perf record -e cs_etm/@20070000.etr/u --per-thread + +The 'perf report' and 'perf script' commands can be used to analyze execution, +synthesizing instruction and branch events from the instruction trace. +'perf inject' can be used to replace the trace data with the synthesized events. +The --itrace option controls the type and frequency of synthesized events +(see perf documentation). + +Note that only 64-bit programs are currently supported - further work is +required to support instruction decode of 32-bit Arm programs. + + +Generating coverage files for Feedback Directed Optimization: AutoFDO +--------------------------------------------------------------------- + +'perf inject' accepts the --itrace option in which case tracing data is +removed and replaced with the synthesized events. e.g. + + perf inject --itrace --strip -i perf.data -o perf.data.new + +Below is an example of using ARM ETM for autoFDO. It requires autofdo +(https://github.com/google/autofdo) and gcc version 5. The bubble +sort example is from the AutoFDO tutorial (https://gcc.gnu.org/wiki/AutoFDO/Tutorial). + + $ gcc-5 -O3 sort.c -o sort + $ taskset -c 2 ./sort + Bubble sorting array of 30000 elements + 5910 ms + + $ perf record -e cs_etm/@20070000.etr/u --per-thread taskset -c 2 ./sort + Bubble sorting array of 30000 elements + 12543 ms + [ perf record: Woken up 35 times to write data ] + [ perf record: Captured and wrote 69.640 MB perf.data ] + + $ perf inject -i perf.data -o inj.data --itrace=il64 --strip + $ create_gcov --binary=./sort --profile=inj.data --gcov=sort.gcov -gcov_version=1 + $ gcc-5 -O3 -fauto-profile=sort.gcov sort.c -o sort_autofdo + $ taskset -c 2 ./sort_autofdo + Bubble sorting array of 30000 elements + 5806 ms