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