Debian Codegraph @

src/signaltest/signaltest.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 /*
4 * RT signal roundtrip test software
5 *
6 * (C) 2007 Thomas Gleixner <[email protected]>
7 *
8 */
9
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <pthread.h>
13 #include <signal.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <time.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <sched.h>
21 #include <inttypes.h>
22
23 #include <linux/unistd.h>
24
25 #include <sys/prctl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/time.h>
29 #include <sys/mman.h>
30
31 #include "rt-error.h"
32 #include "rt-utils.h"
33 #include "rt-numa.h"
34
35 /* Must be power of 2 ! */
36 #define VALBUF_SIZE 16384
37
38 /* Struct to transfer parameters to the thread */
39 struct thread_param {
40 int id;
41 int prio;
42 int signal;
43 unsigned long max_cycles;
44 struct thread_stat *stats;
45 int bufmsk;
46 int cpu;
47 };
48
49 /* Struct for statistics */
50 struct thread_stat {
51 unsigned long cycles;
52 unsigned long cyclesread;
53 long min;
54 long max;
55 long act;
56 double avg;
57 long *values;
58 pthread_t thread;
59 pthread_t tothread;
60 int threadstarted;
61 int tid;
62 int interrupted;
63 };
64
65 static int shutdown;
66 static int tracelimit;
67
68
69 /*
70 * signal thread
71 *
72 */
73 void *signalthread(void *param)
74 {
75 struct thread_param *par = param;
76 struct sched_param schedp;
77 sigset_t sigset;
78 struct timespec before, after;
79 struct thread_stat *stat = par->stats;
80 int policy = par->prio ? SCHED_FIFO : SCHED_OTHER;
81 int stopped = 0;
82 int first = 1;
83 pthread_t thread;
84 cpu_set_t mask;
85
86 stat->tid = gettid();
87
88 if (par->cpu != -1) {
89 CPU_ZERO(&mask);
90 CPU_SET(par->cpu, &mask);
91 thread = pthread_self();
92 if (pthread_setaffinity_np(thread, sizeof(mask), &mask) != 0)
93 warn("Could not set CPU affinity to CPU #%d\n",
94 par->cpu);
95 }
96
97 sigemptyset(&sigset);
98 sigaddset(&sigset, par->signal);
99 sigprocmask(SIG_BLOCK, &sigset, NULL);
100
101 memset(&schedp, 0, sizeof(schedp));
102 schedp.sched_priority = par->prio;
103 sched_setscheduler(0, policy, &schedp);
104
105 stat->threadstarted++;
106
107 clock_gettime(CLOCK_MONOTONIC, &before);
108
109 while (!shutdown) {
110 struct timespec now;
111 long diff;
112 int sigs;
113
114 if (sigwait(&sigset, &sigs) < 0)
115 goto out;
116
117 clock_gettime(CLOCK_MONOTONIC, &after);
118
119 /*
120 * If it is the first thread, sleep after every 16
121 * round trips.
122 */
123 if (!par->id && !(stat->cycles & 0x0F))
124 usleep(10000);
125
126 /* Get current time */
127 clock_gettime(CLOCK_MONOTONIC, &now);
128 pthread_kill(stat->tothread, SIGUSR1);
129
130 /* Skip the first cycle */
131 if (first) {
132 first = 0;
133 before = now;
134 continue;
135 }
136
137 diff = calcdiff(after, before);
138 before = now;
139
140 if (diff < stat->min)
141 stat->min = diff;
142 if (diff > stat->max)
143 stat->max = diff;
144 stat->avg += (double) diff;
145
146 if (!stopped && tracelimit && !par->id && (diff > tracelimit)) {
147 stat->act = diff;
148 stat->interrupted = 1;
149 stopped++;
150 shutdown++;
151 }
152 stat->act = diff;
153 stat->cycles++;
154
155 if (par->bufmsk)
156 stat->values[stat->cycles & par->bufmsk] = diff;
157
158 if (par->max_cycles && par->max_cycles == stat->cycles)
159 break;
160 }
161
162 out:
163 /* switch to normal */
164 schedp.sched_priority = 0;
165 sched_setscheduler(0, SCHED_OTHER, &schedp);
166
167 stat->threadstarted = -1;
168
169 return NULL;
170 }
171
172
173 /* Print usage information */
174 static void display_help(int error)
175 {
176 printf("signaltest V %1.2f\n", VERSION);
177 printf("Usage:\n"
178 "signaltest <options>\n\n"
179 "-a [NUM] --affinity run thread #N on processor #N, if possible\n"
180 " with NUM pin all threads to the processor NUM\n"
181 "-b USEC --breaktrace=USEC send break trace command when latency > USEC\n"
182 "-D --duration=TIME specify a length for the test run.\n"
183 " Append 'm', 'h', or 'd' to specify minutes, hours or\n"
184 " days.\n"
185 "-h --help display usage information\n"
186 " --json=FILENAME write final results into FILENAME, JSON formatted\n"
187 "-l LOOPS --loops=LOOPS number of loops: default=0(endless)\n"
188 "-m --mlockall lock current and future memory allocations\n"
189 "-p PRIO --prio=PRIO priority of highest prio thread\n"
190 "-q --quiet print a summary only on exit\n"
191 "-t NUM --threads=NUM number of threads: default=2\n"
192 "-v --verbose output values on stdout for statistics\n"
193 " format: n:c:v n=tasknum c=count v=value in us\n"
194 );
195 exit(error);
196 }
197
198 static int priority;
199 static int num_threads = 2;
200 static int max_cycles;
201 static int duration;
202 static int verbose;
203 static int quiet;
204 static int lockall;
205 static struct bitmask *affinity_mask = NULL;
206 static int smp = 0;
207 static int numa = 0;
208 static int setaffinity = AFFINITY_UNSPECIFIED;
209 static char jsonfile[MAX_PATH];
210
211 enum option_values {
212 OPT_AFFINITY=1, OPT_BREAKTRACE,
213 OPT_DURATION, OPT_HELP, OPT_JSON,
214 OPT_LOOPS, OPT_MLOCKALL, OPT_PRIORITY,
215 OPT_QUIET, OPT_SMP, OPT_THREADS, OPT_VERBOSE
216 };
217
218 /* Process commandline options */
219 static void process_options(int argc, char *argv[], unsigned int max_cpus)
220 {
221 int option_affinity = 0;
222 int error = 0;
223
224 for (;;) {
225 int option_index = 0;
226 /** Options for getopt */
227 static struct option long_options[] = {
228 {"affinity", optional_argument, NULL, OPT_AFFINITY},
229 {"breaktrace", required_argument, NULL, OPT_BREAKTRACE},
230 {"duration", required_argument, NULL, OPT_DURATION},
231 {"help", no_argument, NULL, OPT_HELP},
232 {"json", required_argument, NULL, OPT_JSON},
233 {"loops", required_argument, NULL, OPT_LOOPS},
234 {"mlockall", no_argument, NULL, OPT_MLOCKALL},
235 {"priority", required_argument, NULL, OPT_PRIORITY},
236 {"quiet", no_argument, NULL, OPT_QUIET},
237 {"smp", no_argument, NULL, OPT_SMP},
238 {"threads", required_argument, NULL, OPT_THREADS},
239 {"verbose", no_argument, NULL, OPT_VERBOSE},
240 {NULL, 0, NULL, 0}
241 };
242 int c = getopt_long(argc, argv, "a::b:D:hl:mp:qSt:v",
243 long_options, &option_index);
244 if (c == -1)
245 break;
246 switch (c) {
247 case OPT_AFFINITY:
248 case 'a':
249 option_affinity = 1;
250 /* smp sets AFFINITY_USEALL in OPT_SMP */
251 if (smp)
252 break;
253 numa = numa_initialize();
254 if (optarg) {
255 parse_cpumask(optarg, max_cpus, &affinity_mask);
256 setaffinity = AFFINITY_SPECIFIED;
257 } else if (optind < argc &&
258 (atoi(argv[optind]) ||
259 argv[optind][0] == '0' ||
260 argv[optind][0] == '!' ||
261 argv[optind][0] == '+' ||
262 argv[optind][0] == 'a')) {
263 parse_cpumask(argv[optind], max_cpus, &affinity_mask);
264 setaffinity = AFFINITY_SPECIFIED;
265 } else {
266 setaffinity = AFFINITY_USEALL;
267 }
268
269 if (setaffinity == AFFINITY_SPECIFIED && !affinity_mask)
270 display_help(1);
271 if (verbose)
272 printf("Using %u cpus.\n",
273 numa_bitmask_weight(affinity_mask));
274 break;
275 case OPT_BREAKTRACE:
276 case 'b':
277 tracelimit = atoi(optarg);
278 break;
279 case OPT_DURATION:
280 case 'D':
281 duration = parse_time_string(optarg);
282 break;
283 case OPT_HELP:
284 case '?':
285 case 'h':
286 display_help(0);
287 break;
288 case OPT_JSON:
289 strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1));
290 break;
291 case OPT_LOOPS:
292 case 'l':
293 max_cycles = atoi(optarg);
294 break;
295 case OPT_MLOCKALL:
296 case 'm':
297 lockall = 1;
298 break;
299 case OPT_PRIORITY:
300 case 'p':
301 priority = atoi(optarg);
302 break;
303 case OPT_QUIET:
304 case 'q':
305 quiet = 1;
306 break;
307 case OPT_SMP:
308 case 'S':
309 if (numa)
310 fatal("numa and smp options are mutually exclusive\n");
311 smp = 1;
312 num_threads = -1; /* update after parsing */
313 setaffinity = AFFINITY_USEALL;
314 break;
315 case OPT_THREADS:
316 case 't':
317 num_threads = atoi(optarg);
318 break;
319 case OPT_VERBOSE:
320 case 'v': verbose = 1;
321 break;
322 }
323 }
324
325 if (duration < 0)
326 error = 1;
327
328 if (priority < 0 || priority > 99)
329 error = 1;
330
331 if (num_threads == -1)
332 num_threads = get_available_cpus(affinity_mask);
333
334 if (num_threads < 2)
335 error = 1;
336
337 /* if smp wasn't requested, test for numa automatically */
338 if (!smp) {
339 numa = numa_initialize();
340 if (setaffinity == AFFINITY_UNSPECIFIED)
341 setaffinity = AFFINITY_USEALL;
342 }
343
344 if (option_affinity) {
345 if (smp)
346 warn("-a ignored due to smp mode\n");
347 }
348
349 if (error) {
350 if (affinity_mask)
351 numa_bitmask_free(affinity_mask);
352 display_help(error);
353 }
354 }
355
356 static void sighand(int sig)
357 {
358 shutdown = 1;
359 }
360
361 static void print_stat(struct thread_param *par, int index, int verbose)
362 {
363 struct thread_stat *stat = par->stats;
364
365 if (!verbose) {
366 if (quiet != 1) {
367 printf("T:%2d (%5d) P:%2d C:%7lu "
368 "Min:%7ld Act:%5ld Avg:%5ld Max:%8ld\n",
369 index, stat->tid, par->prio,
370 stat->cycles, stat->min, stat->act,
371 stat->cycles ?
372 (long)(stat->avg/stat->cycles) : 0, stat->max);
373 }
374 } else {
375 while (stat->cycles != stat->cyclesread) {
376 long diff = stat->values[stat->cyclesread & par->bufmsk];
377 printf("%8d:%8lu:%8ld\n", index, stat->cyclesread, diff);
378 stat->cyclesread++;
379 }
380 }
381 }
382
383 static void write_stats(FILE *f, void *data)
384 {
385 struct thread_param *par = data;
386 struct thread_stat *s;
387 int i;
388
389 fprintf(f, " \"num_threads\": %d,\n", num_threads);
390 fprintf(f, " \"thread\": {\n");
391 for (i = 0; i < num_threads; i++) {
392 fprintf(f, " \"%u\": {\n", i);
393 s = &par->stats[i];
394 fprintf(f, " \"cycles\": %ld,\n", s->cycles);
395 fprintf(f, " \"min\": %ld,\n", s->min);
396 fprintf(f, " \"max\": %ld,\n", s->max);
397 fprintf(f, " \"avg\": %.2f,\n", s->avg/s->cycles);
398 fprintf(f, " \"cpu\": %d\n", par->cpu);
399 fprintf(f, " }%s\n", i == num_threads - 1 ? "" : ",");
400
401 }
402 fprintf(f, " }\n");
403 }
404
405 int main(int argc, char **argv)
406 {
407 sigset_t sigset;
408 int signum = SIGUSR1;
409 struct thread_param *par;
410 struct thread_stat *stat;
411 int i, ret = -1;
412 int status, cpu;
413 int max_cpus = sysconf(_SC_NPROCESSORS_ONLN);
414
415 rt_init(argc, argv);
416 process_options(argc, argv, max_cpus);
417
418 if (check_privs())
419 exit(1);
420
421 /* lock all memory (prevent paging) */
422 if (lockall)
423 if (mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
424 perror("mlockall");
425 goto out;
426 }
427
428 /* Restrict the main pid to the affinity specified by the user */
429 if (affinity_mask != NULL) {
430 int res;
431
432 errno = 0;
433 res = numa_sched_setaffinity(getpid(), affinity_mask);
434 if (res != 0)
435 warn("Couldn't setaffinity in main thread: %s\n", strerror(errno));
436 }
437
438 sigemptyset(&sigset);
439 sigaddset(&sigset, signum);
440 sigprocmask(SIG_BLOCK, &sigset, NULL);
441
442 signal(SIGINT, sighand);
443 signal(SIGTERM, sighand);
444 signal(SIGALRM, sighand);
445
446 if (duration)
447 alarm(duration);
448
449 par = calloc(num_threads, sizeof(struct thread_param));
450 if (!par)
451 goto out;
452 stat = calloc(num_threads, sizeof(struct thread_stat));
453 if (!stat)
454 goto outpar;
455
456 for (i = 0; i < num_threads; i++) {
457 if (verbose) {
458 stat[i].values = calloc(VALBUF_SIZE, sizeof(long));
459 if (!stat[i].values)
460 goto outall;
461 par[i].bufmsk = VALBUF_SIZE - 1;
462 }
463
464 switch (setaffinity) {
465 case AFFINITY_UNSPECIFIED:
466 cpu = -1;
467 break;
468 case AFFINITY_SPECIFIED:
469 cpu = cpu_for_thread_sp(i, max_cpus, affinity_mask);
470 if (verbose)
471 printf("Thread %d using cpu %d.\n", i, cpu);
472 break;
473 case AFFINITY_USEALL:
474 cpu = cpu_for_thread_ua(i, max_cpus);
475 break;
476 default:
477 cpu = -1;
478 }
479
480 par[i].id = i;
481 par[i].prio = priority;
482 #if 0
483 if (priority)
484 priority--;
485 #endif
486 par[i].signal = signum;
487 par[i].max_cycles = max_cycles;
488 par[i].stats = &stat[i];
489 par[i].cpu = cpu;
490 stat[i].min = 1000000;
491 stat[i].max = -1000000;
492 stat[i].avg = 0.0;
493 stat[i].threadstarted = 1;
494 status = pthread_create(&stat[i].thread, NULL, signalthread,
495 &par[i]);
496 if (status)
497 fatal("failed to create thread %d: %s\n", i,
498 strerror(status));
499 }
500
501 while (!shutdown) {
502 int allstarted = 1;
503
504 for (i = 0; i < num_threads; i++) {
505 if (stat[i].threadstarted != 2)
506 allstarted = 0;
507 }
508 if (!allstarted)
509 continue;
510
511 for (i = 0; i < num_threads - 1; i++)
512 stat[i].tothread = stat[i+1].thread;
513 stat[i].tothread = stat[0].thread;
514 break;
515 }
516 pthread_kill(stat[0].thread, signum);
517
518 while (!shutdown) {
519 char lavg[256];
520 int fd, len, allstopped = 0;
521
522 if (!verbose && !quiet) {
523 fd = open("/proc/loadavg", O_RDONLY, 0666);
524 len = read(fd, &lavg, 255);
525 close(fd);
526 lavg[len-1] = 0x0;
527 printf("%s \n\n", lavg);
528 }
529
530 print_stat(&par[0], 0, verbose);
531 if (max_cycles && stat[0].cycles >= max_cycles)
532 allstopped++;
533
534 usleep(10000);
535 if (shutdown || allstopped)
536 break;
537 if (!verbose && !quiet)
538 printf("\033[%dA", 3);
539 }
540 ret = 0;
541 outall:
542 shutdown = 1;
543 usleep(50000);
544 if (quiet)
545 quiet = 2;
546 for (i = 0; i < num_threads; i++) {
547 if (stat[i].threadstarted > 0)
548 pthread_kill(stat[i].thread, SIGUSR1);
549 if (stat[i].interrupted)
550 printf("Thread %d exceeded trace limit.\n", i);
551 if (stat[i].threadstarted) {
552 pthread_join(stat[i].thread, NULL);
553 print_stat(&par[i], i, 0);
554 }
555 if (stat[i].values)
556 free(stat[i].values);
557 }
558 if (strlen(jsonfile) != 0)
559 rt_write_json(jsonfile, ret, write_stats, par);
560
561 free(stat);
562 outpar:
563 free(par);
564 out:
565 if (lockall)
566 munlockall();
567
568 exit(ret);
569 }