]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
irqtop: add max iteration support
authorJoe Jin <joe.jin@oracle.com>
Fri, 28 Feb 2025 16:13:32 +0000 (08:13 -0800)
committerKarel Zak <kzak@redhat.com>
Mon, 3 Mar 2025 08:26:20 +0000 (09:26 +0100)
Add support for setting the number of iterations. This is useful in
non-interactive mode.

Signed-off-by: Joe Jin <joe.jin@oracle.com>
Cc: Zhenwei Pi <pizhenwei@bytedance.com>
Cc: Sami Kerola <kerolasa@iki.fi>
Cc: Karel Zak <kzak@redhat.com>
bash-completion/irqtop
sys-utils/irqtop.1.adoc
sys-utils/irqtop.c

index 215281ee8c426050c138b16c41cf9bfdc4d53d69..d18ef99bbb7ff311c5d94f5acecf23bee2c10540 100644 (file)
@@ -22,6 +22,10 @@ _irqtop_module()
                        COMPREPLY=( $(compgen -W "secs" -- $cur) )
                        return 0
                        ;;
+               '-n'|'--iter')
+                       COMPREPLY=( $(compgen -W "the max iterations" -- $cur) )
+                       return 0
+                       ;;
                '-s'|'--sort')
                        COMPREPLY=( $(compgen -W "irq total delta name" -- $cur) )
                        return 0
@@ -47,6 +51,7 @@ _irqtop_module()
                --cpu-stat
                --cpu-list
                --delay
+               --iter
                --sort
                --output
                --softirq
index e81f4fbb6b6a4cf2872a5f151b6a682de82c2723..75930f5cfe89e1ec26a97f96c5ccb2a76c907702 100644 (file)
@@ -37,6 +37,9 @@ Specify cpus in list format to show.
 *-d*, *--delay* _seconds_::
 Update interrupt output every _seconds_ intervals.
 
+*-n*, *--iter* _number_::
+Specifies the maximum iterations before quitting.
+
 *-s*, *--sort* _column_::
 Specify sort criteria by column name. See *--help* output to get column names. The sort criteria may be changes in interactive mode.
 
index 81a137be07bc019182241e9b52008c5666164191..17c7d72cb98c25925a9603b17979a8599c6f4c56 100644 (file)
@@ -83,6 +83,7 @@ struct irqtop_ctl {
        cpu_set_t *cpuset;
 
        enum irqtop_cpustat_mode cpustat_mode;
+       int64_t iter;
        bool    batch;
        bool    request_exit,
                softirq;
@@ -190,6 +191,12 @@ static int update_screen(struct irqtop_ctl *ctl, struct irq_output *out)
        if (ctl->prev_stat)
                free_irqstat(ctl->prev_stat);
        ctl->prev_stat = stat;
+
+       if (ctl->iter > 0) {
+               ctl->iter--;
+               if (ctl->iter == 0)
+                       ctl->request_exit = 1;
+       }
        return 0;
 }
 
@@ -295,6 +302,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -c, --cpu-stat <mode> show per-cpu stat (auto, enable, disable)\n"), stdout);
        fputs(_(" -C, --cpu-list <list> specify cpus in list format\n"), stdout);
        fputs(_(" -d, --delay <secs>   delay updates\n"), stdout);
+       fputs(_(" -n, --iter <number>  the maximum number of iterations\n"), stdout);
        fputs(_(" -o, --output <list>  define which output columns to use\n"), stdout);
        fputs(_(" -s, --sort <column>  specify sort column\n"), stdout);
        fputs(_(" -S, --softirq        show softirqs instead of interrupts\n"), stdout);
@@ -327,6 +335,7 @@ static void parse_args(     struct irqtop_ctl *ctl,
                {"cpu-stat", required_argument, NULL, 'c'},
                {"cpu-list", required_argument, NULL, 'C'},
                {"delay", required_argument, NULL, 'd'},
+               {"iter", required_argument, NULL, 'n'},
                {"sort", required_argument, NULL, 's'},
                {"output", required_argument, NULL, 'o'},
                {"softirq", no_argument, NULL, 'S'},
@@ -337,7 +346,7 @@ static void parse_args(     struct irqtop_ctl *ctl,
        };
        int o;
 
-       while ((o = getopt_long(argc, argv, "bc:C:d:o:s:St:hV", longopts, NULL)) != -1) {
+       while ((o = getopt_long(argc, argv, "bc:C:d:n:o:s:St:hV", longopts, NULL)) != -1) {
                switch (o) {
                case 'b':
                        ctl->batch = 1;
@@ -377,6 +386,11 @@ static void parse_args(    struct irqtop_ctl *ctl,
                                ctl->timer.it_value = ctl->timer.it_interval;
                        }
                        break;
+               case 'n':
+                       ctl->iter = str2num_or_err(optarg, 10,
+                                       _("failed to parse iter argument"),
+                                       0, INT_MAX);
+                       break;
                case 's':
                        set_sort_func_by_name(out, optarg);
                        break;
@@ -423,7 +437,8 @@ int main(int argc, char **argv)
        };
        struct irqtop_ctl ctl = {
                .timer.it_interval = {3, 0},
-               .timer.it_value = {3, 0}
+               .timer.it_value = {3, 0},
+               .iter = -1
        };
 
        setlocale(LC_ALL, "");