hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * Copyright (C) 2007 Jan Kiszka <jan.kiszka@web.de>.
 *
 * Xenomai is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Xenomai is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Xenomai; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <errno.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <error.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <boilerplate/ancillaries.h>
#include <boilerplate/atomic.h>
#include <cobalt/uapi/kernel/vdso.h>
#include <xeno_config.h>
 
extern struct xnvdso *cobalt_vdso;
 
/*
 * We can't really trust POSIX headers to check for features, since
 * some archs may not implement all of the declared uClibc POSIX
 * features (e.g. NIOS2).
 */
#ifdef HAVE_PTHREAD_SPIN_LOCK
pthread_spinlock_t lock;
#define init_lock(lock)                pthread_spin_init(lock, 0)
#define acquire_lock(lock)            pthread_spin_lock(lock)
#define release_lock(lock)            pthread_spin_unlock(lock)
#else
pthread_mutex_t lock;
#define init_lock(lock)                pthread_mutex_init(lock, NULL)
#define acquire_lock(lock)            pthread_mutex_lock(lock)
#define release_lock(lock)            pthread_mutex_unlock(lock)
#endif
 
static uint64_t last_common = 0;
static clockid_t clock_id = CLOCK_REALTIME;
static cpu_set_t cpu_realtime_set, cpu_online_set;
 
struct per_cpu_data {
   uint64_t first_tod, first_clock;
   int first_round;
   int64_t offset;
   double drift;
   unsigned long warps;
   uint64_t max_warp;
   pthread_t thread;
} *per_cpu_data;
 
static void show_hostrt_diagnostics(void)
{
   if (!xnvdso_test_feature(cobalt_vdso, XNVDSO_FEAT_HOST_REALTIME)) {
       printf("XNVDSO_FEAT_HOST_REALTIME not available\n");
       return;
   }
 
   if (cobalt_vdso->hostrt_data.live)
       printf("hostrt data area is live\n");
   else {
       printf("hostrt data area is not live\n");
       return;
   }
 
   printf("sequence counter : %u\n",
          cobalt_vdso->hostrt_data.lock.sequence);
   printf("wall_time_sec    : %lld\n",
          (unsigned long long)cobalt_vdso->hostrt_data.wall_sec);
   printf("wall_time_nsec   : %u\n", cobalt_vdso->hostrt_data.wall_nsec);
   printf("wall_to_monotonic_sec    : %lld\n",
          (unsigned long long)cobalt_vdso->hostrt_data.wtom_sec);
   printf("wall_to_monotonic_nsec   : %u\n", cobalt_vdso->hostrt_data.wtom_nsec);
   printf("cycle_last       : %Lu\n", (long long)cobalt_vdso->hostrt_data.cycle_last);
   printf("mask             : 0x%Lx\n", (long long)cobalt_vdso->hostrt_data.mask);
   printf("mult             : %u\n", cobalt_vdso->hostrt_data.mult);
   printf("shift            : %u\n\n", cobalt_vdso->hostrt_data.shift);
}
 
static void show_realtime_offset(void)
{
   if (!xnvdso_test_feature(cobalt_vdso, XNVDSO_FEAT_HOST_REALTIME)) {
       printf("XNVDSO_FEAT_WALLCLOCK_OFFSET not available\n");
       return;
   }
 
   printf("Wallclock offset : %llu\n", (long long)cobalt_vdso->wallclock_offset);
}
 
static inline uint64_t read_clock(clockid_t clock_id)
{
   struct timespec ts;
   int res;
 
   res = clock_gettime(clock_id, &ts);
   if (res != 0) {
       fprintf(stderr, "clock_gettime failed for clock id %d\n",
           clock_id);
       if (clock_id == CLOCK_HOST_REALTIME)
           show_hostrt_diagnostics();
       else if (clock_id == CLOCK_REALTIME)
           show_realtime_offset();
 
       exit(-1);
   }
   return ts.tv_nsec + ts.tv_sec * 1000000000ULL;
}
 
static inline uint64_t read_reference_clock(void)
{
   struct timeval tv;
 
   /*
    * Make sure we do not pick the vsyscall variant. It won't
    * switch us into secondary mode and can easily deadlock.
    */
   syscall(SYS_gettimeofday, &tv, NULL);
   return tv.tv_usec * 1000ULL + tv.tv_sec * 1000000000ULL;
}
 
