Commit | Line | Data |
---|---|---|
ac9b9101 JA |
1 | /* |
2 | * Code related to writing an iolog of what a thread is doing, and to | |
3 | * later read that back and replay | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <libgen.h> | |
8 | #include <assert.h> | |
b26317c9 JA |
9 | #include <sys/types.h> |
10 | #include <sys/stat.h> | |
11 | #include <unistd.h> | |
aee2ab67 JA |
12 | #ifdef CONFIG_ZLIB |
13 | #include <zlib.h> | |
14 | #endif | |
15 | ||
ac9b9101 JA |
16 | #include "flist.h" |
17 | #include "fio.h" | |
18 | #include "verify.h" | |
19 | #include "trim.h" | |
243bfe19 | 20 | #include "filelock.h" |
a47591e4 | 21 | #include "smalloc.h" |
a3e59412 | 22 | #include "blktrace.h" |
ac9b9101 | 23 | |
7e419452 JA |
24 | static int iolog_flush(struct io_log *log); |
25 | ||
ac9b9101 JA |
26 | static const char iolog_ver2[] = "fio version 2 iolog"; |
27 | ||
28 | void queue_io_piece(struct thread_data *td, struct io_piece *ipo) | |
29 | { | |
30 | flist_add_tail(&ipo->list, &td->io_log_list); | |
31 | td->total_io_size += ipo->len; | |
32 | } | |
33 | ||
1f440ece | 34 | void log_io_u(const struct thread_data *td, const struct io_u *io_u) |
ac9b9101 | 35 | { |
ac9b9101 JA |
36 | if (!td->o.write_iolog_file) |
37 | return; | |
38 | ||
39 | fprintf(td->iolog_f, "%s %s %llu %lu\n", io_u->file->file_name, | |
173375c2 JA |
40 | io_ddir_name(io_u->ddir), |
41 | io_u->offset, io_u->buflen); | |
ac9b9101 JA |
42 | } |
43 | ||
44 | void log_file(struct thread_data *td, struct fio_file *f, | |
45 | enum file_log_act what) | |
46 | { | |
47 | const char *act[] = { "add", "open", "close" }; | |
48 | ||
49 | assert(what < 3); | |
50 | ||
51 | if (!td->o.write_iolog_file) | |
52 | return; | |
53 | ||
54 | ||
55 | /* | |
56 | * this happens on the pre-open/close done before the job starts | |
57 | */ | |
58 | if (!td->iolog_f) | |
59 | return; | |
60 | ||
61 | fprintf(td->iolog_f, "%s %s\n", f->file_name, act[what]); | |
62 | } | |
63 | ||
64 | static void iolog_delay(struct thread_data *td, unsigned long delay) | |
65 | { | |
df8fb609 JA |
66 | uint64_t usec = utime_since_now(&td->last_issue); |
67 | uint64_t this_delay; | |
8b6a404c | 68 | struct timespec ts; |
ac9b9101 | 69 | |
df8fb609 JA |
70 | if (delay < td->time_offset) { |
71 | td->time_offset = 0; | |
72 | return; | |
73 | } | |
74 | ||
75 | delay -= td->time_offset; | |
ac9b9101 JA |
76 | if (delay < usec) |
77 | return; | |
78 | ||
79 | delay -= usec; | |
80 | ||
8b6a404c | 81 | fio_gettime(&ts, NULL); |
30b18672 JA |
82 | while (delay && !td->terminate) { |
83 | this_delay = delay; | |
84 | if (this_delay > 500000) | |
85 | this_delay = 500000; | |
86 | ||
87 | usec_sleep(td, this_delay); | |
88 | delay -= this_delay; | |
89 | } | |
df8fb609 | 90 | |
8b6a404c | 91 | usec = utime_since_now(&ts); |
df8fb609 JA |
92 | if (usec > delay) |
93 | td->time_offset = usec - delay; | |
94 | else | |
95 | td->time_offset = 0; | |
ac9b9101 JA |
96 | } |
97 | ||
98 | static int ipo_special(struct thread_data *td, struct io_piece *ipo) | |
99 | { | |
100 | struct fio_file *f; | |
101 | int ret; | |
102 | ||
103 | /* | |
104 | * Not a special ipo | |
105 | */ | |
106 | if (ipo->ddir != DDIR_INVAL) | |
107 | return 0; | |
108 | ||
109 | f = td->files[ipo->fileno]; | |
110 | ||
111 | switch (ipo->file_action) { | |
112 | case FIO_LOG_OPEN_FILE: | |
96d663ad SW |
113 | if (td->o.replay_redirect && fio_file_open(f)) { |
114 | dprint(FD_FILE, "iolog: ignoring re-open of file %s\n", | |
115 | f->file_name); | |
116 | break; | |
117 | } | |
ac9b9101 JA |
118 | ret = td_io_open_file(td, f); |
119 | if (!ret) | |
120 | break; | |
121 | td_verror(td, ret, "iolog open file"); | |
122 | return -1; | |
123 | case FIO_LOG_CLOSE_FILE: | |
124 | td_io_close_file(td, f); | |
125 | break; | |
126 | case FIO_LOG_UNLINK_FILE: | |
38ef9c90 | 127 | td_io_unlink_file(td, f); |
ac9b9101 JA |
128 | break; |
129 | default: | |
130 | log_err("fio: bad file action %d\n", ipo->file_action); | |
131 | break; | |
132 | } | |
133 | ||
134 | return 1; | |
135 | } | |
136 | ||
137 | int read_iolog_get(struct thread_data *td, struct io_u *io_u) | |
138 | { | |
139 | struct io_piece *ipo; | |
140 | unsigned long elapsed; | |
3c3ed070 | 141 | |
ac9b9101 JA |
142 | while (!flist_empty(&td->io_log_list)) { |
143 | int ret; | |
144 | ||
9342d5f8 | 145 | ipo = flist_first_entry(&td->io_log_list, struct io_piece, list); |
ac9b9101 JA |
146 | flist_del(&ipo->list); |
147 | remove_trim_entry(td, ipo); | |
148 | ||
149 | ret = ipo_special(td, ipo); | |
150 | if (ret < 0) { | |
151 | free(ipo); | |
152 | break; | |
153 | } else if (ret > 0) { | |
154 | free(ipo); | |
155 | continue; | |
156 | } | |
157 | ||
158 | io_u->ddir = ipo->ddir; | |
159 | if (ipo->ddir != DDIR_WAIT) { | |
160 | io_u->offset = ipo->offset; | |
161 | io_u->buflen = ipo->len; | |
162 | io_u->file = td->files[ipo->fileno]; | |
163 | get_file(io_u->file); | |
164 | dprint(FD_IO, "iolog: get %llu/%lu/%s\n", io_u->offset, | |
165 | io_u->buflen, io_u->file->file_name); | |
166 | if (ipo->delay) | |
167 | iolog_delay(td, ipo->delay); | |
168 | } else { | |
169 | elapsed = mtime_since_genesis(); | |
170 | if (ipo->delay > elapsed) | |
171 | usec_sleep(td, (ipo->delay - elapsed) * 1000); | |
ac9b9101 JA |
172 | } |
173 | ||
174 | free(ipo); | |
3c3ed070 | 175 | |
ac9b9101 JA |
176 | if (io_u->ddir != DDIR_WAIT) |
177 | return 0; | |
178 | } | |
179 | ||
180 | td->done = 1; | |
181 | return 1; | |
182 | } | |
183 | ||
184 | void prune_io_piece_log(struct thread_data *td) | |
185 | { | |
186 | struct io_piece *ipo; | |
187 | struct rb_node *n; | |
188 | ||
189 | while ((n = rb_first(&td->io_hist_tree)) != NULL) { | |
190 | ipo = rb_entry(n, struct io_piece, rb_node); | |
191 | rb_erase(n, &td->io_hist_tree); | |
192 | remove_trim_entry(td, ipo); | |
193 | td->io_hist_len--; | |
194 | free(ipo); | |
195 | } | |
196 | ||
197 | while (!flist_empty(&td->io_hist_list)) { | |
4e1020f8 | 198 | ipo = flist_first_entry(&td->io_hist_list, struct io_piece, list); |
ac9b9101 JA |
199 | flist_del(&ipo->list); |
200 | remove_trim_entry(td, ipo); | |
201 | td->io_hist_len--; | |
202 | free(ipo); | |
203 | } | |
204 | } | |
205 | ||
206 | /* | |
207 | * log a successful write, so we can unwind the log for verify | |
208 | */ | |
209 | void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
210 | { | |
211 | struct rb_node **p, *parent; | |
212 | struct io_piece *ipo, *__ipo; | |
213 | ||
214 | ipo = malloc(sizeof(struct io_piece)); | |
215 | init_ipo(ipo); | |
216 | ipo->file = io_u->file; | |
217 | ipo->offset = io_u->offset; | |
218 | ipo->len = io_u->buflen; | |
da0a7bd2 | 219 | ipo->numberio = io_u->numberio; |
f9401285 JA |
220 | ipo->flags = IP_F_IN_FLIGHT; |
221 | ||
222 | io_u->ipo = ipo; | |
ac9b9101 JA |
223 | |
224 | if (io_u_should_trim(td, io_u)) { | |
225 | flist_add_tail(&ipo->trim_list, &td->trim_list); | |
226 | td->trim_entries++; | |
227 | } | |
228 | ||
229 | /* | |
8d4564e9 SW |
230 | * We don't need to sort the entries if we only performed sequential |
231 | * writes. In this case, just reading back data in the order we wrote | |
232 | * it out is the faster but still safe. | |
ac9b9101 | 233 | * |
4a06cb41 SW |
234 | * One exception is if we don't have a random map in which case we need |
235 | * to check for duplicate blocks and drop the old one, which we rely on | |
236 | * the rb insert/lookup for handling. | |
ac9b9101 | 237 | */ |
8d4564e9 | 238 | if (((!td->o.verifysort) || !td_random(td)) && |
4a06cb41 | 239 | file_randommap(td, ipo->file)) { |
ac9b9101 JA |
240 | INIT_FLIST_HEAD(&ipo->list); |
241 | flist_add_tail(&ipo->list, &td->io_hist_list); | |
242 | ipo->flags |= IP_F_ONLIST; | |
243 | td->io_hist_len++; | |
244 | return; | |
245 | } | |
246 | ||
247 | RB_CLEAR_NODE(&ipo->rb_node); | |
248 | ||
249 | /* | |
250 | * Sort the entry into the verification list | |
251 | */ | |
252 | restart: | |
253 | p = &td->io_hist_tree.rb_node; | |
254 | parent = NULL; | |
255 | while (*p) { | |
3b1ff869 | 256 | int overlap = 0; |
ac9b9101 JA |
257 | parent = *p; |
258 | ||
259 | __ipo = rb_entry(parent, struct io_piece, rb_node); | |
260 | if (ipo->file < __ipo->file) | |
261 | p = &(*p)->rb_left; | |
262 | else if (ipo->file > __ipo->file) | |
263 | p = &(*p)->rb_right; | |
3b1ff869 | 264 | else if (ipo->offset < __ipo->offset) { |
ac9b9101 | 265 | p = &(*p)->rb_left; |
3b1ff869 JE |
266 | overlap = ipo->offset + ipo->len > __ipo->offset; |
267 | } | |
268 | else if (ipo->offset > __ipo->offset) { | |
ac9b9101 | 269 | p = &(*p)->rb_right; |
3b1ff869 JE |
270 | overlap = __ipo->offset + __ipo->len > ipo->offset; |
271 | } | |
272 | else | |
273 | overlap = 1; | |
274 | ||
275 | if (overlap) { | |
7abe6a27 | 276 | dprint(FD_IO, "iolog: overlap %llu/%lu, %llu/%lu\n", |
885ac623 JA |
277 | __ipo->offset, __ipo->len, |
278 | ipo->offset, ipo->len); | |
ac9b9101 JA |
279 | td->io_hist_len--; |
280 | rb_erase(parent, &td->io_hist_tree); | |
281 | remove_trim_entry(td, __ipo); | |
e8b74655 SW |
282 | if (!(__ipo->flags & IP_F_IN_FLIGHT)) |
283 | free(__ipo); | |
ac9b9101 JA |
284 | goto restart; |
285 | } | |
286 | } | |
287 | ||
288 | rb_link_node(&ipo->rb_node, parent, p); | |
289 | rb_insert_color(&ipo->rb_node, &td->io_hist_tree); | |
290 | ipo->flags |= IP_F_ONRB; | |
291 | td->io_hist_len++; | |
292 | } | |
293 | ||
890b6656 JA |
294 | void unlog_io_piece(struct thread_data *td, struct io_u *io_u) |
295 | { | |
296 | struct io_piece *ipo = io_u->ipo; | |
297 | ||
66347cfa DE |
298 | if (td->ts.nr_block_infos) { |
299 | uint32_t *info = io_u_block_info(td, io_u); | |
300 | if (BLOCK_INFO_STATE(*info) < BLOCK_STATE_TRIM_FAILURE) { | |
301 | if (io_u->ddir == DDIR_TRIM) | |
302 | *info = BLOCK_INFO_SET_STATE(*info, | |
303 | BLOCK_STATE_TRIM_FAILURE); | |
304 | else if (io_u->ddir == DDIR_WRITE) | |
305 | *info = BLOCK_INFO_SET_STATE(*info, | |
306 | BLOCK_STATE_WRITE_FAILURE); | |
307 | } | |
308 | } | |
309 | ||
890b6656 JA |
310 | if (!ipo) |
311 | return; | |
312 | ||
313 | if (ipo->flags & IP_F_ONRB) | |
314 | rb_erase(&ipo->rb_node, &td->io_hist_tree); | |
315 | else if (ipo->flags & IP_F_ONLIST) | |
316 | flist_del(&ipo->list); | |
317 | ||
318 | free(ipo); | |
319 | io_u->ipo = NULL; | |
320 | td->io_hist_len--; | |
321 | } | |
322 | ||
1f440ece | 323 | void trim_io_piece(struct thread_data *td, const struct io_u *io_u) |
890b6656 JA |
324 | { |
325 | struct io_piece *ipo = io_u->ipo; | |
326 | ||
327 | if (!ipo) | |
328 | return; | |
329 | ||
330 | ipo->len = io_u->xfer_buflen - io_u->resid; | |
331 | } | |
332 | ||
ac9b9101 JA |
333 | void write_iolog_close(struct thread_data *td) |
334 | { | |
335 | fflush(td->iolog_f); | |
336 | fclose(td->iolog_f); | |
337 | free(td->iolog_buf); | |
338 | td->iolog_f = NULL; | |
339 | td->iolog_buf = NULL; | |
340 | } | |
341 | ||
342 | /* | |
343 | * Read version 2 iolog data. It is enhanced to include per-file logging, | |
344 | * syncs, etc. | |
345 | */ | |
346 | static int read_iolog2(struct thread_data *td, FILE *f) | |
347 | { | |
348 | unsigned long long offset; | |
349 | unsigned int bytes; | |
350 | int reads, writes, waits, fileno = 0, file_action = 0; /* stupid gcc */ | |
dac34079 | 351 | char *rfname, *fname, *act; |
ac9b9101 JA |
352 | char *str, *p; |
353 | enum fio_ddir rw; | |
354 | ||
355 | free_release_files(td); | |
356 | ||
357 | /* | |
358 | * Read in the read iolog and store it, reuse the infrastructure | |
359 | * for doing verifications. | |
360 | */ | |
361 | str = malloc(4096); | |
dac34079 | 362 | rfname = fname = malloc(256+16); |
ac9b9101 JA |
363 | act = malloc(256+16); |
364 | ||
365 | reads = writes = waits = 0; | |
366 | while ((p = fgets(str, 4096, f)) != NULL) { | |
367 | struct io_piece *ipo; | |
368 | int r; | |
369 | ||
dac34079 | 370 | r = sscanf(p, "%256s %256s %llu %u", rfname, act, &offset, |
ac9b9101 | 371 | &bytes); |
dac34079 JA |
372 | |
373 | if (td->o.replay_redirect) | |
374 | fname = td->o.replay_redirect; | |
375 | ||
ac9b9101 JA |
376 | if (r == 4) { |
377 | /* | |
378 | * Check action first | |
379 | */ | |
380 | if (!strcmp(act, "wait")) | |
381 | rw = DDIR_WAIT; | |
382 | else if (!strcmp(act, "read")) | |
383 | rw = DDIR_READ; | |
384 | else if (!strcmp(act, "write")) | |
385 | rw = DDIR_WRITE; | |
386 | else if (!strcmp(act, "sync")) | |
387 | rw = DDIR_SYNC; | |
388 | else if (!strcmp(act, "datasync")) | |
389 | rw = DDIR_DATASYNC; | |
390 | else if (!strcmp(act, "trim")) | |
391 | rw = DDIR_TRIM; | |
392 | else { | |
393 | log_err("fio: bad iolog file action: %s\n", | |
394 | act); | |
395 | continue; | |
396 | } | |
033ace1e | 397 | fileno = get_fileno(td, fname); |
ac9b9101 JA |
398 | } else if (r == 2) { |
399 | rw = DDIR_INVAL; | |
400 | if (!strcmp(act, "add")) { | |
96d663ad SW |
401 | if (td->o.replay_redirect && |
402 | get_fileno(td, fname) != -1) { | |
403 | dprint(FD_FILE, "iolog: ignoring" | |
404 | " re-add of file %s\n", fname); | |
405 | } else { | |
406 | fileno = add_file(td, fname, 0, 1); | |
407 | file_action = FIO_LOG_ADD_FILE; | |
408 | } | |
ac9b9101 JA |
409 | continue; |
410 | } else if (!strcmp(act, "open")) { | |
411 | fileno = get_fileno(td, fname); | |
412 | file_action = FIO_LOG_OPEN_FILE; | |
413 | } else if (!strcmp(act, "close")) { | |
414 | fileno = get_fileno(td, fname); | |
415 | file_action = FIO_LOG_CLOSE_FILE; | |
416 | } else { | |
417 | log_err("fio: bad iolog file action: %s\n", | |
418 | act); | |
419 | continue; | |
420 | } | |
421 | } else { | |
c5839011 | 422 | log_err("bad iolog2: %s\n", p); |
ac9b9101 JA |
423 | continue; |
424 | } | |
425 | ||
426 | if (rw == DDIR_READ) | |
427 | reads++; | |
428 | else if (rw == DDIR_WRITE) { | |
429 | /* | |
430 | * Don't add a write for ro mode | |
431 | */ | |
432 | if (read_only) | |
433 | continue; | |
434 | writes++; | |
435 | } else if (rw == DDIR_WAIT) { | |
74dff443 JA |
436 | if (td->o.no_stall) |
437 | continue; | |
ac9b9101 JA |
438 | waits++; |
439 | } else if (rw == DDIR_INVAL) { | |
440 | } else if (!ddir_sync(rw)) { | |
441 | log_err("bad ddir: %d\n", rw); | |
442 | continue; | |
443 | } | |
444 | ||
445 | /* | |
446 | * Make note of file | |
447 | */ | |
448 | ipo = malloc(sizeof(*ipo)); | |
449 | init_ipo(ipo); | |
450 | ipo->ddir = rw; | |
451 | if (rw == DDIR_WAIT) { | |
452 | ipo->delay = offset; | |
453 | } else { | |
a79f17bf SW |
454 | if (td->o.replay_scale) |
455 | ipo->offset = offset / td->o.replay_scale; | |
456 | else | |
457 | ipo->offset = offset; | |
458 | ipo_bytes_align(td->o.replay_align, ipo); | |
459 | ||
ac9b9101 | 460 | ipo->len = bytes; |
42793d94 | 461 | if (rw != DDIR_INVAL && bytes > td->o.max_bs[rw]) |
ac9b9101 JA |
462 | td->o.max_bs[rw] = bytes; |
463 | ipo->fileno = fileno; | |
464 | ipo->file_action = file_action; | |
70afff59 | 465 | td->o.size += bytes; |
ac9b9101 | 466 | } |
3c3ed070 | 467 | |
ac9b9101 JA |
468 | queue_io_piece(td, ipo); |
469 | } | |
470 | ||
471 | free(str); | |
472 | free(act); | |
dac34079 | 473 | free(rfname); |
ac9b9101 JA |
474 | |
475 | if (writes && read_only) { | |
476 | log_err("fio: <%s> skips replay of %d writes due to" | |
477 | " read-only\n", td->o.name, writes); | |
478 | writes = 0; | |
479 | } | |
480 | ||
481 | if (!reads && !writes && !waits) | |
482 | return 1; | |
483 | else if (reads && !writes) | |
484 | td->o.td_ddir = TD_DDIR_READ; | |
485 | else if (!reads && writes) | |
486 | td->o.td_ddir = TD_DDIR_WRITE; | |
487 | else | |
488 | td->o.td_ddir = TD_DDIR_RW; | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
493 | /* | |
494 | * open iolog, check version, and call appropriate parser | |
495 | */ | |
496 | static int init_iolog_read(struct thread_data *td) | |
497 | { | |
498 | char buffer[256], *p; | |
499 | FILE *f; | |
500 | int ret; | |
501 | ||
502 | f = fopen(td->o.read_iolog_file, "r"); | |
503 | if (!f) { | |
504 | perror("fopen read iolog"); | |
505 | return 1; | |
506 | } | |
507 | ||
508 | p = fgets(buffer, sizeof(buffer), f); | |
509 | if (!p) { | |
510 | td_verror(td, errno, "iolog read"); | |
511 | log_err("fio: unable to read iolog\n"); | |
512 | fclose(f); | |
513 | return 1; | |
514 | } | |
515 | ||
516 | /* | |
517 | * version 2 of the iolog stores a specific string as the | |
518 | * first line, check for that | |
519 | */ | |
520 | if (!strncmp(iolog_ver2, buffer, strlen(iolog_ver2))) | |
521 | ret = read_iolog2(td, f); | |
522 | else { | |
523 | log_err("fio: iolog version 1 is no longer supported\n"); | |
524 | ret = 1; | |
525 | } | |
526 | ||
527 | fclose(f); | |
528 | return ret; | |
529 | } | |
530 | ||
531 | /* | |
532 | * Set up a log for storing io patterns. | |
533 | */ | |
534 | static int init_iolog_write(struct thread_data *td) | |
535 | { | |
536 | struct fio_file *ff; | |
537 | FILE *f; | |
538 | unsigned int i; | |
539 | ||
540 | f = fopen(td->o.write_iolog_file, "a"); | |
541 | if (!f) { | |
542 | perror("fopen write iolog"); | |
543 | return 1; | |
544 | } | |
545 | ||
546 | /* | |
547 | * That's it for writing, setup a log buffer and we're done. | |
548 | */ | |
549 | td->iolog_f = f; | |
550 | td->iolog_buf = malloc(8192); | |
551 | setvbuf(f, td->iolog_buf, _IOFBF, 8192); | |
552 | ||
553 | /* | |
554 | * write our version line | |
555 | */ | |
556 | if (fprintf(f, "%s\n", iolog_ver2) < 0) { | |
557 | perror("iolog init\n"); | |
558 | return 1; | |
559 | } | |
560 | ||
561 | /* | |
562 | * add all known files | |
563 | */ | |
564 | for_each_file(td, ff, i) | |
565 | log_file(td, ff, FIO_LOG_ADD_FILE); | |
566 | ||
567 | return 0; | |
568 | } | |
569 | ||
570 | int init_iolog(struct thread_data *td) | |
571 | { | |
572 | int ret = 0; | |
573 | ||
574 | if (td->o.read_iolog_file) { | |
d95b34a6 JA |
575 | int need_swap; |
576 | ||
ac9b9101 JA |
577 | /* |
578 | * Check if it's a blktrace file and load that if possible. | |
579 | * Otherwise assume it's a normal log file and load that. | |
580 | */ | |
d95b34a6 JA |
581 | if (is_blktrace(td->o.read_iolog_file, &need_swap)) |
582 | ret = load_blktrace(td, td->o.read_iolog_file, need_swap); | |
ac9b9101 JA |
583 | else |
584 | ret = init_iolog_read(td); | |
585 | } else if (td->o.write_iolog_file) | |
586 | ret = init_iolog_write(td); | |
587 | ||
f01b34ae JA |
588 | if (ret) |
589 | td_verror(td, EINVAL, "failed initializing iolog"); | |
590 | ||
c492cb1a | 591 | init_disk_util(td); |
592 | ||
ac9b9101 JA |
593 | return ret; |
594 | } | |
595 | ||
aee2ab67 JA |
596 | void setup_log(struct io_log **log, struct log_params *p, |
597 | const char *filename) | |
ac9b9101 | 598 | { |
e75cc8f3 | 599 | struct io_log *l; |
65a4d15c KC |
600 | int i; |
601 | struct io_u_plat_entry *entry; | |
602 | struct flist_head *list; | |
ac9b9101 | 603 | |
7e419452 JA |
604 | l = scalloc(1, sizeof(*l)); |
605 | INIT_FLIST_HEAD(&l->io_logs); | |
aee2ab67 JA |
606 | l->log_type = p->log_type; |
607 | l->log_offset = p->log_offset; | |
608 | l->log_gz = p->log_gz; | |
b26317c9 | 609 | l->log_gz_store = p->log_gz_store; |
aee2ab67 | 610 | l->avg_msec = p->avg_msec; |
1e613c9c KC |
611 | l->hist_msec = p->hist_msec; |
612 | l->hist_coarseness = p->hist_coarseness; | |
cb7e0ace | 613 | l->filename = strdup(filename); |
aee2ab67 JA |
614 | l->td = p->td; |
615 | ||
65a4d15c KC |
616 | /* Initialize histogram lists for each r/w direction, |
617 | * with initial io_u_plat of all zeros: | |
618 | */ | |
619 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { | |
d730bc58 | 620 | list = &l->hist_window[i].list; |
65a4d15c KC |
621 | INIT_FLIST_HEAD(list); |
622 | entry = calloc(1, sizeof(struct io_u_plat_entry)); | |
623 | flist_add(&entry->list, list); | |
624 | } | |
625 | ||
1fed2080 JA |
626 | if (l->td && l->td->o.io_submit_mode != IO_MODE_OFFLOAD) { |
627 | struct io_logs *p; | |
628 | ||
629 | p = calloc(1, sizeof(*l->pending)); | |
a9afa45a | 630 | p->max_samples = DEF_LOG_ENTRIES; |
1fed2080 JA |
631 | p->log = calloc(p->max_samples, log_entry_sz(l)); |
632 | l->pending = p; | |
633 | } | |
634 | ||
b26317c9 | 635 | if (l->log_offset) |
49e98daa | 636 | l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT; |
b26317c9 | 637 | |
aee2ab67 JA |
638 | INIT_FLIST_HEAD(&l->chunk_list); |
639 | ||
640 | if (l->log_gz && !p->td) | |
641 | l->log_gz = 0; | |
19417eda | 642 | else if (l->log_gz || l->log_gz_store) { |
34febb23 | 643 | mutex_init_pshared(&l->chunk_lock); |
858bce70 | 644 | mutex_init_pshared(&l->deferred_free_lock); |
aee2ab67 JA |
645 | p->td->flags |= TD_F_COMPRESS_LOG; |
646 | } | |
647 | ||
ac9b9101 JA |
648 | *log = l; |
649 | } | |
650 | ||
2e802282 JA |
651 | #ifdef CONFIG_SETVBUF |
652 | static void *set_file_buffer(FILE *f) | |
653 | { | |
654 | size_t size = 1048576; | |
655 | void *buf; | |
656 | ||
657 | buf = malloc(size); | |
658 | setvbuf(f, buf, _IOFBF, size); | |
659 | return buf; | |
660 | } | |
661 | ||
662 | static void clear_file_buffer(void *buf) | |
663 | { | |
664 | free(buf); | |
665 | } | |
666 | #else | |
667 | static void *set_file_buffer(FILE *f) | |
668 | { | |
669 | return NULL; | |
670 | } | |
671 | ||
672 | static void clear_file_buffer(void *buf) | |
673 | { | |
674 | } | |
675 | #endif | |
676 | ||
518dac09 | 677 | void free_log(struct io_log *log) |
cb7e0ace | 678 | { |
7e419452 JA |
679 | while (!flist_empty(&log->io_logs)) { |
680 | struct io_logs *cur_log; | |
681 | ||
682 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); | |
683 | flist_del_init(&cur_log->list); | |
684 | free(cur_log->log); | |
2b7625e2 | 685 | sfree(cur_log); |
7e419452 JA |
686 | } |
687 | ||
1fed2080 JA |
688 | if (log->pending) { |
689 | free(log->pending->log); | |
690 | free(log->pending); | |
691 | log->pending = NULL; | |
692 | } | |
693 | ||
694 | free(log->pending); | |
cb7e0ace | 695 | free(log->filename); |
a47591e4 | 696 | sfree(log); |
cb7e0ace JA |
697 | } |
698 | ||
0225849f | 699 | unsigned long hist_sum(int j, int stride, unsigned int *io_u_plat, |
65a4d15c | 700 | unsigned int *io_u_plat_last) |
548df930 | 701 | { |
f2c972cc JA |
702 | unsigned long sum; |
703 | int k; | |
548df930 | 704 | |
868f6f03 KC |
705 | if (io_u_plat_last) { |
706 | for (k = sum = 0; k < stride; k++) | |
707 | sum += io_u_plat[j + k] - io_u_plat_last[j + k]; | |
708 | } else { | |
709 | for (k = sum = 0; k < stride; k++) | |
710 | sum += io_u_plat[j + k]; | |
711 | } | |
548df930 | 712 | |
1e613c9c KC |
713 | return sum; |
714 | } | |
715 | ||
a89ba4b1 JA |
716 | static void flush_hist_samples(FILE *f, int hist_coarseness, void *samples, |
717 | uint64_t sample_size) | |
1e613c9c KC |
718 | { |
719 | struct io_sample *s; | |
720 | int log_offset; | |
721 | uint64_t i, j, nr_samples; | |
65a4d15c | 722 | struct io_u_plat_entry *entry, *entry_before; |
1e613c9c | 723 | unsigned int *io_u_plat; |
65a4d15c | 724 | unsigned int *io_u_plat_before; |
1e613c9c KC |
725 | |
726 | int stride = 1 << hist_coarseness; | |
727 | ||
728 | if (!sample_size) | |
729 | return; | |
730 | ||
731 | s = __get_sample(samples, 0, 0); | |
732 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; | |
733 | ||
734 | nr_samples = sample_size / __log_entry_sz(log_offset); | |
735 | ||
736 | for (i = 0; i < nr_samples; i++) { | |
737 | s = __get_sample(samples, log_offset, i); | |
d730bc58 | 738 | |
af2fde19 | 739 | entry = s->data.plat_entry; |
65a4d15c | 740 | io_u_plat = entry->io_u_plat; |
d730bc58 | 741 | |
65a4d15c KC |
742 | entry_before = flist_first_entry(&entry->list, struct io_u_plat_entry, list); |
743 | io_u_plat_before = entry_before->io_u_plat; | |
d730bc58 JA |
744 | |
745 | fprintf(f, "%lu, %u, %u, ", (unsigned long) s->time, | |
746 | io_sample_ddir(s), s->bs); | |
1e613c9c | 747 | for (j = 0; j < FIO_IO_U_PLAT_NR - stride; j += stride) { |
d730bc58 JA |
748 | fprintf(f, "%lu, ", hist_sum(j, stride, io_u_plat, |
749 | io_u_plat_before)); | |
1e613c9c | 750 | } |
d730bc58 | 751 | fprintf(f, "%lu\n", (unsigned long) |
65a4d15c | 752 | hist_sum(FIO_IO_U_PLAT_NR - stride, stride, io_u_plat, |
d730bc58 JA |
753 | io_u_plat_before)); |
754 | ||
65a4d15c KC |
755 | flist_del(&entry_before->list); |
756 | free(entry_before); | |
1e613c9c KC |
757 | } |
758 | } | |
759 | ||
bead01d7 | 760 | void flush_samples(FILE *f, void *samples, uint64_t sample_size) |
ac9b9101 | 761 | { |
b26317c9 JA |
762 | struct io_sample *s; |
763 | int log_offset; | |
764 | uint64_t i, nr_samples; | |
765 | ||
766 | if (!sample_size) | |
767 | return; | |
768 | ||
769 | s = __get_sample(samples, 0, 0); | |
49e98daa | 770 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; |
b26317c9 JA |
771 | |
772 | nr_samples = sample_size / __log_entry_sz(log_offset); | |
ac9b9101 | 773 | |
aee2ab67 | 774 | for (i = 0; i < nr_samples; i++) { |
b26317c9 | 775 | s = __get_sample(samples, log_offset, i); |
ac9b9101 | 776 | |
aee2ab67 | 777 | if (!log_offset) { |
af2fde19 | 778 | fprintf(f, "%lu, %" PRId64 ", %u, %u\n", |
ae588852 | 779 | (unsigned long) s->time, |
af2fde19 | 780 | s->data.val, |
b26317c9 | 781 | io_sample_ddir(s), s->bs); |
ae588852 JA |
782 | } else { |
783 | struct io_sample_offset *so = (void *) s; | |
784 | ||
af2fde19 | 785 | fprintf(f, "%lu, %" PRId64 ", %u, %u, %llu\n", |
ae588852 | 786 | (unsigned long) s->time, |
af2fde19 | 787 | s->data.val, |
b26317c9 | 788 | io_sample_ddir(s), s->bs, |
ae588852 JA |
789 | (unsigned long long) so->offset); |
790 | } | |
ac9b9101 | 791 | } |
aee2ab67 JA |
792 | } |
793 | ||
794 | #ifdef CONFIG_ZLIB | |
97eabb2a JA |
795 | |
796 | struct iolog_flush_data { | |
155f2f02 | 797 | struct workqueue_work work; |
97eabb2a JA |
798 | struct io_log *log; |
799 | void *samples; | |
7e419452 JA |
800 | uint32_t nr_samples; |
801 | bool free; | |
97eabb2a JA |
802 | }; |
803 | ||
97eabb2a JA |
804 | #define GZ_CHUNK 131072 |
805 | ||
806 | static struct iolog_compress *get_new_chunk(unsigned int seq) | |
807 | { | |
808 | struct iolog_compress *c; | |
809 | ||
810 | c = malloc(sizeof(*c)); | |
811 | INIT_FLIST_HEAD(&c->list); | |
812 | c->buf = malloc(GZ_CHUNK); | |
813 | c->len = 0; | |
814 | c->seq = seq; | |
97eabb2a JA |
815 | return c; |
816 | } | |
817 | ||
818 | static void free_chunk(struct iolog_compress *ic) | |
819 | { | |
c07826cd JA |
820 | free(ic->buf); |
821 | free(ic); | |
97eabb2a JA |
822 | } |
823 | ||
b26317c9 | 824 | static int z_stream_init(z_stream *stream, int gz_hdr) |
aee2ab67 | 825 | { |
b26317c9 JA |
826 | int wbits = 15; |
827 | ||
acb2a69b | 828 | memset(stream, 0, sizeof(*stream)); |
aee2ab67 JA |
829 | stream->zalloc = Z_NULL; |
830 | stream->zfree = Z_NULL; | |
831 | stream->opaque = Z_NULL; | |
832 | stream->next_in = Z_NULL; | |
833 | ||
1a8e7458 JA |
834 | /* |
835 | * zlib magic - add 32 for auto-detection of gz header or not, | |
836 | * if we decide to store files in a gzip friendly format. | |
837 | */ | |
b26317c9 JA |
838 | if (gz_hdr) |
839 | wbits += 32; | |
840 | ||
841 | if (inflateInit2(stream, wbits) != Z_OK) | |
aee2ab67 JA |
842 | return 1; |
843 | ||
844 | return 0; | |
845 | } | |
846 | ||
1a8e7458 | 847 | struct inflate_chunk_iter { |
aee2ab67 | 848 | unsigned int seq; |
dad95da1 | 849 | int err; |
aee2ab67 JA |
850 | void *buf; |
851 | size_t buf_size; | |
852 | size_t buf_used; | |
853 | size_t chunk_sz; | |
854 | }; | |
855 | ||
b26317c9 | 856 | static void finish_chunk(z_stream *stream, FILE *f, |
1a8e7458 | 857 | struct inflate_chunk_iter *iter) |
aee2ab67 | 858 | { |
aee2ab67 JA |
859 | int ret; |
860 | ||
861 | ret = inflateEnd(stream); | |
862 | if (ret != Z_OK) | |
50f940f1 JA |
863 | log_err("fio: failed to end log inflation seq %d (%d)\n", |
864 | iter->seq, ret); | |
aee2ab67 | 865 | |
b26317c9 | 866 | flush_samples(f, iter->buf, iter->buf_used); |
aee2ab67 JA |
867 | free(iter->buf); |
868 | iter->buf = NULL; | |
869 | iter->buf_size = iter->buf_used = 0; | |
870 | } | |
871 | ||
1a8e7458 JA |
872 | /* |
873 | * Iterative chunk inflation. Handles cases where we cross into a new | |
874 | * sequence, doing flush finish of previous chunk if needed. | |
875 | */ | |
876 | static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f, | |
877 | z_stream *stream, struct inflate_chunk_iter *iter) | |
aee2ab67 | 878 | { |
0c56718d JA |
879 | size_t ret; |
880 | ||
0f989d2e | 881 | dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u\n", |
0c56718d JA |
882 | (unsigned long) ic->len, ic->seq); |
883 | ||
aee2ab67 JA |
884 | if (ic->seq != iter->seq) { |
885 | if (iter->seq) | |
b26317c9 | 886 | finish_chunk(stream, f, iter); |
aee2ab67 | 887 | |
b26317c9 | 888 | z_stream_init(stream, gz_hdr); |
aee2ab67 JA |
889 | iter->seq = ic->seq; |
890 | } | |
891 | ||
892 | stream->avail_in = ic->len; | |
893 | stream->next_in = ic->buf; | |
894 | ||
895 | if (!iter->buf_size) { | |
896 | iter->buf_size = iter->chunk_sz; | |
897 | iter->buf = malloc(iter->buf_size); | |
898 | } | |
899 | ||
900 | while (stream->avail_in) { | |
b26317c9 | 901 | size_t this_out = iter->buf_size - iter->buf_used; |
aee2ab67 JA |
902 | int err; |
903 | ||
b26317c9 | 904 | stream->avail_out = this_out; |
aee2ab67 JA |
905 | stream->next_out = iter->buf + iter->buf_used; |
906 | ||
907 | err = inflate(stream, Z_NO_FLUSH); | |
908 | if (err < 0) { | |
909 | log_err("fio: failed inflating log: %d\n", err); | |
dad95da1 | 910 | iter->err = err; |
aee2ab67 JA |
911 | break; |
912 | } | |
913 | ||
b26317c9 JA |
914 | iter->buf_used += this_out - stream->avail_out; |
915 | ||
916 | if (!stream->avail_out) { | |
917 | iter->buf_size += iter->chunk_sz; | |
918 | iter->buf = realloc(iter->buf, iter->buf_size); | |
919 | continue; | |
920 | } | |
921 | ||
922 | if (err == Z_STREAM_END) | |
923 | break; | |
aee2ab67 JA |
924 | } |
925 | ||
0c56718d JA |
926 | ret = (void *) stream->next_in - ic->buf; |
927 | ||
d0d0cf0a | 928 | dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) iter->buf_size); |
0c56718d JA |
929 | |
930 | return ret; | |
aee2ab67 JA |
931 | } |
932 | ||
1a8e7458 JA |
933 | /* |
934 | * Inflate stored compressed chunks, or write them directly to the log | |
935 | * file if so instructed. | |
936 | */ | |
dad95da1 | 937 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
aee2ab67 | 938 | { |
1a8e7458 | 939 | struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, }; |
aee2ab67 JA |
940 | z_stream stream; |
941 | ||
942 | while (!flist_empty(&log->chunk_list)) { | |
943 | struct iolog_compress *ic; | |
944 | ||
9342d5f8 | 945 | ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list); |
aee2ab67 | 946 | flist_del(&ic->list); |
b26317c9 | 947 | |
1a8e7458 JA |
948 | if (log->log_gz_store) { |
949 | size_t ret; | |
950 | ||
0c56718d JA |
951 | dprint(FD_COMPRESS, "log write chunk size=%lu, " |
952 | "seq=%u\n", (unsigned long) ic->len, ic->seq); | |
953 | ||
1a8e7458 | 954 | ret = fwrite(ic->buf, ic->len, 1, f); |
dad95da1 JA |
955 | if (ret != 1 || ferror(f)) { |
956 | iter.err = errno; | |
1a8e7458 | 957 | log_err("fio: error writing compressed log\n"); |
dad95da1 | 958 | } |
1a8e7458 JA |
959 | } else |
960 | inflate_chunk(ic, log->log_gz_store, f, &stream, &iter); | |
c07826cd JA |
961 | |
962 | free_chunk(ic); | |
aee2ab67 JA |
963 | } |
964 | ||
965 | if (iter.seq) { | |
b26317c9 | 966 | finish_chunk(&stream, f, &iter); |
aee2ab67 JA |
967 | free(iter.buf); |
968 | } | |
dad95da1 JA |
969 | |
970 | return iter.err; | |
aee2ab67 JA |
971 | } |
972 | ||
1a8e7458 JA |
973 | /* |
974 | * Open compressed log file and decompress the stored chunks and | |
975 | * write them to stdout. The chunks are stored sequentially in the | |
976 | * file, so we iterate over them and do them one-by-one. | |
977 | */ | |
b26317c9 JA |
978 | int iolog_file_inflate(const char *file) |
979 | { | |
1a8e7458 | 980 | struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, }; |
b26317c9 JA |
981 | struct iolog_compress ic; |
982 | z_stream stream; | |
983 | struct stat sb; | |
fd771971 | 984 | ssize_t ret; |
f302710c JA |
985 | size_t total; |
986 | void *buf; | |
b26317c9 JA |
987 | FILE *f; |
988 | ||
b26317c9 JA |
989 | f = fopen(file, "r"); |
990 | if (!f) { | |
991 | perror("fopen"); | |
992 | return 1; | |
993 | } | |
994 | ||
fd771971 JA |
995 | if (stat(file, &sb) < 0) { |
996 | fclose(f); | |
997 | perror("stat"); | |
998 | return 1; | |
999 | } | |
1000 | ||
f302710c | 1001 | ic.buf = buf = malloc(sb.st_size); |
b26317c9 | 1002 | ic.len = sb.st_size; |
b26317c9 JA |
1003 | ic.seq = 1; |
1004 | ||
1005 | ret = fread(ic.buf, ic.len, 1, f); | |
1006 | if (ret < 0) { | |
1007 | perror("fread"); | |
1008 | fclose(f); | |
bb1116fe | 1009 | free(buf); |
b26317c9 JA |
1010 | return 1; |
1011 | } else if (ret != 1) { | |
1012 | log_err("fio: short read on reading log\n"); | |
1013 | fclose(f); | |
bb1116fe | 1014 | free(buf); |
b26317c9 JA |
1015 | return 1; |
1016 | } | |
1017 | ||
1018 | fclose(f); | |
1019 | ||
1a8e7458 JA |
1020 | /* |
1021 | * Each chunk will return Z_STREAM_END. We don't know how many | |
1022 | * chunks are in the file, so we just keep looping and incrementing | |
1023 | * the sequence number until we have consumed the whole compressed | |
1024 | * file. | |
1025 | */ | |
f302710c JA |
1026 | total = ic.len; |
1027 | do { | |
8a1db9a1 | 1028 | size_t iret; |
f302710c | 1029 | |
8a1db9a1 JA |
1030 | iret = inflate_chunk(&ic, 1, stdout, &stream, &iter); |
1031 | total -= iret; | |
f302710c JA |
1032 | if (!total) |
1033 | break; | |
dad95da1 JA |
1034 | if (iter.err) |
1035 | break; | |
f302710c JA |
1036 | |
1037 | ic.seq++; | |
8a1db9a1 JA |
1038 | ic.len -= iret; |
1039 | ic.buf += iret; | |
f302710c | 1040 | } while (1); |
b26317c9 JA |
1041 | |
1042 | if (iter.seq) { | |
1043 | finish_chunk(&stream, stdout, &iter); | |
1044 | free(iter.buf); | |
1045 | } | |
1046 | ||
f302710c | 1047 | free(buf); |
dad95da1 | 1048 | return iter.err; |
b26317c9 JA |
1049 | } |
1050 | ||
aee2ab67 JA |
1051 | #else |
1052 | ||
dad95da1 | 1053 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
aee2ab67 | 1054 | { |
b7364e21 | 1055 | return 0; |
aee2ab67 JA |
1056 | } |
1057 | ||
a68c66b2 JA |
1058 | int iolog_file_inflate(const char *file) |
1059 | { | |
1060 | log_err("fio: log inflation not possible without zlib\n"); | |
1061 | return 1; | |
1062 | } | |
1063 | ||
aee2ab67 JA |
1064 | #endif |
1065 | ||
60a25727 | 1066 | void flush_log(struct io_log *log, bool do_append) |
aee2ab67 JA |
1067 | { |
1068 | void *buf; | |
1069 | FILE *f; | |
1070 | ||
3a5db920 JA |
1071 | if (!do_append) |
1072 | f = fopen(log->filename, "w"); | |
1073 | else | |
1074 | f = fopen(log->filename, "a"); | |
aee2ab67 JA |
1075 | if (!f) { |
1076 | perror("fopen log"); | |
1077 | return; | |
1078 | } | |
1079 | ||
1080 | buf = set_file_buffer(f); | |
1081 | ||
1a8e7458 | 1082 | inflate_gz_chunks(log, f); |
aee2ab67 | 1083 | |
7e419452 JA |
1084 | while (!flist_empty(&log->io_logs)) { |
1085 | struct io_logs *cur_log; | |
1086 | ||
1087 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); | |
1088 | flist_del_init(&cur_log->list); | |
1e613c9c | 1089 | |
a9179eeb | 1090 | if (log->td && log == log->td->clat_hist_log) |
1e613c9c | 1091 | flush_hist_samples(f, log->hist_coarseness, cur_log->log, |
868f6f03 | 1092 | log_sample_sz(log, cur_log)); |
1e613c9c | 1093 | else |
868f6f03 | 1094 | flush_samples(f, cur_log->log, log_sample_sz(log, cur_log)); |
1e613c9c | 1095 | |
90d2d53f | 1096 | sfree(cur_log); |
7e419452 | 1097 | } |
ac9b9101 JA |
1098 | |
1099 | fclose(f); | |
2e802282 | 1100 | clear_file_buffer(buf); |
ac9b9101 JA |
1101 | } |
1102 | ||
cb7e0ace | 1103 | static int finish_log(struct thread_data *td, struct io_log *log, int trylock) |
ac9b9101 | 1104 | { |
155f2f02 | 1105 | if (td->flags & TD_F_COMPRESS_LOG) |
7e419452 | 1106 | iolog_flush(log); |
aee2ab67 | 1107 | |
243bfe19 | 1108 | if (trylock) { |
cb7e0ace | 1109 | if (fio_trylock_file(log->filename)) |
243bfe19 JA |
1110 | return 1; |
1111 | } else | |
cb7e0ace | 1112 | fio_lock_file(log->filename); |
243bfe19 | 1113 | |
0cba0f91 | 1114 | if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend) |
cb7e0ace | 1115 | fio_send_iolog(td, log, log->filename); |
aee2ab67 | 1116 | else |
3a5db920 | 1117 | flush_log(log, !td->o.per_job_logs); |
243bfe19 | 1118 | |
cb7e0ace | 1119 | fio_unlock_file(log->filename); |
518dac09 | 1120 | free_log(log); |
243bfe19 | 1121 | return 0; |
ac9b9101 JA |
1122 | } |
1123 | ||
0cba0f91 JA |
1124 | size_t log_chunk_sizes(struct io_log *log) |
1125 | { | |
1126 | struct flist_head *entry; | |
1127 | size_t ret; | |
1128 | ||
1129 | if (flist_empty(&log->chunk_list)) | |
1130 | return 0; | |
1131 | ||
1132 | ret = 0; | |
1133 | pthread_mutex_lock(&log->chunk_lock); | |
1134 | flist_for_each(entry, &log->chunk_list) { | |
1135 | struct iolog_compress *c; | |
1136 | ||
1137 | c = flist_entry(entry, struct iolog_compress, list); | |
1138 | ret += c->len; | |
1139 | } | |
1140 | pthread_mutex_unlock(&log->chunk_lock); | |
1141 | return ret; | |
1142 | } | |
1143 | ||
aee2ab67 JA |
1144 | #ifdef CONFIG_ZLIB |
1145 | ||
858bce70 JA |
1146 | static bool warned_on_drop; |
1147 | ||
1148 | static void iolog_put_deferred(struct io_log *log, void *ptr) | |
1149 | { | |
1150 | if (!ptr) | |
1151 | return; | |
1152 | ||
1153 | pthread_mutex_lock(&log->deferred_free_lock); | |
1154 | if (log->deferred < IOLOG_MAX_DEFER) { | |
1155 | log->deferred_items[log->deferred] = ptr; | |
1156 | log->deferred++; | |
1157 | } else if (!warned_on_drop) { | |
1158 | log_err("fio: had to drop log entry free\n"); | |
1159 | warned_on_drop = true; | |
1160 | } | |
1161 | pthread_mutex_unlock(&log->deferred_free_lock); | |
1162 | } | |
1163 | ||
1164 | static void iolog_free_deferred(struct io_log *log) | |
1165 | { | |
1166 | int i; | |
1167 | ||
1168 | if (!log->deferred) | |
1169 | return; | |
1170 | ||
1171 | pthread_mutex_lock(&log->deferred_free_lock); | |
1172 | ||
1173 | for (i = 0; i < log->deferred; i++) { | |
1174 | free(log->deferred_items[i]); | |
1175 | log->deferred_items[i] = NULL; | |
1176 | } | |
1177 | ||
1178 | log->deferred = 0; | |
1179 | pthread_mutex_unlock(&log->deferred_free_lock); | |
1180 | } | |
1181 | ||
78e93aa6 | 1182 | static int gz_work(struct iolog_flush_data *data) |
c143980a | 1183 | { |
040238ec | 1184 | struct iolog_compress *c = NULL; |
aee2ab67 JA |
1185 | struct flist_head list; |
1186 | unsigned int seq; | |
1187 | z_stream stream; | |
1188 | size_t total = 0; | |
d73ac887 | 1189 | int ret; |
aee2ab67 JA |
1190 | |
1191 | INIT_FLIST_HEAD(&list); | |
1192 | ||
7e419452 | 1193 | memset(&stream, 0, sizeof(stream)); |
aee2ab67 JA |
1194 | stream.zalloc = Z_NULL; |
1195 | stream.zfree = Z_NULL; | |
1196 | stream.opaque = Z_NULL; | |
1197 | ||
b26317c9 | 1198 | ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION); |
b26317c9 | 1199 | if (ret != Z_OK) { |
aee2ab67 | 1200 | log_err("fio: failed to init gz stream\n"); |
152ea035 | 1201 | goto err; |
aee2ab67 JA |
1202 | } |
1203 | ||
1204 | seq = ++data->log->chunk_seq; | |
b26317c9 | 1205 | |
aee2ab67 JA |
1206 | stream.next_in = (void *) data->samples; |
1207 | stream.avail_in = data->nr_samples * log_entry_sz(data->log); | |
1208 | ||
d0d0cf0a JA |
1209 | dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u, log=%s\n", |
1210 | (unsigned long) stream.avail_in, seq, | |
1211 | data->log->filename); | |
aee2ab67 | 1212 | do { |
040238ec | 1213 | if (c) |
0caccfa7 JA |
1214 | dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, |
1215 | (unsigned long) c->len); | |
aee2ab67 JA |
1216 | c = get_new_chunk(seq); |
1217 | stream.avail_out = GZ_CHUNK; | |
1218 | stream.next_out = c->buf; | |
1219 | ret = deflate(&stream, Z_NO_FLUSH); | |
1220 | if (ret < 0) { | |
1221 | log_err("fio: deflate log (%d)\n", ret); | |
dad95da1 JA |
1222 | free_chunk(c); |
1223 | goto err; | |
aee2ab67 JA |
1224 | } |
1225 | ||
1226 | c->len = GZ_CHUNK - stream.avail_out; | |
1227 | flist_add_tail(&c->list, &list); | |
1228 | total += c->len; | |
1229 | } while (stream.avail_in); | |
1230 | ||
1231 | stream.next_out = c->buf + c->len; | |
1232 | stream.avail_out = GZ_CHUNK - c->len; | |
1233 | ||
1234 | ret = deflate(&stream, Z_FINISH); | |
6fa3ad51 JA |
1235 | if (ret < 0) { |
1236 | /* | |
1237 | * Z_BUF_ERROR is special, it just means we need more | |
1238 | * output space. We'll handle that below. Treat any other | |
1239 | * error as fatal. | |
1240 | */ | |
1241 | if (ret != Z_BUF_ERROR) { | |
1242 | log_err("fio: deflate log (%d)\n", ret); | |
1243 | flist_del(&c->list); | |
1244 | free_chunk(c); | |
1245 | goto err; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | total -= c->len; | |
1250 | c->len = GZ_CHUNK - stream.avail_out; | |
1251 | total += c->len; | |
0caccfa7 | 1252 | dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, (unsigned long) c->len); |
040238ec | 1253 | |
6fa3ad51 | 1254 | if (ret != Z_STREAM_END) { |
aee2ab67 JA |
1255 | do { |
1256 | c = get_new_chunk(seq); | |
1257 | stream.avail_out = GZ_CHUNK; | |
1258 | stream.next_out = c->buf; | |
1259 | ret = deflate(&stream, Z_FINISH); | |
1260 | c->len = GZ_CHUNK - stream.avail_out; | |
0c56718d | 1261 | total += c->len; |
aee2ab67 | 1262 | flist_add_tail(&c->list, &list); |
0caccfa7 JA |
1263 | dprint(FD_COMPRESS, "seq=%d, chunk=%lu\n", seq, |
1264 | (unsigned long) c->len); | |
aee2ab67 JA |
1265 | } while (ret != Z_STREAM_END); |
1266 | } | |
1267 | ||
0c56718d JA |
1268 | dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total); |
1269 | ||
aee2ab67 JA |
1270 | ret = deflateEnd(&stream); |
1271 | if (ret != Z_OK) | |
1272 | log_err("fio: deflateEnd %d\n", ret); | |
1273 | ||
858bce70 | 1274 | iolog_put_deferred(data->log, data->samples); |
aee2ab67 JA |
1275 | |
1276 | if (!flist_empty(&list)) { | |
1277 | pthread_mutex_lock(&data->log->chunk_lock); | |
1278 | flist_splice_tail(&list, &data->log->chunk_list); | |
1279 | pthread_mutex_unlock(&data->log->chunk_lock); | |
1280 | } | |
1281 | ||
dad95da1 JA |
1282 | ret = 0; |
1283 | done: | |
7e419452 | 1284 | if (data->free) |
dab41b1c | 1285 | sfree(data); |
dad95da1 JA |
1286 | return ret; |
1287 | err: | |
1288 | while (!flist_empty(&list)) { | |
1289 | c = flist_first_entry(list.next, struct iolog_compress, list); | |
1290 | flist_del(&c->list); | |
1291 | free_chunk(c); | |
1292 | } | |
1293 | ret = 1; | |
1294 | goto done; | |
aee2ab67 JA |
1295 | } |
1296 | ||
78e93aa6 JA |
1297 | /* |
1298 | * Invoked from our compress helper thread, when logging would have exceeded | |
1299 | * the specified memory limitation. Compresses the previously stored | |
1300 | * entries. | |
1301 | */ | |
1302 | static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work) | |
1303 | { | |
1304 | return gz_work(container_of(work, struct iolog_flush_data, work)); | |
1305 | } | |
1306 | ||
c08f9fe2 JA |
1307 | static int gz_init_worker(struct submit_worker *sw) |
1308 | { | |
1309 | struct thread_data *td = sw->wq->td; | |
1310 | ||
1311 | if (!fio_option_is_set(&td->o, log_gz_cpumask)) | |
1312 | return 0; | |
1313 | ||
1314 | if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) { | |
1315 | log_err("gz: failed to set CPU affinity\n"); | |
1316 | return 1; | |
1317 | } | |
1318 | ||
1319 | return 0; | |
1320 | } | |
1321 | ||
155f2f02 | 1322 | static struct workqueue_ops log_compress_wq_ops = { |
78e93aa6 | 1323 | .fn = gz_work_async, |
c08f9fe2 | 1324 | .init_worker_fn = gz_init_worker, |
24660963 | 1325 | .nice = 1, |
155f2f02 JA |
1326 | }; |
1327 | ||
24660963 | 1328 | int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out) |
155f2f02 JA |
1329 | { |
1330 | if (!(td->flags & TD_F_COMPRESS_LOG)) | |
1331 | return 0; | |
1332 | ||
24660963 | 1333 | workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out); |
155f2f02 JA |
1334 | return 0; |
1335 | } | |
1336 | ||
1337 | void iolog_compress_exit(struct thread_data *td) | |
1338 | { | |
1339 | if (!(td->flags & TD_F_COMPRESS_LOG)) | |
1340 | return; | |
1341 | ||
1342 | workqueue_exit(&td->log_compress_wq); | |
1343 | } | |
1344 | ||
1a8e7458 | 1345 | /* |
949ae6dc JA |
1346 | * Queue work item to compress the existing log entries. We reset the |
1347 | * current log to a small size, and reference the existing log in the | |
1348 | * data that we queue for compression. Once compression has been done, | |
7e419452 JA |
1349 | * this old log is freed. If called with finish == true, will not return |
1350 | * until the log compression has completed, and will flush all previous | |
1351 | * logs too | |
1a8e7458 | 1352 | */ |
7e419452 | 1353 | static int iolog_flush(struct io_log *log) |
aee2ab67 | 1354 | { |
aee2ab67 | 1355 | struct iolog_flush_data *data; |
aee2ab67 JA |
1356 | |
1357 | data = malloc(sizeof(*data)); | |
1358 | if (!data) | |
1359 | return 1; | |
1360 | ||
1361 | data->log = log; | |
7e419452 | 1362 | data->free = false; |
aee2ab67 | 1363 | |
7e419452 JA |
1364 | while (!flist_empty(&log->io_logs)) { |
1365 | struct io_logs *cur_log; | |
949ae6dc | 1366 | |
7e419452 JA |
1367 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); |
1368 | flist_del_init(&cur_log->list); | |
1369 | ||
1370 | data->samples = cur_log->log; | |
1371 | data->nr_samples = cur_log->nr_samples; | |
1372 | ||
2b7625e2 | 1373 | sfree(cur_log); |
aee2ab67 | 1374 | |
78e93aa6 | 1375 | gz_work(data); |
7e419452 | 1376 | } |
aee2ab67 | 1377 | |
7e419452 | 1378 | free(data); |
aee2ab67 JA |
1379 | return 0; |
1380 | } | |
1381 | ||
7e419452 JA |
1382 | int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log) |
1383 | { | |
1384 | struct iolog_flush_data *data; | |
1385 | ||
dab41b1c | 1386 | data = smalloc(sizeof(*data)); |
7e419452 JA |
1387 | if (!data) |
1388 | return 1; | |
1389 | ||
1390 | data->log = log; | |
1391 | ||
1392 | data->samples = cur_log->log; | |
1393 | data->nr_samples = cur_log->nr_samples; | |
1394 | data->free = true; | |
1395 | ||
1396 | cur_log->nr_samples = cur_log->max_samples = 0; | |
1397 | cur_log->log = NULL; | |
1398 | ||
1399 | workqueue_enqueue(&log->td->log_compress_wq, &data->work); | |
858bce70 JA |
1400 | |
1401 | iolog_free_deferred(log); | |
1402 | ||
7e419452 JA |
1403 | return 0; |
1404 | } | |
aee2ab67 JA |
1405 | #else |
1406 | ||
7e419452 JA |
1407 | static int iolog_flush(struct io_log *log) |
1408 | { | |
1409 | return 1; | |
1410 | } | |
1411 | ||
1412 | int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log) | |
aee2ab67 JA |
1413 | { |
1414 | return 1; | |
1415 | } | |
1416 | ||
24660963 | 1417 | int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out) |
52618a2a JA |
1418 | { |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | void iolog_compress_exit(struct thread_data *td) | |
1423 | { | |
1424 | } | |
1425 | ||
aee2ab67 JA |
1426 | #endif |
1427 | ||
7e419452 JA |
1428 | struct io_logs *iolog_cur_log(struct io_log *log) |
1429 | { | |
1430 | if (flist_empty(&log->io_logs)) | |
1431 | return NULL; | |
1432 | ||
1433 | return flist_last_entry(&log->io_logs, struct io_logs, list); | |
1434 | } | |
1435 | ||
1436 | uint64_t iolog_nr_samples(struct io_log *iolog) | |
1437 | { | |
1438 | struct flist_head *entry; | |
1439 | uint64_t ret = 0; | |
1440 | ||
1441 | flist_for_each(entry, &iolog->io_logs) { | |
1442 | struct io_logs *cur_log; | |
1443 | ||
1444 | cur_log = flist_entry(entry, struct io_logs, list); | |
1445 | ret += cur_log->nr_samples; | |
1446 | } | |
1447 | ||
1448 | return ret; | |
1449 | } | |
1450 | ||
1c725e70 | 1451 | static int __write_log(struct thread_data *td, struct io_log *log, int try) |
905e3d4f | 1452 | { |
1c725e70 JA |
1453 | if (log) |
1454 | return finish_log(td, log, try); | |
905e3d4f | 1455 | |
1c725e70 JA |
1456 | return 0; |
1457 | } | |
905e3d4f | 1458 | |
a47591e4 | 1459 | static int write_iops_log(struct thread_data *td, int try, bool unit_log) |
1c725e70 | 1460 | { |
a47591e4 JA |
1461 | int ret; |
1462 | ||
1463 | if (per_unit_log(td->iops_log) != unit_log) | |
1464 | return 0; | |
1465 | ||
1466 | ret = __write_log(td, td->iops_log, try); | |
1467 | if (!ret) | |
1468 | td->iops_log = NULL; | |
1469 | ||
1470 | return ret; | |
905e3d4f JA |
1471 | } |
1472 | ||
a47591e4 | 1473 | static int write_slat_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1474 | { |
a47591e4 JA |
1475 | int ret; |
1476 | ||
1477 | if (!unit_log) | |
1478 | return 0; | |
1479 | ||
1480 | ret = __write_log(td, td->slat_log, try); | |
1481 | if (!ret) | |
1482 | td->slat_log = NULL; | |
1483 | ||
1484 | return ret; | |
905e3d4f JA |
1485 | } |
1486 | ||
a47591e4 | 1487 | static int write_clat_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1488 | { |
a47591e4 JA |
1489 | int ret; |
1490 | ||
1491 | if (!unit_log) | |
1492 | return 0; | |
1493 | ||
1494 | ret = __write_log(td, td->clat_log, try); | |
1495 | if (!ret) | |
1496 | td->clat_log = NULL; | |
1497 | ||
1498 | return ret; | |
905e3d4f JA |
1499 | } |
1500 | ||
1e613c9c KC |
1501 | static int write_clat_hist_log(struct thread_data *td, int try, bool unit_log) |
1502 | { | |
1503 | int ret; | |
1504 | ||
1505 | if (!unit_log) | |
1506 | return 0; | |
1507 | ||
1508 | ret = __write_log(td, td->clat_hist_log, try); | |
1509 | if (!ret) | |
1510 | td->clat_hist_log = NULL; | |
1511 | ||
1512 | return ret; | |
1513 | } | |
1514 | ||
a47591e4 | 1515 | static int write_lat_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1516 | { |
a47591e4 JA |
1517 | int ret; |
1518 | ||
1519 | if (!unit_log) | |
1520 | return 0; | |
1521 | ||
1522 | ret = __write_log(td, td->lat_log, try); | |
1523 | if (!ret) | |
1524 | td->lat_log = NULL; | |
1525 | ||
1526 | return ret; | |
905e3d4f JA |
1527 | } |
1528 | ||
a47591e4 | 1529 | static int write_bandw_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1530 | { |
a47591e4 JA |
1531 | int ret; |
1532 | ||
1533 | if (per_unit_log(td->bw_log) != unit_log) | |
1534 | return 0; | |
1535 | ||
1536 | ret = __write_log(td, td->bw_log, try); | |
1537 | if (!ret) | |
1538 | td->bw_log = NULL; | |
1539 | ||
1540 | return ret; | |
905e3d4f JA |
1541 | } |
1542 | ||
1543 | enum { | |
1544 | BW_LOG_MASK = 1, | |
1545 | LAT_LOG_MASK = 2, | |
1546 | SLAT_LOG_MASK = 4, | |
1547 | CLAT_LOG_MASK = 8, | |
1548 | IOPS_LOG_MASK = 16, | |
1e613c9c | 1549 | CLAT_HIST_LOG_MASK = 32, |
905e3d4f | 1550 | |
1e613c9c | 1551 | ALL_LOG_NR = 6, |
905e3d4f JA |
1552 | }; |
1553 | ||
1554 | struct log_type { | |
1555 | unsigned int mask; | |
a47591e4 | 1556 | int (*fn)(struct thread_data *, int, bool); |
905e3d4f JA |
1557 | }; |
1558 | ||
1559 | static struct log_type log_types[] = { | |
1560 | { | |
1561 | .mask = BW_LOG_MASK, | |
1562 | .fn = write_bandw_log, | |
1563 | }, | |
1564 | { | |
1565 | .mask = LAT_LOG_MASK, | |
1566 | .fn = write_lat_log, | |
1567 | }, | |
1568 | { | |
1569 | .mask = SLAT_LOG_MASK, | |
1570 | .fn = write_slat_log, | |
1571 | }, | |
1572 | { | |
1573 | .mask = CLAT_LOG_MASK, | |
1574 | .fn = write_clat_log, | |
1575 | }, | |
1576 | { | |
1577 | .mask = IOPS_LOG_MASK, | |
1578 | .fn = write_iops_log, | |
1579 | }, | |
1e613c9c KC |
1580 | { |
1581 | .mask = CLAT_HIST_LOG_MASK, | |
1582 | .fn = write_clat_hist_log, | |
1583 | } | |
905e3d4f JA |
1584 | }; |
1585 | ||
a47591e4 | 1586 | void td_writeout_logs(struct thread_data *td, bool unit_logs) |
905e3d4f | 1587 | { |
ea5409f9 | 1588 | unsigned int log_mask = 0; |
905e3d4f JA |
1589 | unsigned int log_left = ALL_LOG_NR; |
1590 | int old_state, i; | |
1591 | ||
1592 | old_state = td_bump_runstate(td, TD_FINISHING); | |
1593 | ||
a47591e4 | 1594 | finalize_logs(td, unit_logs); |
905e3d4f JA |
1595 | |
1596 | while (log_left) { | |
1597 | int prev_log_left = log_left; | |
1598 | ||
1599 | for (i = 0; i < ALL_LOG_NR && log_left; i++) { | |
1600 | struct log_type *lt = &log_types[i]; | |
1601 | int ret; | |
1602 | ||
ea5409f9 | 1603 | if (!(log_mask & lt->mask)) { |
a47591e4 | 1604 | ret = lt->fn(td, log_left != 1, unit_logs); |
905e3d4f JA |
1605 | if (!ret) { |
1606 | log_left--; | |
ea5409f9 | 1607 | log_mask |= lt->mask; |
905e3d4f JA |
1608 | } |
1609 | } | |
1610 | } | |
1611 | ||
1612 | if (prev_log_left == log_left) | |
1613 | usleep(5000); | |
1614 | } | |
1615 | ||
1616 | td_restore_runstate(td, old_state); | |
1617 | } | |
a47591e4 JA |
1618 | |
1619 | void fio_writeout_logs(bool unit_logs) | |
1620 | { | |
1621 | struct thread_data *td; | |
1622 | int i; | |
1623 | ||
1624 | for_each_td(td, i) | |
1625 | td_writeout_logs(td, unit_logs); | |
1626 | } |