From: Karel Zak Date: Fri, 6 Mar 2020 14:28:57 +0000 (+0100) Subject: irqtop: cleanup sort stuff X-Git-Tag: v2.36-rc1~196^2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=44e39c9940e3e2fbd66b3ec219d4b164cf03720f;p=thirdparty%2Futil-linux.git irqtop: cleanup sort stuff * 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 --- diff --git a/sys-utils/irq-common.c b/sys-utils/irq-common.c index 9766899c76..e0f5c720c3 100644 --- a/sys-utils/irq-common.c +++ b/sys-utils/irq-common.c @@ -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; } } diff --git a/sys-utils/irq-common.h b/sys-utils/irq-common.h index 1dc760da83..a0646b129d 100644 --- a/sys-utils/irq-common.h +++ b/sys-utils/irq-common.h @@ -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, diff --git a/sys-utils/irqtop.c b/sys-utils/irqtop.c index 8c15712ee5..2739e4e352 100644 --- a/sys-utils/irqtop.c +++ b/sys-utils/irqtop.c @@ -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 delay updates\n"), stdout); - fputs(_(" -o --output define which output columns to use (see below)\n"), stdout); - fputs(_(" -s, --sort specify sort criteria by character (see below)\n"), stdout); + fputs(_(" -o, --output define which output columns to use\n"), stdout); + fputs(_(" -s, --sort 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},