This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profgen] Add process filter for perf reader
ClosedPublic

Authored by wenlei on Apr 15 2022, 12:32 PM.

Details

Summary

For profile generation, we need to filter raw perf samples for binary of interest. Sometimes binary name along isn't enough as we can have binary of the same name running in the system. This change adds a process id filter to allow users to further disambiguiate the input raw samples.

Diff Detail

Event Timeline

wenlei created this revision.Apr 15 2022, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2022, 12:32 PM
Herald added a subscriber: modimo. · View Herald Transcript
wenlei requested review of this revision.Apr 15 2022, 12:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 15 2022, 12:32 PM
hoy added inline comments.Apr 15 2022, 3:39 PM
llvm/tools/llvm-profgen/PerfReader.h
549

Can we just check against the switch ProcessId in convertPerfDataToTrace? Feel like PID is perf data specific and changing the general interface here may not be necessary. We have other filters that are for a particular reader, as an example ignore-stack-samples.

wenlei added inline comments.Apr 15 2022, 10:13 PM
llvm/tools/llvm-profgen/PerfReader.h
549

Can we just check against the switch ProcessId in convertPerfDataToTrace?

We also need to check PID in updateBinaryAddress. I think PID is in the same category as Binary, both are filters for perf events.

Feel like PID is perf data specific and changing the general interface here may not be necessary.

Isn't any perf reader type under PerfReaderBase supposed to be perf data specific? The only thing that is not perf data related is UnsymbolizedProfileReader, but then I think the real problem is really why we have UnsymbolizedProfileReader inheriting from perf reader while it's clearly not reading perf data/script.

hoy added inline comments.Apr 16 2022, 11:01 AM
llvm/tools/llvm-profgen/PerfReader.h
549

We also need to check PID in updateBinaryAddress.

The PID filter isn't that meaningful in perf script file. Though we can still use pid to filter out the mmap events there, but the main LBR/stack samples mixed from multiple processes are not separable. So PID filter should be applied when converting perf.data to perf script, which makes me think it is perf data specific.

Isn't any perf reader type under PerfReaderBase supposed to be perf data specific?

Not necessarily. Perf script and our internal lbr profile do not see
everything that perf.data has. The two formats assume all data in them are for one process. Do you see with pid filter, convertPerfDataToTrace still gives mixed mmap evens from all processes?

wenlei added inline comments.Apr 16 2022, 4:30 PM
llvm/tools/llvm-profgen/PerfReader.h
549

We also need to check PID in updateBinaryAddress.

The PID filter isn't that meaningful in perf script file. Though we can still use pid to filter out the mmap events there, but the main LBR/stack samples mixed from multiple processes are not separable. So PID filter should be applied when converting perf.data to perf script, which makes me think it is perf data specific.

We need to use pid to filter mmap event, otherwise we don't know which mmap to use to set base address and the result would be wrong, so pid is meaningful for perf script.

Isn't any perf reader type under PerfReaderBase supposed to be perf data specific?

Not necessarily. Perf script and our internal lbr profile do not see
everything that perf.data has. The two formats assume all data in them are for one process.

Maybe I didn't understand what you're trying to say. I don't see a problem because: 1) there's no fundamental difference between perf data and perf script; 2) By the name PerfReaderBase, the entire hierarchy is perf specific; 3) then adding pid to general interface isn't a problem.

Also I don't see pid is being very different from binary name, it's another filter, similar to binary name.

Do you see with pid filter, convertPerfDataToTrace still gives mixed mmap evens from all processes?

Yes, if you are asking about output from perf script .. with pid filter. mmap from processes are still there.

hoy added inline comments.Apr 16 2022, 8:35 PM
llvm/tools/llvm-profgen/PerfReader.h
549

I see, looks like mmap events are special from other sample events in that they cannot be filtered by pid. I was hoping that perf script output could be made dedicate to a process since that's what perf script --pid is shooting for? Once filtered, the processing of perf script output should no longer need to deal with pid again. This sounds like a bug of perf script.

