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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*---------------------------------------------------------------
* Copyright (c) 2014
* Broadcom Corporation
* All Rights Reserved.
*---------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and
* the following disclaimers.
*
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimers in the documentation and/or other materials
* provided with the distribution.
*
*
* Neither the name of Broadcom Coporation,
* nor the names of its contributors may be used to endorse
* or promote products derived from this Software without
* specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ________________________________________________________________
*
* checkdelay.c
* Simple tool to measure mean/min/max of nanosleep
* by Robert J. McMahon (rjmcmahon@rjmcmahon.com, rmcmahon@broadcom.com)
* ------------------------------------------------------------------- */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>
#include "headers.h"
#include "util.h"
#include "delay.h"
#if HAVE_SCHED_SETSCHEDULER
#include <sched.h>
#ifdef HAVE_MLOCKALL
#include <sys/mman.h>
#endif
#endif
#define BILLION 1000000000
#define MILLION 1000000
int main (int argc, char **argv) {
double sum=0;
double time1, time2;
double delta, max=0, min=-1;
int ix, delay=1,loopcount=1000;
int c;
int clockgettime = 0, kalman = 0;
#if HAVE_DECL_CPU_SET
int affinity = 0;
#endif
#if HAVE_SCHED_SETSCHEDULER
int realtime = 0;
struct sched_param sp;
#endif
#ifdef HAVE_CLOCK_GETTIME
struct timespec t1;
#else
struct timeval t1;
#endif
while ((c=getopt(argc, argv, "a:bkd:i:r")) != -1)
switch (c) {
case 'b':
clockgettime = 1;
break;
case 'k':
kalman = 1;
break;
case 'd':
delay = atoi(optarg);
break;
case 'i':
loopcount = atoi(optarg);
break;
#if HAVE_DECL_CPU_SET
case 'a':
affinity=atoi(optarg);
break;
#endif
#if HAVE_SCHED_SETSCHEDULER
case 'r':
realtime = 1;
break;
#endif
case '?':
fprintf(stderr,"Usage -b busyloop, -d usec delay, -i iterations"
#if HAVE_DECL_CPU_SET
", -a affinity"
#endif
#if HAVE_SCHED_SETSCHEDULER
", -r realtime"
#endif
"\n");
return 1;
default:
abort();
}
#ifndef HAVE_NANOSLEEP
clockgettime = 1;
#endif
#if HAVE_SCHED_SETSCHEDULER
if (realtime) {
fprintf(stdout,"Setting scheduler to realtime via SCHED_RR\n");
// SCHED_OTHER, SCHED_FIFO, SCHED_RR
sp.sched_priority = sched_get_priority_max(SCHED_RR);
WARN_errno(sched_setscheduler(0, SCHED_RR, &sp) < 0,
"Client set scheduler");
#ifdef HAVE_MLOCKALL
// lock the threads memory
WARN_errno(mlockall(MCL_CURRENT | MCL_FUTURE) != 0, "mlockall");
#endif
}
#if HAVE_DECL_CPU_SET
if (affinity) {
fprintf(stdout,"CPU affinity set to %d\n", affinity);
cpu_set_t myset;
CPU_ZERO(&myset);
CPU_SET(affinity,&myset);
}
#endif
#endif
if (loopcount > 1000)
fprintf(stdout,"Measuring %s over %.0e iterations using %d usec delay\n",
kalman ? "kalman" :
clockgettime ? "clock_gettime" : "nanosleep",
(double) loopcount, delay);
else
fprintf(stdout,"Measuring %s over %d iterations using %d usec delay\n",
kalman ? "kalman" :
clockgettime ? "clock_gettime" : "nanosleep",
loopcount, delay);
fflush(stdout);
for (ix=0; ix < loopcount; ix++) {
// Find the max jitter for delay call
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &t1);
time1 = t1.tv_sec + (t1.tv_nsec / 1000000000.0);
#else
gettimeofday( &t1, NULL );
time1 = t1.tv_sec + (t1.tv_usec / 1000000.0);
#endif
#ifdef HAVE_KALMAN
if (kalman) {
delay_kalman(delay);
} else
#endif
if (clockgettime) {
delay_busyloop(delay);
} else {
#ifdef HAVE_NANOSLEEP
delay_nanosleep(delay);
#endif
}
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &t1);
time2 = t1.tv_sec + (t1.tv_nsec / 1000000000.0);
#else
gettimeofday( &t1, NULL );
time2 = t1.tv_sec + (t1.tv_usec / 1000000.0);
#endif
delta = (time2 - time1) * 1e6;
if (delta > max) {
max = delta;
}
if (delta < min || min < 0) {
min = delta;
}
sum += (double) delta;
}
fprintf(stdout,"delay=%.03f/%.03f/%.03f usec (mean/min/max)\n",
(sum / loopcount), min, max);
return(0);
}
|