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