Skip to content

Commit e50a388

Browse files
committed
[profile] Add binary id into profiles
This patch adds binary id into profiles to easily associate binaries with the corresponding profiles. There is an RFC that discusses the motivation, design and implementation in more detail: https://lists.llvm.org/pipermail/llvm-dev/2021-June/151154.html Differential Revision: https://reviews.llvm.org/D102039
1 parent 120b187 commit e50a388

23 files changed

+256
-24
lines changed

compiler-rt/include/profile/InstrProfData.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
137137
INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
138138
INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
139139
INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
140+
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
140141
#undef INSTR_PROF_RAW_HEADER
141142
/* INSTR_PROF_RAW_HEADER end */
142143

@@ -645,7 +646,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
645646
(uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
646647

647648
/* Raw profile format version (start from 1). */
648-
#define INSTR_PROF_RAW_VERSION 5
649+
#define INSTR_PROF_RAW_VERSION 6
649650
/* Indexed profile format version (start from 1). */
650651
#define INSTR_PROF_INDEX_VERSION 7
651652
/* Coverage mapping format version (start from 0). */

compiler-rt/lib/profile/InstrProfilingInternal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,10 @@ COMPILER_RT_VISIBILITY extern ValueProfNode *CurrentVNode;
193193
COMPILER_RT_VISIBILITY extern ValueProfNode *EndVNode;
194194
extern void (*VPMergeHook)(struct ValueProfData *, __llvm_profile_data *);
195195

196+
/*
197+
* Write binary ids into profiles if writer is given.
198+
* Return -1 if an error occurs, otherwise, return total size of binary ids.
199+
*/
200+
int __llvm_write_binary_ids(ProfDataWriter *Writer);
201+
196202
#endif

compiler-rt/lib/profile/InstrProfilingPlatformDarwin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// with freestanding compilation. See `darwin_add_builtin_libraries`.
1111

1212
#include "InstrProfiling.h"
13+
#include "InstrProfilingInternal.h"
1314

1415
#if defined(__APPLE__)
1516
/* Use linker magic to find the bounds of the Data section. */
@@ -67,4 +68,9 @@ ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
6768

6869
COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &VNodesStart;
6970
COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &VNodesEnd;
71+
72+
COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
73+
return 0;
74+
}
75+
7076
#endif

compiler-rt/lib/profile/InstrProfilingPlatformLinux.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#if defined(__linux__) || defined(__FreeBSD__) || defined(__Fuchsia__) || \
1010
(defined(__sun__) && defined(__svr4__)) || defined(__NetBSD__)
1111

12+
#include <elf.h>
13+
#include <link.h>
1214
#include <stdlib.h>
15+
#include <string.h>
1316

1417
#include "InstrProfiling.h"
18+
#include "InstrProfilingInternal.h"
1519

