]>
Commit | Line | Data |
---|---|---|
a0f62b0b | 1 | /* |
9abd5e4b KZ |
2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
3 | * | |
a0f62b0b KZ |
4 | * lsirq - utility to display kernel interrupt information. |
5 | * | |
6 | * Copyright (C) 2019 zhenwei pi <pizhenwei@bytedance.com> | |
7 | * Copyright (C) 2020 Karel Zak <kzak@redhat.com> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
a0f62b0b KZ |
13 | */ |
14 | #include <ctype.h> | |
15 | #include <errno.h> | |
16 | #include <getopt.h> | |
17 | #include <limits.h> | |
18 | #include <locale.h> | |
19 | #include <stdio.h> | |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | #include <sys/types.h> | |
23 | #include <unistd.h> | |
24 | ||
25 | #include <libsmartcols.h> | |
26 | ||
27 | #include "closestream.h" | |
968a50b3 | 28 | #include "optutils.h" |
a0f62b0b KZ |
29 | #include "strutils.h" |
30 | #include "xalloc.h" | |
31 | ||
32 | #include "irq-common.h" | |
33 | ||
b6ce063b | 34 | static int print_irq_data(struct irq_output *out, int softirq) |
a0f62b0b KZ |
35 | { |
36 | struct libscols_table *table; | |
37 | ||
4b2fadb1 | 38 | table = get_scols_table(out, NULL, NULL, softirq, 0, NULL); |
a0f62b0b KZ |
39 | if (!table) |
40 | return -1; | |
41 | ||
42 | scols_print_table(table); | |
43 | scols_unref_table(table); | |
44 | return 0; | |
45 | } | |
46 | ||
47 | static void __attribute__((__noreturn__)) usage(void) | |
48 | { | |
49 | fputs(USAGE_HEADER, stdout); | |
50 | printf(_(" %s [options]\n"), program_invocation_short_name); | |
51 | fputs(USAGE_SEPARATOR, stdout); | |
52 | ||
f11785b5 | 53 | fputsln(_("Utility to display kernel interrupt information."), stdout); |
a0f62b0b KZ |
54 | |
55 | fputs(USAGE_OPTIONS, stdout); | |
56 | fputs(_(" -J, --json use JSON output format\n"), stdout); | |
5a20c0de | 57 | fputs(_(" -P, --pairs use key=\"value\" output format\n"), stdout); |
dd52c4fa | 58 | fputs(_(" -n, --noheadings don't print headings\n"), stdout); |
a0f62b0b KZ |
59 | fputs(_(" -o, --output <list> define which output columns to use\n"), stdout); |
60 | fputs(_(" -s, --sort <column> specify sort column\n"), stdout); | |
b6ce063b | 61 | fputs(_(" -S, --softirq show softirqs instead of interrupts\n"), stdout); |
a0f62b0b | 62 | fputs(USAGE_SEPARATOR, stdout); |
bad4c729 | 63 | fprintf(stdout, USAGE_HELP_OPTIONS(22)); |
a0f62b0b KZ |
64 | |
65 | fputs(USAGE_COLUMNS, stdout); | |
66 | irq_print_columns(stdout, 1); | |
67 | ||
bad4c729 | 68 | fprintf(stdout, USAGE_MAN_TAIL("lsirq(1)")); |
a0f62b0b KZ |
69 | exit(EXIT_SUCCESS); |
70 | } | |
71 | ||
72 | int main(int argc, char **argv) | |
73 | { | |
74 | struct irq_output out = { | |
75 | .ncolumns = 0 | |
76 | }; | |
77 | static const struct option longopts[] = { | |
78 | {"sort", required_argument, NULL, 's'}, | |
dd52c4fa | 79 | {"noheadings", no_argument, NULL, 'n'}, |
a0f62b0b | 80 | {"output", required_argument, NULL, 'o'}, |
b6ce063b | 81 | {"softirq", no_argument, NULL, 'S'}, |
a0f62b0b | 82 | {"json", no_argument, NULL, 'J'}, |
5a20c0de | 83 | {"pairs", no_argument, NULL, 'P'}, |
a0f62b0b KZ |
84 | {"help", no_argument, NULL, 'h'}, |
85 | {"version", no_argument, NULL, 'V'}, | |
86 | {NULL, 0, NULL, 0} | |
87 | }; | |
88 | int c; | |
89 | const char *outarg = NULL; | |
968a50b3 SK |
90 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
91 | {'J', 'P'}, | |
92 | {0} | |
93 | }; | |
94 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
b6ce063b | 95 | int softirq = 0; |
a0f62b0b KZ |
96 | |
97 | setlocale(LC_ALL, ""); | |
98 | ||
b6ce063b | 99 | while ((c = getopt_long(argc, argv, "no:s:ShJPV", longopts, NULL)) != -1) { |
968a50b3 SK |
100 | err_exclusive_options(c, longopts, excl, excl_st); |
101 | ||
a0f62b0b KZ |
102 | switch (c) { |
103 | case 'J': | |
104 | out.json = 1; | |
105 | break; | |
5a20c0de KZ |
106 | case 'P': |
107 | out.pairs = 1; | |
108 | break; | |
dd52c4fa KZ |
109 | case 'n': |
110 | out.no_headings = 1; | |
111 | break; | |
a0f62b0b KZ |
112 | case 'o': |
113 | outarg = optarg; | |
114 | break; | |
115 | case 's': | |
116 | set_sort_func_by_name(&out, optarg); | |
117 | break; | |
b6ce063b | 118 | case 'S': |
119 | softirq = 1; | |
120 | break; | |
a0f62b0b KZ |
121 | case 'V': |
122 | print_version(EXIT_SUCCESS); | |
123 | case 'h': | |
124 | usage(); | |
125 | default: | |
126 | errtryhelp(EXIT_FAILURE); | |
127 | } | |
128 | } | |
129 | ||
130 | /* default */ | |
131 | if (!out.ncolumns) { | |
132 | out.columns[out.ncolumns++] = COL_IRQ; | |
133 | out.columns[out.ncolumns++] = COL_TOTAL; | |
b2a49bc2 | 134 | out.columns[out.ncolumns++] = COL_NAME; |
a0f62b0b KZ |
135 | } |
136 | ||
137 | /* add -o [+]<list> to putput */ | |
138 | if (outarg && string_add_to_idarray(outarg, out.columns, | |
139 | ARRAY_SIZE(out.columns), | |
140 | &out.ncolumns, | |
141 | irq_column_name_to_id) < 0) | |
142 | exit(EXIT_FAILURE); | |
143 | ||
b6ce063b | 144 | return print_irq_data(&out, softirq) == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
a0f62b0b | 145 | } |