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> |
892199bd JA |
32 | #include <sys/time.h> |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/wait.h> | |
36 | #include <semaphore.h> | |
37 | #include <sys/ipc.h> | |
38 | #include <sys/shm.h> | |
39 | #include <asm/unistd.h> | |
40 | ||
4240cfa1 JA |
41 | #define MAX_JOBS (1024) |
42 | ||
892199bd JA |
43 | /* |
44 | * assume we don't have _get either, if _set isn't defined | |
45 | */ | |
46 | #ifndef __NR_ioprio_set | |
47 | ||
48 | #if defined(__i386__) | |
49 | #define __NR_ioprio_set 289 | |
50 | #define __NR_ioprio_get 290 | |
51 | #elif defined(__powerpc__) || defined(__powerpc64__) | |
52 | #define __NR_ioprio_set 273 | |
53 | #define __NR_ioprio_get 274 | |
54 | #elif defined(__x86_64__) | |
55 | #define __NR_ioprio_set 251 | |
56 | #define __NR_ioprio_get 252 | |
57 | #elif defined(__ia64__) | |
58 | #define __NR_ioprio_set 1274 | |
59 | #define __NR_ioprio_get 1275 | |
60 | #elif defined(__alpha__) | |
61 | #define __NR_ioprio_set 442 | |
62 | #define __NR_ioprio_get 443 | |
63 | #elif defined(__s390x__) || defined(__s390__) | |
64 | #define __NR_ioprio_set 282 | |
65 | #define __NR_ioprio_get 283 | |
66 | #else | |
67 | #error "Unsupported arch" | |
68 | #endif | |
69 | ||
70 | #endif | |
71 | ||
72 | static int ioprio_set(int which, int who, int ioprio) | |
73 | { | |
74 | return syscall(__NR_ioprio_set, which, who, ioprio); | |
75 | } | |
76 | ||
77 | enum { | |
78 | IOPRIO_WHO_PROCESS = 1, | |
79 | IOPRIO_WHO_PGRP, | |
80 | IOPRIO_WHO_USER, | |
81 | }; | |
82 | ||
83 | #define IOPRIO_CLASS_SHIFT 13 | |
84 | ||
892199bd JA |
85 | #define MASK (4095) |
86 | ||
4240cfa1 JA |
87 | #define DEF_BS (4096) |
88 | #define DEF_TIMEOUT (30) | |
89 | #define DEF_RATE_CYCLE (1000) | |
90 | #define DEF_ODIRECT (1) | |
91 | #define DEF_SEQUENTIAL (1) | |
92 | #define DEF_WRITESTAT (0) | |
93 | #define DEF_RAND_REPEAT (1) | |
94 | ||
95 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
892199bd | 96 | |
4240cfa1 JA |
97 | static int write_stat = DEF_WRITESTAT; |
98 | static int repeatable = DEF_RAND_REPEAT; | |
02bdd9ba | 99 | static int rate_quit = 1; |
892199bd | 100 | |
892199bd | 101 | static int thread_number; |
7dd1389e | 102 | static char *ini_file; |
892199bd JA |
103 | |
104 | static int shm_id; | |
105 | ||
4240cfa1 JA |
106 | enum { |
107 | DDIR_READ = 0, | |
108 | DDIR_WRITE, | |
109 | }; | |
892199bd | 110 | |
02bdd9ba JA |
111 | /* |
112 | * thread life cycle | |
113 | */ | |
114 | enum { | |
115 | TD_NOT_CREATED = 0, | |
116 | TD_CREATED, | |
117 | TD_STARTED, | |
118 | TD_EXITED, | |
119 | TD_REAPED, | |
120 | }; | |
121 | ||
892199bd JA |
122 | struct thread_data { |
123 | char file_name[256]; | |
124 | int thread_number; | |
125 | int error; | |
126 | int fd; | |
127 | int stat_fd; | |
128 | pid_t pid; | |
7292613b | 129 | char *buf; |
4240cfa1 | 130 | volatile int terminate; |
02bdd9ba | 131 | volatile int runstate; |
f737299d JA |
132 | unsigned int ddir; |
133 | unsigned int ioprio; | |
134 | unsigned int sequential; | |
135 | unsigned int bs; | |
136 | unsigned int odirect; | |
137 | unsigned int delay_sleep; | |
4240cfa1 | 138 | unsigned int fsync_blocks; |
fc24389f | 139 | unsigned int start_delay; |
47d45203 | 140 | unsigned int timeout; |
43000118 | 141 | unsigned int use_aio; |
18e0b78c | 142 | cpu_set_t cpumask; |
86184d14 | 143 | |
43000118 JA |
144 | io_context_t *aio_ctx; |
145 | struct iocb *aio_iocbs; | |
146 | unsigned int aio_depth; | |
147 | unsigned int aio_cur_depth; | |
148 | struct io_event *aio_events; | |
149 | char *aio_iocbs_status; | |
150 | ||
7dd1389e | 151 | unsigned int rate; |
4240cfa1 JA |
152 | unsigned int ratemin; |
153 | unsigned int ratecycle; | |
154 | unsigned long rate_usec_cycle; | |
155 | long rate_pending_usleep; | |
156 | unsigned long rate_blocks; | |
157 | struct timeval lastrate; | |
86184d14 | 158 | |
892199bd JA |
159 | unsigned long max_latency; /* msec */ |
160 | unsigned long min_latency; /* msec */ | |
161 | unsigned long runtime; /* sec */ | |
162 | unsigned long blocks; | |
4240cfa1 | 163 | unsigned long io_blocks; |
892199bd JA |
164 | unsigned long last_block; |
165 | sem_t mutex; | |
892199bd JA |
166 | struct drand48_data random_state; |
167 | ||
168 | /* | |
169 | * bandwidth stat | |
170 | */ | |
171 | unsigned long stat_time; | |
172 | unsigned long stat_time_last; | |
173 | unsigned long stat_blocks_last; | |
4240cfa1 JA |
174 | |
175 | struct timeval start; | |
892199bd JA |
176 | }; |
177 | ||
178 | static struct thread_data *threads; | |
47d45203 | 179 | static struct thread_data def_thread; |
892199bd JA |
180 | |
181 | static sem_t startup_sem; | |
182 | ||
5c24b2c4 | 183 | static void sig_handler(int sig) |
892199bd JA |
184 | { |
185 | int i; | |
186 | ||
213b446c JA |
187 | for (i = 0; i < thread_number; i++) { |
188 | struct thread_data *td = &threads[i]; | |
189 | ||
190 | td->terminate = 1; | |
191 | td->start_delay = 0; | |
192 | } | |
02bdd9ba JA |
193 | } |
194 | ||
5c24b2c4 | 195 | static int init_random_state(struct thread_data *td) |
892199bd JA |
196 | { |
197 | unsigned long seed = 123; | |
198 | ||
199 | if (td->sequential) | |
200 | return 0; | |
201 | ||
202 | if (!repeatable) { | |
203 | int fd = open("/dev/random", O_RDONLY); | |
204 | ||
205 | if (fd == -1) { | |
206 | td->error = errno; | |
207 | return 1; | |
208 | } | |
209 | ||
7dd1389e | 210 | if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) { |
892199bd JA |
211 | td->error = EIO; |
212 | close(fd); | |
213 | return 1; | |
214 | } | |
215 | ||
216 | close(fd); | |
217 | } | |
218 | ||
219 | srand48_r(seed, &td->random_state); | |
220 | return 0; | |
221 | } | |
222 | ||
5c24b2c4 | 223 | static void shutdown_stat_file(struct thread_data *td) |
892199bd JA |
224 | { |
225 | if (td->stat_fd != -1) { | |
226 | fsync(td->stat_fd); | |
227 | close(td->stat_fd); | |
228 | } | |
229 | } | |
230 | ||
5c24b2c4 | 231 | static int init_stat_file(struct thread_data *td) |
892199bd | 232 | { |
4240cfa1 | 233 | char n[256]; |
892199bd JA |
234 | |
235 | if (!write_stat) | |
236 | return 0; | |
237 | ||
892199bd JA |
238 | sprintf(n, "%s.stat", td->file_name); |
239 | td->stat_fd = open(n, O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
240 | if (td->stat_fd == -1) { | |
892199bd JA |
241 | td->error = errno; |
242 | return 1; | |
243 | } | |
244 | ||
892199bd JA |
245 | return 0; |
246 | } | |
247 | ||
5c24b2c4 | 248 | static unsigned long utime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
249 | { |
250 | double sec, usec; | |
251 | ||
252 | sec = e->tv_sec - s->tv_sec; | |
253 | usec = e->tv_usec - s->tv_usec; | |
254 | if (sec > 0 && usec < 0) { | |
255 | sec--; | |
256 | usec += 1000000; | |
257 | } | |
258 | ||
259 | sec *= (double) 1000000; | |
260 | ||
261 | return sec + usec; | |
262 | } | |
263 | ||
5c24b2c4 | 264 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
265 | { |
266 | double sec, usec; | |
267 | ||
268 | sec = e->tv_sec - s->tv_sec; | |
269 | usec = e->tv_usec - s->tv_usec; | |
270 | if (sec > 0 && usec < 0) { | |
271 | sec--; | |
272 | usec += 1000000; | |
273 | } | |
274 | ||
275 | sec *= (double) 1000; | |
276 | usec /= (double) 1000; | |
277 | ||
278 | return sec + usec; | |
279 | } | |
280 | ||
98168d55 JA |
281 | static inline unsigned long msec_now(struct timeval *s) |
282 | { | |
283 | return s->tv_sec * 1000 + s->tv_usec / 1000; | |
284 | } | |
285 | ||
5c24b2c4 | 286 | static unsigned long get_next_offset(struct thread_data *td) |
892199bd JA |
287 | { |
288 | unsigned long b; | |
289 | long r; | |
290 | ||
291 | if (!td->sequential) { | |
292 | lrand48_r(&td->random_state, &r); | |
293 | b = (1+(double) (td->blocks-1) * r / (RAND_MAX+1.0)); | |
294 | } else { | |
295 | b = td->last_block; | |
296 | td->last_block++; | |
297 | } | |
298 | ||
299 | return b * td->bs; | |
300 | } | |
301 | ||
5c24b2c4 | 302 | static void add_stat_sample(struct thread_data *td, unsigned long msec) |
892199bd JA |
303 | { |
304 | char sample[256]; | |
305 | ||
306 | if (!td->stat_fd) | |
307 | return; | |
308 | ||
309 | #if 0 | |
4240cfa1 | 310 | sprintf(sample, "%lu, %lu\n", td->io_blocks, msec); |
892199bd JA |
311 | write(td->stat_fd, sample, strlen(sample)); |
312 | #else | |
313 | td->stat_time += msec; | |
314 | td->stat_time_last += msec; | |
315 | td->stat_blocks_last++; | |
316 | ||
317 | if (td->stat_time_last >= 500) { | |
318 | unsigned long rate = td->stat_blocks_last * td->bs / (td->stat_time_last); | |
319 | ||
320 | td->stat_time_last = 0; | |
321 | td->stat_blocks_last = 0; | |
322 | sprintf(sample, "%lu, %lu\n", td->stat_time, rate); | |
4240cfa1 | 323 | //sprintf(sample, "%lu, %lu\n", td->io_blocks, msec); |
892199bd JA |
324 | write(td->stat_fd, sample, strlen(sample)); |
325 | } | |
326 | #endif | |
327 | } | |
328 | ||
5c24b2c4 | 329 | static void usec_sleep(int usec) |
892199bd | 330 | { |
86184d14 JA |
331 | struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 }; |
332 | struct timespec rem; | |
892199bd JA |
333 | |
334 | do { | |
86184d14 JA |
335 | rem.tv_sec = rem.tv_nsec = 0; |
336 | nanosleep(&req, &rem); | |
337 | if (!rem.tv_nsec) | |
892199bd | 338 | break; |
86184d14 JA |
339 | |
340 | req.tv_nsec = rem.tv_nsec; | |
892199bd JA |
341 | } while (1); |
342 | } | |
343 | ||
5c24b2c4 | 344 | static void rate_throttle(struct thread_data *td, unsigned long time_spent) |
86184d14 | 345 | { |
4240cfa1 JA |
346 | if (!td->rate) |
347 | return; | |
348 | ||
86184d14 JA |
349 | if (time_spent < td->rate_usec_cycle) { |
350 | unsigned long s = td->rate_usec_cycle - time_spent; | |
351 | ||
352 | td->rate_pending_usleep += s; | |
fad86e6a | 353 | if (td->rate_pending_usleep >= 100000) { |
86184d14 JA |
354 | usec_sleep(td->rate_pending_usleep); |
355 | td->rate_pending_usleep = 0; | |
356 | } | |
4240cfa1 | 357 | } else { |
42b2b9fe JA |
358 | long overtime = time_spent - td->rate_usec_cycle; |
359 | ||
4240cfa1 JA |
360 | td->rate_pending_usleep -= overtime; |
361 | } | |
362 | } | |
363 | ||
5c24b2c4 | 364 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
4240cfa1 JA |
365 | { |
366 | unsigned long spent = mtime_since(&td->start, now); | |
367 | unsigned long rate; | |
368 | ||
369 | /* | |
370 | * allow a 2 second settle period in the beginning | |
371 | */ | |
372 | if (spent < 2000) | |
373 | return 0; | |
374 | ||
375 | /* | |
376 | * if rate blocks is set, sample is running | |
377 | */ | |
378 | if (td->rate_blocks) { | |
379 | spent = mtime_since(&td->lastrate, now); | |
380 | if (spent < td->ratecycle) | |
381 | return 0; | |
382 | ||
383 | rate = ((td->io_blocks - td->rate_blocks) * td->bs) / spent; | |
384 | if (rate < td->ratemin) { | |
385 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); | |
02bdd9ba | 386 | if (rate_quit) |
e6402082 | 387 | sig_handler(0); |
4240cfa1 JA |
388 | return 1; |
389 | } | |
86184d14 | 390 | } |
4240cfa1 JA |
391 | |
392 | td->rate_blocks = td->io_blocks; | |
393 | memcpy(&td->lastrate, now, sizeof(*now)); | |
394 | return 0; | |
86184d14 JA |
395 | } |
396 | ||
67903a2e JA |
397 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
398 | { | |
399 | if (mtime_since(&td->start, t) >= td->timeout * 1000) | |
400 | return 1; | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
4240cfa1 JA |
405 | #define should_fsync(td) ((td)->ddir == DDIR_WRITE && !(td)->odirect) |
406 | ||
43000118 | 407 | static void do_sync_io(struct thread_data *td) |
892199bd | 408 | { |
4240cfa1 | 409 | struct timeval s, e; |
86184d14 | 410 | unsigned long blocks, msec, usec; |
892199bd | 411 | |
892199bd JA |
412 | for (blocks = 0; blocks < td->blocks; blocks++) { |
413 | off_t offset = get_next_offset(td); | |
414 | int ret; | |
415 | ||
416 | if (td->terminate) | |
417 | break; | |
418 | ||
419 | if (lseek(td->fd, offset, SEEK_SET) == -1) { | |
420 | td->error = errno; | |
421 | break; | |
422 | } | |
423 | ||
424 | if (td->delay_sleep) | |
86184d14 | 425 | usec_sleep(td->delay_sleep); |
892199bd JA |
426 | |
427 | gettimeofday(&s, NULL); | |
428 | ||
429 | if (td->ddir == DDIR_READ) | |
7292613b | 430 | ret = read(td->fd, td->buf, td->bs); |
892199bd | 431 | else |
7292613b | 432 | ret = write(td->fd, td->buf, td->bs); |
892199bd | 433 | |
f737299d | 434 | if (ret < (int) td->bs) { |
892199bd JA |
435 | if (ret == -1) |
436 | td->error = errno; | |
437 | break; | |
438 | } | |
439 | ||
4240cfa1 JA |
440 | td->io_blocks++; |
441 | ||
442 | if (should_fsync(td) && td->fsync_blocks && | |
443 | (td->io_blocks % td->fsync_blocks) == 0) | |
444 | fsync(td->fd); | |
445 | ||
86184d14 JA |
446 | gettimeofday(&e, NULL); |
447 | ||
448 | usec = utime_since(&s, &e); | |
86184d14 | 449 | |
4240cfa1 | 450 | rate_throttle(td, usec); |
892199bd | 451 | |
4240cfa1 JA |
452 | if (check_min_rate(td, &e)) { |
453 | td->error = ENODATA; | |
454 | break; | |
455 | } | |
892199bd | 456 | |
4240cfa1 JA |
457 | msec = usec / 1000; |
458 | add_stat_sample(td, msec); | |
892199bd JA |
459 | |
460 | if (msec < td->min_latency) | |
461 | td->min_latency = msec; | |
462 | if (msec > td->max_latency) | |
463 | td->max_latency = msec; | |
67903a2e JA |
464 | |
465 | if (runtime_exceeded(td, &e)) | |
466 | break; | |
892199bd JA |
467 | } |
468 | ||
4240cfa1 | 469 | if (should_fsync(td)) |
892199bd | 470 | fsync(td->fd); |
892199bd | 471 | } |
43000118 JA |
472 | |
473 | static void aio_put_iocb(struct thread_data *td, struct iocb *iocb) | |
474 | { | |
475 | long offset = ((long) iocb - (long) td->aio_iocbs)/ sizeof(struct iocb); | |
476 | ||
477 | td->aio_iocbs_status[offset] = 0; | |
56b0eff0 | 478 | td->aio_cur_depth--; |
43000118 JA |
479 | } |
480 | ||
7292613b | 481 | static struct iocb *aio_get_iocb(struct thread_data *td, struct timeval *t) |
43000118 JA |
482 | { |
483 | struct iocb *iocb = NULL; | |
56b0eff0 | 484 | unsigned int i; |
43000118 JA |
485 | |
486 | for (i = 0; i < td->aio_depth; i++) { | |
487 | if (td->aio_iocbs_status[i] == 0) { | |
488 | td->aio_iocbs_status[i] = 1; | |
489 | iocb = &td->aio_iocbs[i]; | |
490 | break; | |
491 | } | |
492 | } | |
493 | ||
494 | if (iocb) { | |
495 | off_t off = get_next_offset(td); | |
7292613b | 496 | char *p = td->buf + i * td->bs; |
43000118 JA |
497 | |
498 | if (td->ddir == DDIR_READ) | |
499 | io_prep_pread(iocb, td->fd, p, td->bs, off); | |
500 | else | |
501 | io_prep_pwrite(iocb, td->fd, p, td->bs, off); | |
98168d55 JA |
502 | |
503 | io_set_callback(iocb, (io_callback_t) msec_now(t)); | |
43000118 JA |
504 | } |
505 | ||
506 | return iocb; | |
507 | } | |
508 | ||
56b0eff0 JA |
509 | static int aio_submit(struct thread_data *td, struct iocb *iocb) |
510 | { | |
511 | int ret; | |
512 | ||
513 | do { | |
514 | ret = io_submit(*td->aio_ctx, 1, &iocb); | |
515 | if (ret == 1) | |
516 | return 0; | |
517 | ||
518 | if (errno == EINTR) | |
519 | continue; | |
520 | else if (errno == EAGAIN) | |
521 | usleep(100); | |
522 | else | |
523 | break; | |
524 | } while (1); | |
525 | ||
526 | return 1; | |
527 | } | |
528 | ||
98168d55 JA |
529 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
530 | ||
43000118 JA |
531 | static void do_async_io(struct thread_data *td) |
532 | { | |
533 | struct timeval s, e; | |
43000118 | 534 | unsigned long blocks, msec, usec; |
43000118 | 535 | |
43000118 JA |
536 | for (blocks = 0; blocks < td->blocks; blocks++) { |
537 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
538 | struct timespec *timeout; | |
43000118 | 539 | int ret, i, min_evts = 0; |
8baf1bcc | 540 | struct iocb *iocb; |
43000118 JA |
541 | |
542 | if (td->terminate) | |
543 | break; | |
544 | ||
545 | if (td->delay_sleep) | |
546 | usec_sleep(td->delay_sleep); | |
547 | ||
548 | gettimeofday(&s, NULL); | |
549 | ||
7292613b | 550 | iocb = aio_get_iocb(td, &s); |
8baf1bcc | 551 | |
56b0eff0 JA |
552 | ret = aio_submit(td, iocb); |
553 | if (ret) { | |
43000118 JA |
554 | td->error = errno; |
555 | break; | |
556 | } | |
557 | ||
558 | td->aio_cur_depth++; | |
43000118 JA |
559 | |
560 | if (td->aio_cur_depth < td->aio_depth) { | |
561 | timeout = &ts; | |
562 | min_evts = 0; | |
563 | } else { | |
564 | timeout = NULL; | |
565 | min_evts = 1; | |
566 | } | |
567 | ||
568 | ret = io_getevents(*td->aio_ctx, min_evts, td->aio_cur_depth, td->aio_events, timeout); | |
569 | if (ret < 0) { | |
570 | td->error = errno; | |
571 | break; | |
572 | } else if (!ret) | |
573 | continue; | |
574 | ||
98168d55 JA |
575 | gettimeofday(&e, NULL); |
576 | ||
43000118 JA |
577 | for (i = 0; i < ret; i++) { |
578 | struct io_event *ev = td->aio_events + i; | |
579 | ||
580 | td->io_blocks++; | |
43000118 JA |
581 | |
582 | iocb = ev->obj; | |
98168d55 JA |
583 | |
584 | msec = msec_now(&e) - iocb_time(iocb); | |
585 | add_stat_sample(td, msec); | |
586 | ||
587 | if (msec < td->min_latency) | |
588 | td->min_latency = msec; | |
589 | if (msec > td->max_latency) | |
590 | td->max_latency = msec; | |
591 | ||
43000118 JA |
592 | aio_put_iocb(td, iocb); |
593 | } | |
594 | ||
98168d55 JA |
595 | /* |
596 | * the rate is batched for now, it should work for batches | |
597 | * of completions except the very first one which may look | |
598 | * a little bursty | |
599 | */ | |
43000118 JA |
600 | usec = utime_since(&s, &e); |
601 | ||
602 | rate_throttle(td, usec); | |
603 | ||
604 | if (check_min_rate(td, &e)) { | |
605 | td->error = ENODATA; | |
606 | break; | |
607 | } | |
67903a2e JA |
608 | |
609 | if (runtime_exceeded(td, &e)) | |
610 | break; | |
43000118 | 611 | } |
43000118 JA |
612 | } |
613 | ||
56b0eff0 | 614 | static void cleanup_pending_aio(struct thread_data *td) |
43000118 | 615 | { |
56b0eff0 JA |
616 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
617 | unsigned int i; | |
618 | int r; | |
619 | ||
43000118 | 620 | /* |
56b0eff0 | 621 | * get immediately available events, if any |
43000118 | 622 | */ |
56b0eff0 JA |
623 | r = io_getevents(*td->aio_ctx, 0, td->aio_cur_depth, td->aio_events, &ts); |
624 | if (r > 0) { | |
625 | for (i = 0; i < r; i++) | |
626 | aio_put_iocb(td, &td->aio_iocbs[i]); | |
627 | } | |
628 | ||
629 | /* | |
630 | * now cancel remaining active events | |
631 | */ | |
632 | for (i = 0; i < td->aio_depth; i++) { | |
633 | if (td->aio_iocbs_status[i] == 0) | |
634 | continue; | |
635 | ||
636 | r = io_cancel(*td->aio_ctx, &td->aio_iocbs[i], td->aio_events); | |
637 | if (!r) | |
638 | aio_put_iocb(td, &td->aio_iocbs[i]); | |
639 | } | |
640 | ||
43000118 JA |
641 | if (td->aio_cur_depth) |
642 | io_getevents(*td->aio_ctx, td->aio_cur_depth, td->aio_cur_depth, td->aio_events, NULL); | |
56b0eff0 JA |
643 | } |
644 | ||
645 | static void cleanup_aio(struct thread_data *td) | |
646 | { | |
647 | if (td->aio_cur_depth) | |
648 | cleanup_pending_aio(td); | |
43000118 JA |
649 | |
650 | if (td->aio_ctx) { | |
651 | io_destroy(*td->aio_ctx); | |
652 | free(td->aio_ctx); | |
653 | } | |
654 | if (td->aio_iocbs) | |
655 | free(td->aio_iocbs); | |
656 | if (td->aio_events) | |
657 | free(td->aio_events); | |
658 | if (td->aio_iocbs_status) | |
659 | free(td->aio_iocbs_status); | |
660 | } | |
661 | ||
662 | static int init_aio(struct thread_data *td) | |
663 | { | |
664 | td->aio_ctx = malloc(sizeof(*td->aio_ctx)); | |
665 | ||
666 | if (io_queue_init(td->aio_depth, td->aio_ctx)) { | |
667 | td->error = errno; | |
668 | return 1; | |
669 | } | |
670 | ||
671 | td->aio_iocbs = malloc(td->aio_depth * sizeof(struct iocb)); | |
672 | td->aio_events = malloc(td->aio_depth * sizeof(struct io_event)); | |
673 | td->aio_iocbs_status = malloc(td->aio_depth * sizeof(char)); | |
674 | return 0; | |
675 | } | |
676 | ||
5c24b2c4 | 677 | static void *thread_main(int shm_id, int offset, char *argv[]) |
892199bd JA |
678 | { |
679 | struct thread_data *td; | |
7292613b JA |
680 | struct timeval end; |
681 | void *data, *ptr = NULL; | |
4240cfa1 | 682 | struct stat st; |
892199bd JA |
683 | int ret = 1, flags; |
684 | ||
7292613b JA |
685 | setsid(); |
686 | ||
892199bd JA |
687 | data = shmat(shm_id, NULL, 0); |
688 | td = data + offset * sizeof(struct thread_data); | |
689 | td->pid = getpid(); | |
690 | ||
18e0b78c JA |
691 | td->fd = -1; |
692 | ||
693 | if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) { | |
694 | td->error = errno; | |
695 | goto err; | |
696 | } | |
697 | ||
359e4e6b | 698 | printf("Thread (%s) (pid=%u) (f=%s) (aio=%d) started\n", td->ddir == DDIR_READ ? "read" : "write", td->pid, td->file_name, td->aio_depth); |
892199bd JA |
699 | fflush(stdout); |
700 | ||
4240cfa1 | 701 | sprintf(argv[0], "fio%d", offset); |
892199bd | 702 | |
7dd1389e | 703 | flags = 0; |
892199bd JA |
704 | if (td->odirect) |
705 | flags |= O_DIRECT; | |
706 | ||
7dd1389e JA |
707 | if (td->ddir == DDIR_READ) |
708 | td->fd = open(td->file_name, flags | O_RDONLY); | |
709 | else | |
710 | td->fd = open(td->file_name, flags | O_WRONLY | O_CREAT | O_TRUNC, 0644); | |
711 | ||
892199bd JA |
712 | if (td->fd == -1) { |
713 | td->error = errno; | |
714 | goto err; | |
715 | } | |
716 | ||
43000118 JA |
717 | if (td->use_aio && init_aio(td)) |
718 | goto err; | |
719 | ||
892199bd JA |
720 | if (init_random_state(td)) |
721 | goto out; | |
722 | if (init_stat_file(td)) | |
723 | goto out; | |
724 | ||
725 | if (td->ddir == DDIR_READ) { | |
4240cfa1 | 726 | if (fstat(td->fd, &st) == -1) { |
892199bd JA |
727 | td->error = errno; |
728 | goto out; | |
729 | } | |
730 | ||
4240cfa1 | 731 | td->blocks = st.st_size / td->bs; |
892199bd JA |
732 | if (!td->blocks) { |
733 | td->error = EINVAL; | |
734 | goto out; | |
735 | } | |
736 | } else | |
737 | td->blocks = 1024 * 1024 * 1024 / td->bs; | |
738 | ||
f737299d | 739 | if (td->ioprio) { |
892199bd JA |
740 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
741 | td->error = errno; | |
742 | goto out; | |
743 | } | |
744 | } | |
745 | ||
746 | sem_post(&startup_sem); | |
747 | sem_wait(&td->mutex); | |
43000118 | 748 | |
7292613b JA |
749 | gettimeofday(&td->start, NULL); |
750 | ||
751 | if (td->ratemin) | |
752 | memcpy(&td->lastrate, &td->start, sizeof(td->start)); | |
753 | ||
754 | if (!td->use_aio) { | |
755 | ptr = malloc(td->bs + MASK); | |
756 | td->buf = ALIGN(ptr); | |
43000118 | 757 | do_sync_io(td); |
7292613b JA |
758 | } else { |
759 | ptr = malloc(td->bs * td->aio_depth + MASK); | |
760 | td->buf = ALIGN(ptr); | |
43000118 | 761 | do_async_io(td); |
7292613b JA |
762 | } |
763 | ||
764 | gettimeofday(&end, NULL); | |
765 | td->runtime = mtime_since(&td->start, &end); | |
43000118 | 766 | |
892199bd JA |
767 | ret = 0; |
768 | ||
769 | out: | |
892199bd JA |
770 | shutdown_stat_file(td); |
771 | err: | |
43000118 JA |
772 | if (td->use_aio) |
773 | cleanup_aio(td); | |
7292613b JA |
774 | if (td->fd != -1) { |
775 | close(td->fd); | |
776 | td->fd = -1; | |
777 | } | |
892199bd JA |
778 | if (ret) |
779 | sem_post(&startup_sem); | |
7292613b JA |
780 | if (ptr) |
781 | free(ptr); | |
02bdd9ba | 782 | td->runstate = TD_EXITED; |
4240cfa1 | 783 | shmdt(data); |
892199bd JA |
784 | return NULL; |
785 | } | |
786 | ||
5c24b2c4 | 787 | static void free_shm(void) |
892199bd JA |
788 | { |
789 | shmdt(threads); | |
790 | } | |
791 | ||
5c24b2c4 | 792 | static void show_thread_status(struct thread_data *td) |
892199bd JA |
793 | { |
794 | int prio, prio_class; | |
795 | unsigned long bw = 0; | |
796 | ||
213b446c JA |
797 | if (!td->io_blocks && !td->error) |
798 | return; | |
799 | ||
892199bd | 800 | if (td->runtime) |
4240cfa1 | 801 | bw = (td->io_blocks * td->bs) / td->runtime; |
892199bd JA |
802 | |
803 | prio = td->ioprio & 0xff; | |
804 | prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT; | |
805 | ||
4240cfa1 | 806 | printf("thread%d (%s): err=%2d, prio=%1d/%1d maxl=%5lumsec, io=%6luMiB, bw=%6luKiB/sec\n", td->thread_number, td->ddir == DDIR_READ ? " read": "write", td->error, prio_class, prio, td->max_latency, td->io_blocks * td->bs >> 20, bw); |
892199bd JA |
807 | } |
808 | ||
5c24b2c4 | 809 | static int setup_rate(struct thread_data *td) |
86184d14 | 810 | { |
4240cfa1 JA |
811 | int nr_reads_per_sec; |
812 | ||
813 | if (!td->rate) | |
814 | return 0; | |
815 | ||
816 | if (td->rate < td->ratemin) { | |
817 | fprintf(stderr, "min rate larger than nominal rate\n"); | |
818 | return -1; | |
819 | } | |
86184d14 | 820 | |
4240cfa1 | 821 | nr_reads_per_sec = td->rate * 1024 / td->bs; |
86184d14 JA |
822 | td->rate_usec_cycle = 1000000 / nr_reads_per_sec; |
823 | td->rate_pending_usleep = 0; | |
4240cfa1 | 824 | return 0; |
86184d14 JA |
825 | } |
826 | ||
47d45203 | 827 | static struct thread_data *get_new_job(int global) |
892199bd | 828 | { |
4240cfa1 JA |
829 | struct thread_data *td; |
830 | ||
47d45203 JA |
831 | if (global) |
832 | return &def_thread; | |
4240cfa1 JA |
833 | if (thread_number >= MAX_JOBS) |
834 | return NULL; | |
835 | ||
836 | td = &threads[thread_number++]; | |
fc24389f | 837 | memset(td, 0, sizeof(*td)); |
892199bd | 838 | |
86184d14 | 839 | td->thread_number = thread_number; |
47d45203 JA |
840 | td->ddir = def_thread.ddir; |
841 | td->bs = def_thread.bs; | |
842 | td->odirect = def_thread.odirect; | |
843 | td->ratecycle = def_thread.ratecycle; | |
844 | td->sequential = def_thread.sequential; | |
67903a2e | 845 | td->timeout = def_thread.timeout; |
47d45203 | 846 | memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask)); |
f737299d JA |
847 | |
848 | return td; | |
849 | } | |
850 | ||
4240cfa1 JA |
851 | static void put_job(struct thread_data *td) |
852 | { | |
853 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); | |
854 | thread_number--; | |
855 | } | |
856 | ||
5c24b2c4 JA |
857 | static int add_job(struct thread_data *td, const char *filename, int prioclass, |
858 | int prio) | |
f737299d | 859 | { |
47d45203 JA |
860 | if (td == &def_thread) |
861 | return 0; | |
862 | ||
f737299d | 863 | strcpy(td->file_name, filename); |
892199bd | 864 | td->stat_fd = -1; |
4240cfa1 | 865 | sem_init(&td->mutex, 1, 0); |
892199bd | 866 | td->min_latency = 10000000; |
f737299d JA |
867 | td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio; |
868 | ||
43000118 JA |
869 | if (td->use_aio && !td->aio_depth) |
870 | td->aio_depth = 1; | |
871 | ||
4240cfa1 JA |
872 | if (setup_rate(td)) |
873 | return -1; | |
f737299d | 874 | |
43000118 | 875 | printf("Client%d: file=%s, rw=%d, prio=%d, seq=%d, odir=%d, bs=%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, td->ioprio, td->sequential, td->odirect, td->bs, td->rate, td->use_aio, td->aio_depth); |
4240cfa1 | 876 | return 0; |
892199bd JA |
877 | } |
878 | ||
18e0b78c JA |
879 | static void fill_cpu_mask(cpu_set_t cpumask, int cpu) |
880 | { | |
f737299d | 881 | unsigned int i; |
18e0b78c JA |
882 | |
883 | CPU_ZERO(&cpumask); | |
884 | ||
885 | for (i = 0; i < sizeof(int) * 8; i++) { | |
886 | if ((1 << i) & cpu) | |
887 | CPU_SET(i, &cpumask); | |
888 | } | |
889 | } | |
890 | ||
5c24b2c4 | 891 | static void fill_option(const char *input, char *output) |
892199bd JA |
892 | { |
893 | int i; | |
894 | ||
895 | i = 0; | |
896 | while (input[i] != ',' && input[i] != '}' && input[i] != '\0') { | |
897 | output[i] = input[i]; | |
898 | i++; | |
899 | } | |
900 | ||
901 | output[i] = '\0'; | |
902 | } | |
903 | ||
904 | /* | |
905 | * job key words: | |
906 | * | |
907 | * file= | |
908 | * bs= | |
909 | * rw= | |
910 | * direct= | |
911 | */ | |
5c24b2c4 | 912 | static void parse_jobs_cmd(int argc, char *argv[], int index) |
892199bd | 913 | { |
f737299d JA |
914 | struct thread_data *td; |
915 | unsigned int prio, prioclass, cpu; | |
892199bd JA |
916 | char *string, *filename, *p, *c; |
917 | int i; | |
918 | ||
919 | string = malloc(256); | |
920 | filename = malloc(256); | |
921 | ||
922 | for (i = index; i < argc; i++) { | |
923 | p = argv[i]; | |
924 | ||
925 | c = strpbrk(p, "{"); | |
926 | if (!c) | |
927 | break; | |
928 | ||
929 | filename[0] = 0; | |
4240cfa1 | 930 | |
47d45203 | 931 | td = get_new_job(0); |
4240cfa1 JA |
932 | if (!td) |
933 | break; | |
f737299d | 934 | |
892199bd | 935 | prioclass = 2; |
f737299d | 936 | prio = 4; |
892199bd JA |
937 | |
938 | c = strstr(p, "rw="); | |
939 | if (c) { | |
940 | c += 3; | |
941 | if (*c == '0') | |
f737299d | 942 | td->ddir = DDIR_READ; |
892199bd | 943 | else |
f737299d | 944 | td->ddir = DDIR_WRITE; |
892199bd JA |
945 | } |
946 | ||
947 | c = strstr(p, "prio="); | |
948 | if (c) { | |
949 | c += 5; | |
950 | prio = *c - '0'; | |
951 | } | |
952 | ||
953 | c = strstr(p, "prioclass="); | |
954 | if (c) { | |
955 | c += 10; | |
956 | prioclass = *c - '0'; | |
957 | } | |
958 | ||
959 | c = strstr(p, "file="); | |
960 | if (c) { | |
961 | c += 5; | |
962 | fill_option(c, filename); | |
963 | } | |
964 | ||
965 | c = strstr(p, "bs="); | |
966 | if (c) { | |
967 | c += 3; | |
968 | fill_option(c, string); | |
f737299d JA |
969 | td->bs = strtoul(string, NULL, 10); |
970 | td->bs <<= 10; | |
892199bd JA |
971 | } |
972 | ||
973 | c = strstr(p, "direct="); | |
974 | if (c) { | |
975 | c += 7; | |
976 | if (*c != '0') | |
f737299d | 977 | td->odirect = 1; |
892199bd | 978 | else |
f737299d | 979 | td->odirect = 0; |
892199bd JA |
980 | } |
981 | ||
982 | c = strstr(p, "delay="); | |
983 | if (c) { | |
984 | c += 6; | |
985 | fill_option(c, string); | |
f737299d | 986 | td->delay_sleep = strtoul(string, NULL, 10); |
892199bd JA |
987 | } |
988 | ||
86184d14 JA |
989 | c = strstr(p, "rate="); |
990 | if (c) { | |
991 | c += 5; | |
992 | fill_option(c, string); | |
f737299d | 993 | td->rate = strtoul(string, NULL, 10); |
86184d14 JA |
994 | } |
995 | ||
4240cfa1 JA |
996 | c = strstr(p, "ratemin="); |
997 | if (c) { | |
998 | c += 8; | |
999 | fill_option(c, string); | |
1000 | td->ratemin = strtoul(string, NULL, 10); | |
1001 | } | |
1002 | ||
1003 | c = strstr(p, "ratecycle="); | |
1004 | if (c) { | |
1005 | c += 10; | |
1006 | fill_option(c, string); | |
1007 | td->ratecycle = strtoul(string, NULL, 10); | |
1008 | } | |
1009 | ||
18e0b78c JA |
1010 | c = strstr(p, "cpumask="); |
1011 | if (c) { | |
1012 | c += 8; | |
1013 | fill_option(c, string); | |
1014 | cpu = strtoul(string, NULL, 10); | |
f737299d | 1015 | fill_cpu_mask(td->cpumask, cpu); |
18e0b78c JA |
1016 | } |
1017 | ||
4240cfa1 JA |
1018 | c = strstr(p, "fsync="); |
1019 | if (c) { | |
1020 | c += 6; | |
1021 | fill_option(c, string); | |
1022 | td->fsync_blocks = strtoul(string, NULL, 10); | |
1023 | } | |
18e0b78c | 1024 | |
fc24389f JA |
1025 | c = strstr(p, "startdelay="); |
1026 | if (c) { | |
1027 | c += 11; | |
1028 | fill_option(c, string); | |
1029 | td->start_delay = strtoul(string, NULL, 10); | |
1030 | } | |
1031 | ||
67903a2e JA |
1032 | c = strstr(p, "timeout="); |
1033 | if (c) { | |
1034 | c += 8; | |
1035 | fill_option(c, string); | |
1036 | td->timeout = strtoul(string, NULL, 10); | |
1037 | } | |
1038 | ||
43000118 JA |
1039 | c = strstr(p, "aio_depth="); |
1040 | if (c) { | |
1041 | c += 10; | |
1042 | fill_option(c, string); | |
1043 | td->aio_depth = strtoul(string, NULL, 10); | |
1044 | } | |
1045 | ||
1046 | c = strstr(p, "aio"); | |
1047 | if (c) | |
1048 | td->use_aio = 1; | |
1049 | ||
892199bd JA |
1050 | c = strstr(p, "random"); |
1051 | if (c) | |
f737299d | 1052 | td->sequential = 0; |
892199bd JA |
1053 | c = strstr(p, "sequential"); |
1054 | if (c) | |
f737299d | 1055 | td->sequential = 1; |
892199bd | 1056 | |
4240cfa1 JA |
1057 | if (add_job(td, filename, prioclass, prio)) |
1058 | put_job(td); | |
892199bd JA |
1059 | } |
1060 | ||
7dd1389e JA |
1061 | free(string); |
1062 | free(filename); | |
892199bd JA |
1063 | } |
1064 | ||
5c24b2c4 | 1065 | static int check_int(char *p, char *name, unsigned int *val) |
7dd1389e JA |
1066 | { |
1067 | char str[128]; | |
1068 | ||
1069 | sprintf(str, "%s=%%d", name); | |
1070 | if (sscanf(p, str, val) == 1) | |
1071 | return 0; | |
1072 | ||
1073 | sprintf(str, "%s = %%d", name); | |
1074 | if (sscanf(p, str, val) == 1) | |
1075 | return 0; | |
1076 | ||
1077 | return 1; | |
1078 | } | |
1079 | ||
7292613b | 1080 | static int is_empty_or_comment(char *line) |
7dd1389e JA |
1081 | { |
1082 | unsigned int i; | |
1083 | ||
7292613b | 1084 | for (i = 0; i < strlen(line); i++) { |
7292613b | 1085 | if (line[i] == ';') |
47d45203 JA |
1086 | return 1; |
1087 | if (!isspace(line[i]) && !iscntrl(line[i])) | |
7292613b JA |
1088 | return 0; |
1089 | } | |
7dd1389e JA |
1090 | |
1091 | return 1; | |
1092 | } | |
1093 | ||
5c24b2c4 | 1094 | static int parse_jobs_ini(char *file) |
7dd1389e | 1095 | { |
47d45203 | 1096 | unsigned int prioclass, prio, cpu, global; |
f737299d | 1097 | struct thread_data *td; |
7dd1389e JA |
1098 | char *string, *name; |
1099 | fpos_t off; | |
1100 | FILE *f; | |
1101 | char *p; | |
1102 | ||
1103 | f = fopen(file, "r"); | |
1104 | if (!f) { | |
1105 | perror("fopen"); | |
4240cfa1 | 1106 | return 1; |
7dd1389e JA |
1107 | } |
1108 | ||
1109 | string = malloc(4096); | |
1110 | name = malloc(256); | |
1111 | ||
7dd1389e | 1112 | while ((p = fgets(string, 4096, f)) != NULL) { |
7292613b JA |
1113 | if (is_empty_or_comment(p)) |
1114 | continue; | |
7dd1389e JA |
1115 | if (sscanf(p, "[%s]", name) != 1) |
1116 | continue; | |
1117 | ||
47d45203 JA |
1118 | global = !strncmp(name, "global", 6); |
1119 | ||
7dd1389e JA |
1120 | name[strlen(name) - 1] = '\0'; |
1121 | ||
47d45203 | 1122 | td = get_new_job(global); |
4240cfa1 JA |
1123 | if (!td) |
1124 | break; | |
f737299d | 1125 | |
7dd1389e | 1126 | prioclass = 2; |
f737299d | 1127 | prio = 4; |
7dd1389e JA |
1128 | |
1129 | fgetpos(f, &off); | |
1130 | while ((p = fgets(string, 4096, f)) != NULL) { | |
7292613b | 1131 | if (is_empty_or_comment(p)) |
e6402082 JA |
1132 | continue; |
1133 | if (strstr(p, "[")) | |
7dd1389e | 1134 | break; |
f737299d JA |
1135 | if (!check_int(p, "bs", &td->bs)) { |
1136 | td->bs <<= 10; | |
7dd1389e JA |
1137 | fgetpos(f, &off); |
1138 | continue; | |
1139 | } | |
f737299d | 1140 | if (!check_int(p, "rw", &td->ddir)) { |
7dd1389e JA |
1141 | fgetpos(f, &off); |
1142 | continue; | |
1143 | } | |
1144 | if (!check_int(p, "prio", &prio)) { | |
1145 | fgetpos(f, &off); | |
1146 | continue; | |
1147 | } | |
1148 | if (!check_int(p, "prioclass", &prioclass)) { | |
1149 | fgetpos(f, &off); | |
1150 | continue; | |
1151 | } | |
f737299d | 1152 | if (!check_int(p, "direct", &td->odirect)) { |
7dd1389e JA |
1153 | fgetpos(f, &off); |
1154 | continue; | |
1155 | } | |
f737299d | 1156 | if (!check_int(p, "rate", &td->rate)) { |
7dd1389e JA |
1157 | fgetpos(f, &off); |
1158 | continue; | |
1159 | } | |
4240cfa1 JA |
1160 | if (!check_int(p, "ratemin", &td->ratemin)) { |
1161 | fgetpos(f, &off); | |
1162 | continue; | |
1163 | } | |
1164 | if (!check_int(p, "ratecycle", &td->ratecycle)) { | |
1165 | fgetpos(f, &off); | |
1166 | continue; | |
1167 | } | |
f737299d | 1168 | if (!check_int(p, "delay", &td->delay_sleep)) { |
7dd1389e JA |
1169 | fgetpos(f, &off); |
1170 | continue; | |
1171 | } | |
18e0b78c | 1172 | if (!check_int(p, "cpumask", &cpu)) { |
f737299d | 1173 | fill_cpu_mask(td->cpumask, cpu); |
18e0b78c JA |
1174 | fgetpos(f, &off); |
1175 | continue; | |
1176 | } | |
4240cfa1 JA |
1177 | if (!check_int(p, "fsync", &td->fsync_blocks)) { |
1178 | fgetpos(f, &off); | |
1179 | continue; | |
1180 | } | |
fc24389f JA |
1181 | if (!check_int(p, "startdelay", &td->start_delay)) { |
1182 | fgetpos(f, &off); | |
1183 | continue; | |
1184 | } | |
67903a2e JA |
1185 | if (!check_int(p, "timeout", &td->timeout)) { |
1186 | fgetpos(f, &off); | |
1187 | continue; | |
1188 | } | |
43000118 JA |
1189 | if (!check_int(p, "aio_depth", &td->aio_depth)) { |
1190 | fgetpos(f, &off); | |
1191 | continue; | |
1192 | } | |
1193 | if (!strncmp(p, "sequential", 10)) { | |
f737299d | 1194 | td->sequential = 1; |
7dd1389e JA |
1195 | fgetpos(f, &off); |
1196 | continue; | |
1197 | } | |
43000118 | 1198 | if (!strncmp(p, "random", 6)) { |
f737299d | 1199 | td->sequential = 0; |
7dd1389e JA |
1200 | fgetpos(f, &off); |
1201 | continue; | |
1202 | } | |
43000118 JA |
1203 | if (!strncmp(p, "aio", 3)) { |
1204 | td->use_aio = 1; | |
1205 | fgetpos(f, &off); | |
1206 | continue; | |
1207 | } | |
1208 | ||
e6402082 | 1209 | printf("Client%d: bad option %s\n",td->thread_number,p); |
7dd1389e JA |
1210 | } |
1211 | fsetpos(f, &off); | |
1212 | ||
4240cfa1 JA |
1213 | if (add_job(td, name, prioclass, prio)) |
1214 | put_job(td); | |
7dd1389e JA |
1215 | } |
1216 | ||
1217 | free(string); | |
1218 | free(name); | |
fc7d63df | 1219 | fclose(f); |
4240cfa1 | 1220 | return 0; |
7dd1389e JA |
1221 | } |
1222 | ||
5c24b2c4 | 1223 | static int parse_options(int argc, char *argv[]) |
892199bd | 1224 | { |
01c4d8de | 1225 | int i; |
892199bd JA |
1226 | |
1227 | for (i = 1; i < argc; i++) { | |
1228 | char *parm = argv[i]; | |
1229 | ||
1230 | if (parm[0] != '-') | |
1231 | break; | |
1232 | ||
1233 | parm++; | |
1234 | switch (*parm) { | |
1235 | case 's': | |
1236 | parm++; | |
47d45203 | 1237 | def_thread.sequential = !!atoi(parm); |
892199bd JA |
1238 | break; |
1239 | case 'b': | |
1240 | parm++; | |
47d45203 JA |
1241 | def_thread.bs = atoi(parm); |
1242 | def_thread.bs <<= 10; | |
1243 | if (!def_thread.bs) { | |
4240cfa1 | 1244 | printf("bad block size\n"); |
47d45203 | 1245 | def_thread.bs = DEF_BS; |
4240cfa1 | 1246 | } |
892199bd JA |
1247 | break; |
1248 | case 't': | |
1249 | parm++; | |
47d45203 | 1250 | def_thread.timeout = atoi(parm); |
892199bd JA |
1251 | break; |
1252 | case 'w': | |
1253 | parm++; | |
1254 | write_stat = !!atoi(parm); | |
1255 | break; | |
1256 | case 'r': | |
1257 | parm++; | |
1258 | repeatable = !!atoi(parm); | |
1259 | break; | |
02bdd9ba JA |
1260 | case 'R': |
1261 | parm++; | |
1262 | rate_quit = !!atoi(parm); | |
1263 | break; | |
892199bd JA |
1264 | case 'o': |
1265 | parm++; | |
47d45203 | 1266 | def_thread.odirect = !!atoi(parm); |
892199bd | 1267 | break; |
7dd1389e JA |
1268 | case 'f': |
1269 | if (i + 1 >= argc) { | |
1270 | printf("-f needs file as arg\n"); | |
1271 | break; | |
1272 | } | |
1273 | ini_file = strdup(argv[i+1]); | |
1274 | break; | |
892199bd | 1275 | default: |
7dd1389e | 1276 | printf("bad option %s\n", argv[i]); |
892199bd JA |
1277 | break; |
1278 | } | |
1279 | } | |
1280 | ||
892199bd JA |
1281 | return i; |
1282 | } | |
1283 | ||
213b446c | 1284 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
02bdd9ba | 1285 | { |
213b446c | 1286 | int i; |
02bdd9ba JA |
1287 | |
1288 | for (i = 0; i < thread_number; i++) { | |
1289 | struct thread_data *td = &threads[i]; | |
1290 | ||
213b446c JA |
1291 | if (td->runstate != TD_EXITED) |
1292 | continue; | |
02bdd9ba | 1293 | |
213b446c JA |
1294 | td->runstate = TD_REAPED; |
1295 | waitpid(td->pid, NULL, 0); | |
1296 | (*nr_running)--; | |
1297 | (*m_rate) -= td->ratemin; | |
1298 | (*t_rate) -= td->rate; | |
e6402082 JA |
1299 | |
1300 | if (td->terminate) | |
1301 | continue; | |
1302 | ||
213b446c JA |
1303 | printf("Threads now running: %d", *nr_running); |
1304 | if (*m_rate || *t_rate) | |
1305 | printf(", rate %d/%dKiB/sec", *t_rate, *m_rate); | |
1306 | printf("\n"); | |
1307 | } | |
02bdd9ba JA |
1308 | } |
1309 | ||
fc24389f JA |
1310 | static void run_threads(char *argv[]) |
1311 | { | |
1312 | struct timeval genesis, now; | |
1313 | struct thread_data *td; | |
1314 | unsigned long spent; | |
213b446c | 1315 | int i, todo, nr_running, m_rate, t_rate; |
fc24389f JA |
1316 | |
1317 | gettimeofday(&genesis, NULL); | |
1318 | ||
1319 | printf("Starting %d threads\n", thread_number); | |
1320 | fflush(stdout); | |
1321 | ||
7292613b JA |
1322 | signal(SIGINT, sig_handler); |
1323 | ||
fc24389f | 1324 | todo = thread_number; |
02bdd9ba | 1325 | nr_running = 0; |
213b446c | 1326 | m_rate = t_rate = 0; |
fc24389f | 1327 | |
213b446c | 1328 | while (todo) { |
fc24389f JA |
1329 | for (i = 0; i < thread_number; i++) { |
1330 | td = &threads[i]; | |
1331 | ||
02bdd9ba | 1332 | if (td->runstate != TD_NOT_CREATED) |
fc24389f JA |
1333 | continue; |
1334 | ||
213b446c JA |
1335 | /* |
1336 | * never got a chance to start, killed by other | |
1337 | * thread for some reason | |
1338 | */ | |
1339 | if (td->terminate) { | |
1340 | todo--; | |
1341 | continue; | |
1342 | } | |
1343 | ||
fc24389f JA |
1344 | if (td->start_delay) { |
1345 | gettimeofday(&now, NULL); | |
1346 | spent = mtime_since(&genesis, &now); | |
1347 | ||
1348 | if (td->start_delay * 1000 > spent) | |
1349 | continue; | |
1350 | } | |
1351 | ||
02bdd9ba | 1352 | td->runstate = TD_CREATED; |
fc24389f JA |
1353 | sem_init(&startup_sem, 1, 1); |
1354 | todo--; | |
1355 | ||
1356 | if (fork()) | |
1357 | sem_wait(&startup_sem); | |
1358 | else { | |
1359 | thread_main(shm_id, i, argv); | |
1360 | exit(0); | |
1361 | } | |
1362 | } | |
1363 | ||
1364 | for (i = 0; i < thread_number; i++) { | |
1365 | struct thread_data *td = &threads[i]; | |
1366 | ||
02bdd9ba JA |
1367 | if (td->runstate == TD_CREATED) { |
1368 | td->runstate = TD_STARTED; | |
1369 | nr_running++; | |
213b446c JA |
1370 | m_rate += td->ratemin; |
1371 | t_rate += td->rate; | |
fc24389f | 1372 | sem_post(&td->mutex); |
213b446c JA |
1373 | |
1374 | printf("Threads now running: %d", nr_running); | |
1375 | if (m_rate || t_rate) | |
1376 | printf(", rate %d/%dKiB/sec", t_rate, m_rate); | |
1377 | printf("\n"); | |
fc24389f JA |
1378 | } |
1379 | } | |
1380 | ||
213b446c | 1381 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba | 1382 | |
fc24389f JA |
1383 | if (todo) |
1384 | usleep(100000); | |
1385 | } | |
02bdd9ba JA |
1386 | |
1387 | while (nr_running) { | |
213b446c | 1388 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba JA |
1389 | usleep(10000); |
1390 | } | |
fc24389f JA |
1391 | } |
1392 | ||
892199bd JA |
1393 | int main(int argc, char *argv[]) |
1394 | { | |
1395 | static unsigned long max_run[2], min_run[2], total_blocks[2]; | |
1396 | static unsigned long max_bw[2], min_bw[2], maxl[2], minl[2]; | |
1397 | static unsigned long read_mb, write_mb, read_agg, write_agg; | |
4240cfa1 | 1398 | int i; |
18e0b78c | 1399 | |
4240cfa1 | 1400 | shm_id = shmget(0, MAX_JOBS * sizeof(struct thread_data), IPC_CREAT | 0600); |
892199bd JA |
1401 | if (shm_id == -1) { |
1402 | perror("shmget"); | |
1403 | return 1; | |
1404 | } | |
1405 | ||
1406 | threads = shmat(shm_id, NULL, 0); | |
86184d14 JA |
1407 | if (threads == (void *) -1 ) { |
1408 | perror("shmat"); | |
1409 | return 1; | |
1410 | } | |
892199bd JA |
1411 | |
1412 | atexit(free_shm); | |
1413 | ||
47d45203 | 1414 | if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) { |
4240cfa1 JA |
1415 | perror("sched_getaffinity"); |
1416 | return 1; | |
1417 | } | |
1418 | ||
47d45203 JA |
1419 | /* |
1420 | * fill globals | |
1421 | */ | |
1422 | def_thread.ddir = DDIR_READ; | |
1423 | def_thread.bs = DEF_BS; | |
1424 | def_thread.odirect = 1; | |
1425 | def_thread.ratecycle = DEF_RATE_CYCLE; | |
1426 | def_thread.sequential = 1; | |
1427 | def_thread.timeout = DEF_TIMEOUT; | |
1428 | ||
892199bd | 1429 | i = parse_options(argc, argv); |
7dd1389e | 1430 | |
4240cfa1 JA |
1431 | if (ini_file) { |
1432 | if (parse_jobs_ini(ini_file)) | |
1433 | return 1; | |
1434 | } else | |
1435 | parse_jobs_cmd(argc, argv, i); | |
7dd1389e | 1436 | |
4240cfa1 JA |
1437 | if (!thread_number) { |
1438 | printf("Nothing to do\n"); | |
1439 | return 1; | |
1440 | } | |
7dd1389e | 1441 | |
47d45203 | 1442 | printf("%s: %s, bs=%uKiB, timeo=%u, write_stat=%u, odirect=%d\n", argv[0], def_thread.sequential ? "sequential" : "random", def_thread.bs >> 10, def_thread.timeout, write_stat, def_thread.odirect); |
7dd1389e | 1443 | |
fc24389f | 1444 | run_threads(argv); |
892199bd | 1445 | |
892199bd JA |
1446 | min_bw[0] = min_run[0] = ~0UL; |
1447 | min_bw[1] = min_run[1] = ~0UL; | |
1448 | minl[0] = minl[1] = ~0UL; | |
1449 | for (i = 0; i < thread_number; i++) { | |
1450 | struct thread_data *td = &threads[i]; | |
1451 | unsigned long bw = 0; | |
1452 | ||
1453 | if (td->error) | |
7dd1389e | 1454 | goto show_stat; |
892199bd JA |
1455 | |
1456 | if (td->runtime < min_run[td->ddir]) | |
1457 | min_run[td->ddir] = td->runtime; | |
1458 | if (td->runtime > max_run[td->ddir]) | |
1459 | max_run[td->ddir] = td->runtime; | |
1460 | ||
1461 | if (td->runtime) | |
4240cfa1 | 1462 | bw = (td->io_blocks * td->bs) / td->runtime; |
892199bd JA |
1463 | if (bw < min_bw[td->ddir]) |
1464 | min_bw[td->ddir] = bw; | |
1465 | if (bw > max_bw[td->ddir]) | |
1466 | max_bw[td->ddir] = bw; | |
1467 | if (td->max_latency < minl[td->ddir]) | |
1468 | minl[td->ddir] = td->max_latency; | |
1469 | if (td->max_latency > maxl[td->ddir]) | |
1470 | maxl[td->ddir] = td->max_latency; | |
1471 | ||
4240cfa1 | 1472 | total_blocks[td->ddir] += td->io_blocks; |
892199bd JA |
1473 | |
1474 | if (td->ddir == DDIR_READ) { | |
4240cfa1 | 1475 | read_mb += (td->bs * td->io_blocks) >> 20; |
892199bd | 1476 | if (td->runtime) |
4240cfa1 | 1477 | read_agg += (td->io_blocks * td->bs) / td->runtime; |
892199bd JA |
1478 | } |
1479 | if (td->ddir == DDIR_WRITE) { | |
4240cfa1 | 1480 | write_mb += (td->bs * td->io_blocks) >> 20; |
892199bd | 1481 | if (td->runtime) |
4240cfa1 | 1482 | write_agg += (td->io_blocks * td->bs) / td->runtime; |
892199bd JA |
1483 | } |
1484 | ||
7dd1389e | 1485 | show_stat: |
892199bd JA |
1486 | show_thread_status(td); |
1487 | } | |
1488 | ||
1489 | printf("Run status:\n"); | |
1490 | if (max_run[DDIR_READ]) | |
1491 | printf(" READ: io=%luMiB, aggrb=%lu, minl=%lu, maxl=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", read_mb, read_agg, minl[0], maxl[0], min_bw[0], max_bw[0], min_run[0], max_run[0]); | |
1492 | if (max_run[DDIR_WRITE]) | |
1493 | printf(" WRITE: io=%luMiB, aggrb=%lu, minl=%lu, maxl=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", write_mb, write_agg, minl[1], maxl[1], min_bw[1], max_bw[1], min_run[1], max_run[1]); | |
fc24389f | 1494 | |
892199bd JA |
1495 | return 0; |
1496 | } |