Commit | Line | Data |
---|---|---|
abe4da87 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 | */ | |
892199bd JA |
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> | |
7dd1389e | 29 | #include <ctype.h> |
18e0b78c | 30 | #include <sched.h> |
43000118 | 31 | #include <libaio.h> |
e128065d | 32 | #include <math.h> |
02983297 | 33 | #include <limits.h> |
892199bd JA |
34 | #include <sys/time.h> |
35 | #include <sys/types.h> | |
36 | #include <sys/stat.h> | |
37 | #include <sys/wait.h> | |
38 | #include <semaphore.h> | |
39 | #include <sys/ipc.h> | |
40 | #include <sys/shm.h> | |
41 | #include <asm/unistd.h> | |
946d8870 | 42 | #include <asm/types.h> |
892199bd | 43 | |
2c83567e | 44 | #include "list.h" |
e8457004 | 45 | #include "md5.h" |
2c83567e | 46 | |
4240cfa1 JA |
47 | #define MAX_JOBS (1024) |
48 | ||
892199bd JA |
49 | /* |
50 | * assume we don't have _get either, if _set isn't defined | |
51 | */ | |
52 | #ifndef __NR_ioprio_set | |
892199bd JA |
53 | #if defined(__i386__) |
54 | #define __NR_ioprio_set 289 | |
55 | #define __NR_ioprio_get 290 | |
56 | #elif defined(__powerpc__) || defined(__powerpc64__) | |
57 | #define __NR_ioprio_set 273 | |
58 | #define __NR_ioprio_get 274 | |
59 | #elif defined(__x86_64__) | |
60 | #define __NR_ioprio_set 251 | |
61 | #define __NR_ioprio_get 252 | |
62 | #elif defined(__ia64__) | |
63 | #define __NR_ioprio_set 1274 | |
64 | #define __NR_ioprio_get 1275 | |
65 | #elif defined(__alpha__) | |
66 | #define __NR_ioprio_set 442 | |
67 | #define __NR_ioprio_get 443 | |
68 | #elif defined(__s390x__) || defined(__s390__) | |
69 | #define __NR_ioprio_set 282 | |
70 | #define __NR_ioprio_get 283 | |
71 | #else | |
72 | #error "Unsupported arch" | |
73 | #endif | |
b95799ca | 74 | #endif |
892199bd | 75 | |
b95799ca JA |
76 | #ifndef __NR_fadvise64 |
77 | #if defined(__i386__) | |
78 | #define __NR_fadvise64 250 | |
79 | #elif defined(__powerpc__) || defined(__powerpc64__) | |
80 | #define __NR_fadvise64 233 | |
81 | #elif defined(__x86_64__) | |
82 | #define __NR_fadvise64 221 | |
83 | #elif defined(__ia64__) | |
84 | #define __NR_fadvise64 1234 | |
85 | #elif defined(__alpha__) | |
86 | #define __NR_fadvise64 413 | |
87 | #elif defined(__s390x__) || defined(__s390__) | |
88 | #define __NR_fadvise64 253 | |
89 | #else | |
90 | #error "Unsupported arch" | |
91 | #endif | |
892199bd JA |
92 | #endif |
93 | ||
94 | static int ioprio_set(int which, int who, int ioprio) | |
95 | { | |
96 | return syscall(__NR_ioprio_set, which, who, ioprio); | |
97 | } | |
98 | ||
b95799ca JA |
99 | /* |
100 | * we want fadvise64 really, but it's so tangled... later | |
101 | */ | |
102 | static int fadvise(int fd, loff_t offset, size_t len, int advice) | |
103 | { | |
104 | #if 0 | |
105 | return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice); | |
106 | #else | |
107 | return posix_fadvise(fd, (off_t) offset, len, advice); | |
108 | #endif | |
109 | } | |
110 | ||
892199bd JA |
111 | enum { |
112 | IOPRIO_WHO_PROCESS = 1, | |
113 | IOPRIO_WHO_PGRP, | |
114 | IOPRIO_WHO_USER, | |
115 | }; | |
116 | ||
117 | #define IOPRIO_CLASS_SHIFT 13 | |
118 | ||
892199bd JA |
119 | #define MASK (4095) |
120 | ||
4240cfa1 JA |
121 | #define DEF_BS (4096) |
122 | #define DEF_TIMEOUT (30) | |
123 | #define DEF_RATE_CYCLE (1000) | |
124 | #define DEF_ODIRECT (1) | |
125 | #define DEF_SEQUENTIAL (1) | |
4240cfa1 | 126 | #define DEF_RAND_REPEAT (1) |
02983297 JA |
127 | #define DEF_OVERWRITE (0) |
128 | #define DEF_CREATE (1) | |
b95799ca | 129 | #define DEF_INVALIDATE (1) |
99c6704f | 130 | #define DEF_SYNCIO (0) |
eb39963f | 131 | #define DEF_RANDSEED (0xb1899bedUL) |
1d035750 | 132 | #define DEF_BWAVGTIME (500) |
fc097bfe JA |
133 | #define DEF_CREATE_SER (1) |
134 | #define DEF_CREATE_FSYNC (1) | |
b6794fbf | 135 | #define DEF_LOOPS (1) |
cfc702bd | 136 | #define DEF_VERIFY (0) |
4240cfa1 JA |
137 | |
138 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
892199bd | 139 | |
4240cfa1 | 140 | static int repeatable = DEF_RAND_REPEAT; |
02bdd9ba | 141 | static int rate_quit = 1; |
a0a9b35b JA |
142 | static int write_lat_log; |
143 | static int write_bw_log; | |
98dd52d6 | 144 | static int exitall_on_terminate; |
892199bd | 145 | |
892199bd | 146 | static int thread_number; |
7dd1389e | 147 | static char *ini_file; |
892199bd | 148 | |
8867c0a8 JA |
149 | static int max_jobs = MAX_JOBS; |
150 | ||
8dbff0b1 JA |
151 | static char run_str[MAX_JOBS + 1]; |
152 | ||
892199bd JA |
153 | static int shm_id; |
154 | ||
4240cfa1 JA |
155 | enum { |
156 | DDIR_READ = 0, | |
157 | DDIR_WRITE, | |
158 | }; | |
892199bd | 159 | |
02bdd9ba JA |
160 | /* |
161 | * thread life cycle | |
162 | */ | |
163 | enum { | |
164 | TD_NOT_CREATED = 0, | |
165 | TD_CREATED, | |
e8457004 JA |
166 | TD_RUNNING, |
167 | TD_VERIFYING, | |
02bdd9ba JA |
168 | TD_EXITED, |
169 | TD_REAPED, | |
170 | }; | |
171 | ||
99c6704f JA |
172 | enum { |
173 | MEM_MALLOC, | |
174 | MEM_SHM, | |
175 | }; | |
176 | ||
2c83567e JA |
177 | /* |
178 | * The io unit | |
179 | */ | |
180 | struct io_u { | |
181 | struct iocb iocb; | |
57d753e3 | 182 | struct timeval start_time; |
2c83567e JA |
183 | struct timeval issue_time; |
184 | ||
2c83567e JA |
185 | char *buf; |
186 | unsigned int buflen; | |
4ac89145 | 187 | unsigned long long offset; |
2c83567e JA |
188 | |
189 | struct list_head list; | |
190 | }; | |
191 | ||
57d753e3 JA |
192 | struct io_stat { |
193 | unsigned long val; | |
194 | unsigned long val_sq; | |
195 | unsigned long max_val; | |
196 | unsigned long min_val; | |
197 | unsigned long samples; | |
198 | }; | |
199 | ||
a0a9b35b JA |
200 | struct io_sample { |
201 | unsigned long time; | |
202 | unsigned long val; | |
203 | }; | |
204 | ||
205 | struct io_log { | |
206 | unsigned long nr_samples; | |
207 | unsigned long max_samples; | |
208 | struct io_sample *log; | |
209 | }; | |
210 | ||
e8457004 JA |
211 | #define FIO_HDR_MAGIC 0xf00baaef |
212 | ||
213 | struct verify_header { | |
214 | unsigned int fio_magic; | |
215 | unsigned int len; | |
216 | char md5_digest[MD5_HASH_WORDS * 4]; | |
217 | }; | |
218 | ||
02983297 JA |
219 | #define td_read(td) ((td)->ddir == DDIR_READ) |
220 | #define should_fsync(td) (!td_read(td) && !(td)->odirect) | |
221 | ||
892199bd JA |
222 | struct thread_data { |
223 | char file_name[256]; | |
224 | int thread_number; | |
225 | int error; | |
226 | int fd; | |
892199bd | 227 | pid_t pid; |
6b71c826 | 228 | char *orig_buffer; |
4240cfa1 | 229 | volatile int terminate; |
02bdd9ba | 230 | volatile int runstate; |
40ef7f64 | 231 | volatile int old_runstate; |
f737299d JA |
232 | unsigned int ddir; |
233 | unsigned int ioprio; | |
234 | unsigned int sequential; | |
235 | unsigned int bs; | |
8c033f93 JA |
236 | unsigned int min_bs; |
237 | unsigned int max_bs; | |
f737299d | 238 | unsigned int odirect; |
e97712ed | 239 | unsigned int thinktime; |
4240cfa1 | 240 | unsigned int fsync_blocks; |
fc24389f | 241 | unsigned int start_delay; |
47d45203 | 242 | unsigned int timeout; |
43000118 | 243 | unsigned int use_aio; |
02983297 JA |
244 | unsigned int create_file; |
245 | unsigned int overwrite; | |
b95799ca | 246 | unsigned int invalidate_cache; |
1d035750 | 247 | unsigned int bw_avg_time; |
fc097bfe JA |
248 | unsigned int create_serialize; |
249 | unsigned int create_fsync; | |
b6794fbf | 250 | unsigned int loops; |
02983297 JA |
251 | unsigned long long file_size; |
252 | unsigned long long file_offset; | |
74b4b5fb | 253 | unsigned int sync_io; |
99c6704f | 254 | unsigned int mem_type; |
cfc702bd | 255 | unsigned int verify; |
18e0b78c | 256 | cpu_set_t cpumask; |
86184d14 | 257 | |
7889f07b | 258 | struct drand48_data bsrange_state; |
e8457004 | 259 | struct drand48_data verify_state; |
7889f07b | 260 | |
99c6704f JA |
261 | int shm_id; |
262 | ||
63a09e51 JA |
263 | off_t cur_off; |
264 | ||
254605cd | 265 | io_context_t aio_ctx; |
43000118 | 266 | unsigned int aio_depth; |
43000118 | 267 | struct io_event *aio_events; |
2c83567e JA |
268 | |
269 | unsigned int cur_depth; | |
270 | struct list_head io_u_freelist; | |
271 | struct list_head io_u_busylist; | |
43000118 | 272 | |
7dd1389e | 273 | unsigned int rate; |
4240cfa1 JA |
274 | unsigned int ratemin; |
275 | unsigned int ratecycle; | |
276 | unsigned long rate_usec_cycle; | |
277 | long rate_pending_usleep; | |
8c033f93 | 278 | unsigned long rate_kb; |
4240cfa1 | 279 | struct timeval lastrate; |
86184d14 | 280 | |
892199bd | 281 | unsigned long runtime; /* sec */ |
7889f07b | 282 | unsigned long kb; |
4240cfa1 | 283 | unsigned long io_blocks; |
8c033f93 | 284 | unsigned long io_kb; |
f8e3d840 | 285 | unsigned long this_io_kb; |
7889f07b | 286 | unsigned long last_kb; |
892199bd | 287 | sem_t mutex; |
892199bd JA |
288 | struct drand48_data random_state; |
289 | ||
290 | /* | |
e128065d | 291 | * bandwidth and latency stats |
892199bd | 292 | */ |
57d753e3 JA |
293 | struct io_stat clat_stat; /* completion latency */ |
294 | struct io_stat slat_stat; /* submission latency */ | |
295 | ||
296 | struct io_stat bw_stat; /* bandwidth stats */ | |
8c033f93 | 297 | unsigned long stat_io_kb; |
fd1ae4c9 | 298 | struct timeval stat_sample_time; |
4240cfa1 | 299 | |
a0a9b35b JA |
300 | struct io_log *lat_log; |
301 | struct io_log *bw_log; | |
302 | ||
4240cfa1 | 303 | struct timeval start; |
892199bd JA |
304 | }; |
305 | ||
306 | static struct thread_data *threads; | |
47d45203 | 307 | static struct thread_data def_thread; |
892199bd JA |
308 | |
309 | static sem_t startup_sem; | |
310 | ||
5c24b2c4 | 311 | static void sig_handler(int sig) |
892199bd JA |
312 | { |
313 | int i; | |
314 | ||
213b446c JA |
315 | for (i = 0; i < thread_number; i++) { |
316 | struct thread_data *td = &threads[i]; | |
317 | ||
318 | td->terminate = 1; | |
319 | td->start_delay = 0; | |
320 | } | |
02bdd9ba JA |
321 | } |
322 | ||
e8457004 | 323 | static int init_random_state(struct thread_data *td) |
946d8870 | 324 | { |
e8457004 JA |
325 | unsigned long seed; |
326 | int fd; | |
946d8870 | 327 | |
e8457004 JA |
328 | fd = open("/dev/random", O_RDONLY); |
329 | if (fd == -1) { | |
330 | td->error = errno; | |
331 | return 1; | |
946d8870 JA |
332 | } |
333 | ||
e8457004 JA |
334 | if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) { |
335 | td->error = EIO; | |
336 | close(fd); | |
337 | return 1; | |
946d8870 JA |
338 | } |
339 | ||
e8457004 | 340 | close(fd); |
7889f07b JA |
341 | |
342 | srand48_r(seed, &td->bsrange_state); | |
e8457004 | 343 | srand48_r(seed, &td->verify_state); |
892199bd JA |
344 | |
345 | if (td->sequential) | |
346 | return 0; | |
347 | ||
e8457004 JA |
348 | if (repeatable) |
349 | seed = DEF_RANDSEED; | |
892199bd JA |
350 | |
351 | srand48_r(seed, &td->random_state); | |
352 | return 0; | |
353 | } | |
354 | ||
5c24b2c4 | 355 | static unsigned long utime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
356 | { |
357 | double sec, usec; | |
358 | ||
359 | sec = e->tv_sec - s->tv_sec; | |
360 | usec = e->tv_usec - s->tv_usec; | |
361 | if (sec > 0 && usec < 0) { | |
362 | sec--; | |
363 | usec += 1000000; | |
364 | } | |
365 | ||
366 | sec *= (double) 1000000; | |
367 | ||
368 | return sec + usec; | |
369 | } | |
370 | ||
5c24b2c4 | 371 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
372 | { |
373 | double sec, usec; | |
374 | ||
375 | sec = e->tv_sec - s->tv_sec; | |
376 | usec = e->tv_usec - s->tv_usec; | |
377 | if (sec > 0 && usec < 0) { | |
378 | sec--; | |
379 | usec += 1000000; | |
380 | } | |
381 | ||
382 | sec *= (double) 1000; | |
383 | usec /= (double) 1000; | |
384 | ||
385 | return sec + usec; | |
386 | } | |
387 | ||
be33abe4 JA |
388 | static unsigned long mtime_since_now(struct timeval *s) |
389 | { | |
390 | struct timeval t; | |
391 | ||
392 | gettimeofday(&t, NULL); | |
393 | return mtime_since(s, &t); | |
394 | } | |
395 | ||
98168d55 JA |
396 | static inline unsigned long msec_now(struct timeval *s) |
397 | { | |
398 | return s->tv_sec * 1000 + s->tv_usec / 1000; | |
399 | } | |
400 | ||
4ac89145 | 401 | static unsigned long long get_next_offset(struct thread_data *td) |
892199bd | 402 | { |
f31fd9c8 | 403 | unsigned long long kb; |
40ef7f64 | 404 | double r; |
892199bd JA |
405 | |
406 | if (!td->sequential) { | |
40ef7f64 | 407 | drand48_r(&td->random_state, &r); |
f31fd9c8 | 408 | kb = (1+(double) (td->kb-1) * r / (RAND_MAX+1.0)); |
7889f07b | 409 | } else |
f31fd9c8 | 410 | kb = td->last_kb; |
7889f07b | 411 | |
f31fd9c8 | 412 | return (kb << 10) + td->file_offset; |
7889f07b JA |
413 | } |
414 | ||
415 | static unsigned int get_next_buflen(struct thread_data *td) | |
416 | { | |
417 | unsigned int buflen; | |
40ef7f64 | 418 | double r; |
7889f07b JA |
419 | |
420 | if (td->min_bs == td->max_bs) | |
421 | buflen = td->min_bs; | |
422 | else { | |
40ef7f64 | 423 | drand48_r(&td->bsrange_state, &r); |
7889f07b JA |
424 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
425 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); | |
892199bd JA |
426 | } |
427 | ||
f8e3d840 JA |
428 | if (buflen > ((td->kb - td->this_io_kb) << 10)) |
429 | buflen = (td->kb - td->this_io_kb) << 10; | |
7889f07b JA |
430 | |
431 | td->last_kb += buflen >> 10; | |
432 | return buflen; | |
892199bd JA |
433 | } |
434 | ||
57d753e3 JA |
435 | static inline void add_stat_sample(struct thread_data *td, struct io_stat *is, |
436 | unsigned long val) | |
892199bd | 437 | { |
57d753e3 JA |
438 | if (val > is->max_val) |
439 | is->max_val = val; | |
440 | if (val < is->min_val) | |
441 | is->min_val = val; | |
442 | ||
443 | is->val += val; | |
444 | is->val_sq += val * val; | |
445 | is->samples++; | |
446 | } | |
fd1ae4c9 | 447 | |
a0a9b35b JA |
448 | static void add_log_sample(struct thread_data *td, struct io_log *log, |
449 | unsigned long val) | |
450 | { | |
451 | if (log->nr_samples == log->max_samples) { | |
452 | int new_size = sizeof(struct io_sample) * log->max_samples * 2; | |
453 | ||
454 | log->log = realloc(log->log, new_size); | |
455 | log->max_samples <<= 1; | |
456 | } | |
457 | ||
458 | log->log[log->nr_samples].val = val; | |
459 | log->log[log->nr_samples].time = mtime_since_now(&td->start); | |
460 | log->nr_samples++; | |
461 | } | |
462 | ||
57d753e3 JA |
463 | static void add_clat_sample(struct thread_data *td, unsigned long msec) |
464 | { | |
465 | add_stat_sample(td, &td->clat_stat, msec); | |
a0a9b35b JA |
466 | |
467 | if (td->lat_log) | |
468 | add_log_sample(td, td->lat_log, msec); | |
57d753e3 | 469 | } |
fd1ae4c9 | 470 | |
57d753e3 JA |
471 | static void add_slat_sample(struct thread_data *td, unsigned long msec) |
472 | { | |
473 | add_stat_sample(td, &td->slat_stat, msec); | |
474 | } | |
fd1ae4c9 | 475 | |
57d753e3 JA |
476 | static void add_bw_sample(struct thread_data *td, unsigned long msec) |
477 | { | |
478 | unsigned long spent = mtime_since_now(&td->stat_sample_time); | |
479 | unsigned long rate; | |
480 | ||
1d035750 | 481 | if (spent < td->bw_avg_time) |
57d753e3 JA |
482 | return; |
483 | ||
f8e3d840 | 484 | rate = ((td->this_io_kb - td->stat_io_kb) * 1024) / spent; |
57d753e3 JA |
485 | add_stat_sample(td, &td->bw_stat, rate); |
486 | ||
a0a9b35b JA |
487 | if (td->bw_log) |
488 | add_log_sample(td, td->bw_log, rate); | |
489 | ||
57d753e3 | 490 | gettimeofday(&td->stat_sample_time, NULL); |
f8e3d840 | 491 | td->stat_io_kb = td->this_io_kb; |
892199bd JA |
492 | } |
493 | ||
5c24b2c4 | 494 | static void usec_sleep(int usec) |
892199bd | 495 | { |
86184d14 JA |
496 | struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 }; |
497 | struct timespec rem; | |
892199bd JA |
498 | |
499 | do { | |
86184d14 JA |
500 | rem.tv_sec = rem.tv_nsec = 0; |
501 | nanosleep(&req, &rem); | |
502 | if (!rem.tv_nsec) | |
892199bd | 503 | break; |
86184d14 JA |
504 | |
505 | req.tv_nsec = rem.tv_nsec; | |
892199bd JA |
506 | } while (1); |
507 | } | |
508 | ||
9e850933 JA |
509 | static void rate_throttle(struct thread_data *td, unsigned long time_spent, |
510 | unsigned int bytes) | |
86184d14 | 511 | { |
9e850933 JA |
512 | unsigned long usec_cycle; |
513 | ||
4240cfa1 JA |
514 | if (!td->rate) |
515 | return; | |
516 | ||
9e850933 JA |
517 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs); |
518 | ||
519 | if (time_spent < usec_cycle) { | |
520 | unsigned long s = usec_cycle - time_spent; | |
86184d14 JA |
521 | |
522 | td->rate_pending_usleep += s; | |
fad86e6a | 523 | if (td->rate_pending_usleep >= 100000) { |
86184d14 JA |
524 | usec_sleep(td->rate_pending_usleep); |
525 | td->rate_pending_usleep = 0; | |
526 | } | |
4240cfa1 | 527 | } else { |
9e850933 | 528 | long overtime = time_spent - usec_cycle; |
42b2b9fe | 529 | |
4240cfa1 JA |
530 | td->rate_pending_usleep -= overtime; |
531 | } | |
532 | } | |
533 | ||
5c24b2c4 | 534 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
4240cfa1 | 535 | { |
7607bc6b | 536 | unsigned long spent; |
4240cfa1 JA |
537 | unsigned long rate; |
538 | ||
539 | /* | |
540 | * allow a 2 second settle period in the beginning | |
541 | */ | |
7607bc6b | 542 | if (mtime_since(&td->start, now) < 2000) |
4240cfa1 JA |
543 | return 0; |
544 | ||
545 | /* | |
546 | * if rate blocks is set, sample is running | |
547 | */ | |
8c033f93 | 548 | if (td->rate_kb) { |
4240cfa1 JA |
549 | spent = mtime_since(&td->lastrate, now); |
550 | if (spent < td->ratecycle) | |
551 | return 0; | |
552 | ||
f8e3d840 | 553 | rate = ((td->this_io_kb - td->rate_kb) * 1024) / spent; |
4240cfa1 JA |
554 | if (rate < td->ratemin) { |
555 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); | |
02bdd9ba | 556 | if (rate_quit) |
e6402082 | 557 | sig_handler(0); |
4240cfa1 JA |
558 | return 1; |
559 | } | |
86184d14 | 560 | } |
4240cfa1 | 561 | |
f8e3d840 | 562 | td->rate_kb = td->this_io_kb; |
4240cfa1 JA |
563 | memcpy(&td->lastrate, now, sizeof(*now)); |
564 | return 0; | |
86184d14 JA |
565 | } |
566 | ||
67903a2e JA |
567 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
568 | { | |
569 | if (mtime_since(&td->start, t) >= td->timeout * 1000) | |
570 | return 1; | |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
e8457004 JA |
575 | static void fill_random_bytes(struct thread_data *td, |
576 | unsigned char *p, unsigned int len) | |
577 | { | |
40ef7f64 | 578 | double r; |
e8457004 | 579 | int todo; |
e8457004 JA |
580 | |
581 | while (len) { | |
40ef7f64 | 582 | drand48_r(&td->verify_state, &r); |
e8457004 | 583 | |
40ef7f64 JA |
584 | /* |
585 | * lrand48_r seems to be broken and only fill the bottom | |
586 | * 32-bits, even on 64-bit archs with 64-bit longs | |
587 | */ | |
588 | todo = sizeof(r); | |
e8457004 JA |
589 | if (todo > len) |
590 | todo = len; | |
591 | ||
592 | memcpy(p, &r, todo); | |
593 | ||
594 | len -= todo; | |
595 | p += todo; | |
596 | } | |
597 | } | |
598 | ||
599 | static int verify_io_u(struct thread_data *td, struct io_u *io_u) | |
600 | { | |
601 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
602 | unsigned char *p = (unsigned char *) io_u->buf; | |
603 | struct md5_ctx md5_ctx; | |
604 | ||
2356490b JA |
605 | /* |
606 | * assume this is a file hole, if we have been doing random writes | |
607 | * and magic is zero | |
608 | */ | |
609 | if (hdr->fio_magic != FIO_HDR_MAGIC) { | |
610 | if (!td->sequential && !hdr->fio_magic) | |
611 | return 0; | |
612 | ||
e8457004 | 613 | return 1; |
2356490b | 614 | } |
e8457004 JA |
615 | |
616 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
617 | p += sizeof(*hdr); | |
618 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); | |
619 | ||
620 | return memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
621 | } | |
622 | ||
623 | static int verify_io_us(struct thread_data *td, struct io_u *io_u, int *back) | |
624 | { | |
625 | struct verify_header *hdr; | |
626 | unsigned int left; | |
627 | struct io_u i; | |
628 | char *buf; | |
629 | off_t off; | |
630 | ||
f0f3411b JA |
631 | if (back) |
632 | *back = 0; | |
e8457004 JA |
633 | |
634 | left = io_u->buflen; | |
635 | buf = io_u->buf; | |
636 | off = io_u->offset; | |
637 | while (left) { | |
638 | hdr = (struct verify_header *) buf; | |
639 | i.buf = buf; | |
640 | i.buflen = hdr->len; | |
641 | ||
642 | if (hdr->len > left) { | |
f0f3411b JA |
643 | if (back) |
644 | *back = left; | |
e8457004 JA |
645 | return 0; |
646 | } | |
647 | ||
648 | if (verify_io_u(td, &i)) { | |
649 | printf("failed verify at offset %lu\n", (unsigned long) off); | |
f0f3411b | 650 | td->error = EBADMSG; |
e8457004 JA |
651 | return 1; |
652 | } | |
653 | ||
654 | buf += hdr->len; | |
655 | left -= hdr->len; | |
656 | off += hdr->len; | |
657 | } | |
658 | ||
659 | return 0; | |
660 | } | |
661 | ||
cfc702bd JA |
662 | /* |
663 | * fill body of io_u->buf with random data and add a header with the | |
664 | * (eg) sha1sum of that data. | |
665 | */ | |
e8457004 | 666 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
cfc702bd | 667 | { |
e8457004 JA |
668 | struct md5_ctx md5_ctx; |
669 | struct verify_header hdr; | |
670 | unsigned char *p = (unsigned char *) io_u->buf; | |
671 | ||
672 | hdr.fio_magic = FIO_HDR_MAGIC; | |
673 | hdr.len = io_u->buflen; | |
674 | p += sizeof(hdr); | |
675 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); | |
676 | ||
677 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
678 | md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr)); | |
679 | memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
680 | memcpy(io_u->buf, &hdr, sizeof(hdr)); | |
cfc702bd JA |
681 | } |
682 | ||
2c83567e JA |
683 | static void put_io_u(struct thread_data *td, struct io_u *io_u) |
684 | { | |
685 | list_del(&io_u->list); | |
686 | list_add(&io_u->list, &td->io_u_freelist); | |
687 | td->cur_depth--; | |
688 | } | |
689 | ||
f0f3411b JA |
690 | #define queue_full(td) (list_empty(&(td)->io_u_freelist)) |
691 | ||
e8457004 JA |
692 | static struct io_u *__get_io_u(struct thread_data *td) |
693 | { | |
694 | struct io_u *io_u; | |
695 | ||
f0f3411b | 696 | if (queue_full(td)) |
e8457004 JA |
697 | return NULL; |
698 | ||
699 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); | |
700 | list_del(&io_u->list); | |
701 | list_add(&io_u->list, &td->io_u_busylist); | |
f4bb2243 | 702 | td->cur_depth++; |
e8457004 JA |
703 | return io_u; |
704 | } | |
705 | ||
2c83567e JA |
706 | static struct io_u *get_io_u(struct thread_data *td) |
707 | { | |
708 | struct io_u *io_u; | |
709 | ||
e8457004 JA |
710 | io_u = __get_io_u(td); |
711 | if (!io_u) | |
2c83567e JA |
712 | return NULL; |
713 | ||
b2a369fb JA |
714 | io_u->offset = get_next_offset(td); |
715 | io_u->buflen = get_next_buflen(td); | |
716 | if (!io_u->buflen) { | |
e8457004 | 717 | put_io_u(td, io_u); |
7889f07b | 718 | return NULL; |
e8457004 | 719 | } |
2c83567e | 720 | |
cfc702bd | 721 | if (!td_read(td) && td->verify) |
e8457004 | 722 | populate_io_u(td, io_u); |
cfc702bd | 723 | |
2c83567e JA |
724 | if (td->use_aio) { |
725 | if (td_read(td)) | |
726 | io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
727 | else | |
728 | io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
729 | } | |
730 | ||
57d753e3 | 731 | gettimeofday(&io_u->start_time, NULL); |
2c83567e JA |
732 | return io_u; |
733 | } | |
734 | ||
40ef7f64 JA |
735 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
736 | { | |
737 | td->old_runstate = td->runstate; | |
738 | td->runstate = runstate; | |
739 | } | |
740 | ||
cfc702bd JA |
741 | static int do_sync_verify(struct thread_data *td) |
742 | { | |
40ef7f64 | 743 | struct timeval t; |
e8457004 JA |
744 | struct io_u *io_u = NULL; |
745 | loff_t off = 0; | |
746 | int back, ret; | |
747 | ||
40ef7f64 | 748 | td_set_runstate(td, TD_VERIFYING); |
e8457004 JA |
749 | |
750 | io_u = __get_io_u(td); | |
751 | ||
752 | if (lseek(td->fd, 0, SEEK_SET) < 0) { | |
753 | td->error = errno; | |
754 | goto out; | |
755 | } | |
756 | ||
40ef7f64 JA |
757 | if (!td->odirect) { |
758 | unsigned long size = td->kb << 10; | |
759 | ||
760 | if (fadvise(td->fd, 0, size, POSIX_FADV_DONTNEED) < 0) { | |
761 | td->error = errno; | |
762 | goto out; | |
763 | } | |
764 | } | |
765 | ||
e8457004 JA |
766 | do { |
767 | if (td->terminate) | |
768 | break; | |
40ef7f64 JA |
769 | |
770 | gettimeofday(&t, NULL); | |
771 | if (runtime_exceeded(td, &t)) | |
772 | break; | |
773 | ||
e8457004 JA |
774 | io_u->offset = off; |
775 | io_u->buflen = td->max_bs; | |
776 | ||
777 | ret = read(td->fd, io_u->buf, io_u->buflen); | |
778 | if (ret < (int) io_u->buflen) { | |
779 | if (ret == -1) { | |
780 | td->error = errno; | |
781 | break; | |
782 | } else if (!ret) | |
783 | break; | |
784 | else | |
785 | io_u->buflen = ret; | |
786 | } | |
787 | ||
788 | if (verify_io_us(td, io_u, &back)) | |
789 | break; | |
790 | ||
791 | if (back) { | |
f8e3d840 | 792 | ret -= back; |
e8457004 JA |
793 | if (lseek(td->fd, -back, SEEK_CUR) < 0) { |
794 | td->error = errno; | |
795 | break; | |
796 | } | |
797 | } | |
798 | ||
799 | off += ret; | |
800 | } while (1); | |
801 | ||
802 | out: | |
40ef7f64 | 803 | td_set_runstate(td, TD_RUNNING); |
e8457004 JA |
804 | put_io_u(td, io_u); |
805 | ||
806 | return td->error == 0; | |
cfc702bd JA |
807 | } |
808 | ||
43000118 | 809 | static void do_sync_io(struct thread_data *td) |
892199bd | 810 | { |
7889f07b | 811 | unsigned long msec, usec; |
e8457004 | 812 | struct io_u *io_u = NULL; |
2c83567e | 813 | struct timeval e; |
892199bd | 814 | |
f8e3d840 | 815 | for (td->this_io_kb = 0; td->this_io_kb < td->kb;) { |
892199bd JA |
816 | int ret; |
817 | ||
818 | if (td->terminate) | |
819 | break; | |
820 | ||
2c83567e | 821 | io_u = get_io_u(td); |
7889f07b JA |
822 | if (!io_u) |
823 | break; | |
2c83567e | 824 | |
63a09e51 JA |
825 | if (td->cur_off != io_u->offset) { |
826 | if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) { | |
827 | td->error = errno; | |
828 | break; | |
829 | } | |
892199bd JA |
830 | } |
831 | ||
02983297 | 832 | if (td_read(td)) |
2c83567e | 833 | ret = read(td->fd, io_u->buf, io_u->buflen); |
892199bd | 834 | else |
2c83567e | 835 | ret = write(td->fd, io_u->buf, io_u->buflen); |
892199bd | 836 | |
2c83567e | 837 | if (ret < (int) io_u->buflen) { |
892199bd JA |
838 | if (ret == -1) |
839 | td->error = errno; | |
840 | break; | |
841 | } | |
842 | ||
4240cfa1 | 843 | td->io_blocks++; |
8c033f93 | 844 | td->io_kb += io_u->buflen >> 10; |
f8e3d840 | 845 | td->this_io_kb += io_u->buflen >> 10; |
63a09e51 | 846 | td->cur_off = io_u->offset + io_u->buflen; |
4240cfa1 | 847 | |
86184d14 JA |
848 | gettimeofday(&e, NULL); |
849 | ||
57d753e3 | 850 | usec = utime_since(&io_u->start_time, &e); |
86184d14 | 851 | |
9e850933 | 852 | rate_throttle(td, usec, io_u->buflen); |
892199bd | 853 | |
4240cfa1 JA |
854 | if (check_min_rate(td, &e)) { |
855 | td->error = ENODATA; | |
856 | break; | |
857 | } | |
892199bd | 858 | |
4240cfa1 | 859 | msec = usec / 1000; |
57d753e3 JA |
860 | add_clat_sample(td, msec); |
861 | add_bw_sample(td, msec); | |
67903a2e JA |
862 | |
863 | if (runtime_exceeded(td, &e)) | |
864 | break; | |
2c83567e | 865 | |
cdf92433 | 866 | put_io_u(td, io_u); |
e8457004 | 867 | io_u = NULL; |
cdf92433 | 868 | |
e97712ed JA |
869 | if (td->thinktime) |
870 | usec_sleep(td->thinktime); | |
871 | ||
cdf92433 JA |
872 | if (should_fsync(td) && td->fsync_blocks && |
873 | (td->io_blocks % td->fsync_blocks) == 0) | |
874 | fsync(td->fd); | |
892199bd JA |
875 | } |
876 | ||
e8457004 JA |
877 | if (io_u) |
878 | put_io_u(td, io_u); | |
879 | ||
4240cfa1 | 880 | if (should_fsync(td)) |
892199bd | 881 | fsync(td->fd); |
892199bd | 882 | } |
43000118 | 883 | |
2c83567e | 884 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
56b0eff0 | 885 | { |
2c83567e | 886 | struct iocb *iocb = &io_u->iocb; |
56b0eff0 JA |
887 | int ret; |
888 | ||
889 | do { | |
254605cd | 890 | ret = io_submit(td->aio_ctx, 1, &iocb); |
56b0eff0 JA |
891 | if (ret == 1) |
892 | return 0; | |
a592bd33 | 893 | else if (ret == EAGAIN) |
56b0eff0 | 894 | usleep(100); |
a592bd33 JA |
895 | else if (ret == EINTR) |
896 | continue; | |
56b0eff0 JA |
897 | else |
898 | break; | |
899 | } while (1); | |
900 | ||
a592bd33 | 901 | return ret; |
56b0eff0 JA |
902 | } |
903 | ||
98168d55 | 904 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
2c83567e JA |
905 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
906 | ||
f0f3411b | 907 | static int ios_completed(struct thread_data *td, int nr) |
2c83567e JA |
908 | { |
909 | unsigned long msec; | |
910 | struct io_u *io_u; | |
911 | struct timeval e; | |
9e850933 | 912 | int i, bytes_done; |
2c83567e | 913 | |
f0f3411b | 914 | gettimeofday(&e, NULL); |
2c83567e | 915 | |
9e850933 | 916 | for (i = 0, bytes_done = 0; i < nr; i++) { |
2c83567e JA |
917 | io_u = ev_to_iou(td->aio_events + i); |
918 | ||
f0f3411b JA |
919 | td->io_blocks++; |
920 | td->io_kb += io_u->buflen >> 10; | |
921 | td->this_io_kb += io_u->buflen >> 10; | |
8c033f93 | 922 | |
f0f3411b | 923 | msec = mtime_since(&io_u->issue_time, &e); |
2c83567e | 924 | |
f0f3411b JA |
925 | add_clat_sample(td, msec); |
926 | add_bw_sample(td, msec); | |
2c83567e | 927 | |
f4bb2243 | 928 | bytes_done += io_u->buflen; |
2c83567e JA |
929 | put_io_u(td, io_u); |
930 | } | |
9e850933 JA |
931 | |
932 | return bytes_done; | |
2c83567e JA |
933 | } |
934 | ||
935 | static void cleanup_pending_aio(struct thread_data *td) | |
936 | { | |
937 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
938 | struct list_head *entry, *n; | |
939 | struct io_u *io_u; | |
940 | int r; | |
941 | ||
942 | /* | |
943 | * get immediately available events, if any | |
944 | */ | |
945 | r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts); | |
946 | if (r > 0) | |
f0f3411b | 947 | ios_completed(td, r); |
2c83567e JA |
948 | |
949 | /* | |
950 | * now cancel remaining active events | |
951 | */ | |
952 | list_for_each_safe(entry, n, &td->io_u_busylist) { | |
953 | io_u = list_entry(entry, struct io_u, list); | |
954 | ||
955 | r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events); | |
956 | if (!r) | |
957 | put_io_u(td, io_u); | |
958 | } | |
959 | ||
960 | if (td->cur_depth) { | |
961 | r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL); | |
962 | if (r > 0) | |
f0f3411b | 963 | ios_completed(td, r); |
2c83567e JA |
964 | } |
965 | } | |
98168d55 | 966 | |
cfc702bd JA |
967 | static int do_async_verify(struct thread_data *td) |
968 | { | |
f4bb2243 | 969 | struct timeval t; |
f0f3411b JA |
970 | struct io_u *io_u, *v_io_u; |
971 | struct verify_header *hdr; | |
f4bb2243 | 972 | int ret, back; |
f0f3411b | 973 | char *p; |
f4bb2243 JA |
974 | |
975 | td_set_runstate(td, TD_VERIFYING); | |
976 | ||
977 | td->cur_off = 0; | |
978 | td->last_kb = 0; | |
f0f3411b | 979 | v_io_u = NULL; |
f4bb2243 JA |
980 | |
981 | do { | |
982 | if (td->terminate) | |
983 | break; | |
984 | ||
985 | gettimeofday(&t, NULL); | |
986 | if (runtime_exceeded(td, &t)) | |
987 | break; | |
988 | ||
989 | io_u = __get_io_u(td); | |
990 | if (!io_u) | |
991 | break; | |
992 | ||
993 | io_u->offset = td->cur_off; | |
994 | io_u->buflen = td->max_bs; | |
995 | ||
996 | if (io_u->offset + io_u->buflen > (td->kb << 10)) { | |
997 | io_u->buflen = (td->kb << 10) - io_u->offset; | |
998 | if (!io_u->buflen) { | |
999 | put_io_u(td, io_u); | |
1000 | break; | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
1005 | ret = io_u_queue(td, io_u); | |
1006 | if (ret) { | |
1007 | put_io_u(td, io_u); | |
1008 | td->error = ret; | |
1009 | break; | |
1010 | } | |
1011 | ||
f0f3411b JA |
1012 | /* |
1013 | * we have one pending to verify, do that while the next | |
1014 | * we are doing io on the next one | |
1015 | */ | |
1016 | if (v_io_u) { | |
1017 | ret = verify_io_us(td, v_io_u, NULL); | |
1018 | put_io_u(td, v_io_u); | |
1019 | v_io_u = NULL; | |
1020 | if (ret) | |
1021 | break; | |
1022 | } | |
1023 | ||
1024 | ret = io_getevents(td->aio_ctx, 1, 1, td->aio_events, NULL); | |
1025 | if (ret != 1) { | |
1026 | if (ret < 0) | |
1027 | td->error = ret; | |
f4bb2243 JA |
1028 | break; |
1029 | } | |
1030 | ||
f0f3411b JA |
1031 | /* |
1032 | * got our io_u to verify, find back offset so we can | |
1033 | * submit the next one before verifying this one | |
1034 | */ | |
1035 | v_io_u = ev_to_iou(td->aio_events); | |
1036 | p = v_io_u->buf; | |
1037 | back = v_io_u->buflen; | |
1038 | do { | |
1039 | hdr = (struct verify_header *) p; | |
1040 | ||
1041 | if (hdr->len > back) | |
1042 | break; | |
f4bb2243 | 1043 | |
f0f3411b JA |
1044 | back -= hdr->len; |
1045 | p += hdr->len; | |
1046 | } while (back); | |
f4bb2243 | 1047 | |
f0f3411b JA |
1048 | td->cur_off += (v_io_u->buflen - back); |
1049 | ||
1050 | /* | |
1051 | * if max depth is 1, we need to verify now | |
1052 | */ | |
1053 | if (queue_full(td)) { | |
1054 | ret = verify_io_us(td, v_io_u, NULL); | |
1055 | put_io_u(td, v_io_u); | |
1056 | v_io_u = NULL; | |
1057 | if (ret) | |
1058 | break; | |
1059 | } | |
f4bb2243 JA |
1060 | } while (1); |
1061 | ||
f0f3411b JA |
1062 | if (v_io_u) { |
1063 | verify_io_us(td, v_io_u, NULL); | |
1064 | put_io_u(td, v_io_u); | |
1065 | } | |
1066 | ||
f4bb2243 JA |
1067 | if (td->cur_depth) |
1068 | cleanup_pending_aio(td); | |
1069 | ||
1070 | td_set_runstate(td, TD_RUNNING); | |
1071 | return td->error == 0; | |
cfc702bd JA |
1072 | } |
1073 | ||
43000118 JA |
1074 | static void do_async_io(struct thread_data *td) |
1075 | { | |
1076 | struct timeval s, e; | |
7889f07b | 1077 | unsigned long usec; |
43000118 | 1078 | |
f8e3d840 | 1079 | for (td->this_io_kb = 0; td->this_io_kb < td->kb;) { |
43000118 JA |
1080 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
1081 | struct timespec *timeout; | |
2c83567e JA |
1082 | int ret, min_evts = 0; |
1083 | struct io_u *io_u; | |
9e850933 | 1084 | unsigned int bytes_done; |
43000118 JA |
1085 | |
1086 | if (td->terminate) | |
1087 | break; | |
1088 | ||
2c83567e | 1089 | io_u = get_io_u(td); |
7889f07b JA |
1090 | if (!io_u) |
1091 | break; | |
43000118 | 1092 | |
57d753e3 | 1093 | memcpy(&s, &io_u->start_time, sizeof(s)); |
8baf1bcc | 1094 | |
2c83567e | 1095 | ret = io_u_queue(td, io_u); |
56b0eff0 | 1096 | if (ret) { |
a3fdb993 | 1097 | put_io_u(td, io_u); |
a592bd33 | 1098 | td->error = ret; |
43000118 JA |
1099 | break; |
1100 | } | |
1101 | ||
57d753e3 JA |
1102 | gettimeofday(&io_u->issue_time, NULL); |
1103 | add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
2c83567e | 1104 | if (td->cur_depth < td->aio_depth) { |
43000118 JA |
1105 | timeout = &ts; |
1106 | min_evts = 0; | |
1107 | } else { | |
1108 | timeout = NULL; | |
1109 | min_evts = 1; | |
1110 | } | |
1111 | ||
2c83567e | 1112 | ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout); |
43000118 JA |
1113 | if (ret < 0) { |
1114 | td->error = errno; | |
1115 | break; | |
1116 | } else if (!ret) | |
1117 | continue; | |
1118 | ||
f0f3411b | 1119 | bytes_done = ios_completed(td, ret); |
43000118 | 1120 | |
98168d55 JA |
1121 | /* |
1122 | * the rate is batched for now, it should work for batches | |
1123 | * of completions except the very first one which may look | |
1124 | * a little bursty | |
1125 | */ | |
2c83567e | 1126 | gettimeofday(&e, NULL); |
43000118 JA |
1127 | usec = utime_since(&s, &e); |
1128 | ||
9e850933 | 1129 | rate_throttle(td, usec, bytes_done); |
43000118 JA |
1130 | |
1131 | if (check_min_rate(td, &e)) { | |
1132 | td->error = ENODATA; | |
1133 | break; | |
1134 | } | |
67903a2e JA |
1135 | |
1136 | if (runtime_exceeded(td, &e)) | |
1137 | break; | |
765d9223 JA |
1138 | |
1139 | if (td->thinktime) | |
1140 | usec_sleep(td->thinktime); | |
cdf92433 JA |
1141 | |
1142 | if (should_fsync(td) && td->fsync_blocks && | |
1143 | (td->io_blocks % td->fsync_blocks) == 0) | |
1144 | fsync(td->fd); | |
43000118 | 1145 | } |
56b0eff0 | 1146 | |
2c83567e JA |
1147 | if (td->cur_depth) |
1148 | cleanup_pending_aio(td); | |
4ac89145 JA |
1149 | |
1150 | if (should_fsync(td)) | |
1151 | fsync(td->fd); | |
56b0eff0 JA |
1152 | } |
1153 | ||
1154 | static void cleanup_aio(struct thread_data *td) | |
1155 | { | |
254605cd JA |
1156 | io_destroy(td->aio_ctx); |
1157 | ||
43000118 JA |
1158 | if (td->aio_events) |
1159 | free(td->aio_events); | |
43000118 JA |
1160 | } |
1161 | ||
1162 | static int init_aio(struct thread_data *td) | |
1163 | { | |
254605cd | 1164 | if (io_queue_init(td->aio_depth, &td->aio_ctx)) { |
43000118 JA |
1165 | td->error = errno; |
1166 | return 1; | |
1167 | } | |
1168 | ||
43000118 | 1169 | td->aio_events = malloc(td->aio_depth * sizeof(struct io_event)); |
43000118 JA |
1170 | return 0; |
1171 | } | |
1172 | ||
2c83567e JA |
1173 | static void cleanup_io_u(struct thread_data *td) |
1174 | { | |
1175 | struct list_head *entry, *n; | |
1176 | struct io_u *io_u; | |
1177 | ||
1178 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
1179 | io_u = list_entry(entry, struct io_u, list); | |
1180 | ||
1181 | list_del(&io_u->list); | |
2c83567e JA |
1182 | free(io_u); |
1183 | } | |
6b71c826 | 1184 | |
99c6704f JA |
1185 | if (td->mem_type == MEM_MALLOC) |
1186 | free(td->orig_buffer); | |
1187 | else if (td->mem_type == MEM_SHM) { | |
1188 | struct shmid_ds sbuf; | |
1189 | ||
1190 | shmdt(td->orig_buffer); | |
1191 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
1192 | } | |
2c83567e JA |
1193 | } |
1194 | ||
99c6704f | 1195 | static int init_io_u(struct thread_data *td) |
2c83567e JA |
1196 | { |
1197 | struct io_u *io_u; | |
99c6704f | 1198 | int i, max_units, mem_size; |
6b71c826 | 1199 | char *p; |
2c83567e JA |
1200 | |
1201 | if (!td->use_aio) | |
1202 | max_units = 1; | |
1203 | else | |
1204 | max_units = td->aio_depth; | |
1205 | ||
7889f07b | 1206 | mem_size = td->max_bs * max_units + MASK; |
99c6704f JA |
1207 | |
1208 | if (td->mem_type == MEM_MALLOC) | |
1209 | td->orig_buffer = malloc(mem_size); | |
1210 | else if (td->mem_type == MEM_SHM) { | |
1211 | td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600); | |
1212 | if (td->shm_id < 0) { | |
1213 | td->error = errno; | |
1214 | perror("shmget"); | |
1215 | return 1; | |
1216 | } | |
1217 | ||
1218 | td->orig_buffer = shmat(td->shm_id, NULL, 0); | |
1219 | if (td->orig_buffer == (void *) -1) { | |
1220 | td->error = errno; | |
1221 | perror("shmat"); | |
1222 | return 1; | |
1223 | } | |
1224 | } | |
6b71c826 | 1225 | |
2c83567e JA |
1226 | INIT_LIST_HEAD(&td->io_u_freelist); |
1227 | INIT_LIST_HEAD(&td->io_u_busylist); | |
1228 | ||
99c6704f | 1229 | p = ALIGN(td->orig_buffer); |
2c83567e JA |
1230 | for (i = 0; i < max_units; i++) { |
1231 | io_u = malloc(sizeof(*io_u)); | |
1232 | memset(io_u, 0, sizeof(*io_u)); | |
1233 | INIT_LIST_HEAD(&io_u->list); | |
1234 | ||
7889f07b | 1235 | io_u->buf = p + td->max_bs * i; |
2c83567e JA |
1236 | list_add(&io_u->list, &td->io_u_freelist); |
1237 | } | |
99c6704f JA |
1238 | |
1239 | return 0; | |
2c83567e JA |
1240 | } |
1241 | ||
a0a9b35b JA |
1242 | static void setup_log(struct io_log **log) |
1243 | { | |
1244 | struct io_log *l = malloc(sizeof(*l)); | |
1245 | ||
1246 | l->nr_samples = 0; | |
1247 | l->max_samples = 1024; | |
1248 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); | |
1249 | *log = l; | |
1250 | } | |
1251 | ||
1252 | static void finish_log(struct thread_data *td, struct io_log *log, char *name) | |
1253 | { | |
1254 | char file_name[128]; | |
1255 | FILE *f; | |
1256 | int i; | |
1257 | ||
1258 | sprintf(file_name, "client%d_%s.log", td->thread_number, name); | |
1259 | f = fopen(file_name, "w"); | |
1260 | if (!f) { | |
1261 | perror("fopen log"); | |
1262 | return; | |
1263 | } | |
1264 | ||
1265 | for (i = 0; i < log->nr_samples; i++) | |
1266 | fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val); | |
1267 | ||
1268 | fclose(f); | |
1269 | free(log->log); | |
1270 | free(log); | |
1271 | } | |
1272 | ||
02983297 JA |
1273 | static int create_file(struct thread_data *td) |
1274 | { | |
7889f07b | 1275 | unsigned long long left; |
02983297 | 1276 | char *b; |
7889f07b | 1277 | int r, bs; |
02983297 | 1278 | |
02983297 JA |
1279 | /* |
1280 | * unless specifically asked for overwrite, let normal io extend it | |
1281 | */ | |
1282 | if (!td_read(td) && !td->overwrite) | |
1283 | return 0; | |
1284 | ||
57d753e3 JA |
1285 | if (!td->file_size) { |
1286 | fprintf(stderr, "Need size for create\n"); | |
1287 | td->error = EINVAL; | |
1288 | return 1; | |
1289 | } | |
1290 | ||
42fd89a7 JA |
1291 | printf("Client%d: Laying out IO file\n", td->thread_number); |
1292 | ||
02983297 JA |
1293 | td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
1294 | if (td->fd < 0) { | |
1295 | td->error = errno; | |
1296 | return 1; | |
1297 | } | |
1298 | ||
7889f07b JA |
1299 | td->kb = td->file_size >> 10; |
1300 | b = malloc(td->max_bs); | |
1301 | memset(b, 0, td->max_bs); | |
1302 | ||
1303 | left = td->file_size; | |
1304 | while (left) { | |
1305 | bs = td->max_bs; | |
1306 | if (bs > left) | |
1307 | bs = left; | |
02983297 | 1308 | |
7889f07b | 1309 | r = write(td->fd, b, bs); |
02983297 | 1310 | |
7889f07b JA |
1311 | if (r == bs) { |
1312 | left -= bs; | |
02983297 | 1313 | continue; |
7889f07b | 1314 | } else { |
02983297 JA |
1315 | if (r < 0) |
1316 | td->error = errno; | |
1317 | else | |
1318 | td->error = EIO; | |
1319 | ||
1320 | break; | |
1321 | } | |
1322 | } | |
1323 | ||
fc097bfe JA |
1324 | if (td->create_fsync) |
1325 | fsync(td->fd); | |
1326 | ||
02983297 JA |
1327 | close(td->fd); |
1328 | td->fd = -1; | |
1329 | free(b); | |
1330 | return 0; | |
1331 | } | |
1332 | ||
1333 | static int file_exists(struct thread_data *td) | |
1334 | { | |
1335 | struct stat st; | |
1336 | ||
1337 | if (stat(td->file_name, &st) != -1) | |
1338 | return 1; | |
1339 | ||
1340 | return errno != ENOENT; | |
1341 | } | |
1342 | ||
1343 | static int setup_file(struct thread_data *td) | |
1344 | { | |
1345 | struct stat st; | |
1346 | int flags = 0; | |
1347 | ||
1348 | if (!file_exists(td)) { | |
1349 | if (!td->create_file) { | |
1350 | td->error = ENOENT; | |
1351 | return 1; | |
1352 | } | |
1353 | if (create_file(td)) | |
1354 | return 1; | |
1355 | } | |
1356 | ||
1357 | if (td->odirect) | |
1358 | flags |= O_DIRECT; | |
1359 | ||
1360 | if (td_read(td)) | |
1361 | td->fd = open(td->file_name, flags | O_RDONLY); | |
1362 | else { | |
1363 | if (!td->overwrite) | |
1364 | flags |= O_TRUNC; | |
74b4b5fb JA |
1365 | if (td->sync_io) |
1366 | flags |= O_SYNC; | |
e8457004 JA |
1367 | if (td->verify) |
1368 | flags |= O_RDWR; | |
1369 | else | |
1370 | flags |= O_WRONLY; | |
02983297 | 1371 | |
e8457004 | 1372 | td->fd = open(td->file_name, flags | O_CREAT, 0600); |
02983297 JA |
1373 | } |
1374 | ||
1375 | if (td->fd == -1) { | |
1376 | td->error = errno; | |
1377 | return 1; | |
1378 | } | |
1379 | ||
1380 | if (td_read(td)) { | |
1381 | if (fstat(td->fd, &st) == -1) { | |
1382 | td->error = errno; | |
1383 | return 1; | |
1384 | } | |
1385 | ||
1386 | if (td->file_size > st.st_size) | |
1387 | st.st_size = td->file_size; | |
1388 | } else { | |
1389 | if (!td->file_size) | |
1390 | td->file_size = 1024 * 1024 * 1024; | |
1391 | ||
1392 | st.st_size = td->file_size; | |
1393 | } | |
1394 | ||
7889f07b JA |
1395 | td->kb = (st.st_size - td->file_offset) / 1024; |
1396 | if (!td->kb) { | |
02983297 JA |
1397 | fprintf(stderr, "Client%d: no io blocks\n", td->thread_number); |
1398 | td->error = EINVAL; | |
1399 | return 1; | |
1400 | } | |
1401 | ||
b95799ca JA |
1402 | if (td->invalidate_cache) { |
1403 | if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) { | |
1404 | td->error = errno; | |
1405 | return 1; | |
1406 | } | |
1407 | } | |
1408 | ||
02983297 JA |
1409 | return 0; |
1410 | } | |
1411 | ||
5c24b2c4 | 1412 | static void *thread_main(int shm_id, int offset, char *argv[]) |
892199bd JA |
1413 | { |
1414 | struct thread_data *td; | |
02983297 | 1415 | int ret = 1; |
2c83567e | 1416 | void *data; |
892199bd | 1417 | |
7292613b JA |
1418 | setsid(); |
1419 | ||
892199bd | 1420 | data = shmat(shm_id, NULL, 0); |
4ac89145 JA |
1421 | if (data == (void *) -1) { |
1422 | perror("shmat"); | |
1423 | return NULL; | |
1424 | } | |
1425 | ||
892199bd JA |
1426 | td = data + offset * sizeof(struct thread_data); |
1427 | td->pid = getpid(); | |
1428 | ||
99c6704f JA |
1429 | if (init_io_u(td)) |
1430 | goto err; | |
2c83567e | 1431 | |
18e0b78c JA |
1432 | if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) { |
1433 | td->error = errno; | |
1434 | goto err; | |
1435 | } | |
1436 | ||
4240cfa1 | 1437 | sprintf(argv[0], "fio%d", offset); |
892199bd | 1438 | |
43000118 JA |
1439 | if (td->use_aio && init_aio(td)) |
1440 | goto err; | |
1441 | ||
892199bd | 1442 | if (init_random_state(td)) |
599002b3 | 1443 | goto err; |
892199bd | 1444 | |
f737299d | 1445 | if (td->ioprio) { |
892199bd JA |
1446 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
1447 | td->error = errno; | |
599002b3 | 1448 | goto err; |
892199bd JA |
1449 | } |
1450 | } | |
1451 | ||
1452 | sem_post(&startup_sem); | |
1453 | sem_wait(&td->mutex); | |
43000118 | 1454 | |
fc097bfe JA |
1455 | if (!td->create_serialize && setup_file(td)) |
1456 | goto err; | |
1457 | ||
7292613b JA |
1458 | gettimeofday(&td->start, NULL); |
1459 | ||
293753bb JA |
1460 | while (td->loops--) { |
1461 | gettimeofday(&td->stat_sample_time, NULL); | |
1462 | ||
1463 | if (td->ratemin) | |
1464 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
7292613b | 1465 | |
293753bb JA |
1466 | td->cur_off = 0; |
1467 | td->last_kb = 0; | |
1468 | td->stat_io_kb = 0; | |
fd1ae4c9 | 1469 | |
cfc702bd | 1470 | if (!td->use_aio) { |
b6794fbf | 1471 | do_sync_io(td); |
cfc702bd | 1472 | |
494bb9d4 | 1473 | if (td->verify && !td_read(td)) { |
f8e3d840 | 1474 | if (!do_sync_verify(td)) |
cfc702bd JA |
1475 | break; |
1476 | } | |
1477 | } else { | |
b6794fbf | 1478 | do_async_io(td); |
cfc702bd | 1479 | |
494bb9d4 | 1480 | if (td->verify && !td_read(td)) { |
f8e3d840 | 1481 | if (!do_async_verify(td)) |
cfc702bd JA |
1482 | break; |
1483 | } | |
1484 | } | |
b6794fbf | 1485 | } |
7292613b | 1486 | |
be33abe4 | 1487 | td->runtime = mtime_since_now(&td->start); |
892199bd | 1488 | ret = 0; |
a0a9b35b JA |
1489 | |
1490 | if (td->bw_log) | |
1491 | finish_log(td, td->bw_log, "bw"); | |
1492 | if (td->lat_log) | |
1493 | finish_log(td, td->lat_log, "lat"); | |
4ac89145 | 1494 | |
98dd52d6 JA |
1495 | if (exitall_on_terminate) |
1496 | sig_handler(0); | |
1497 | ||
892199bd | 1498 | err: |
7292613b JA |
1499 | if (td->fd != -1) { |
1500 | close(td->fd); | |
1501 | td->fd = -1; | |
1502 | } | |
4ac89145 JA |
1503 | if (td->use_aio) |
1504 | cleanup_aio(td); | |
2c83567e | 1505 | cleanup_io_u(td); |
599002b3 | 1506 | if (ret) { |
892199bd | 1507 | sem_post(&startup_sem); |
599002b3 JA |
1508 | sem_wait(&td->mutex); |
1509 | } | |
40ef7f64 | 1510 | td_set_runstate(td, TD_EXITED); |
4240cfa1 | 1511 | shmdt(data); |
892199bd JA |
1512 | return NULL; |
1513 | } | |
1514 | ||
5c24b2c4 | 1515 | static void free_shm(void) |
892199bd | 1516 | { |
c269123b JA |
1517 | struct shmid_ds sbuf; |
1518 | ||
1519 | if (threads) { | |
1520 | shmdt(threads); | |
1521 | threads = NULL; | |
1522 | shmctl(shm_id, IPC_RMID, &sbuf); | |
1523 | } | |
892199bd JA |
1524 | } |
1525 | ||
57d753e3 JA |
1526 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, |
1527 | double *mean, double *dev) | |
1528 | { | |
1529 | double n; | |
1530 | ||
1531 | if (is->samples == 0) | |
1532 | return 0; | |
1533 | ||
1534 | *min = is->min_val; | |
1535 | *max = is->max_val; | |
1536 | ||
1537 | n = (double) is->samples; | |
1538 | *mean = (double) is->val / n; | |
1539 | *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1)); | |
1540 | return 1; | |
1541 | } | |
1542 | ||
5c24b2c4 | 1543 | static void show_thread_status(struct thread_data *td) |
892199bd JA |
1544 | { |
1545 | int prio, prio_class; | |
57d753e3 JA |
1546 | unsigned long min, max, bw = 0; |
1547 | double mean, dev; | |
892199bd | 1548 | |
8c033f93 | 1549 | if (!td->io_kb && !td->error) |
213b446c JA |
1550 | return; |
1551 | ||
892199bd | 1552 | if (td->runtime) |
7889f07b | 1553 | bw = td->io_kb * 1024 / td->runtime; |
892199bd JA |
1554 | |
1555 | prio = td->ioprio & 0xff; | |
1556 | prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT; | |
1557 | ||
396fe7cd | 1558 | printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->error, td->io_kb >> 10, bw, td->runtime); |
fd1ae4c9 | 1559 | |
57d753e3 JA |
1560 | if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev)) |
1561 | printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1562 | if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev)) | |
1563 | printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1564 | if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) | |
1565 | printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
892199bd JA |
1566 | } |
1567 | ||
5c24b2c4 | 1568 | static int setup_rate(struct thread_data *td) |
86184d14 | 1569 | { |
4240cfa1 JA |
1570 | int nr_reads_per_sec; |
1571 | ||
1572 | if (!td->rate) | |
1573 | return 0; | |
1574 | ||
1575 | if (td->rate < td->ratemin) { | |
1576 | fprintf(stderr, "min rate larger than nominal rate\n"); | |
1577 | return -1; | |
1578 | } | |
86184d14 | 1579 | |
7889f07b | 1580 | nr_reads_per_sec = td->rate * 1024 / td->min_bs; |
86184d14 JA |
1581 | td->rate_usec_cycle = 1000000 / nr_reads_per_sec; |
1582 | td->rate_pending_usleep = 0; | |
4240cfa1 | 1583 | return 0; |
86184d14 JA |
1584 | } |
1585 | ||
47d45203 | 1586 | static struct thread_data *get_new_job(int global) |
892199bd | 1587 | { |
4240cfa1 JA |
1588 | struct thread_data *td; |
1589 | ||
47d45203 JA |
1590 | if (global) |
1591 | return &def_thread; | |
8867c0a8 | 1592 | if (thread_number >= max_jobs) |
4240cfa1 JA |
1593 | return NULL; |
1594 | ||
1595 | td = &threads[thread_number++]; | |
fc24389f | 1596 | memset(td, 0, sizeof(*td)); |
892199bd | 1597 | |
e4ed35c3 | 1598 | td->fd = -1; |
86184d14 | 1599 | td->thread_number = thread_number; |
76cb7b42 | 1600 | |
47d45203 | 1601 | td->ddir = def_thread.ddir; |
76cb7b42 JA |
1602 | td->ioprio = def_thread.ioprio; |
1603 | td->sequential = def_thread.sequential; | |
47d45203 | 1604 | td->bs = def_thread.bs; |
8c033f93 JA |
1605 | td->min_bs = def_thread.min_bs; |
1606 | td->max_bs = def_thread.max_bs; | |
47d45203 | 1607 | td->odirect = def_thread.odirect; |
e97712ed | 1608 | td->thinktime = def_thread.thinktime; |
76cb7b42 JA |
1609 | td->fsync_blocks = def_thread.fsync_blocks; |
1610 | td->start_delay = def_thread.start_delay; | |
67903a2e | 1611 | td->timeout = def_thread.timeout; |
76cb7b42 | 1612 | td->use_aio = def_thread.use_aio; |
02983297 JA |
1613 | td->create_file = def_thread.create_file; |
1614 | td->overwrite = def_thread.overwrite; | |
b95799ca | 1615 | td->invalidate_cache = def_thread.invalidate_cache; |
76cb7b42 | 1616 | td->file_size = def_thread.file_size; |
9b5cf6c0 | 1617 | td->file_offset = def_thread.file_offset; |
76cb7b42 JA |
1618 | td->rate = def_thread.rate; |
1619 | td->ratemin = def_thread.ratemin; | |
1620 | td->ratecycle = def_thread.ratecycle; | |
1621 | td->aio_depth = def_thread.aio_depth; | |
99c6704f JA |
1622 | td->sync_io = def_thread.sync_io; |
1623 | td->mem_type = def_thread.mem_type; | |
1d035750 | 1624 | td->bw_avg_time = def_thread.bw_avg_time; |
fc097bfe JA |
1625 | td->create_serialize = def_thread.create_serialize; |
1626 | td->create_fsync = def_thread.create_fsync; | |
b6794fbf | 1627 | td->loops = def_thread.loops; |
cfc702bd | 1628 | td->verify = def_thread.verify; |
47d45203 | 1629 | memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask)); |
f737299d JA |
1630 | |
1631 | return td; | |
1632 | } | |
1633 | ||
4240cfa1 JA |
1634 | static void put_job(struct thread_data *td) |
1635 | { | |
1636 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); | |
1637 | thread_number--; | |
1638 | } | |
1639 | ||
5c24b2c4 JA |
1640 | static int add_job(struct thread_data *td, const char *filename, int prioclass, |
1641 | int prio) | |
f737299d | 1642 | { |
47d45203 JA |
1643 | if (td == &def_thread) |
1644 | return 0; | |
1645 | ||
f737299d | 1646 | strcpy(td->file_name, filename); |
4240cfa1 | 1647 | sem_init(&td->mutex, 1, 0); |
f737299d JA |
1648 | td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio; |
1649 | ||
57d753e3 JA |
1650 | td->clat_stat.min_val = ULONG_MAX; |
1651 | td->slat_stat.min_val = ULONG_MAX; | |
1652 | td->bw_stat.min_val = ULONG_MAX; | |
1653 | ||
8dbff0b1 JA |
1654 | run_str[td->thread_number - 1] = 'P'; |
1655 | ||
4ac89145 JA |
1656 | if (td->use_aio && !td->aio_depth) |
1657 | td->aio_depth = 1; | |
43000118 | 1658 | |
8bdcfab5 JA |
1659 | if (td->min_bs == -1) |
1660 | td->min_bs = td->bs; | |
1661 | if (td->max_bs == -1) | |
1662 | td->max_bs = td->bs; | |
1663 | ||
4240cfa1 JA |
1664 | if (setup_rate(td)) |
1665 | return -1; | |
f737299d | 1666 | |
a0a9b35b JA |
1667 | if (write_lat_log) |
1668 | setup_log(&td->lat_log); | |
1669 | if (write_bw_log) | |
1670 | setup_log(&td->bw_log); | |
1671 | ||
7889f07b | 1672 | printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d-%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->min_bs, td->max_bs, td->rate, td->use_aio, td->aio_depth); |
4240cfa1 | 1673 | return 0; |
892199bd JA |
1674 | } |
1675 | ||
18e0b78c JA |
1676 | static void fill_cpu_mask(cpu_set_t cpumask, int cpu) |
1677 | { | |
f737299d | 1678 | unsigned int i; |
18e0b78c JA |
1679 | |
1680 | CPU_ZERO(&cpumask); | |
1681 | ||
1682 | for (i = 0; i < sizeof(int) * 8; i++) { | |
1683 | if ((1 << i) & cpu) | |
1684 | CPU_SET(i, &cpumask); | |
1685 | } | |
1686 | } | |
1687 | ||
7889f07b JA |
1688 | unsigned long get_mult(char c) |
1689 | { | |
1690 | switch (c) { | |
1691 | case 'k': | |
1692 | case 'K': | |
1693 | return 1024; | |
1694 | case 'm': | |
1695 | case 'M': | |
1696 | return 1024 * 1024; | |
1697 | case 'g': | |
1698 | case 'G': | |
1699 | return 1024 * 1024 * 1024; | |
1700 | default: | |
1701 | return 1; | |
1702 | } | |
1703 | } | |
1704 | ||
02983297 JA |
1705 | /* |
1706 | * convert string after '=' into decimal value, noting any size suffix | |
1707 | */ | |
1708 | static int str_cnv(char *p, unsigned long long *val) | |
1709 | { | |
02983297 JA |
1710 | char *str; |
1711 | int len; | |
1712 | ||
1713 | str = strstr(p, "="); | |
1714 | if (!str) | |
1715 | return 1; | |
1716 | ||
1717 | str++; | |
1718 | len = strlen(str); | |
02983297 JA |
1719 | |
1720 | *val = strtoul(str, NULL, 10); | |
1721 | if (*val == ULONG_MAX && errno == ERANGE) | |
1722 | return 1; | |
1723 | ||
7889f07b | 1724 | *val *= get_mult(str[len - 2]); |
02983297 | 1725 | return 0; |
02983297 JA |
1726 | } |
1727 | ||
02983297 JA |
1728 | static int check_strcnv(char *p, char *name, unsigned long long *val) |
1729 | { | |
1730 | if (!strstr(p, name)) | |
1731 | return 1; | |
1732 | ||
1733 | return str_cnv(p, val); | |
1734 | } | |
1735 | ||
99c6704f JA |
1736 | static int check_str(char *p, char *name, char *option) |
1737 | { | |
1738 | char *s = strstr(p, name); | |
1739 | ||
1740 | if (!s) | |
1741 | return 1; | |
1742 | ||
1743 | s += strlen(name); | |
1744 | if (strstr(s, option)) | |
1745 | return 0; | |
1746 | ||
1747 | return 1; | |
1748 | } | |
1749 | ||
7889f07b JA |
1750 | static int check_range(char *p, char *name, unsigned long *s, unsigned long *e) |
1751 | { | |
1752 | char str[128]; | |
1753 | char s1, s2; | |
1754 | ||
1755 | sprintf(str, "%s=%%lu%%c-%%lu%%c", name); | |
1756 | if (sscanf(p, str, s, &s1, e, &s2) == 4) { | |
1757 | *s *= get_mult(s1); | |
1758 | *e *= get_mult(s2); | |
1759 | return 0; | |
1760 | } | |
1761 | ||
1762 | sprintf(str, "%s = %%lu%%c-%%lu%%c", name); | |
1763 | if (sscanf(p, str, s, &s1, e, &s2) == 4) { | |
1764 | *s *= get_mult(s1); | |
1765 | *e *= get_mult(s2); | |
1766 | return 0; | |
1767 | } | |
1768 | ||
1769 | sprintf(str, "%s=%%lu-%%lu", name); | |
1770 | if (sscanf(p, str, s, e) == 2) | |
1771 | return 0; | |
1772 | ||
1773 | sprintf(str, "%s = %%lu-%%lu", name); | |
1774 | if (sscanf(p, str, s, e) == 2) | |
1775 | return 0; | |
1776 | ||
1777 | return 1; | |
1778 | ||
1779 | } | |
1780 | ||
5c24b2c4 | 1781 | static int check_int(char *p, char *name, unsigned int *val) |
7dd1389e JA |
1782 | { |
1783 | char str[128]; | |
1784 | ||
1785 | sprintf(str, "%s=%%d", name); | |
1786 | if (sscanf(p, str, val) == 1) | |
1787 | return 0; | |
1788 | ||
1789 | sprintf(str, "%s = %%d", name); | |
1790 | if (sscanf(p, str, val) == 1) | |
1791 | return 0; | |
1792 | ||
1793 | return 1; | |
1794 | } | |
1795 | ||
7292613b | 1796 | static int is_empty_or_comment(char *line) |
7dd1389e JA |
1797 | { |
1798 | unsigned int i; | |
1799 | ||
7292613b | 1800 | for (i = 0; i < strlen(line); i++) { |
7292613b | 1801 | if (line[i] == ';') |
47d45203 JA |
1802 | return 1; |
1803 | if (!isspace(line[i]) && !iscntrl(line[i])) | |
7292613b JA |
1804 | return 0; |
1805 | } | |
7dd1389e JA |
1806 | |
1807 | return 1; | |
1808 | } | |
1809 | ||
5c24b2c4 | 1810 | static int parse_jobs_ini(char *file) |
7dd1389e | 1811 | { |
47d45203 | 1812 | unsigned int prioclass, prio, cpu, global; |
8c033f93 | 1813 | unsigned long long ull; |
7889f07b | 1814 | unsigned long ul1, ul2; |
f737299d | 1815 | struct thread_data *td; |
7dd1389e JA |
1816 | char *string, *name; |
1817 | fpos_t off; | |
1818 | FILE *f; | |
1819 | char *p; | |
1820 | ||
1821 | f = fopen(file, "r"); | |
1822 | if (!f) { | |
1823 | perror("fopen"); | |
4240cfa1 | 1824 | return 1; |
7dd1389e JA |
1825 | } |
1826 | ||
1827 | string = malloc(4096); | |
1828 | name = malloc(256); | |
1829 | ||
7dd1389e | 1830 | while ((p = fgets(string, 4096, f)) != NULL) { |
7292613b JA |
1831 | if (is_empty_or_comment(p)) |
1832 | continue; | |
7dd1389e JA |
1833 | if (sscanf(p, "[%s]", name) != 1) |
1834 | continue; | |
1835 | ||
47d45203 JA |
1836 | global = !strncmp(name, "global", 6); |
1837 | ||
7dd1389e JA |
1838 | name[strlen(name) - 1] = '\0'; |
1839 | ||
47d45203 | 1840 | td = get_new_job(global); |
4240cfa1 JA |
1841 | if (!td) |
1842 | break; | |
f737299d | 1843 | |
7dd1389e | 1844 | prioclass = 2; |
f737299d | 1845 | prio = 4; |
7dd1389e JA |
1846 | |
1847 | fgetpos(f, &off); | |
1848 | while ((p = fgets(string, 4096, f)) != NULL) { | |
7292613b | 1849 | if (is_empty_or_comment(p)) |
e6402082 JA |
1850 | continue; |
1851 | if (strstr(p, "[")) | |
7dd1389e | 1852 | break; |
f737299d | 1853 | if (!check_int(p, "rw", &td->ddir)) { |
7dd1389e JA |
1854 | fgetpos(f, &off); |
1855 | continue; | |
1856 | } | |
1857 | if (!check_int(p, "prio", &prio)) { | |
1858 | fgetpos(f, &off); | |
1859 | continue; | |
1860 | } | |
1861 | if (!check_int(p, "prioclass", &prioclass)) { | |
1862 | fgetpos(f, &off); | |
1863 | continue; | |
1864 | } | |
f737299d | 1865 | if (!check_int(p, "direct", &td->odirect)) { |
7dd1389e JA |
1866 | fgetpos(f, &off); |
1867 | continue; | |
1868 | } | |
f737299d | 1869 | if (!check_int(p, "rate", &td->rate)) { |
7dd1389e JA |
1870 | fgetpos(f, &off); |
1871 | continue; | |
1872 | } | |
4240cfa1 JA |
1873 | if (!check_int(p, "ratemin", &td->ratemin)) { |
1874 | fgetpos(f, &off); | |
1875 | continue; | |
1876 | } | |
1877 | if (!check_int(p, "ratecycle", &td->ratecycle)) { | |
1878 | fgetpos(f, &off); | |
1879 | continue; | |
1880 | } | |
e97712ed | 1881 | if (!check_int(p, "thinktime", &td->thinktime)) { |
7dd1389e JA |
1882 | fgetpos(f, &off); |
1883 | continue; | |
1884 | } | |
18e0b78c | 1885 | if (!check_int(p, "cpumask", &cpu)) { |
f737299d | 1886 | fill_cpu_mask(td->cpumask, cpu); |
18e0b78c JA |
1887 | fgetpos(f, &off); |
1888 | continue; | |
1889 | } | |
4240cfa1 JA |
1890 | if (!check_int(p, "fsync", &td->fsync_blocks)) { |
1891 | fgetpos(f, &off); | |
1892 | continue; | |
1893 | } | |
fc24389f JA |
1894 | if (!check_int(p, "startdelay", &td->start_delay)) { |
1895 | fgetpos(f, &off); | |
1896 | continue; | |
1897 | } | |
67903a2e JA |
1898 | if (!check_int(p, "timeout", &td->timeout)) { |
1899 | fgetpos(f, &off); | |
1900 | continue; | |
1901 | } | |
b95799ca JA |
1902 | if (!check_int(p, "invalidate",&td->invalidate_cache)) { |
1903 | fgetpos(f, &off); | |
1904 | continue; | |
1905 | } | |
43000118 JA |
1906 | if (!check_int(p, "aio_depth", &td->aio_depth)) { |
1907 | fgetpos(f, &off); | |
1908 | continue; | |
1909 | } | |
74b4b5fb JA |
1910 | if (!check_int(p, "sync", &td->sync_io)) { |
1911 | fgetpos(f, &off); | |
1912 | continue; | |
1913 | } | |
1d035750 JA |
1914 | if (!check_int(p, "bwavgtime", &td->bw_avg_time)) { |
1915 | fgetpos(f, &off); | |
1916 | continue; | |
1917 | } | |
fc097bfe JA |
1918 | if (!check_int(p, "create_serialize", &td->create_serialize)) { |
1919 | fgetpos(f, &off); | |
1920 | continue; | |
1921 | } | |
1922 | if (!check_int(p, "create_fsync", &td->create_fsync)) { | |
1923 | fgetpos(f, &off); | |
1924 | continue; | |
1925 | } | |
b6794fbf JA |
1926 | if (!check_int(p, "loops", &td->loops)) { |
1927 | fgetpos(f, &off); | |
1928 | continue; | |
1929 | } | |
e8457004 JA |
1930 | if (!check_int(p, "verify", &td->verify)) { |
1931 | fgetpos(f, &off); | |
1932 | continue; | |
1933 | } | |
7889f07b JA |
1934 | if (!check_range(p, "bsrange", &ul1, &ul2)) { |
1935 | td->min_bs = ul1; | |
1936 | td->max_bs = ul2; | |
1937 | fgetpos(f, &off); | |
1938 | continue; | |
1939 | } | |
8c033f93 JA |
1940 | if (!check_strcnv(p, "bs", &ull)) { |
1941 | td->bs = ull; | |
1942 | fgetpos(f, &off); | |
1943 | continue; | |
1944 | } | |
02983297 JA |
1945 | if (!check_strcnv(p, "size", &td->file_size)) { |
1946 | fgetpos(f, &off); | |
1947 | continue; | |
1948 | } | |
1949 | if (!check_strcnv(p, "offset", &td->file_offset)) { | |
1950 | fgetpos(f, &off); | |
1951 | continue; | |
1952 | } | |
99c6704f | 1953 | if (!check_str(p, "mem", "malloc")) { |
99c6704f JA |
1954 | td->mem_type = MEM_MALLOC; |
1955 | fgetpos(f, &off); | |
1956 | continue; | |
1957 | } | |
1958 | if (!check_str(p, "mem", "shm")) { | |
99c6704f JA |
1959 | td->mem_type = MEM_SHM; |
1960 | fgetpos(f, &off); | |
1961 | continue; | |
1962 | } | |
43000118 | 1963 | if (!strncmp(p, "sequential", 10)) { |
f737299d | 1964 | td->sequential = 1; |
7dd1389e JA |
1965 | fgetpos(f, &off); |
1966 | continue; | |
1967 | } | |
43000118 | 1968 | if (!strncmp(p, "random", 6)) { |
f737299d | 1969 | td->sequential = 0; |
7dd1389e JA |
1970 | fgetpos(f, &off); |
1971 | continue; | |
1972 | } | |
43000118 JA |
1973 | if (!strncmp(p, "aio", 3)) { |
1974 | td->use_aio = 1; | |
1975 | fgetpos(f, &off); | |
1976 | continue; | |
1977 | } | |
02983297 JA |
1978 | if (!strncmp(p, "create", 6)) { |
1979 | td->create_file = 1; | |
1980 | fgetpos(f, &off); | |
1981 | continue; | |
1982 | } | |
1983 | if (!strncmp(p, "overwrite", 9)) { | |
1984 | td->overwrite = 1; | |
1985 | fgetpos(f, &off); | |
1986 | continue; | |
1987 | } | |
98dd52d6 JA |
1988 | if (!strncmp(p, "exitall", 7)) { |
1989 | exitall_on_terminate = 1; | |
1990 | fgetpos(f, &off); | |
1991 | continue; | |
1992 | } | |
e6402082 | 1993 | printf("Client%d: bad option %s\n",td->thread_number,p); |
7dd1389e JA |
1994 | } |
1995 | fsetpos(f, &off); | |
1996 | ||
4240cfa1 JA |
1997 | if (add_job(td, name, prioclass, prio)) |
1998 | put_job(td); | |
7dd1389e JA |
1999 | } |
2000 | ||
2001 | free(string); | |
2002 | free(name); | |
fc7d63df | 2003 | fclose(f); |
4240cfa1 | 2004 | return 0; |
7dd1389e JA |
2005 | } |
2006 | ||
5c24b2c4 | 2007 | static int parse_options(int argc, char *argv[]) |
892199bd | 2008 | { |
01c4d8de | 2009 | int i; |
892199bd JA |
2010 | |
2011 | for (i = 1; i < argc; i++) { | |
2012 | char *parm = argv[i]; | |
2013 | ||
2014 | if (parm[0] != '-') | |
2015 | break; | |
2016 | ||
2017 | parm++; | |
2018 | switch (*parm) { | |
2019 | case 's': | |
2020 | parm++; | |
47d45203 | 2021 | def_thread.sequential = !!atoi(parm); |
892199bd JA |
2022 | break; |
2023 | case 'b': | |
2024 | parm++; | |
47d45203 JA |
2025 | def_thread.bs = atoi(parm); |
2026 | def_thread.bs <<= 10; | |
2027 | if (!def_thread.bs) { | |
4240cfa1 | 2028 | printf("bad block size\n"); |
47d45203 | 2029 | def_thread.bs = DEF_BS; |
4240cfa1 | 2030 | } |
892199bd JA |
2031 | break; |
2032 | case 't': | |
2033 | parm++; | |
47d45203 | 2034 | def_thread.timeout = atoi(parm); |
892199bd | 2035 | break; |
892199bd JA |
2036 | case 'r': |
2037 | parm++; | |
2038 | repeatable = !!atoi(parm); | |
2039 | break; | |
02bdd9ba JA |
2040 | case 'R': |
2041 | parm++; | |
2042 | rate_quit = !!atoi(parm); | |
2043 | break; | |
892199bd JA |
2044 | case 'o': |
2045 | parm++; | |
47d45203 | 2046 | def_thread.odirect = !!atoi(parm); |
892199bd | 2047 | break; |
7dd1389e JA |
2048 | case 'f': |
2049 | if (i + 1 >= argc) { | |
2050 | printf("-f needs file as arg\n"); | |
2051 | break; | |
2052 | } | |
2053 | ini_file = strdup(argv[i+1]); | |
a642279f | 2054 | i++; |
7dd1389e | 2055 | break; |
a0a9b35b JA |
2056 | case 'l': |
2057 | write_lat_log = 1; | |
2058 | break; | |
2059 | case 'w': | |
2060 | write_bw_log = 1; | |
2061 | break; | |
892199bd | 2062 | default: |
7dd1389e | 2063 | printf("bad option %s\n", argv[i]); |
892199bd JA |
2064 | break; |
2065 | } | |
2066 | } | |
2067 | ||
892199bd JA |
2068 | return i; |
2069 | } | |
2070 | ||
3f39453a | 2071 | static void print_thread_status(struct thread_data *td, int nr_running, |
8dbff0b1 | 2072 | int t_rate, int m_rate) |
3f39453a | 2073 | { |
3f39453a JA |
2074 | printf("Threads now running: %d", nr_running); |
2075 | if (m_rate || t_rate) | |
2076 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); | |
8dbff0b1 JA |
2077 | printf(" : [%s]\r", run_str); |
2078 | fflush(stdout); | |
3f39453a JA |
2079 | } |
2080 | ||
40ef7f64 JA |
2081 | static void check_str_update(struct thread_data *td, int n, int t, int m) |
2082 | { | |
2083 | char c = run_str[td->thread_number - 1]; | |
2084 | ||
2085 | if (td->runstate == td->old_runstate) | |
2086 | return; | |
2087 | ||
2088 | switch (td->runstate) { | |
2089 | case TD_REAPED: | |
2090 | c = '_'; | |
2091 | break; | |
f4bb2243 JA |
2092 | case TD_EXITED: |
2093 | c = 'E'; | |
2094 | break; | |
40ef7f64 JA |
2095 | case TD_RUNNING: |
2096 | c = '+'; | |
2097 | break; | |
2098 | case TD_VERIFYING: | |
2099 | c = 'V'; | |
2100 | break; | |
2101 | case TD_CREATED: | |
2102 | c = 'C'; | |
2103 | break; | |
2104 | case TD_NOT_CREATED: | |
2105 | c = 'P'; | |
2106 | break; | |
2107 | default: | |
2108 | printf("state %d\n", td->runstate); | |
2109 | } | |
2110 | ||
2111 | run_str[td->thread_number - 1] = c; | |
2112 | print_thread_status(td, n, t, m); | |
2113 | td->old_runstate = td->runstate; | |
2114 | } | |
2115 | ||
213b446c | 2116 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
02bdd9ba | 2117 | { |
213b446c | 2118 | int i; |
02bdd9ba | 2119 | |
3f39453a JA |
2120 | /* |
2121 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
2122 | */ | |
02bdd9ba JA |
2123 | for (i = 0; i < thread_number; i++) { |
2124 | struct thread_data *td = &threads[i]; | |
2125 | ||
40ef7f64 JA |
2126 | check_str_update(td, *nr_running, *t_rate, *m_rate); |
2127 | ||
213b446c JA |
2128 | if (td->runstate != TD_EXITED) |
2129 | continue; | |
02bdd9ba | 2130 | |
40ef7f64 | 2131 | td_set_runstate(td, TD_REAPED); |
213b446c JA |
2132 | waitpid(td->pid, NULL, 0); |
2133 | (*nr_running)--; | |
2134 | (*m_rate) -= td->ratemin; | |
2135 | (*t_rate) -= td->rate; | |
40ef7f64 | 2136 | check_str_update(td, *nr_running, *t_rate, *m_rate); |
e6402082 JA |
2137 | |
2138 | if (td->terminate) | |
2139 | continue; | |
213b446c | 2140 | } |
02bdd9ba JA |
2141 | } |
2142 | ||
fc24389f JA |
2143 | static void run_threads(char *argv[]) |
2144 | { | |
be33abe4 | 2145 | struct timeval genesis; |
fc24389f JA |
2146 | struct thread_data *td; |
2147 | unsigned long spent; | |
213b446c | 2148 | int i, todo, nr_running, m_rate, t_rate; |
fc24389f | 2149 | |
fc24389f JA |
2150 | printf("Starting %d threads\n", thread_number); |
2151 | fflush(stdout); | |
2152 | ||
7292613b JA |
2153 | signal(SIGINT, sig_handler); |
2154 | ||
fc24389f | 2155 | todo = thread_number; |
02bdd9ba | 2156 | nr_running = 0; |
213b446c | 2157 | m_rate = t_rate = 0; |
fc24389f | 2158 | |
8bdcfab5 JA |
2159 | for (i = 0; i < thread_number; i++) { |
2160 | td = &threads[i]; | |
2161 | ||
fc097bfe JA |
2162 | if (!td->create_serialize) |
2163 | continue; | |
2164 | ||
8bdcfab5 JA |
2165 | /* |
2166 | * do file setup here so it happens sequentially, | |
2167 | * we don't want X number of threads getting their | |
2168 | * client data interspersed on disk | |
2169 | */ | |
2170 | if (setup_file(td)) { | |
40ef7f64 | 2171 | td_set_runstate(td, TD_REAPED); |
8bdcfab5 JA |
2172 | todo--; |
2173 | } | |
2174 | } | |
2175 | ||
2176 | gettimeofday(&genesis, NULL); | |
2177 | ||
213b446c | 2178 | while (todo) { |
3f39453a JA |
2179 | /* |
2180 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
2181 | */ | |
fc24389f JA |
2182 | for (i = 0; i < thread_number; i++) { |
2183 | td = &threads[i]; | |
2184 | ||
02bdd9ba | 2185 | if (td->runstate != TD_NOT_CREATED) |
fc24389f JA |
2186 | continue; |
2187 | ||
213b446c JA |
2188 | /* |
2189 | * never got a chance to start, killed by other | |
2190 | * thread for some reason | |
2191 | */ | |
2192 | if (td->terminate) { | |
2193 | todo--; | |
2194 | continue; | |
2195 | } | |
2196 | ||
fc24389f | 2197 | if (td->start_delay) { |
be33abe4 | 2198 | spent = mtime_since_now(&genesis); |
fc24389f JA |
2199 | |
2200 | if (td->start_delay * 1000 > spent) | |
2201 | continue; | |
2202 | } | |
2203 | ||
40ef7f64 JA |
2204 | td_set_runstate(td, TD_CREATED); |
2205 | check_str_update(td, nr_running, t_rate, m_rate); | |
fc24389f JA |
2206 | sem_init(&startup_sem, 1, 1); |
2207 | todo--; | |
2208 | ||
2209 | if (fork()) | |
2210 | sem_wait(&startup_sem); | |
2211 | else { | |
2212 | thread_main(shm_id, i, argv); | |
2213 | exit(0); | |
2214 | } | |
2215 | } | |
2216 | ||
3f39453a | 2217 | /* |
e8457004 | 2218 | * start created threads (TD_CREATED -> TD_RUNNING) |
3f39453a | 2219 | */ |
fc24389f JA |
2220 | for (i = 0; i < thread_number; i++) { |
2221 | struct thread_data *td = &threads[i]; | |
2222 | ||
3f39453a JA |
2223 | if (td->runstate != TD_CREATED) |
2224 | continue; | |
2225 | ||
40ef7f64 | 2226 | td_set_runstate(td, TD_RUNNING); |
3f39453a JA |
2227 | nr_running++; |
2228 | m_rate += td->ratemin; | |
2229 | t_rate += td->rate; | |
40ef7f64 | 2230 | check_str_update(td, nr_running, t_rate, m_rate); |
3f39453a | 2231 | sem_post(&td->mutex); |
fc24389f JA |
2232 | } |
2233 | ||
e8457004 JA |
2234 | for (i = 0; i < thread_number; i++) { |
2235 | struct thread_data *td = &threads[i]; | |
2236 | ||
2237 | if (td->runstate == TD_RUNNING) | |
2238 | run_str[td->thread_number - 1] = '+'; | |
2239 | else if (td->runstate == TD_VERIFYING) | |
2240 | run_str[td->thread_number - 1] = 'V'; | |
2241 | else | |
2242 | continue; | |
2243 | ||
40ef7f64 | 2244 | check_str_update(td, nr_running, t_rate, m_rate); |
e8457004 JA |
2245 | } |
2246 | ||
213b446c | 2247 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba | 2248 | |
fc24389f JA |
2249 | if (todo) |
2250 | usleep(100000); | |
2251 | } | |
02bdd9ba JA |
2252 | |
2253 | while (nr_running) { | |
213b446c | 2254 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba JA |
2255 | usleep(10000); |
2256 | } | |
fc24389f JA |
2257 | } |
2258 | ||
8867c0a8 | 2259 | int setup_thread_area(void) |
892199bd | 2260 | { |
8867c0a8 JA |
2261 | /* |
2262 | * 1024 is too much on some machines, scale max_jobs if | |
2263 | * we get a failure that looks like too large a shm segment | |
2264 | */ | |
2265 | do { | |
2266 | int s = max_jobs * sizeof(struct thread_data); | |
18e0b78c | 2267 | |
8867c0a8 JA |
2268 | shm_id = shmget(0, s, IPC_CREAT | 0600); |
2269 | if (shm_id != -1) | |
2270 | break; | |
2271 | if (errno != EINVAL) { | |
2272 | perror("shmget"); | |
2273 | break; | |
2274 | } | |
2275 | ||
2276 | max_jobs >>= 1; | |
d4fac444 | 2277 | } while (max_jobs); |
8867c0a8 JA |
2278 | |
2279 | if (shm_id == -1) | |
892199bd | 2280 | return 1; |
892199bd JA |
2281 | |
2282 | threads = shmat(shm_id, NULL, 0); | |
8867c0a8 | 2283 | if (threads == (void *) -1) { |
86184d14 JA |
2284 | perror("shmat"); |
2285 | return 1; | |
2286 | } | |
892199bd JA |
2287 | |
2288 | atexit(free_shm); | |
8867c0a8 JA |
2289 | return 0; |
2290 | } | |
2291 | ||
2292 | int main(int argc, char *argv[]) | |
2293 | { | |
8c033f93 | 2294 | static unsigned long max_run[2], min_run[2]; |
57d753e3 | 2295 | static unsigned long max_bw[2], min_bw[2]; |
22334044 | 2296 | static unsigned long io_mb[2], agg[2]; |
8867c0a8 JA |
2297 | int i; |
2298 | ||
2299 | if (setup_thread_area()) | |
2300 | return 1; | |
892199bd | 2301 | |
47d45203 | 2302 | if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) { |
4240cfa1 JA |
2303 | perror("sched_getaffinity"); |
2304 | return 1; | |
2305 | } | |
2306 | ||
47d45203 JA |
2307 | /* |
2308 | * fill globals | |
2309 | */ | |
2310 | def_thread.ddir = DDIR_READ; | |
2311 | def_thread.bs = DEF_BS; | |
7889f07b JA |
2312 | def_thread.min_bs = -1; |
2313 | def_thread.max_bs = -1; | |
02983297 | 2314 | def_thread.odirect = DEF_ODIRECT; |
47d45203 | 2315 | def_thread.ratecycle = DEF_RATE_CYCLE; |
02983297 | 2316 | def_thread.sequential = DEF_SEQUENTIAL; |
47d45203 | 2317 | def_thread.timeout = DEF_TIMEOUT; |
02983297 JA |
2318 | def_thread.create_file = DEF_CREATE; |
2319 | def_thread.overwrite = DEF_OVERWRITE; | |
b95799ca | 2320 | def_thread.invalidate_cache = DEF_INVALIDATE; |
99c6704f JA |
2321 | def_thread.sync_io = DEF_SYNCIO; |
2322 | def_thread.mem_type = MEM_MALLOC; | |
1d035750 | 2323 | def_thread.bw_avg_time = DEF_BWAVGTIME; |
fc097bfe JA |
2324 | def_thread.create_serialize = DEF_CREATE_SER; |
2325 | def_thread.create_fsync = DEF_CREATE_FSYNC; | |
b6794fbf | 2326 | def_thread.loops = DEF_LOOPS; |
cfc702bd | 2327 | def_thread.verify = DEF_VERIFY; |
47d45203 | 2328 | |
892199bd | 2329 | i = parse_options(argc, argv); |
7dd1389e | 2330 | |
5961d92c JA |
2331 | if (!ini_file) { |
2332 | printf("Need job file\n"); | |
2333 | return 1; | |
2334 | } | |
2335 | ||
2336 | if (parse_jobs_ini(ini_file)) | |
2337 | return 1; | |
7dd1389e | 2338 | |
4240cfa1 JA |
2339 | if (!thread_number) { |
2340 | printf("Nothing to do\n"); | |
2341 | return 1; | |
2342 | } | |
7dd1389e | 2343 | |
fc24389f | 2344 | run_threads(argv); |
892199bd | 2345 | |
892199bd JA |
2346 | min_bw[0] = min_run[0] = ~0UL; |
2347 | min_bw[1] = min_run[1] = ~0UL; | |
22334044 JA |
2348 | io_mb[0] = io_mb[1] = 0; |
2349 | agg[0] = agg[1] = 0; | |
892199bd JA |
2350 | for (i = 0; i < thread_number; i++) { |
2351 | struct thread_data *td = &threads[i]; | |
2352 | unsigned long bw = 0; | |
2353 | ||
22334044 JA |
2354 | if (!td->error) { |
2355 | if (td->runtime < min_run[td->ddir]) | |
2356 | min_run[td->ddir] = td->runtime; | |
2357 | if (td->runtime > max_run[td->ddir]) | |
2358 | max_run[td->ddir] = td->runtime; | |
892199bd | 2359 | |
892199bd | 2360 | if (td->runtime) |
22334044 JA |
2361 | bw = td->io_kb * 1024 / td->runtime; |
2362 | if (bw < min_bw[td->ddir]) | |
2363 | min_bw[td->ddir] = bw; | |
2364 | if (bw > max_bw[td->ddir]) | |
2365 | max_bw[td->ddir] = bw; | |
2366 | ||
2367 | io_mb[td->ddir] += td->io_kb >> 10; | |
892199bd JA |
2368 | } |
2369 | ||
2370 | show_thread_status(td); | |
2371 | } | |
22334044 JA |
2372 | |
2373 | if (max_run[0]) | |
2374 | agg[0] = io_mb[0] * 1024 * 1000 / max_run[0]; | |
2375 | if (max_run[1]) | |
2376 | agg[1] = io_mb[1] * 1024 * 1000 / max_run[1]; | |
2377 | ||
57d753e3 | 2378 | printf("\nRun status:\n"); |
892199bd | 2379 | if (max_run[DDIR_READ]) |
22334044 | 2380 | printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[0], agg[0], min_bw[0], max_bw[0], min_run[0], max_run[0]); |
892199bd | 2381 | if (max_run[DDIR_WRITE]) |
22334044 | 2382 | printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[1], agg[1], min_bw[1], max_bw[1], min_run[1], max_run[1]); |
fc24389f | 2383 | |
892199bd JA |
2384 | return 0; |
2385 | } |