static struct reader_thread reader_thread;
static struct writer_thread writer_thread;
-uint64_t utime_since(const struct timeval *s, const struct timeval *e)
+uint64_t utime_since(const struct timespec *s, const struct timespec *e)
{
long sec, usec;
uint64_t ret;
sec = e->tv_sec - s->tv_sec;
- usec = e->tv_usec - s->tv_usec;
+ usec = (e->tv_nsec - s->tv_nsec) / 1000;
if (sec > 0 && usec < 0) {
sec--;
usec += 1000000;
static int write_work(struct work_item *work)
{
- struct timeval s, e;
+ struct timespec s, e;
ssize_t ret;
- gettimeofday(&s, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &s);
ret = write(STDOUT_FILENO, work->buf, work->buf_size);
- gettimeofday(&e, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &e);
assert(ret == work->buf_size);
add_lat(&work->writer->s, utime_since(&s, &e), "write");
static void reader_work(struct work_item *work)
{
- struct timeval s, e;
+ struct timespec s, e;
ssize_t ret;
size_t left;
void *buf;
off_t off;
- gettimeofday(&s, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &s);
left = work->buf_size;
buf = work->buf;
buf += ret;
}
- gettimeofday(&e, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &e);
add_lat(&work->reader->s, utime_since(&s, &e), "read");
pthread_cond_signal(&rt->thread.cond);
} else {
int ret = pthread_create(&work->thread, NULL, reader_one_off, work);
- if (ret)
+ if (ret) {
fprintf(stderr, "pthread_create=%d\n", ret);
- else
- pthread_detach(work->thread);
+ } else {
+ ret = pthread_detach(work->thread);
+ if (ret)
+ fprintf(stderr, "pthread_detach=%d\n", ret);
+ }
}
}
while ((c = getopt(argc, argv, "f:b:t:w:")) != -1) {
switch (c) {
case 'f':
+ if (file)
+ return usage(argv);
file = strdup(optarg);
break;
case 'b':
int main(int argc, char *argv[])
{
pthread_condattr_t cattr;
- struct timeval s, re, we;
+ struct timespec s, re, we;
struct reader_thread *rt;
struct writer_thread *wt;
unsigned long rate;
+ uint64_t elapsed;
struct stat sb;
size_t bytes;
off_t off;
assert(ret == 0);
#endif
- gettimeofday(&s, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &s);
while (sb.st_size) {
struct work_item *work;
}
exit_thread(&rt->thread, NULL, NULL);
- gettimeofday(&re, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &re);
exit_thread(&wt->thread, prune_done_entries, wt);
- gettimeofday(&we, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &we);
show_latencies(&rt->s, "READERS");
show_latencies(&wt->s, "WRITERS");
bytes /= 1024;
- rate = (bytes * 1000UL * 1000UL) / utime_since(&s, &re);
+ elapsed = utime_since(&s, &re);
+ rate = elapsed ? (bytes * 1000UL * 1000UL) / elapsed : 0;
fprintf(stderr, "Read rate (KiB/sec) : %lu\n", rate);
- rate = (bytes * 1000UL * 1000UL) / utime_since(&s, &we);
+ elapsed = utime_since(&s, &we);
+ rate = elapsed ? (bytes * 1000UL * 1000UL) / elapsed : 0;
fprintf(stderr, "Write rate (KiB/sec): %lu\n", rate);
close(fd);