Skip to content

Commit 52465e6

Browse files
remove comment
1 parent dab9c01 commit 52465e6

File tree

5 files changed

+90
-81
lines changed

5 files changed

+90
-81
lines changed

llvm/tools/llvm-profgen/DataAccessPerfReader.cpp

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,10 @@ void DataAccessPerfReader::parsePerfTraces() {
1515
parsePerfTrace(PerfTraceFilename);
1616
}
1717

18-
static void testPerfSampleRecordRegex() {
19-
std::regex logRegex(
20-
R"(^.*?PERF_RECORD_SAMPLE\(.*?\):\s*(\d+)\/(\d+):\s*(0x[0-9a-fA-F]+)\s+period:\s*\d+\s+addr:\s*(0x[0-9a-fA-F]+)$)");
21-
22-
std::smatch testMatch;
23-
const std::string testLine =
24-
"2193330181938979 0xa88 [0x48]: PERF_RECORD_SAMPLE(IP, 0x4002): "
25-
"1807344/1807344: 0x260b45 period: 100 addr: 0x200630";
26-
if (std::regex_search(testLine, testMatch, logRegex)) {
27-
if (testMatch.size() != 5) {
28-
exitWithError("Regex did not match expected number of groups.");
29-
}
30-
for (size_t i = 0; i < testMatch.size(); ++i) {
31-
errs() << "Group " << i << ": " << testMatch[i] << "\n";
32-
}
33-
// errs() << "Test line matched successfully.\n";
34-
} else {
35-
exitWithError("Test line did not match regex.");
36-
}
37-
}
38-
3918
// Ignore mmap events.
4019
void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
4120
std::regex logRegex(
4221
R"(^.*?PERF_RECORD_SAMPLE\(.*?\):\s*(\d+)\/(\d+):\s*(0x[0-9a-fA-F]+)\s+period:\s*\d+\s+addr:\s*(0x[0-9a-fA-F]+)$)");
43-
uint64_t UnmatchedLine = 0, MatchedLine = 0;
4422

4523
auto BufferOrErr = MemoryBuffer::getFile(PerfTrace);
4624
std::error_code EC = BufferOrErr.getError();
@@ -51,18 +29,23 @@ void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
5129
for (; !LineIt.is_at_eof(); ++LineIt) {
5230
StringRef Line = *LineIt;
5331

32+
// Parse MMAP event from perf trace.
5433
// Parse MMAP event from perf trace.
5534
// Construct a binary from the binary file path.
5635
PerfScriptReader::MMapEvent MMap;
5736
if (Line.contains("PERF_RECORD_MMAP2")) {
5837
if (PerfScriptReader::extractMMapEventForBinary(Binary, Line, MMap)) {
59-
errs() << "MMap event found: "
60-
<< "PID: " << MMap.PID
61-
<< ", Address: " << format("0x%llx", MMap.Address)
62-
<< ", Size: " << MMap.Size << ", Offset: " << MMap.Offset
63-
<< ", Binary Path: " << MMap.BinaryPath << "\n";
38+
// TODO: This is a hack to avoid mapping binary address for data section
39+
// mappings.
6440
if (MMap.Offset == 0) {
6541
updateBinaryAddress(MMap);
42+
errs() << "Binary base address is "
43+
<< format("0x%" PRIx64, Binary->getBaseAddress())
44+
<< " and preferred base address is "
45+
<< format("0x%" PRIx64, Binary->getPreferredBaseAddress())
46+
<< " and first loadable address is "
47+
<< format("0x%" PRIx64, Binary->getFirstLoadableAddress())
48+
<< "\n";
6649
}
6750
}
6851
continue;
@@ -72,14 +55,6 @@ void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
7255
// Skip lines that do not contain "PERF_RECORD_SAMPLE".
7356
continue;
7457
}
75-
// errs() << "Processing line: " << Line << "\n";
76-
77-
// if (IPSampleRegex.match(Line, &Matches)) {
78-
// errs() << "IP Captured: " << Matches.size() << "\n";
79-
// }
80-
// if (DataAddressRegex.match(Line, &Matches)) {
81-
// errs() << "Data Address Captured: " << Matches.size() << "\n";
82-
// }
8358

8459
std::smatch matches;
8560
const std::string LineStr = Line.str();
@@ -89,41 +64,29 @@ void DataAccessPerfReader::parsePerfTrace(StringRef PerfTrace) {
8964
continue;
9065

9166
uint64_t DataAddress = std::stoull(matches[4].str(), nullptr, 16);
92-
uint64_t IP = std::stoull(matches[3].str(), nullptr, 16);
67+
68+
// Skip addresses out of the specified PT_LOAD section for data.
69+
if (DataAddress < DataMMap.Address ||
70+
DataAddress >= DataMMap.Address + DataMMap.Size)
71+
continue;
72+
9373
int32_t PID = std::stoi(matches[1].str());
94-
// if (DataAddress == 0x200630) {
95-
// errs() << "Find data address at 0x200630, IP: " << format("0x%llx",
96-
// IP)
97-
// << " pid is " << PID << "\n";
98-
// }
99-
100-
// errs() << matches.size() << " matches found in line: " << LineStr <<
101-
// "\n"; for (const auto &Match : matches) {
102-
// errs() << "Match: " << Match.str() << "\n";
103-
// }
10474
// Check if the PID matches the filter.
10575

10676
if (PIDFilter && *PIDFilter != PID) {
10777
continue;
10878
}
10979

80+
uint64_t IP = std::stoull(matches[3].str(), nullptr, 16);
11081
// Extract the address and count.
111-
11282
uint64_t CanonicalDataAddress =
113-
Binary->canonicalizeVirtualAddress(DataAddress);
114-
// errs() << "Data address is " << format("0x" PRIx64 ":", DataAddress)
115-
// << " Canonical data address is "
116-
// << format("0x" PRIx64 ":", CanonicalDataAddress) << "\n";
117-
AddressToCount[CanonicalDataAddress] += 1;
118-
MatchedLine++;
119-
} else {
120-
// errs() << "\tNo match found for line: " << Line << "\n";
121-
UnmatchedLine++;
83+
canonicalizeDataAddress(DataAddress, *Binary, DataMMap, DataSegment);
84+
85+
uint64_t CanonicalIPAddress = Binary->canonicalizeVirtualAddress(IP);
86+
87+
AddressMap[CanonicalIPAddress][CanonicalDataAddress] += 1;
12288
}
12389
}
124-
125-
errs() << "Total unmatched lines: " << UnmatchedLine << "\t"
126-
<< "Matched lines: " << MatchedLine << "\n";
12790
}
12891

