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