-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiming_a.c
138 lines (107 loc) · 2.65 KB
/
timing_a.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* timing_a.c
*
* Timing the process with SIGALRM and itmer.
*
* 2016-12-11 Frank Jeschke <[email protected]>
*/
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 700
#endif /* __STDC_VERSION__ */
#include "commons.h"
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
static unsigned int g_sig_alarm = 0;
static int g_running;
static int setup_timer(struct itimerval *tv)
{
tv->it_interval.tv_sec = 0;
tv->it_interval.tv_usec = 1000;
tv->it_value.tv_sec = 0;
tv->it_value.tv_usec = 1000;
if (setitimer(ITIMER_REAL, tv, NULL)) {
fprintf(stderr, "Error setup itimer\n");
return -1;
}
return 0;
}
void signal_handler(int signum)
{
switch (signum) {
case SIGALRM:
g_sig_alarm++;
break;
case SIGINT:
g_running = 0;
break;
}
}
static int setup_sig_handler(struct sigaction *sa)
{
sa->sa_handler = signal_handler;
sigemptyset(&(sa->sa_mask));
sa->sa_flags = 0;
if (sigaction(SIGALRM, sa, 0)) {
fprintf(stderr, "Error setup signal handler\n");
return -1;
}
if (sigaction(SIGINT, sa, 0)) {
fprintf(stderr, "Error setup signal handler\n");
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
struct sigaction sa;
struct itimerval tv;
if (setup_sig_handler(&sa))
return -1;
if (setup_timer(&tv))
return -1;
g_running = 1;
clock_t start = clock();
unsigned int user_alarm = 0;
struct timespec previous;
struct timespec current;
struct timespec difference;
double mean = 0; /* mean value in nanoseconds (ns) */
size_t mean_count = 0;
double max = 0, min = 1000000000;
const size_t MaxDifference = 1500000;
size_t values_miss = 0;
clock_gettime(CLOCK_MONOTONIC, &previous);
while (g_running) {
pause();
while (g_sig_alarm != user_alarm) {
user_alarm++;
clock_gettime(CLOCK_MONOTONIC, ¤t);
timespec_diff(&previous, ¤t, &difference);
double cur = (double)difference.tv_nsec;
mean = calc_mean(mean, cur, mean_count++);
max = (cur > max) ? cur : max;
min = (cur < min) ? cur : min;
if (difference.tv_nsec > MaxDifference) {
// printf("%u: %u\n", user_alarm, difference.tv_nsec / 1000);
values_miss++;
}
previous.tv_sec = current.tv_sec;
previous.tv_nsec = current.tv_nsec;
}
}
clock_t end = clock();
double runtime = (double)(end - start) / CLOCKS_PER_SEC;
printf("Summary:\n");
printf("Processortime: %f s\n", runtime);
printf("mean: %f ns; min: %f ns; max: %f ns\n", mean, min, max);
printf(" on %u iterations with %u values larger %d ns\n", mean_count, values_miss, MaxDifference);
return 0;
}