Skip to content

Commit

Permalink
adding a new API to duration timer (#152)
Browse files Browse the repository at this point in the history
* adding a new API to duration timer

* adding const modifier to snapshot API
  • Loading branch information
Yao Yue authored May 19, 2018
1 parent 9406717 commit 727905b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions include/time/cc_timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
11 changes: 11 additions & 0 deletions src/time/cc_timer_darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
11 changes: 11 additions & 0 deletions src/time/cc_timer_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
15 changes: 11 additions & 4 deletions test/time/timer/check_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 727905b

Please sign in to comment.