]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
irqtop: cleanup sort stuff
authorKarel Zak <kzak@redhat.com>
Fri, 6 Mar 2020 14:28:57 +0000 (15:28 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 6 Mar 2020 14:29:03 +0000 (15:29 +0100)
* user "cmp" in the function names (it does not sort)
* keep all in irq-common.c
* use column names on command line (e.g. --sort NAME)
* hardcode default to sort_result()

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/irq-common.c
sys-utils/irq-common.h
sys-utils/irqtop.c

index 9766899c76b2dc2bd6242a578c0d6e2c988a298a..e0f5c720c3535f43e2dcd3b4ee42402d3b06c1c8 100644 (file)
@@ -293,28 +293,72 @@ void free_irqstat(struct irq_stat *stat)
        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 (strcmp(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 *))out->sort_func);
+                       (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, _("unssupported column name to sort output"));
 }
 
-sort_fp *set_sort_func(char key)
+void set_sort_func_by_key(struct irq_output *out, char c)
 {
-       switch (key) {
+       switch (c) {
        case 'i':
-               return sort_interrupts;
+               out->sort_cmp_func = cmp_interrupts;
+               break;
        case 't':
-               return sort_total;
+               out->sort_cmp_func = cmp_total;
+               break;
        case 'd':
-               return sort_delta;
+               out->sort_cmp_func = cmp_delta;
+               break;
        case 'n':
-               return sort_name;
-       default:
-               return DEF_SORT_FUNC;
+               out->sort_cmp_func = cmp_name;
+               break;
        }
 }
 
index 1dc760da83a098842f4881f83b4902f21aed3979..a0646b129d8e807f4404d559ec43cfa899267124 100644 (file)
@@ -31,16 +31,14 @@ struct irq_stat {
        unsigned long delta_irq;        /* delta irqs */
 };
 
-typedef int (sort_fp)(const struct irq_info *, const struct irq_info *);
-
-#define DEF_SORT_FUNC  ((sort_fp *)sort_total)
+typedef int (irq_cmp_t)(const struct irq_info *, const struct irq_info *);
 
 /* output definition */
 struct irq_output {
        int columns[__COL_COUNT * 2];
        size_t ncolumns;
 
-       sort_fp *sort_func;
+       irq_cmp_t *sort_cmp_func;
 
        unsigned int
                json:1,
@@ -52,31 +50,8 @@ void free_irqstat(struct irq_stat *stat);
 
 void irq_print_columns(FILE *f);
 
-static inline int sort_name(const struct irq_info *a,
-                    const struct irq_info *b)
-{
-       return (strcmp(a->name, b->name) > 0) ? 1 : 0;
-}
-
-static inline int sort_total(const struct irq_info *a,
-                     const struct irq_info *b)
-{
-       return a->total < b->total;
-}
-
-static inline int sort_delta(const struct irq_info *a,
-                     const struct irq_info *b)
-{
-       return a->delta < b->delta;
-}
-
-static inline int sort_interrupts(const struct irq_info *a,
-                          const struct irq_info *b)
-{
-       return (strcmp(a->irq, b->irq) > 0) ? 1 : 0;
-}
-
-sort_fp *set_sort_func(char key);
+void set_sort_func_by_name(struct irq_output *out, const char *name);
+void set_sort_func_by_key(struct irq_output *out, const char c);
 
 struct libscols_table *get_scols_table(struct irq_output *out,
                                               struct irq_stat *prev,
index 8c15712ee5afeddb47aae81a2ee0b7cb4664c207..2739e4e352b927caf1e12886ddde076813005db6 100644 (file)
@@ -85,22 +85,13 @@ struct irqtop_ctl {
 static void parse_input(struct irqtop_ctl *ctl, struct irq_output *out, char c)
 {
        switch (c) {
-       case 'i':
-               out->sort_func = sort_interrupts;
-               break;
-       case 't':
-               out->sort_func = sort_total;
-               break;
-       case 'd':
-               out->sort_func = sort_delta;
-               break;
-       case 'n':
-               out->sort_func = sort_name;
-               break;
        case 'q':
        case 'Q':
                ctl->request_exit = 1;
                break;
+       default:
+               set_sort_func_by_key(out, c);
+               break;
        }
 }
 
@@ -228,8 +219,8 @@ static void __attribute__((__noreturn__)) usage(void)
 
        fputs(USAGE_OPTIONS, stdout);
        fputs(_(" -d, --delay <secs>   delay updates\n"), stdout);
-       fputs(_(" -o  --output <list>  define which output columns to use (see below)\n"), stdout);
-       fputs(_(" -s, --sort <char>    specify sort criteria by character (see below)\n"), stdout);
+       fputs(_(" -o, --output <list>  define which output columns to use\n"), stdout);
+       fputs(_(" -s, --sort <column>  specify sort column\n"), stdout);
        fputs(USAGE_SEPARATOR, stdout);
        printf(USAGE_HELP_OPTIONS(22));
 
@@ -263,7 +254,7 @@ static void parse_args(     struct irqtop_ctl *ctl,
        };
        int o;
 
-       while ((o = getopt_long(argc, argv, "d:o:s:hJV", longopts, NULL)) != -1) {
+       while ((o = getopt_long(argc, argv, "d:o:s:hV", longopts, NULL)) != -1) {
                switch (o) {
                case 'd':
                        {
@@ -276,7 +267,7 @@ static void parse_args(     struct irqtop_ctl *ctl,
                        }
                        break;
                case 's':
-                       out->sort_func = set_sort_func(optarg[0]);
+                       set_sort_func_by_name(out, optarg);
                        break;
                case 'o':
                        outarg = optarg;
@@ -311,7 +302,7 @@ int main(int argc, char **argv)
        int is_tty = 0;
        struct termios saved_tty;
        struct irq_output out = {
-               .sort_func = DEF_SORT_FUNC
+               .ncolumns = 0
        };
        struct irqtop_ctl ctl = {
                .timer.it_interval = {3, 0},