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