LLVM 23.0.0git
JITLoaderPerf.cpp
Go to the documentation of this file.
1//===------- JITLoaderPerf.cpp - Register profiler objects ------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Register objects for access by profilers via the perf JIT interface.
10//
11//===----------------------------------------------------------------------===//
12
14
16
19#include "llvm/Support/Path.h"
22
23#include <mutex>
24#include <optional>
25
26#ifdef __linux__
27
28#include <sys/mman.h> // mmap()
29#include <time.h> // clock_gettime(), time(), localtime_r() */
30
31#define DEBUG_TYPE "orc"
32
33// language identifier (XXX: should we generate something better from debug
34// info?)
35#define JIT_LANG "llvm-IR"
36#define LLVM_PERF_JIT_MAGIC \
37 ((uint32_t)'J' << 24 | (uint32_t)'i' << 16 | (uint32_t)'T' << 8 | \
38 (uint32_t)'D')
39#define LLVM_PERF_JIT_VERSION 1
40
41using namespace llvm;
42using namespace llvm::orc;
43
44struct PerfState {
45 // cache lookups
46 uint32_t Pid;
47
48 // base directory for output data
49 std::string JitPath;
50
51 // output data stream, closed via Dumpstream
52 int DumpFd = -1;
53
54 // output data stream
55 std::unique_ptr<raw_fd_ostream> Dumpstream;
56
57 // perf mmap marker
58 void *MarkerAddr = nullptr;
59};
60
61// prevent concurrent dumps from messing up the output file
62static std::mutex Mutex;
63static std::optional<PerfState> State;
64
65struct RecHeader {
66 uint32_t Id;
67 uint32_t TotalSize;
68 uint64_t Timestamp;
69};
70
71struct DIR {
72 RecHeader Prefix;
73 uint64_t CodeAddr;
74 uint64_t NrEntry;
75};
76
77struct DIE {
78 uint64_t CodeAddr;
79 uint32_t Line;
80 uint32_t Discrim;
81};
82
83struct CLR {
84 RecHeader Prefix;
85 uint32_t Pid;
86 uint32_t Tid;
87 uint64_t Vma;
88 uint64_t CodeAddr;
89 uint64_t CodeSize;
90 uint64_t CodeIndex;
91};
92
93struct UWR {
94 RecHeader Prefix;
95 uint64_t UnwindDataSize;
96 uint64_t EhFrameHeaderSize;
97 uint64_t MappedSize;
98};
99
100static inline uint64_t timespec_to_ns(const struct timespec *TS) {
101 const uint64_t NanoSecPerSec = 1000000000;
102 return ((uint64_t)TS->tv_sec * NanoSecPerSec) + TS->tv_nsec;
103}
104
105static inline uint64_t perf_get_timestamp() {
106 timespec TS;
107 if (clock_gettime(CLOCK_MONOTONIC, &TS))
108 return 0;
109
110 return timespec_to_ns(&TS);
111}
112
113static void writeDebugRecord(const PerfJITDebugInfoRecord &DebugRecord) {
114 assert(State && "PerfState not initialized");
115 LLVM_DEBUG(dbgs() << "Writing debug record with "
116 << DebugRecord.Entries.size() << " entries\n");
117 [[maybe_unused]] size_t Written = 0;
118 DIR Dir{RecHeader{static_cast<uint32_t>(DebugRecord.Prefix.Id),
119 DebugRecord.Prefix.TotalSize, perf_get_timestamp()},
120 DebugRecord.CodeAddr, DebugRecord.Entries.size()};
121 State->Dumpstream->write(reinterpret_cast<const char *>(&Dir), sizeof(Dir));
122 Written += sizeof(Dir);
123 for (auto &Die : DebugRecord.Entries) {
124 DIE d{Die.Addr, Die.Lineno, Die.Discrim};
125 State->Dumpstream->write(reinterpret_cast<const char *>(&d), sizeof(d));
126 State->Dumpstream->write(Die.Name.data(), Die.Name.size() + 1);
127 Written += sizeof(d) + Die.Name.size() + 1;
128 }
129 LLVM_DEBUG(dbgs() << "wrote " << Written << " bytes of debug info\n");
130}
131
132static void writeCodeRecord(const PerfJITCodeLoadRecord &CodeRecord) {
133 assert(State && "PerfState not initialized");
134 uint32_t Tid = get_threadid();
135 LLVM_DEBUG(dbgs() << "Writing code record with code size "
136 << CodeRecord.CodeSize << " and code index "
137 << CodeRecord.CodeIndex << "\n");
138 CLR Clr{RecHeader{static_cast<uint32_t>(CodeRecord.Prefix.Id),
139 CodeRecord.Prefix.TotalSize, perf_get_timestamp()},
140 State->Pid,
141 Tid,
142 CodeRecord.Vma,
143 CodeRecord.CodeAddr,
144 CodeRecord.CodeSize,
145 CodeRecord.CodeIndex};
146 LLVM_DEBUG(dbgs() << "wrote " << sizeof(Clr) << " bytes of CLR, "
147 << CodeRecord.Name.size() + 1 << " bytes of name, "
148 << CodeRecord.CodeSize << " bytes of code\n");
149 State->Dumpstream->write(reinterpret_cast<const char *>(&Clr), sizeof(Clr));
150 State->Dumpstream->write(CodeRecord.Name.data(), CodeRecord.Name.size() + 1);
151 State->Dumpstream->write((const char *)CodeRecord.CodeAddr,
152 CodeRecord.CodeSize);
153}
154
155static void
156writeUnwindRecord(const PerfJITCodeUnwindingInfoRecord &UnwindRecord) {
157 assert(State && "PerfState not initialized");
158 LLVM_DEBUG(dbgs() << "Writing unwind record with unwind data size "
159 << UnwindRecord.UnwindDataSize
160 << " and EH frame header size "
161 << UnwindRecord.EHFrameHdrSize << " and mapped size "
162 << UnwindRecord.MappedSize << "\n");
163 UWR Uwr{RecHeader{static_cast<uint32_t>(UnwindRecord.Prefix.Id),
164 UnwindRecord.Prefix.TotalSize, perf_get_timestamp()},
165 UnwindRecord.UnwindDataSize, UnwindRecord.EHFrameHdrSize,
166 UnwindRecord.MappedSize};
167 LLVM_DEBUG(dbgs() << "wrote " << sizeof(Uwr) << " bytes of UWR, "
168 << UnwindRecord.EHFrameHdrSize
169 << " bytes of EH frame header, "
170 << UnwindRecord.UnwindDataSize - UnwindRecord.EHFrameHdrSize
171 << " bytes of EH frame\n");
172 State->Dumpstream->write(reinterpret_cast<const char *>(&Uwr), sizeof(Uwr));
173 if (UnwindRecord.EHFrameHdrAddr)
174 State->Dumpstream->write((const char *)UnwindRecord.EHFrameHdrAddr,
175 UnwindRecord.EHFrameHdrSize);
176 else
177 State->Dumpstream->write(UnwindRecord.EHFrameHdr.data(),
178 UnwindRecord.EHFrameHdrSize);
179 State->Dumpstream->write((const char *)UnwindRecord.EHFrameAddr,
180 UnwindRecord.UnwindDataSize -
181 UnwindRecord.EHFrameHdrSize);
182}
183
184static Error registerJITLoaderPerfImpl(const PerfJITRecordBatch &Batch) {
185 if (!State)
186 return make_error<StringError>("PerfState not initialized",
188
189 // Serialize the batch
190 std::lock_guard<std::mutex> Lock(Mutex);
191 if (Batch.UnwindingRecord.Prefix.TotalSize > 0)
192 writeUnwindRecord(Batch.UnwindingRecord);
193
194 for (const auto &DebugInfo : Batch.DebugInfoRecords)
195 writeDebugRecord(DebugInfo);
196
197 for (const auto &CodeLoad : Batch.CodeLoadRecords)
198 writeCodeRecord(CodeLoad);
199
200 State->Dumpstream->flush();
201
202 return Error::success();
203}
204
205struct Header {
206 uint32_t Magic; // characters "JiTD"
207 uint32_t Version; // header version
208 uint32_t TotalSize; // total size of header
209 uint32_t ElfMach; // elf mach target
210 uint32_t Pad1; // reserved
211 uint32_t Pid;
212 uint64_t Timestamp; // timestamp
213 uint64_t Flags; // flags
214};
215
216static Error OpenMarker(PerfState &State) {
217 // We mmap the jitdump to create an MMAP RECORD in perf.data file. The mmap
218 // is captured either live (perf record running when we mmap) or in deferred
219 // mode, via /proc/PID/maps. The MMAP record is used as a marker of a jitdump
220 // file for more meta data info about the jitted code. Perf report/annotate
221 // detect this special filename and process the jitdump file.
222 //
223 // Mapping must be PROT_EXEC to ensure it is captured by perf record
224 // even when not using -d option.
225 State.MarkerAddr =
226 ::mmap(NULL, sys::Process::getPageSizeEstimate(), PROT_READ | PROT_EXEC,
227 MAP_PRIVATE, State.DumpFd, 0);
228
229 if (State.MarkerAddr == MAP_FAILED)
230 return make_error<llvm::StringError>("could not mmap JIT marker",
232
233 return Error::success();
234}
235
236void CloseMarker(PerfState &State) {
237 if (!State.MarkerAddr)
238 return;
239
240 munmap(State.MarkerAddr, sys::Process::getPageSizeEstimate());
241 State.MarkerAddr = nullptr;
242}
243
244static Expected<Header> FillMachine(PerfState &State) {
245 Header Hdr = {};
246 Hdr.Magic = LLVM_PERF_JIT_MAGIC;
247 Hdr.Version = LLVM_PERF_JIT_VERSION;
248 Hdr.TotalSize = sizeof(Hdr);
249 Hdr.Pid = State.Pid;
250 Hdr.Timestamp = perf_get_timestamp();
251
252 char Id[16];
253 struct {
254 uint16_t e_type;
255 uint16_t e_machine;
256 } Info;
257
258 size_t RequiredMemory = sizeof(Id) + sizeof(Info);
259
261 MemoryBuffer::getFileSlice("/proc/self/exe", RequiredMemory, 0);
262
263 // This'll not guarantee that enough data was actually read from the
264 // underlying file. Instead the trailing part of the buffer would be
265 // zeroed. Given the ELF signature check below that seems ok though,
266 // it's unlikely that the file ends just after that, and the
267 // consequence would just be that perf wouldn't recognize the
268 // signature.
269 if (!MB)
270 return make_error<llvm::StringError>("could not open /proc/self/exe",
271 MB.getError());
272
273 memcpy(&Id, (*MB)->getBufferStart(), sizeof(Id));
274 memcpy(&Info, (*MB)->getBufferStart() + sizeof(Id), sizeof(Info));
275
276 // check ELF signature
277 if (Id[0] != 0x7f || Id[1] != 'E' || Id[2] != 'L' || Id[3] != 'F')
278 return make_error<llvm::StringError>("invalid ELF signature",
280
281 Hdr.ElfMach = Info.e_machine;
282
283 return Hdr;
284}
285
286static Error InitDebuggingDir(PerfState &State) {
287 time_t Time;
288 struct tm LocalTime;
289 char TimeBuffer[sizeof("YYYYMMDD")];
291
292 // search for location to dump data to
293 if (const char *BaseDir = getenv("JITDUMPDIR"))
294 Path.append(BaseDir);
295 else if (!sys::path::home_directory(Path))
296 Path = ".";
297
298 // create debug directory
299 Path += "/.debug/jit/";
300 if (auto EC = sys::fs::create_directories(Path)) {
301 std::string ErrStr;
302 raw_string_ostream ErrStream(ErrStr);
303 ErrStream << "could not create jit cache directory " << Path << ": "
304 << EC.message() << "\n";
305 return make_error<StringError>(std::move(ErrStr), inconvertibleErrorCode());
306 }
307
308 // create unique directory for dump data related to this process
309 time(&Time);
310 localtime_r(&Time, &LocalTime);
311 strftime(TimeBuffer, sizeof(TimeBuffer), "%Y%m%d", &LocalTime);
312 Path += JIT_LANG "-jit-";
313 Path += TimeBuffer;
314
315 SmallString<128> UniqueDebugDir;
316
318 if (auto EC = createUniqueDirectory(Path, UniqueDebugDir)) {
319 std::string ErrStr;
320 raw_string_ostream ErrStream(ErrStr);
321 ErrStream << "could not create unique jit cache directory "
322 << UniqueDebugDir << ": " << EC.message() << "\n";
323 return make_error<StringError>(std::move(ErrStr), inconvertibleErrorCode());
324 }
325
326 State.JitPath = std::string(UniqueDebugDir);
327
328 return Error::success();
329}
330
331static Error registerJITLoaderPerfStartImpl() {
332 PerfState Tentative;
333 Tentative.Pid = sys::Process::getProcessId();
334 // check if clock-source is supported
335 if (!perf_get_timestamp())
336 return make_error<StringError>("kernel does not support CLOCK_MONOTONIC",
338
339 if (auto Err = InitDebuggingDir(Tentative))
340 return Err;
341
342 std::string Filename;
343 raw_string_ostream FilenameBuf(Filename);
344 FilenameBuf << Tentative.JitPath << "/jit-" << Tentative.Pid << ".dump";
345
346 // Need to open ourselves, because we need to hand the FD to OpenMarker() and
347 // raw_fd_ostream doesn't expose the FD.
349 if (auto EC = openFileForReadWrite(Filename, Tentative.DumpFd,
351 std::string ErrStr;
352 raw_string_ostream ErrStream(ErrStr);
353 ErrStream << "could not open JIT dump file " << Filename << ": "
354 << EC.message() << "\n";
355 return make_error<StringError>(std::move(ErrStr), inconvertibleErrorCode());
356 }
357
358 Tentative.Dumpstream =
359 std::make_unique<raw_fd_ostream>(Tentative.DumpFd, true);
360
361 auto Header = FillMachine(Tentative);
362 if (!Header)
363 return Header.takeError();
364
365 // signal this process emits JIT information
366 if (auto Err = OpenMarker(Tentative))
367 return Err;
368
369 Tentative.Dumpstream->write(reinterpret_cast<const char *>(&Header.get()),
370 sizeof(*Header));
371
372 // Everything initialized, can do profiling now.
373 if (Tentative.Dumpstream->has_error())
374 return make_error<StringError>("could not write JIT dump header",
376
377 State = std::move(Tentative);
378 return Error::success();
379}
380
381static Error registerJITLoaderPerfEndImpl() {
382 if (!State)
383 return make_error<StringError>("PerfState not initialized",
385
386 RecHeader Close;
387 Close.Id = static_cast<uint32_t>(PerfJITRecordType::JIT_CODE_CLOSE);
388 Close.TotalSize = sizeof(Close);
389 Close.Timestamp = perf_get_timestamp();
390 State->Dumpstream->write(reinterpret_cast<const char *>(&Close),
391 sizeof(Close));
392 if (State->MarkerAddr)
393 CloseMarker(*State);
394
395 State.reset();
396 return Error::success();
397}
398
400llvm_orc_registerJITLoaderPerfImpl(const char *ArgData, size_t ArgSize) {
401 using namespace orc::shared;
402 return WrapperFunction<SPSError(SPSPerfJITRecordBatch)>::handle(
403 ArgData, ArgSize, registerJITLoaderPerfImpl)
404 .release();
405}
406
408llvm_orc_registerJITLoaderPerfStart(const char *ArgData, size_t ArgSize) {
409 using namespace orc::shared;
410 return WrapperFunction<SPSError()>::handle(ArgData, ArgSize,
411 registerJITLoaderPerfStartImpl)
412 .release();
413}
414
416llvm_orc_registerJITLoaderPerfEnd(const char *ArgData, size_t ArgSize) {
417 using namespace orc::shared;
418 return WrapperFunction<SPSError()>::handle(ArgData, ArgSize,
419 registerJITLoaderPerfEndImpl)
420 .release();
421}
422
423#else
424
425using namespace llvm;
426using namespace llvm::orc;
427
428static Error badOS() {
429 using namespace llvm;
431 "unsupported OS (perf support is only available on linux!)",
433}
434
435static Error badOSBatch(PerfJITRecordBatch &Batch) { return badOS(); }
436
438llvm_orc_registerJITLoaderPerfImpl(const char *ArgData, size_t ArgSize) {
439 using namespace shared;
440 return WrapperFunction<SPSError(SPSPerfJITRecordBatch)>::handle(
441 ArgData, ArgSize, badOSBatch)
442 .release();
443}
444
446llvm_orc_registerJITLoaderPerfStart(const char *ArgData, size_t ArgSize) {
447 using namespace shared;
448 return WrapperFunction<SPSError()>::handle(ArgData, ArgSize, badOS).release();
449}
450
452llvm_orc_registerJITLoaderPerfEnd(const char *ArgData, size_t ArgSize) {
453 using namespace shared;
454 return WrapperFunction<SPSError()>::handle(ArgData, ArgSize, badOS).release();
455}
456
457#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
llvm::orc::shared::CWrapperFunctionBuffer llvm_orc_registerJITLoaderPerfEnd(const char *ArgData, size_t ArgSize)
llvm::orc::shared::CWrapperFunctionBuffer llvm_orc_registerJITLoaderPerfImpl(const char *ArgData, size_t ArgSize)
static Error badOSBatch(PerfJITRecordBatch &Batch)
static Error badOS()
llvm::orc::shared::CWrapperFunctionBuffer llvm_orc_registerJITLoaderPerfStart(const char *ArgData, size_t ArgSize)
static constexpr StringLiteral Filename
#define JIT_LANG
#define LLVM_PERF_JIT_MAGIC
#define LLVM_PERF_JIT_VERSION
Provides a library for accessing information about this process and other processes on the operating ...
#define LLVM_DEBUG(...)
Definition Debug.h:114
A structured debug information entry.
Definition DIE.h:828
Represents either an error or a value T.
Definition ErrorOr.h:56
std::error_code getError() const
Definition ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Map a subrange of the specified file as a MemoryBuffer.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
A raw_ostream that writes to an std::string.
static LLVM_ABI Pid getProcessId()
Get the process's identifier.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition Process.h:62
constexpr uint16_t Magic
Definition SFrame.h:32
std::error_code openFileForReadWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp, OpenFlags Flags, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
@ CD_CreateNew
CD_CreateNew - When opening a file:
Definition FileSystem.h:754
std::error_code openFileForWrite(const Twine &Name, int &ResultFD, CreationDisposition Disp=CD_CreateAlways, OpenFlags Flags=OF_None, unsigned Mode=0666)
Opens the file with the given name in a write-only or read-write mode, returning its open file descri...
LLVM_ABI std::error_code create_directories(const Twine &path, bool IgnoreExisting=true, perms Perms=owner_all|group_all)
Create all the non-existent directories in path.
Definition Path.cpp:983
LLVM_ABI std::error_code createUniqueDirectory(const Twine &Prefix, SmallVectorImpl< char > &ResultPath)
Definition Path.cpp:948
LLVM_ABI bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition Mutex.h:66
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:334
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
Definition Threading.cpp:33
std::vector< PerfJITDebugEntry > Entries
std::vector< PerfJITDebugInfoRecord > DebugInfoRecords
PerfJITCodeUnwindingInfoRecord UnwindingRecord
std::vector< PerfJITCodeLoadRecord > CodeLoadRecords