summaryrefslogtreecommitdiffstats
path: root/sys-utils/irq-common.c
blob: 11a4e4eb8bb03fea23bd6fb7a380eaf6a2f6dc47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
 * irq-common.c - functions to display kernel interrupt information.
 *
 * Copyright (C) 2019 zhenwei pi <pizhenwei@bytedance.com>
 * Copyright (C) 2020 Karel Zak <kzak@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <libsmartcols.h>

#include "c.h"
#include "nls.h"
#include "pathnames.h"
#include "strutils.h"
#include "xalloc.h"

#include "irq-common.h"

#define IRQ_INFO_LEN	64

struct colinfo {
	const char *name;
	double whint;
	int flags;
	const char *help;
	int json_type;
};

static const struct colinfo infos[] = {
	[COL_IRQ]   = {"IRQ",   0.10, SCOLS_FL_RIGHT, N_("interrupts"),  SCOLS_JSON_STRING},
	[COL_TOTAL] = {"TOTAL", 0.10, SCOLS_FL_RIGHT, N_("total count"), SCOLS_JSON_NUMBER},
	[COL_DELTA] = {"DELTA", 0.10, SCOLS_FL_RIGHT, N_("delta count"), SCOLS_JSON_NUMBER},
	[COL_NAME]  = {"NAME",  0.70, SCOLS_FL_TRUNC, N_("name"),        SCOLS_JSON_STRING},
};

/* make softirq friendly to end-user */
struct softirq_desc {
	char *irq;
	char *desc;
} softirq_descs[] = {
	{ .irq = "HI", .desc = "high priority tasklet softirq" },
	{ .irq = "TIMER", .desc = "timer softirq" },
	{ .irq = "NET_TX", .desc = "network transmit softirq", },
	{ .irq = "NET_RX", .desc = "network receive softirq" },
	{ .irq = "BLOCK", .desc = "block device softirq" },
	{ .irq = "IRQ_POLL", .desc = "IO poll softirq" },
	{ .irq = "TASKLET", .desc = "normal priority tasklet softirq" },
	{ .irq = "SCHED", .desc = "schedule softirq" },
	{ .irq = "HRTIMER", .desc = "high resolution timer softirq" },
	{ .irq = "RCU", .desc = "RCU softirq" },
};

static void get_softirq_desc(struct irq_info *curr)
{
	int i, size = ARRAY_SIZE(softirq_descs);

	for (i = 0; i < size; i++) {
		if (!strcmp(curr->irq, softirq_descs[i].irq))
			break;
	}

	if (i < size)
		curr->name = xstrdup(softirq_descs[i].desc);
	else
		curr->name = xstrdup("");
}

int irq_column_name_to_id(const char *name, size_t namesz)
{
	size_t i;

	assert(name);
	for (i = 0; i < ARRAY_SIZE(infos); i++) {
		const char *cn = infos[i].name;

		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
			return i;
	}
	warnx(_("unknown column: %s"), name);
	return -1;
}

static inline int get_column_id(struct irq_output *out, size_t const num)
{
	assert(num < out->ncolumns);
	assert(out->columns[num] < (int)ARRAY_SIZE(infos));

	return out->columns[num];
}

static inline const struct colinfo *get_column_info(
			struct irq_output *out, unsigned num)
{
	return &infos[get_column_id(out, num)];
}

void irq_print_columns(FILE *f, int nodelta)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(infos); i++) {
		if (nodelta && i == COL_DELTA)
			continue;
		fprintf(f, "  %-5s  %s\n", infos[i].name, _(infos[i].help));
	}
}

static struct libscols_table *new_scols_table(struct irq_output *out)
{
	size_t i;
	struct libscols_table *table;

	table = scols_new_table();
	if (!table) {
		warn(_("failed to initialize output table"));
		return NULL;
	}
	scols_table_enable_json(table, out->json);
	scols_table_enable_noheadings(table, out->no_headings);
	scols_table_enable_export(table, out->pairs);

