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