X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=time.c;h=45a415cbb43424bf54266f3913b287e5515efd39;hp=d08c791673f55b8139be5ec20cc757a891913423;hb=2270890cef8d98ab97f87d348d16dce6454e631f;hpb=c7b0818d2a9d1b8597256ea002d9cf9346ea97f3 diff --git a/time.c b/time.c index d08c7916..45a415cb 100644 --- a/time.c +++ b/time.c @@ -4,10 +4,11 @@ #include "fio.h" static struct timeval genesis; +static unsigned long ns_granularity; unsigned long utime_since(struct timeval *s, struct timeval *e) { - double sec, usec; + long sec, usec; sec = e->tv_sec - s->tv_sec; usec = e->tv_usec - s->tv_usec; @@ -21,17 +22,17 @@ unsigned long utime_since(struct timeval *s, struct timeval *e) return sec + usec; } -static unsigned long utime_since_now(struct timeval *s) +unsigned long utime_since_now(struct timeval *s) { struct timeval t; - gettimeofday(&t, NULL); + fio_gettime(&t, NULL); return utime_since(s, &t); } unsigned long mtime_since(struct timeval *s, struct timeval *e) { - double sec, usec; + long sec, usec; sec = e->tv_sec - s->tv_sec; usec = e->tv_usec - s->tv_usec; @@ -49,8 +50,9 @@ unsigned long mtime_since(struct timeval *s, struct timeval *e) unsigned long mtime_since_now(struct timeval *s) { struct timeval t; + void *p = __builtin_return_address(0); - gettimeofday(&t, NULL); + fio_gettime(&t, p); return mtime_since(s, &t); } @@ -66,47 +68,63 @@ void __usec_sleep(unsigned int usec) { struct timeval start; - gettimeofday(&start, NULL); + fio_gettime(&start, NULL); while (utime_since_now(&start) < usec) nop; } void usec_sleep(struct thread_data *td, unsigned long usec) { - struct timespec req, rem; - - req.tv_sec = usec / 1000000; - req.tv_nsec = usec * 1000 - req.tv_sec * 1000000; + struct timespec req; + struct timeval tv; do { - if (usec < 5000) { + unsigned long ts = usec; + + if (usec < ns_granularity) { __usec_sleep(usec); break; } - rem.tv_sec = rem.tv_nsec = 0; - if (nanosleep(&req, &rem) < 0) - break; + ts = usec - ns_granularity; + + if (ts >= 1000000) { + req.tv_sec = ts / 1000000; + ts -= 1000000 * req.tv_sec; + } else + req.tv_sec = 0; - if ((rem.tv_sec + rem.tv_nsec) == 0) + req.tv_nsec = ts * 1000; + fio_gettime(&tv, NULL); + + if (nanosleep(&req, NULL) < 0) break; - req.tv_nsec = rem.tv_nsec; - req.tv_sec = rem.tv_sec; + ts = utime_since_now(&tv); + if (ts >= usec) + break; - usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000; + usec -= ts; } while (!td->terminate); } void rate_throttle(struct thread_data *td, unsigned long time_spent, - unsigned int bytes, int ddir) + unsigned int bytes) { unsigned long usec_cycle; + unsigned int bs; if (!td->rate) return; - usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs[ddir]); + if (td_rw(td)) + bs = td->rw_min_bs; + else if (td_read(td)) + bs = td->min_bs[DDIR_READ]; + else + bs = td->min_bs[DDIR_WRITE]; + + usec_cycle = td->rate_usec_cycle * (bytes / bs); if (time_spent < usec_cycle) { unsigned long s = usec_cycle - time_spent; @@ -128,9 +146,33 @@ unsigned long mtime_since_genesis(void) return mtime_since_now(&genesis); } -void time_init(void) +static void fio_init time_init(void) +{ + int i; + + /* + * Check the granularity of the nanosleep function + */ + for (i = 0; i < 10; i++) { + struct timeval tv; + struct timespec ts; + unsigned long elapsed; + + fio_gettime(&tv, NULL); + ts.tv_sec = 0; + ts.tv_nsec = 1000; + + nanosleep(&ts, NULL); + elapsed = utime_since_now(&tv); + + if (elapsed > ns_granularity) + ns_granularity = elapsed; + } +} + +void set_genesis_time(void) { - gettimeofday(&genesis, NULL); + fio_gettime(&genesis, NULL); } void fill_start_time(struct timeval *t)