1620
#define PROF_DATA_START INSTR_PROF_SECT_START(INSTR_PROF_DATA_COMMON)
1721
#define PROF_DATA_STOP INSTR_PROF_SECT_STOP(INSTR_PROF_DATA_COMMON)
@@ -72,4 +76,108 @@ COMPILER_RT_VISIBILITY ValueProfNode *__llvm_profile_end_vnodes(void) {
7276
COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = &PROF_VNODES_START;
7377
COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = &PROF_VNODES_STOP;
7478

79+
static size_t RoundUp(size_t size, size_t align) {
80+
return (size + align - 1) & ~(align - 1);
81+
}
82+
83+
/*
84+
* Write binary id length and then its data, because binary id does not
85+
* have a fixed length.
86+
*/
87+
int WriteOneBinaryId(ProfDataWriter *Writer, uint64_t BinaryIdLen,
88+
const uint8_t *BinaryIdData) {
89+
ProfDataIOVec BinaryIdIOVec[] = {
90+
{&BinaryIdLen, sizeof(uint64_t), 1, 0},
91+
{BinaryIdData, sizeof(uint8_t), BinaryIdLen, 0}};
92+
if (Writer->Write(Writer, BinaryIdIOVec,
93+
sizeof(BinaryIdIOVec) / sizeof(*BinaryIdIOVec)))
94+
return -1;
95+
96+
/* Successfully wrote binary id, report success. */
97+
return 0;
98+
}
99+
100+
/*
101+
* Look for the note that has the name "GNU\0" and type NT_GNU_BUILD_ID
102+
* that contains build id. If build id exists, write binary id.
103+
*
104+
* Each note in notes section starts with a struct which includes
105+
* n_namesz, n_descsz, and n_type members. It is followed by the name
106+
* (whose length is defined in n_namesz) and then by the descriptor
107+
* (whose length is defined in n_descsz).
108+
*
109+
* Note sections like .note.ABI-tag and .note.gnu.build-id are aligned
110+
* to 4 bytes, so round n_namesz and n_descsz to the nearest 4 bytes.
111+
*/
112+
int WriteBinaryIdForNote(ProfDataWriter *Writer, const ElfW(Nhdr) * Note) {
113+
int BinaryIdSize = 0;
114+
115+
const char *NoteName = (const char *)Note + sizeof(ElfW(Nhdr));
116+
if (Note->n_type == NT_GNU_BUILD_ID && Note->n_namesz == 4 &&
117+
memcmp(NoteName, "GNU\0", 4) == 0) {
118+
119+
uint64_t BinaryIdLen = Note->n_descsz;
120+
const uint8_t *BinaryIdData =
121+
(const uint8_t *)(NoteName + RoundUp(Note->n_namesz, 4));
122+
if (Writer != NULL &&
123+
WriteOneBinaryId(Writer, BinaryIdLen, BinaryIdData) == -1)
124+
return -1;
125+
126+
BinaryIdSize = sizeof(BinaryIdLen) + BinaryIdLen;
127+
}
128+
129+
return BinaryIdSize;
130+
}
131+
132+
/*
133+
* Helper function that iterates through notes section and find build ids.
134+
* If writer is given, write binary ids into profiles.
135+
* If an error happens while writing, return -1.
136+
*/
137+
int WriteBinaryIds(ProfDataWriter *Writer, const ElfW(Nhdr) * Note,
138+
const ElfW(Nhdr) * NotesEnd) {
139+
int TotalBinaryIdsSize = 0;
140+
while (Note < NotesEnd) {
141+
int Result = WriteBinaryIdForNote(Writer, Note);
142+
if (Result == -1)
143+
return -1;
144+
TotalBinaryIdsSize += Result;
145+
146+
/* Calculate the offset of the next note in notes section. */
147+
size_t NoteOffset = sizeof(ElfW(Nhdr)) + RoundUp(Note->n_namesz, 4) +
148+
RoundUp(Note->n_descsz, 4);
149+
Note = (const ElfW(Nhdr) *)((const char *)(Note) + NoteOffset);
150+
}
151+
152+
return TotalBinaryIdsSize;
153+
}
154+
155+
/*
156+
* Write binary ids into profiles if writer is given.
157+
* Return the total size of binary ids.
158+
* If an error happens while writing, return -1.
159+
*/
160+
COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
161+
extern const ElfW(Ehdr) __ehdr_start __attribute__((visibility("hidden")));
162+
const ElfW(Ehdr) *ElfHeader = &__ehdr_start;
163+
const ElfW(Phdr) *ProgramHeader =
164+
(const ElfW(Phdr) *)((uintptr_t)ElfHeader + ElfHeader->e_phoff);
165+
166+
uint32_t I;
167+
/* Iterate through entries in the program header. */
168+
for (I = 0; I < ElfHeader->e_phnum; I++) {
169+
/* Look for the notes section in program header entries. */
170+
if (ProgramHeader[I].p_type != PT_NOTE)
171+
continue;
172+
173+
const ElfW(Nhdr) *Note =
174+
(const ElfW(Nhdr) *)((uintptr_t)ElfHeader + ProgramHeader[I].p_offset);
175+
const ElfW(Nhdr) *NotesEnd =
176+
(const ElfW(Nhdr) *)((const char *)(Note) + ProgramHeader[I].p_filesz);
177+
return WriteBinaryIds(Writer, Note, NotesEnd);
178+
}
179+
180+
return 0;
181+
}
182+
75183
#endif

