forked from OpenCloudOS/perf-prof
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blktrace.c
509 lines (446 loc) · 14.9 KB
/
blktrace.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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/compiler.h>
#include <linux/rblist.h>
#include <api/fs/fs.h>
#include <monitor.h>
#include <tep.h>
#include <trace_helpers.h>
#include <stack_helpers.h>
typedef u64 sector_t;
static profiler blktrace;
enum block_tp {
BLOCK_GETRQ,
BLOCK_RQ_INSERT,
BLOCK_RQ_ISSUE,
BLOCK_RQ_COMPLETE,
BLOCK_MAX,
};
struct request_track {
struct rb_node rbnode;
enum block_tp tp;
dev_t dev;
sector_t sector;
unsigned int nr_sector;
u64 time;
};
struct block_iostat {
const char *name;
__u64 type;
__u64 min;
__u64 max;
__u64 n;
__u64 sum;
__u64 than;
};
static struct blktrace_ctx {
struct perf_evlist *evlist;
struct block_iostat stats[BLOCK_MAX];
dev_t dev;
int partition;
sector_t start_sector;
sector_t end_sector;
int max_name_len;
struct rblist rq_tracks;
struct env *env;
} ctx;
struct trace_block_getrq {
unsigned short common_type;// offset:0; size:2; signed:0;
unsigned char common_flags;// offset:2; size:1; signed:0;
unsigned char common_preempt_count;// offset:3; size:1; signed:0;
int common_pid;// offset:4; size:4; signed:1;
dev_t dev;// offset:8; size:4; signed:0;
sector_t sector;// offset:16; size:8; signed:0;
unsigned int nr_sector;// offset:24; size:4; signed:0;
char rwbs[8];// offset:28; size:8; signed:1;
char comm[16];// offset:36; size:16; signed:1;
};
struct trace_block_rq_insert {
unsigned short common_type;// offset:0; size:2; signed:0;
unsigned char common_flags;// offset:2; size:1; signed:0;
unsigned char common_preempt_count;// offset:3; size:1; signed:0;
int common_pid;// offset:4; size:4; signed:1;
dev_t dev;// offset:8; size:4; signed:0;
sector_t sector;// offset:16; size:8; signed:0;
unsigned int nr_sector;// offset:24; size:4; signed:0;
unsigned int bytes;// offset:28; size:4; signed:0;
char rwbs[8];// offset:32; size:8; signed:1;
char comm[16];// offset:40; size:16; signed:1;
//__data_loc char[] cmd; offset:56; size:4; signed:1;
};
struct trace_block_rq_issue {
unsigned short common_type;// offset:0; size:2; signed:0;
unsigned char common_flags;// offset:2; size:1; signed:0;
unsigned char common_preempt_count;// offset:3; size:1; signed:0;
int common_pid;// offset:4; size:4; signed:1;
dev_t dev;// offset:8; size:4; signed:0;
sector_t sector;// offset:16; size:8; signed:0;
unsigned int nr_sector;// offset:24; size:4; signed:0;
unsigned int bytes;// offset:28; size:4; signed:0;
char rwbs[8];// offset:32; size:8; signed:1;
char comm[16];// offset:40; size:16; signed:1;
//__data_loc char[] cmd; offset:56; size:4; signed:1;
};
struct trace_block_rq_complete {
unsigned short common_type;// offset:0; size:2; signed:0;
unsigned char common_flags;// offset:2; size:1; signed:0;
unsigned char common_preempt_count;// offset:3; size:1; signed:0;
int common_pid;// offset:4; size:4; signed:1;
dev_t dev;// offset:8; size:4; signed:0;
sector_t sector;// offset:16; size:8; signed:0;
unsigned int nr_sector;// offset:24; size:4; signed:0;
int error;// offset:28; size:4; signed:1;
char rwbs[8];// offset:32; size:8; signed:1;
//__data_loc char[] cmd; offset:40; size:4; signed:1;
};
// in linux/perf_event.h
// PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_RAW
struct sample_type_raw {
struct {
__u32 pid;
__u32 tid;
} tid_entry;
__u64 time;
struct {
__u32 cpu;
__u32 reserved;
} cpu_entry;
struct {
__u32 size;
union {
__u8 data[0];
unsigned short common_type;
struct trace_block_getrq getrq;
struct trace_block_rq_insert rq_insert;
struct trace_block_rq_issue rq_issue;
struct trace_block_rq_complete rq_complete;
} __packed;
} raw;
};
#define MINORBITS 20
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))
//linux fs/stat.c
static inline dev_t new_decode_dev(u32 dev)
{
unsigned major = (dev & 0xfff00) >> 8;
unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
return MKDEV(major, minor);
}
static int request_track_node_cmp(struct rb_node *rbn, const void *entry)
{
struct request_track *rq = container_of(rbn, struct request_track, rbnode);
const struct request_track *r = entry;
if (rq->dev > r->dev)
return 1;
else if (rq->dev < r->dev)
return -1;
if (rq->sector >= r->sector + r->nr_sector)
return 1;
else if (rq->sector + rq->nr_sector <= r->sector)
return -1;
else
return 0;
}
static struct rb_node *request_track_node_new(struct rblist *rlist, const void *new_entry)
{
const struct request_track *r = new_entry;
struct request_track *rq = malloc(sizeof(*rq));
if (rq) {
*rq = *r;
RB_CLEAR_NODE(&rq->rbnode);
return &rq->rbnode;
} else
return NULL;
}
static void request_track_node_delete(struct rblist *rblist, struct rb_node *rb_node)
{
struct request_track *rq = container_of(rb_node, struct request_track, rbnode);
free(rq);
}
static void iostat_reset(void)
{
int i;
for (i = 0; i < BLOCK_MAX; i ++) {
ctx.stats[i].min = ~0UL;
ctx.stats[i].max = 0UL;
ctx.stats[i].n = 0UL;
ctx.stats[i].sum = 0UL;
ctx.stats[i].than = 0UL;
}
}
static inline unsigned get_dev_major(u32 dev)
{
return (dev & 0xfff00) >> 8;
}
static inline unsigned get_dev_minor(u32 dev)
{
return (dev & 0xff) | ((dev >> 12) & 0xfff00);
}
static int monitor_ctx_init(struct env *env)
{
struct stat st;
char path[4096];
unsigned int major, minor;
char *buf;
size_t len;
if (!env->device)
return -1;
if (stat(env->device, &st) < 0)
return -1;
ctx.dev = new_decode_dev((u32)st.st_rdev);
major = get_dev_major((u32)st.st_rdev);
minor = get_dev_minor((u32)st.st_rdev);
memset(path, 0, 4096);
sprintf(path, "dev/block/%u:%u/partition", major, minor);
if (sysfs__read_str(path, &buf, &len) < 0)
ctx.partition = 0;
else {
ctx.partition = atoi(buf);
ctx.dev -= ctx.partition;
free(buf);
memset(path, 0, 4096);
sprintf(path, "dev/block/%u:%u/start", major, minor);
if (sysfs__read_str(path, &buf, &len) < 0)
return -1;
else {
ctx.start_sector = atol(buf);
free(buf);
}
memset(path, 0, 4096);
sprintf(path, "dev/block/%u:%u/size", major, minor);
if (sysfs__read_str(path, &buf, &len) < 0)
return -1;
else {
ctx.end_sector = ctx.start_sector + atol(buf);
free(buf);
}
}
iostat_reset();
rblist__init(&ctx.rq_tracks);
ctx.rq_tracks.node_cmp = request_track_node_cmp;
ctx.rq_tracks.node_new = request_track_node_new;
ctx.rq_tracks.node_delete = request_track_node_delete;
tep__ref();
ctx.env = env;
return 0;
}
static void monitor_ctx_exit(void)
{
rblist__exit(&ctx.rq_tracks);
tep__unref();
}
static struct perf_evsel *add_tp_event(struct perf_evlist *evlist, const char *sys, const char *name, int i)
{
struct perf_event_attr attr = {
.type = PERF_TYPE_TRACEPOINT,
.config = 0,
.size = sizeof(struct perf_event_attr),
.sample_period = 1,
.sample_type = PERF_SAMPLE_TID | PERF_SAMPLE_TIME | PERF_SAMPLE_CPU | PERF_SAMPLE_RAW,
.read_format = 0,
.pinned = 1,
.disabled = 1,
.wakeup_events = 1,
};
struct perf_evsel *evsel;
int id;
id = tep__event_id(sys, name);
if (id < 0)
return NULL;
reduce_wakeup_times(&blktrace, &attr);
attr.config = id;
evsel = perf_evsel__new(&attr);
if (!evsel) {
return NULL;
}
perf_evlist__add(evlist, evsel);
ctx.stats[i].name = name;
ctx.stats[i].type = id;
if (strlen(name) > ctx.max_name_len)
ctx.max_name_len = strlen(name);
return evsel;
}
static int blktrace_init(struct perf_evlist *evlist, struct env *env)
{
if (monitor_ctx_init(env) < 0)
return -1;
add_tp_event(evlist, "block", "block_getrq", BLOCK_GETRQ);
add_tp_event(evlist, "block", "block_rq_insert", BLOCK_RQ_INSERT);
add_tp_event(evlist, "block", "block_rq_issue", BLOCK_RQ_ISSUE);
add_tp_event(evlist, "block", "block_rq_complete", BLOCK_RQ_COMPLETE);
ctx.evlist = evlist;
return 0;
}
static int blktrace_filter(struct perf_evlist *evlist, struct env *env)
{
char filter[256];
struct perf_evsel *evsel;
int err;
if (ctx.partition)
snprintf(filter, sizeof(filter), "dev==%u && sector>=%lu && sector<=%lu", \
(unsigned int)ctx.dev, ctx.start_sector, ctx.end_sector);
else
snprintf(filter, sizeof(filter), "dev==%u", (unsigned int)ctx.dev);
if (ctx.env->verbose)
printf("%s\n", filter);
perf_evlist__for_each_evsel(evlist, evsel) {
err = perf_evsel__apply_filter(evsel, filter);
if (err < 0)
return err;
}
return 0;
}
static void blktrace_interval(void)
{
int i;
bool than = !!ctx.env->greater_than;
print_time(stdout);
printf("\n");
printf("%*s => %-*s %8s %16s %12s %12s %12s", ctx.max_name_len, "start", ctx.max_name_len, "end", "reqs",
ctx.env->tsc ? "total(kcyc)" : "total(us)",
ctx.env->tsc ? "min(kcyc)" : "min(us)",
ctx.env->tsc ? "avg(kcyc)" : "avg(us)",
ctx.env->tsc ? "max(kcyc)" : "max(us)");
if (than)
printf(" than(reqs)\n");
else
printf("\n");
for (i=0; i<ctx.max_name_len; i++) printf("-");
printf(" ");
for (i=0; i<ctx.max_name_len; i++) printf("-");
printf(" %8s %16s %12s %12s %12s",
"--------", "----------------", "------------", "------------", "------------");
if (than)
printf(" -------------\n");
else
printf("\n");
for (i = 1; i < BLOCK_MAX; i++) {
struct block_iostat *iostat1 = &ctx.stats[i-1];
struct block_iostat *iostat = &ctx.stats[i];
printf("%*s => %-*s %8llu %16.3f %12.3f %12.3f %12.3f",
ctx.max_name_len, iostat1->name,
ctx.max_name_len, iostat->name,
iostat->n, iostat->sum/1000.0, iostat->n ? iostat->min/1000.0 : 0.0,
iostat->n ? iostat->sum/iostat->n/1000.0 : 0.0, iostat->max/1000.0);
if (than)
if (iostat->than && isatty(1))
printf(" \033[31;1m%6llu (%3llu%s)\033[0m\n", iostat->than, iostat->than * 100 / (iostat->n ? iostat->n : 1), "%");
else
printf(" %6llu (%3llu%s)\n", iostat->than, iostat->than * 100 / (iostat->n ? iostat->n : 1), "%");
else
printf("\n");
}
iostat_reset();
}
static void blktrace_exit(struct perf_evlist *evlist)
{
blktrace_interval();
monitor_ctx_exit();
}
#define IF(i, trace) \
if (common_type == ctx.stats[i].type) { \
r.tp = i; \
r.dev = data->raw.trace.dev; \
r.sector = data->raw.trace.sector; \
r.nr_sector = data->raw.trace.nr_sector; \
}
static void blktrace_sample(union perf_event *event, int instance)
{
struct sample_type_raw *data = (void *)event->sample.array;
void *raw = data->raw.data;
int size = data->raw.size;
unsigned short common_type = data->raw.common_type;
struct request_track *rq, *prev_rq, r;
struct block_iostat *iostat;
struct rb_node *rbn = NULL;
u64 delta;
const char *print = NULL;
int verbose = ctx.env->verbose;
r.time = data->time;
IF(BLOCK_GETRQ, getrq)
else IF(BLOCK_RQ_INSERT, rq_insert)
else IF(BLOCK_RQ_ISSUE, rq_issue)
else IF(BLOCK_RQ_COMPLETE, rq_complete)
else return;
// sector == -1: flush req
if (r.sector == (sector_t)-1)
return;
iostat = &ctx.stats[r.tp];
rbn = rblist__find(&ctx.rq_tracks, &r);
if (rbn) {
prev_rq = container_of(rbn, struct request_track, rbnode);
if (r.tp == BLOCK_GETRQ)
print = "EXIST";
else if (prev_rq->tp != r.tp - 1) {
if (r.tp == BLOCK_RQ_ISSUE)
print = "BYPASS_INSERT";
else
print = "LOST";
}
delta = r.time - prev_rq->time;
if (delta < iostat->min)
iostat->min = delta;
if (delta > iostat->max)
iostat->max = delta;
if (ctx.env->greater_than && delta > ctx.env->greater_than)
iostat->than ++;
iostat->n ++;
iostat->sum += delta;
if (ctx.env->greater_than &&
delta > ctx.env->greater_than) {
print = "GREATER_THAN";
verbose = VERBOSE_NOTICE;
}
if (r.tp != BLOCK_RQ_COMPLETE) {
rq = prev_rq;
rq->tp = r.tp;
rq->dev = r.dev;
rq->sector = r.sector;
rq->nr_sector = r.nr_sector;
rq->time = r.time;
} else
rblist__remove_node(&ctx.rq_tracks, rbn);
}
else if (r.tp != BLOCK_RQ_COMPLETE)
rblist__add_node(&ctx.rq_tracks, &r);
if (verbose &&
(print || verbose >= VERBOSE_EVENT)) {
tep__update_comm(NULL, data->tid_entry.tid);
print_time(stdout);
if (print)
printf("%s", print);
tep__print_event(data->time/1000, data->cpu_entry.cpu, raw, size);
}
}
static const char *blktrace_desc[] = PROFILER_DESC("blktrace",
"[OPTION...] -d device [--than ns]",
"Track IO latency on block devices.", "",
"TRACEPOINT", "",
" block:block_getrq, block:block_rq_insert, block:block_rq_issue, block:block_rq_complete", "",
"EXAMPLES", "",
" "PROGRAME" blktrace -d /dev/sda -i 1000",
" "PROGRAME" blktrace -d /dev/sda -i 1000 --than 10ms");
static const char *blktrace_argv[] = PROFILER_ARGV("blktrace",
"OPTION:",
"interval", "output", "order", "order-mem", "mmap-pages", "exit-N", "tsc", "tsc-offset",
"version", "verbose", "quiet", "help",
PROFILER_ARGV_PROFILER, "device", "than");
static profiler blktrace = {
.name = "blktrace",
.desc = blktrace_desc,
.argv = blktrace_argv,
.pages = 8,
.order = true,
.init = blktrace_init,
.filter = blktrace_filter,
.deinit = blktrace_exit,
.interval = blktrace_interval,
.sample = blktrace_sample,
};
PROFILER_REGISTER(blktrace)