|
4 | 4 | #include <pthread.h> |
5 | 5 | #include <signal.h> |
6 | 6 | #include <stdlib.h> |
| 7 | +#include <string.h> |
7 | 8 | #include <unistd.h> |
| 9 | +#include <getopt.h> |
8 | 10 | #include "common.h" |
9 | 11 |
|
10 | 12 | struct trace_instance *trace_inst; |
11 | | -int stop_tracing; |
| 13 | +volatile int stop_tracing; |
12 | 14 |
|
13 | 15 | static void stop_trace(int sig) |
14 | 16 | { |
@@ -37,6 +39,84 @@ static void set_signals(struct common_params *params) |
37 | 39 | } |
38 | 40 | } |
39 | 41 |
|
| 42 | +/* |
| 43 | + * common_parse_options - parse common command line options |
| 44 | + * |
| 45 | + * @argc: argument count |
| 46 | + * @argv: argument vector |
| 47 | + * @common: common parameters structure |
| 48 | + * |
| 49 | + * Parse command line options that are common to all rtla tools. |
| 50 | + * |
| 51 | + * Returns: non zero if a common option was parsed, or 0 |
| 52 | + * if the option should be handled by tool-specific parsing. |
| 53 | + */ |
| 54 | +int common_parse_options(int argc, char **argv, struct common_params *common) |
| 55 | +{ |
| 56 | + struct trace_events *tevent; |
| 57 | + int saved_state = optind; |
| 58 | + int c; |
| 59 | + |
| 60 | + static struct option long_options[] = { |
| 61 | + {"cpus", required_argument, 0, 'c'}, |
| 62 | + {"cgroup", optional_argument, 0, 'C'}, |
| 63 | + {"debug", no_argument, 0, 'D'}, |
| 64 | + {"duration", required_argument, 0, 'd'}, |
| 65 | + {"event", required_argument, 0, 'e'}, |
| 66 | + {"house-keeping", required_argument, 0, 'H'}, |
| 67 | + {"priority", required_argument, 0, 'P'}, |
| 68 | + {0, 0, 0, 0} |
| 69 | + }; |
| 70 | + |
| 71 | + opterr = 0; |
| 72 | + c = getopt_long(argc, argv, "c:C::Dd:e:H:P:", long_options, NULL); |
| 73 | + opterr = 1; |
| 74 | + |
| 75 | + switch (c) { |
| 76 | + case 'c': |
| 77 | + if (parse_cpu_set(optarg, &common->monitored_cpus)) |
| 78 | + fatal("Invalid -c cpu list"); |
| 79 | + common->cpus = optarg; |
| 80 | + break; |
| 81 | + case 'C': |
| 82 | + common->cgroup = 1; |
| 83 | + common->cgroup_name = parse_optional_arg(argc, argv); |
| 84 | + break; |
| 85 | + case 'D': |
| 86 | + config_debug = 1; |
| 87 | + break; |
| 88 | + case 'd': |
| 89 | + common->duration = parse_seconds_duration(optarg); |
| 90 | + if (!common->duration) |
| 91 | + fatal("Invalid -d duration"); |
| 92 | + break; |
| 93 | + case 'e': |
| 94 | + tevent = trace_event_alloc(optarg); |
| 95 | + if (!tevent) |
| 96 | + fatal("Error alloc trace event"); |
| 97 | + |
| 98 | + if (common->events) |
| 99 | + tevent->next = common->events; |
| 100 | + common->events = tevent; |
| 101 | + break; |
| 102 | + case 'H': |
| 103 | + common->hk_cpus = 1; |
| 104 | + if (parse_cpu_set(optarg, &common->hk_cpu_set)) |
| 105 | + fatal("Error parsing house keeping CPUs"); |
| 106 | + break; |
| 107 | + case 'P': |
| 108 | + if (parse_prio(optarg, &common->sched_param) == -1) |
| 109 | + fatal("Invalid -P priority"); |
| 110 | + common->set_sched = 1; |
| 111 | + break; |
| 112 | + default: |
| 113 | + optind = saved_state; |
| 114 | + return 0; |
| 115 | + } |
| 116 | + |
| 117 | + return c; |
| 118 | +} |
| 119 | + |
40 | 120 | /* |
41 | 121 | * common_apply_config - apply common configs to the initialized tool |
42 | 122 | */ |
@@ -348,3 +428,61 @@ int hist_main_loop(struct osnoise_tool *tool) |
348 | 428 |
|
349 | 429 | return retval; |
350 | 430 | } |
| 431 | + |
| 432 | +int osn_set_stop(struct osnoise_tool *tool) |
| 433 | +{ |
| 434 | + struct common_params *params = tool->params; |
| 435 | + int retval; |
| 436 | + |
| 437 | + retval = osnoise_set_stop_us(tool->context, params->stop_us); |
| 438 | + if (retval) { |
| 439 | + err_msg("Failed to set stop us\n"); |
| 440 | + return retval; |
| 441 | + } |
| 442 | + |
| 443 | + retval = osnoise_set_stop_total_us(tool->context, params->stop_total_us); |
| 444 | + if (retval) { |
| 445 | + err_msg("Failed to set stop total us\n"); |
| 446 | + return retval; |
| 447 | + } |
| 448 | + |
| 449 | + return 0; |
| 450 | +} |
| 451 | + |
| 452 | +static void print_msg_array(const char * const *msgs) |
| 453 | +{ |
| 454 | + if (!msgs) |
| 455 | + return; |
| 456 | + |
| 457 | + for (int i = 0; msgs[i]; i++) |
| 458 | + fprintf(stderr, "%s\n", msgs[i]); |
| 459 | +} |
| 460 | + |
| 461 | +/* |
| 462 | + * common_usage - print complete usage information |
| 463 | + */ |
| 464 | +void common_usage(const char *tool, const char *mode, |
| 465 | + const char *desc, const char * const *start_msgs, const char * const *opt_msgs) |
| 466 | +{ |
| 467 | + static const char * const common_options[] = { |
| 468 | + " -h/--help: print this menu", |
| 469 | + NULL |
| 470 | + }; |
| 471 | + fprintf(stderr, "rtla %s", tool); |
| 472 | + if (strcmp(mode, "")) |
| 473 | + fprintf(stderr, " %s", mode); |
| 474 | + fprintf(stderr, ": %s (version %s)\n\n", desc, VERSION); |
| 475 | + fprintf(stderr, " usage: [rtla] %s ", tool); |
| 476 | + |
| 477 | + if (strcmp(mode, "top") == 0) |
| 478 | + fprintf(stderr, "[top] [-h] "); |
| 479 | + else |
| 480 | + fprintf(stderr, "%s [-h] ", mode); |
| 481 | + |
| 482 | + print_msg_array(start_msgs); |
| 483 | + fprintf(stderr, "\n"); |
| 484 | + print_msg_array(common_options); |
| 485 | + print_msg_array(opt_msgs); |
| 486 | + |
| 487 | + exit(EXIT_SUCCESS); |
| 488 | +} |
0 commit comments