compiler-rt/lib/profile/InstrProfilingPlatformOther.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <stdio.h>
1515

1616
#include "InstrProfiling.h"
17+
#include "InstrProfilingInternal.h"
1718

1819
static const __llvm_profile_data *DataFirst = NULL;
1920
static const __llvm_profile_data *DataLast = NULL;
@@ -97,4 +98,8 @@ ValueProfNode *__llvm_profile_end_vnodes(void) { return 0; }
9798
COMPILER_RT_VISIBILITY ValueProfNode *CurrentVNode = 0;
9899
COMPILER_RT_VISIBILITY ValueProfNode *EndVNode = 0;
99100

101+
COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
102+
return 0;
103+
}
104+
100105
#endif

compiler-rt/lib/profile/InstrProfilingPlatformWindows.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
\*===----------------------------------------------------------------------===*/
88

99
#include "InstrProfiling.h"
10+
#include "InstrProfilingInternal.h"
1011

1112
#if defined(_WIN32)
1213

@@ -65,4 +66,8 @@ ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
6566
ValueProfNode *CurrentVNode = &VNodesStart + 1;
6667
ValueProfNode *EndVNode = &VNodesEnd;
6768

69+
COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
70+
return 0;
71+
}
72+
6873
#endif

compiler-rt/lib/profile/InstrProfilingWriter.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,24 @@ lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
283283
#define INSTR_PROF_RAW_HEADER(Type, Name, Init) Header.Name = Init;
284284
#include "profile/InstrProfData.inc"
285285

