[CSSPGO][llvm-profgen] Context-sensitive profile data generation
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
This change supports context-sensitive profile data generation into llvm-profgen. With simultaneous sampling for LBR and call stack, we can identify leaf of LBR sample with calling context from stack sample . During the process of deriving fall through path from LBR entries, we unwind LBR by replaying all the calls and returns (including implicit calls/returns due to inlining) backwards on top of the sampled call stack. Then the state of call stack as we unwind through LBR always represents the calling context of current fall through path.
we have two types of virtual unwinding 1) LBR unwinding and 2) linear range unwinding.
Specifically, for each LBR entry which can be classified into call, return, regular branch, LBR unwinding will replay the operation by pushing, popping or switching leaf frame towards the call stack and since the initial call stack is most recently sampled, the replay should be in anti-execution order, i.e. for the regular case, pop the call stack when LBR is call, push frame on call stack when LBR is return. After each LBR processed, it also needs to align with the next LBR by going through instructions from previous LBR's target to current LBR's source, which we named linear unwinding. As instruction from linear range can come from different function by inlining, linear unwinding will do the range splitting and record counters through the range with same inline context.
With each fall through path from LBR unwinding, we aggregate each sample into counters by the calling context and eventually generate full context sensitive profile (without relying on inlining) to driver compiler's PGO/FDO.
A breakdown of noteworthy changes:
- Added `HybridSample` class as the abstraction perf sample including LBR stack and call stack
* Extended `PerfReader` to implement auto-detect whether input perf script output contains CS profile, then do the parsing. Multiple `HybridSample` are extracted
* Speed up by aggregating `HybridSample` into `AggregatedSamples`
* Added VirtualUnwinder that consumes aggregated `HybridSample` and implements unwinding of calls, returns, and linear path that contains implicit call/return from inlining. Ranges and branches counters are aggregated by the calling context.
Here calling context is string type, each context is a pair of function name and callsite location info, the whole context is like `main:1 @ foo:2 @ bar`.
* Added PorfileGenerater that accumulates counters by ranges unfolding or branch target mapping, then generates context-sensitive function profile including function body, inferring callee's head sample, callsite target samples, eventually records into ProfileMap.
* Leveraged LLVM build-in(`SampleProfWriter`) writer to support different serialization format with no stop
- `getCanonicalFnName` for callee name and name from ELF section
- Added regression test for both unwinding and profile generation
Test Plan:
ninja & ninja check-llvm
Reviewed By: hoy, wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89723
2020-10-20 03:55:59 +08:00
|
|
|
//===- llvm-profgen.cpp - LLVM SPGO profile generation tool -----*- C++ -*-===//
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// llvm-profgen generates SPGO profiles from perf script ouput.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ErrorHandling.h"
|
|
|
|
#include "PerfReader.h"
|
[CSSPGO][llvm-profgen] Context-sensitive profile data generation
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
This change supports context-sensitive profile data generation into llvm-profgen. With simultaneous sampling for LBR and call stack, we can identify leaf of LBR sample with calling context from stack sample . During the process of deriving fall through path from LBR entries, we unwind LBR by replaying all the calls and returns (including implicit calls/returns due to inlining) backwards on top of the sampled call stack. Then the state of call stack as we unwind through LBR always represents the calling context of current fall through path.
we have two types of virtual unwinding 1) LBR unwinding and 2) linear range unwinding.
Specifically, for each LBR entry which can be classified into call, return, regular branch, LBR unwinding will replay the operation by pushing, popping or switching leaf frame towards the call stack and since the initial call stack is most recently sampled, the replay should be in anti-execution order, i.e. for the regular case, pop the call stack when LBR is call, push frame on call stack when LBR is return. After each LBR processed, it also needs to align with the next LBR by going through instructions from previous LBR's target to current LBR's source, which we named linear unwinding. As instruction from linear range can come from different function by inlining, linear unwinding will do the range splitting and record counters through the range with same inline context.
With each fall through path from LBR unwinding, we aggregate each sample into counters by the calling context and eventually generate full context sensitive profile (without relying on inlining) to driver compiler's PGO/FDO.
A breakdown of noteworthy changes:
- Added `HybridSample` class as the abstraction perf sample including LBR stack and call stack
* Extended `PerfReader` to implement auto-detect whether input perf script output contains CS profile, then do the parsing. Multiple `HybridSample` are extracted
* Speed up by aggregating `HybridSample` into `AggregatedSamples`
* Added VirtualUnwinder that consumes aggregated `HybridSample` and implements unwinding of calls, returns, and linear path that contains implicit call/return from inlining. Ranges and branches counters are aggregated by the calling context.
Here calling context is string type, each context is a pair of function name and callsite location info, the whole context is like `main:1 @ foo:2 @ bar`.
* Added PorfileGenerater that accumulates counters by ranges unfolding or branch target mapping, then generates context-sensitive function profile including function body, inferring callee's head sample, callsite target samples, eventually records into ProfileMap.
* Leveraged LLVM build-in(`SampleProfWriter`) writer to support different serialization format with no stop
- `getCanonicalFnName` for callee name and name from ELF section
- Added regression test for both unwinding and profile generation
Test Plan:
ninja & ninja check-llvm
Reviewed By: hoy, wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89723
2020-10-20 03:55:59 +08:00
|
|
|
#include "ProfileGenerator.h"
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
#include "ProfiledBinary.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2021-08-18 06:53:31 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
2020-10-20 01:02:05 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
|
2021-07-20 23:10:34 +08:00
|
|
|
static cl::OptionCategory ProfGenCategory("ProfGen Options");
|
|
|
|
|
2021-10-15 11:18:53 +08:00
|
|
|
static cl::opt<std::string> PerfScriptFilename(
|
2021-09-30 00:04:44 +08:00
|
|
|
"perfscript", cl::value_desc("perfscript"), cl::ZeroOrMore,
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
llvm::cl::MiscFlags::CommaSeparated,
|
|
|
|
cl::desc("Path of perf-script trace created by Linux perf tool with "
|
2021-07-20 23:10:34 +08:00
|
|
|
"`script` command(the raw perf.data should be profiled with -b)"),
|
|
|
|
cl::cat(ProfGenCategory));
|
2021-10-15 11:18:53 +08:00
|
|
|
static cl::alias PSA("ps", cl::desc("Alias for --perfscript"),
|
|
|
|
cl::aliasopt(PerfScriptFilename));
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
|
2021-09-30 00:04:44 +08:00
|
|
|
static cl::opt<std::string> PerfDataFilename(
|
|
|
|
"perfdata", cl::value_desc("perfdata"), cl::ZeroOrMore,
|
|
|
|
llvm::cl::MiscFlags::CommaSeparated,
|
|
|
|
cl::desc("Path of raw perf data created by Linux perf tool (it should be "
|
|
|
|
"profiled with -b)"),
|
|
|
|
cl::cat(ProfGenCategory));
|
2021-10-15 11:18:53 +08:00
|
|
|
static cl::alias PDA("pd", cl::desc("Alias for --perfdata"),
|
|
|
|
cl::aliasopt(PerfDataFilename));
|
|
|
|
|
|
|
|
static cl::opt<std::string> UnsymbolizedProfFilename(
|
|
|
|
"unsymbolized-profile", cl::value_desc("unsymbolized profile"),
|
|
|
|
cl::ZeroOrMore, llvm::cl::MiscFlags::CommaSeparated,
|
|
|
|
cl::desc("Path of the unsymbolized profile created by "
|
|
|
|
"`llvm-profgen` with `--skip-symbolization`"),
|
|
|
|
cl::cat(ProfGenCategory));
|
|
|
|
static cl::alias UPA("up", cl::desc("Alias for --unsymbolized-profile"),
|
|
|
|
cl::aliasopt(UnsymbolizedProfFilename));
|
2021-09-30 00:04:44 +08:00
|
|
|
|
2022-01-25 08:55:05 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
BinaryPath("binary", cl::value_desc("binary"), cl::Required,
|
|
|
|
cl::desc("Path of profiled executable binary."),
|
|
|
|
cl::cat(ProfGenCategory));
|
|
|
|
|
|
|
|
static cl::opt<std::string> DebugBinPath(
|
|
|
|
"debug-binary", cl::value_desc("debug-binary"), cl::ZeroOrMore,
|
|
|
|
cl::desc("Path of debug info binary, llvm-profgen will load the DWARF info "
|
|
|
|
"from it instead of the executable binary."),
|
2021-08-12 09:01:37 +08:00
|
|
|
cl::cat(ProfGenCategory));
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
|
2021-02-10 08:41:44 +08:00
|
|
|
extern cl::opt<bool> ShowDisassemblyOnly;
|
2021-08-18 06:53:31 +08:00
|
|
|
extern cl::opt<bool> ShowSourceLocations;
|
2021-09-01 04:27:42 +08:00
|
|
|
extern cl::opt<bool> SkipSymbolization;
|
2021-02-10 08:41:44 +08:00
|
|
|
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace sampleprof;
|
|
|
|
|
2021-08-18 06:53:31 +08:00
|
|
|
// Validate the command line input.
|
2021-09-30 00:04:44 +08:00
|
|
|
static void validateCommandLine() {
|
2021-10-15 11:18:53 +08:00
|
|
|
// Allow the missing perfscript if we only use to show binary disassembly.
|
2021-08-18 06:53:31 +08:00
|
|
|
if (!ShowDisassemblyOnly) {
|
2021-10-15 11:18:53 +08:00
|
|
|
// Validate input profile is provided only once
|
|
|
|
uint16_t HasPerfData = PerfDataFilename.getNumOccurrences();
|
|
|
|
uint16_t HasPerfScript = PerfScriptFilename.getNumOccurrences();
|
|
|
|
uint16_t HasUnsymbolizedProfile =
|
|
|
|
UnsymbolizedProfFilename.getNumOccurrences();
|
|
|
|
uint16_t S = HasPerfData + HasPerfScript + HasUnsymbolizedProfile;
|
|
|
|
if (S != 1) {
|
2021-09-30 00:04:44 +08:00
|
|
|
std::string Msg =
|
2021-10-15 11:18:53 +08:00
|
|
|
S > 1
|
|
|
|
? "`--perfscript`, `--perfdata` and `--unsymbolized-profile` "
|
|
|
|
"cannot be used together."
|
|
|
|
: "Perf input file is missing, please use one of `--perfscript`, "
|
|
|
|
"`--perfdata` and `--unsymbolized-profile` for the input.";
|
2021-09-30 00:04:44 +08:00
|
|
|
exitWithError(Msg);
|
|
|
|
}
|
|
|
|
|
2021-10-15 11:18:53 +08:00
|
|
|
auto CheckFileExists = [](bool H, StringRef File) {
|
|
|
|
if (H && !llvm::sys::fs::exists(File)) {
|
|
|
|
std::string Msg = "Input perf file(" + File.str() + ") doesn't exist.";
|
|
|
|
exitWithError(Msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CheckFileExists(HasPerfData, PerfDataFilename);
|
|
|
|
CheckFileExists(HasPerfScript, PerfScriptFilename);
|
|
|
|
CheckFileExists(HasUnsymbolizedProfile, UnsymbolizedProfFilename);
|
2021-08-18 06:53:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!llvm::sys::fs::exists(BinaryPath)) {
|
2021-09-30 00:04:44 +08:00
|
|
|
std::string Msg = "Input binary(" + BinaryPath + ") doesn't exist.";
|
2021-08-18 06:53:31 +08:00
|
|
|
exitWithError(Msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CSProfileGenerator::MaxCompressionSize < -1) {
|
|
|
|
exitWithError("Value of --compress-recursion should >= -1");
|
|
|
|
}
|
|
|
|
if (ShowSourceLocations && !ShowDisassemblyOnly) {
|
|
|
|
exitWithError("--show-source-locations should work together with "
|
|
|
|
"--show-disassembly-only!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 11:18:53 +08:00
|
|
|
static PerfInputFile getPerfInputFile() {
|
|
|
|
PerfInputFile File;
|
|
|
|
if (PerfDataFilename.getNumOccurrences()) {
|
|
|
|
File.InputFile = PerfDataFilename;
|
|
|
|
File.Format = PerfFormat::PerfData;
|
|
|
|
} else if (PerfScriptFilename.getNumOccurrences()) {
|
|
|
|
File.InputFile = PerfScriptFilename;
|
|
|
|
File.Format = PerfFormat::PerfScript;
|
|
|
|
} else if (UnsymbolizedProfFilename.getNumOccurrences()) {
|
|
|
|
File.InputFile = UnsymbolizedProfFilename;
|
|
|
|
File.Format = PerfFormat::UnsymbolizedProfile;
|
|
|
|
}
|
|
|
|
return File;
|
|
|
|
}
|
|
|
|
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
int main(int argc, const char *argv[]) {
|
|
|
|
InitLLVM X(argc, argv);
|
|
|
|
|
2020-10-20 01:02:05 +08:00
|
|
|
// Initialize targets and assembly printers/parsers.
|
|
|
|
InitializeAllTargetInfos();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
InitializeAllDisassemblers();
|
|
|
|
|
2021-07-20 23:10:34 +08:00
|
|
|
cl::HideUnrelatedOptions({&ProfGenCategory, &getColorCategory()});
|
2020-12-22 05:29:56 +08:00
|
|
|
cl::ParseCommandLineOptions(argc, argv, "llvm SPGO profile generator\n");
|
2021-09-30 00:04:44 +08:00
|
|
|
validateCommandLine();
|
2020-12-22 05:29:56 +08:00
|
|
|
|
2021-08-18 06:53:31 +08:00
|
|
|
// Load symbols and disassemble the code of a given binary.
|
|
|
|
std::unique_ptr<ProfiledBinary> Binary =
|
2022-01-25 08:55:05 +08:00
|
|
|
std::make_unique<ProfiledBinary>(BinaryPath, DebugBinPath);
|
2021-08-18 06:53:31 +08:00
|
|
|
if (ShowDisassemblyOnly)
|
2021-02-10 08:41:44 +08:00
|
|
|
return EXIT_SUCCESS;
|
2021-07-29 06:07:40 +08:00
|
|
|
|
2021-10-15 11:18:53 +08:00
|
|
|
PerfInputFile PerfFile = getPerfInputFile();
|
2021-07-29 06:07:40 +08:00
|
|
|
std::unique_ptr<PerfReaderBase> Reader =
|
2021-10-15 11:18:53 +08:00
|
|
|
PerfReaderBase::create(Binary.get(), PerfFile);
|
|
|
|
// Parse perf events and samples
|
2021-09-30 00:04:44 +08:00
|
|
|
Reader->parsePerfTraces();
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
|
2021-09-01 04:27:42 +08:00
|
|
|
if (SkipSymbolization)
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
2021-09-23 11:00:24 +08:00
|
|
|
std::unique_ptr<ProfileGeneratorBase> Generator =
|
|
|
|
ProfileGeneratorBase::create(Binary.get(), Reader->getSampleCounters(),
|
2021-12-15 02:03:05 +08:00
|
|
|
Reader->profileIsCSFlat());
|
[CSSPGO][llvm-profgen] Context-sensitive profile data generation
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
This change supports context-sensitive profile data generation into llvm-profgen. With simultaneous sampling for LBR and call stack, we can identify leaf of LBR sample with calling context from stack sample . During the process of deriving fall through path from LBR entries, we unwind LBR by replaying all the calls and returns (including implicit calls/returns due to inlining) backwards on top of the sampled call stack. Then the state of call stack as we unwind through LBR always represents the calling context of current fall through path.
we have two types of virtual unwinding 1) LBR unwinding and 2) linear range unwinding.
Specifically, for each LBR entry which can be classified into call, return, regular branch, LBR unwinding will replay the operation by pushing, popping or switching leaf frame towards the call stack and since the initial call stack is most recently sampled, the replay should be in anti-execution order, i.e. for the regular case, pop the call stack when LBR is call, push frame on call stack when LBR is return. After each LBR processed, it also needs to align with the next LBR by going through instructions from previous LBR's target to current LBR's source, which we named linear unwinding. As instruction from linear range can come from different function by inlining, linear unwinding will do the range splitting and record counters through the range with same inline context.
With each fall through path from LBR unwinding, we aggregate each sample into counters by the calling context and eventually generate full context sensitive profile (without relying on inlining) to driver compiler's PGO/FDO.
A breakdown of noteworthy changes:
- Added `HybridSample` class as the abstraction perf sample including LBR stack and call stack
* Extended `PerfReader` to implement auto-detect whether input perf script output contains CS profile, then do the parsing. Multiple `HybridSample` are extracted
* Speed up by aggregating `HybridSample` into `AggregatedSamples`
* Added VirtualUnwinder that consumes aggregated `HybridSample` and implements unwinding of calls, returns, and linear path that contains implicit call/return from inlining. Ranges and branches counters are aggregated by the calling context.
Here calling context is string type, each context is a pair of function name and callsite location info, the whole context is like `main:1 @ foo:2 @ bar`.
* Added PorfileGenerater that accumulates counters by ranges unfolding or branch target mapping, then generates context-sensitive function profile including function body, inferring callee's head sample, callsite target samples, eventually records into ProfileMap.
* Leveraged LLVM build-in(`SampleProfWriter`) writer to support different serialization format with no stop
- `getCanonicalFnName` for callee name and name from ELF section
- Added regression test for both unwinding and profile generation
Test Plan:
ninja & ninja check-llvm
Reviewed By: hoy, wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89723
2020-10-20 03:55:59 +08:00
|
|
|
Generator->generateProfile();
|
|
|
|
Generator->write();
|
|
|
|
|
[CSSPGO][llvm-profgen] Parse mmap events from perf script
This stack of changes introduces `llvm-profgen` utility which generates a profile data file from given perf script data files for sample-based PGO. It’s part of(not only) the CSSPGO work. Specifically to support context-sensitive with/without pseudo probe profile, it implements a series of functionalities including perf trace parsing, instruction symbolization, LBR stack/call frame stack unwinding, pseudo probe decoding, etc. Also high throughput is achieved by multiple levels of sample aggregation and compatible format with one stop is generated at the end. Please refer to: https://groups.google.com/g/llvm-dev/c/1p1rdYbL93s for the CSSPGO RFC.
As a starter, this change sets up an entry point by introducing PerfReader to load profiled binaries and perf traces(including perf events and perf samples). For the event, here it parses the mmap2 events from perf script to build the loader snaps, which is used to retrieve the image load address in the subsequent perf tracing parsing.
As described in llvm-profgen.rst, the tool being built aims to support multiple input perf data (preprocessed by perf script) as well as multiple input binary images. It should also support dynamic reload/unload shared objects by leveraging the loader snaps being built by this change
Reviewed By: wenlei, wmi
Differential Revision: https://reviews.llvm.org/D89707
2020-10-19 12:36:54 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|