-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
64 lines (53 loc) · 895 Bytes
/
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
#include <err.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include "glue_hdr.h"
static unsigned sample[SAMPLE_COUNT];
static unsigned n_samples, sorted;
unsigned long
nsec(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000UL + ts.tv_nsec;
}
unsigned
elapsed(unsigned long start)
{
return nsec() - start;
}
bool
stat_add(unsigned s)
{
if (n_samples == SAMPLE_COUNT)
return false;
sample[n_samples++] = s;
return true;
}
static int
cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
static void
sort_samples(void)
{
if (sorted == n_samples)
return;
qsort(sample, n_samples, sizeof(sample[0]), cmp);
sorted = n_samples;
}
unsigned
stat_pct(float pct)
{
int idx;
idx = (pct / 100.0) * (n_samples - 1);
sort_samples();
return sample[idx];
}
unsigned
stat_count(void)
{
return n_samples;
}