-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathperfjitdump.cpp
More file actions
465 lines (368 loc) · 11 KB
/
perfjitdump.cpp
File metadata and controls
465 lines (368 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// ===========================================================================
#include "pal/palinternal.h"
#include "pal/dbgmsg.h"
#include <cstddef>
#if defined(__linux__) || defined(__APPLE__)
#define JITDUMP_SUPPORTED
#endif
#ifdef JITDUMP_SUPPORTED
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#include "minipal/time.h"
#include "../inc/llvm/ELF.h"
#if defined(HOST_AMD64)
#include <x86intrin.h>
#endif
SET_DEFAULT_DEBUG_CHANNEL(MISC);
namespace
{
enum
{
JIT_DUMP_MAGIC = 0x4A695444,
JIT_DUMP_VERSION = 1,
JITDUMP_FLAGS_ARCH_TIMESTAMP = 1 << 0,
#if defined(HOST_X86)
ELF_MACHINE = EM_386,
#elif defined(HOST_ARM)
ELF_MACHINE = EM_ARM,
#elif defined(HOST_AMD64)
ELF_MACHINE = EM_X86_64,
#elif defined(HOST_ARM64)
ELF_MACHINE = EM_AARCH64,
#elif defined(HOST_LOONGARCH64)
ELF_MACHINE = EM_LOONGARCH,
#elif defined(HOST_RISCV64)
ELF_MACHINE = EM_RISCV,
#elif defined(HOST_S390X)
ELF_MACHINE = EM_S390,
#elif defined(HOST_POWERPC64)
ELF_MACHINE = EM_PPC64,
#else
#error ELF_MACHINE unsupported for target
#endif
JIT_CODE_LOAD = 0,
JIT_CODE_DEBUG_INFO = 2,
};
static bool UseArchTimeStamp()
{
static bool initialized = false;
static bool useArchTimestamp = false;
if (!initialized)
{
#if defined(HOST_AMD64)
const char* archTimestamp = getenv("JITDUMP_USE_ARCH_TIMESTAMP");
useArchTimestamp = (archTimestamp != nullptr && strcmp(archTimestamp, "1") == 0);
#endif
initialized = true;
}
return useArchTimestamp;
}
static uint64_t GetTimeStampNS()
{
#if defined(HOST_AMD64)
if (UseArchTimeStamp()) {
return static_cast<uint64_t>(__rdtsc());
}
#endif
return (uint64_t)minipal_hires_ticks();
}
struct FileHeader
{
FileHeader() :
magic(JIT_DUMP_MAGIC),
version(JIT_DUMP_VERSION),
total_size(sizeof(FileHeader)),
elf_mach(ELF_MACHINE),
pad1(0),
pid(getpid()),
timestamp(GetTimeStampNS()),
flags(UseArchTimeStamp() ? JITDUMP_FLAGS_ARCH_TIMESTAMP : 0)
{}
uint32_t magic;
uint32_t version;
uint32_t total_size;
uint32_t elf_mach;
uint32_t pad1;
uint32_t pid;
uint64_t timestamp;
uint64_t flags;
};
struct RecordHeader
{
uint32_t id;
uint32_t total_size;
uint64_t timestamp;
};
struct JitCodeLoadRecord
{
JitCodeLoadRecord() :
pid(getpid()),
tid((uint32_t)THREADSilentGetCurrentThreadId())
{
header.id = JIT_CODE_LOAD;
header.timestamp = GetTimeStampNS();
}
RecordHeader header;
uint32_t pid;
uint32_t tid;
uint64_t vma;
uint64_t code_addr;
uint64_t code_size;
uint64_t code_index;
// Null terminated name
// Optional native code
};
struct JitCodeDebugInfoRecord
{
JitCodeDebugInfoRecord()
{
header.id = JIT_CODE_DEBUG_INFO;
header.timestamp = GetTimeStampNS();
}
RecordHeader header;
};
};
struct PerfJitDumpState
{
PerfJitDumpState() :
enabled(false),
fd(-1),
mmapAddr(MAP_FAILED),
codeIndex(0)
{}
volatile bool enabled;
int fd;
void *mmapAddr;
volatile uint64_t codeIndex;
int FatalError()
{
enabled = false;
if (mmapAddr != MAP_FAILED)
{
munmap(mmapAddr, sizeof(FileHeader));
mmapAddr = MAP_FAILED;
}
if (fd != -1)
{
close(fd);
fd = -1;
}
return -1;
}
int Start(const char* path)
{
int result = 0;
// On platforms where JITDUMP is used, minipal_hires_tick_frequency()
// returns tccSecondsToNanoSeconds. If this isn't true,
// then some other method will need to be used to implement GetTimeStampNS.
// Validate this is true once in Start here.
if (minipal_hires_tick_frequency() != tccSecondsToNanoSeconds)
{
_ASSERTE(!"minipal_hires_tick_frequency() does not return tccSecondsToNanoSeconds. Implement JITDUMP GetTimeStampNS directly for this platform.\n");
FatalError();
}
// Write file header
FileHeader header;
if (result != 0)
return FatalError();
if (enabled)
goto exit;
char jitdumpPath[PATH_MAX];
result = snprintf(jitdumpPath, sizeof(jitdumpPath), "%s/jit-%i.dump", path, getpid());
if (result >= PATH_MAX)
return FatalError();
result = open(jitdumpPath, O_CREAT|O_TRUNC|O_RDWR|O_CLOEXEC, S_IRUSR|S_IWUSR );
if (result == -1)
return FatalError();
fd = result;
result = write(fd, &header, sizeof(FileHeader));
if (result == -1)
return FatalError();
result = fsync(fd);
if (result == -1)
return FatalError();
#if !defined(__APPLE__)
// mmap jitdump file
// this is a marker for perf inject to find the jitdumpfile on linux.
// On OSX, samply and others hook open and mmap is not needed. It also fails on OSX,
// likely because of PROT_EXEC and hardened runtime
mmapAddr = mmap(nullptr, sizeof(FileHeader), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0);
if (mmapAddr == MAP_FAILED)
return FatalError();
#else
mmapAddr = NULL;
#endif
enabled = true;
exit:
return 0;
}
int LogMethod(void* pCode, size_t codeSize, const char* symbol, const void* debugInfo, size_t debugInfoSize, const void* unwindInfo, size_t unwindInfoSize, bool reportCodeBlock)
{
int result = 0;
(void)unwindInfo;
(void)unwindInfoSize;
if (enabled)
{
size_t symbolLen = strlen(symbol);
JitCodeLoadRecord record;
JitCodeDebugInfoRecord debugRecord;
size_t reportedCodeSize = reportCodeBlock ? codeSize : 0;
size_t bytesRemaining =
(debugInfo != nullptr ? sizeof(JitCodeDebugInfoRecord) + debugInfoSize : 0) +
sizeof(JitCodeLoadRecord) +
symbolLen +
1 +
reportedCodeSize;
record.header.timestamp = GetTimeStampNS();
record.vma = (uint64_t) pCode;
record.code_addr = (uint64_t) pCode;
record.code_size = reportedCodeSize;
record.header.total_size = sizeof(JitCodeLoadRecord) + symbolLen + 1 + reportedCodeSize;
debugRecord.header.timestamp = record.header.timestamp;
debugRecord.header.total_size = sizeof(JitCodeDebugInfoRecord) + debugInfoSize;
iovec items[5];
size_t itemsCount = 0;
if (debugInfo != nullptr)
{
items[itemsCount++] = { &debugRecord, sizeof(JitCodeDebugInfoRecord) };
items[itemsCount++] = { const_cast<void*>(debugInfo), debugInfoSize };
}
// TODO: insert unwindInfo record items here.
items[itemsCount++] = { &record, sizeof(JitCodeLoadRecord) };
items[itemsCount++] = { (void *)symbol, symbolLen + 1 };
items[itemsCount++] = { pCode, reportedCodeSize };
size_t itemsWritten = 0;
if (!enabled)
goto exit;
// Increment codeIndex while locked
record.code_index = ++codeIndex;
do
{
result = writev(fd, items + itemsWritten, itemsCount - itemsWritten);
if ((size_t)result == bytesRemaining)
break;
if (result == -1)
{
if (errno == EINTR)
continue;
return FatalError();
}
// Detect unexpected failure cases.
_ASSERTE(bytesRemaining > (size_t)result);
_ASSERTE(result > 0);
// Handle partial write case
bytesRemaining -= result;
do
{
if ((size_t)result < items[itemsWritten].iov_len)
{
items[itemsWritten].iov_len -= result;
items[itemsWritten].iov_base = (void*)((size_t) items[itemsWritten].iov_base + result);
break;
}
else
{
result -= items[itemsWritten].iov_len;
itemsWritten++;
// Detect unexpected failure case.
_ASSERTE(itemsWritten < itemsCount);
}
} while (result > 0);
} while (true);
}
exit:
return 0;
}
int Finish()
{
int result = 0;
if (enabled)
{
enabled = false;
if (mmapAddr != NULL)
{
result = munmap(mmapAddr, sizeof(FileHeader));
if (result == -1)
return FatalError();
}
mmapAddr = MAP_FAILED;
result = fsync(fd);
if (result == -1)
return FatalError();
result = close(fd);
if (result == -1 && errno != EINTR)
return FatalError();
fd = -1;
}
return 0;
}
};
PerfJitDumpState& GetState()
{
static PerfJitDumpState s;
return s;
}
int
PALAPI
PAL_PerfJitDump_Start(const char* path)
{
return GetState().Start(path);
}
bool
PALAPI
PAL_PerfJitDump_IsStarted()
{
return GetState().enabled;
}
int
PALAPI
PAL_PerfJitDump_LogMethod(void* pCode, size_t codeSize, const char* symbol, const void* debugInfo, size_t debugInfoSize, const void* unwindInfo, size_t unwindInfoSize, bool reportCodeBlock)
{
return GetState().LogMethod(pCode, codeSize, symbol, debugInfo, debugInfoSize, unwindInfo, unwindInfoSize, reportCodeBlock);
}
int
PALAPI
PAL_PerfJitDump_Finish()
{
return GetState().Finish();
}
#else // JITDUMP_SUPPORTED
int
PALAPI
PAL_PerfJitDump_Start(const char* path)
{
return 0;
}
bool
PALAPI
PAL_PerfJitDump_IsStarted()
{
return false;
}
int
PALAPI
PAL_PerfJitDump_LogMethod(void* pCode, size_t codeSize, const char* symbol, const void* debugInfo, size_t debugInfoSize, const void* unwindInfo, size_t unwindInfoSize, bool reportCodeBlock)
{
return 0;
}
int
PALAPI
PAL_PerfJitDump_Finish()
{
return 0;
}
#endif // JITDUMP_SUPPORTED