[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-18 21:36:54 -07:00
|
|
|
//===-- ProfiledBinary.h - Binary decoder -----------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TOOLS_LLVM_PROFGEN_PROFILEDBINARY_H
|
|
|
|
#define LLVM_TOOLS_LLVM_PROFGEN_PROFILEDBINARY_H
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2020-10-19 10:02:05 -07:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCDisassembler/MCDisassembler.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
|
|
|
#include "llvm/MC/MCInstrAnalysis.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
|
|
|
#include "llvm/MC/MCTargetOptions.h"
|
|
|
|
#include "llvm/Object/ELFObjectFile.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-18 21:36:54 -07:00
|
|
|
#include "llvm/Support/Path.h"
|
2020-10-19 10:02:05 -07:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm::object;
|
[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-18 21:36:54 -07:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace sampleprof {
|
|
|
|
|
|
|
|
class ProfiledBinary {
|
2020-10-19 10:02:05 -07:00
|
|
|
// Absolute path of the binary.
|
[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-18 21:36:54 -07:00
|
|
|
std::string Path;
|
2020-10-19 10:02:05 -07:00
|
|
|
// The target triple.
|
|
|
|
Triple TheTriple;
|
|
|
|
// The runtime base address that the executable sections are loaded at.
|
[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-18 21:36:54 -07:00
|
|
|
mutable uint64_t BaseAddress = 0;
|
2020-10-19 10:02:05 -07:00
|
|
|
// The preferred base address that the executable sections are loaded at.
|
|
|
|
uint64_t PreferredBaseAddress = 0;
|
|
|
|
// Mutiple MC component info
|
|
|
|
std::unique_ptr<const MCRegisterInfo> MRI;
|
|
|
|
std::unique_ptr<const MCAsmInfo> AsmInfo;
|
|
|
|
std::unique_ptr<const MCSubtargetInfo> STI;
|
|
|
|
std::unique_ptr<const MCInstrInfo> MII;
|
|
|
|
std::unique_ptr<MCDisassembler> DisAsm;
|
|
|
|
std::unique_ptr<const MCInstrAnalysis> MIA;
|
|
|
|
std::unique_ptr<MCInstPrinter> IP;
|
|
|
|
// A list of text sections sorted by start RVA and size. Used to check
|
|
|
|
// if a given RVA is a valid code address.
|
|
|
|
std::set<std::pair<uint64_t, uint64_t>> TextSections;
|
|
|
|
// Function offset to name mapping.
|
|
|
|
std::unordered_map<uint64_t, std::string> FuncStartAddrMap;
|
|
|
|
// An array of offsets of all instructions sorted in increasing order. The
|
|
|
|
// sorting is needed to fast advance to the next forward/backward instruction.
|
|
|
|
std::vector<uint64_t> CodeAddrs;
|
|
|
|
// A set of call instruction offsets. Used by virtual unwinding.
|
|
|
|
std::unordered_set<uint64_t> CallAddrs;
|
|
|
|
// A set of return instruction offsets. Used by virtual unwinding.
|
|
|
|
std::unordered_set<uint64_t> RetAddrs;
|
|
|
|
|
|
|
|
void setPreferredBaseAddress(const ELFObjectFileBase *O);
|
|
|
|
|
|
|
|
// Set up disassembler and related components.
|
|
|
|
void setUpDisassembler(const ELFObjectFileBase *Obj);
|
|
|
|
|
|
|
|
/// Dissassemble the text section and build various address maps.
|
|
|
|
void disassemble(const ELFObjectFileBase *O);
|
|
|
|
|
|
|
|
/// Helper function to dissassemble the symbol and extract info for unwinding
|
|
|
|
bool dissassembleSymbol(std::size_t SI, ArrayRef<uint8_t> Bytes,
|
|
|
|
SectionSymbolsTy &Symbols, const SectionRef &Section);
|
|
|
|
|
|
|
|
/// Decode the interesting parts of the binary and build internal data
|
|
|
|
/// structures. On high level, the parts of interest are:
|
|
|
|
/// 1. Text sections, including the main code section and the PLT
|
|
|
|
/// entries that will be used to handle cross-module call transitions.
|
|
|
|
/// 2. The .debug_line section, used by Dwarf-based profile generation.
|
|
|
|
/// 3. Pseudo probe related sections, used by probe-based profile
|
|
|
|
/// generation.
|
|
|
|
void load();
|
[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-18 21:36:54 -07:00
|
|
|
|
|
|
|
public:
|
|
|
|
ProfiledBinary(StringRef Path) : Path(Path) { load(); }
|
|
|
|
|
|
|
|
const StringRef getPath() const { return Path; }
|
|
|
|
const StringRef getName() const { return llvm::sys::path::filename(Path); }
|
|
|
|
uint64_t getBaseAddress() const { return BaseAddress; }
|
|
|
|
void setBaseAddress(uint64_t Address) { BaseAddress = Address; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace sampleprof
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|