286-
/* Write the data. */
287-
ProfDataIOVec IOVec[] = {
288-
{&Header, sizeof(__llvm_profile_header), 1, 0},
286+
/* Write the profile header. */
287+
ProfDataIOVec IOVec[] = {{&Header, sizeof(__llvm_profile_header), 1, 0}};
288+
if (Writer->Write(Writer, IOVec, sizeof(IOVec) / sizeof(*IOVec)))
289+
return -1;
290+
291+
/* Write the binary id lengths and data. */
292+
if (__llvm_write_binary_ids(Writer) == -1)
293+
return -1;
294+
295+
/* Write the profile data. */
296+
ProfDataIOVec IOVecData[] = {
289297
{DataBegin, sizeof(__llvm_profile_data), DataSize, 0},
290298
{NULL, sizeof(uint8_t), PaddingBytesBeforeCounters, 1},
291299
{CountersBegin, sizeof(uint64_t), CountersSize, 0},
292300
{NULL, sizeof(uint8_t), PaddingBytesAfterCounters, 1},
293301
{SkipNameDataWrite ? NULL : NamesBegin, sizeof(uint8_t), NamesSize, 0},
294302
{NULL, sizeof(uint8_t), PaddingBytesAfterNames, 1}};
295-
if (Writer->Write(Writer, IOVec, sizeof(IOVec) / sizeof(*IOVec)))
303+
if (Writer->Write(Writer, IOVecData, sizeof(IOVecData) / sizeof(*IOVecData)))
296304
return -1;
297305

298306
/* Value profiling is not yet supported in continuous mode. */
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// REQUIRES: linux
2+
// RUN: %clang_profgen -Wl,--build-id=none -O2 -o %t %s
3+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
4+
// RUN: llvm-profdata show --binary-ids %t.profraw > %t.out
5+
// RUN: FileCheck %s --check-prefix=NO-BINARY-ID < %t.out
6+
// RUN: llvm-profdata merge -o %t.profdata %t.profraw
7+
8+
// RUN: %clang_profgen -Wl,--build-id -O2 -o %t %s
9+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
10+
// RUN: llvm-profdata show --binary-ids %t.profraw > %t.profraw.out
11+
// RUN: FileCheck %s --check-prefix=BINARY-ID-RAW-PROF < %t.profraw.out
12+
13+
void foo() {
14+
}
15+
16+
void bar() {
17+
}
18+
19+
int main() {
20+
foo();
21+
bar();
22+
return 0;
23+
}
24+
25+
// NO-BINARY-ID: Instrumentation level: Front-end
26+
// NO-BINARY-ID-NEXT: Total functions: 3
27+
// NO-BINARY-ID-NEXT: Maximum function count: 1
28+
// NO-BINARY-ID-NEXT: Maximum internal block count: 0
29+
// NO-BINARY-ID-NOT: Binary IDs:
30+
31+
// BINARY-ID-RAW-PROF: Instrumentation level: Front-end
32+
// BINARY-ID-RAW-PROF-NEXT: Total functions: 3
33+
// BINARY-ID-RAW-PROF-NEXT: Maximum function count: 1
34+
// BINARY-ID-RAW-PROF-NEXT: Maximum internal block count: 0
35+
// BINARY-ID-RAW-PROF-NEXT: Binary IDs:
36+
// BINARY-ID-RAW-PROF-NEXT: {{[0-9a-f]+}}

compiler-rt/test/profile/Linux/corrupted-profile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ int main(int argc, char** argv) {
4343
bail("mmap");
4444

4545
// We're trying to make the first CounterPtr invalid.
46-
// 10 64-bit words as header.
46+
// 11 64-bit words as header.
4747
// CounterPtr is the third 64-bit word field.
48-
memset(&Buf[10 * 8 + 2 * 8], 0xAB, 8);
48+
memset(&Buf[11 * 8 + 2 * 8], 0xAB, 8);
4949

5050
if (munmap(Buf, FileSize))
5151
bail("munmap");

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,7 @@ namespace RawInstrProf {
11031103
// raw header.
11041104
// Version 5: Bit 60 of FuncHash is reserved for the flag for the context
11051105
// sensitive records.
1106+
// Version 6: Added binary id.
11061107
const uint64_t Version = INSTR_PROF_RAW_VERSION;
11071108

11081109
template <class IntPtrT> inline uint64_t getMagic();

llvm/include/llvm/ProfileData/InstrProfData.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
137137
INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta, (uintptr_t)CountersBegin)
138138
INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
139139
INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
140+
INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
140141
#undef INSTR_PROF_RAW_HEADER
141142
/* INSTR_PROF_RAW_HEADER end */
142143

@@ -645,7 +646,7 @@ serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
645646
(uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
646647

647648
/* Raw profile format version (start from 1). */
648-
#define INSTR_PROF_RAW_VERSION 5
649+
#define INSTR_PROF_RAW_VERSION 6
649650
/* Indexed profile format version (start from 1). */
650651
#define INSTR_PROF_INDEX_VERSION 7
651652
/* Coverage mapping format version (start from 0). */

llvm/include/llvm/ProfileData/InstrProfReader.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class InstrProfReader {
8282
/// Read a single record.
8383
virtual Error readNextRecord(NamedInstrProfRecord &Record) = 0;
8484

85+
/// Print binary ids on stream OS.
86+
virtual Error printBinaryIds(raw_ostream &OS) { return success(); };
87+
8588
/// Iterator over profile data.
8689
InstrProfIterator begin() { return InstrProfIterator(this); }
8790
InstrProfIterator end() { return InstrProfIterator(); }
@@ -222,6 +225,9 @@ class RawInstrProfReader : public InstrProfReader {
222225
uint32_t ValueKindLast;
223226
uint32_t CurValueDataSize;
224227

228+
uint64_t BinaryIdsSize;
229+
const uint8_t *BinaryIdsStart;
230+
225231
public:
226232
RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
227233
: DataBuffer(std::move(DataBuffer)) {}
@@ -231,6 +237,7 @@ class RawInstrProfReader : public InstrProfReader {
231237
static bool hasFormat(const MemoryBuffer &DataBuffer);
232238
Error readHeader() override;
233239
Error readNextRecord(NamedInstrProfRecord &Record) override;
240+
Error printBinaryIds(raw_ostream &OS) override;
234241

235242
bool isIRLevelProfile() const override {
236243
return (Version & VARIANT_MASK_IR_PROF) != 0;

0 commit comments

Comments
 (0)