File: profiler.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (697 lines) | stat: -rw-r--r-- 15,823 bytes parent folder | download | duplicates (2)
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include <assert.h>
#include <fcntl.h>
#include <linux/perf_event.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>

#include "profiler.h"

#define PATH_TEMPLATE "/sys/kernel/tracing/events/%s/enable"

#define PAGE_SIZE 4096
#define N_SAMPLE_PAGES 1024
#define MMAP_SIZE (PAGE_SIZE * (1 /* header pager */ + N_SAMPLE_PAGES))

#define DEVICE_NAME_LEN 32

#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
#define u64 uint64_t

/* Datasource types. */
enum {
    NVME = 0,
    BLOCK,
};

/* Tracepoint types. */
enum {
    NVME_SETUP_CMD = 0,
    NVME_COMPLETE_RQ,
};

enum {
    BLOCK_BIO_QUEUE = 0,
    BLOCK_RQ_COMPLETE,
    BLOCK_BIO_COMPLETE,
};

struct sample
{
    struct perf_event_header header;
    u64 time;     /* if PERF_SAMPLE_TIME */
    u32 cpu, res; /* if PERF_SAMPLE_CPU */
    u32 size;     /* if PERF_SAMPLE_RAW */
};

struct tracepoint
{
    u16 common_type;
    u8 common_flag;
    u8 common_preempt_count;
    u32 common_pid;
};

struct nvmeSetupCmd
{
    struct tracepoint tp;
    char device[DEVICE_NAME_LEN];
    int ctrl_id;
    int qid;
    u8 opcode;
    u8 flags;
    u8 fctype;
    u16 cid;
    u32 nsid;
    bool metdata;
    u8 cdw10[24];
};

struct nvmeCompleteRq
{
    struct tracepoint tp;
    char device[DEVICE_NAME_LEN];
    int ctrl_id;
    int qid;
    int cid;
    u64 result;
    u8 retries;
    u8 flags;
    u16 status;
};

struct blockBioQueue
{
    struct tracepoint tp;
    u32 dev;
    u64 sector;
    u32 nr_sector;
};

struct blockRqComplete
{
    struct tracepoint tp;
    u32 dev;
    u64 sector;
    u32 nr_sector;
};

struct blockBioComplete
{
    struct tracepoint tp;
    u32 dev;
    u64 sector;
    u32 nr_sector;
};

static int perf_event_open(struct perf_event_attr *hw_event,
                           pid_t pid,
                           int cpu,
                           int group_fd,
                           unsigned long flags)
{
    int rv;
    rv = (int)syscall(SYS_perf_event_open, hw_event, pid, cpu, group_fd, flags);
    return rv;
}

/* Return the exact size of the PERF_RECORD_SAMPLE struct, without alignment. */
static size_t sizeofSample(void)
{
    return sizeof(struct perf_event_header) +
           sizeof(u64) + /* PERF_SAMPLE_TIME */
           sizeof(u64) + /* PERF_SAMPLE_CPU */
           sizeof(u32) /* if PERF_SAMPLE_RAW */;
}

static unsigned eventId(const char *name)
{
    char path[1024];
    int fd;
    int rv;
    char n[32];

    sprintf(path, "/sys/kernel/tracing/events/%s/id", name);
    fd = open(path, O_RDONLY);
    if (fd < 0) {
        perror("open tracefs id");
        exit(1);
    }

    rv = (int)read(fd, n, sizeof n);
    if (rv < 0) {
        perror("read tracefs id");
        exit(1);
    }

    close(fd);

    return (unsigned)atoi(n);
}

static int profilerEventGroupAdd(struct ProfilerEventGroup *g, unsigned type)
{
    struct perf_event_attr attr;
    unsigned i = g->n_events;
    unsigned long flags;
    bool is_leader;
    int group_fd;

    is_leader = g->n_events == 0;

    g->n_events++;
    g->events = realloc(g->events, sizeof *g->events * g->n_events);
    assert(g->events != NULL);

    memset(&attr, 0, sizeof attr);

    attr.type = PERF_TYPE_TRACEPOINT;
    attr.size = sizeof attr;
    attr.config = type;
    attr.sample_type = (PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_RAW);
    attr.sample_period = 1;
    attr.disabled = is_leader ? 1 : 0;
    attr.use_clockid = 1;
    attr.clockid = CLOCK_MONOTONIC;
    attr.read_format = is_leader ? PERF_FORMAT_GROUP : 0;

    group_fd = is_leader ? -1 : g->events[0];
    flags = is_leader ? 0 : PERF_FLAG_FD_OUTPUT;

    g->events[i] = perf_event_open(&attr, -1, g->cpu, group_fd, flags);
    if (g->events[i] == -1) {
        perror("perf_event_open child");
        return -1;
    }

    if (is_leader) {
        g->data = mmap(NULL, MMAP_SIZE, PROT_READ, MAP_SHARED, g->events[0], 0);
        if (g->data == NULL) {
            printf("perf: mmap error\n");
            return -1;
        }
    }

    return 0;
}