	if (out->json)
		scols_table_set_name(table, "interrupts");

	for (i = 0; i < out->ncolumns; i++) {
		const struct colinfo *col = get_column_info(out, i);
		int flags = col->flags;
		struct libscols_column *cl;

		cl = scols_table_new_column(table, col->name, col->whint, flags);
		if (cl == NULL) {
			warnx(_("failed to initialize output column"));
			goto err;
		}
		if (out->json)
			scols_column_set_json_type(cl, col->json_type);
	}

	return table;
 err:
	scols_unref_table(table);
	return NULL;
}

static struct libscols_line *new_scols_line(struct libscols_table *table)
{
	struct libscols_line *line = scols_table_new_line(table, NULL);
	if (!line) {
		warn(_("failed to add line to output"));
		return NULL;
	}
	return line;
}

static void add_scols_line(struct irq_output *out,
			   struct irq_info *info,
			   struct libscols_table *table)
{
	size_t i;
	struct libscols_line *line = new_scols_line(table);

	for (i = 0; i < out->ncolumns; i++) {
		char *str = NULL;

		switch (get_column_id(out, i)) {
		case COL_IRQ:
			xasprintf(&str, "%s", info->irq);
			break;
		case COL_TOTAL:
			xasprintf(&str, "%ld", info->total);
			break;
		case COL_DELTA:
			xasprintf(&str, "%ld", info->delta);
			break;
		case COL_NAME:
			xasprintf(&str, "%s", info->name);
			break;
		default:
			break;
		}

		if (str && scols_line_refer_data(line, i, str) != 0)
			err_oom();
	}
}

static char *remove_repeated_spaces(char *str)
{
	char *inp = str, *outp = str;
	uint8_t prev_space = 0;

	while (*inp) {
		if (isspace(*inp)) {
			if (!prev_space) {
				*outp++ = ' ';
				prev_space = 1;
			}
		} else {
			*outp++ = *inp;
			prev_space = 0;
		}
		++inp;
	}
	*outp = '\0';
	return str;
}

/*
 * irqinfo - parse the system's interrupts
 */
static struct irq_stat *get_irqinfo(int softirq)
{
	FILE *irqfile;
	char *line = NULL, *tmp;
	size_t len = 0;
	struct irq_stat *stat;
	struct irq_info *curr;

	/* NAME + ':' + 11 bytes/cpu + IRQ_NAME_LEN */
	stat = xcalloc(1, sizeof(*stat));

	stat->irq_info = xmalloc(sizeof(*stat->irq_info) * IRQ_INFO_LEN);
	stat->nr_irq_info = IRQ_INFO_LEN;

	if (softirq)
		irqfile = fopen(_PATH_PROC_SOFTIRQS, "r");
	else
		irqfile = fopen(_PATH_PROC_INTERRUPTS, "r");
	if (!irqfile) {
		warn(_("cannot open %s"), _PATH_PROC_INTERRUPTS);
		goto free_stat;
	}

	/* read header firstly */
	if (getline(&line, &len, irqfile) < 0) {
		warn(_("cannot read %s"), _PATH_PROC_INTERRUPTS);
		goto close_file;
	}

	tmp = line;
	while ((tmp = strstr(tmp, "CPU")) != NULL) {
		tmp += 3;	/* skip this "CPU", find next */
		stat->nr_active_cpu++;
	}

	stat->cpus =  xcalloc(stat->nr_active_cpu, sizeof(struct irq_cpu));

