From 727905b4927d63d695e9bf9a865a345d0253d945 Mon Sep 17 00:00:00 2001 From: Yao Yue Date: Sat, 19 May 2018 15:41:55 -0700 Subject: [PATCH] adding a new API to duration timer (#152) * adding a new API to duration timer * adding const modifier to snapshot API --- include/time/cc_timer.h | 2 ++ src/time/cc_timer_darwin.c | 11 +++++++++++ src/time/cc_timer_linux.c | 11 +++++++++++ test/time/timer/check_timer.c | 15 +++++++++++---- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/include/time/cc_timer.h b/include/time/cc_timer.h index 13112c682..6a8c382ee 100644 --- a/include/time/cc_timer.h +++ b/include/time/cc_timer.h @@ -89,6 +89,8 @@ struct timeout { /* update duration */ void duration_reset(struct duration *d); +/* get a reading of duration and copy it without stopping the original timer */ +void duration_snapshot(struct duration *s, const struct duration *d); void duration_start(struct duration *d); void duration_stop(struct duration *d); /* read duration */ diff --git a/src/time/cc_timer_darwin.c b/src/time/cc_timer_darwin.c index a4984f9f1..3127643be 100644 --- a/src/time/cc_timer_darwin.c +++ b/src/time/cc_timer_darwin.c @@ -52,6 +52,17 @@ duration_reset(struct duration *d) d->stop = 0; } +void +duration_snapshot(struct duration *s, const struct duration *d) +{ + ASSERT(s != 0 && d != NULL); + + s->started = true; + s->start = d->start; + s->stopped = true; + s->stop = mach_absolute_time(); +} + void duration_start(struct duration *d) { diff --git a/src/time/cc_timer_linux.c b/src/time/cc_timer_linux.c index 1b8dddb26..778fedf98 100644 --- a/src/time/cc_timer_linux.c +++ b/src/time/cc_timer_linux.c @@ -80,6 +80,17 @@ duration_start(struct duration *d) d->started = true; } +void +duration_snapshot(struct duration *s, const struct duration *d) +{ + ASSERT(s != 0 && d != NULL); + + s->started = true; + s->start = d->start; + s->stopped = true; + _gettime(&s->stop); +} + void duration_stop(struct duration *d) { diff --git a/test/time/timer/check_timer.c b/test/time/timer/check_timer.c index de186a378..9822b475f 100644 --- a/test/time/timer/check_timer.c +++ b/test/time/timer/check_timer.c @@ -31,18 +31,25 @@ START_TEST(test_duration) { #define DURATION_NS 100000 - struct duration d; - double d_ns, d_us, d_ms, d_sec; + struct duration d, s; + double d_ns, d_us, d_ms, d_sec, s_ns; struct timespec ts = (struct timespec){0, DURATION_NS}; duration_reset(&d); duration_start(&d); + nanosleep(&ts, NULL); + duration_snapshot(&s, &d); + + /* snapshot is as expected */ + s_ns = duration_ns(&s); + ck_assert_uint_ge((unsigned int)s_ns, DURATION_NS); + nanosleep(&ts, NULL); duration_stop(&d); - /* duration is as expected */ + /* final duration is as expected */ d_ns = duration_ns(&d); - ck_assert_uint_ge((unsigned int)d_ns, DURATION_NS); + ck_assert_uint_ge((unsigned int)d_ns, 2 * DURATION_NS); /* readings of different units are consistent */ d_us = duration_us(&d);