Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | /* |
2 | * fio - the flexible io tester | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
27 | #include <signal.h> | |
28 | #include <time.h> | |
29 | #include <math.h> | |
30 | #include <assert.h> | |
31 | #include <dirent.h> | |
32 | #include <libgen.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/wait.h> | |
36 | #include <sys/ipc.h> | |
37 | #include <sys/shm.h> | |
38 | #include <sys/ioctl.h> | |
39 | #include <sys/mman.h> | |
40 | ||
41 | #include "fio.h" | |
42 | #include "os.h" | |
43 | ||
44 | #define MASK (4095) | |
45 | ||
46 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
47 | ||
48 | int groupid = 0; | |
49 | int thread_number = 0; | |
50 | static char run_str[MAX_JOBS + 1]; | |
51 | int shm_id = 0; | |
52 | static LIST_HEAD(disk_list); | |
53 | static struct itimerval itimer; | |
54 | ||
55 | static void update_io_ticks(void); | |
56 | static void disk_util_timer_arm(void); | |
57 | static void print_thread_status(void); | |
58 | ||
59 | /* | |
60 | * thread life cycle | |
61 | */ | |
62 | enum { | |
63 | TD_NOT_CREATED = 0, | |
64 | TD_CREATED, | |
65 | TD_RUNNING, | |
66 | TD_VERIFYING, | |
67 | TD_EXITED, | |
68 | TD_REAPED, | |
69 | }; | |
70 | ||
71 | #define should_fsync(td) (td_write(td) && (!(td)->odirect || (td)->override_sync)) | |
72 | ||
73 | static sem_t startup_sem; | |
74 | ||
75 | #define TERMINATE_ALL (-1) | |
76 | ||
77 | static void terminate_threads(int group_id) | |
78 | { | |
79 | int i; | |
80 | ||
81 | for (i = 0; i < thread_number; i++) { | |
82 | struct thread_data *td = &threads[i]; | |
83 | ||
84 | if (group_id == TERMINATE_ALL || groupid == td->groupid) { | |
85 | td->terminate = 1; | |
86 | td->start_delay = 0; | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | static void sig_handler(int sig) | |
92 | { | |
93 | switch (sig) { | |
94 | case SIGALRM: | |
95 | update_io_ticks(); | |
96 | disk_util_timer_arm(); | |
97 | print_thread_status(); | |
98 | break; | |
99 | default: | |
100 | printf("\nfio: terminating on signal\n"); | |
101 | fflush(stdout); | |
102 | terminate_threads(TERMINATE_ALL); | |
103 | break; | |
104 | } | |
105 | } | |
106 | ||
107 | static unsigned long utime_since(struct timeval *s, struct timeval *e) | |
108 | { | |
109 | double sec, usec; | |
110 | ||
111 | sec = e->tv_sec - s->tv_sec; | |
112 | usec = e->tv_usec - s->tv_usec; | |
113 | if (sec > 0 && usec < 0) { | |
114 | sec--; | |
115 | usec += 1000000; | |
116 | } | |
117 | ||
118 | sec *= (double) 1000000; | |
119 | ||
120 | return sec + usec; | |
121 | } | |
122 | ||
123 | static unsigned long utime_since_now(struct timeval *s) | |
124 | { | |
125 | struct timeval t; | |
126 | ||
127 | gettimeofday(&t, NULL); | |
128 | return utime_since(s, &t); | |
129 | } | |
130 | ||
131 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) | |
132 | { | |
133 | double sec, usec; | |
134 | ||
135 | sec = e->tv_sec - s->tv_sec; | |
136 | usec = e->tv_usec - s->tv_usec; | |
137 | if (sec > 0 && usec < 0) { | |
138 | sec--; | |
139 | usec += 1000000; | |
140 | } | |
141 | ||
142 | sec *= (double) 1000; | |
143 | usec /= (double) 1000; | |
144 | ||
145 | return sec + usec; | |
146 | } | |
147 | ||
148 | static unsigned long mtime_since_now(struct timeval *s) | |
149 | { | |
150 | struct timeval t; | |
151 | ||
152 | gettimeofday(&t, NULL); | |
153 | return mtime_since(s, &t); | |
154 | } | |
155 | ||
156 | static inline unsigned long msec_now(struct timeval *s) | |
157 | { | |
158 | return s->tv_sec * 1000 + s->tv_usec / 1000; | |
159 | } | |
160 | ||
161 | static int random_map_free(struct thread_data *td, unsigned long long block) | |
162 | { | |
163 | unsigned int idx = RAND_MAP_IDX(td, block); | |
164 | unsigned int bit = RAND_MAP_BIT(td, block); | |
165 | ||
166 | return (td->file_map[idx] & (1UL << bit)) == 0; | |
167 | } | |
168 | ||
169 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) | |
170 | { | |
171 | int i; | |
172 | ||
173 | *b = 0; | |
174 | i = 0; | |
175 | while ((*b) * td->min_bs < td->io_size) { | |
176 | if (td->file_map[i] != -1UL) { | |
177 | *b += ffz(td->file_map[i]); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | *b += BLOCKS_PER_MAP; | |
182 | i++; | |
183 | } | |
184 | ||
185 | return 1; | |
186 | } | |
187 | ||
188 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) | |
189 | { | |
190 | unsigned long block = io_u->offset / td->min_bs; | |
191 | unsigned int blocks = 0; | |
192 | ||
193 | while (blocks < (io_u->buflen / td->min_bs)) { | |
194 | unsigned int idx, bit; | |
195 | ||
196 | if (!random_map_free(td, block)) | |
197 | break; | |
198 | ||
199 | idx = RAND_MAP_IDX(td, block); | |
200 | bit = RAND_MAP_BIT(td, block); | |
201 | ||
202 | assert(idx < td->num_maps); | |
203 | ||
204 | td->file_map[idx] |= (1UL << bit); | |
205 | block++; | |
206 | blocks++; | |
207 | } | |
208 | ||
209 | if ((blocks * td->min_bs) < io_u->buflen) | |
210 | io_u->buflen = blocks * td->min_bs; | |
211 | } | |
212 | ||
213 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) | |
214 | { | |
215 | unsigned long long b, rb; | |
216 | long r; | |
217 | ||
218 | if (!td->sequential) { | |
219 | unsigned long max_blocks = td->io_size / td->min_bs; | |
220 | int loops = 50; | |
221 | ||
222 | do { | |
223 | lrand48_r(&td->random_state, &r); | |
224 | b = ((max_blocks - 1) * r / (RAND_MAX+1.0)); | |
225 | rb = b + (td->file_offset / td->min_bs); | |
226 | loops--; | |
227 | } while (!random_map_free(td, rb) && loops); | |
228 | ||
229 | if (!loops) { | |
230 | if (get_next_free_block(td, &b)) | |
231 | return 1; | |
232 | } | |
233 | } else | |
234 | b = td->last_bytes / td->min_bs; | |
235 | ||
236 | *offset = (b * td->min_bs) + td->file_offset; | |
237 | if (*offset > td->file_size) | |
238 | return 1; | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
243 | static unsigned int get_next_buflen(struct thread_data *td) | |
244 | { | |
245 | unsigned int buflen; | |
246 | long r; | |
247 | ||
248 | if (td->min_bs == td->max_bs) | |
249 | buflen = td->min_bs; | |
250 | else { | |
251 | lrand48_r(&td->bsrange_state, &r); | |
252 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); | |
253 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); | |
254 | } | |
255 | ||
256 | if (buflen > td->io_size - td->this_io_bytes[td->ddir]) | |
257 | buflen = td->io_size - td->this_io_bytes[td->ddir]; | |
258 | ||
259 | return buflen; | |
260 | } | |
261 | ||
262 | static inline void add_stat_sample(struct io_stat *is, unsigned long val) | |
263 | { | |
264 | if (val > is->max_val) | |
265 | is->max_val = val; | |
266 | if (val < is->min_val) | |
267 | is->min_val = val; | |
268 | ||
269 | is->val += val; | |
270 | is->val_sq += val * val; | |
271 | is->samples++; | |
272 | } | |
273 | ||
274 | static void add_log_sample(struct thread_data *td, struct io_log *iolog, | |
275 | unsigned long val, int ddir) | |
276 | { | |
277 | if (iolog->nr_samples == iolog->max_samples) { | |
278 | int new_size = sizeof(struct io_sample) * iolog->max_samples*2; | |
279 | ||
280 | iolog->log = realloc(iolog->log, new_size); | |
281 | iolog->max_samples <<= 1; | |
282 | } | |
283 | ||
284 | iolog->log[iolog->nr_samples].val = val; | |
285 | iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch); | |
286 | iolog->log[iolog->nr_samples].ddir = ddir; | |
287 | iolog->nr_samples++; | |
288 | } | |
289 | ||
290 | static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec) | |
291 | { | |
292 | add_stat_sample(&td->clat_stat[ddir], msec); | |
293 | ||
294 | if (td->clat_log) | |
295 | add_log_sample(td, td->clat_log, msec, ddir); | |
296 | } | |
297 | ||
298 | static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec) | |
299 | { | |
300 | add_stat_sample(&td->slat_stat[ddir], msec); | |
301 | ||
302 | if (td->slat_log) | |
303 | add_log_sample(td, td->slat_log, msec, ddir); | |
304 | } | |
305 | ||
306 | static void add_bw_sample(struct thread_data *td, int ddir) | |
307 | { | |
308 | unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]); | |
309 | unsigned long rate; | |
310 | ||
311 | if (spent < td->bw_avg_time) | |
312 | return; | |
313 | ||
314 | rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent; | |
315 | add_stat_sample(&td->bw_stat[ddir], rate); | |
316 | ||
317 | if (td->bw_log) | |
318 | add_log_sample(td, td->bw_log, rate, ddir); | |
319 | ||
320 | gettimeofday(&td->stat_sample_time[ddir], NULL); | |
321 | td->stat_io_bytes[ddir] = td->this_io_bytes[ddir]; | |
322 | } | |
323 | ||
324 | /* | |
325 | * busy looping version for the last few usec | |
326 | */ | |
327 | static void __usec_sleep(unsigned int usec) | |
328 | { | |
329 | struct timeval start; | |
330 | ||
331 | gettimeofday(&start, NULL); | |
332 | while (utime_since_now(&start) < usec) | |
333 | nop; | |
334 | } | |
335 | ||
336 | static void usec_sleep(struct thread_data *td, unsigned long usec) | |
337 | { | |
338 | struct timespec req, rem; | |
339 | ||
340 | req.tv_sec = usec / 1000000; | |
341 | req.tv_nsec = usec * 1000 - req.tv_sec * 1000000; | |
342 | ||
343 | do { | |
344 | if (usec < 5000) { | |
345 | __usec_sleep(usec); | |
346 | break; | |
347 | } | |
348 | ||
349 | rem.tv_sec = rem.tv_nsec = 0; | |
350 | if (nanosleep(&req, &rem) < 0) | |
351 | break; | |
352 | ||
353 | if ((rem.tv_sec + rem.tv_nsec) == 0) | |
354 | break; | |
355 | ||
356 | req.tv_nsec = rem.tv_nsec; | |
357 | req.tv_sec = rem.tv_sec; | |
358 | ||
359 | usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000; | |
360 | } while (!td->terminate); | |
361 | } | |
362 | ||
363 | static void rate_throttle(struct thread_data *td, unsigned long time_spent, | |
364 | unsigned int bytes) | |
365 | { | |
366 | unsigned long usec_cycle; | |
367 | ||
368 | if (!td->rate) | |
369 | return; | |
370 | ||
371 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs); | |
372 | ||
373 | if (time_spent < usec_cycle) { | |
374 | unsigned long s = usec_cycle - time_spent; | |
375 | ||
376 | td->rate_pending_usleep += s; | |
377 | if (td->rate_pending_usleep >= 100000) { | |
378 | usec_sleep(td, td->rate_pending_usleep); | |
379 | td->rate_pending_usleep = 0; | |
380 | } | |
381 | } else { | |
382 | long overtime = time_spent - usec_cycle; | |
383 | ||
384 | td->rate_pending_usleep -= overtime; | |
385 | } | |
386 | } | |
387 | ||
388 | static int check_min_rate(struct thread_data *td, struct timeval *now) | |
389 | { | |
390 | unsigned long spent; | |
391 | unsigned long rate; | |
392 | int ddir = td->ddir; | |
393 | ||
394 | /* | |
395 | * allow a 2 second settle period in the beginning | |
396 | */ | |
397 | if (mtime_since(&td->start, now) < 2000) | |
398 | return 0; | |
399 | ||
400 | /* | |
401 | * if rate blocks is set, sample is running | |
402 | */ | |
403 | if (td->rate_bytes) { | |
404 | spent = mtime_since(&td->lastrate, now); | |
405 | if (spent < td->ratecycle) | |
406 | return 0; | |
407 | ||
408 | rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent; | |
409 | if (rate < td->ratemin) { | |
410 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); | |
411 | if (rate_quit) | |
412 | terminate_threads(td->groupid); | |
413 | return 1; | |
414 | } | |
415 | } | |
416 | ||
417 | td->rate_bytes = td->this_io_bytes[ddir]; | |
418 | memcpy(&td->lastrate, now, sizeof(*now)); | |
419 | return 0; | |
420 | } | |
421 | ||
422 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) | |
423 | { | |
424 | if (!td->timeout) | |
425 | return 0; | |
426 | if (mtime_since(&td->epoch, t) >= td->timeout * 1000) | |
427 | return 1; | |
428 | ||
429 | return 0; | |
430 | } | |
431 | ||
432 | static void fill_random_bytes(struct thread_data *td, | |
433 | unsigned char *p, unsigned int len) | |
434 | { | |
435 | unsigned int todo; | |
436 | double r; | |
437 | ||
438 | while (len) { | |
439 | drand48_r(&td->verify_state, &r); | |
440 | ||
441 | /* | |
442 | * lrand48_r seems to be broken and only fill the bottom | |
443 | * 32-bits, even on 64-bit archs with 64-bit longs | |
444 | */ | |
445 | todo = sizeof(r); | |
446 | if (todo > len) | |
447 | todo = len; | |
448 | ||
449 | memcpy(p, &r, todo); | |
450 | ||
451 | len -= todo; | |
452 | p += todo; | |
453 | } | |
454 | } | |
455 | ||
456 | static void hexdump(void *buffer, int len) | |
457 | { | |
458 | unsigned char *p = buffer; | |
459 | int i; | |
460 | ||
461 | for (i = 0; i < len; i++) | |
462 | printf("%02x", p[i]); | |
463 | printf("\n"); | |
464 | } | |
465 | ||
466 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) | |
467 | { | |
468 | unsigned char *p = (unsigned char *) io_u->buf; | |
469 | unsigned long c; | |
470 | int ret; | |
471 | ||
472 | p += sizeof(*hdr); | |
473 | c = crc32(p, hdr->len - sizeof(*hdr)); | |
474 | ret = c != hdr->crc32; | |
475 | ||
476 | if (ret) { | |
477 | fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); | |
478 | fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c); | |
479 | } | |
480 | ||
481 | return ret; | |
482 | } | |
483 | ||
484 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) | |
485 | { | |
486 | unsigned char *p = (unsigned char *) io_u->buf; | |
487 | struct md5_ctx md5_ctx; | |
488 | int ret; | |
489 | ||
490 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
491 | p += sizeof(*hdr); | |
492 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); | |
493 | ||
494 | ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
495 | if (ret) { | |
496 | fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen); | |
497 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); | |
498 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); | |
499 | } | |
500 | ||
501 | return ret; | |
502 | } | |
503 | ||
504 | static int verify_io_u(struct io_u *io_u) | |
505 | { | |
506 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
507 | int ret; | |
508 | ||
509 | if (hdr->fio_magic != FIO_HDR_MAGIC) | |
510 | return 1; | |
511 | ||
512 | if (hdr->verify_type == VERIFY_MD5) | |
513 | ret = verify_io_u_md5(hdr, io_u); | |
514 | else if (hdr->verify_type == VERIFY_CRC32) | |
515 | ret = verify_io_u_crc32(hdr, io_u); | |
516 | else { | |
517 | fprintf(stderr, "Bad verify type %d\n", hdr->verify_type); | |
518 | ret = 1; | |
519 | } | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
524 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) | |
525 | { | |
526 | hdr->crc32 = crc32(p, len); | |
527 | } | |
528 | ||
529 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) | |
530 | { | |
531 | struct md5_ctx md5_ctx; | |
532 | ||
533 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
534 | md5_update(&md5_ctx, p, len); | |
535 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
536 | } | |
537 | ||
538 | /* | |
539 | * fill body of io_u->buf with random data and add a header with the | |
540 | * (eg) sha1sum of that data. | |
541 | */ | |
542 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) | |
543 | { | |
544 | unsigned char *p = (unsigned char *) io_u->buf; | |
545 | struct verify_header hdr; | |
546 | ||
547 | hdr.fio_magic = FIO_HDR_MAGIC; | |
548 | hdr.len = io_u->buflen; | |
549 | p += sizeof(hdr); | |
550 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); | |
551 | ||
552 | if (td->verify == VERIFY_MD5) { | |
553 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); | |
554 | hdr.verify_type = VERIFY_MD5; | |
555 | } else { | |
556 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); | |
557 | hdr.verify_type = VERIFY_CRC32; | |
558 | } | |
559 | ||
560 | memcpy(io_u->buf, &hdr, sizeof(hdr)); | |
561 | } | |
562 | ||
563 | static void put_io_u(struct thread_data *td, struct io_u *io_u) | |
564 | { | |
565 | list_del(&io_u->list); | |
566 | list_add(&io_u->list, &td->io_u_freelist); | |
567 | td->cur_depth--; | |
568 | } | |
569 | ||
570 | #define queue_full(td) (list_empty(&(td)->io_u_freelist)) | |
571 | ||
572 | static struct io_u *__get_io_u(struct thread_data *td) | |
573 | { | |
574 | struct io_u *io_u; | |
575 | ||
576 | if (queue_full(td)) | |
577 | return NULL; | |
578 | ||
579 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); | |
580 | io_u->error = 0; | |
581 | io_u->resid = 0; | |
582 | list_del(&io_u->list); | |
583 | list_add(&io_u->list, &td->io_u_busylist); | |
584 | td->cur_depth++; | |
585 | return io_u; | |
586 | } | |
587 | ||
588 | static int td_io_prep(struct thread_data *td, struct io_u *io_u, int read) | |
589 | { | |
590 | if (read) | |
591 | io_u->ddir = DDIR_READ; | |
592 | else | |
593 | io_u->ddir = DDIR_WRITE; | |
594 | ||
595 | if (td->io_prep && td->io_prep(td, io_u)) | |
596 | return 1; | |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
601 | static struct io_u *get_io_u(struct thread_data *td) | |
602 | { | |
603 | struct io_u *io_u; | |
604 | ||
605 | io_u = __get_io_u(td); | |
606 | if (!io_u) | |
607 | return NULL; | |
608 | ||
609 | if (get_next_offset(td, &io_u->offset)) { | |
610 | put_io_u(td, io_u); | |
611 | return NULL; | |
612 | } | |
613 | ||
614 | io_u->buflen = get_next_buflen(td); | |
615 | if (!io_u->buflen) { | |
616 | put_io_u(td, io_u); | |
617 | return NULL; | |
618 | } | |
619 | ||
620 | if (io_u->buflen + io_u->offset > td->file_size) | |
621 | io_u->buflen = td->file_size - io_u->offset; | |
622 | ||
623 | if (!io_u->buflen) { | |
624 | put_io_u(td, io_u); | |
625 | return NULL; | |
626 | } | |
627 | ||
628 | if (!td->sequential) | |
629 | mark_random_map(td, io_u); | |
630 | ||
631 | td->last_bytes += io_u->buflen; | |
632 | ||
633 | if (td->verify != VERIFY_NONE) | |
634 | populate_io_u(td, io_u); | |
635 | ||
636 | if (td_io_prep(td, io_u, td_read(td))) { | |
637 | put_io_u(td, io_u); | |
638 | return NULL; | |
639 | } | |
640 | ||
641 | gettimeofday(&io_u->start_time, NULL); | |
642 | return io_u; | |
643 | } | |
644 | ||
645 | static inline void td_set_runstate(struct thread_data *td, int runstate) | |
646 | { | |
647 | td->old_runstate = td->runstate; | |
648 | td->runstate = runstate; | |
649 | } | |
650 | ||
651 | static int get_next_verify(struct thread_data *td, | |
652 | unsigned long long *offset, unsigned int *len) | |
653 | { | |
654 | struct io_piece *ipo; | |
655 | ||
656 | if (list_empty(&td->io_hist_list)) | |
657 | return 1; | |
658 | ||
659 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
660 | list_del(&ipo->list); | |
661 | ||
662 | *offset = ipo->offset; | |
663 | *len = ipo->len; | |
664 | free(ipo); | |
665 | return 0; | |
666 | } | |
667 | ||
668 | static void prune_io_piece_log(struct thread_data *td) | |
669 | { | |
670 | struct io_piece *ipo; | |
671 | ||
672 | while (!list_empty(&td->io_hist_list)) { | |
673 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
674 | ||
675 | list_del(&ipo->list); | |
676 | free(ipo); | |
677 | } | |
678 | } | |
679 | ||
680 | /* | |
681 | * log a succesful write, so we can unwind the log for verify | |
682 | */ | |
683 | static void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
684 | { | |
685 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); | |
686 | struct list_head *entry; | |
687 | ||
688 | INIT_LIST_HEAD(&ipo->list); | |
689 | ipo->offset = io_u->offset; | |
690 | ipo->len = io_u->buflen; | |
691 | ||
692 | /* | |
693 | * for random io where the writes extend the file, it will typically | |
694 | * be laid out with the block scattered as written. it's faster to | |
695 | * read them in in that order again, so don't sort | |
696 | */ | |
697 | if (td->sequential || !td->overwrite) { | |
698 | list_add_tail(&ipo->list, &td->io_hist_list); | |
699 | return; | |
700 | } | |
701 | ||
702 | /* | |
703 | * for random io, sort the list so verify will run faster | |
704 | */ | |
705 | entry = &td->io_hist_list; | |
706 | while ((entry = entry->prev) != &td->io_hist_list) { | |
707 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); | |
708 | ||
709 | if (__ipo->offset < ipo->offset) | |
710 | break; | |
711 | } | |
712 | ||
713 | list_add(&ipo->list, entry); | |
714 | } | |
715 | ||
716 | static int sync_td(struct thread_data *td) | |
717 | { | |
718 | if (td->io_sync) | |
719 | return td->io_sync(td); | |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
724 | static int io_u_getevents(struct thread_data *td, int min, int max, | |
725 | struct timespec *t) | |
726 | { | |
727 | return td->io_getevents(td, min, max, t); | |
728 | } | |
729 | ||
730 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) | |
731 | { | |
732 | gettimeofday(&io_u->issue_time, NULL); | |
733 | ||
734 | return td->io_queue(td, io_u); | |
735 | } | |
736 | ||
737 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) | |
738 | ||
739 | static void io_completed(struct thread_data *td, struct io_u *io_u, | |
740 | struct io_completion_data *icd) | |
741 | { | |
742 | struct timeval e; | |
743 | unsigned long msec; | |
744 | ||
745 | gettimeofday(&e, NULL); | |
746 | ||
747 | if (!io_u->error) { | |
748 | int idx = io_u->ddir; | |
749 | ||
750 | td->io_blocks[idx]++; | |
751 | td->io_bytes[idx] += (io_u->buflen - io_u->resid); | |
752 | td->this_io_bytes[idx] += (io_u->buflen - io_u->resid); | |
753 | ||
754 | msec = mtime_since(&io_u->issue_time, &e); | |
755 | ||
756 | add_clat_sample(td, io_u->ddir, msec); | |
757 | add_bw_sample(td, io_u->ddir); | |
758 | ||
759 | if (td_write(td) && io_u->ddir == DDIR_WRITE) | |
760 | log_io_piece(td, io_u); | |
761 | ||
762 | icd->bytes_done[idx] += (io_u->buflen - io_u->resid); | |
763 | } else | |
764 | icd->error = io_u->error; | |
765 | } | |
766 | ||
767 | static void ios_completed(struct thread_data *td,struct io_completion_data *icd) | |
768 | { | |
769 | struct io_u *io_u; | |
770 | int i; | |
771 | ||
772 | icd->error = 0; | |
773 | icd->bytes_done[0] = icd->bytes_done[1] = 0; | |
774 | ||
775 | for (i = 0; i < icd->nr; i++) { | |
776 | io_u = td->io_event(td, i); | |
777 | ||
778 | io_completed(td, io_u, icd); | |
779 | put_io_u(td, io_u); | |
780 | } | |
781 | } | |
782 | ||
783 | static void cleanup_pending_aio(struct thread_data *td) | |
784 | { | |
785 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
786 | struct list_head *entry, *n; | |
787 | struct io_completion_data icd; | |
788 | struct io_u *io_u; | |
789 | int r; | |
790 | ||
791 | /* | |
792 | * get immediately available events, if any | |
793 | */ | |
794 | r = io_u_getevents(td, 0, td->cur_depth, &ts); | |
795 | if (r > 0) { | |
796 | icd.nr = r; | |
797 | ios_completed(td, &icd); | |
798 | } | |
799 | ||
800 | /* | |
801 | * now cancel remaining active events | |
802 | */ | |
803 | if (td->io_cancel) { | |
804 | list_for_each_safe(entry, n, &td->io_u_busylist) { | |
805 | io_u = list_entry(entry, struct io_u, list); | |
806 | ||
807 | r = td->io_cancel(td, io_u); | |
808 | if (!r) | |
809 | put_io_u(td, io_u); | |
810 | } | |
811 | } | |
812 | ||
813 | if (td->cur_depth) { | |
814 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); | |
815 | if (r > 0) { | |
816 | icd.nr = r; | |
817 | ios_completed(td, &icd); | |
818 | } | |
819 | } | |
820 | } | |
821 | ||
822 | static int do_io_u_verify(struct thread_data *td, struct io_u **io_u) | |
823 | { | |
824 | struct io_u *v_io_u = *io_u; | |
825 | int ret = 0; | |
826 | ||
827 | if (v_io_u) { | |
828 | ret = verify_io_u(v_io_u); | |
829 | put_io_u(td, v_io_u); | |
830 | *io_u = NULL; | |
831 | } | |
832 | ||
833 | return ret; | |
834 | } | |
835 | ||
836 | static void do_verify(struct thread_data *td) | |
837 | { | |
838 | struct timeval t; | |
839 | struct io_u *io_u, *v_io_u = NULL; | |
840 | struct io_completion_data icd; | |
841 | int ret; | |
842 | ||
843 | td_set_runstate(td, TD_VERIFYING); | |
844 | ||
845 | do { | |
846 | if (td->terminate) | |
847 | break; | |
848 | ||
849 | gettimeofday(&t, NULL); | |
850 | if (runtime_exceeded(td, &t)) | |
851 | break; | |
852 | ||
853 | io_u = __get_io_u(td); | |
854 | if (!io_u) | |
855 | break; | |
856 | ||
857 | if (get_next_verify(td, &io_u->offset, &io_u->buflen)) { | |
858 | put_io_u(td, io_u); | |
859 | break; | |
860 | } | |
861 | ||
862 | if (td_io_prep(td, io_u, 1)) { | |
863 | put_io_u(td, io_u); | |
864 | break; | |
865 | } | |
866 | ||
867 | ret = io_u_queue(td, io_u); | |
868 | if (ret) { | |
869 | put_io_u(td, io_u); | |
870 | td_verror(td, ret); | |
871 | break; | |
872 | } | |
873 | ||
874 | /* | |
875 | * we have one pending to verify, do that while | |
876 | * we are doing io on the next one | |
877 | */ | |
878 | if (do_io_u_verify(td, &v_io_u)) | |
879 | break; | |
880 | ||
881 | ret = io_u_getevents(td, 1, 1, NULL); | |
882 | if (ret != 1) { | |
883 | if (ret < 0) | |
884 | td_verror(td, ret); | |
885 | break; | |
886 | } | |
887 | ||
888 | v_io_u = td->io_event(td, 0); | |
889 | icd.nr = 1; | |
890 | icd.error = 0; | |
891 | io_completed(td, v_io_u, &icd); | |
892 | ||
893 | if (icd.error) { | |
894 | td_verror(td, icd.error); | |
895 | put_io_u(td, v_io_u); | |
896 | v_io_u = NULL; | |
897 | break; | |
898 | } | |
899 | ||
900 | td->cur_off = v_io_u->offset + v_io_u->buflen; | |
901 | ||
902 | /* | |
903 | * if we can't submit more io, we need to verify now | |
904 | */ | |
905 | if (queue_full(td) && do_io_u_verify(td, &v_io_u)) | |
906 | break; | |
907 | ||
908 | } while (1); | |
909 | ||
910 | do_io_u_verify(td, &v_io_u); | |
911 | ||
912 | if (td->cur_depth) | |
913 | cleanup_pending_aio(td); | |
914 | ||
915 | td_set_runstate(td, TD_RUNNING); | |
916 | } | |
917 | ||
918 | static void do_io(struct thread_data *td) | |
919 | { | |
920 | struct io_completion_data icd; | |
921 | struct timeval s, e; | |
922 | unsigned long usec; | |
923 | ||
924 | while (td->this_io_bytes[td->ddir] < td->io_size) { | |
925 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
926 | struct timespec *timeout; | |
927 | int ret, min_evts = 0; | |
928 | struct io_u *io_u; | |
929 | ||
930 | if (td->terminate) | |
931 | break; | |
932 | ||
933 | io_u = get_io_u(td); | |
934 | if (!io_u) | |
935 | break; | |
936 | ||
937 | memcpy(&s, &io_u->start_time, sizeof(s)); | |
938 | ||
939 | ret = io_u_queue(td, io_u); | |
940 | if (ret) { | |
941 | put_io_u(td, io_u); | |
942 | td_verror(td, ret); | |
943 | break; | |
944 | } | |
945 | ||
946 | add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
947 | ||
948 | if (td->cur_depth < td->iodepth) { | |
949 | timeout = &ts; | |
950 | min_evts = 0; | |
951 | } else { | |
952 | timeout = NULL; | |
953 | min_evts = 1; | |
954 | } | |
955 | ||
956 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); | |
957 | if (ret < 0) { | |
958 | td_verror(td, ret); | |
959 | break; | |
960 | } else if (!ret) | |
961 | continue; | |
962 | ||
963 | icd.nr = ret; | |
964 | ios_completed(td, &icd); | |
965 | if (icd.error) { | |
966 | td_verror(td, icd.error); | |
967 | break; | |
968 | } | |
969 | ||
970 | /* | |
971 | * the rate is batched for now, it should work for batches | |
972 | * of completions except the very first one which may look | |
973 | * a little bursty | |
974 | */ | |
975 | gettimeofday(&e, NULL); | |
976 | usec = utime_since(&s, &e); | |
977 | ||
978 | rate_throttle(td, usec, icd.bytes_done[td->ddir]); | |
979 | ||
980 | if (check_min_rate(td, &e)) { | |
981 | td_verror(td, ENOMEM); | |
982 | break; | |
983 | } | |
984 | ||
985 | if (runtime_exceeded(td, &e)) | |
986 | break; | |
987 | ||
988 | if (td->thinktime) | |
989 | usec_sleep(td, td->thinktime); | |
990 | ||
991 | if (should_fsync(td) && td->fsync_blocks && | |
992 | (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0) | |
993 | sync_td(td); | |
994 | } | |
995 | ||
996 | if (td->cur_depth) | |
997 | cleanup_pending_aio(td); | |
998 | ||
999 | if (should_fsync(td)) | |
1000 | sync_td(td); | |
1001 | } | |
1002 | ||
1003 | static void cleanup_io(struct thread_data *td) | |
1004 | { | |
1005 | if (td->io_cleanup) | |
1006 | td->io_cleanup(td); | |
1007 | } | |
1008 | ||
1009 | static int init_io(struct thread_data *td) | |
1010 | { | |
1011 | if (td->io_engine == FIO_SYNCIO) | |
1012 | return fio_syncio_init(td); | |
1013 | else if (td->io_engine == FIO_MMAPIO) | |
1014 | return fio_mmapio_init(td); | |
1015 | else if (td->io_engine == FIO_LIBAIO) | |
1016 | return fio_libaio_init(td); | |
1017 | else if (td->io_engine == FIO_POSIXAIO) | |
1018 | return fio_posixaio_init(td); | |
1019 | else if (td->io_engine == FIO_SGIO) | |
1020 | return fio_sgio_init(td); | |
1021 | else { | |
1022 | fprintf(stderr, "bad io_engine %d\n", td->io_engine); | |
1023 | return 1; | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | static void cleanup_io_u(struct thread_data *td) | |
1028 | { | |
1029 | struct list_head *entry, *n; | |
1030 | struct io_u *io_u; | |
1031 | ||
1032 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
1033 | io_u = list_entry(entry, struct io_u, list); | |
1034 | ||
1035 | list_del(&io_u->list); | |
1036 | free(io_u); | |
1037 | } | |
1038 | ||
1039 | if (td->mem_type == MEM_MALLOC) | |
1040 | free(td->orig_buffer); | |
1041 | else if (td->mem_type == MEM_SHM) { | |
1042 | struct shmid_ds sbuf; | |
1043 | ||
1044 | shmdt(td->orig_buffer); | |
1045 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
1046 | } else if (td->mem_type == MEM_MMAP) | |
1047 | munmap(td->orig_buffer, td->orig_buffer_size); | |
1048 | else | |
1049 | fprintf(stderr, "Bad memory type %d\n", td->mem_type); | |
1050 | ||
1051 | td->orig_buffer = NULL; | |
1052 | } | |
1053 | ||
1054 | static int init_io_u(struct thread_data *td) | |
1055 | { | |
1056 | struct io_u *io_u; | |
1057 | int i, max_units; | |
1058 | char *p; | |
1059 | ||
1060 | if (td->io_engine & FIO_SYNCIO) | |
1061 | max_units = 1; | |
1062 | else | |
1063 | max_units = td->iodepth; | |
1064 | ||
1065 | td->orig_buffer_size = td->max_bs * max_units + MASK; | |
1066 | ||
1067 | if (td->mem_type == MEM_MALLOC) | |
1068 | td->orig_buffer = malloc(td->orig_buffer_size); | |
1069 | else if (td->mem_type == MEM_SHM) { | |
1070 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); | |
1071 | if (td->shm_id < 0) { | |
1072 | td_verror(td, errno); | |
1073 | perror("shmget"); | |
1074 | return 1; | |
1075 | } | |
1076 | ||
1077 | td->orig_buffer = shmat(td->shm_id, NULL, 0); | |
1078 | if (td->orig_buffer == (void *) -1) { | |
1079 | td_verror(td, errno); | |
1080 | perror("shmat"); | |
1081 | td->orig_buffer = NULL; | |
1082 | return 1; | |
1083 | } | |
1084 | } else if (td->mem_type == MEM_MMAP) { | |
1085 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); | |
1086 | if (td->orig_buffer == MAP_FAILED) { | |
1087 | td_verror(td, errno); | |
1088 | perror("mmap"); | |
1089 | td->orig_buffer = NULL; | |
1090 | return 1; | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | INIT_LIST_HEAD(&td->io_u_freelist); | |
1095 | INIT_LIST_HEAD(&td->io_u_busylist); | |
1096 | INIT_LIST_HEAD(&td->io_hist_list); | |
1097 | ||
1098 | p = ALIGN(td->orig_buffer); | |
1099 | for (i = 0; i < max_units; i++) { | |
1100 | io_u = malloc(sizeof(*io_u)); | |
1101 | memset(io_u, 0, sizeof(*io_u)); | |
1102 | INIT_LIST_HEAD(&io_u->list); | |
1103 | ||
1104 | io_u->buf = p + td->max_bs * i; | |
1105 | list_add(&io_u->list, &td->io_u_freelist); | |
1106 | } | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
1111 | static int create_file(struct thread_data *td, unsigned long long size, | |
1112 | int extend) | |
1113 | { | |
1114 | unsigned long long left; | |
1115 | unsigned int bs; | |
1116 | int r, oflags; | |
1117 | char *b; | |
1118 | ||
1119 | /* | |
1120 | * unless specifically asked for overwrite, let normal io extend it | |
1121 | */ | |
1122 | if (td_write(td) && !td->overwrite) | |
1123 | return 0; | |
1124 | ||
1125 | if (!size) { | |
1126 | fprintf(stderr, "Need size for create\n"); | |
1127 | td_verror(td, EINVAL); | |
1128 | return 1; | |
1129 | } | |
1130 | ||
1131 | if (!extend) { | |
1132 | oflags = O_CREAT | O_TRUNC; | |
1133 | printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20); | |
1134 | } else { | |
1135 | oflags = O_APPEND; | |
1136 | printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20); | |
1137 | } | |
1138 | ||
1139 | td->fd = open(td->file_name, O_WRONLY | oflags, 0644); | |
1140 | if (td->fd < 0) { | |
1141 | td_verror(td, errno); | |
1142 | return 1; | |
1143 | } | |
1144 | ||
1145 | if (!extend && ftruncate(td->fd, td->file_size) == -1) { | |
1146 | td_verror(td, errno); | |
1147 | return 1; | |
1148 | } | |
1149 | ||
1150 | td->io_size = td->file_size; | |
1151 | b = malloc(td->max_bs); | |
1152 | memset(b, 0, td->max_bs); | |
1153 | ||
1154 | left = size; | |
1155 | while (left && !td->terminate) { | |
1156 | bs = td->max_bs; | |
1157 | if (bs > left) | |
1158 | bs = left; | |
1159 | ||
1160 | r = write(td->fd, b, bs); | |
1161 | ||
1162 | if (r == (int) bs) { | |
1163 | left -= bs; | |
1164 | continue; | |
1165 | } else { | |
1166 | if (r < 0) | |
1167 | td_verror(td, errno); | |
1168 | else | |
1169 | td_verror(td, EIO); | |
1170 | ||
1171 | break; | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | if (td->terminate) | |
1176 | unlink(td->file_name); | |
1177 | else if (td->create_fsync) | |
1178 | fsync(td->fd); | |
1179 | ||
1180 | close(td->fd); | |
1181 | td->fd = -1; | |
1182 | free(b); | |
1183 | return 0; | |
1184 | } | |
1185 | ||
1186 | static int file_size(struct thread_data *td) | |
1187 | { | |
1188 | struct stat st; | |
1189 | ||
1190 | if (fstat(td->fd, &st) == -1) { | |
1191 | td_verror(td, errno); | |
1192 | return 1; | |
1193 | } | |
1194 | ||
1195 | if (!td->file_size) | |
1196 | td->file_size = st.st_size; | |
1197 | ||
1198 | return 0; | |
1199 | } | |
1200 | ||
1201 | static int bdev_size(struct thread_data *td) | |
1202 | { | |
1203 | size_t bytes; | |
1204 | int r; | |
1205 | ||
1206 | r = blockdev_size(td->fd, &bytes); | |
1207 | if (r) { | |
1208 | td_verror(td, r); | |
1209 | return 1; | |
1210 | } | |
1211 | ||
1212 | /* | |
1213 | * no extend possibilities, so limit size to device size if too large | |
1214 | */ | |
1215 | if (!td->file_size || td->file_size > bytes) | |
1216 | td->file_size = bytes; | |
1217 | ||
1218 | return 0; | |
1219 | } | |
1220 | ||
1221 | static int get_file_size(struct thread_data *td) | |
1222 | { | |
1223 | int ret; | |
1224 | ||
1225 | if (td->filetype == FIO_TYPE_FILE) | |
1226 | ret = file_size(td); | |
1227 | else | |
1228 | ret = bdev_size(td); | |
1229 | ||
1230 | if (ret) | |
1231 | return ret; | |
1232 | ||
1233 | if (td->file_offset > td->file_size) { | |
1234 | fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size); | |
1235 | return 1; | |
1236 | } | |
1237 | ||
1238 | td->io_size = td->file_size - td->file_offset; | |
1239 | if (td->io_size == 0) { | |
1240 | fprintf(stderr, "Client%d: no io blocks\n", td->thread_number); | |
1241 | td_verror(td, EINVAL); | |
1242 | return 1; | |
1243 | } | |
1244 | ||
1245 | td->total_io_size = td->io_size * td->loops; | |
1246 | return 0; | |
1247 | } | |
1248 | ||
1249 | static int setup_file_mmap(struct thread_data *td) | |
1250 | { | |
1251 | int flags; | |
1252 | ||
1253 | if (td_read(td)) | |
1254 | flags = PROT_READ; | |
1255 | else { | |
1256 | flags = PROT_WRITE; | |
1257 | ||
1258 | if (td->verify != VERIFY_NONE) | |
1259 | flags |= PROT_READ; | |
1260 | } | |
1261 | ||
1262 | td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset); | |
1263 | if (td->mmap == MAP_FAILED) { | |
1264 | td->mmap = NULL; | |
1265 | td_verror(td, errno); | |
1266 | return 1; | |
1267 | } | |
1268 | ||
1269 | if (td->invalidate_cache) { | |
1270 | if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) { | |
1271 | td_verror(td, errno); | |
1272 | return 1; | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | if (td->sequential) { | |
1277 | if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) { | |
1278 | td_verror(td, errno); | |
1279 | return 1; | |
1280 | } | |
1281 | } else { | |
1282 | if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) { | |
1283 | td_verror(td, errno); | |
1284 | return 1; | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | return 0; | |
1289 | } | |
1290 | ||
1291 | static int setup_file_plain(struct thread_data *td) | |
1292 | { | |
1293 | if (td->invalidate_cache) { | |
1294 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) { | |
1295 | td_verror(td, errno); | |
1296 | return 1; | |
1297 | } | |
1298 | } | |
1299 | ||
1300 | if (td->sequential) { | |
1301 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
1302 | td_verror(td, errno); | |
1303 | return 1; | |
1304 | } | |
1305 | } else { | |
1306 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) { | |
1307 | td_verror(td, errno); | |
1308 | return 1; | |
1309 | } | |
1310 | } | |
1311 | ||
1312 | return 0; | |
1313 | } | |
1314 | ||
1315 | static int setup_file(struct thread_data *td) | |
1316 | { | |
1317 | struct stat st; | |
1318 | int flags = 0; | |
1319 | ||
1320 | if (stat(td->file_name, &st) == -1) { | |
1321 | if (errno != ENOENT) { | |
1322 | td_verror(td, errno); | |
1323 | return 1; | |
1324 | } | |
1325 | if (!td->create_file) { | |
1326 | td_verror(td, ENOENT); | |
1327 | return 1; | |
1328 | } | |
1329 | if (create_file(td, td->file_size, 0)) | |
1330 | return 1; | |
1331 | } else if (td->filetype == FIO_TYPE_FILE) { | |
1332 | if (st.st_size < td->file_size) { | |
1333 | if (create_file(td, td->file_size - st.st_size, 1)) | |
1334 | return 1; | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | if (td->odirect) | |
1339 | flags |= O_DIRECT; | |
1340 | ||
1341 | if (td_read(td)) | |
1342 | td->fd = open(td->file_name, flags | O_RDONLY); | |
1343 | else { | |
1344 | if (td->filetype == FIO_TYPE_FILE) { | |
1345 | if (!td->overwrite) | |
1346 | flags |= O_TRUNC; | |
1347 | ||
1348 | flags |= O_CREAT; | |
1349 | } | |
1350 | if (td->sync_io) | |
1351 | flags |= O_SYNC; | |
1352 | ||
1353 | flags |= O_RDWR; | |
1354 | ||
1355 | td->fd = open(td->file_name, flags, 0600); | |
1356 | } | |
1357 | ||
1358 | if (td->fd == -1) { | |
1359 | td_verror(td, errno); | |
1360 | return 1; | |
1361 | } | |
1362 | ||
1363 | if (get_file_size(td)) | |
1364 | return 1; | |
1365 | ||
1366 | if (td->io_engine != FIO_MMAPIO) | |
1367 | return setup_file_plain(td); | |
1368 | else | |
1369 | return setup_file_mmap(td); | |
1370 | } | |
1371 | ||
1372 | static int check_dev_match(dev_t dev, char *path) | |
1373 | { | |
1374 | unsigned int major, minor; | |
1375 | char line[256], *p; | |
1376 | FILE *f; | |
1377 | ||
1378 | f = fopen(path, "r"); | |
1379 | if (!f) { | |
1380 | perror("open path"); | |
1381 | return 1; | |
1382 | } | |
1383 | ||
1384 | p = fgets(line, sizeof(line), f); | |
1385 | if (!p) { | |
1386 | fclose(f); | |
1387 | return 1; | |
1388 | } | |
1389 | ||
1390 | if (sscanf(p, "%u:%u", &major, &minor) != 2) { | |
1391 | fclose(f); | |
1392 | return 1; | |
1393 | } | |
1394 | ||
1395 | if (((major << 8) | minor) == dev) { | |
1396 | fclose(f); | |
1397 | return 0; | |
1398 | } | |
1399 | ||
1400 | fclose(f); | |
1401 | return 1; | |
1402 | } | |
1403 | ||
1404 | static int find_block_dir(dev_t dev, char *path) | |
1405 | { | |
1406 | struct dirent *dir; | |
1407 | struct stat st; | |
1408 | int found = 0; | |
1409 | DIR *D; | |
1410 | ||
1411 | D = opendir(path); | |
1412 | if (!D) | |
1413 | return 0; | |
1414 | ||
1415 | while ((dir = readdir(D)) != NULL) { | |
1416 | char full_path[256]; | |
1417 | ||
1418 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) | |
1419 | continue; | |
1420 | if (!strcmp(dir->d_name, "device")) | |
1421 | continue; | |
1422 | ||
1423 | sprintf(full_path, "%s/%s", path, dir->d_name); | |
1424 | ||
1425 | if (!strcmp(dir->d_name, "dev")) { | |
1426 | if (!check_dev_match(dev, full_path)) { | |
1427 | found = 1; | |
1428 | break; | |
1429 | } | |
1430 | } | |
1431 | ||
1432 | if (stat(full_path, &st) == -1) { | |
1433 | perror("stat"); | |
1434 | break; | |
1435 | } | |
1436 | ||
1437 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) | |
1438 | continue; | |
1439 | ||
1440 | found = find_block_dir(dev, full_path); | |
1441 | if (found) { | |
1442 | strcpy(path, full_path); | |
1443 | break; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | closedir(D); | |
1448 | return found; | |
1449 | } | |
1450 | ||
1451 | static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus) | |
1452 | { | |
1453 | unsigned in_flight; | |
1454 | char line[256]; | |
1455 | FILE *f; | |
1456 | char *p; | |
1457 | ||
1458 | f = fopen(du->path, "r"); | |
1459 | if (!f) | |
1460 | return 1; | |
1461 | ||
1462 | p = fgets(line, sizeof(line), f); | |
1463 | if (!p) { | |
1464 | fclose(f); | |
1465 | return 1; | |
1466 | } | |
1467 | ||
1468 | if (sscanf(p, "%u %u %llu %u %u %u %llu %u %u %u %u\n", &dus->ios[0], &dus->merges[0], &dus->sectors[0], &dus->ticks[0], &dus->ios[1], &dus->merges[1], &dus->sectors[1], &dus->ticks[1], &in_flight, &dus->io_ticks, &dus->time_in_queue) != 11) { | |
1469 | fclose(f); | |
1470 | return 1; | |
1471 | } | |
1472 | ||
1473 | fclose(f); | |
1474 | return 0; | |
1475 | } | |
1476 | ||
1477 | static void update_io_tick_disk(struct disk_util *du) | |
1478 | { | |
1479 | struct disk_util_stat __dus, *dus, *ldus; | |
1480 | struct timeval t; | |
1481 | ||
1482 | if (get_io_ticks(du, &__dus)) | |
1483 | return; | |
1484 | ||
1485 | dus = &du->dus; | |
1486 | ldus = &du->last_dus; | |
1487 | ||
1488 | dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]); | |
1489 | dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]); | |
1490 | dus->ios[0] += (__dus.ios[0] - ldus->ios[0]); | |
1491 | dus->ios[1] += (__dus.ios[1] - ldus->ios[1]); | |
1492 | dus->merges[0] += (__dus.merges[0] - ldus->merges[0]); | |
1493 | dus->merges[1] += (__dus.merges[1] - ldus->merges[1]); | |
1494 | dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]); | |
1495 | dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]); | |
1496 | dus->io_ticks += (__dus.io_ticks - ldus->io_ticks); | |
1497 | dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue); | |
1498 | ||
1499 | gettimeofday(&t, NULL); | |
1500 | du->msec += mtime_since(&du->time, &t); | |
1501 | memcpy(&du->time, &t, sizeof(t)); | |
1502 | memcpy(ldus, &__dus, sizeof(__dus)); | |
1503 | } | |
1504 | ||
1505 | static void update_io_ticks(void) | |
1506 | { | |
1507 | struct list_head *entry; | |
1508 | struct disk_util *du; | |
1509 | ||
1510 | list_for_each(entry, &disk_list) { | |
1511 | du = list_entry(entry, struct disk_util, list); | |
1512 | update_io_tick_disk(du); | |
1513 | } | |
1514 | } | |
1515 | ||
1516 | static int disk_util_exists(dev_t dev) | |
1517 | { | |
1518 | struct list_head *entry; | |
1519 | struct disk_util *du; | |
1520 | ||
1521 | list_for_each(entry, &disk_list) { | |
1522 | du = list_entry(entry, struct disk_util, list); | |
1523 | ||
1524 | if (du->dev == dev) | |
1525 | return 1; | |
1526 | } | |
1527 | ||
1528 | return 0; | |
1529 | } | |
1530 | ||
1531 | static void disk_util_add(dev_t dev, char *path) | |
1532 | { | |
1533 | struct disk_util *du = malloc(sizeof(*du)); | |
1534 | ||
1535 | memset(du, 0, sizeof(*du)); | |
1536 | INIT_LIST_HEAD(&du->list); | |
1537 | sprintf(du->path, "%s/stat", path); | |
1538 | du->name = strdup(basename(path)); | |
1539 | du->dev = dev; | |
1540 | ||
1541 | gettimeofday(&du->time, NULL); | |
1542 | get_io_ticks(du, &du->last_dus); | |
1543 | ||
1544 | list_add_tail(&du->list, &disk_list); | |
1545 | } | |
1546 | ||
1547 | static void init_disk_util(struct thread_data *td) | |
1548 | { | |
1549 | struct stat st; | |
1550 | char foo[256], tmp[256]; | |
1551 | dev_t dev; | |
1552 | char *p; | |
1553 | ||
1554 | if (!td->do_disk_util) | |
1555 | return; | |
1556 | ||
1557 | if (!stat(td->file_name, &st)) { | |
1558 | if (S_ISBLK(st.st_mode)) | |
1559 | dev = st.st_rdev; | |
1560 | else | |
1561 | dev = st.st_dev; | |
1562 | } else { | |
1563 | /* | |
1564 | * must be a file, open "." in that path | |
1565 | */ | |
1566 | strcpy(foo, td->file_name); | |
1567 | p = dirname(foo); | |
1568 | if (stat(p, &st)) { | |
1569 | perror("disk util stat"); | |
1570 | return; | |
1571 | } | |
1572 | ||
1573 | dev = st.st_dev; | |
1574 | } | |
1575 | ||
1576 | if (disk_util_exists(dev)) | |
1577 | return; | |
1578 | ||
1579 | sprintf(foo, "/sys/block"); | |
1580 | if (!find_block_dir(dev, foo)) | |
1581 | return; | |
1582 | ||
1583 | /* | |
1584 | * if this is inside a partition dir, jump back to parent | |
1585 | */ | |
1586 | sprintf(tmp, "%s/queue", foo); | |
1587 | if (stat(tmp, &st)) { | |
1588 | p = dirname(foo); | |
1589 | sprintf(tmp, "%s/queue", p); | |
1590 | if (stat(tmp, &st)) { | |
1591 | fprintf(stderr, "unknown sysfs layout\n"); | |
1592 | return; | |
1593 | } | |
1594 | sprintf(foo, "%s", p); | |
1595 | } | |
1596 | ||
1597 | disk_util_add(dev, foo); | |
1598 | } | |
1599 | ||
1600 | static void disk_util_timer_arm(void) | |
1601 | { | |
1602 | itimer.it_value.tv_sec = 0; | |
1603 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; | |
1604 | setitimer(ITIMER_REAL, &itimer, NULL); | |
1605 | } | |
1606 | ||
1607 | static void clear_io_state(struct thread_data *td) | |
1608 | { | |
1609 | if (td->io_engine == FIO_SYNCIO) | |
1610 | lseek(td->fd, SEEK_SET, 0); | |
1611 | ||
1612 | td->cur_off = 0; | |
1613 | td->last_bytes = 0; | |
1614 | td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0; | |
1615 | td->this_io_bytes[0] = td->this_io_bytes[1] = 0; | |
1616 | ||
1617 | if (td->file_map) | |
1618 | memset(td->file_map, 0, td->num_maps * sizeof(long)); | |
1619 | } | |
1620 | ||
1621 | static void update_rusage_stat(struct thread_data *td) | |
1622 | { | |
1623 | if (!(td->runtime[0] + td->runtime[1])) | |
1624 | return; | |
1625 | ||
1626 | getrusage(RUSAGE_SELF, &td->ru_end); | |
1627 | ||
1628 | td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime); | |
1629 | td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime); | |
1630 | td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw); | |
1631 | ||
1632 | ||
1633 | memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end)); | |
1634 | } | |
1635 | ||
1636 | static void *thread_main(void *data) | |
1637 | { | |
1638 | struct thread_data *td = data; | |
1639 | int ret = 1; | |
1640 | ||
1641 | if (!td->use_thread) | |
1642 | setsid(); | |
1643 | ||
1644 | td->pid = getpid(); | |
1645 | ||
1646 | if (init_io_u(td)) | |
1647 | goto err; | |
1648 | ||
1649 | if (fio_setaffinity(td) == -1) { | |
1650 | td_verror(td, errno); | |
1651 | goto err; | |
1652 | } | |
1653 | ||
1654 | if (init_io(td)) | |
1655 | goto err; | |
1656 | ||
1657 | if (td->ioprio) { | |
1658 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { | |
1659 | td_verror(td, errno); | |
1660 | goto err; | |
1661 | } | |
1662 | } | |
1663 | ||
1664 | sem_post(&startup_sem); | |
1665 | sem_wait(&td->mutex); | |
1666 | ||
1667 | if (!td->create_serialize && setup_file(td)) | |
1668 | goto err; | |
1669 | ||
1670 | if (init_random_state(td)) | |
1671 | goto err; | |
1672 | ||
1673 | gettimeofday(&td->epoch, NULL); | |
1674 | ||
1675 | while (td->loops--) { | |
1676 | getrusage(RUSAGE_SELF, &td->ru_start); | |
1677 | gettimeofday(&td->start, NULL); | |
1678 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); | |
1679 | ||
1680 | if (td->ratemin) | |
1681 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
1682 | ||
1683 | clear_io_state(td); | |
1684 | prune_io_piece_log(td); | |
1685 | ||
1686 | do_io(td); | |
1687 | ||
1688 | td->runtime[td->ddir] += mtime_since_now(&td->start); | |
1689 | update_rusage_stat(td); | |
1690 | ||
1691 | if (td->error || td->terminate) | |
1692 | break; | |
1693 | ||
1694 | if (td->verify == VERIFY_NONE) | |
1695 | continue; | |
1696 | ||
1697 | clear_io_state(td); | |
1698 | gettimeofday(&td->start, NULL); | |
1699 | ||
1700 | do_verify(td); | |
1701 | ||
1702 | td->runtime[DDIR_READ] += mtime_since_now(&td->start); | |
1703 | ||
1704 | if (td->error || td->terminate) | |
1705 | break; | |
1706 | } | |
1707 | ||
1708 | ret = 0; | |
1709 | ||
1710 | if (td->bw_log) | |
1711 | finish_log(td, td->bw_log, "bw"); | |
1712 | if (td->slat_log) | |
1713 | finish_log(td, td->slat_log, "slat"); | |
1714 | if (td->clat_log) | |
1715 | finish_log(td, td->clat_log, "clat"); | |
1716 | ||
1717 | if (exitall_on_terminate) | |
1718 | terminate_threads(td->groupid); | |
1719 | ||
1720 | err: | |
1721 | if (td->fd != -1) { | |
1722 | close(td->fd); | |
1723 | td->fd = -1; | |
1724 | } | |
1725 | if (td->mmap) | |
1726 | munmap(td->mmap, td->file_size); | |
1727 | cleanup_io(td); | |
1728 | cleanup_io_u(td); | |
1729 | if (ret) { | |
1730 | sem_post(&startup_sem); | |
1731 | sem_wait(&td->mutex); | |
1732 | } | |
1733 | td_set_runstate(td, TD_EXITED); | |
1734 | return NULL; | |
1735 | ||
1736 | } | |
1737 | ||
1738 | static void *fork_main(int shmid, int offset) | |
1739 | { | |
1740 | struct thread_data *td; | |
1741 | void *data; | |
1742 | ||
1743 | data = shmat(shmid, NULL, 0); | |
1744 | if (data == (void *) -1) { | |
1745 | perror("shmat"); | |
1746 | return NULL; | |
1747 | } | |
1748 | ||
1749 | td = data + offset * sizeof(struct thread_data); | |
1750 | thread_main(td); | |
1751 | shmdt(data); | |
1752 | return NULL; | |
1753 | } | |
1754 | ||
1755 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, | |
1756 | double *mean, double *dev) | |
1757 | { | |
1758 | double n; | |
1759 | ||
1760 | if (is->samples == 0) | |
1761 | return 0; | |
1762 | ||
1763 | *min = is->min_val; | |
1764 | *max = is->max_val; | |
1765 | ||
1766 | n = (double) is->samples; | |
1767 | *mean = (double) is->val / n; | |
1768 | *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1)); | |
1769 | if (!(*min + *max) && !(*mean + *dev)) | |
1770 | return 0; | |
1771 | ||
1772 | return 1; | |
1773 | } | |
1774 | ||
1775 | static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs, | |
1776 | int ddir) | |
1777 | { | |
1778 | char *ddir_str[] = { "read ", "write" }; | |
1779 | unsigned long min, max, bw; | |
1780 | double mean, dev; | |
1781 | ||
1782 | if (!td->runtime[ddir]) | |
1783 | return; | |
1784 | ||
1785 | bw = td->io_bytes[ddir] / td->runtime[ddir]; | |
1786 | printf(" %s: io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]); | |
1787 | ||
1788 | if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev)) | |
1789 | printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1790 | ||
1791 | if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev)) | |
1792 | printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1793 | ||
1794 | if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) { | |
1795 | double p_of_agg; | |
1796 | ||
1797 | p_of_agg = mean * 100 / (double) rs->agg[ddir]; | |
1798 | printf(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev); | |
1799 | } | |
1800 | } | |
1801 | ||
1802 | static void show_thread_status(struct thread_data *td, | |
1803 | struct group_run_stats *rs) | |
1804 | { | |
1805 | double usr_cpu, sys_cpu; | |
1806 | ||
1807 | if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error) | |
1808 | return; | |
1809 | ||
1810 | printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error); | |
1811 | ||
1812 | show_ddir_status(td, rs, td->ddir); | |
1813 | show_ddir_status(td, rs, td->ddir ^ 1); | |
1814 | ||
1815 | if (td->runtime[0] + td->runtime[1]) { | |
1816 | double runt = td->runtime[0] + td->runtime[1]; | |
1817 | ||
1818 | usr_cpu = (double) td->usr_time * 100 / runt; | |
1819 | sys_cpu = (double) td->sys_time * 100 / runt; | |
1820 | } else { | |
1821 | usr_cpu = 0; | |
1822 | sys_cpu = 0; | |
1823 | } | |
1824 | ||
1825 | printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx); | |
1826 | } | |
1827 | ||
1828 | static void check_str_update(struct thread_data *td) | |
1829 | { | |
1830 | char c = run_str[td->thread_number - 1]; | |
1831 | ||
1832 | if (td->runstate == td->old_runstate) | |
1833 | return; | |
1834 | ||
1835 | switch (td->runstate) { | |
1836 | case TD_REAPED: | |
1837 | c = '_'; | |
1838 | break; | |
1839 | case TD_EXITED: | |
1840 | c = 'E'; | |
1841 | break; | |
1842 | case TD_RUNNING: | |
1843 | if (td_read(td)) { | |
1844 | if (td->sequential) | |
1845 | c = 'R'; | |
1846 | else | |
1847 | c = 'r'; | |
1848 | } else { | |
1849 | if (td->sequential) | |
1850 | c = 'W'; | |
1851 | else | |
1852 | c = 'w'; | |
1853 | } | |
1854 | break; | |
1855 | case TD_VERIFYING: | |
1856 | c = 'V'; | |
1857 | break; | |
1858 | case TD_CREATED: | |
1859 | c = 'C'; | |
1860 | break; | |
1861 | case TD_NOT_CREATED: | |
1862 | c = 'P'; | |
1863 | break; | |
1864 | default: | |
1865 | printf("state %d\n", td->runstate); | |
1866 | } | |
1867 | ||
1868 | run_str[td->thread_number - 1] = c; | |
1869 | td->old_runstate = td->runstate; | |
1870 | } | |
1871 | ||
1872 | static void print_thread_status(void) | |
1873 | { | |
1874 | unsigned long long bytes_done, bytes_total; | |
1875 | int i, nr_running, t_rate, m_rate; | |
1876 | double perc; | |
1877 | ||
1878 | bytes_done = bytes_total = 0; | |
1879 | nr_running = t_rate = m_rate = 0; | |
1880 | for (i = 0; i < thread_number; i++) { | |
1881 | struct thread_data *td = &threads[i]; | |
1882 | ||
1883 | if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){ | |
1884 | nr_running++; | |
1885 | t_rate += td->rate; | |
1886 | m_rate += td->ratemin; | |
1887 | } | |
1888 | ||
1889 | bytes_total += td->total_io_size; | |
1890 | if (td->verify) | |
1891 | bytes_total += td->total_io_size; | |
1892 | ||
1893 | bytes_done += td->io_bytes[DDIR_READ] +td->io_bytes[DDIR_WRITE]; | |
1894 | ||
1895 | check_str_update(td); | |
1896 | } | |
1897 | ||
1898 | perc = 0; | |
1899 | if (bytes_total && bytes_done) { | |
1900 | perc = (double) 100 * bytes_done / (double) bytes_total; | |
1901 | if (perc > 100.0) | |
1902 | perc = 100.0; | |
1903 | } | |
1904 | ||
1905 | printf("Threads now running: %d", nr_running); | |
1906 | if (m_rate || t_rate) | |
1907 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); | |
1908 | printf(" : [%s] [%3.2f%% done]\r", run_str, perc); | |
1909 | fflush(stdout); | |
1910 | } | |
1911 | ||
1912 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) | |
1913 | { | |
1914 | int i; | |
1915 | ||
1916 | /* | |
1917 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1918 | */ | |
1919 | for (i = 0; i < thread_number; i++) { | |
1920 | struct thread_data *td = &threads[i]; | |
1921 | ||
1922 | if (td->runstate != TD_EXITED) | |
1923 | continue; | |
1924 | ||
1925 | td_set_runstate(td, TD_REAPED); | |
1926 | ||
1927 | if (td->use_thread) { | |
1928 | long ret; | |
1929 | ||
1930 | if (pthread_join(td->thread, (void *) &ret)) | |
1931 | perror("thread_join"); | |
1932 | } else | |
1933 | waitpid(td->pid, NULL, 0); | |
1934 | ||
1935 | (*nr_running)--; | |
1936 | (*m_rate) -= td->ratemin; | |
1937 | (*t_rate) -= td->rate; | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | static void run_threads(void) | |
1942 | { | |
1943 | struct timeval genesis; | |
1944 | struct thread_data *td; | |
1945 | unsigned long spent; | |
1946 | int i, todo, nr_running, m_rate, t_rate, nr_started; | |
1947 | ||
1948 | printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : ""); | |
1949 | fflush(stdout); | |
1950 | ||
1951 | signal(SIGINT, sig_handler); | |
1952 | signal(SIGALRM, sig_handler); | |
1953 | ||
1954 | todo = thread_number; | |
1955 | nr_running = 0; | |
1956 | nr_started = 0; | |
1957 | m_rate = t_rate = 0; | |
1958 | ||
1959 | for (i = 0; i < thread_number; i++) { | |
1960 | td = &threads[i]; | |
1961 | ||
1962 | run_str[td->thread_number - 1] = 'P'; | |
1963 | ||
1964 | init_disk_util(td); | |
1965 | ||
1966 | if (!td->create_serialize) | |
1967 | continue; | |
1968 | ||
1969 | /* | |
1970 | * do file setup here so it happens sequentially, | |
1971 | * we don't want X number of threads getting their | |
1972 | * client data interspersed on disk | |
1973 | */ | |
1974 | if (setup_file(td)) { | |
1975 | td_set_runstate(td, TD_REAPED); | |
1976 | todo--; | |
1977 | } | |
1978 | } | |
1979 | ||
1980 | gettimeofday(&genesis, NULL); | |
1981 | ||
1982 | while (todo) { | |
1983 | /* | |
1984 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
1985 | */ | |
1986 | for (i = 0; i < thread_number; i++) { | |
1987 | td = &threads[i]; | |
1988 | ||
1989 | if (td->runstate != TD_NOT_CREATED) | |
1990 | continue; | |
1991 | ||
1992 | /* | |
1993 | * never got a chance to start, killed by other | |
1994 | * thread for some reason | |
1995 | */ | |
1996 | if (td->terminate) { | |
1997 | todo--; | |
1998 | continue; | |
1999 | } | |
2000 | ||
2001 | if (td->start_delay) { | |
2002 | spent = mtime_since_now(&genesis); | |
2003 | ||
2004 | if (td->start_delay * 1000 > spent) | |
2005 | continue; | |
2006 | } | |
2007 | ||
2008 | if (td->stonewall && (nr_started || nr_running)) | |
2009 | break; | |
2010 | ||
2011 | td_set_runstate(td, TD_CREATED); | |
2012 | sem_init(&startup_sem, 0, 1); | |
2013 | todo--; | |
2014 | nr_started++; | |
2015 | ||
2016 | if (td->use_thread) { | |
2017 | if (pthread_create(&td->thread, NULL, thread_main, td)) { | |
2018 | perror("thread_create"); | |
2019 | nr_started--; | |
2020 | } | |
2021 | } else { | |
2022 | if (fork()) | |
2023 | sem_wait(&startup_sem); | |
2024 | else { | |
2025 | fork_main(shm_id, i); | |
2026 | exit(0); | |
2027 | } | |
2028 | } | |
2029 | } | |
2030 | ||
2031 | /* | |
2032 | * start created threads (TD_CREATED -> TD_RUNNING) | |
2033 | */ | |
2034 | for (i = 0; i < thread_number; i++) { | |
2035 | td = &threads[i]; | |
2036 | ||
2037 | if (td->runstate != TD_CREATED) | |
2038 | continue; | |
2039 | ||
2040 | td_set_runstate(td, TD_RUNNING); | |
2041 | nr_running++; | |
2042 | nr_started--; | |
2043 | m_rate += td->ratemin; | |
2044 | t_rate += td->rate; | |
2045 | sem_post(&td->mutex); | |
2046 | } | |
2047 | ||
2048 | reap_threads(&nr_running, &t_rate, &m_rate); | |
2049 | ||
2050 | if (todo) | |
2051 | usleep(100000); | |
2052 | } | |
2053 | ||
2054 | while (nr_running) { | |
2055 | reap_threads(&nr_running, &t_rate, &m_rate); | |
2056 | usleep(10000); | |
2057 | } | |
2058 | ||
2059 | update_io_ticks(); | |
2060 | } | |
2061 | ||
2062 | static void show_group_stats(struct group_run_stats *rs, int id) | |
2063 | { | |
2064 | printf("\nRun status group %d (all jobs):\n", id); | |
2065 | ||
2066 | if (rs->max_run[DDIR_READ]) | |
2067 | printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]); | |
2068 | if (rs->max_run[DDIR_WRITE]) | |
2069 | printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]); | |
2070 | } | |
2071 | ||
2072 | static void show_disk_util(void) | |
2073 | { | |
2074 | struct disk_util_stat *dus; | |
2075 | struct list_head *entry; | |
2076 | struct disk_util *du; | |
2077 | double util; | |
2078 | ||
2079 | printf("\nDisk stats (read/write):\n"); | |
2080 | ||
2081 | list_for_each(entry, &disk_list) { | |
2082 | du = list_entry(entry, struct disk_util, list); | |
2083 | dus = &du->dus; | |
2084 | ||
2085 | util = (double) 100 * du->dus.io_ticks / (double) du->msec; | |
2086 | if (util > 100.0) | |
2087 | util = 100.0; | |
2088 | ||
2089 | printf(" %s: ios=%u/%u, merge=%u/%u, ticks=%u/%u, in_queue=%u, util=%3.2f%%\n", du->name, dus->ios[0], dus->ios[1], dus->merges[0], dus->merges[1], dus->ticks[0], dus->ticks[1], dus->time_in_queue, util); | |
2090 | } | |
2091 | } | |
2092 | ||
2093 | static void show_run_stats(void) | |
2094 | { | |
2095 | struct group_run_stats *runstats, *rs; | |
2096 | struct thread_data *td; | |
2097 | int i; | |
2098 | ||
2099 | runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1)); | |
2100 | ||
2101 | for (i = 0; i < groupid + 1; i++) { | |
2102 | rs = &runstats[i]; | |
2103 | ||
2104 | memset(rs, 0, sizeof(*rs)); | |
2105 | rs->min_bw[0] = rs->min_run[0] = ~0UL; | |
2106 | rs->min_bw[1] = rs->min_run[1] = ~0UL; | |
2107 | } | |
2108 | ||
2109 | for (i = 0; i < thread_number; i++) { | |
2110 | unsigned long rbw, wbw; | |
2111 | ||
2112 | td = &threads[i]; | |
2113 | ||
2114 | if (td->error) { | |
2115 | printf("Client%d: %s\n", td->thread_number, td->verror); | |
2116 | continue; | |
2117 | } | |
2118 | ||
2119 | rs = &runstats[td->groupid]; | |
2120 | ||
2121 | if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0]) | |
2122 | rs->min_run[0] = td->runtime[0]; | |
2123 | if (td->runtime[0] > rs->max_run[0]) | |
2124 | rs->max_run[0] = td->runtime[0]; | |
2125 | if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1]) | |
2126 | rs->min_run[1] = td->runtime[1]; | |
2127 | if (td->runtime[1] > rs->max_run[1]) | |
2128 | rs->max_run[1] = td->runtime[1]; | |
2129 | ||
2130 | rbw = wbw = 0; | |
2131 | if (td->runtime[0]) | |
2132 | rbw = td->io_bytes[0] / td->runtime[0]; | |
2133 | if (td->runtime[1]) | |
2134 | wbw = td->io_bytes[1] / td->runtime[1]; | |
2135 | ||
2136 | if (rbw < rs->min_bw[0]) | |
2137 | rs->min_bw[0] = rbw; | |
2138 | if (wbw < rs->min_bw[1]) | |
2139 | rs->min_bw[1] = wbw; | |
2140 | if (rbw > rs->max_bw[0]) | |
2141 | rs->max_bw[0] = rbw; | |
2142 | if (wbw > rs->max_bw[1]) | |
2143 | rs->max_bw[1] = wbw; | |
2144 | ||
2145 | rs->io_mb[0] += td->io_bytes[0] >> 20; | |
2146 | rs->io_mb[1] += td->io_bytes[1] >> 20; | |
2147 | } | |
2148 | ||
2149 | for (i = 0; i < groupid + 1; i++) { | |
2150 | rs = &runstats[i]; | |
2151 | ||
2152 | if (rs->max_run[0]) | |
2153 | rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0]; | |
2154 | if (rs->max_run[1]) | |
2155 | rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1]; | |
2156 | } | |
2157 | ||
2158 | /* | |
2159 | * don't overwrite last signal output | |
2160 | */ | |
2161 | printf("\n"); | |
2162 | ||
2163 | for (i = 0; i < thread_number; i++) { | |
2164 | td = &threads[i]; | |
2165 | rs = &runstats[td->groupid]; | |
2166 | ||
2167 | show_thread_status(td, rs); | |
2168 | } | |
2169 | ||
2170 | for (i = 0; i < groupid + 1; i++) | |
2171 | show_group_stats(&runstats[i], i); | |
2172 | ||
2173 | show_disk_util(); | |
2174 | } | |
2175 | ||
2176 | int main(int argc, char *argv[]) | |
2177 | { | |
2178 | if (parse_options(argc, argv)) | |
2179 | return 1; | |
2180 | ||
2181 | if (!thread_number) { | |
2182 | printf("Nothing to do\n"); | |
2183 | return 1; | |
2184 | } | |
2185 | ||
2186 | disk_util_timer_arm(); | |
2187 | ||
2188 | run_threads(); | |
2189 | show_run_stats(); | |
2190 | ||
2191 | return 0; | |
2192 | } |