Commit | Line | Data |
---|---|---|
9e066e23 CM |
1 | /* |
2 | * Copyright (C) 2012 Fusion-io | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | * | |
17 | * Parts of this file were imported from Jens Axboe's blktrace sources (also GPL) | |
18 | */ | |
19 | #include <sys/types.h> | |
20 | #include <sys/stat.h> | |
21 | #include <fcntl.h> | |
22 | #include <unistd.h> | |
23 | #include <stdlib.h> | |
24 | #include <stdio.h> | |
25 | #include <math.h> | |
26 | #include <inttypes.h> | |
27 | #include <string.h> | |
28 | #include <asm/types.h> | |
29 | #include <errno.h> | |
30 | #include <sys/mman.h> | |
31 | #include <time.h> | |
32 | #include <math.h> | |
33 | ||
34 | #include "plot.h" | |
35 | #include "blkparse.h" | |
36 | #include "list.h" | |
37 | #include "tracers.h" | |
38 | ||
39 | #define IO_HASH_TABLE_BITS 11 | |
40 | #define IO_HASH_TABLE_SIZE (1 << IO_HASH_TABLE_BITS) | |
41 | static struct list_head io_hash_table[IO_HASH_TABLE_SIZE]; | |
42 | static u64 ios_in_flight = 0; | |
43 | ||
44 | ||
45 | /* | |
46 | * Trace categories | |
47 | */ | |
48 | enum { | |
49 | BLK_TC_READ = 1 << 0, /* reads */ | |
50 | BLK_TC_WRITE = 1 << 1, /* writes */ | |
51 | BLK_TC_FLUSH = 1 << 2, /* flush */ | |
52 | BLK_TC_SYNC = 1 << 3, /* sync */ | |
53 | BLK_TC_QUEUE = 1 << 4, /* queueing/merging */ | |
54 | BLK_TC_REQUEUE = 1 << 5, /* requeueing */ | |
55 | BLK_TC_ISSUE = 1 << 6, /* issue */ | |
56 | BLK_TC_COMPLETE = 1 << 7, /* completions */ | |
57 | BLK_TC_FS = 1 << 8, /* fs requests */ | |
58 | BLK_TC_PC = 1 << 9, /* pc requests */ | |
59 | BLK_TC_NOTIFY = 1 << 10, /* special message */ | |
60 | BLK_TC_AHEAD = 1 << 11, /* readahead */ | |
61 | BLK_TC_META = 1 << 12, /* metadata */ | |
62 | BLK_TC_DISCARD = 1 << 13, /* discard requests */ | |
63 | BLK_TC_DRV_DATA = 1 << 14, /* binary driver data */ | |
64 | BLK_TC_FUA = 1 << 15, /* fua requests */ | |
65 | ||
66 | BLK_TC_END = 1 << 15, /* we've run out of bits! */ | |
67 | }; | |
68 | ||
69 | #define BLK_TC_SHIFT (16) | |
70 | #define BLK_TC_ACT(act) ((act) << BLK_TC_SHIFT) | |
71 | #define BLK_DATADIR(a) (((a) >> BLK_TC_SHIFT) & (BLK_TC_READ | BLK_TC_WRITE)) | |
72 | ||
73 | /* | |
74 | * Basic trace actions | |
75 | */ | |
76 | enum { | |
77 | __BLK_TA_QUEUE = 1, /* queued */ | |
78 | __BLK_TA_BACKMERGE, /* back merged to existing rq */ | |
79 | __BLK_TA_FRONTMERGE, /* front merge to existing rq */ | |
80 | __BLK_TA_GETRQ, /* allocated new request */ | |
81 | __BLK_TA_SLEEPRQ, /* sleeping on rq allocation */ | |
82 | __BLK_TA_REQUEUE, /* request requeued */ | |
83 | __BLK_TA_ISSUE, /* sent to driver */ | |
84 | __BLK_TA_COMPLETE, /* completed by driver */ | |
85 | __BLK_TA_PLUG, /* queue was plugged */ | |
86 | __BLK_TA_UNPLUG_IO, /* queue was unplugged by io */ | |
87 | __BLK_TA_UNPLUG_TIMER, /* queue was unplugged by timer */ | |
88 | __BLK_TA_INSERT, /* insert request */ | |
89 | __BLK_TA_SPLIT, /* bio was split */ | |
90 | __BLK_TA_BOUNCE, /* bio was bounced */ | |
91 | __BLK_TA_REMAP, /* bio was remapped */ | |
92 | __BLK_TA_ABORT, /* request aborted */ | |
93 | __BLK_TA_DRV_DATA, /* binary driver data */ | |
94 | }; | |
95 | ||
1582ecc9 JK |
96 | #define BLK_TA_MASK ((1 << BLK_TC_SHIFT) - 1) |
97 | ||
9e066e23 CM |
98 | /* |
99 | * Notify events. | |
100 | */ | |
101 | enum blktrace_notify { | |
102 | __BLK_TN_PROCESS = 0, /* establish pid/name mapping */ | |
103 | __BLK_TN_TIMESTAMP, /* include system clock */ | |
104 | __BLK_TN_MESSAGE, /* Character string message */ | |
105 | }; | |
106 | ||
107 | /* | |
108 | * Trace actions in full. Additionally, read or write is masked | |
109 | */ | |
110 | #define BLK_TA_QUEUE (__BLK_TA_QUEUE | BLK_TC_ACT(BLK_TC_QUEUE)) | |
111 | #define BLK_TA_BACKMERGE (__BLK_TA_BACKMERGE | BLK_TC_ACT(BLK_TC_QUEUE)) | |
112 | #define BLK_TA_FRONTMERGE (__BLK_TA_FRONTMERGE | BLK_TC_ACT(BLK_TC_QUEUE)) | |
113 | #define BLK_TA_GETRQ (__BLK_TA_GETRQ | BLK_TC_ACT(BLK_TC_QUEUE)) | |
114 | #define BLK_TA_SLEEPRQ (__BLK_TA_SLEEPRQ | BLK_TC_ACT(BLK_TC_QUEUE)) | |
115 | #define BLK_TA_REQUEUE (__BLK_TA_REQUEUE | BLK_TC_ACT(BLK_TC_REQUEUE)) | |
116 | #define BLK_TA_ISSUE (__BLK_TA_ISSUE | BLK_TC_ACT(BLK_TC_ISSUE)) | |
117 | #define BLK_TA_COMPLETE (__BLK_TA_COMPLETE| BLK_TC_ACT(BLK_TC_COMPLETE)) | |
118 | #define BLK_TA_PLUG (__BLK_TA_PLUG | BLK_TC_ACT(BLK_TC_QUEUE)) | |
119 | #define BLK_TA_UNPLUG_IO (__BLK_TA_UNPLUG_IO | BLK_TC_ACT(BLK_TC_QUEUE)) | |
120 | #define BLK_TA_UNPLUG_TIMER (__BLK_TA_UNPLUG_TIMER | BLK_TC_ACT(BLK_TC_QUEUE)) | |
121 | #define BLK_TA_INSERT (__BLK_TA_INSERT | BLK_TC_ACT(BLK_TC_QUEUE)) | |
122 | #define BLK_TA_SPLIT (__BLK_TA_SPLIT) | |
123 | #define BLK_TA_BOUNCE (__BLK_TA_BOUNCE) | |
124 | #define BLK_TA_REMAP (__BLK_TA_REMAP | BLK_TC_ACT(BLK_TC_QUEUE)) | |
125 | #define BLK_TA_ABORT (__BLK_TA_ABORT | BLK_TC_ACT(BLK_TC_QUEUE)) | |
126 | #define BLK_TA_DRV_DATA (__BLK_TA_DRV_DATA | BLK_TC_ACT(BLK_TC_DRV_DATA)) | |
127 | ||
128 | #define BLK_TN_PROCESS (__BLK_TN_PROCESS | BLK_TC_ACT(BLK_TC_NOTIFY)) | |
129 | #define BLK_TN_TIMESTAMP (__BLK_TN_TIMESTAMP | BLK_TC_ACT(BLK_TC_NOTIFY)) | |
130 | #define BLK_TN_MESSAGE (__BLK_TN_MESSAGE | BLK_TC_ACT(BLK_TC_NOTIFY)) | |
131 | ||
132 | #define BLK_IO_TRACE_MAGIC 0x65617400 | |
133 | #define BLK_IO_TRACE_VERSION 0x07 | |
134 | /* | |
135 | * The trace itself | |
136 | */ | |
137 | struct blk_io_trace { | |
138 | __u32 magic; /* MAGIC << 8 | version */ | |
139 | __u32 sequence; /* event number */ | |
140 | __u64 time; /* in nanoseconds */ | |
141 | __u64 sector; /* disk offset */ | |
142 | __u32 bytes; /* transfer length */ | |
143 | __u32 action; /* what happened */ | |
144 | __u32 pid; /* who did it */ | |
145 | __u32 device; /* device identifier (dev_t) */ | |
146 | __u32 cpu; /* on what cpu did it happen */ | |
147 | __u16 error; /* completion error */ | |
148 | __u16 pdu_len; /* length of data after this trace */ | |
149 | }; | |
150 | ||
151 | struct pending_io { | |
152 | /* sector offset of this IO */ | |
153 | u64 sector; | |
154 | ||
155 | /* time this IO was dispatched */ | |
156 | u64 dispatch_time; | |
157 | /* time this IO was finished */ | |
158 | u64 completion_time; | |
159 | struct list_head hash_list; | |
160 | }; | |
161 | ||
162 | #define MINORBITS 20 | |
163 | #define MINORMASK ((1 << MINORBITS) - 1) | |
164 | #define SECONDS(x) ((unsigned long long)(x) / 1000000000) | |
165 | #define NANO_SECONDS(x) ((unsigned long long)(x) % 1000000000) | |
166 | #define DOUBLE_TO_NANO_ULL(d) ((unsigned long long)((d) * 1000000000)) | |
167 | #define CHECK_MAGIC(t) (((t)->magic & 0xffffff00) == BLK_IO_TRACE_MAGIC) | |
168 | ||
169 | void init_io_hash_table(void) | |
170 | { | |
171 | int i; | |
172 | struct list_head *head; | |
173 | ||
174 | for (i = 0; i < IO_HASH_TABLE_SIZE; i++) { | |
175 | head = io_hash_table + i; | |
176 | INIT_LIST_HEAD(head); | |
177 | } | |
178 | } | |
179 | ||
180 | /* taken from the kernel hash.h */ | |
181 | static inline u64 hash_sector(u64 val) | |
182 | { | |
183 | u64 hash = val; | |
184 | ||
185 | /* Sigh, gcc can't optimise this alone like it does for 32 bits. */ | |
186 | u64 n = hash; | |
187 | n <<= 18; | |
188 | hash -= n; | |
189 | n <<= 33; | |
190 | hash -= n; | |
191 | n <<= 3; | |
192 | hash += n; | |
193 | n <<= 3; | |
194 | hash -= n; | |
195 | n <<= 4; | |
196 | hash += n; | |
197 | n <<= 2; | |
198 | hash += n; | |
199 | ||
200 | /* High bits are more random, so use them. */ | |
201 | return hash >> (64 - IO_HASH_TABLE_BITS); | |
202 | } | |
203 | ||
204 | static int hash_table_insert(struct pending_io *ins_pio) | |
205 | { | |
206 | u64 sector = ins_pio->sector; | |
207 | int slot = hash_sector(sector); | |
208 | struct list_head *head; | |
209 | struct pending_io *pio; | |
210 | ||
211 | head = io_hash_table + slot; | |
212 | list_for_each_entry(pio, head, hash_list) { | |
213 | if (pio->sector == sector) | |
214 | return -EEXIST; | |
215 | } | |
216 | list_add_tail(&ins_pio->hash_list, head); | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static struct pending_io *hash_table_search(u64 sector) | |
221 | { | |
222 | int slot = hash_sector(sector); | |
223 | struct list_head *head; | |
224 | struct pending_io *pio; | |
225 | ||
226 | head = io_hash_table + slot; | |
227 | list_for_each_entry(pio, head, hash_list) { | |
228 | if (pio->sector == sector) | |
229 | return pio; | |
230 | } | |
231 | return NULL; | |
232 | } | |
233 | ||
234 | static int hash_dispatched_io(struct blk_io_trace *io) | |
235 | { | |
236 | struct pending_io *pio; | |
237 | int ret; | |
238 | ||
239 | pio = calloc(1, sizeof(*pio)); | |
240 | pio->sector = io->sector; | |
241 | pio->dispatch_time = io->time; | |
242 | ||
243 | ret = hash_table_insert(pio); | |
244 | if (ret == -EEXIST) { | |
245 | /* crud, the IO isn't here */ | |
246 | free(pio); | |
247 | } | |
248 | return ret; | |
249 | } | |
250 | ||
251 | static struct pending_io *hash_completed_io(struct blk_io_trace *io) | |
252 | { | |
253 | struct pending_io *pio; | |
254 | ||
255 | pio = hash_table_search(io->sector); | |
256 | ||
257 | if (!pio) | |
258 | return NULL; | |
259 | return pio; | |
260 | } | |
261 | ||
262 | static void handle_notify(struct trace *trace) | |
263 | { | |
264 | struct blk_io_trace *io = trace->io; | |
265 | void *payload = (char *)io + sizeof(*io); | |
266 | u32 two32[2]; | |
267 | ||
268 | ||
269 | if (io->action != BLK_TN_TIMESTAMP) | |
270 | return; | |
271 | ||
272 | if (io->pdu_len != sizeof(two32)) | |
273 | return; | |
274 | ||
275 | memcpy(two32, payload, sizeof(two32)); | |
276 | trace->start_timestamp = io->time; | |
277 | trace->abs_start_time.tv_sec = two32[0]; | |
278 | trace->abs_start_time.tv_nsec = two32[1]; | |
279 | if (trace->abs_start_time.tv_nsec < 0) { | |
280 | trace->abs_start_time.tv_sec--; | |
281 | trace->abs_start_time.tv_nsec += 1000000000; | |
282 | } | |
283 | } | |
284 | ||
285 | int next_record(struct trace *trace) | |
286 | { | |
287 | int skip = trace->io->pdu_len; | |
288 | u64 offset; | |
289 | ||
290 | trace->cur += sizeof(*trace->io) + skip; | |
291 | offset = trace->cur - trace->start; | |
292 | if (offset >= trace->len) | |
293 | return 1; | |
294 | ||
295 | trace->io = (struct blk_io_trace *)trace->cur; | |
296 | return 0; | |
297 | } | |
298 | ||
299 | void first_record(struct trace *trace) | |
300 | { | |
301 | trace->cur = trace->start; | |
302 | trace->io = (struct blk_io_trace *)trace->cur; | |
303 | } | |
304 | ||
bfb0e441 CM |
305 | int is_io_event(struct blk_io_trace *test) |
306 | { | |
307 | char *message; | |
308 | if (!(test->action & BLK_TC_ACT(BLK_TC_NOTIFY))) | |
309 | return 1; | |
310 | if (test->action == BLK_TN_MESSAGE) { | |
311 | int len = test->pdu_len; | |
312 | if (len < 3) | |
313 | return 0; | |
314 | message = (char *)(test + 1); | |
315 | if (strncmp(message, "fio ", 4) == 0) { | |
316 | return 1; | |
317 | } | |
318 | } | |
319 | return 0; | |
320 | } | |
321 | ||
9e066e23 CM |
322 | u64 find_last_time(struct trace *trace) |
323 | { | |
324 | char *p = trace->start + trace->len; | |
325 | struct blk_io_trace *test; | |
326 | int search_len = 0; | |
327 | u64 found = 0; | |
328 | ||
329 | if (trace->len < sizeof(*trace->io)) | |
330 | return 0; | |
331 | p -= sizeof(*trace->io); | |
332 | while (p >= trace->start) { | |
333 | test = (struct blk_io_trace *)p; | |
bfb0e441 | 334 | if (CHECK_MAGIC(test) && is_io_event(test)) { |
9e066e23 CM |
335 | u64 offset = p - trace->start; |
336 | if (offset + sizeof(*test) + test->pdu_len == trace->len) { | |
337 | return test->time; | |
338 | } | |
339 | } | |
340 | p--; | |
341 | search_len++; | |
342 | if (search_len > 8192) { | |
343 | break; | |
344 | } | |
345 | } | |
346 | ||
347 | /* searching backwards didn't work out, we'll have to scan the file */ | |
348 | first_record(trace); | |
349 | while (1) { | |
bfb0e441 | 350 | if (is_io_event(trace->io)) |
9e066e23 CM |
351 | found = trace->io->time; |
352 | if (next_record(trace)) | |
353 | break; | |
354 | } | |
355 | first_record(trace); | |
356 | return found; | |
357 | } | |
358 | ||
bfb0e441 CM |
359 | int parse_fio_bank_message(struct trace *trace, u64 *bank_ret, u64 *offset_ret, |
360 | u64 *num_banks_ret) | |
361 | { | |
362 | char *s; | |
363 | char *next; | |
364 | char *message; | |
365 | struct blk_io_trace *test = trace->io; | |
366 | int len = test->pdu_len; | |
367 | u64 bank; | |
368 | u64 offset; | |
369 | u64 num_banks; | |
370 | ||
371 | if (!(test->action & BLK_TC_ACT(BLK_TC_NOTIFY))) | |
372 | return -1; | |
373 | if (test->action != BLK_TN_MESSAGE) | |
374 | return -1; | |
375 | ||
376 | /* the message is fio rw bank offset num_banks */ | |
377 | if (len < 3) | |
378 | return -1; | |
379 | message = (char *)(test + 1); | |
380 | if (strncmp(message, "fio r ", 6) != 0) | |
381 | return -1; | |
382 | ||
383 | message = strndup(message, len); | |
384 | s = strchr(message, ' '); | |
385 | if (!s) | |
386 | goto out; | |
387 | s++; | |
388 | s = strchr(s, ' '); | |
389 | if (!s) | |
390 | goto out; | |
391 | ||
392 | bank = strtoll(s, &next, 10); | |
393 | if (s == next) | |
394 | goto out; | |
395 | s = next; | |
396 | ||
397 | offset = strtoll(s, &next, 10); | |
398 | if (s == next) | |
399 | goto out; | |
400 | s = next; | |
401 | ||
402 | num_banks = strtoll(s, &next, 10); | |
403 | if (s == next) | |
404 | goto out; | |
405 | ||
406 | *bank_ret = bank; | |
407 | *offset_ret = offset; | |
408 | *num_banks_ret = num_banks; | |
409 | ||
410 | return 0; | |
411 | out: | |
412 | free(message); | |
413 | return -1; | |
414 | } | |
415 | ||
9b9fa04b JK |
416 | void find_extreme_offsets(struct trace *trace, u64 *min_ret, u64 *max_ret, u64 *max_bank_ret, |
417 | u64 *max_offset_ret) | |
9e066e23 CM |
418 | { |
419 | u64 found = 0; | |
9b9fa04b | 420 | u64 max = 0, min = ~(u64)0; |
bfb0e441 CM |
421 | u64 max_bank = 0; |
422 | u64 max_bank_offset = 0; | |
423 | u64 num_banks = 0; | |
9e066e23 CM |
424 | first_record(trace); |
425 | while (1) { | |
426 | if (!(trace->io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) { | |
427 | found = trace->io->sector << 9; | |
9b9fa04b JK |
428 | if (found < min) |
429 | min = found; | |
9e066e23 | 430 | |
9b9fa04b JK |
431 | found += trace->io->bytes; |
432 | if (max < found) | |
9e066e23 | 433 | max = found; |
bfb0e441 CM |
434 | } else { |
435 | u64 bank; | |
436 | u64 offset; | |
437 | if (!parse_fio_bank_message(trace, &bank, | |
438 | &offset, &num_banks)) { | |
439 | if (bank > max_bank) | |
440 | max_bank = bank; | |
441 | if (offset > max_bank_offset) | |
442 | max_bank_offset = offset; | |
443 | } | |
9e066e23 CM |
444 | } |
445 | if (next_record(trace)) | |
446 | break; | |
447 | } | |
448 | first_record(trace); | |
9b9fa04b | 449 | *min_ret = min; |
bfb0e441 CM |
450 | *max_ret = max; |
451 | *max_bank_ret = max_bank; | |
452 | *max_offset_ret = max_bank_offset; | |
9e066e23 CM |
453 | } |
454 | ||
9b9fa04b | 455 | int filter_outliers(struct trace *trace, u64 min_offset, u64 max_offset, |
9e066e23 CM |
456 | u64 *yzoom_min, u64 *yzoom_max) |
457 | { | |
458 | int hits[11]; | |
459 | u64 max_per_bucket[11]; | |
9b9fa04b JK |
460 | u64 min_per_bucket[11]; |
461 | u64 bytes_per_bucket = (max_offset - min_offset + 1) / 10; | |
9e066e23 CM |
462 | int slot; |
463 | int fat_count = 0; | |
464 | ||
465 | memset(hits, 0, sizeof(int) * 11); | |
466 | memset(max_per_bucket, 0, sizeof(u64) * 11); | |
9b9fa04b | 467 | memset(min_per_bucket, 0xff, sizeof(u64) * 11); |
9e066e23 CM |
468 | first_record(trace); |
469 | while (1) { | |
41fdf407 JK |
470 | if (!(trace->io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) && |
471 | (trace->io->action & BLK_TA_MASK) == __BLK_TA_QUEUE) { | |
9b9fa04b JK |
472 | u64 off = (trace->io->sector << 9) - min_offset; |
473 | ||
474 | slot = (int)(off / bytes_per_bucket); | |
475 | hits[slot]++; | |
476 | if (off < min_per_bucket[slot]) | |
477 | min_per_bucket[slot] = off; | |
478 | ||
479 | off += trace->io->bytes; | |
480 | slot = (int)(off / bytes_per_bucket); | |
9e066e23 | 481 | hits[slot]++; |
9b9fa04b JK |
482 | if (off > max_per_bucket[slot]) |
483 | max_per_bucket[slot] = off; | |
9e066e23 CM |
484 | } |
485 | if (next_record(trace)) | |
486 | break; | |
487 | } | |
488 | first_record(trace); | |
489 | for (slot = 0; slot < 11; slot++) { | |
490 | if (hits[slot] > fat_count) { | |
491 | fat_count = hits[slot]; | |
492 | } | |
493 | } | |
494 | ||
495 | *yzoom_max = max_offset; | |
496 | for (slot = 10; slot >= 0; slot--) { | |
497 | double d = hits[slot]; | |
498 | ||
499 | if (d >= (double)fat_count * .05) { | |
9b9fa04b | 500 | *yzoom_max = max_per_bucket[slot] + min_offset; |
9e066e23 CM |
501 | break; |
502 | } | |
503 | } | |
504 | ||
9b9fa04b | 505 | *yzoom_min = min_offset; |
9e066e23 CM |
506 | for (slot = 0; slot < 10; slot++) { |
507 | double d = hits[slot]; | |
508 | ||
509 | if (d >= (double)fat_count * .05) { | |
9b9fa04b | 510 | *yzoom_min = min_per_bucket[slot] + min_offset; |
9e066e23 CM |
511 | break; |
512 | } | |
513 | } | |
514 | return 0; | |
515 | } | |
516 | ||
517 | static char *find_trace_file(char *filename) | |
518 | { | |
519 | int ret; | |
520 | struct stat st; | |
521 | char line[1024]; | |
522 | char *dot; | |
523 | char *try; | |
524 | ||
525 | ret = stat(filename, &st); | |
526 | if (ret == 0) | |
527 | return strdup(filename); | |
528 | ||
529 | snprintf(line, 1024, "%s.%s", filename, "dump"); | |
e199d546 | 530 | ret = stat(line, &st); |
9e066e23 CM |
531 | if (ret == 0) |
532 | return strdup(line); | |
533 | ||
534 | try = strdup(filename); | |
535 | dot = strrchr(try, '.'); | |
536 | if (!dot || strcmp(".dump", dot) != 0) { | |
537 | if (dot) | |
538 | *dot = '\0'; | |
539 | snprintf(line, 1024, "%s%s", try, ".blktrace.0"); | |
540 | ret = stat(line, &st); | |
541 | if (ret == 0) { | |
542 | blktrace_to_dump(try); | |
543 | snprintf(line, 1024, "%s.%s", try, "dump"); | |
544 | ret = stat(line, &st); | |
545 | if (ret == 0) { | |
546 | free(try); | |
547 | return strdup(line); | |
548 | } | |
549 | } | |
550 | } | |
551 | free(try); | |
552 | return NULL; | |
553 | } | |
554 | struct trace *open_trace(char *filename) | |
555 | { | |
556 | int fd; | |
557 | char *p; | |
558 | struct stat st; | |
559 | int ret; | |
560 | struct trace *trace; | |
561 | char *found_filename; | |
562 | ||
563 | trace = calloc(1, sizeof(*trace)); | |
564 | if (!trace) { | |
565 | fprintf(stderr, "unable to allocate memory for trace\n"); | |
566 | return NULL; | |
567 | } | |
568 | ||
569 | found_filename = find_trace_file(filename); | |
570 | if (!found_filename) { | |
571 | fprintf(stderr, "Unable to find trace file %s\n", filename); | |
572 | goto fail; | |
573 | } | |
9e066e23 CM |
574 | filename = found_filename; |
575 | ||
576 | fd = open(filename, O_RDONLY); | |
577 | if (fd < 0) { | |
578 | fprintf(stderr, "Unable to open trace file %s err %s\n", filename, strerror(errno)); | |
579 | goto fail; | |
580 | } | |
581 | ret = fstat(fd, &st); | |
582 | if (ret < 0) { | |
583 | fprintf(stderr, "stat failed on %s err %s\n", filename, strerror(errno)); | |
584 | goto fail_fd; | |
585 | } | |
586 | p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
587 | if (p == MAP_FAILED) { | |
588 | fprintf(stderr, "Unable to mmap trace file %s, err %s\n", filename, strerror(errno)); | |
589 | goto fail_fd; | |
590 | } | |
591 | trace->fd = fd; | |
592 | trace->len = st.st_size; | |
593 | trace->start = p; | |
594 | trace->cur = p; | |
595 | trace->io = (struct blk_io_trace *)p; | |
596 | return trace; | |
597 | ||
598 | fail_fd: | |
599 | close(fd); | |
600 | fail: | |
601 | free(trace); | |
602 | return NULL; | |
603 | } | |
604 | static inline int tput_event(struct trace *trace) | |
605 | { | |
606 | if (trace->found_completion) | |
607 | return __BLK_TA_COMPLETE; | |
608 | if (trace->found_issue) | |
609 | return __BLK_TA_ISSUE; | |
610 | if (trace->found_queue) | |
611 | return __BLK_TA_QUEUE; | |
612 | ||
613 | return __BLK_TA_COMPLETE; | |
614 | } | |
615 | ||
616 | static inline int io_event(struct trace *trace) | |
617 | { | |
618 | if (trace->found_queue) | |
619 | return __BLK_TA_QUEUE; | |
620 | if (trace->found_issue) | |
621 | return __BLK_TA_ISSUE; | |
622 | if (trace->found_completion) | |
623 | return __BLK_TA_COMPLETE; | |
624 | ||
625 | return __BLK_TA_COMPLETE; | |
626 | } | |
627 | ||
628 | void add_tput(struct trace *trace, struct graph_line_data *gld) | |
629 | { | |
630 | struct blk_io_trace *io = trace->io; | |
1582ecc9 | 631 | int action = io->action & BLK_TA_MASK; |
9e066e23 CM |
632 | int seconds; |
633 | ||
634 | if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) | |
635 | return; | |
636 | ||
637 | if (action != tput_event(trace)) | |
638 | return; | |
639 | ||
640 | seconds = SECONDS(io->time); | |
f752a6eb JK |
641 | if (seconds > gld->max_seconds) { |
642 | fprintf(stderr, "Bad record %d %d %d\n", seconds, gld->max_seconds, action); | |
9e066e23 CM |
643 | abort(); |
644 | } | |
645 | ||
646 | gld->data[seconds].sum += io->bytes; | |
647 | gld->data[seconds].count = 1; | |
648 | if (gld->data[seconds].sum > gld->max) | |
649 | gld->max = gld->data[seconds].sum; | |
650 | } | |
651 | ||
652 | void add_io(struct trace *trace, struct graph_dot_data *gdd_writes, | |
653 | struct graph_dot_data *gdd_reads) | |
654 | { | |
655 | struct blk_io_trace *io = trace->io; | |
1582ecc9 | 656 | int action = io->action & BLK_TA_MASK; |
9e066e23 CM |
657 | u64 offset; |
658 | ||
659 | if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) | |
660 | return; | |
661 | ||
662 | if (action != io_event(trace)) | |
663 | return; | |
664 | ||
665 | offset = io->sector << 9; | |
666 | ||
667 | if (BLK_DATADIR(io->action) & BLK_TC_READ) | |
668 | set_gdd_bit(gdd_reads, offset, io->bytes, io->time); | |
669 | else if (BLK_DATADIR(io->action) & BLK_TC_WRITE) | |
670 | set_gdd_bit(gdd_writes, offset, io->bytes, io->time); | |
671 | } | |
672 | ||
673 | void add_pending_io(struct trace *trace, struct graph_line_data *gld) | |
674 | { | |
675 | int ret; | |
676 | int seconds; | |
677 | struct blk_io_trace *io = trace->io; | |
1582ecc9 | 678 | int action = io->action & BLK_TA_MASK; |
9e066e23 CM |
679 | double avg; |
680 | ||
681 | if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) | |
682 | return; | |
683 | ||
684 | if (action != __BLK_TA_ISSUE) | |
685 | return; | |
686 | ||
687 | seconds = SECONDS(io->time); | |
f752a6eb JK |
688 | if (seconds > gld->max_seconds) { |
689 | fprintf(stderr, "Bad record %d %d\n", seconds, gld->max_seconds); | |
9e066e23 CM |
690 | abort(); |
691 | } | |
692 | ||
693 | ret = hash_dispatched_io(trace->io); | |
694 | if (ret) | |
695 | return; | |
696 | ||
697 | ios_in_flight++; | |
698 | ||
699 | gld->data[seconds].sum += ios_in_flight; | |
700 | gld->data[seconds].count++; | |
701 | ||
702 | avg = (double)gld->data[seconds].sum / gld->data[seconds].count; | |
703 | if (gld->max < (u64)avg) { | |
704 | gld->max = avg; | |
705 | } | |
706 | } | |
707 | ||
708 | void add_completed_io(struct trace *trace, | |
709 | struct graph_line_data *latency_gld) | |
710 | { | |
711 | struct blk_io_trace *io = trace->io; | |
712 | int seconds; | |
1582ecc9 | 713 | int action = io->action & BLK_TA_MASK; |
9e066e23 CM |
714 | struct pending_io *pio; |
715 | double avg; | |
716 | u64 latency; | |
717 | ||
718 | if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) | |
719 | return; | |
720 | ||
721 | if (action != __BLK_TA_COMPLETE) | |
722 | return; | |
723 | ||
724 | seconds = SECONDS(io->time); | |
725 | ||
726 | pio = hash_completed_io(trace->io); | |
727 | if (!pio) | |
728 | return; | |
729 | ||
730 | if (ios_in_flight > 0) | |
731 | ios_in_flight--; | |
732 | if (io->time >= pio->dispatch_time) { | |
733 | latency = io->time - pio->dispatch_time; | |
734 | latency_gld->data[seconds].sum += latency; | |
735 | latency_gld->data[seconds].count++; | |
736 | } | |
737 | ||
738 | list_del(&pio->hash_list); | |
739 | free(pio); | |
740 | ||
741 | avg = (double)latency_gld->data[seconds].sum / | |
742 | latency_gld->data[seconds].count; | |
743 | if (latency_gld->max < (u64)avg) { | |
744 | latency_gld->max = avg; | |
745 | } | |
746 | } | |
747 | ||
748 | void add_iop(struct trace *trace, struct graph_line_data *gld) | |
749 | { | |
750 | struct blk_io_trace *io = trace->io; | |
1582ecc9 | 751 | int action = io->action & BLK_TA_MASK; |
9e066e23 CM |
752 | int seconds; |
753 | ||
754 | if (io->action & BLK_TC_ACT(BLK_TC_NOTIFY)) | |
755 | return; | |
756 | ||
757 | /* iops and tput use the same events */ | |
758 | if (action != tput_event(trace)) | |
759 | return; | |
760 | ||
761 | seconds = SECONDS(io->time); | |
f752a6eb JK |
762 | if (seconds > gld->max_seconds) { |
763 | fprintf(stderr, "Bad record %d %d\n", seconds, gld->max_seconds); | |
9e066e23 CM |
764 | abort(); |
765 | } | |
766 | ||
767 | gld->data[seconds].sum += 1; | |
768 | gld->data[seconds].count = 1; | |
769 | if (gld->data[seconds].sum > gld->max) | |
770 | gld->max = gld->data[seconds].sum; | |
771 | } | |
772 | ||
773 | void check_record(struct trace *trace) | |
774 | { | |
775 | struct blk_io_trace *io = trace->io; | |
1582ecc9 | 776 | int action = io->action & BLK_TA_MASK; |
9e066e23 CM |
777 | |
778 | if (!(io->action & BLK_TC_ACT(BLK_TC_NOTIFY))) { | |
779 | switch (action) { | |
780 | case __BLK_TA_COMPLETE: | |
781 | trace->found_completion = 1; | |
782 | break; | |
783 | case __BLK_TA_ISSUE: | |
784 | trace->found_issue = 1; | |
785 | break; | |
786 | case __BLK_TA_QUEUE: | |
787 | trace->found_queue = 1; | |
788 | break; | |
789 | }; | |
790 | } | |
791 | handle_notify(trace); | |
792 | } |