This is an archive of the discontinued LLVM Phabricator instance.

[CSSPGO][llvm-profgen] Fix bug with parsing hybrid sample trace line
ClosedPublic

Authored by wlei on Jan 26 2021, 1:46 PM.

Details

Summary

when we skip the call stack starting with an external address, we should also skip the bottom LBR entry, otherwise it will cause a truncated context issue.

Diff Detail

Event Timeline

wlei created this revision.Jan 26 2021, 1:46 PM
wlei requested review of this revision.Jan 26 2021, 1:46 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 26 2021, 1:46 PM
wlei retitled this revision from [NFC] Fix bug with parsing trace line to [NFC] Fix bug with parsing hybrid sample trace line.Jan 26 2021, 1:52 PM
wlei edited the summary of this revision. (Show Details)
wlei added reviewers: hoy, wenlei, wmi, davidxl.
hoy added inline comments.Jan 26 2021, 3:04 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

A truncated context might still be helpful to the sample loader inliner. We saw this happened to some SPEC2006 benchmarks. What issue did it cause to you?

wlei added inline comments.Jan 26 2021, 3:19 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

Sorry for the confusion. This line is for the non-address line like empty line.

This patch is for the issue like below

external address1
..
address2
address3
LBR entry

The first one is an external address1, so we need also skip the address2 and address3. otherwise, the missing part is the top frame, it will make the unwinder incorrect.

For the right truncated context, it's like below

address1
address2
external address3
..
LBR entry

Here the truncated part is at the bottom, then the compiler can use this for the inliner. The tool can handle this case.

hoy added inline comments.Jan 26 2021, 4:03 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

Oh I see. Thanks for explaining. So with your change, we now restart when seeing an empty line. And we continue scanning even with an empty call stack to avoid breaking the samples into two parts, with the second part being a truncated call stack?

wlei added inline comments.Jan 26 2021, 4:15 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

Yeah, exactly. Our internal prototype didn't have this issue since the whole trace file is split by the '\n\n', so it will always ensure a whole sample and avoid breaking into two parts. But for llvm-profgen, it's processed line by line and previously it will start parsing new sample when the call stack is empty which will cause this bug when the first address is an external one.

hoy added inline comments.Jan 26 2021, 4:20 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

Sounds good. Is it possible to add a test based on the current test binaries?

wlei added inline comments.Jan 26 2021, 4:23 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

Yeah, Good idea! I can manually create a perfscript for this.

wlei updated this revision to Diff 319435.Jan 26 2021, 4:38 PM

add test for this

wlei updated this revision to Diff 319436.Jan 26 2021, 4:40 PM

fix indentation

wlei updated this revision to Diff 319437.Jan 26 2021, 4:42 PM

fix indentation..

wlei added inline comments.Jan 26 2021, 4:44 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

Just copy another sample and added a fake top address on it. with out this fix, it will generate wrong profile count.

hoy accepted this revision.Jan 26 2021, 4:47 PM

LGTM, thanks for the fix!

llvm/tools/llvm-profgen/PerfReader.cpp
486–487

Nit: comment may be updated to reflect the truncated callstack case.

This revision is now accepted and ready to land.Jan 26 2021, 4:47 PM
wlei updated this revision to Diff 319442.Jan 26 2021, 5:06 PM

update comments

wenlei added inline comments.Jan 26 2021, 10:35 PM
llvm/tools/llvm-profgen/PerfReader.cpp
444

line 413 and line 416 can be hoisted to line 409?

445

This change means we no longer handle a comment line mixed in stack sample? Although I'm not sure if we ever need a comment line..

wlei added inline comments.Jan 26 2021, 11:00 PM
llvm/tools/llvm-profgen/PerfReader.cpp
444

This is tricky, because the underlying TraceStream read line by line .. you see in line 408 StringRef FrameStr which use StringRef, if we hoist TraceIt.advance();, the internal string instance will be freed. That means we must ensure to use the StringRef before call advance. have the comments in TraceStream.

445

yeah, I think so. For the perf generated perfscript, I saw the only case is a log line at the head of the file, shouldn't have comments in other place. Here the comments are most for the regression test, but as you see we have to make sure it's not mixed in the sample.

wenlei added inline comments.Jan 26 2021, 11:28 PM
llvm/tools/llvm-profgen/PerfReader.cpp
444

I see.. thanks for clarification.

445

Ok, update the comment then, and we can also assert that if FrameStr.getAsInteger(16, FrameAddr) is true, it must be empty line.

and btw.. bug fix isn't really NFC

hoy added inline comments.Jan 26 2021, 11:35 PM
llvm/tools/llvm-profgen/PerfReader.cpp
445

A comment line at the beginning of a callstack sample should still be OK, but not for a comment in the middle of a callstack sample. Previously we would just skip the frames following a comment. Will a continue work here if we need comments?

wlei added a comment.Jan 27 2021, 10:10 AM

and btw.. bug fix isn't really NFC

Ah,I misuse it.. let me change the title.

llvm/tools/llvm-profgen/PerfReader.cpp
445

we can also assert that if FrameStr.getAsInteger(16, FrameAddr) is true, it must be empty line.

Sorry I just make mistakes to explain, it could have comments between different sample but not in the middle of sample(as Hongtao reminded), like:

...

address1
address2
LBR

;legal comment

address3
;illegal comment
address4
LBR

Will a continue work here if we need comments?

continue might not work here. because we could have MMAP event, like:

; comment1
MMAP_Event_..
address1
...

So it will also skip the MMAP line, that's why we return false to defer parsing the next line to parseEventOrSample.

wlei retitled this revision from [NFC] Fix bug with parsing hybrid sample trace line to [CSSPGO][llvm-profgen] Fix bug with parsing hybrid sample trace line.Jan 27 2021, 10:14 AM
wenlei accepted this revision.Jan 27 2021, 12:08 PM