xref: /linux/tools/tracing/rtla/src/rtla.c (revision 0be3ff0c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
4  */
5 
6 #include <getopt.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10 
11 #include "osnoise.h"
12 #include "timerlat.h"
13 
14 /*
15  * rtla_usage - print rtla usage
16  */
17 static void rtla_usage(void)
18 {
19 	int i;
20 
21 	static const char *msg[] = {
22 		"",
23 		"rtla version " VERSION,
24 		"",
25 		"  usage: rtla COMMAND ...",
26 		"",
27 		"  commands:",
28 		"     osnoise  - gives information about the operating system noise (osnoise)",
29 		"     timerlat - measures the timer irq and thread latency",
30 		"",
31 		NULL,
32 	};
33 
34 	for (i = 0; msg[i]; i++)
35 		fprintf(stderr, "%s\n", msg[i]);
36 	exit(1);
37 }
38 
39 /*
40  * run_command - try to run a rtla tool command
41  *
42  * It returns 0 if it fails. The tool's main will generally not
43  * return as they should call exit().
44  */
45 int run_command(int argc, char **argv, int start_position)
46 {
47 	if (strcmp(argv[start_position], "osnoise") == 0) {
48 		osnoise_main(argc-start_position, &argv[start_position]);
49 		goto ran;
50 	} else if (strcmp(argv[start_position], "timerlat") == 0) {
51 		timerlat_main(argc-start_position, &argv[start_position]);
52 		goto ran;
53 	}
54 
55 	return 0;
56 ran:
57 	return 1;
58 }
59 
60 int main(int argc, char *argv[])
61 {
62 	int retval;
63 
64 	/* is it an alias? */
65 	retval = run_command(argc, argv, 0);
66 	if (retval)
67 		exit(0);
68 
69 	if (argc < 2)
70 		goto usage;
71 
72 	if (strcmp(argv[1], "-h") == 0) {
73 		rtla_usage();
74 		exit(0);
75 	} else if (strcmp(argv[1], "--help") == 0) {
76 		rtla_usage();
77 		exit(0);
78 	}
79 
80 	retval = run_command(argc, argv, 1);
81 	if (retval)
82 		exit(0);
83 
84 usage:
85 	rtla_usage();
86 	exit(1);
87 }
88