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