forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimerjitter.c
340 lines (277 loc) · 8.21 KB
/
timerjitter.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/****************************************************************************
* apps/testing/sched/timerjitter/timerjitter.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define DEFAULT_CLOCKID CLOCK_REALTIME
#define DEFAULT_INTERVAL 1000
#define DEFAULT_ITERATION 1000
/* Fix compilation error for Non-NuttX OS */
#ifndef FAR
#define FAR
#endif
#ifndef USEC_PER_SEC
#define USEC_PER_SEC 1000000
#endif
#ifndef NSEC_PER_SEC
#define NSEC_PER_SEC 1000000000
#endif
/****************************************************************************
* Private Type
****************************************************************************/
struct timerjitter_param_s
{
clockid_t clockid;
unsigned int interval;
unsigned long max_cnt;
unsigned long cur_cnt;
double avg;
unsigned long max;
unsigned long min;
int print;
unsigned int missed;
};
/****************************************************************************
* Private Functions
****************************************************************************/
/* Helper functions for timespec calculating */
static inline int64_t calc_diff(FAR const struct timespec *t1,
FAR const struct timespec *t2)
{
int64_t diff;
diff = USEC_PER_SEC * (t1->tv_sec - t2->tv_sec);
diff += (t1->tv_nsec - t2->tv_nsec) / 1000;
return diff;
}
static inline void ts_norm(FAR struct timespec *ts)
{
while (ts->tv_nsec >= NSEC_PER_SEC)
{
ts->tv_nsec -= NSEC_PER_SEC;
ts->tv_sec++;
}
}
static inline int ts_greater(FAR const struct timespec *a,
FAR const struct timespec *b)
{
return (a->tv_sec > b->tv_sec) ||
(a->tv_sec == b->tv_sec && a->tv_nsec > b->tv_nsec);
}
static inline void calc_next(FAR struct timespec *t,
FAR struct timespec *intv)
{
t->tv_sec += intv->tv_sec;
t->tv_nsec += intv->tv_nsec;
ts_norm(t);
}
/* Helper function for number parsing */
static unsigned long get_num(FAR const char *str)
{
FAR char *end;
unsigned long val;
if (!str)
{
return 0;
}
val = strtoul(str, &end, 0);
if (!val || val == (unsigned long)-1)
{
return 0;
}
return val;
}
static FAR void *timerjitter(FAR void *arg)
{
FAR struct timerjitter_param_s *param = arg;
struct timespec now;
struct timespec next;
struct timespec intv;
struct itimerspec tspec;
struct sigevent sigev;
sigset_t sigset;
timer_t timer;
int64_t diff;
int sigs;
int ret;
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM);
sigprocmask(SIG_BLOCK, &sigset, NULL);
intv.tv_sec = param->interval / USEC_PER_SEC;
intv.tv_nsec = (param->interval % USEC_PER_SEC) * 1000;
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = SIGALRM;
timer_create(param->clockid, &sigev, &timer);
clock_gettime(param->clockid, &now);
next = now;
calc_next(&next, &intv);
/* Set cyclic timer */
tspec.it_interval = intv;
/* Using TIMER_ABSTIME */
tspec.it_value = next;
timer_settime(timer, TIMER_ABSTIME, &tspec, NULL);
param->avg = 0;
param->max = 0;
param->min = (unsigned long)-1;
while (param->cur_cnt++ < param->max_cnt)
{
/* Wait for SIGALRM */
if (sigwait(&sigset, &sigs) < 0)
{
printf("sig wait failed\n");
break;
}
ret = clock_gettime(param->clockid, &now);
if (ret)
{
printf("clock_gettime failed %d\n", ret);
}
diff = calc_diff(&now, &next);
if (param->print)
{
printf("diff %lu, now %lu.%lu\n", diff, now.tv_sec, now.tv_nsec);
}
if (diff > param->max)
{
param->max = diff;
}
if (diff < param->min)
{
param->min = diff;
}
param->avg += diff;
/* Calculate next = next + intv */
calc_next(&next, &intv);
/* Calibrate if we miss current time frame */
while (ts_greater(&now, &next))
{
calc_next(&next, &intv);
printf("time frame missed %u\n", ++param->missed);
}
}
param->avg = param->avg / param->cur_cnt;
return NULL;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* timerjitter main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
struct timerjitter_param_s param =
{
.clockid = DEFAULT_CLOCKID,
.interval = DEFAULT_INTERVAL,
.max_cnt = DEFAULT_ITERATION,
.cur_cnt = 0,
.print = 0,
.missed = 0
};
pthread_attr_t attr;
pthread_t thread;
sigset_t sigset;
FAR const char *arg;
int ret;
/* Mask SIGALRM at first */
sigemptyset(&sigset);
sigaddset(&sigset, SIGALRM);
sigprocmask(SIG_BLOCK, &sigset, NULL);
if (argc > 1)
{
while ((arg = argv[1]) != NULL)
{
if (*arg != '-')
{
break;
}
for (; ; )
{
switch (*++arg)
{
case 0:
break;
case 'p':
param.print = 1;
continue;
case 'm':
param.clockid = CLOCK_MONOTONIC;
continue;
case 'r':
param.clockid = CLOCK_REALTIME;
continue;
case 'h':
printf(
"usage: timerjitter [-pmr] [interval(us)] [iteration]\n"
"-p: print time diff between two iteration\n"
"-m: use CLOCK_MONOTONIC\n"
"-r: use CLOCK_REALTIME\n"
"");
return 0;
default:
printf("Unknown flag '%s'", arg);
return -1;
}
break;
}
/* Find next parameters */
argv++;
argc--;
}
if (argc > 1)
{
param.interval = get_num(argv[1]);
}
if (argc > 2)
{
param.max_cnt = get_num(argv[2]);
}
}
ret = pthread_attr_init(&attr);
if (ret)
{
printf("pthread_attr_init failed %d\n", ret);
}
ret = pthread_create(&thread, &attr, timerjitter, ¶m);
if (ret)
{
printf("thread created failed %d\n", ret);
}
pthread_join(thread, NULL);
ret = pthread_attr_destroy(&attr);
if (ret)
{
printf("pthread_attr_destroy failed %d\n", ret);
}
printf("timer jitter in %lu run:\n", param.max_cnt);
printf("(latency/us) min: %lu, avg: %.0lf, max %lu\n",
param.min, param.avg, param.max);
return 0;
}