Debian Codegraph @

tools/tracing/rtla/src/common.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3
4 #include <pthread.h>
5 #include <signal.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <getopt.h>
10 #include "common.h"
11
12 struct trace_instance *trace_inst;
13 volatile int stop_tracing;
14
15 static void stop_trace(int sig)
16 {
17 if (stop_tracing) {
18 /*
19 * Stop requested twice in a row; abort event processing and
20 * exit immediately
21 */
22 tracefs_iterate_stop(trace_inst->inst);
23 return;
24 }
25 stop_tracing = 1;
26 if (trace_inst)
27 trace_instance_stop(trace_inst);
28 }
29
30 /*
31 * set_signals - handles the signal to stop the tool
32 */
33 static void set_signals(struct common_params *params)
34 {
35 signal(SIGINT, stop_trace);
36 if (params->duration) {
37 signal(SIGALRM, stop_trace);
38 alarm(params->duration);
39 }
40 }
41
42 /*
43 * unset_signals - unsets the signals to stop the tool
44 */
45 static void unset_signals(struct common_params *params)
46 {
47 signal(SIGINT, SIG_DFL);
48 if (params->duration) {
49 alarm(0);
50 signal(SIGALRM, SIG_DFL);
51 }
52 }
53
54 /*
55 * getopt_auto - auto-generates optstring from long_options
56 */
57 int getopt_auto(int argc, char **argv, const struct option *long_opts)
58 {
59 char opts[256];
60 int n = 0;
61
62 for (int i = 0; long_opts[i].name; i++) {
63 if (long_opts[i].val < 32 || long_opts[i].val > 127)
64 continue;
65
66 if (n + 4 >= sizeof(opts))
67 fatal("optstring buffer overflow");
68
69 opts[n++] = long_opts[i].val;
70
71 if (long_opts[i].has_arg == required_argument)
72 opts[n++] = ':';
73 else if (long_opts[i].has_arg == optional_argument) {
74 opts[n++] = ':';
75 opts[n++] = ':';
76 }
77 }
78
79 opts[n] = '\0';
80
81 return getopt_long(argc, argv, opts, long_opts, NULL);
82 }
83
84 /*
85 * common_parse_options - parse common command line options
86 *
87 * @argc: argument count
88 * @argv: argument vector
89 * @common: common parameters structure
90 *
91 * Parse command line options that are common to all rtla tools.
92 *
93 * Returns: non zero if a common option was parsed, or 0
94 * if the option should be handled by tool-specific parsing.
95 */
96 int common_parse_options(int argc, char **argv, struct common_params *common)
97 {
98 struct trace_events *tevent;
99 int saved_state = optind;
100 int c;
101
102 static struct option long_options[] = {
103 {"cpus", required_argument, 0, 'c'},
104 {"cgroup", optional_argument, 0, 'C'},
105 {"debug", no_argument, 0, 'D'},
106 {"duration", required_argument, 0, 'd'},
107 {"event", required_argument, 0, 'e'},
108 {"house-keeping", required_argument, 0, 'H'},
109 {"priority", required_argument, 0, 'P'},
110 {0, 0, 0, 0}
111 };
112
113 opterr = 0;
114 c = getopt_auto(argc, argv, long_options);
115 opterr = 1;
116
117 switch (c) {
118 case 'c':
119 if (parse_cpu_set(optarg, &common->monitored_cpus))
120 fatal("Invalid -c cpu list");
121 common->cpus = optarg;
122 break;
123 case 'C':
124 common->cgroup = 1;
125 common->cgroup_name = parse_optional_arg(argc, argv);
126 break;
127 case 'D':
128 config_debug = 1;
129 break;
130 case 'd':
131 common->duration = parse_seconds_duration(optarg);
132 if (!common->duration)
133 fatal("Invalid -d duration");
134 break;
135 case 'e':
136 tevent = trace_event_alloc(optarg);
137 if (!tevent)
138 fatal("Error alloc trace event");
139
140 if (common->events)
141 tevent->next = common->events;
142 common->events = tevent;
143 break;
144 case 'H':
145 common->hk_cpus = 1;
146 if (parse_cpu_set(optarg, &common->hk_cpu_set))
147 fatal("Error parsing house keeping CPUs");
148 break;
149 case 'P':
150 if (parse_prio(optarg, &common->sched_param) == -1)
151 fatal("Invalid -P priority");
152 common->set_sched = 1;
153 break;
154 default:
155 optind = saved_state;
156 return 0;
157 }
158
159 return c;
160 }
161
162 /*
163 * common_apply_config - apply common configs to the initialized tool
164 */
165 int
166 common_apply_config(struct osnoise_tool *tool, struct common_params *params)
167 {
168 int retval, i;
169
170 if (!params->sleep_time)
171 params->sleep_time = 1;
172
173 retval = osnoise_set_cpus(tool->context, params->cpus ? params->cpus : "all");
174 if (retval) {
175 err_msg("Failed to apply CPUs config\n");
176 goto out_err;
177 }
178
179 if (!params->cpus) {
180 for (i = 0; i < sysconf(_SC_NPROCESSORS_CONF); i++)
181 CPU_SET(i, &params->monitored_cpus);
182 }
183
184 if (params->hk_cpus) {
185 retval = sched_setaffinity(getpid(), sizeof(params->hk_cpu_set),
186 &params->hk_cpu_set);
187 if (retval == -1) {
188 err_msg("Failed to set rtla to the house keeping CPUs\n");
189 goto out_err;
190 }
191 } else if (params->cpus) {
192 /*
193 * Even if the user do not set a house-keeping CPU, try to
194 * move rtla to a CPU set different to the one where the user
195 * set the workload to run.
196 *
197 * No need to check results as this is an automatic attempt.
198 */
199 auto_house_keeping(&params->monitored_cpus);
200 }
201
202 /*
203 * Set workload according to type of thread if the kernel supports it.
204 * On kernels without support, user threads will have already failed
205 * on missing fd, and kernel threads do not need it.
206 */
207 retval = osnoise_set_workload(tool->context, params->kernel_workload);
208 if (retval < -1) {
209 err_msg("Failed to set OSNOISE_WORKLOAD option\n");
210 goto out_err;
211 }
212
213 return 0;
214
215 out_err:
216 return -1;
217 }
218
219
220 int run_tool(struct tool_ops *ops, int argc, char *argv[])
221 {
222 struct common_params *params;
223 enum result return_value = ERROR;
224 struct osnoise_tool *tool;
225 bool stopped;
226 int retval;
227
228 params = ops->parse_args(argc, argv);
229 if (!params)
230 exit(1);
231
232 tool = ops->init_tool(params);
233 if (!tool) {
234 err_msg("Could not init osnoise tool\n");
235 goto out_exit;
236 }
237 tool->ops = ops;
238 tool->params = params;
239
240 /*
241 * Save trace instance into global variable so that SIGINT can stop
242 * the timerlat tracer.
243 * Otherwise, rtla could loop indefinitely when overloaded.
244 */
245 trace_inst = &tool->trace;
246
247 retval = ops->apply_config(tool);
248 if (retval) {
249 err_msg("Could not apply config\n");
250 goto out_free;
251 }
252
253 retval = enable_tracer_by_name(trace_inst->inst, ops->tracer);
254 if (retval) {
255 err_msg("Failed to enable %s tracer\n", ops->tracer);
256 goto out_free;
257 }
258
259 if (params->set_sched) {
260 retval = set_comm_sched_attr(ops->comm_prefix, &params->sched_param);
261 if (retval) {
262 err_msg("Failed to set sched parameters\n");
263 goto out_free;
264 }
265 }
266
267 if (params->cgroup && !params->user_data) {
268 retval = set_comm_cgroup(ops->comm_prefix, params->cgroup_name);
269 if (!retval) {
270 err_msg("Failed to move threads to cgroup\n");
271 goto out_free;
272 }
273 }
274
275
276 if (params->threshold_actions.present[ACTION_TRACE_OUTPUT] ||
277 params->end_actions.present[ACTION_TRACE_OUTPUT]) {
278 tool->record = osnoise_init_trace_tool(ops->tracer);
279 if (!tool->record) {
280 err_msg("Failed to enable the trace instance\n");
281 goto out_free;
282 }
283 params->threshold_actions.trace_output_inst = tool->record->trace.inst;
284 params->end_actions.trace_output_inst = tool->record->trace.inst;
285
286 if (params->events) {
287 retval = trace_events_enable(&tool->record->trace, params->events);
288 if (retval)
289 goto out_trace;
290 }
291
292 if (params->buffer_size > 0) {
293 retval = trace_set_buffer_size(&tool->record->trace, params->buffer_size);
294 if (retval)
295 goto out_trace;
296 }
297 }
298
299 if (params->user_workload) {
300 pthread_t user_thread;
301
302 /* rtla asked to stop */
303 params->user.should_run = 1;
304 /* all threads left */
305 params->user.stopped_running = 0;
306
307 params->user.set = &params->monitored_cpus;
308 if (params->set_sched)
309 params->user.sched_param = &params->sched_param;
310 else
311 params->user.sched_param = NULL;
312
313 params->user.cgroup_name = params->cgroup_name;
314
315 retval = pthread_create(&user_thread, NULL, timerlat_u_dispatcher, &params->user);
316 if (retval)
317 err_msg("Error creating timerlat user-space threads\n");
318 }
319
320 retval = ops->enable(tool);
321 if (retval)
322 goto out_trace;
323
324 tool->start_time = time(NULL);
325 set_signals(params);
326
327 retval = ops->main(tool);
328 if (retval)
329 goto out_signals;
330
331 if (params->user_workload && !params->user.stopped_running) {
332 params->user.should_run = 0;
333 sleep(1);
334 }
335
336 ops->print_stats(tool);
337
338 actions_perform(&params->end_actions);
339
340 return_value = PASSED;
341
342 stopped = osnoise_trace_is_off(tool, tool->record) && !stop_tracing;
343 if (stopped) {
344 printf("%s hit stop tracing\n", ops->tracer);
345 return_value = FAILED;
346 }
347
348 if (ops->analyze)
349 ops->analyze(tool, stopped);
350
351 out_signals:
352 unset_signals(params);
353 out_trace:
354 trace_events_destroy(&tool->record->trace, params->events);
355 params->events = NULL;
356 out_free:
357 ops->free(tool);
358 osnoise_destroy_tool(tool->record);
359 osnoise_destroy_tool(tool);
360 actions_destroy(&params->threshold_actions);
361 actions_destroy(&params->end_actions);
362 free(params);
363 out_exit:
364 exit(return_value);
365 }
366
367 int top_main_loop(struct osnoise_tool *tool)
368 {
369 struct common_params *params = tool->params;
370 struct trace_instance *trace = &tool->trace;
371 struct osnoise_tool *record = tool->record;
372 int retval;
373
374 while (!stop_tracing) {
375 sleep(params->sleep_time);
376
377 if (params->aa_only && !osnoise_trace_is_off(tool, record))
378 continue;
379
380 retval = tracefs_iterate_raw_events(trace->tep,
381 trace->inst,
382 NULL,
383 0,
384 collect_registered_events,
385 trace);
386 if (retval < 0) {
387 err_msg("Error iterating on events\n");
388 return retval;
389 }
390
391 if (!params->quiet)
392 tool->ops->print_stats(tool);
393
394 if (osnoise_trace_is_off(tool, record)) {
395 if (stop_tracing)
396 /* stop tracing requested, do not perform actions */
397 return 0;
398
399 actions_perform(&params->threshold_actions);
400
401 if (!params->threshold_actions.continue_flag)
402 /* continue flag not set, break */
403 return 0;
404
405 /* continue action reached, re-enable tracing */
406 if (record)
407 trace_instance_start(&record->trace);
408 if (tool->aa)
409 trace_instance_start(&tool->aa->trace);
410 trace_instance_start(trace);
411 }
412
413 /* is there still any user-threads ? */
414 if (params->user_workload) {
415 if (params->user.stopped_running) {
416 debug_msg("timerlat user space threads stopped!\n");
417 break;
418 }
419 }
420 }
421
422 return 0;
423 }
424
425 int hist_main_loop(struct osnoise_tool *tool)
426 {
427 struct common_params *params = tool->params;
428 struct trace_instance *trace = &tool->trace;
429 int retval = 0;
430
431 while (!stop_tracing) {
432 sleep(params->sleep_time);
433
434 retval = tracefs_iterate_raw_events(trace->tep,
435 trace->inst,
436 NULL,
437 0,
438 collect_registered_events,
439 trace);
440 if (retval < 0) {
441 err_msg("Error iterating on events\n");
442 break;
443 }
444
445 if (osnoise_trace_is_off(tool, tool->record)) {
446 if (stop_tracing)
447 /* stop tracing requested, do not perform actions */
448 break;
449
450 actions_perform(&params->threshold_actions);
451
452 if (!params->threshold_actions.continue_flag)
453 /* continue flag not set, break */
454 break;
455
456 /* continue action reached, re-enable tracing */
457 if (tool->record)
458 trace_instance_start(&tool->record->trace);
459 if (tool->aa)
460 trace_instance_start(&tool->aa->trace);
461 trace_instance_start(&tool->trace);
462 }
463
464 /* is there still any user-threads ? */
465 if (params->user_workload) {
466 if (params->user.stopped_running) {
467 debug_msg("user-space threads stopped!\n");
468 break;
469 }
470 }
471 }
472
473 return retval;
474 }
475
476 int osn_set_stop(struct osnoise_tool *tool)
477 {
478 struct common_params *params = tool->params;
479 int retval;
480
481 retval = osnoise_set_stop_us(tool->context, params->stop_us);
482 if (retval) {
483 err_msg("Failed to set stop us\n");
484 return retval;
485 }
486
487 retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us);
488 if (retval) {
489 err_msg("Failed to set stop total us\n");
490 return retval;
491 }
492
493 return 0;
494 }
495
496 static void print_msg_array(const char * const *msgs)
497 {
498 if (!msgs)
499 return;
500
501 for (int i = 0; msgs[i]; i++)
502 fprintf(stderr, "%s\n", msgs[i]);
503 }
504
505 /*
506 * common_usage - print complete usage information
507 */
508 void common_usage(const char *tool, const char *mode,
509 const char *desc, const char * const *start_msgs, const char * const *opt_msgs)
510 {
511 static const char * const common_options[] = {
512 " -h/--help: print this menu",
513 NULL
514 };
515 fprintf(stderr, "rtla %s", tool);
516 if (strcmp(mode, ""))
517 fprintf(stderr, " %s", mode);
518 fprintf(stderr, ": %s (version %s)\n\n", desc, VERSION);
519 fprintf(stderr, " usage: [rtla] %s ", tool);
520
521 if (strcmp(mode, "top") == 0)
522 fprintf(stderr, "[top] [-h] ");
523 else
524 fprintf(stderr, "%s [-h] ", mode);
525
526 print_msg_array(start_msgs);
527 fprintf(stderr, "\n");
528 print_msg_array(common_options);
529 print_msg_array(opt_msgs);
530
531 exit(EXIT_SUCCESS);
532 }