The PerfReaderBase hierarchy seems improper in naming. Both perf.data and perf script output are processed by PerfScriptReader and the base class PerfReaderBase is there only to provide an interface to create a specific reader instance, including UnsymbolizedProfileReader. I think the intention of including UnsymbolizedProfileReader in the hierarchy was to avoid a special path in the driver, but then PerfReaderBase should be named ReaderBase or something.

It then makes sense to pass pid to the perf reader hierarchy and separating UnsymbolizedProfileReader out. The separation can be done in a different patch.

Use optional<uint32_t> for PIDFilter in case zero is a valid kernel process?

wenlei added inline comments.Apr 16 2022, 9:00 PM
llvm/tools/llvm-profgen/PerfReader.h
549

I was hoping that perf script output could be made dedicate to a process since that's what perf script --pid is shooting for?

No, as I said that's what you get with perf script. I added the change in updateBinaryAddress after I realized that.

This sounds like a bug of perf script.

I'm not sure, unless there's a clear spec this is basically implementation dependent. As you mentioned, mmap can be considered special.

I think the intention of including UnsymbolizedProfileReader in the hierarchy was to avoid a special path in the driver, but then PerfReaderBase should be named ReaderBase or something.

I don't think make non-perf reader a subclass of perf reader is a good idea, and avoiding special path in driver doesn't justify that.

The PerfReaderBase hierarchy seems improper in naming.

The hierarchy is improper, but I'm not sure if it's just a naming thing. One can also argue perhaps it's better to have special path in driver like llvm-sample-profile. It was a similar situation and we settled on having special path there. Regardless this is unrelated to this patch.

Use optional<uint32_t> for PIDFilter in case zero is a valid kernel process?

I'd prefer simplicity unless proven necessary. I don't think zero process id is something we'd worry about. But I don't have strong opinion on this.

hoy added inline comments.Apr 17 2022, 8:18 PM
llvm/tools/llvm-profgen/PerfReader.h
549

I'd prefer simplicity unless proven necessary. I don't think zero process id is something we'd worry about. But I don't have strong opinion on this.

In case we do with Kernel with autofdo where zero can be a valid kernel process id. Also using optional sounds a trend as I see more refactoring changes for that in the code base. The way I see it, it helps achieve simplicity, otherwise you would need an explicit comment somewhere about using zero as an invalid pid.

wenlei added inline comments.Apr 17 2022, 8:55 PM
llvm/tools/llvm-profgen/PerfReader.h
549

I'd prefer simplicity unless proven necessary. I don't think zero process id is something we'd worry about. But I don't have strong opinion on this.

In case we do with Kernel with autofdo where zero can be a valid kernel process id. Also using optional sounds a trend as I see more refactoring changes for that in the code base. The way I see it, it helps achieve simplicity, otherwise you would need an explicit comment somewhere about using zero as an invalid pid.

Sounds good. I just realized that kernel scheduler actually uses 0 process id.

wenlei updated this revision to Diff 423331.Apr 17 2022, 8:56 PM

Use Optional<> for PIDFilter

hoy accepted this revision.Apr 17 2022, 10:15 PM

lgtm, thanks.

This revision is now accepted and ready to land.Apr 17 2022, 10:15 PM
This revision was landed with ongoing or failed builds.Apr 18 2022, 9:58 AM
This revision was automatically updated to reflect the committed changes.
Meinersbur added inline comments.
llvm/tools/llvm-profgen/PerfReader.h
16

This broke the Windows build. bits/stdint-uintn.h is platform-specific.

wenlei added inline comments.Apr 18 2022, 11:03 AM
llvm/tools/llvm-profgen/PerfReader.h
16

Ops, sorry this change wasn't intentional, just pushed in a fix to remove it. https://reviews.llvm.org/rG526af13ebaa9