static int profilerEventGroupInit(struct ProfilerEventGroup *g,
                                  int cpu,
                                  struct Profiler *p)
{
    int rv;

    g->cpu = cpu;

    g->events = NULL;
    g->n_events = 0;
    g->p = p;

    rv = profilerEventGroupAdd(g, p->nvme.types[NVME_SETUP_CMD]);
    if (rv != 0) {
        return -1;
    }

    rv = profilerEventGroupAdd(g, p->nvme.types[NVME_COMPLETE_RQ]);
    if (rv != 0) {
        return -1;
    }

    rv = profilerEventGroupAdd(g, p->block.types[BLOCK_BIO_QUEUE]);
    if (rv != 0) {
        return -1;
    }

    rv = profilerEventGroupAdd(g, p->block.types[BLOCK_RQ_COMPLETE]);
    if (rv != 0) {
        return -1;
    }

    rv = profilerEventGroupAdd(g, p->block.types[BLOCK_BIO_COMPLETE]);
    if (rv != 0) {
        return -1;
    }

    return 0;
}

static void profilerInitDataSource(struct ProfilerDataSource *d, int code)
{
    memset(d, 0, sizeof *d);

    d->n_commands = 0;

    switch (code) {
        case NVME:
            d->types[NVME_SETUP_CMD] = eventId("nvme/nvme_setup_cmd");
            d->types[NVME_COMPLETE_RQ] = eventId("nvme/nvme_complete_rq");
            break;
        case BLOCK:
            d->types[BLOCK_BIO_QUEUE] = eventId("block/block_bio_queue");
            d->types[BLOCK_RQ_COMPLETE] = eventId("block/block_rq_complete");
            d->types[BLOCK_BIO_COMPLETE] = eventId("block/block_bio_complete");
            break;
        default:
            assert(0);
            break;
    }
}

static int profilerInitPerf(struct Profiler *p)
{
    unsigned i;
    int rv;

    profilerInitDataSource(&p->nvme, NVME);
    profilerInitDataSource(&p->block, BLOCK);

    p->n_groups = (unsigned)sysconf(_SC_NPROCESSORS_ONLN);
    assert(p->n_groups > 0);

    p->groups = malloc(sizeof *p->groups * p->n_groups);
    assert(p->groups != NULL);

    for (i = 0; i < p->n_groups; i++) {
        rv = profilerEventGroupInit(&p->groups[i], (int)i, p);
        if (rv != 0) {
            return -1;
        }
    }

    return 0;
}

void ProfilerInit(struct Profiler *p, struct FsFileInfo *device)
{
    p->device = device;
    p->n_traces = 0;
    p->n_groups = 0;
    p->switches = 0;
}

int ProfilerPerf(struct Profiler *p)
{
    int rv;
    rv = profilerInitPerf(p);
    if (rv != 0) {
        return rv;
    }
    return 0;
}

static void profilerEventGroupClose(struct ProfilerEventGroup *g)
{
    unsigned i;

    for (i = 0; i < g->n_events; i++) {
        close(g->events[i]);
    }
    free(g->events);

    munmap(g->data, MMAP_SIZE);
}

void ProfilerClose(struct Profiler *p)
{
    unsigned i;

    for (i = 0; i < p->n_groups; i++) {
        profilerEventGroupClose(&p->groups[i]);
    }

    free(p->groups);
}

void ProfilerTrace(struct Profiler *p, const char *name)
{
    p->traces[p->n_traces] = name;
    p->n_traces++;
}

static int profilerTraceFsWrite(const char *name, const char *text)
{
    char path[1024];
    FILE *file;
    sprintf(path, PATH_TEMPLATE, name);
    file = fopen(path, "w");

    if (file == NULL) {
        perror("fopen tracefs");
        return -1;
    }
    fprintf(file, "%s", text);
    fclose(file);

    return 0;
}

