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 */
17static void rtla_usage(int err)
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		"     hwnoise  - gives information about hardware-related noise",
30		"     timerlat - measures the timer irq and thread latency",
31		"",
32		NULL,
33	};
34
35	for (i = 0; msg[i]; i++)
36		fprintf(stderr, "%s\n", msg[i]);
37	exit(err);
38}
39
40/*
41 * run_command - try to run a rtla tool command
42 *
43 * It returns 0 if it fails. The tool's main will generally not
44 * return as they should call exit().
45 */
46int run_command(int argc, char **argv, int start_position)
47{
48	if (strcmp(argv[start_position], "osnoise") == 0) {
49		osnoise_main(argc-start_position, &argv[start_position]);
50		goto ran;
51	} else if (strcmp(argv[start_position], "hwnoise") == 0) {
52		hwnoise_main(argc-start_position, &argv[start_position]);
53		goto ran;
54	} else if (strcmp(argv[start_position], "timerlat") == 0) {
55		timerlat_main(argc-start_position, &argv[start_position]);
56		goto ran;
57	}
58
59	return 0;
60ran:
61	return 1;
62}
63
64int main(int argc, char *argv[])
65{
66	int retval;
67
68	/* is it an alias? */
69	retval = run_command(argc, argv, 0);
70	if (retval)
71		exit(0);
72
73	if (argc < 2)
74		goto usage;
75
76	if (strcmp(argv[1], "-h") == 0) {
77		rtla_usage(0);
78	} else if (strcmp(argv[1], "--help") == 0) {
79		rtla_usage(0);
80	}
81
82	retval = run_command(argc, argv, 1);
83	if (retval)
84		exit(0);
85
86usage:
87	rtla_usage(1);
88	exit(1);
89}
90