-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathhost_perf_data.c
466 lines (399 loc) · 12.6 KB
/
host_perf_data.c
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
466
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2020 Intel Corporation
* Paweł Marczewski <pawel@invisiblethingslab.com>
*/
/*
* A library for dumping synthetic perf.data events, for consumption by 'perf report' tool.
*
* For more information on the format, see Linux sources:
*
* - tools/perf/Documentation/perf.data-file-format.txt
* - include/uapi/linux/perf_event.h
* - tools/perf/util/header.h
*
* Unfortunately, the format contains a header with data offsets and sizes, as well as additional
* headers at the end of files. To simplify processing, we write all of these on closing (pd_close).
*
* (There is also a simpler "pipe-mode data" format, which does not require seeking, but it's less
* convenient to use because perf userspace tools recognize it only when reading from stdin. This
* has been fixed in Linux 5.8: https://lkml.org/lkml/2020/5/7/294)
*
* To view the report, use 'perf report -i <filename>'.
*
* For debugging the output, you can use:
*
* - perf script -D -i <filename>: show a partial parse of the file
* - perf script -v -i <filename>: show more errors if the file doesn't parse
*/
#include <asm/errno.h>
#include <asm/perf_regs.h>
#include <assert.h>
#include <linux/perf_event.h>
#include <linux/fs.h>
#include "host_internal.h"
#include "host_log.h"
#include "perm.h"
#ifndef __x86_64__
#error "Unsupported architecture"
#endif
#define PROLOGUE_SIZE (sizeof(struct perf_file_header) + sizeof(struct perf_file_attr))
/* Buffer size for pending perf data. Choose a big enough size (32 MB) so that we don't need to
* flush to a file too often. */
#define BUF_SIZE (32 * 1024 * 1024)
/* Registers to sample - see arch/x86/include/uapi/asm/perf_regs.h */
#define SAMPLE_REGS_CNT 18
#define SAMPLE_REGS ((1 << PERF_REG_X86_AX) | \
(1 << PERF_REG_X86_BX) | \
(1 << PERF_REG_X86_CX) | \
(1 << PERF_REG_X86_DX) | \
(1 << PERF_REG_X86_SI) | \
(1 << PERF_REG_X86_DI) | \
(1 << PERF_REG_X86_BP) | \
(1 << PERF_REG_X86_SP) | \
(1 << PERF_REG_X86_IP) | \
(1 << PERF_REG_X86_FLAGS) | \
(1 << PERF_REG_X86_R8) | \
(1 << PERF_REG_X86_R9) | \
(1 << PERF_REG_X86_R10) | \
(1 << PERF_REG_X86_R11) | \
(1 << PERF_REG_X86_R12) | \
(1 << PERF_REG_X86_R13) | \
(1 << PERF_REG_X86_R14) | \
(1 << PERF_REG_X86_R15))
/* Internal perf.data file definitions - see linux/tools/perf/util/header.h */
#define PERF_MAGIC 0x32454c4946524550ULL // "PERFILE2"
#define HEADER_ARCH 6
struct perf_file_section {
uint64_t offset;
uint64_t size;
};
struct perf_file_header {
uint64_t magic;
uint64_t size;
uint64_t attr_size;
struct perf_file_section attrs;
struct perf_file_section data;
struct perf_file_section event_types;
uint64_t flags[4];
};
struct perf_file_attr {
struct perf_event_attr attr;
struct perf_file_section ids;
};
struct perf_data {
int fd;
// Data to be flushed to a file
size_t buf_count;
uint8_t buf[BUF_SIZE];
bool with_stack;
};
static ssize_t write_all(int fd, const void* buf, size_t count) {
while (count > 0) {
ssize_t ret = DO_SYSCALL(write, fd, buf, count);
if (ret == -EINTR)
continue;
if (ret < 0)
return ret;
count -= ret;
buf += ret;
}
return 0;
}
static int pd_flush(struct perf_data* pd) {
if (pd->buf_count == 0)
return 0;
ssize_t ret = write_all(pd->fd, pd->buf, pd->buf_count);
if (ret < 0)
return ret;
pd->buf_count = 0;
return 0;
}
// Add data to buffer; flush first if necessary
static int pd_write(struct perf_data* pd, const void* data, size_t size) {
if (pd->buf_count + size > sizeof(pd->buf)) {
int ret = pd_flush(pd);
if (ret < 0)
return ret;
}
assert(pd->buf_count + size <= sizeof(pd->buf));
memcpy(pd->buf + pd->buf_count, data, size);
pd->buf_count += size;
return 0;
}
int pd_open_file(struct perf_data* pd, const char* file_name) {
int ret;
int fd = DO_SYSCALL(open, file_name, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, PERM_rw_r__r__);
if (fd < 0) {
log_error("pd_open_file: cannot open %s for writing: %s", file_name, unix_strerror(fd));
return fd;
}
/*
* Start writing to file from PROLOGUE_SIZE position. The beginning will be overwritten in
* write_prologue_epilogue().
*/
ret = DO_SYSCALL(ftruncate, fd, PROLOGUE_SIZE);
if (ret < 0)
goto fail;
ret = DO_SYSCALL(lseek, fd, PROLOGUE_SIZE, SEEK_SET);
if (ret < 0)
goto fail;
pd->fd = fd;
return 0;
fail: ;
int close_ret = DO_SYSCALL(close, fd);
if (close_ret < 0)
log_error("pd_open_file: close failed: %s", unix_strerror(close_ret));
return ret;
}
struct perf_data* pd_open(const char* file_name, bool with_stack) {
struct perf_data* pd = malloc(sizeof(*pd));
if (!pd) {
log_error("pd_open: out of memory");
return NULL;
}
int ret = pd_open_file(pd, file_name);
if (ret < 0) {
free(pd);
return NULL;
}
pd->buf_count = 0;
pd->with_stack = with_stack;
return pd;
}
static int write_prologue_epilogue(struct perf_data* pd) {
int ret;
assert(pd->buf_count == 0); // all data flushed
ssize_t data_end = DO_SYSCALL(lseek, pd->fd, 0, SEEK_CUR);
if (data_end < 0)
return data_end;
/*
* Prologue, at beginning of file:
* - struct perf_file_header: description of other file sections
* - struct perf_file_attr: event configuration
*/
ret = DO_SYSCALL(lseek, pd->fd, 0, SEEK_SET);
if (ret < 0)
return ret;
// Determines the set of data in PERF_RECORD_SAMPLE
uint64_t sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID | PERF_SAMPLE_PERIOD;
if (pd->with_stack)
sample_type |= PERF_SAMPLE_CALLCHAIN | PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER;
struct perf_file_attr attr = {
.attr = {
.type = PERF_TYPE_SOFTWARE,
.size = sizeof(attr.attr),
.sample_type = sample_type,
.sample_regs_user = SAMPLE_REGS,
},
.ids = {0},
};
struct perf_file_header header = {
.magic = PERF_MAGIC,
.size = sizeof(header),
.attr_size = sizeof(attr.attr),
.attrs = {
.offset = sizeof(header),
.size = sizeof(attr),
},
.data = {
.offset = PROLOGUE_SIZE,
.size = data_end - PROLOGUE_SIZE,
},
.event_types = {0},
.flags = {0},
};
// Signifies that a HEADER_ARCH section will be included after data section
header.flags[0] |= 1 << HEADER_ARCH;
ret = write_all(pd->fd, &header, sizeof(header));
if (ret < 0)
return ret;
ret = write_all(pd->fd, &attr, sizeof(attr));
if (ret < 0)
return ret;
/*
* Epilogue, after the data section. Contains header sections, as specified by header.flags.
* We include a HEADER_ARCH section. While it's documented as optional, 'perf script' crashes
* without it.
*/
ret = DO_SYSCALL(lseek, pd->fd, data_end, SEEK_SET);
if (ret < 0)
return ret;
const char* arch = "x86_64";
uint32_t arch_size = strlen(arch) + 1;
struct perf_file_section arch_section = {
.offset = data_end + sizeof(arch_section),
.size = sizeof(arch_size) + arch_size,
};
ret = write_all(pd->fd, &arch_section, sizeof(arch_section));
if (ret < 0)
return ret;
ret = write_all(pd->fd, &arch_size, sizeof(arch_size));
if (ret < 0)
return ret;
ret = write_all(pd->fd, arch, arch_size);
if (ret < 0)
return ret;
return 0;
}
ssize_t pd_close_file(struct perf_data* pd) {
ssize_t ret;
ret = pd_flush(pd);
if (ret < 0)
goto out;
ret = write_prologue_epilogue(pd);
if (ret < 0)
goto out;
ret = DO_SYSCALL(lseek, pd->fd, 0, SEEK_CUR);
if (ret < 0)
goto out;
out: ;
int close_ret = DO_SYSCALL(close, pd->fd);
if (close_ret < 0)
log_error("pd_close_file: close failed: %s", unix_strerror(close_ret));
/* Returns the size of the finalized perf-data file on success */
return ret;
}
ssize_t pd_close(struct perf_data* pd) {
ssize_t ret = pd_close_file(pd);
free(pd);
return ret;
}
int pd_event_command(struct perf_data* pd, const char* command, uint32_t pid, uint32_t tid) {
size_t command_size = strlen(command) + 1;
struct {
struct perf_event_header header;
uint32_t pid, tid;
} event = {
.header = {
.type = PERF_RECORD_COMM,
.misc = 0,
.size = sizeof(event) + command_size,
},
.pid = pid,
.tid = tid,
};
int ret;
ret = pd_write(pd, &event, sizeof(event));
if (ret < 0)
return ret;
ret = pd_write(pd, command, command_size);
if (ret < 0)
return ret;
return 0;
}
int pd_event_mmap(struct perf_data* pd, const char* filename, uint32_t pid, uint64_t addr,
uint64_t len, uint64_t pgoff) {
size_t filename_size = strlen(filename) + 1;
struct {
struct perf_event_header header;
uint32_t pid, tid;
uint64_t addr, len, pgoff;
} event = {
.header = {
.type = PERF_RECORD_MMAP,
.misc = 0,
.size = sizeof(event) + filename_size,
},
.pid = pid,
.tid = pid,
.addr = addr,
.len = len,
.pgoff = pgoff,
};
int ret;
ret = pd_write(pd, &event, sizeof(event));
if (ret < 0)
return ret;
ret = pd_write(pd, filename, filename_size);
if (ret < 0)
return ret;
return 0;
}
static int pd_event_sample(struct perf_data* pd, uint64_t ip, uint32_t pid, uint32_t tid,
uint64_t period, size_t extra_size) {
struct {
struct perf_event_header header;
uint64_t ip;
uint32_t pid, tid;
uint64_t period;
} event = {
.header = {
.type = PERF_RECORD_SAMPLE,
.misc = PERF_RECORD_MISC_USER, // user/kernel/hypervisor
.size = sizeof(event) + extra_size,
},
.ip = ip,
.pid = pid,
.tid = tid,
.period = period,
};
return pd_write(pd, &event, sizeof(event));
}
int pd_event_sample_simple(struct perf_data* pd, uint64_t ip, uint32_t pid, uint32_t tid,
uint64_t period) {
assert(!pd->with_stack);
return pd_event_sample(pd, ip, pid, tid, period, /*extra_size=*/0);
}
int pd_event_sample_stack(struct perf_data* pd, uint64_t ip, uint32_t pid, uint32_t tid,
uint64_t period, sgx_pal_gpr_t* gpr, void* stack, size_t stack_size) {
assert(pd->with_stack);
struct {
// Empty callchain section - needed so that perf will attempt to recover call chain
struct {
uint64_t nr;
} callchain;
struct {
uint64_t abi;
uint64_t regs[SAMPLE_REGS_CNT];
} regs;
} extra = {
.callchain = {
.nr = 0,
},
.regs = {
.abi = PERF_SAMPLE_REGS_ABI_64,
.regs = {
gpr->rax,
gpr->rbx,
gpr->rcx,
gpr->rdx,
gpr->rsi,
gpr->rdi,
gpr->rbp,
gpr->rsp,
gpr->rip,
gpr->rflags,
gpr->r8,
gpr->r9,
gpr->r10,
gpr->r11,
gpr->r12,
gpr->r13,
gpr->r14,
gpr->r15,
},
},
};
size_t extra_size = sizeof(extra) + stack_size + 2 * sizeof(uint64_t);
int ret;
// Common section
ret = pd_event_sample(pd, ip, pid, tid, period, extra_size);
if (ret < 0)
return ret;
// Callchain and regs sections
ret = pd_write(pd, &extra, sizeof(extra));
if (ret < 0)
return ret;
// Stack section (variable length)
uint64_t size_field = stack_size;
ret = pd_write(pd, &size_field, sizeof(size_field)); // uint64_t size
if (ret < 0)
return ret;
ret = pd_write(pd, stack, stack_size);
if (ret < 0)
return ret;
ret = pd_write(pd, &size_field, sizeof(size_field)); // uint64_t dyn_size = size
if (ret < 0)
return ret;
return 0;
}