static int profilerTraceFsStart(const char *name)
{
    return profilerTraceFsWrite(name, "1");
}

static int contextSwitchCounterStart(unsigned *counter)
{
    struct rusage usage;
    int rv;

    rv = getrusage(RUSAGE_SELF, &usage);
    if (rv != 0) {
        return -1;
    }

    *counter = (unsigned)usage.ru_nvcsw;

    return 0;
}

static int profilerEventGroupStart(struct ProfilerEventGroup *g)
{
    int rv;

    rv = ioctl(g->events[0], PERF_EVENT_IOC_RESET, 0);
    if (rv != 0) {
        perror("ioctl reset perf");
        return -1;
    }

    rv = ioctl(g->events[0], PERF_EVENT_IOC_ENABLE, 0);
    if (rv != 0) {
        perror("ioctl enable perf");
        return -1;
    }

    return 0;
}

int ProfilerStart(struct Profiler *p)
{
    unsigned i;
    int rv;

    for (i = 0; i < p->n_traces; i++) {
        rv = profilerTraceFsStart(p->traces[i]);
        if (rv != 0) {
            return rv;
        }
    }

    rv = contextSwitchCounterStart(&p->switches);
    if (rv != 0) {
        return rv;
    }

    if (p->device->driver != FS_DRIVER_GENERIC) {
        for (i = 0; i < p->n_groups; i++) {
            rv = profilerEventGroupStart(&p->groups[i]);
            if (rv != 0) {
                return -1;
            }
        }
    }

    return 0;
}

static int profilerTraceFsStop(const char *name)
{
    return profilerTraceFsWrite(name, "0");
}

static int contextSwitchCounterStop(unsigned *counter)
{
    struct rusage usage;
    int rv;

    rv = getrusage(RUSAGE_SELF, &usage);
    if (rv != 0) {
        return -1;
    }

    *counter = (unsigned)usage.ru_nvcsw - *counter;

    return 0;
}

static int processNvmeSetupCmd(struct ProfilerEventGroup *g,
                               struct sample *s,
                               struct nvmeSetupCmd *t)
{
    struct ProfilerDataSource *data = &g->p->nvme;
    struct FsFileInfo *device = g->p->device;
    unsigned i = data->n_commands;
    u64 slba = *(u64 *)(t->cdw10);
    u16 control = *(u16 *)(t->cdw10 + 10);
    u16 flags;

    /* Skip writes targeted to other partitions. */
    if (slba < device->block_dev_start || slba >= device->block_dev_end) {
        return 0;
    }

    if (t->opcode != 0x01 /* Write opcode, from linux/nvme.h */) {
        printf("unexpected nvme opcode 0x%x\n", t->opcode);
        return -1;
    }

    /* When the device has power-loss protection, we expect to have no flush
     * flags in the NVMe command. */
    if (device->block_dev_write_through) {
        flags = 0x0;
    } else {
        flags = 0x4000;
    }

    if (control != flags /* Possible flush flag (check trace output) */) {
        printf("unexpected nvme control 0x%x vs 0x%x\n", control, flags);
        return -1;
    }

    data->commands[i].id = t->cid;
    data->commands[i].start = s->time;
    data->n_commands++;
    return 0;
}

static int processNvmeCompleteRq(struct ProfilerEventGroup *g,
                                 struct sample *s,
                                 struct nvmeCompleteRq *t)
{
    struct ProfilerDataSource *data = &g->p->nvme;
    unsigned i;

    for (i = 0; i < data->n_commands; i++) {
        if (data->commands[i].id != (unsigned long long)t->cid) {
            continue;
        }
        data->commands[i].duration = s->time - data->commands[i].start;
        return 0;
    }

    /* The setup for this completion was probably skipped. TODO: make sure of
     * that. */

    return 0;
}

static int processBlockBioQueue(struct ProfilerEventGroup *g,
                                struct sample *s,
                                struct blockBioQueue *t)
{
    struct ProfilerDataSource *data = &g->p->block;
    struct FsFileInfo *device = g->p->device;
    unsigned i = data->n_commands;

    /* Skip writes targeted to other partitions. */
    if (t->sector < device->block_dev_start ||
        t->sector >= device->block_dev_end) {
        return 0;
    }

    data->commands[i].id = t->sector;
    data->commands[i].start = s->time;
    data->commands[i].duration = 0;
    data->n_commands++;

    return 0;
}

