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
/*
 * Copyright (C) 2014 Philippe Gerum <rpm@xenomai.org>
 *
 * 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 <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <limits.h>
#include <time.h>
#include <signal.h>
#include <error.h>
#include <sys/cobalt.h>
#include <rtdm/autotune.h>
#include <xenomai/init.h>
 
static int tune_irqlat, tune_kernlat, tune_userlat;
 
static int reset, noload, background;
 
/*
 * --verbosity_level=0 means fully quiet, =1 means almost quiet.
 */
#define verbose (__base_setup_data.verbosity_level)
 
static const struct option base_options[] = {
   {
#define irq_opt        0
       .name = "irq",
       .has_arg = no_argument,
       .flag = &tune_irqlat,
       .val = 1
   },
   {
#define kernel_opt    1
       .name = "kernel",
       .has_arg = no_argument,
       .flag = &tune_kernlat,
       .val = 1
   },
   {
#define user_opt    2
       .name = "user",
       .has_arg = no_argument,
       .flag = &tune_userlat,
       .val = 1
   },
   {
#define reset_opt    3
       .name = "reset",
       .has_arg = no_argument,
       .flag = &reset,
       .val = 1
   },
   {
#define noload_opt    4
       .name = "noload",
       .has_arg = no_argument,
       .flag = &noload,
       .val = 1
   },
   {
#define period_opt    5
       .name = "period",
       .has_arg = required_argument,
   },
   {
#define background_opt    6
       .name = "background",
       .has_arg = no_argument,
       .flag = &background,
       .val = 1,
   },
   { /* Sentinel */ }
};
 
static void *sampler_thread(void *arg)
{
   int fd = (long)arg, ret, n = 0;
   __u64 timestamp = 0;
   struct timespec now;
 
   for (;;) {
       ret = ioctl(fd, AUTOTUNE_RTIOC_PULSE, &timestamp);
       if (ret) {
           if (errno != EPIPE)
               error(1, errno, "pulse failed");
           timestamp = 0; /* Next tuning period. */
           n = 0;
       } else {
           n++;
           clock_gettime(CLOCK_MONOTONIC, &now);
           timestamp = (__u64)now.tv_sec * 1000000000 + now.tv_nsec;
       }
   }
 
   return NULL;
}
 
static void *load_thread(void *arg)
{
   int fdi, fdo, count = 0, wakelim;
   ssize_t nbytes, ret;
   struct timespec rqt;
   char buf[512];
 
   fdi = open("/dev/zero", O_RDONLY);
   if (fdi < 0)
       error(1, errno, "/dev/zero");
 
   fdo = open("/dev/null", O_WRONLY);
   if (fdi < 0)
       error(1, errno, "/dev/null");
 
   rqt.tv_sec = 0;
   rqt.tv_nsec = CONFIG_XENO_DEFAULT_PERIOD * 2;
   wakelim = 20000000 / rqt.tv_nsec;
 
   for (;;) {
       clock_nanosleep(CLOCK_MONOTONIC, 0, &rqt, NULL);
 
       if ((++count % wakelim) == 0) {
           cobalt_thread_relax();
           continue;
       }
 
       nbytes = read(fdi, buf, sizeof(buf));
       if (nbytes <= 0)
           error(1, EIO, "load streaming");
       if (nbytes > 0) {
           ret = write(fdo, buf, nbytes);
           (void)ret;
       }
   }
 
   return NULL;
}
 
static void create_sampler(pthread_t *tid, int fd)
{
   struct sched_param param;
   pthread_attr_t attr;
   int ret;
 
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
   pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
   param.sched_priority = 99;
   pthread_attr_setschedparam(&attr, &param);
   ret = pthread_create(tid, &attr, sampler_thread, (void *)(long)fd);
   if (ret)
       error(1, ret, "sampling thread");
 
   pthread_attr_destroy(&attr);
   pthread_setname_np(*tid, "sampler");
}
 
static void create_load(pthread_t *tid)
{
   struct sched_param param;
   pthread_attr_t attr;
   int ret;
 
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
   pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
   param.sched_priority = 1;
   pthread_attr_setschedparam(&attr, &param);
   ret = pthread_create(tid, &attr, load_thread, NULL);
   if (ret)
       error(1, ret, "load thread");
 
   pthread_attr_destroy(&attr);
   pthread_setname_np(*tid, "loadgen");
}
 
