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; |
cb7e0ace | 587 | l->filename = strdup(filename); |
aee2ab67 JA |
588 | l->td = p->td; |
589 | ||
b26317c9 | 590 | if (l->log_offset) |
49e98daa | 591 | l->log_ddir_mask = LOG_OFFSET_SAMPLE_BIT; |
b26317c9 | 592 | |
aee2ab67 JA |
593 | INIT_FLIST_HEAD(&l->chunk_list); |
594 | ||
595 | if (l->log_gz && !p->td) | |
596 | l->log_gz = 0; | |
19417eda | 597 | else if (l->log_gz || l->log_gz_store) { |
aee2ab67 JA |
598 | pthread_mutex_init(&l->chunk_lock, NULL); |
599 | p->td->flags |= TD_F_COMPRESS_LOG; | |
600 | } | |
601 | ||
ac9b9101 JA |
602 | *log = l; |
603 | } | |
604 | ||
2e802282 JA |
605 | #ifdef CONFIG_SETVBUF |
606 | static void *set_file_buffer(FILE *f) | |
607 | { | |
608 | size_t size = 1048576; | |
609 | void *buf; | |
610 | ||
611 | buf = malloc(size); | |
612 | setvbuf(f, buf, _IOFBF, size); | |
613 | return buf; | |
614 | } | |
615 | ||
616 | static void clear_file_buffer(void *buf) | |
617 | { | |
618 | free(buf); | |
619 | } | |
620 | #else | |
621 | static void *set_file_buffer(FILE *f) | |
622 | { | |
623 | return NULL; | |
624 | } | |
625 | ||
626 | static void clear_file_buffer(void *buf) | |
627 | { | |
628 | } | |
629 | #endif | |
630 | ||
518dac09 | 631 | void free_log(struct io_log *log) |
cb7e0ace | 632 | { |
7e419452 JA |
633 | while (!flist_empty(&log->io_logs)) { |
634 | struct io_logs *cur_log; | |
635 | ||
636 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); | |
637 | flist_del_init(&cur_log->list); | |
638 | free(cur_log->log); | |
639 | } | |
640 | ||
cb7e0ace | 641 | free(log->filename); |
a47591e4 | 642 | sfree(log); |
cb7e0ace JA |
643 | } |
644 | ||
bead01d7 | 645 | void flush_samples(FILE *f, void *samples, uint64_t sample_size) |
ac9b9101 | 646 | { |
b26317c9 JA |
647 | struct io_sample *s; |
648 | int log_offset; | |
649 | uint64_t i, nr_samples; | |
650 | ||
651 | if (!sample_size) | |
652 | return; | |
653 | ||
654 | s = __get_sample(samples, 0, 0); | |
49e98daa | 655 | log_offset = (s->__ddir & LOG_OFFSET_SAMPLE_BIT) != 0; |
b26317c9 JA |
656 | |
657 | nr_samples = sample_size / __log_entry_sz(log_offset); | |
ac9b9101 | 658 | |
aee2ab67 | 659 | for (i = 0; i < nr_samples; i++) { |
b26317c9 | 660 | s = __get_sample(samples, log_offset, i); |
ac9b9101 | 661 | |
aee2ab67 | 662 | if (!log_offset) { |
ae588852 JA |
663 | fprintf(f, "%lu, %lu, %u, %u\n", |
664 | (unsigned long) s->time, | |
665 | (unsigned long) s->val, | |
b26317c9 | 666 | io_sample_ddir(s), s->bs); |
ae588852 JA |
667 | } else { |
668 | struct io_sample_offset *so = (void *) s; | |
669 | ||
670 | fprintf(f, "%lu, %lu, %u, %u, %llu\n", | |
671 | (unsigned long) s->time, | |
672 | (unsigned long) s->val, | |
b26317c9 | 673 | io_sample_ddir(s), s->bs, |
ae588852 JA |
674 | (unsigned long long) so->offset); |
675 | } | |
ac9b9101 | 676 | } |
aee2ab67 JA |
677 | } |
678 | ||
679 | #ifdef CONFIG_ZLIB | |
97eabb2a JA |
680 | |
681 | struct iolog_flush_data { | |
155f2f02 | 682 | struct workqueue_work work; |
97eabb2a JA |
683 | struct io_log *log; |
684 | void *samples; | |
7e419452 JA |
685 | uint32_t nr_samples; |
686 | bool free; | |
97eabb2a JA |
687 | }; |
688 | ||
97eabb2a JA |
689 | #define GZ_CHUNK 131072 |
690 | ||
691 | static struct iolog_compress *get_new_chunk(unsigned int seq) | |
692 | { | |
693 | struct iolog_compress *c; | |
694 | ||
695 | c = malloc(sizeof(*c)); | |
696 | INIT_FLIST_HEAD(&c->list); | |
697 | c->buf = malloc(GZ_CHUNK); | |
698 | c->len = 0; | |
699 | c->seq = seq; | |
97eabb2a JA |
700 | return c; |
701 | } | |
702 | ||
703 | static void free_chunk(struct iolog_compress *ic) | |
704 | { | |
c07826cd JA |
705 | free(ic->buf); |
706 | free(ic); | |
97eabb2a JA |
707 | } |
708 | ||
b26317c9 | 709 | static int z_stream_init(z_stream *stream, int gz_hdr) |
aee2ab67 | 710 | { |
b26317c9 JA |
711 | int wbits = 15; |
712 | ||
acb2a69b | 713 | memset(stream, 0, sizeof(*stream)); |
aee2ab67 JA |
714 | stream->zalloc = Z_NULL; |
715 | stream->zfree = Z_NULL; | |
716 | stream->opaque = Z_NULL; | |
717 | stream->next_in = Z_NULL; | |
718 | ||
1a8e7458 JA |
719 | /* |
720 | * zlib magic - add 32 for auto-detection of gz header or not, | |
721 | * if we decide to store files in a gzip friendly format. | |
722 | */ | |
b26317c9 JA |
723 | if (gz_hdr) |
724 | wbits += 32; | |
725 | ||
726 | if (inflateInit2(stream, wbits) != Z_OK) | |
aee2ab67 JA |
727 | return 1; |
728 | ||
729 | return 0; | |
730 | } | |
731 | ||
1a8e7458 | 732 | struct inflate_chunk_iter { |
aee2ab67 | 733 | unsigned int seq; |
dad95da1 | 734 | int err; |
aee2ab67 JA |
735 | void *buf; |
736 | size_t buf_size; | |
737 | size_t buf_used; | |
738 | size_t chunk_sz; | |
739 | }; | |
740 | ||
b26317c9 | 741 | static void finish_chunk(z_stream *stream, FILE *f, |
1a8e7458 | 742 | struct inflate_chunk_iter *iter) |
aee2ab67 | 743 | { |
aee2ab67 JA |
744 | int ret; |
745 | ||
746 | ret = inflateEnd(stream); | |
747 | if (ret != Z_OK) | |
748 | log_err("fio: failed to end log inflation (%d)\n", ret); | |
749 | ||
b26317c9 | 750 | flush_samples(f, iter->buf, iter->buf_used); |
aee2ab67 JA |
751 | free(iter->buf); |
752 | iter->buf = NULL; | |
753 | iter->buf_size = iter->buf_used = 0; | |
754 | } | |
755 | ||
1a8e7458 JA |
756 | /* |
757 | * Iterative chunk inflation. Handles cases where we cross into a new | |
758 | * sequence, doing flush finish of previous chunk if needed. | |
759 | */ | |
760 | static size_t inflate_chunk(struct iolog_compress *ic, int gz_hdr, FILE *f, | |
761 | z_stream *stream, struct inflate_chunk_iter *iter) | |
aee2ab67 | 762 | { |
0c56718d JA |
763 | size_t ret; |
764 | ||
0f989d2e | 765 | dprint(FD_COMPRESS, "inflate chunk size=%lu, seq=%u\n", |
0c56718d JA |
766 | (unsigned long) ic->len, ic->seq); |
767 | ||
aee2ab67 JA |
768 | if (ic->seq != iter->seq) { |
769 | if (iter->seq) | |
b26317c9 | 770 | finish_chunk(stream, f, iter); |
aee2ab67 | 771 | |
b26317c9 | 772 | z_stream_init(stream, gz_hdr); |
aee2ab67 JA |
773 | iter->seq = ic->seq; |
774 | } | |
775 | ||
776 | stream->avail_in = ic->len; | |
777 | stream->next_in = ic->buf; | |
778 | ||
779 | if (!iter->buf_size) { | |
780 | iter->buf_size = iter->chunk_sz; | |
781 | iter->buf = malloc(iter->buf_size); | |
782 | } | |
783 | ||
784 | while (stream->avail_in) { | |
b26317c9 | 785 | size_t this_out = iter->buf_size - iter->buf_used; |
aee2ab67 JA |
786 | int err; |
787 | ||
b26317c9 | 788 | stream->avail_out = this_out; |
aee2ab67 JA |
789 | stream->next_out = iter->buf + iter->buf_used; |
790 | ||
791 | err = inflate(stream, Z_NO_FLUSH); | |
792 | if (err < 0) { | |
793 | log_err("fio: failed inflating log: %d\n", err); | |
dad95da1 | 794 | iter->err = err; |
aee2ab67 JA |
795 | break; |
796 | } | |
797 | ||
b26317c9 JA |
798 | iter->buf_used += this_out - stream->avail_out; |
799 | ||
800 | if (!stream->avail_out) { | |
801 | iter->buf_size += iter->chunk_sz; | |
802 | iter->buf = realloc(iter->buf, iter->buf_size); | |
803 | continue; | |
804 | } | |
805 | ||
806 | if (err == Z_STREAM_END) | |
807 | break; | |
aee2ab67 JA |
808 | } |
809 | ||
0c56718d JA |
810 | ret = (void *) stream->next_in - ic->buf; |
811 | ||
812 | dprint(FD_COMPRESS, "inflated to size=%lu\n", (unsigned long) ret); | |
813 | ||
814 | return ret; | |
aee2ab67 JA |
815 | } |
816 | ||
1a8e7458 JA |
817 | /* |
818 | * Inflate stored compressed chunks, or write them directly to the log | |
819 | * file if so instructed. | |
820 | */ | |
dad95da1 | 821 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
aee2ab67 | 822 | { |
1a8e7458 | 823 | struct inflate_chunk_iter iter = { .chunk_sz = log->log_gz, }; |
aee2ab67 JA |
824 | z_stream stream; |
825 | ||
826 | while (!flist_empty(&log->chunk_list)) { | |
827 | struct iolog_compress *ic; | |
828 | ||
9342d5f8 | 829 | ic = flist_first_entry(&log->chunk_list, struct iolog_compress, list); |
aee2ab67 | 830 | flist_del(&ic->list); |
b26317c9 | 831 | |
1a8e7458 JA |
832 | if (log->log_gz_store) { |
833 | size_t ret; | |
834 | ||
0c56718d JA |
835 | dprint(FD_COMPRESS, "log write chunk size=%lu, " |
836 | "seq=%u\n", (unsigned long) ic->len, ic->seq); | |
837 | ||
1a8e7458 | 838 | ret = fwrite(ic->buf, ic->len, 1, f); |
dad95da1 JA |
839 | if (ret != 1 || ferror(f)) { |
840 | iter.err = errno; | |
1a8e7458 | 841 | log_err("fio: error writing compressed log\n"); |
dad95da1 | 842 | } |
1a8e7458 JA |
843 | } else |
844 | inflate_chunk(ic, log->log_gz_store, f, &stream, &iter); | |
c07826cd JA |
845 | |
846 | free_chunk(ic); | |
aee2ab67 JA |
847 | } |
848 | ||
849 | if (iter.seq) { | |
b26317c9 | 850 | finish_chunk(&stream, f, &iter); |
aee2ab67 JA |
851 | free(iter.buf); |
852 | } | |
dad95da1 JA |
853 | |
854 | return iter.err; | |
aee2ab67 JA |
855 | } |
856 | ||
1a8e7458 JA |
857 | /* |
858 | * Open compressed log file and decompress the stored chunks and | |
859 | * write them to stdout. The chunks are stored sequentially in the | |
860 | * file, so we iterate over them and do them one-by-one. | |
861 | */ | |
b26317c9 JA |
862 | int iolog_file_inflate(const char *file) |
863 | { | |
1a8e7458 | 864 | struct inflate_chunk_iter iter = { .chunk_sz = 64 * 1024 * 1024, }; |
b26317c9 JA |
865 | struct iolog_compress ic; |
866 | z_stream stream; | |
867 | struct stat sb; | |
fd771971 | 868 | ssize_t ret; |
f302710c JA |
869 | size_t total; |
870 | void *buf; | |
b26317c9 JA |
871 | FILE *f; |
872 | ||
b26317c9 JA |
873 | f = fopen(file, "r"); |
874 | if (!f) { | |
875 | perror("fopen"); | |
876 | return 1; | |
877 | } | |
878 | ||
fd771971 JA |
879 | if (stat(file, &sb) < 0) { |
880 | fclose(f); | |
881 | perror("stat"); | |
882 | return 1; | |
883 | } | |
884 | ||
f302710c | 885 | ic.buf = buf = malloc(sb.st_size); |
b26317c9 | 886 | ic.len = sb.st_size; |
b26317c9 JA |
887 | ic.seq = 1; |
888 | ||
889 | ret = fread(ic.buf, ic.len, 1, f); | |
890 | if (ret < 0) { | |
891 | perror("fread"); | |
892 | fclose(f); | |
bb1116fe | 893 | free(buf); |
b26317c9 JA |
894 | return 1; |
895 | } else if (ret != 1) { | |
896 | log_err("fio: short read on reading log\n"); | |
897 | fclose(f); | |
bb1116fe | 898 | free(buf); |
b26317c9 JA |
899 | return 1; |
900 | } | |
901 | ||
902 | fclose(f); | |
903 | ||
1a8e7458 JA |
904 | /* |
905 | * Each chunk will return Z_STREAM_END. We don't know how many | |
906 | * chunks are in the file, so we just keep looping and incrementing | |
907 | * the sequence number until we have consumed the whole compressed | |
908 | * file. | |
909 | */ | |
f302710c JA |
910 | total = ic.len; |
911 | do { | |
8a1db9a1 | 912 | size_t iret; |
f302710c | 913 | |
8a1db9a1 JA |
914 | iret = inflate_chunk(&ic, 1, stdout, &stream, &iter); |
915 | total -= iret; | |
f302710c JA |
916 | if (!total) |
917 | break; | |
dad95da1 JA |
918 | if (iter.err) |
919 | break; | |
f302710c JA |
920 | |
921 | ic.seq++; | |
8a1db9a1 JA |
922 | ic.len -= iret; |
923 | ic.buf += iret; | |
f302710c | 924 | } while (1); |
b26317c9 JA |
925 | |
926 | if (iter.seq) { | |
927 | finish_chunk(&stream, stdout, &iter); | |
928 | free(iter.buf); | |
929 | } | |
930 | ||
f302710c | 931 | free(buf); |
dad95da1 | 932 | return iter.err; |
b26317c9 JA |
933 | } |
934 | ||
aee2ab67 JA |
935 | #else |
936 | ||
dad95da1 | 937 | static int inflate_gz_chunks(struct io_log *log, FILE *f) |
aee2ab67 | 938 | { |
b7364e21 | 939 | return 0; |
aee2ab67 JA |
940 | } |
941 | ||
a68c66b2 JA |
942 | int iolog_file_inflate(const char *file) |
943 | { | |
944 | log_err("fio: log inflation not possible without zlib\n"); | |
945 | return 1; | |
946 | } | |
947 | ||
aee2ab67 JA |
948 | #endif |
949 | ||
3a5db920 | 950 | void flush_log(struct io_log *log, int do_append) |
aee2ab67 JA |
951 | { |
952 | void *buf; | |
953 | FILE *f; | |
954 | ||
3a5db920 JA |
955 | if (!do_append) |
956 | f = fopen(log->filename, "w"); | |
957 | else | |
958 | f = fopen(log->filename, "a"); | |
aee2ab67 JA |
959 | if (!f) { |
960 | perror("fopen log"); | |
961 | return; | |
962 | } | |
963 | ||
964 | buf = set_file_buffer(f); | |
965 | ||
1a8e7458 | 966 | inflate_gz_chunks(log, f); |
aee2ab67 | 967 | |
7e419452 JA |
968 | while (!flist_empty(&log->io_logs)) { |
969 | struct io_logs *cur_log; | |
970 | ||
971 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); | |
972 | flist_del_init(&cur_log->list); | |
973 | flush_samples(f, cur_log->log, cur_log->nr_samples * log_entry_sz(log)); | |
974 | } | |
ac9b9101 JA |
975 | |
976 | fclose(f); | |
2e802282 | 977 | clear_file_buffer(buf); |
ac9b9101 JA |
978 | } |
979 | ||
cb7e0ace | 980 | static int finish_log(struct thread_data *td, struct io_log *log, int trylock) |
ac9b9101 | 981 | { |
155f2f02 | 982 | if (td->flags & TD_F_COMPRESS_LOG) |
7e419452 | 983 | iolog_flush(log); |
aee2ab67 | 984 | |
243bfe19 | 985 | if (trylock) { |
cb7e0ace | 986 | if (fio_trylock_file(log->filename)) |
243bfe19 JA |
987 | return 1; |
988 | } else | |
cb7e0ace | 989 | fio_lock_file(log->filename); |
243bfe19 | 990 | |
0cba0f91 | 991 | if (td->client_type == FIO_CLIENT_TYPE_GUI || is_backend) |
cb7e0ace | 992 | fio_send_iolog(td, log, log->filename); |
aee2ab67 | 993 | else |
3a5db920 | 994 | flush_log(log, !td->o.per_job_logs); |
243bfe19 | 995 | |
cb7e0ace | 996 | fio_unlock_file(log->filename); |
518dac09 | 997 | free_log(log); |
243bfe19 | 998 | return 0; |
ac9b9101 JA |
999 | } |
1000 | ||
0cba0f91 JA |
1001 | size_t log_chunk_sizes(struct io_log *log) |
1002 | { | |
1003 | struct flist_head *entry; | |
1004 | size_t ret; | |
1005 | ||
1006 | if (flist_empty(&log->chunk_list)) | |
1007 | return 0; | |
1008 | ||
1009 | ret = 0; | |
1010 | pthread_mutex_lock(&log->chunk_lock); | |
1011 | flist_for_each(entry, &log->chunk_list) { | |
1012 | struct iolog_compress *c; | |
1013 | ||
1014 | c = flist_entry(entry, struct iolog_compress, list); | |
1015 | ret += c->len; | |
1016 | } | |
1017 | pthread_mutex_unlock(&log->chunk_lock); | |
1018 | return ret; | |
1019 | } | |
1020 | ||
aee2ab67 JA |
1021 | #ifdef CONFIG_ZLIB |
1022 | ||
78e93aa6 | 1023 | static int gz_work(struct iolog_flush_data *data) |
c143980a | 1024 | { |
aee2ab67 JA |
1025 | struct iolog_compress *c; |
1026 | struct flist_head list; | |
1027 | unsigned int seq; | |
1028 | z_stream stream; | |
1029 | size_t total = 0; | |
d73ac887 | 1030 | int ret; |
aee2ab67 JA |
1031 | |
1032 | INIT_FLIST_HEAD(&list); | |
1033 | ||
7e419452 | 1034 | memset(&stream, 0, sizeof(stream)); |
aee2ab67 JA |
1035 | stream.zalloc = Z_NULL; |
1036 | stream.zfree = Z_NULL; | |
1037 | stream.opaque = Z_NULL; | |
1038 | ||
b26317c9 | 1039 | ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION); |
b26317c9 | 1040 | if (ret != Z_OK) { |
aee2ab67 | 1041 | log_err("fio: failed to init gz stream\n"); |
152ea035 | 1042 | goto err; |
aee2ab67 JA |
1043 | } |
1044 | ||
1045 | seq = ++data->log->chunk_seq; | |
b26317c9 | 1046 | |
aee2ab67 JA |
1047 | stream.next_in = (void *) data->samples; |
1048 | stream.avail_in = data->nr_samples * log_entry_sz(data->log); | |
1049 | ||
0c56718d JA |
1050 | dprint(FD_COMPRESS, "deflate input size=%lu, seq=%u\n", |
1051 | (unsigned long) stream.avail_in, seq); | |
aee2ab67 JA |
1052 | do { |
1053 | c = get_new_chunk(seq); | |
1054 | stream.avail_out = GZ_CHUNK; | |
1055 | stream.next_out = c->buf; | |
1056 | ret = deflate(&stream, Z_NO_FLUSH); | |
1057 | if (ret < 0) { | |
1058 | log_err("fio: deflate log (%d)\n", ret); | |
dad95da1 JA |
1059 | free_chunk(c); |
1060 | goto err; | |
aee2ab67 JA |
1061 | } |
1062 | ||
1063 | c->len = GZ_CHUNK - stream.avail_out; | |
1064 | flist_add_tail(&c->list, &list); | |
1065 | total += c->len; | |
1066 | } while (stream.avail_in); | |
1067 | ||
1068 | stream.next_out = c->buf + c->len; | |
1069 | stream.avail_out = GZ_CHUNK - c->len; | |
1070 | ||
1071 | ret = deflate(&stream, Z_FINISH); | |
1072 | if (ret == Z_STREAM_END) | |
1073 | c->len = GZ_CHUNK - stream.avail_out; | |
1074 | else { | |
1075 | do { | |
1076 | c = get_new_chunk(seq); | |
1077 | stream.avail_out = GZ_CHUNK; | |
1078 | stream.next_out = c->buf; | |
1079 | ret = deflate(&stream, Z_FINISH); | |
1080 | c->len = GZ_CHUNK - stream.avail_out; | |
0c56718d | 1081 | total += c->len; |
aee2ab67 JA |
1082 | flist_add_tail(&c->list, &list); |
1083 | } while (ret != Z_STREAM_END); | |
1084 | } | |
1085 | ||
0c56718d JA |
1086 | dprint(FD_COMPRESS, "deflated to size=%lu\n", (unsigned long) total); |
1087 | ||
aee2ab67 JA |
1088 | ret = deflateEnd(&stream); |
1089 | if (ret != Z_OK) | |
1090 | log_err("fio: deflateEnd %d\n", ret); | |
1091 | ||
1092 | free(data->samples); | |
1093 | ||
1094 | if (!flist_empty(&list)) { | |
1095 | pthread_mutex_lock(&data->log->chunk_lock); | |
1096 | flist_splice_tail(&list, &data->log->chunk_list); | |
1097 | pthread_mutex_unlock(&data->log->chunk_lock); | |
1098 | } | |
1099 | ||
dad95da1 JA |
1100 | ret = 0; |
1101 | done: | |
7e419452 JA |
1102 | if (data->free) |
1103 | free(data); | |
dad95da1 JA |
1104 | return ret; |
1105 | err: | |
1106 | while (!flist_empty(&list)) { | |
1107 | c = flist_first_entry(list.next, struct iolog_compress, list); | |
1108 | flist_del(&c->list); | |
1109 | free_chunk(c); | |
1110 | } | |
1111 | ret = 1; | |
1112 | goto done; | |
aee2ab67 JA |
1113 | } |
1114 | ||
78e93aa6 JA |
1115 | /* |
1116 | * Invoked from our compress helper thread, when logging would have exceeded | |
1117 | * the specified memory limitation. Compresses the previously stored | |
1118 | * entries. | |
1119 | */ | |
1120 | static int gz_work_async(struct submit_worker *sw, struct workqueue_work *work) | |
1121 | { | |
1122 | return gz_work(container_of(work, struct iolog_flush_data, work)); | |
1123 | } | |
1124 | ||
c08f9fe2 JA |
1125 | static int gz_init_worker(struct submit_worker *sw) |
1126 | { | |
1127 | struct thread_data *td = sw->wq->td; | |
1128 | ||
1129 | if (!fio_option_is_set(&td->o, log_gz_cpumask)) | |
1130 | return 0; | |
1131 | ||
1132 | if (fio_setaffinity(gettid(), td->o.log_gz_cpumask) == -1) { | |
1133 | log_err("gz: failed to set CPU affinity\n"); | |
1134 | return 1; | |
1135 | } | |
1136 | ||
1137 | return 0; | |
1138 | } | |
1139 | ||
155f2f02 | 1140 | static struct workqueue_ops log_compress_wq_ops = { |
78e93aa6 | 1141 | .fn = gz_work_async, |
c08f9fe2 | 1142 | .init_worker_fn = gz_init_worker, |
24660963 | 1143 | .nice = 1, |
155f2f02 JA |
1144 | }; |
1145 | ||
24660963 | 1146 | int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out) |
155f2f02 JA |
1147 | { |
1148 | if (!(td->flags & TD_F_COMPRESS_LOG)) | |
1149 | return 0; | |
1150 | ||
24660963 | 1151 | workqueue_init(td, &td->log_compress_wq, &log_compress_wq_ops, 1, sk_out); |
155f2f02 JA |
1152 | return 0; |
1153 | } | |
1154 | ||
1155 | void iolog_compress_exit(struct thread_data *td) | |
1156 | { | |
1157 | if (!(td->flags & TD_F_COMPRESS_LOG)) | |
1158 | return; | |
1159 | ||
1160 | workqueue_exit(&td->log_compress_wq); | |
1161 | } | |
1162 | ||
1a8e7458 | 1163 | /* |
949ae6dc JA |
1164 | * Queue work item to compress the existing log entries. We reset the |
1165 | * current log to a small size, and reference the existing log in the | |
1166 | * data that we queue for compression. Once compression has been done, | |
7e419452 JA |
1167 | * this old log is freed. If called with finish == true, will not return |
1168 | * until the log compression has completed, and will flush all previous | |
1169 | * logs too | |
1a8e7458 | 1170 | */ |
7e419452 | 1171 | static int iolog_flush(struct io_log *log) |
aee2ab67 | 1172 | { |
aee2ab67 | 1173 | struct iolog_flush_data *data; |
aee2ab67 JA |
1174 | |
1175 | data = malloc(sizeof(*data)); | |
1176 | if (!data) | |
1177 | return 1; | |
1178 | ||
1179 | data->log = log; | |
7e419452 | 1180 | data->free = false; |
aee2ab67 | 1181 | |
7e419452 JA |
1182 | while (!flist_empty(&log->io_logs)) { |
1183 | struct io_logs *cur_log; | |
949ae6dc | 1184 | |
7e419452 JA |
1185 | cur_log = flist_first_entry(&log->io_logs, struct io_logs, list); |
1186 | flist_del_init(&cur_log->list); | |
1187 | ||
1188 | data->samples = cur_log->log; | |
1189 | data->nr_samples = cur_log->nr_samples; | |
1190 | ||
1191 | cur_log->nr_samples = 0; | |
1192 | cur_log->max_samples = 0; | |
1193 | cur_log->log = NULL; | |
aee2ab67 | 1194 | |
78e93aa6 | 1195 | gz_work(data); |
7e419452 | 1196 | } |
aee2ab67 | 1197 | |
7e419452 | 1198 | free(data); |
aee2ab67 JA |
1199 | return 0; |
1200 | } | |
1201 | ||
7e419452 JA |
1202 | int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log) |
1203 | { | |
1204 | struct iolog_flush_data *data; | |
1205 | ||
7e419452 JA |
1206 | data = malloc(sizeof(*data)); |
1207 | if (!data) | |
1208 | return 1; | |
1209 | ||
1210 | data->log = log; | |
1211 | ||
1212 | data->samples = cur_log->log; | |
1213 | data->nr_samples = cur_log->nr_samples; | |
1214 | data->free = true; | |
1215 | ||
1216 | cur_log->nr_samples = cur_log->max_samples = 0; | |
1217 | cur_log->log = NULL; | |
1218 | ||
1219 | workqueue_enqueue(&log->td->log_compress_wq, &data->work); | |
1220 | return 0; | |
1221 | } | |
aee2ab67 JA |
1222 | #else |
1223 | ||
7e419452 JA |
1224 | static int iolog_flush(struct io_log *log) |
1225 | { | |
1226 | return 1; | |
1227 | } | |
1228 | ||
1229 | int iolog_cur_flush(struct io_log *log, struct io_logs *cur_log) | |
aee2ab67 JA |
1230 | { |
1231 | return 1; | |
1232 | } | |
1233 | ||
24660963 | 1234 | int iolog_compress_init(struct thread_data *td, struct sk_out *sk_out) |
52618a2a JA |
1235 | { |
1236 | return 0; | |
1237 | } | |
1238 | ||
1239 | void iolog_compress_exit(struct thread_data *td) | |
1240 | { | |
1241 | } | |
1242 | ||
aee2ab67 JA |
1243 | #endif |
1244 | ||
7e419452 JA |
1245 | struct io_logs *iolog_cur_log(struct io_log *log) |
1246 | { | |
1247 | if (flist_empty(&log->io_logs)) | |
1248 | return NULL; | |
1249 | ||
1250 | return flist_last_entry(&log->io_logs, struct io_logs, list); | |
1251 | } | |
1252 | ||
1253 | uint64_t iolog_nr_samples(struct io_log *iolog) | |
1254 | { | |
1255 | struct flist_head *entry; | |
1256 | uint64_t ret = 0; | |
1257 | ||
1258 | flist_for_each(entry, &iolog->io_logs) { | |
1259 | struct io_logs *cur_log; | |
1260 | ||
1261 | cur_log = flist_entry(entry, struct io_logs, list); | |
1262 | ret += cur_log->nr_samples; | |
1263 | } | |
1264 | ||
1265 | return ret; | |
1266 | } | |
1267 | ||
1c725e70 | 1268 | static int __write_log(struct thread_data *td, struct io_log *log, int try) |
905e3d4f | 1269 | { |
1c725e70 JA |
1270 | if (log) |
1271 | return finish_log(td, log, try); | |
905e3d4f | 1272 | |
1c725e70 JA |
1273 | return 0; |
1274 | } | |
905e3d4f | 1275 | |
a47591e4 | 1276 | static int write_iops_log(struct thread_data *td, int try, bool unit_log) |
1c725e70 | 1277 | { |
a47591e4 JA |
1278 | int ret; |
1279 | ||
1280 | if (per_unit_log(td->iops_log) != unit_log) | |
1281 | return 0; | |
1282 | ||
1283 | ret = __write_log(td, td->iops_log, try); | |
1284 | if (!ret) | |
1285 | td->iops_log = NULL; | |
1286 | ||
1287 | return ret; | |
905e3d4f JA |
1288 | } |
1289 | ||
a47591e4 | 1290 | static int write_slat_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1291 | { |
a47591e4 JA |
1292 | int ret; |
1293 | ||
1294 | if (!unit_log) | |
1295 | return 0; | |
1296 | ||
1297 | ret = __write_log(td, td->slat_log, try); | |
1298 | if (!ret) | |
1299 | td->slat_log = NULL; | |
1300 | ||
1301 | return ret; | |
905e3d4f JA |
1302 | } |
1303 | ||
a47591e4 | 1304 | static int write_clat_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1305 | { |
a47591e4 JA |
1306 | int ret; |
1307 | ||
1308 | if (!unit_log) | |
1309 | return 0; | |
1310 | ||
1311 | ret = __write_log(td, td->clat_log, try); | |
1312 | if (!ret) | |
1313 | td->clat_log = NULL; | |
1314 | ||
1315 | return ret; | |
905e3d4f JA |
1316 | } |
1317 | ||
a47591e4 | 1318 | static int write_lat_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1319 | { |
a47591e4 JA |
1320 | int ret; |
1321 | ||
1322 | if (!unit_log) | |
1323 | return 0; | |
1324 | ||
1325 | ret = __write_log(td, td->lat_log, try); | |
1326 | if (!ret) | |
1327 | td->lat_log = NULL; | |
1328 | ||
1329 | return ret; | |
905e3d4f JA |
1330 | } |
1331 | ||
a47591e4 | 1332 | static int write_bandw_log(struct thread_data *td, int try, bool unit_log) |
905e3d4f | 1333 | { |
a47591e4 JA |
1334 | int ret; |
1335 | ||
1336 | if (per_unit_log(td->bw_log) != unit_log) | |
1337 | return 0; | |
1338 | ||
1339 | ret = __write_log(td, td->bw_log, try); | |
1340 | if (!ret) | |
1341 | td->bw_log = NULL; | |
1342 | ||
1343 | return ret; | |
905e3d4f JA |
1344 | } |
1345 | ||
1346 | enum { | |
1347 | BW_LOG_MASK = 1, | |
1348 | LAT_LOG_MASK = 2, | |
1349 | SLAT_LOG_MASK = 4, | |
1350 | CLAT_LOG_MASK = 8, | |
1351 | IOPS_LOG_MASK = 16, | |
1352 | ||
905e3d4f JA |
1353 | ALL_LOG_NR = 5, |
1354 | }; | |
1355 | ||
1356 | struct log_type { | |
1357 | unsigned int mask; | |
a47591e4 | 1358 | int (*fn)(struct thread_data *, int, bool); |
905e3d4f JA |
1359 | }; |
1360 | ||
1361 | static struct log_type log_types[] = { | |
1362 | { | |
1363 | .mask = BW_LOG_MASK, | |
1364 | .fn = write_bandw_log, | |
1365 | }, | |
1366 | { | |
1367 | .mask = LAT_LOG_MASK, | |
1368 | .fn = write_lat_log, | |
1369 | }, | |
1370 | { | |
1371 | .mask = SLAT_LOG_MASK, | |
1372 | .fn = write_slat_log, | |
1373 | }, | |
1374 | { | |
1375 | .mask = CLAT_LOG_MASK, | |
1376 | .fn = write_clat_log, | |
1377 | }, | |
1378 | { | |
1379 | .mask = IOPS_LOG_MASK, | |
1380 | .fn = write_iops_log, | |
1381 | }, | |
1382 | }; | |
1383 | ||
a47591e4 | 1384 | void td_writeout_logs(struct thread_data *td, bool unit_logs) |
905e3d4f | 1385 | { |
ea5409f9 | 1386 | unsigned int log_mask = 0; |
905e3d4f JA |
1387 | unsigned int log_left = ALL_LOG_NR; |
1388 | int old_state, i; | |
1389 | ||
1390 | old_state = td_bump_runstate(td, TD_FINISHING); | |
1391 | ||
a47591e4 | 1392 | finalize_logs(td, unit_logs); |
905e3d4f JA |
1393 | |
1394 | while (log_left) { | |
1395 | int prev_log_left = log_left; | |
1396 | ||
1397 | for (i = 0; i < ALL_LOG_NR && log_left; i++) { | |
1398 | struct log_type *lt = &log_types[i]; | |
1399 | int ret; | |
1400 | ||
ea5409f9 | 1401 | if (!(log_mask & lt->mask)) { |
a47591e4 | 1402 | ret = lt->fn(td, log_left != 1, unit_logs); |
905e3d4f JA |
1403 | if (!ret) { |
1404 | log_left--; | |
ea5409f9 | 1405 | log_mask |= lt->mask; |
905e3d4f JA |
1406 | } |
1407 | } | |
1408 | } | |
1409 | ||
1410 | if (prev_log_left == log_left) | |
1411 | usleep(5000); | |
1412 | } | |
1413 | ||
1414 | td_restore_runstate(td, old_state); | |
1415 | } | |
a47591e4 JA |
1416 | |
1417 | void fio_writeout_logs(bool unit_logs) | |
1418 | { | |
1419 | struct thread_data *td; | |
1420 | int i; | |
1421 | ||
1422 | for_each_td(td, i) | |
1423 | td_writeout_logs(td, unit_logs); | |
1424 | } |