forked from namhyung/uftrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf.c
635 lines (526 loc) · 14.6 KB
/
perf.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include <byteswap.h>
#include <errno.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#include "uftrace.h"
#include "utils/compiler.h"
#include "utils/fstack.h"
#include "utils/perf.h"
/* It needs to synchronize records using monotonic clock */
#ifdef HAVE_PERF_CLOCKID
#define PERF_PARANOID_CHECK "/proc/sys/kernel/perf_event_paranoid"
static bool use_perf = true;
static int open_perf_event(int pid, int cpu, int use_ctxsw)
{
/* use dummy events to get scheduling info (Linux v4.3 or later) */
struct perf_event_attr attr = {
.size = sizeof(attr),
.type = PERF_TYPE_SOFTWARE,
.config = PERF_COUNT_SW_DUMMY,
.sample_type = PERF_SAMPLE_TIME | PERF_SAMPLE_TID,
.sample_period = 1,
.sample_id_all = 1,
.exclude_kernel = 1,
.disabled = 1,
.enable_on_exec = 1,
.inherit = 1,
.watermark = 1,
.wakeup_watermark = PERF_WATERMARK,
.task = 1,
.comm = 1,
.use_clockid = 1,
.clockid = clock_source,
#ifdef HAVE_PERF_CTXSW
.context_switch = use_ctxsw,
#endif
};
unsigned long flag = PERF_FLAG_FD_NO_GROUP;
return syscall(SYS_perf_event_open, &attr, pid, cpu, -1, flag);
}
/**
* setup_perf_record - prepare recording perf events
* @perf: data structure for perf record
* @nr_cpu: total number of cpus to record
* @pid: process id to record
* @dirname: directory name to save perf record data
* @use_ctxsw: whether to use context_switch attribute
*
* This function prepares recording linux perf events. The perf_event
* fd should be opened and mmaped for each cpu.
*
* It returns 0 for success, -1 if failed. Callers should call
* finish_perf_record() after recording.
*/
int setup_perf_record(struct uftrace_perf_writer *perf, int nr_cpu, int pid, const char *dirname,
int use_ctxsw)
{
char filename[PATH_MAX];
int fd, cpu;
perf->event_fd = xcalloc(nr_cpu, sizeof(*perf->event_fd));
perf->data_pos = xcalloc(nr_cpu, sizeof(*perf->data_pos));
perf->page = xcalloc(nr_cpu, sizeof(*perf->page));
perf->fp = xcalloc(nr_cpu, sizeof(*perf->fp));
perf->nr_event = nr_cpu;
memset(perf->event_fd, -1, nr_cpu * sizeof(fd));
if (!PERF_CTXSW_AVAILABLE && use_ctxsw) {
/* Operation not supported */
pr_dbg("linux:schedule event is not supported for this kernel\n");
use_ctxsw = 0;
}
for (cpu = 0; cpu < nr_cpu; cpu++) {
fd = open_perf_event(pid, cpu, use_ctxsw);
if (fd < 0) {
int saved_errno = errno;
pr_dbg("skipping perf event due to error: %m\n");
if (saved_errno == EACCES)
pr_dbg("please check %s\n", PERF_PARANOID_CHECK);
use_perf = false;
break;
}
perf->event_fd[cpu] = fd;
perf->page[cpu] =
mmap(NULL, PERF_MMAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (perf->page[cpu] == MAP_FAILED) {
pr_warn("failed to mmap perf event: %m\n");
use_perf = false;
break;
}
snprintf(filename, sizeof(filename), "%s/perf-cpu%d.dat", dirname, cpu);
perf->fp[cpu] = fopen(filename, "w");
if (perf->fp[cpu] == NULL) {
pr_warn("failed to create perf data file: %m\n");
use_perf = false;
break;
}
}
if (!use_perf) {
finish_perf_record(perf);
return -1;
}
return 0;
}
/**
* finish_perf_record - destroy data structure for perf recording
* @perf: data structure for perf record
*
* This function releases all resources in the @perf.
*/
void finish_perf_record(struct uftrace_perf_writer *perf)
{
int cpu;
for (cpu = 0; cpu < perf->nr_event; cpu++) {
close(perf->event_fd[cpu]);
munmap(perf->page[cpu], PERF_MMAP_SIZE);
if (perf->fp[cpu])
fclose(perf->fp[cpu]);
}
free(perf->event_fd);
free(perf->page);
free(perf->data_pos);
free(perf->fp);
perf->event_fd = NULL;
perf->page = NULL;
perf->data_pos = NULL;
perf->fp = NULL;
perf->nr_event = 0;
}
/**
* record_perf_data - record perf event data to file or socket
* @perf: data structure for perf record
* @cpu: cpu number for perf event
* @sock: socket fd to send perf data
*
* This function copies contents in the perf ring buffer to a file
* or a network socket.
*/
void record_perf_data(struct uftrace_perf_writer *perf, int cpu, int sock)
{
struct perf_event_mmap_page *pc;
unsigned char *data;
volatile uint64_t *ptr;
uint64_t mask;
uint64_t old, pos, start, end;
unsigned long size;
unsigned char *buf;
/*
* it can have invalid cpu index due to rounding.
* see cmds/record.c::start_tracing()
*/
if (cpu < 0)
return;
pc = perf->page[cpu];
data = perf->page[cpu] + pc->data_offset;
ptr = (void *)&pc->data_head;
mask = pc->data_size - 1;
pos = *ptr;
old = perf->data_pos[cpu];
/* ensure reading the data head first */
read_memory_barrier();
if (pos == old)
return;
size = pos - old;
if (size > (unsigned long)(mask) + 1) {
static bool once = true;
if (once) {
pr_warn("failed to keep up with mmap data.\n");
once = false;
}
pc->data_tail = pos;
perf->data_pos[cpu] = pos;
return;
}
start = old;
end = pos;
/* handle wrap around */
if ((start & mask) + size != (end & mask)) {
buf = &data[start & mask];
size = mask + 1 - (start & mask);
start += size;
if (sock > 0)
send_trace_perf_data(sock, cpu, buf, size);
else if (fwrite(buf, 1, size, perf->fp[cpu]) != size) {
pr_dbg("failed to write perf data: %m\n");
goto out;
}
}
buf = &data[start & mask];
size = end - start;
if (sock > 0)
send_trace_perf_data(sock, cpu, buf, size);
else if (fwrite(buf, 1, size, perf->fp[cpu]) != size)
pr_dbg("failed to write perf data: %m\n");
out:
/* ensure all reads are done before we write the tail. */
full_memory_barrier();
pc->data_tail = pos;
perf->data_pos[cpu] = pos;
}
#endif /* HAVE_PERF_CLOCKID */
/**
* setup_perf_data - prepare reading perf event data
* @handle - uftrace data file handle
*
* This function prepares to read perf event data from perf-cpu*.dat
* files. It returns 0 on success which includes that perf event data
* already setup, -1 on failure. Callers should call
* finish_perf_data() after reading all perf event data.
*/
int setup_perf_data(struct uftrace_data *handle)
{
struct uftrace_perf_reader *perf;
glob_t globbuf;
char *pattern;
size_t i;
int ret = -1;
if (has_perf_data(handle))
return 0;
xasprintf(&pattern, "%s/perf-cpu*.dat", handle->dirname);
if (glob(pattern, GLOB_ERR, NULL, &globbuf)) {
pr_dbg("failed to search perf data file\n");
handle->hdr.feat_mask &= ~PERF_EVENT;
handle->nr_perf = 0;
goto out;
}
perf = xcalloc(globbuf.gl_pathc, sizeof(*perf));
for (i = 0; i < globbuf.gl_pathc; i++) {
perf[i].fp = fopen(globbuf.gl_pathv[i], "r");
if (perf[i].fp == NULL)
pr_err("open failed: %s", globbuf.gl_pathv[i]);
}
handle->nr_perf = globbuf.gl_pathc;
handle->perf = perf;
ret = 0;
globfree(&globbuf);
out:
free(pattern);
return ret;
}
/**
* finish_perf_data - destroy resources for perf event data
* @handle - uftrace data file handle
*
* This function releases all resources regarding perf event.
*/
void finish_perf_data(struct uftrace_data *handle)
{
int i;
if (handle->perf == NULL)
return;
for (i = 0; i < handle->nr_perf; i++)
fclose(handle->perf[i].fp);
free(handle->perf);
handle->perf = NULL;
}
static int read_perf_event(struct uftrace_data *handle, struct uftrace_perf_reader *perf)
{
struct perf_event_header h;
struct uftrace_task_reader *task;
union {
struct perf_context_switch_event cs;
struct perf_task_event t;
struct perf_comm_event c;
} u;
size_t len;
int comm_len;
if (perf->done || perf->fp == NULL)
return -1;
again:
if (fread(&h, sizeof(h), 1, perf->fp) != 1) {
perf->done = true;
return -1;
}
if (handle->needs_byte_swap) {
h.type = bswap_32(h.type);
h.misc = bswap_16(h.misc);
h.size = bswap_16(h.size);
}
len = h.size - sizeof(h);
switch (h.type) {
case PERF_RECORD_SWITCH:
if (fread(&u.cs, len, 1, perf->fp) != 1)
return -1;
if (handle->needs_byte_swap) {
u.cs.sample_id.time = bswap_64(u.cs.sample_id.time);
u.cs.sample_id.tid = bswap_32(u.cs.sample_id.tid);
}
perf->u.ctxsw.out = h.misc & PERF_RECORD_MISC_SWITCH_OUT;
perf->u.ctxsw.preempt = h.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT;
perf->time = u.cs.sample_id.time;
perf->tid = u.cs.sample_id.tid;
break;
case PERF_RECORD_FORK:
case PERF_RECORD_EXIT:
if (fread(&u.t, len, 1, perf->fp) != 1)
return -1;
if (handle->needs_byte_swap) {
u.t.tid = bswap_32(u.t.tid);
u.t.pid = bswap_32(u.t.pid);
u.t.ppid = bswap_32(u.t.ppid);
u.t.time = bswap_64(u.t.time);
}
perf->u.task.pid = u.t.pid;
perf->u.task.ppid = u.t.ppid;
perf->time = u.t.time;
perf->tid = u.t.tid;
break;
case PERF_RECORD_COMM:
/* length of comm event is variable */
comm_len = ALIGN(len - sizeof(u.c.sample_id), 8);
if (fread(&u.c, comm_len, 1, perf->fp) != 1)
return -1;
if (fread(&u.c.sample_id, sizeof(u.c.sample_id), 1, perf->fp) != 1)
return -1;
if (handle->needs_byte_swap) {
u.c.tid = bswap_32(u.c.tid);
u.c.pid = bswap_32(u.c.pid);
u.c.sample_id.time = bswap_64(u.c.sample_id.time);
}
perf->u.comm.pid = u.c.pid;
perf->u.comm.exec = h.misc & PERF_RECORD_MISC_COMM_EXEC;
strncpy(perf->u.comm.comm, u.c.comm, sizeof(perf->u.comm.comm));
perf->time = u.c.sample_id.time;
perf->tid = u.c.tid;
break;
default:
pr_dbg3("skip unknown event: %u\n", h.type);
if (fseek(perf->fp, len, SEEK_CUR) < 0) {
pr_warn("skipping perf data failed: %m\n");
perf->done = true;
return -1;
}
goto again;
}
task = get_task_handle(handle, perf->tid);
if (unlikely(task == NULL || task->fp == NULL))
goto again;
if (!check_time_range(&handle->time_range, perf->time))
goto again;
perf->type = h.type;
perf->valid = true;
return 0;
}
/**
* read_perf_data - read perf event data
* @handle: uftrace data file handle
*
* This function reads perf events for each cpu data file and returns
* the (cpu) index of earliest event. The event info can be found in
* @handle->perf[idx].
*
* It's important that callers should reset the valid bit after using
* the event so that it can read next event for the cpu data file.
*/
int read_perf_data(struct uftrace_data *handle)
{
struct uftrace_perf_reader *perf;
uint64_t min_time = ~0ULL;
int best = -1;
int i;
for (i = 0; i < handle->nr_perf; i++) {
perf = &handle->perf[i];
if (perf->done)
continue;
if (!perf->valid) {
if (read_perf_event(handle, perf) < 0)
continue;
}
if (perf->time < min_time) {
min_time = perf->time;
best = i;
}
}
handle->last_perf_idx = best;
return best;
}
/**
* get_perf_record - convert perf event into uftrace record format
* @handle: uftrace data file handle
* @perf: data structure for perf event
*
* This function converts the last perf event into an uftrace record
* so that it can be handled in the fstack code like normal function
* record. This is useful for schedule event treated as a function.
*
* Normally this is called after read_perf_data() so it knows current
* event. But do_dump_file() calls it directly without the above
* function in order to access to the raw file contents.
*/
struct uftrace_record *get_perf_record(struct uftrace_data *handle,
struct uftrace_perf_reader *perf)
{
static struct uftrace_record rec;
if (!perf->valid) {
if (read_perf_event(handle, perf) < 0)
return NULL;
}
rec.type = UFTRACE_EVENT;
rec.time = perf->time;
rec.magic = RECORD_MAGIC;
rec.more = 0;
switch (perf->type) {
case PERF_RECORD_FORK:
rec.addr = EVENT_ID_PERF_TASK;
break;
case PERF_RECORD_EXIT:
rec.addr = EVENT_ID_PERF_EXIT;
break;
case PERF_RECORD_COMM:
rec.addr = EVENT_ID_PERF_COMM;
break;
case PERF_RECORD_SWITCH:
if (perf->u.ctxsw.out) {
if (perf->u.ctxsw.preempt)
rec.addr = EVENT_ID_PERF_SCHED_OUT_PREEMPT;
else
rec.addr = EVENT_ID_PERF_SCHED_OUT;
}
else
rec.addr = EVENT_ID_PERF_SCHED_IN;
break;
}
return &rec;
}
/**
* update_perf_task_comm - read perf event data and update task's comm
* @handle: uftrace data file handle
*
* This function reads perf events for each cpu data file and updates
* task->comm for each PERF_RECORD_COMM.
*/
void update_perf_task_comm(struct uftrace_data *handle)
{
struct uftrace_perf_reader *perf;
struct uftrace_task *task;
int i;
for (i = 0; i < handle->nr_perf; i++) {
perf = &handle->perf[i];
while (!perf->done) {
if (read_perf_event(handle, perf) < 0)
continue;
task = find_task(&handle->sessions, perf->tid);
if (task == NULL)
continue;
if (task->time.stamp == 0 || task->time.stamp > perf->time)
task->time.stamp = perf->time;
if (perf->type != PERF_RECORD_COMM)
continue;
memcpy(task->comm, perf->u.comm.comm, sizeof(task->comm));
}
/* reset file position for future processing */
rewind(perf->fp);
perf->valid = false;
perf->done = false;
}
}
static void remove_event_rstack(struct uftrace_task_reader *task)
{
struct uftrace_rstack_list_node *last;
uint64_t last_addr;
/* also delete matching entry (at the last) */
do {
last = list_last_entry(&task->event_list.read, typeof(*last), list);
last_addr = last->rstack.addr;
delete_last_rstack_list(&task->event_list);
} while (last_addr != EVENT_ID_PERF_SCHED_OUT &&
last_addr != EVENT_ID_PERF_SCHED_OUT_PREEMPT);
}
void process_perf_event(struct uftrace_data *handle)
{
struct uftrace_perf_reader *perf;
struct uftrace_task_reader *task;
struct uftrace_record *rec;
struct uftrace_fstack_args args = {};
int p;
if (handle->perf_event_processed)
return;
while (1) {
p = read_perf_data(handle);
if (p < 0)
break;
perf = &handle->perf[p];
rec = get_perf_record(handle, perf);
task = get_task_handle(handle, perf->tid);
if (unlikely(task == NULL || task->fp == NULL))
continue;
if (perf->type == PERF_RECORD_COMM) {
rec->more = 1;
args.args = NULL;
args.data = xstrdup(perf->u.comm.comm);
args.len = strlen(perf->u.comm.comm) + 1;
}
else if (perf->type == PERF_RECORD_SWITCH && !perf->u.ctxsw.out) {
struct uftrace_rstack_list_node *last;
uint64_t delta;
if (task->event_list.count == 0)
goto add_it;
last = list_last_entry(&task->event_list.read, typeof(*last), list);
/* time filter is meaningful only for schedule events */
while (last->rstack.addr != EVENT_ID_PERF_SCHED_OUT &&
last->rstack.addr != EVENT_ID_PERF_SCHED_OUT_PREEMPT) {
if (last->list.prev == &task->event_list.read)
goto add_it;
last = list_prev_entry(last, list);
}
delta = perf->time - last->rstack.time;
if (delta < handle->time_filter) {
remove_event_rstack(task);
perf->valid = false;
continue;
}
}
add_it:
add_to_rstack_list(&task->event_list, rec, &args);
if (args.len) {
free(args.data);
args.data = NULL;
args.len = 0;
}
perf->valid = false;
}
handle->perf_event_processed = true;
}