Repository navigation
Expand file tree
/
Copy pathbenchmark.c
More file actions
116 lines (93 loc) · 2.75 KB
/
Copy pathbenchmark.c
File metadata and controls
116 lines (93 loc) · 2.75 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "cinterplot_common.h"
#include "benchmark.h"
#define nanosec() clock_gettime_nsec_np (CLOCK_MONOTONIC_RAW);
typedef struct BenchmarkCheckpoint
{
uint64_t tsp;
char *str;
uint64_t elapsed;
} BenchmarkCheckpoint;
typedef struct BenchmarkData
{
int checkpointIdx;
uint64_t startTsp;
uint64_t stopTsp;
BenchmarkCheckpoint checkpoints[MAX_NUM_CHECKPOINTS];
int lineNumberToIdx[MAX_NUM_LINES];
int isRunning;
} BenchmarkData;
static BenchmarkData *bench = NULL;
void benchmark_create (void)
{
bench = calloc (1, sizeof (BenchmarkData));
assert (bench);
}
void benchmark_destroy (void)
{
if (bench)
free (bench);
bench = NULL;
}
void benchmark_start (int lineNumber, char *str)
{
assert (bench);
bzero (bench->lineNumberToIdx, sizeof (bench->lineNumberToIdx));
bzero (bench->checkpoints, sizeof (bench->checkpoints));
bench->isRunning = 1;
bench->checkpointIdx = 0;
bench->checkpoints[0].tsp = nanosec ();
bench->checkpoints[0].str = str;
}
void benchmark_add_checkpoint (int lineNumber, char *str)
{
if (bench->lineNumberToIdx[lineNumber] == 0)
bench->lineNumberToIdx[lineNumber] = bench->checkpointIdx + 1;
int nextIdx = bench->lineNumberToIdx[lineNumber];
uint64_t tsp = nanosec ();
BenchmarkCheckpoint *cp = & bench->checkpoints[bench->checkpointIdx];
cp->elapsed += tsp - cp->tsp;
bench->checkpointIdx = nextIdx;
bench->checkpoints[nextIdx].tsp = tsp;
bench->checkpoints[nextIdx].str = str;
}
void benchmark_stop (void)
{
if (!bench || !bench->isRunning)
{
//print_debug ("invalid call to stop");
return;
}
bench->isRunning = 0;
uint64_t tsp = nanosec ();
BenchmarkCheckpoint *cp = & bench->checkpoints[bench->checkpointIdx];
cp->elapsed += tsp - cp->tsp;
}
void benchmark_dump (void)
{
if (!bench || bench->isRunning)
print_debug ("dumping cancelled");
int numCheckpoints = bench->checkpointIdx + 1;
BenchmarkCheckpoint *cps = bench->checkpoints;
int maxlen = 0;
uint64_t totalElapsed = 0;
for (int ci=0; ci<numCheckpoints; ci++)
{
totalElapsed += cps[ci].elapsed;
char *str = cps[ci].str ? cps[ci].str : "(null)";
int len = strlen (str);
if (maxlen < len)
maxlen = len;
}
double pcntConversionFactor = 100.0 / totalElapsed;
for (int ci=0; ci<numCheckpoints; ci++)
{
char *str = cps[ci].str ? cps[ci].str : "(null)";
int len = strlen (str);
printf ("%s", str);
for (int i=0; i<(maxlen+4-len); i++)
putchar ('.');
printf ("%6.2f\n", cps[ci].elapsed * pcntConversionFactor);
}
double fps = 1e9 / totalElapsed;
printf ("fps: %0.2f\n", fps);
}