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