File: timer.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (32 lines) | stat: -rw-r--r-- 684 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <limits.h>

#include "assert.h"
#include "timer.h"

/* Save current time in 'time'. */
static void timerNow(struct timespec *time)
{
    int rv;
    rv = clock_gettime(CLOCK_MONOTONIC, time);
    assert(rv == 0);
}

void TimerStart(struct timer *timer)
{
    timerNow(&timer->start);
}

unsigned long TimerStop(struct timer *timer)
{
    struct timespec now;
    time_t nsecs;
    timerNow(&now);
    if (timer->start.tv_sec == now.tv_sec) {
        nsecs = now.tv_nsec - timer->start.tv_nsec;
    } else {
        nsecs = (now.tv_sec - timer->start.tv_sec) * 1000 * 1000 * 1000 -
                timer->start.tv_nsec + now.tv_nsec;
    }

    return (unsigned long)nsecs;
}