-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimemeasure.c
84 lines (73 loc) · 2.44 KB
/
timemeasure.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
75
76
77
78
79
80
81
82
83
84
#include <time.h>
#include <stdio.h>
#include "timemeasure.h"
#ifndef DISABLE_MEASUREMENT
#define MAX_NESTED 32
#define CPU_LIMIT 0.000
#define REAL_LIMIT 0.000
static unsigned int _nestedLevel = 0;
static struct timespec _cpuStart[MAX_NESTED];
static struct timespec _realStart[MAX_NESTED];
void _tic(char const *fileName, int lineNumber, char const *callerName)
{
_tic_msg(fileName, lineNumber, callerName, NULL);
}
void _tic_msg(char const *fileName, int lineNumber, char const *callerName, const char *msg)
{
if (_nestedLevel < MAX_NESTED)
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &_cpuStart[_nestedLevel]);
clock_gettime(CLOCK_REALTIME, &_realStart[_nestedLevel]);
}
else
{
printf("tic: Maximum nested depth reached.\n");
}
_nestedLevel++;
if (msg != NULL)
{
printf("%s:%s:%d, level %d, start - %s\n", fileName, callerName, lineNumber, _nestedLevel, msg);
}
}
void _toc(char const *fileName, int lineNumber, char const *callerName)
{
_toc_msg(fileName, lineNumber, callerName, NULL);
}
void _toc_msg(char const *fileName, int lineNumber, char const *callerName, const char *msg)
{
if (_nestedLevel < MAX_NESTED +1 && _nestedLevel > 0)
{
struct timespec cpuStop;
struct timespec realStop;
double elapsedCpu;
double elapsedReal;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cpuStop);
clock_gettime(CLOCK_REALTIME, &realStop);
elapsedCpu = (cpuStop.tv_sec - _cpuStart[_nestedLevel-1].tv_sec) +
(cpuStop.tv_nsec - _cpuStart[_nestedLevel-1].tv_nsec) / 1e9;
elapsedReal = (realStop.tv_sec - _realStart[_nestedLevel-1].tv_sec) +
(realStop.tv_nsec - _realStart[_nestedLevel-1].tv_nsec) / 1e9;
if (elapsedCpu > CPU_LIMIT || elapsedReal > REAL_LIMIT)
{
if (msg != NULL)
{
printf("%s:%s:%d, level %d, %.3f ms (real), %.3f ms (cpu) - %s\n",
fileName, callerName, lineNumber, _nestedLevel, elapsedReal, elapsedCpu, msg);
}
else
{
printf("%s:%s:%d, level %d, %.3f ms (real), %.3f ms (cpu)\n",
fileName, callerName, lineNumber, _nestedLevel, elapsedReal, elapsedCpu);
}
}
}
else
{
printf("toc: Outside max nested depth.\n");
}
if (_nestedLevel > 0)
{
_nestedLevel--;
}
}
#endif