perf, script: Minimize "not reaching sample" for brstackinsn
PATCH 1/1 perf, script: Minimize "not reaching sample" for brstackinsn
In some situations perf script -F +brstackinsn sees a lot of "not reaching sample" messages. This happens when the last LBR block before the sample contains a branch that is not in the LBR, and the instruction dumping stops.
$ perf record -b emacs -Q --batch '()'
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.396 MB perf.data (443 samples) ]
$ perf script -F +brstackinsn
...
00007f0ab2d171a4 insn: 41 0f 94 c0
00007f0ab2d171a8 insn: 83 fa 01
00007f0ab2d171ab insn: 74 d3 # PRED 6 cycles [313] 1.00 IPC
00007f0ab2d17180 insn: 45 84 c0
00007f0ab2d17183 insn: 74 28
... not reaching sample ...$ perf script -F +brstackinsn | grep -c reach 136
This is a problem for further analysis that wants to see the full code upto the sample.
There are two common cases where the message is bogus:
- The LBR only logs taken branches, but the branch might be a conditional branch that is not taken (that is the most common case actually)
- The LBR sampling uses a filter ignoring some branches, but the perf script check checks for all branches.
This patch fixes these two conditions, by only checking for conditional branches, as well as checking the perf_event_attr's branch filter attributes.
For the test case above it fixes all the messages:
$ ./perf script -F +brstackinsn | grep -c reach 0
Note that there are still conditions when the message is hit -- sometimes there can be a unconditional branch that misses the LBR update before the sample -- but they are much more rare now.
--- tools/perf/builtin-script.c | 4 +++- tools/perf/util/dump-insn.c | 2 +- tools/perf/util/dump-insn.h | 2 +- tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c | 5 +++-- 4 files changed, 8 insertions(+), 5 deletions(-)
tools/perf/builtin-script.c +3 -1
@@ -1378,7 +1378,9 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, printed +=fprintf (fp,"\n" );if (ilen ==0 )break ; -if (arch_is_branch (buffer + off, len - off, x.is64bit) && start + off != sample->ip) { +if ((attr->branch_sample_type ==0 || attr->branch_sample_type &PERF_SAMPLE_BRANCH_ANY ) + &&arch_is_uncond_branch (buffer + off, len - off, x.is64bit) + && start + off != sample->ip) {/* * Hit a missing branch. Just stop. */
tools/perf/util/dump-insn.c +1 -1
@@ -15,7 +15,7 @@ const char *dump_insn(struct perf_insn *x __maybe_unused, }__weak -int arch_is_branch (const unsigned char *buf__maybe_unused , +int arch_is_uncond_branch (const unsigned char *buf__maybe_unused ,size_t len__maybe_unused ,int x86_64__maybe_unused ) {
tools/perf/util/dump-insn.h +1 -1
@@ -20,6 +20,6 @@ struct perf_insn {const char *dump_insn (struct perf_insn *x,u64 ip,u8 *inbuf,int inlen,int *lenp); -int arch_is_branch (const unsigned char *buf,size_t len,int x86_64); +int arch_is_uncond_branch (const unsigned char *buf,size_t len,int x86_64);#endif
tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c +3 -2
@@ -200,12 +200,13 @@ int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,return 0 ; } -int arch_is_branch (const unsigned char *buf,size_t len,int x86_64) +int arch_is_uncond_branch (const unsigned char *buf,size_t len,int x86_64) {struct intel_pt_insn in;if (intel_pt_get_insn (buf, len, x86_64, &in) <0 )return -1 ; -return in.branch !=INTEL_PT_BR_NO_BRANCH ; +return in.branch ==INTEL_PT_BR_UNCONDITIONAL || + in.branch ==INTEL_PT_BR_INDIRECT ; } const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
Signature · 1 line
2.43.0
Adrian Hunter
reviewerQuoted 20 lines · Andi Kleen
In some situations perf script -F +brstackinsn sees a lot of "not reaching sample" messages. This happens when the last LBR block before the sample contains a branch that is not in the LBR, and the instruction dumping stops.
$ perf record -b emacs -Q --batch '()' [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.396 MB perf.data (443 samples) ] $ perf script -F +brstackinsn ... 00007f0ab2d171a4 insn: 41 0f 94 c0 00007f0ab2d171a8 insn: 83 fa 01 00007f0ab2d171ab insn: 74 d3 # PRED 6 cycles [313] 1.00 IPC 00007f0ab2d17180 insn: 45 84 c0 00007f0ab2d17183 insn: 74 28 ... not reaching sample ...$ perf script -F +brstackinsn | grep -c reach 136
Quoted 7 lines
This is a problem for further analysis that wants to see the full code upto the sample.
There are two common cases where the message is bogus:
- The LBR only logs taken branches, but the branch might be a conditional branch that is not taken (that is the most common case actually)
How do you know it is not a taken branch that missed the LBR update?
Quoted 2 lines
- The LBR sampling uses a filter ignoring some branches, but the perf script check checks for all branches.
Not understanding this case. Do you mean you expect not to reach the sample, so there is no point printing the message?
Quoted 27 lines
This patch fixes these two conditions, by only checking for conditional branches, as well as checking the perf_event_attr's branch filter attributes.
For the test case above it fixes all the messages:
$ ./perf script -F +brstackinsn | grep -c reach 0Note that there are still conditions when the message is hit -- sometimes there can be a unconditional branch that misses the LBR update before the sample -- but they are much more rare now.
Signed-off-by Andi Kleen <[email protected]>--- tools/perf/builtin-script.c | 4 +++- tools/perf/util/dump-insn.c | 2 +- tools/perf/util/dump-insn.h | 2 +- tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c | 5 +++-- 4 files changed, 8 insertions(+), 5 deletions(-)tools/perf/builtin-script.c +0
-0
tools/perf/builtin-script.c static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, · line 1383
Quoted 7 lines
tools/perf/builtin-script.c +3
-1@@ -1378,7 +1378,9 @@ static int perf_sample__fprintf_brstackinsn(struct perf_sample *sample, printed +=fprintf (fp,"\n" );if (ilen ==0 )break ; -if (arch_is_branch (buffer + off, len - off, x.is64bit) && start + off != sample->ip) { +if ((attr->branch_sample_type ==0 || attr->branch_sample_type &PERF_SAMPLE_BRANCH_ANY ) + &&arch_is_uncond_branch (buffer + off, len - off, x.is64bit) + && start + off != sample->ip) {
Needs a comment, or update the comment further back.
Quoted 48 lines
tools/perf/builtin-script.c +0
-0/* * Hit a missing branch. Just stop. */ tools/perf/util/dump-insn.c +1
-1@@ -15,7 +15,7 @@ const char *dump_insn(struct perf_insn *x __maybe_unused, }__weak -int arch_is_branch (const unsigned char *buf__maybe_unused , +int arch_is_uncond_branch (const unsigned char *buf__maybe_unused ,size_t len__maybe_unused ,int x86_64__maybe_unused ) {tools/perf/util/dump-insn.h +1
-1@@ -20,6 +20,6 @@ struct perf_insn {const char *dump_insn (struct perf_insn *x,u64 ip,u8 *inbuf,int inlen,int *lenp); -int arch_is_branch (const unsigned char *buf,size_t len,int x86_64); +int arch_is_uncond_branch (const unsigned char *buf,size_t len,int x86_64);#endif tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c +3
-2@@ -200,12 +200,13 @@ int intel_pt_get_insn(const unsigned char *buf, size_t len, int x86_64,return 0 ; } -int arch_is_branch (const unsigned char *buf,size_t len,int x86_64) +int arch_is_uncond_branch (const unsigned char *buf,size_t len,int x86_64) {struct intel_pt_insn in;if (intel_pt_get_insn (buf, len, x86_64, &in) <0 )return -1 ; -return in.branch !=INTEL_PT_BR_NO_BRANCH ; +return in.branch ==INTEL_PT_BR_UNCONDITIONAL || + in.branch ==INTEL_PT_BR_INDIRECT ; } const char *dump_insn(struct perf_insn *x, uint64_t ip __maybe_unused,
Quoted 6 lines
Quoted 4 lines
There are two common cases where the message is bogus:
- The LBR only logs taken branches, but the branch might be a conditional branch that is not taken (that is the most common case actually)How do you know it is not a taken branch that missed the LBR update?
I don't, but the not taken case is totally valid (and also common) so it doesn't make sense to have a mere sanity check make a common case unusable.
Quoted 6 lines
Quoted 3 lines
- The LBR sampling uses a filter ignoring some branches, but the perf script check checks for all branches.
Not understanding this case. Do you mean you expect not to reach the sample, so there is no point printing the message?
If the LBR is e.g. filtered on far branches it makes no sense to check for non far branches. There are lot of filtering cases here which would be very complicated to handle for a mere sanity check, so the best way is to not do the bogus sanity check.
-Andi
Adrian Hunter
reviewerQuoted 15 lines · Andi Kleen
Quoted 6 lines
Quoted 4 lines
There are two common cases where the message is bogus:
- The LBR only logs taken branches, but the branch might be a conditional branch that is not taken (that is the most common case actually)How do you know it is not a taken branch that missed the LBR update?
I don't, but the not taken case is totally valid (and also common) so it doesn't make sense to have a mere sanity check make a common case unusable.
Quoted 4 lines
Quoted 3 lines
- The LBR sampling uses a filter ignoring some branches, but the perf script check checks for all branches.
Quoted 7 lines
Quoted 2 lines
Not understanding this case. Do you mean you expect not to reach the sample, so there is no point printing the message?
If the LBR is e.g. filtered on far branches it makes no sense to check for non far branches. There are lot of filtering cases here which would be very complicated to handle for a mere sanity check, so the best way is to not do the bogus sanity check.
This comment could be updated
* between final branch and sample. When this happens just
* continue walking after the last TO until we hit a branch.
It's not clear why "attr->branch_sample_type == 0" is there.