Skip to contentSkip to conversation

lsirq,irqtop: Add filter support

RFC PATCH In review Joe Jin 3 participants 8 messages Mar 7, 2025 – Mar 10, 2025 Util-Linux package

lore
Share & tools
lore

Joe Jin

author
#0 lore raw

cover lsirq,irqtop: Add filter support

Sometimes people may only be interested in a specific IRQ, adding regular expression support can help engineers find/monitor IRQs easily.

Joe Jin (2):
lsirq: Add filter support
irqtop: add filter support

 sys-utils/irq-common.c  | 88 +++++++++++++++++++++++++++++++++++++++--
 sys-utils/irq-common.h  |  3 +-
 sys-utils/irqtop.1.adoc |  4 +-
 sys-utils/irqtop.c      | 29 +++++++++++++-
 sys-utils/lsirq.1.adoc  |  4 +-
 sys-utils/lsirq.c       | 29 ++++++++++++--
 6 files changed, 145 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.5
author
#1 lore raw

RFC PATCH 1/2 lsirq: add filter support

Cc Zhenwei Pi <[email protected]>
Cc Sami Kerola <[email protected]>
Cc Karel Zak <[email protected]>
---
 sys-utils/irq-common.c | 88 ++++++++++++++++++++++++++++++++++++++++--
 sys-utils/irq-common.h |  3 +-
 sys-utils/irqtop.c     |  2 +-
 sys-utils/lsirq.1.adoc |  4 +-
 sys-utils/lsirq.c      | 29 ++++++++++++--
 5 files changed, 116 insertions(+), 10 deletions(-)
sys-utils/irq-common.c +84 -4
index 560dd4b82..059f57e3a 100644
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <regex.h>
 
 #include <libsmartcols.h>
 
@@ -230,11 +231,67 @@ static bool cpu_in_list(int cpu, size_t setsize, cpu_set_t *cpuset)
 	return CPU_ISSET_S(cpu, setsize, cpuset);
 }
 