	/* parse each line of _PATH_PROC_INTERRUPTS */
	while (getline(&line, &len, irqfile) >= 0) {
		unsigned long count;
		size_t index;
		int length;

		tmp = strchr(line, ':');
		if (!tmp)
			continue;

		length = strlen(line);

		curr = stat->irq_info + stat->nr_irq++;
		memset(curr, 0, sizeof(*curr));
		*tmp = '\0';
		curr->irq = xstrdup(line);
		ltrim_whitespace((unsigned char *)curr->irq);

		tmp += 1;
		for (index = 0; (index < stat->nr_active_cpu) && (tmp - line < length); index++) {
			struct irq_cpu *cpu = &stat->cpus[index];

			if (sscanf(tmp, " %10lu", &count) != 1)
				continue;
			curr->total += count;
			cpu->total += count;
			stat->total_irq += count;

			tmp += 11;
		}

		/* softirq always has no desc, add additional desc for softirq */
		if (softirq)
			get_softirq_desc(curr);
		else {
			if (tmp - line < length) {
				/* strip all space before desc */
				while (isspace(*tmp))
					tmp++;
				tmp = remove_repeated_spaces(tmp);
				rtrim_whitespace((unsigned char *)tmp);
				curr->name = xstrdup(tmp);
			} else /* no irq name string, we have to set '\0' here */
				curr->name = xstrdup("");
		}

		if (stat->nr_irq == stat->nr_irq_info) {
			stat->nr_irq_info *= 2;
			stat->irq_info = xrealloc(stat->irq_info,
						  sizeof(*stat->irq_info) * stat->nr_irq_info);
		}
	}
	fclose(irqfile);
	free(line);
	return stat;

 close_file:
	fclose(irqfile);
 free_stat:
	free(stat->irq_info);
	free(stat->cpus);
	free(stat);
	free(line);
	return NULL;
}

void free_irqstat(struct irq_stat *stat)
{
	size_t i;

	if (!stat)
		return;

	for (i = 0; i < stat->nr_irq; i++) {
		free(stat->irq_info[i].name);
		free(stat->irq_info[i].irq);
	}

	free(stat->irq_info);
	free(stat->cpus);
	free(stat);
}

static inline int cmp_name(const struct irq_info *a,
		     const struct irq_info *b)
{
	return (strcmp(a->name, b->name) > 0) ? 1 : 0;
}

static inline int cmp_total(const struct irq_info *a,
		      const struct irq_info *b)
{
	return a->total < b->total;
}

static inline int cmp_delta(const struct irq_info *a,
		      const struct irq_info *b)
{
	return a->delta < b->delta;
}

static inline int cmp_interrupts(const struct irq_info *a,
			   const struct irq_info *b)
{
	return (strverscmp(a->irq, b->irq) > 0) ? 1 : 0;
}

static void sort_result(struct irq_output *out,
			struct irq_info *result,
			size_t nmemb)
{
	irq_cmp_t *func = cmp_total;	/* default */

	if (out->sort_cmp_func)
		func = out->sort_cmp_func;

	qsort(result, nmemb, sizeof(*result),
			(int (*)(const void *, const void *)) func);
}

void set_sort_func_by_name(struct irq_output *out, const char *name)
{
	if (strcasecmp(name, "IRQ") == 0)
		out->sort_cmp_func = cmp_interrupts;
	else if (strcasecmp(name, "TOTAL") == 0)
		out->sort_cmp_func = cmp_total;
	else if (strcasecmp(name, "DELTA") == 0)
		out->sort_cmp_func = cmp_delta;
	else if (strcasecmp(name, "NAME") == 0)
		out->sort_cmp_func = cmp_name;
	else
		errx(EXIT_FAILURE, _("unsupported column name to sort output"));
}

void set_sort_func_by_key(struct irq_output *out, char c)
{
	switch (c) {
	case 'i':
		out->sort_cmp_func = cmp_interrupts;
		break;
	case 't':
		out->sort_cmp_func = cmp_total;
		break;
	case 'd':
		out->sort_cmp_func = cmp_delta;
		break;
	case 'n':
		out->sort_cmp_func = cmp_name;
		break;
	}
}

