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