xref: /linux/tools/tracing/rtla/src/timerlat.c (revision 64b14a184e83eb62ea0615e31a409956049d40e7)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
4  */
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <pthread.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 
15 #include "timerlat.h"
16 
17 static void timerlat_usage(void)
18 {
19 	int i;
20 
21 	static const char * const msg[] = {
22 		"",
23 		"timerlat version " VERSION,
24 		"",
25 		"  usage: [rtla] timerlat [MODE] ...",
26 		"",
27 		"  modes:",
28 		"     top   - prints the summary from timerlat tracer",
29 		"     hist  - prints a histogram of timer latencies",
30 		"",
31 		"if no MODE is given, the top mode is called, passing the arguments",
32 		NULL,
33 	};
34 
35 	for (i = 0; msg[i]; i++)
36 		fprintf(stderr, "%s\n", msg[i]);
37 	exit(1);
38 }
39 
40 int timerlat_main(int argc, char *argv[])
41 {
42 	if (argc == 0)
43 		goto usage;
44 
45 	/*
46 	 * if timerlat was called without any argument, run the
47 	 * default cmdline.
48 	 */
49 	if (argc == 1) {
50 		timerlat_top_main(argc, argv);
51 		exit(0);
52 	}
53 
54 	if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0)) {
55 		timerlat_usage();
56 		exit(0);
57 	} else if (strncmp(argv[1], "-", 1) == 0) {
58 		/* the user skipped the tool, call the default one */
59 		timerlat_top_main(argc, argv);
60 		exit(0);
61 	} else if (strcmp(argv[1], "top") == 0) {
62 		timerlat_top_main(argc-1, &argv[1]);
63 		exit(0);
64 	} else if (strcmp(argv[1], "hist") == 0) {
65 		timerlat_hist_main(argc-1, &argv[1]);
66 		exit(0);
67 	}
68 
69 usage:
70 	timerlat_usage();
71 	exit(1);
72 }
73