static int processBlockRqComplete(struct ProfilerEventGroup *g,
                                  struct sample *s,
                                  struct blockRqComplete *t)
{
    struct ProfilerDataSource *data = &g->p->block;
    struct FsFileInfo *device = g->p->device;
    unsigned i;

    /* Skip writes targeted to other partitions. */
    if (t->sector < device->block_dev_start ||
        t->sector >= device->block_dev_end) {
        return 0;
    }

    for (i = 0; i < data->n_commands; i++) {
        if (data->commands[i].id != t->sector) {
            continue;
        }
        if (data->commands[i].duration != 0) {
            printf("unexpected non-zero block command duration\n");
            return -1;
        }
        data->commands[i].duration = s->time - data->commands[i].start;
        return 0;
    }
    return -1;
}

static int processBlockBioComplete(struct ProfilerEventGroup *g,
                                   struct sample *s,
                                   struct blockBioComplete *t)
{
    struct ProfilerDataSource *data = &g->p->block;
    struct FsFileInfo *device = g->p->device;
    unsigned i;

    /* Skip writes targeted to other partitions. */
    if (t->sector < device->block_dev_start ||
        t->sector >= device->block_dev_end) {
        return 0;
    }

    for (i = 0; i < data->n_commands; i++) {
        if (data->commands[i].id != t->sector) {
            continue;
        }
        if (data->commands[i].duration != 0) {
            printf("unexpected non-zero block command duration\n");
            return -1;
        }
        data->commands[i].duration = s->time - data->commands[i].start;
        return 0;
    }
    return -1;
}

static int processPerfSample(struct ProfilerEventGroup *g,
                             struct sample *s,
                             u32 *size)
{
    struct tracepoint *t = (void *)(((char *)s) + sizeofSample());

    assert(s->header.type == PERF_RECORD_SAMPLE);
    *size = s->header.size;

    if (t->common_type == g->p->nvme.types[NVME_SETUP_CMD]) {
        return processNvmeSetupCmd(g, s, (void *)t);
    }

    if (t->common_type == g->p->nvme.types[NVME_COMPLETE_RQ]) {
        return processNvmeCompleteRq(g, s, (void *)t);
    }

    if (t->common_type == g->p->block.types[BLOCK_BIO_QUEUE]) {
        return processBlockBioQueue(g, s, (void *)t);
    }

    if (t->common_type == g->p->block.types[BLOCK_RQ_COMPLETE]) {
        return processBlockRqComplete(g, s, (void *)t);
    }

    if (t->common_type == g->p->block.types[BLOCK_BIO_COMPLETE]) {
        return processBlockBioComplete(g, s, (void *)t);
    }

    return -1;
}

struct read_format
{
    u64 nr; /* The number of event file descriptors */
    struct
    {
        u64 value; /* The value of the event (nr of samples) */
    } values[128]; /* TODO: remove hard-wired child limit */
};

static int profilerEventGroupStop(struct ProfilerEventGroup *g)
{
    struct sample *sample;
    struct read_format v;
    unsigned long long n;
    unsigned i;
    int rv;

    rv = ioctl(g->events[0], PERF_EVENT_IOC_DISABLE, 0);
    if (rv != 0) {
        perror("ioctl disable perf");
        return -1;
    }

    rv = (int)read(g->events[0], &v, sizeof v);
    assert(rv >= (int)(sizeof n));

    if (v.nr != g->n_events) {
        printf("mismatch between reported and registered event fds");
        return -1;
    }

    n = 0;
    for (i = 0; i < v.nr; i++) {
        n += v.values[i].value;
    }
    if (n == 0) {
        return 0;
    }

    sample = (void *)((char *)g->data + PAGE_SIZE);

    for (i = 0; i < n; i++) {
        u32 size;
        rv = processPerfSample(g, sample, &size);
        if (rv != 0) {
            return -1;
        }
        sample = (void *)((char *)sample + size);
    }

    return 0;
}

int ProfilerStop(struct Profiler *p)
{
    unsigned i;
    int rv;

    rv = contextSwitchCounterStop(&p->switches);
    if (rv != 0) {
        return rv;
    }

    for (i = 0; i < p->n_traces; i++) {
        rv = profilerTraceFsStop(p->traces[i]);
        if (rv != 0) {
            return rv;
        }
    }

    if (p->device->driver != FS_DRIVER_GENERIC) {
        for (i = 0; i < p->n_groups; i++) {
            rv = profilerEventGroupStop(&p->groups[i]);
            if (rv != 0) {
                return -1;
            }
        }
    }

    return 0;
}