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