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
17static void timerlat_usage(int err)
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(err);
38}
39
40int 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(0);
56	} else if (strncmp(argv[1], "-", 1) == 0) {
57		/* the user skipped the tool, call the default one */
58		timerlat_top_main(argc, argv);
59		exit(0);
60	} else if (strcmp(argv[1], "top") == 0) {
61		timerlat_top_main(argc-1, &argv[1]);
62		exit(0);
63	} else if (strcmp(argv[1], "hist") == 0) {
64		timerlat_hist_main(argc-1, &argv[1]);
65		exit(0);
66	}
67
68usage:
69	timerlat_usage(1);
70	exit(1);
71}
72