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