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