12992
} // namespace llvm

llvm/tools/llvm-profgen/DataAccessPerfReader.h

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,90 @@ namespace llvm {
1717

1818
class DataAccessPerfReader : public PerfScriptReader {
1919
public:
20+
class DataSegment {
21+
public:
22+
uint64_t FileOffset;
23+
uint64_t VirtualAddress;
24+
};
2025
DataAccessPerfReader(ProfiledBinary *Binary, StringRef PerfTrace,
2126
std::optional<int32_t> PID)
2227
: PerfScriptReader(Binary, PerfTrace, PID), PerfTraceFilename(PerfTrace) {
28+
hackMMapEventAndDataSegment(DataMMap, DataSegment, *Binary);
29+
}
30+
31+
// The MMapEvent is hard-coded as a hack to illustrate the change.
32+
static void
33+
hackMMapEventAndDataSegment(PerfScriptReader::MMapEvent &MMap,
34+
DataSegment &DataSegment,
35+
const ProfiledBinary &ProfiledBinary) {
36+
// The PERF_RECORD_MMAP2 event is
37+
// 0 0x4e8 [0xa0]: PERF_RECORD_MMAP2 1849842/1849842:
38+
// [0x55d977426000(0x1000) @ 0x1000 fd:01 20869534 0]: r--p /path/to/binary
39+
MMap.PID = 1849842; // Example PID
40+
MMap.BinaryPath = ProfiledBinary.getPath();
41+
MMap.Address = 0x55d977426000;
42+
MMap.Size = 0x1000;
43+
MMap.Offset = 0x1000; // File Offset in the binary.
44+
45+
// TODO: Set binary fields to do address canonicalization, and compute
46+
// static data address range.
47+
DataSegment.FileOffset =
48+
0x1180; // The byte offset of the segment start in the binary.
49+
DataSegment.VirtualAddress =
50+
0x3180; // The virtual address of the segment start in the binary.
51+
}
52+
53+
uint64_t canonicalizeDataAddress(uint64_t Address,
54+
const ProfiledBinary &ProfiledBinary,
55+
const PerfScriptReader::MMapEvent &MMap,
56+
const DataSegment &DataSegment) {
57+
// virtual-addr = segment.virtual-addr (0x3180) + (runtime-addr -
58+
// map.adddress - segment.file-offset (0x1180) + map.file-offset (0x1000))
59+
return DataSegment.VirtualAddress +
60+
(Address - MMap.Address - (DataSegment.FileOffset - MMap.Offset));
2361
}
2462

2563
// Entry of the reader to parse multiple perf traces
2664
void parsePerfTraces() override;
2765

28-
auto getAddressToCount() const {
29-
return AddressToCount.getArrayRef();
30-
}
66+
struct ProfiledInfo {
67+
ProfiledInfo(uint64_t InstructionAddr, uint64_t DataAddr, uint64_t Count)
68+
: InstructionAddr(InstructionAddr), DataAddr(DataAddr), Count(Count) {}
69+
uint64_t InstructionAddr;
70+
uint64_t DataAddr;
71+
uint64_t Count;
72+
};
3173

74+
// A hack to demonstrate the symbolized output of vtable type profiling.
3275
void print() const {
33-
auto addrCountArray = AddressToCount.getArrayRef();
34-
std::vector<std::pair<uint64_t, uint64_t>> SortedEntries(
35-
addrCountArray.begin(), addrCountArray.end());
36-
llvm::sort(SortedEntries, [](const auto &A, const auto &B) {
37-
return A.second > B.second;
38-
});
39-
for (const auto &Entry : SortedEntries) {
40-
if (Entry.second == 0)
76+
77+
std::vector<ProfiledInfo> Entries;
78+
Entries.reserve(AddressMap.size());
79+
for (const auto &[IpAddr, DataCount] : AddressMap) {
80+
for (const auto [DataAddr, Count] : DataCount) {
81+
Entries.emplace_back(ProfiledInfo(IpAddr, DataAddr, Count));
82+
}
83+
}
84+
llvm::sort(Entries,
85+
[](const auto &A, const auto &B) { return A.Count > B.Count; });
86+
for (const auto &Entry : Entries) {
87+
if (Entry.Count == 0)
4188
continue; // Skip entries with zero count
42-
dbgs() << "Address: " << format("0x%llx", Entry.first)
43-
<< ", Count: " << Entry.second << "\n";
89+
dbgs() << "Address: " << format("0x%llx", Entry.InstructionAddr)
90+
<< " Data Address: " << format("0x%llx", Entry.DataAddr)
91+
<< " Count: " << Entry.Count << "\n";
4492
}
4593
}
4694

4795
private:
4896
void parsePerfTrace(StringRef PerfTrace);
4997

50-
MapVector<uint64_t, uint64_t> AddressToCount;
98+
DenseMap<uint64_t, DenseMap<uint64_t, uint64_t>> AddressMap;
5199

52100
StringRef PerfTraceFilename;
101+
102+
PerfScriptReader::MMapEvent DataMMap;
103+
DataSegment DataSegment;
53104
};
54105

55106
} // namespace llvm

llvm/tools/llvm-profgen/ProfiledBinary.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,6 @@ void ProfiledBinary::setPreferredTextSegmentAddresses(const ELFFile<ELFT> &Obj,
341341
~(PageSize - 1U));
342342
TextSegmentOffsets.push_back(Phdr.p_offset & ~(PageSize - 1U));
343343
}
344-
// else if ((Phdr.p_flags & ELF::PF_R) && !TextSegmentOffsets.empty()) {
345-
// if (RecordDataSegment) {
346-
// ReadOnlyDataSegmentOffsets.push_back(Phdr.p_offset &
347-
// ~(PageSize - 1U));
348-
// }
349-
// }
350344
}
351345
}
352346

llvm/tools/llvm-profgen/ProfiledBinary.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ class ProfiledBinary {
205205
// The file offset of each executable segment.
206206
std::vector<uint64_t> TextSegmentOffsets;
207207

208+
std::vector<uint64_t> ReadOnlyDataSegmentOffsets;
209+
208210
// Mutiple MC component info
209211
std::unique_ptr<const MCRegisterInfo> MRI;
210212
std::unique_ptr<const MCAsmInfo> AsmInfo;

llvm/tools/llvm-profgen/llvm-profgen.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ int main(int argc, const char *argv[]) {
206206
} else {
207207
assert(Binary.get() &&
208208
"Binary should be initialized for data access profile");
209-
errs() << "binary text segment offset is "
210-
<< format("0x%" PRIx64 ":", Binary->getTextSegmentOffset()) << "\n";
209+
211210
// data access profile.
212211
SmallVector<StringRef, 4> PerfTraces{PerfScriptFilename};
213212
auto Reader = std::make_unique<DataAccessPerfReader>(

0 commit comments

Comments
 (0)