void application_usage(void)
{
        fprintf(stderr, "usage: %s [options]:\n", get_program_name());
   fprintf(stderr, "--irq                tune for interrupt latency\n");
   fprintf(stderr, "--kernel            tune for kernel scheduling latency\n");
   fprintf(stderr, "--user                tune for user scheduling latency\n");
   fprintf(stderr, "    [ if none of --irq, --kernel and --user is given,\n"
                  "      tune for all contexts ]\n");
   fprintf(stderr, "--period            set the sampling period\n");
   fprintf(stderr, "--reset             reset core timer gravity to factory defaults\n");
   fprintf(stderr, "--noload            disable load generation\n");
   fprintf(stderr, "--background             run in the background\n");
}
 
static void run_tuner(int fd, unsigned int op, int period, const char *type)
{
   struct autotune_setup setup;
   pthread_t sampler;
   __u32 gravity;
   int ret;
 
   setup.period = period;
   setup.quiet = verbose > 2 ? 0 : 2 - verbose;
   ret = ioctl(fd, op, &setup);
   if (ret)
       error(1, errno, "setup failed (%s)", type);
 
   if (verbose) {
       printf("%s gravity... ", type);
       fflush(stdout);
   }
 
   if (op == AUTOTUNE_RTIOC_USER)
       create_sampler(&sampler, fd);
 
   ret = ioctl(fd, AUTOTUNE_RTIOC_RUN, &gravity);
   if (ret)
       error(1, errno, "tuning failed (%s)", type);
 
   if (op == AUTOTUNE_RTIOC_USER)
       pthread_cancel(sampler);
 
   if (verbose)
       printf("%u ns\n", gravity);
}
 
int main(int argc, char *const argv[])
{
   int fd, period, ret, c, lindex, tuned = 0;
   pthread_t load_pth;
   cpu_set_t cpu_set;
   time_t start;
 
   period = CONFIG_XENO_DEFAULT_PERIOD;
 
   for (;;) {
       c = getopt_long_only(argc, argv, "", base_options, &lindex);
       if (c == EOF)
           break;
       if (c == '?') {
           xenomai_usage();
           return EINVAL;
       }
       if (c > 0)
           continue;
 
       switch (lindex) {
       case period_opt:
           period = atoi(optarg);
           if (period <= 0)
               error(1, EINVAL, "invalid sampling period (default %d)",
                     CONFIG_XENO_DEFAULT_PERIOD);
           break;
       case noload_opt:
       case background_opt:
           break;
       case irq_opt:
       case kernel_opt:
       case user_opt:
       case reset_opt:
           tuned = 1;
           break;
       default:
           return EINVAL;
       }
   }
 
   CPU_ZERO(&cpu_set);
   CPU_SET(0, &cpu_set);
   ret = sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
   if (ret)
       error(1, errno, "cannot set CPU affinity");
 
   if (background) {
       signal(SIGHUP, SIG_IGN);
       ret = daemon(0, 0);
       if (ret)
           error(1, errno, "cannot daemonize");
   }
 
   fd = open("/dev/rtdm/autotune", O_RDONLY);
   if (fd < 0)
       error(1, errno, "cannot open autotune device");
 
   if (!tuned)
       tune_irqlat = tune_kernlat = tune_userlat = 1;
 
   if (reset) {
       ret = ioctl(fd, AUTOTUNE_RTIOC_RESET);
       if (ret)
           error(1, errno, "reset failed");
   }
 
   if (tune_irqlat || tune_kernlat || tune_userlat) {
       if (!noload)
           create_load(&load_pth);
       if (verbose)
           printf("== auto-tuning started, period=%d ns (may take a while)\n",
                  period);
   } else
       noload = 1;
 
   time(&start);
 
   if (tune_irqlat)
       run_tuner(fd, AUTOTUNE_RTIOC_IRQ, period, "irq");
 
   if (tune_kernlat)
       run_tuner(fd, AUTOTUNE_RTIOC_KERN, period, "kernel");
 
   if (tune_userlat)
       run_tuner(fd, AUTOTUNE_RTIOC_USER, period, "user");
 
   if (verbose && (tune_userlat || tune_kernlat || tune_userlat))
       printf("== auto-tuning completed after %ds\n",
              (int)(time(NULL) - start));
 
   if (!noload)
       pthread_cancel(load_pth);
 
   close(fd);
 
   return 0;
}