+static bool match_irqname_by_regex(char *regex_str, const char *irqname)
+{
+	regex_t re;
+	int ret;
+	char *expr = NULL;
+	int i, j;
+
+	/* Exactly matches the string */
+	if (strcmp(regex_str, irqname) == 0 || strcmp(regex_str, "*") == 0)
+	       return true;
+
+	/*
+	 * To generate ls-style regex we need:
+	 *   - Add '^' at the beginning.
+	 *   - Replace '*' with '.*'
+	 *   - Append '$' to the end.
+	 */
+	expr = xmalloc(strlen(regex_str) + 3);
+	memset(expr, 0, strlen(regex_str) + 3);
+	/* Add '^' at the beginning */
+	expr[0] = '^';
+
+	/* Replace '*' with '.*' */
+	for (i = 0, j = 1; regex_str[i] != '\0'; i++, j++) {
+		if (regex_str[i] == '*') {
+			if ((i > 0 && regex_str[i-1] != '.') || i == 0) {
+				expr = realloc(expr, strlen(regex_str) + (j - i) + 1);
+				expr[j] = '.';
+				expr[++j] = '*';
+			} else {
+				expr[j] = regex_str[i];
+			}
+		} else {
+			expr[j] = regex_str[i];
+		}
+	}
+
+	/* Append '$' to the end if necessary */
+	if (regex_str[strlen(regex_str)-1] != '$') {
+		expr[j] = '$';
+		expr[j+1] = '\0';
+	} else {
+		expr[j] = '\0';
+	}
+
+	/* Try regex */
+	if (regcomp(&re, expr, REG_NOSUB|REG_EXTENDED) != 0)
+		return false;
+
+	ret = regexec(&re, irqname, 0, NULL, 0) == 0 ? true : false;
+	regfree(&re);
+
+	free(expr);
+	return ret;
+}
+
 /*
  * irqinfo - parse the system's interrupts
  */
 static struct irq_stat *get_irqinfo(const char *input_file, int softirq,
-				    size_t setsize, cpu_set_t *cpuset)
+				    size_t setsize, cpu_set_t *cpuset, char **filters)
 {
 	FILE *irqfile;
 	char *line = NULL, *tmp;
@@ -280,7 +337,7 @@ static struct irq_stat *get_irqinfo(const char *input_file, int softirq,
 
 		length = strlen(line);
 
-		curr = stat->irq_info + stat->nr_irq++;
+		curr = stat->irq_info + stat->nr_irq;
 		memset(curr, 0, sizeof(*curr));
 		*tmp = '\0';
 		curr->irq = xstrdup(line);
@@ -316,6 +373,28 @@ static struct irq_stat *get_irqinfo(const char *input_file, int softirq,
 				curr->name = xstrdup("");
 		}
 
+		/* Match filter */
+		if (filters) {
+			int matched = 0;
+			char **saved = filters;
+
+			while (*saved) {
+				if (strcmp(curr->irq, *saved) == 0 ||
+				    match_irqname_by_regex(*saved, curr->name) == true) {
+					matched = 1;
+					break;
+				}
+				saved++;
+			}
+
+			if (!matched) {
+				memset(curr, 0, sizeof(*curr));
+				continue;
+			}
+		}
+
+		stat->nr_irq++;
+
 		if (stat->nr_irq == stat->nr_irq_info) {
 			stat->nr_irq_info *= 2;
 			stat->irq_info = xreallocarray(stat->irq_info, stat->nr_irq_info,
@@ -532,7 +611,8 @@ struct libscols_table *get_scols_table(const char *input_file,
 					      int softirq,
 					      uintmax_t threshold,
 					      size_t setsize,
-					      cpu_set_t *cpuset)
+					      cpu_set_t *cpuset,
+					      char **filters)
 {
 	struct libscols_table *table;
 	struct irq_info *result;
@@ -541,7 +621,7 @@ struct libscols_table *get_scols_table(const char *input_file,
 	size_t i;
 
 	/* the stats */
-	stat = get_irqinfo(input_file, softirq, setsize, cpuset);
+	stat = get_irqinfo(input_file, softirq, setsize, cpuset, filters);
 	if (!stat)
 		return NULL;
 
sys-utils/irq-common.h +2 -1
index b9cf72d2a..ec6c89d01 100644
@@ -80,7 +80,8 @@ struct libscols_table *get_scols_table(const char *input_file,
                                               int softirq,
                                               uintmax_t threshold,
                                               size_t setsize,
-                                              cpu_set_t *cpuset);
+                                              cpu_set_t *cpuset,
+                                              char **filters);
 
 struct libscols_table *get_scols_cpus_table(struct irq_output *out,
                                         struct irq_stat *prev,
sys-utils/irqtop.c +1 -1
index 6d001cb10..dc59f4829 100644
@@ -140,7 +140,7 @@ static int update_screen(struct irqtop_ctl *ctl, struct irq_output *out)
 
 	table = get_scols_table(input_file, out, ctl->prev_stat, &stat,
 				ctl->softirq, ctl->threshold, ctl->setsize,
-				ctl->cpuset);
+				ctl->cpuset, NULL);
 	free(input_file);
 	if (!table) {
 		ctl->request_exit = 1;
sys-utils/lsirq.1.adoc +3 -1
index dd265710c..7a3bfb69e 100644
@@ -12,7 +12,7 @@ lsirq - utility to display kernel interrupt information
 
 == SYNOPSIS
 
-*lsirq* [options]
+*lsirq* [options]... [IRQ] [PATTERN] ...
 
 == DESCRIPTION
 
@@ -20,6 +20,8 @@ Display kernel interrupt counter information.
 
 The default output is subject to change. So whenever possible, you should avoid using default outputs in your scripts. Always explicitly define expected columns by using *--output*.
 
+Displays interrupt counter information only for the specified _IRQ_ or for irqnames matching _PATTERN_ when given.
+
 == OPTIONS
 
 *-n*, *--noheadings*::
sys-utils/lsirq.c +26 -3
index fa2dcaaf3..a6528889f 100644
@@ -35,11 +35,11 @@
 
 static int print_irq_data(const char *input_file, struct irq_output *out,
 			  int softirq, unsigned long threshold,
-			  size_t setsize, cpu_set_t *cpuset)
+			  size_t setsize, cpu_set_t *cpuset, char **filters)
 {
 	struct libscols_table *table;
 
-	table = get_scols_table(input_file, out, NULL, NULL, softirq, threshold, setsize, cpuset);
+	table = get_scols_table(input_file, out, NULL, NULL, softirq, threshold, setsize, cpuset, filters);
 	if (!table)
 		return -1;
 
@@ -78,6 +78,8 @@ static void __attribute__((__noreturn__)) usage(void)
 
 int main(int argc, char **argv)
 {
+	char **filters = NULL;
+	int filters_len;
 	struct irq_output out = {
 		.ncolumns = 0
 	};
@@ -171,6 +173,18 @@ int main(int argc, char **argv)
 			input_file = xstrdup(_PATH_PROC_INTERRUPTS);
 	}
 
+	if (optind < argc) {
+		int i;
+
+		filters_len = argc - optind + 1;
+		filters = (char **)xmalloc(filters_len * sizeof(void *));
+		memset(filters, 0, filters_len * sizeof(void *));
+
+		for (i = optind; i < argc; i++)
+			filters[i - optind] = xstrdup(argv[i]);
+		filters[i - optind] = NULL;
+	}
+
 	/* default */
 	if (!out.ncolumns) {
 		out.columns[out.ncolumns++] = COL_IRQ;
@@ -185,9 +199,18 @@ int main(int argc, char **argv)
 				irq_column_name_to_id) < 0)
 		exit(EXIT_FAILURE);
 
-	if (print_irq_data(input_file, &out, softirq, threshold, setsize, cpuset) < 0)
+	if (print_irq_data(input_file, &out, softirq, threshold, setsize, cpuset, filters) < 0)
 		return EXIT_FAILURE;
 
+	if (filters) {
+		char **saved = filters;
+
+		while(*filters) {
+			free(*filters);
+			filters++;
+		}
+		free(saved);
+	}
 	free(input_file);
 
 	return EXIT_SUCCESS;
Signature · 1 line
2.43.5
author
#2 lore raw

RFC PATCH 2/2 irqtop: add filter support

In reply to Joe Jin
Cc Zhenwei Pi <[email protected]>
Cc Sami Kerola <[email protected]>
Cc Karel Zak <[email protected]>
---
 sys-utils/irqtop.1.adoc |  4 +++-
 sys-utils/irqtop.c      | 29 +++++++++++++++++++++++++++--
 2 files changed, 30 insertions(+), 3 deletions(-)
sys-utils/irqtop.1.adoc +3 -1
index 880ee1906..b9962ce1c 100644
@@ -12,7 +12,7 @@ irqtop - utility to display kernel interrupt information
 
 == SYNOPSIS
 
-*irqtop* [options]
+*irqtop* [options]... [IRQ] [PATTERN] ...
 
 == DESCRIPTION
 
@@ -20,6 +20,8 @@ Display kernel interrupt counter information in *top*(1) style view.
 
 The default output is subject to change. So whenever possible, you should avoid using default outputs in your scripts. Always explicitly define expected columns by using *--output*.
 
+Displays interrupt counter information only for the specified _IRQ_ or for irqnames matching _PATTERN_ when given.
+
 == OPTIONS
 
 *-o*, *--output* _list_::
sys-utils/irqtop.c +27 -2
index dc59f4829..c5deb840b 100644
@@ -87,6 +87,8 @@ struct irqtop_ctl {
 	bool	batch;
 	bool	request_exit,
 		softirq;
+
+	char	**filters;
 };
 
 static inline int irqtop_printf(struct irqtop_ctl *ctl, const char *fmt, ...)
@@ -140,7 +142,7 @@ static int update_screen(struct irqtop_ctl *ctl, struct irq_output *out)
 
 	table = get_scols_table(input_file, out, ctl->prev_stat, &stat,
 				ctl->softirq, ctl->threshold, ctl->setsize,
-				ctl->cpuset, NULL);
+				ctl->cpuset, ctl->filters);
 	free(input_file);
 	if (!table) {
 		ctl->request_exit = 1;
@@ -426,6 +428,19 @@ static void parse_args(	struct irqtop_ctl *ctl,
 		}
 	}
 
+	if (optind < argc) {
+		int i;
+		int filters_len;
+
+		filters_len = argc - optind + 1;
+		ctl->filters = (char **)xmalloc(filters_len * sizeof(void *));
+		memset(ctl->filters, 0, filters_len * sizeof(void *));
+
+		for (i = optind; i < argc; i++)
+			ctl->filters[i - optind] = xstrdup(argv[i]);
+		ctl->filters[i - optind] = NULL;
+	}
+
 	/* default */
 	if (!out->ncolumns) {
 		out->columns[out->ncolumns++] = COL_IRQ;
@@ -452,7 +467,8 @@ int main(int argc, char **argv)
 	struct irqtop_ctl ctl = {
 		.timer.it_interval = {3, 0},
 		.timer.it_value = {3, 0},
-		.iter = -1
+		.iter = -1,
+		.filters = NULL
 	};
 
 	setlocale(LC_ALL, "");
@@ -478,6 +494,15 @@ int main(int argc, char **argv)
 	free_irqstat(ctl.prev_stat);
 	free(ctl.hostname);
 	cpuset_free(ctl.cpuset);
+	if (ctl.filters) {
+		char **saved = ctl.filters;
+
+		while(*(ctl.filters)) {
+			free(*(ctl.filters));
+			(ctl.filters)++;
+		}
+		free(saved);
+	}
 
 	if (!ctl.batch) {
 		if (is_tty)
Signature · 1 line
2.43.5
#3 lore raw

Re: [RFC PATCH 2/2] irqtop: add filter support

Op 07-03-2025 om 03:52 schreef Joe Jin:

sys-utils/irqtop.1.adoc irqtop - utility to display kernel interrupt information · line 15

Quoted 2 lines
sys-utils/irqtop.1.adoc +1 -1
-*irqtop* [options]
+*irqtop* [options]... [IRQ] [PATTERN] ...

There should be no dots after "[options]", as the "options" word already implies that it can be multiple ones.

(True, it's not fully precise -- better would have been:
"[option...]". But don't use that. Please use what all the other synopses use: "[options]", sans dots.)

Also, the convention nowadays is to put placeholder words in lower case and between angular brackets:
"[<irq>] [<pattern>]..."

Benno

author
#4 lore raw

Re: [RFC PATCH 2/2] irqtop: add filter support

Quoted 9 lines · Benno Schulenberg

Op 07-03-2025 om 03:52 schreef Joe Jin:

sys-utils/irqtop.1.adoc irqtop - utility to display kernel interrupt information · line 15

Quoted 2 lines
sys-utils/irqtop.1.adoc +1 -1
-*irqtop* [options]
+*irqtop* [options]... [IRQ] [PATTERN] ...

There should be no dots after "[options]", as the "options" word already implies that it can be multiple ones.

Quoted 7 lines

(True, it's not fully precise -- better would have been:
"[option...]".  But don't use that.  Please use what all the other synopses use: "[options]", sans dots.)

Also, the convention nowadays is to put placeholder words in lower case and between angular brackets:
"[<irq>] [<pattern>]..."

Thanks for your suggestion, I'll make the change.

Bests,
Joe

Quoted 4 lines

Benno

#5 lore raw

Re: [RFC PATCH 2/2] irqtop: add filter support

In reply to Joe Jin

Op 07-03-2025 om 03:52 schreef Joe Jin:

sys-utils/irqtop.1.adoc irqtop - utility to display kernel interrupt information · line 15

Quoted 8 lines
sys-utils/irqtop.1.adoc +1 -1
@@ -12,7 +12,7 @@ irqtop - utility to display kernel interrupt information
 
  == SYNOPSIS
 
-*irqtop* [options]
+*irqtop* [options]... [IRQ] [PATTERN] ...

Oh -- I failed to notice that this change is for the man page, not for the usage help text.

In a man page, the convention is to put placeholder words in lowercase italics. That is: mark them with underscores:

*irqtop* [options] [_irq_] [_pattern_] ...

If, however, it is possible to specify multiple irq numbers and irq names, and one can mix them, then I would write:

*irqtop* [options] [_irq_|_pattern_]...

sys-utils/irqtop.1.adoc Display kernel interrupt counter information in *top*(1) style view. · line 23

Quoted 1 line
sys-utils/irqtop.1.adoc +1 -0
+Displays interrupt counter information only for the specified _IRQ_ or for irqnames matching _PATTERN_ when given.

Here, lowercase the underscored words, and "irq names" is better as two separate words.

Benno

maintainer
#6 lore raw
In reply to Joe Jin
Quoted 2 lines · Joe Jin

Sometimes people may only be interested in a specific IRQ, adding regular expression support can help engineers find/monitor IRQs easily.

Ah, the libsmartcols provides a filter. See, for example, misc-utils/lsblk.c and the "-Q" command line option, and also "man scols-filter".

scols_new_filter()
scols_line_apply_filter()
scols_line_is_filled()
... etc.

https://www.kernel.org/pub/linux/utils/util-linux/v2.41/libsmartcols-docs/libsmartcols-Filters-and-counters.html

The filter allows the creation of complex queries, so if we integrate it into lsirq and irqtop, it will be possible to use things like:

    irqtop -Q 'NAME ~= "PCI\-.*" && TOTAL > 100'

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
author
#7 lore raw
Quoted 11 lines · Karel Zak
Quoted 2 lines · Joe Jin

Sometimes people may only be interested in a specific IRQ, adding regular expression support can help engineers find/monitor IRQs easily.

Ah, the libsmartcols provides a filter. See, for example, misc-utils/lsblk.c and the "-Q" command line option, and also "man scols-filter".

scols_new_filter()
scols_line_apply_filter()
scols_line_is_filled()
... etc.

Quoted 6 lines

https://www.kernel.org/pub/linux/utils/util-linux/v2.41/libsmartcols-docs/libsmartcols-Filters-and-counters.html

The filter allows the creation of complex queries, so if we integrate it into lsirq and irqtop, it will be possible to use things like:

    irqtop -Q 'NAME ~= "PCI\-.*" && TOTAL > 100'

This looks more powerful, thanks for your suggestions.

Best Regards,
Joe

Quoted 4 lines

Karel