struct libscols_table *get_scols_cpus_table(struct irq_output *out,
					struct irq_stat *prev,
					struct irq_stat *curr)
{
	struct libscols_table *table;
	struct libscols_column *cl;
	struct libscols_line *ln;
	char colname[sizeof("cpu") + sizeof(stringify_value(LONG_MAX))];
	size_t i;

	if (prev) {
		for (i = 0; i < curr->nr_active_cpu; i++) {
			struct irq_cpu *pre = &prev->cpus[i];
			struct irq_cpu *cur = &curr->cpus[i];

			cur->delta = cur->total - pre->total;
		}
	}

	table = scols_new_table();
	if (!table) {
		warn(_("failed to initialize output table"));
		return NULL;
	}
	scols_table_enable_json(table, out->json);
	scols_table_enable_noheadings(table, out->no_headings);
	scols_table_enable_export(table, out->pairs);

	if (out->json)
		scols_table_set_name(table, _("cpu-interrupts"));
	else
		scols_table_new_column(table, "", 0, SCOLS_FL_RIGHT);

	for (i = 0; i < curr->nr_active_cpu; i++) {
		snprintf(colname, sizeof(colname), "cpu%zu", i);
		cl = scols_table_new_column(table, colname, 0, SCOLS_FL_RIGHT);
		if (cl == NULL) {
			warnx(_("failed to initialize output column"));
			goto err;
		}
		if (out->json)
			scols_column_set_json_type(cl, SCOLS_JSON_STRING);
	}

	/* per cpu % of total */
	ln = new_scols_line(table);
	if (!ln || (!out->json && scols_line_set_data(ln, 0, "%irq:") != 0))
		goto err;

	for (i = 0; i < curr->nr_active_cpu; i++) {
		struct irq_cpu *cpu = &curr->cpus[i];
		char *str;

		xasprintf(&str, "%0.1f", (double)((long double) cpu->total / (long double) curr->total_irq * 100.0));
		if (str && scols_line_refer_data(ln, i + 1, str) != 0)
			goto err;
	}

	/* per cpu % of delta */
	ln = new_scols_line(table);
	/* xgettext:no-c-format */
	if (!ln || (!out->json && scols_line_set_data(ln, 0, _("%delta:")) != 0))
		goto err;

	for (i = 0; i < curr->nr_active_cpu; i++) {
		struct irq_cpu *cpu = &curr->cpus[i];
		char *str;

		if (!curr->delta_irq)
			continue;
		xasprintf(&str, "%0.1f", (double)((long double) cpu->delta / (long double) curr->delta_irq * 100.0));
		if (str && scols_line_refer_data(ln, i + 1, str) != 0)
			goto err;
	}

	return table;
 err:
	scols_unref_table(table);
	return NULL;
}

struct libscols_table *get_scols_table(struct irq_output *out,
					      struct irq_stat *prev,
					      struct irq_stat **xstat,
					      int softirq)
{
	struct libscols_table *table;
	struct irq_info *result;
	struct irq_stat *stat;
	size_t size;
	size_t i;

	/* the stats */
	stat = get_irqinfo(softirq);
	if (!stat)
		return NULL;

	size = sizeof(*stat->irq_info) * stat->nr_irq;
	result = xmalloc(size);
	memcpy(result, stat->irq_info, size);

	if (prev) {
		stat->delta_irq = 0;
		for (i = 0; i < stat->nr_irq; i++) {
			struct irq_info *cur = &result[i];
			struct irq_info *pre = &prev->irq_info[i];

			cur->delta = cur->total - pre->total;
			stat->delta_irq += cur->delta;
		}
	}
	sort_result(out, result, stat->nr_irq);

	table = new_scols_table(out);
	if (!table) {
		free(result);
		free_irqstat(stat);
		return NULL;
	}

	for (i = 0; i < stat->nr_irq; i++)
		add_scols_line(out, &result[i], table);

	free(result);

	if (xstat)
		*xstat = stat;
	else
		free_irqstat(stat);

	return table;
}