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