static void check_reference(struct per_cpu_data *per_cpu_data)
{
   uint64_t clock_val[10], tod_val[10];
   int64_t delta, min_delta;
   int i, idx;
 
   for (i = 0; i < 10; i++) {
       tod_val[i] = read_reference_clock();
       clock_val[i] = read_clock(clock_id);
   }
 
   min_delta = tod_val[1] - tod_val[0];
   idx = 1;
 
   for (i = 2; i < 10; i++) {
       delta = tod_val[i] - tod_val[i-1];
       if (delta < min_delta) {
           min_delta = delta;
           idx = i;
       }
   }
 
   if (per_cpu_data->first_round) {
       per_cpu_data->first_round = 0;
 
       per_cpu_data->first_tod = tod_val[idx];
       per_cpu_data->first_clock = clock_val[idx];
   } else
       per_cpu_data->drift =
           (clock_val[idx] - per_cpu_data->first_clock) /
           (double)(tod_val[idx] - per_cpu_data->first_tod) - 1;
 
   per_cpu_data->offset = clock_val[idx] - tod_val[idx];
}
 
static void check_time_warps(struct per_cpu_data *per_cpu_data)
{
   int i;
   uint64_t last, now;
   int64_t incr;
 
   for (i = 0; i < 100; i++) {
       acquire_lock(&lock);
       now = read_clock(clock_id);
       last = last_common;
       last_common = now;
       release_lock(&lock);
 
       incr = now - last;
       if (incr < 0) {
           acquire_lock(&lock);
           per_cpu_data->warps++;
           if (-incr > per_cpu_data->max_warp)
               per_cpu_data->max_warp = -incr;
           release_lock(&lock);
       }
   }
}
 
static void *cpu_thread(void *arg)
{
   int cpuid = (long)arg;
   struct sched_param param = { .sched_priority = 1 };
   struct timespec delay = { 0, 0 };
   cpu_set_t cpu_set;
 
   srandom(read_reference_clock());
 
   CPU_ZERO(&cpu_set);
   CPU_SET(cpuid, &cpu_set);
   sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
   pthread_setschedparam(pthread_self(), SCHED_FIFO, &param);
 
   while (1) {
       check_reference(&per_cpu_data[cpuid]);
 
       check_time_warps(&per_cpu_data[cpuid]);
 
       delay.tv_nsec = 1000000 + random() * (100000.0 / RAND_MAX);
       nanosleep(&delay, NULL);
   }
 
   return NULL;
}
 
static void sighand(int signal)
{
   exit(0);
}
 
static clockid_t resolve_clock_name(const char *name,
                   const char **real_name, int ext)
{
   char *path, buf[BUFSIZ];
   clockid_t clock_id;
   FILE *fp;
   int id;
 
   *real_name = name;
 
   if (!ext) {
       if (isdigit(*name)) {
           clock_id = (clockid_t)atoi(name);
           switch (clock_id) {
           case CLOCK_REALTIME:
               *real_name = "CLOCK_REALTIME";
               break;
           case CLOCK_MONOTONIC:
               *real_name = "CLOCK_MONOTONIC";
               break;
           case CLOCK_MONOTONIC_RAW:
               *real_name = "CLOCK_MONOTONIC_RAW";
               break;
           case CLOCK_HOST_REALTIME:
               *real_name = "CLOCK_HOST_REALTIME";
               break;
           default:
               error(1, EINVAL, "bad built-in clock id '%d'",
                     clock_id);
           }
           return clock_id;
       }
       if (strcmp(name, "CLOCK_REALTIME") == 0)
           return CLOCK_REALTIME;
       if (strcmp(name, "CLOCK_MONOTONIC") == 0)
           return CLOCK_MONOTONIC;
       if (strcmp(name, "CLOCK_MONOTONIC_RAW") == 0)
           return CLOCK_MONOTONIC_RAW;
       if (strcmp(name, "CLOCK_HOST_REALTIME") == 0)
           return CLOCK_HOST_REALTIME;
       if (strcmp(name, "coreclk") == 0) {
           /* Read the core clock as CLOCK_MONOTONIC_RAW */
           *real_name = "CLOCK_MONOTONIC_RAW";
           return CLOCK_MONOTONIC_RAW;
       }
   }
 
   if (!isdigit(*name)) {
       if (asprintf(&path, "/proc/xenomai/clock/%s", name) < 0)
           error(1, ENOMEM, "resolve_clock_name");
       fp = fopen(path, "r");
       if (fp == NULL)
           error(1, EINVAL, "bad extension clock name '%s'", name);
       if (fgets(buf, sizeof(buf), fp) == NULL)
           error(1, EIO, "cannot read %s", path);
       free(path);
       if (sscanf(buf, "%*[ ]id: %d\n", &id) != 1)
           error(1, EINVAL, "bad extension clock name '%s'", name);
       return __COBALT_CLOCK_EXT(id);
   }
 
   id = atoi(name);
   if (!__COBALT_CLOCK_EXT_P(id) ||
       __COBALT_CLOCK_EXT_INDEX(id) >= COBALT_MAX_EXTCLOCKS)
       error(1, EINVAL, "bad extension clock id '%d'", id);
       
   *real_name = "CLOCK_UNKNOWN";
 
   return __COBALT_CLOCK_EXT(id);
}
 
