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;
}
}
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,
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,
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;
}
}
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));
};
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':
{
}
break;
case 's':
- out->sort_func = set_sort_func(optarg[0]);
+ set_sort_func_by_name(out, optarg);
break;
case 'o':
outarg = optarg;
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},