-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
74 lines (68 loc) · 1.69 KB
/
timer.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* -*- mode: C; mode: folding; fill-column: 70; -*- */
/* Copyright 2010-2011, Georgia Institute of Technology, USA. */
/* See COPYING for license. */
#include "compat.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#if defined(__MTA__)
#include <sys/mta_task.h>
long tic_ts;
#elif defined(__MacOSX__)
static AbsoluteTime tic_ts;
#elif defined(HAVE_MACH_ABSOLUTE_TIME)
#include <mach/mach_time.h>
static uint64_t tic_ts;
#else
#if defined(CLOCK_MONOTONIC)
#define TICTOC_CLOCK CLOCK_MONOTONIC
#define TICTOC_CLOCK_NAME "CLOCK_MONOTONIC"
#elif defined(CLOCK_REALTIME)
#define TICTOC_CLOCK CLOCK_REALTIME
#define TICTOC_CLOCK_NAME "CLOCK_REALTIME"
#else
#error "Failed to find a timing clock."
#endif
static struct timespec tic_ts;
#endif
void
tic (void)
{
#if defined(__MTA__)
MTA("mta fence")
tic_ts = mta_get_clock (0);
#elif defined(HAVE_MACH_ABSOLUTE_TIME)
tic_ts = mach_absolute_time();
#else
clock_gettime (TICTOC_CLOCK, &tic_ts);
#endif
}
double
toc (void)
{
double out;
#if defined(__MTA__)
long ts;
MTA("mta fence")
ts = mta_get_clock (tic_ts);
out = ((double)ts) * mta_clock_period ();
/*fprintf (stderr, "%ld %g %g %g\n", ts, out, mta_clock_period(), mta_clock_freq());*/
#elif defined(HAVE_MACH_ABSOLUTE_TIME)
uint64_t ts, nanosec;
static mach_timebase_info_data_t info = {0,0};
if (info.denom == 0) {
mach_timebase_info(&info);
}
ts = mach_absolute_time();
nanosec = (ts - tic_ts) * (info.numer / info.denom);
out = 1.0e-9 * nanosec;
#else
struct timespec ts;
clock_gettime (TICTOC_CLOCK, &ts);
out = (ts.tv_nsec - (double)tic_ts.tv_nsec) * 1.0e-9;
out += (ts.tv_sec - (double)tic_ts.tv_sec);
#endif
return out;
}