int main(int argc, char *argv[])
{
   const char *clock_name = NULL, *real_clock_name = "CLOCK_REALTIME";
   int max_cpu, cpus;
   int i;
   int c;
   int d = 0;
   int ext = 0;
 
   while ((c = getopt(argc, argv, "C:ET:D")) != EOF)
       switch (c) {
       case 'C':
           clock_name = optarg;
           break;
 
       case 'E':
           ext = 1;
           break;
 
       case 'T':
           alarm(atoi(optarg));
           break;
 
       case 'D':
           d = 1;
           break;
 
       default:
           fprintf(stderr, "usage: clocktest [options]\n"
               "  [-C <clock_id|clock_name>]   # tested clock, defaults to CLOCK_REALTIME\n"
               "  [-E]                         # -C specifies extension clock\n"
               "  [-T <test_duration_seconds>] # default=0, so ^C to end\n"
               "  [-D]                         # print extra diagnostics for CLOCK_HOST_REALTIME\n");
           exit(2);
       }
 
   if (clock_name)
       clock_id = resolve_clock_name(clock_name, &real_clock_name, ext);
 
   signal(SIGALRM, sighand);
 
   init_lock(&lock);
 
   if (d && clock_id == CLOCK_HOST_REALTIME)
       show_hostrt_diagnostics();
 
   if (get_realtime_cpu_set(&cpu_realtime_set) != 0)
       error(1, ENOSYS, "get_realtime_cpu_set");
 
   if (get_online_cpu_set(&cpu_online_set) != 0)
       error(1, ENOSYS, "get_online_cpu_set");
 
   CPU_AND(&cpu_realtime_set, &cpu_realtime_set, &cpu_online_set);
 
   max_cpu = 0;
   cpus = 0;
   for (i = 0; i < CPU_SETSIZE; i++) {
       if (!CPU_ISSET(i, &cpu_realtime_set))
           continue;
       cpus++;
       if (i > max_cpu)
           max_cpu = i;
   }
 
   per_cpu_data = malloc(sizeof(*per_cpu_data) * (max_cpu + 1));
   if (per_cpu_data == NULL)
       error(1, ENOMEM, "malloc");
 
   memset(per_cpu_data, 0, sizeof(*per_cpu_data) * (max_cpu + 1));
 
   for (i = 0; i <= max_cpu; i++) {
       if (!CPU_ISSET(i, &cpu_realtime_set))
           continue;
       per_cpu_data[i].first_round = 1;
       pthread_create(&per_cpu_data[i].thread, NULL, cpu_thread,
                  (void *)(long)i);
   }
 
   printf("== Testing %s %s (%d)\n",
          ext ? "extension" : "built-in", real_clock_name, clock_id);
   printf("CPU      ToD offset [us] ToD drift [us/s]      warps max delta [us]\n"
          "--- -------------------- ---------------- ---------- --------------\n");
 
   while (1) {
       for (i = 0; i <= max_cpu; i++)
           if (CPU_ISSET(i, &cpu_realtime_set))
               printf("%3d %20.1f %16.3f %10lu %14.1f\n",
                      i,
                      per_cpu_data[i].offset/1000.0,
                      per_cpu_data[i].drift * 1000000.0,
                      per_cpu_data[i].warps,
                      per_cpu_data[i].max_warp/1000.0);
       usleep(250000);
       printf("\033[%dA", cpus);
   }
}