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