1 /* vi: set sw=4 ts=4: */
3 * Per-processor statistics, based on sysstat version 9.1.2 by Sebastien Godard
5 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //applet:IF_MPSTAT(APPLET(mpstat, _BB_DIR_BIN, _BB_SUID_DROP))
12 //kbuild:lib-$(CONFIG_MPSTAT) += mpstat.o
14 //config:config MPSTAT
15 //config: bool "mpstat"
18 //config: Per-processor statistics
21 #include <sys/utsname.h> /* struct utsname */
23 //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
24 #define debug(fmt, ...) ((void)0)
26 /* Size of /proc/interrupts line, CPU data excluded */
27 #define INTERRUPTS_LINE 64
28 /* Maximum number of interrupts */
30 #define NR_IRQCPU_PREALLOC 3
31 #define MAX_IRQNAME_LEN 16
32 #define MAX_PF_NAME 512
33 /* sysstat 9.0.6 uses width 8, but newer code which also prints /proc/softirqs
34 * data needs more: "interrupts" in /proc/softirqs have longer names,
35 * most are up to 8 chars, one (BLOCK_IOPOLL) is even longer.
36 * We are printing headers in the " IRQNAME/s" form, experimentally
37 * anything smaller than 10 chars looks ugly for /proc/softirqs stats.
39 #define INTRATE_SCRWIDTH 10
40 #define INTRATE_SCRWIDTH_STR "10"
43 #define SYSFS_DEVCPU "/sys/devices/system/cpu"
44 #define PROCFS_STAT "/proc/stat"
45 #define PROCFS_INTERRUPTS "/proc/interrupts"
46 #define PROCFS_SOFTIRQS "/proc/softirqs"
47 #define PROCFS_UPTIME "/proc/uptime"
51 typedef unsigned long long data_t;
52 typedef long long idata_t;
54 #define DATA_MAX ULLONG_MAX
56 typedef unsigned long data_t;
59 #define DATA_MAX ULONG_MAX
65 char irq_name[MAX_IRQNAME_LEN];
85 /* Globals. Sort by size and access frequency. */
89 unsigned cpu_nr; /* Number of CPUs */
90 unsigned irqcpu_nr; /* Number of interrupts per CPU */
91 unsigned softirqcpu_nr; /* Number of soft interrupts per CPU */
94 unsigned cpu_bitmap_len;
96 // 9.0.6 does not do it. Try "mpstat -A 1 2" - headers are repeated!
97 //smallint header_done;
98 //smallint avg_header_done;
99 unsigned char *cpu_bitmap; /* Bit 0: global, bit 1: 1st proc... */
100 data_t global_uptime[3];
101 data_t per_cpu_uptime[3];
102 struct stats_cpu *st_cpu[3];
103 struct stats_irq *st_irq[3];
104 struct stats_irqcpu *st_irqcpu[3];
105 struct stats_irqcpu *st_softirqcpu[3];
106 struct tm timestamp[3];
108 #define G (*ptr_to_globals)
109 #define INIT_G() do { \
110 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
113 /* The selected interrupts statistics (bits in G.options) */
123 static ALWAYS_INLINE int display_opt(int opt)
125 return (opt & G.options);
128 #if DATA_MAX > 0xffffffff
130 * Handle overflow conditions properly for counters which can have
131 * less bits than data_t, depending on the kernel version.
133 /* Surprisingly, on 32bit inlining is a size win */
134 static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
136 data_t v = curr - prev;
138 if ((idata_t)v < 0 /* curr < prev - counter overflow? */
139 && prev <= 0xffffffff /* kernel uses 32bit value for the counter? */
141 /* Add 33th bit set to 1 to curr, compensating for the overflow */
142 /* double shift defeats "warning: left shift count >= width of type" */
143 v += ((data_t)1 << 16) << 16;
148 static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
154 static double percent_value(data_t prev, data_t curr, data_t itv)
156 return ((double)overflow_safe_sub(prev, curr)) / itv * 100;
159 static double hz_value(data_t prev, data_t curr, data_t itv)
161 //bb_error_msg("curr:%lld prev:%lld G.hz:%u", curr, prev, G.hz);
162 return ((double)overflow_safe_sub(prev, curr)) / itv * G.hz;
165 static ALWAYS_INLINE data_t jiffies_diff(data_t old, data_t new)
167 data_t diff = new - old;
168 return (diff == 0) ? 1 : diff;
171 static int is_cpu_in_bitmap(unsigned cpu)
173 return G.cpu_bitmap[cpu >> 3] & (1 << (cpu & 7));
176 static void write_irqcpu_stats(struct stats_irqcpu *per_cpu_stats[],
179 int prev, int current,
180 const char *prev_str, const char *current_str)
184 struct stats_irqcpu *p0, *q0;
186 /* Check if number of IRQs has changed */
187 if (G.interval != 0) {
188 for (j = 0; j <= total_irqs; j++) {
189 p0 = &per_cpu_stats[current][j];
190 if (p0->irq_name[0] != '\0') {
191 q0 = &per_cpu_stats[prev][j];
192 if (strcmp(p0->irq_name, q0->irq_name) != 0) {
193 /* Strings are different */
201 printf("\n%-11s CPU", prev_str);
203 /* A bit complex code to "buy back" space if one header is too wide.
204 * Here's how it looks like. BLOCK_IOPOLL eats too much space,
205 * and latter headers use smaller width to compensate:
206 * ...BLOCK/s BLOCK_IOPOLL/s TASKLET/s SCHED/s HRTIMER/s RCU/s
207 * ... 2.32 0.00 0.01 17.58 0.14 141.96
209 int expected_len = 0;
211 for (j = 0; j < total_irqs; j++) {
212 p0 = &per_cpu_stats[current][j];
213 if (p0->irq_name[0] != '\0') {
214 int n = (INTRATE_SCRWIDTH-3) - (printed_len - expected_len);
215 printed_len += printf(" %*s/s", n > 0 ? n : 0, skip_whitespace(p0->irq_name));
216 expected_len += INTRATE_SCRWIDTH;
222 for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
223 /* Check if we want stats about this CPU */
224 if (!is_cpu_in_bitmap(cpu) && G.p_option) {
228 printf("%-11s %4u", current_str, cpu - 1);
230 for (j = 0; j < total_irqs; j++) {
231 /* IRQ field set only for proc 0 */
232 p0 = &per_cpu_stats[current][j];
235 * An empty string for irq name means that
236 * interrupt is no longer used.
238 if (p0->irq_name[0] != '\0') {
240 q0 = &per_cpu_stats[prev][offset];
243 * If we want stats for the time since boot
244 * we have p0->irq != q0->irq.
246 if (strcmp(p0->irq_name, q0->irq_name) != 0
251 q0 = &per_cpu_stats[prev][offset];
253 if (strcmp(p0->irq_name, q0->irq_name) != 0
254 && (j + 1 < total_irqs)
257 q0 = &per_cpu_stats[prev][offset];
261 if (strcmp(p0->irq_name, q0->irq_name) == 0
264 struct stats_irqcpu *p, *q;
265 p = &per_cpu_stats[current][(cpu - 1) * total_irqs + j];
266 q = &per_cpu_stats[prev][(cpu - 1) * total_irqs + offset];
267 printf("%"INTRATE_SCRWIDTH_STR".2f",
268 (double)(p->interrupts - q->interrupts) / itv * G.hz);
278 static data_t get_per_cpu_interval(const struct stats_cpu *scc,
279 const struct stats_cpu *scp)
281 return ((scc->cpu_user + scc->cpu_nice +
282 scc->cpu_system + scc->cpu_iowait +
283 scc->cpu_idle + scc->cpu_steal +
284 scc->cpu_irq + scc->cpu_softirq) -
285 (scp->cpu_user + scp->cpu_nice +
286 scp->cpu_system + scp->cpu_iowait +
287 scp->cpu_idle + scp->cpu_steal +
288 scp->cpu_irq + scp->cpu_softirq));
291 static void print_stats_cpu_struct(const struct stats_cpu *p,
292 const struct stats_cpu *c,
295 printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
296 percent_value(p->cpu_user - p->cpu_guest,
297 /**/ c->cpu_user - c->cpu_guest, itv),
298 percent_value(p->cpu_nice , c->cpu_nice , itv),
299 percent_value(p->cpu_system , c->cpu_system , itv),
300 percent_value(p->cpu_iowait , c->cpu_iowait , itv),
301 percent_value(p->cpu_irq , c->cpu_irq , itv),
302 percent_value(p->cpu_softirq, c->cpu_softirq, itv),
303 percent_value(p->cpu_steal , c->cpu_steal , itv),
304 percent_value(p->cpu_guest , c->cpu_guest , itv),
305 percent_value(p->cpu_idle , c->cpu_idle , itv)
309 static void write_stats_core(int prev, int current,
310 const char *prev_str, const char *current_str)
312 struct stats_cpu *scc, *scp;
313 data_t itv, global_itv;
316 /* Compute time interval */
317 itv = global_itv = jiffies_diff(G.global_uptime[prev], G.global_uptime[current]);
319 /* Reduce interval to one CPU */
321 itv = jiffies_diff(G.per_cpu_uptime[prev], G.per_cpu_uptime[current]);
323 /* Print CPU stats */
324 if (display_opt(D_CPU)) {
326 ///* This is done exactly once */
327 //if (!G.header_done) {
328 printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq %%soft %%steal %%guest %%idle\n",
331 // G.header_done = 1;
334 for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
337 /* Print stats about this particular CPU? */
338 if (!is_cpu_in_bitmap(cpu))
341 scc = &G.st_cpu[current][cpu];
342 scp = &G.st_cpu[prev][cpu];
343 per_cpu_itv = global_itv;
345 printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
349 * If the CPU is offline, then it isn't in /proc/stat,
350 * so all values are 0.
351 * NB: Guest time is already included in user time.
353 if ((scc->cpu_user | scc->cpu_nice | scc->cpu_system |
354 scc->cpu_iowait | scc->cpu_idle | scc->cpu_steal |
355 scc->cpu_irq | scc->cpu_softirq) == 0
358 * Set current struct fields to values from prev.
359 * iteration. Then their values won't jump from
360 * zero, when the CPU comes back online.
366 /* Compute interval again for current proc */
367 per_cpu_itv = get_per_cpu_interval(scc, scp);
368 if (per_cpu_itv == 0) {
370 * If the CPU is tickless then there is no change in CPU values
371 * but the sum of values is not zero.
375 printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
376 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, idle);
380 print_stats_cpu_struct(scp, scc, per_cpu_itv);
384 /* Print total number of IRQs per CPU */
385 if (display_opt(D_IRQ_SUM)) {
387 ///* Print average header, this is done exactly once */
388 //if (!G.avg_header_done) {
389 printf("\n%-11s CPU intr/s\n", prev_str);
390 // G.avg_header_done = 1;
393 for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
396 /* Print stats about this CPU? */
397 if (!is_cpu_in_bitmap(cpu))
401 printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
403 scc = &G.st_cpu[current][cpu];
404 scp = &G.st_cpu[prev][cpu];
405 /* Compute interval again for current proc */
406 per_cpu_itv = get_per_cpu_interval(scc, scp);
407 if (per_cpu_itv == 0) {
408 printf(" %9.2f\n", 0.0);
412 //bb_error_msg("G.st_irq[%u][%u].irq_nr:%lld - G.st_irq[%u][%u].irq_nr:%lld",
413 // current, cpu, G.st_irq[prev][cpu].irq_nr, prev, cpu, G.st_irq[current][cpu].irq_nr);
414 printf(" %9.2f\n", hz_value(G.st_irq[prev][cpu].irq_nr, G.st_irq[current][cpu].irq_nr, per_cpu_itv));
418 if (display_opt(D_IRQ_CPU)) {
419 write_irqcpu_stats(G.st_irqcpu, G.irqcpu_nr,
422 prev_str, current_str
426 if (display_opt(D_SOFTIRQS)) {
427 write_irqcpu_stats(G.st_softirqcpu, G.softirqcpu_nr,
430 prev_str, current_str
436 * Print the statistics
438 static void write_stats(int current)
443 strftime(prev_time, sizeof(prev_time), "%X", &G.timestamp[!current]);
444 strftime(curr_time, sizeof(curr_time), "%X", &G.timestamp[current]);
446 write_stats_core(!current, current, prev_time, curr_time);
449 static void write_stats_avg(int current)
451 write_stats_core(2, current, "Average:", "Average:");
455 * Read CPU statistics
457 static void get_cpu_statistics(struct stats_cpu *cpu, data_t *up, data_t *up0)
462 fp = xfopen_for_read(PROCFS_STAT);
464 while (fgets(buf, sizeof(buf), fp)) {
467 struct stats_cpu *cp;
469 if (!starts_with_cpu(buf))
470 continue; /* not "cpu" */
472 cp = cpu; /* for "cpu " case */
476 || sscanf(buf + 3, "%u ", &cpu_number) != 1
477 || cpu_number >= G.cpu_nr
481 cp = &cpu[cpu_number + 1];
484 /* Read the counters, save them */
485 /* Not all fields have to be present */
486 memset(cp, 0, sizeof(*cp));
488 " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
489 " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
490 " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u",
491 &cp->cpu_user, &cp->cpu_nice, &cp->cpu_system,
492 &cp->cpu_idle, &cp->cpu_iowait, &cp->cpu_irq,
493 &cp->cpu_softirq, &cp->cpu_steal, &cp->cpu_guest
496 * Compute uptime in jiffies (1/HZ), it'll be the sum of
497 * individual CPU's uptimes.
498 * NB: We have to omit cpu_guest, because cpu_user includes it.
500 sum = cp->cpu_user + cp->cpu_nice + cp->cpu_system +
501 cp->cpu_idle + cp->cpu_iowait + cp->cpu_irq +
502 cp->cpu_softirq + cp->cpu_steal;
509 if (cpu_number == 0 && *up0 != 0) {
510 /* Compute uptime of single CPU */
519 * Read IRQs from /proc/stat
521 static void get_irqs_from_stat(struct stats_irq *irq)
526 fp = fopen_for_read(PROCFS_STAT);
530 while (fgets(buf, sizeof(buf), fp)) {
531 //bb_error_msg("/proc/stat:'%s'", buf);
532 if (strncmp(buf, "intr ", 5) == 0) {
533 /* Read total number of IRQs since system boot */
534 sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
542 * Read stats from /proc/interrupts or /proc/softirqs
544 static void get_irqs_from_interrupts(const char *fname,
545 struct stats_irqcpu *per_cpu_stats[],
546 int irqs_per_cpu, int current)
549 struct stats_irq *irq_i;
550 struct stats_irqcpu *ic;
555 int cpu_index[G.cpu_nr];
559 // Otherwise reading of /proc/softirqs
560 // was resetting counts to 0 after we painstakingly collected them from
561 // /proc/interrupts. Which resulted in:
562 // 01:32:47 PM CPU intr/s
563 // 01:32:47 PM all 591.47
564 // 01:32:47 PM 0 0.00 <= ???
565 // 01:32:47 PM 1 0.00 <= ???
566 // for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
567 // G.st_irq[current][cpu].irq_nr = 0;
568 // //bb_error_msg("G.st_irq[%u][%u].irq_nr=0", current, cpu);
571 fp = fopen_for_read(fname);
575 buflen = INTERRUPTS_LINE + 16 * G.cpu_nr;
576 buf = xmalloc(buflen);
578 /* Parse header and determine, which CPUs are online */
580 while (fgets(buf, buflen, fp)) {
583 while ((cp = strstr(next, "CPU")) != NULL
586 cpu = strtoul(cp + 3, &next, 10);
587 cpu_index[iindex++] = cpu;
589 if (iindex) /* We found header */
594 while (fgets(buf, buflen, fp)
595 && irq < irqs_per_cpu
601 /* Skip over "IRQNAME:" */
602 cp = strchr(buf, ':');
607 ic = &per_cpu_stats[current][irq];
609 if (len >= sizeof(ic->irq_name)) {
610 len = sizeof(ic->irq_name) - 1;
612 safe_strncpy(ic->irq_name, buf, len + 1);
613 //bb_error_msg("%s: irq%d:'%s' buf:'%s'", fname, irq, ic->irq_name, buf);
616 for (cpu = 0; cpu < iindex; cpu++) {
618 ic = &per_cpu_stats[current][cpu_index[cpu] * irqs_per_cpu + irq];
619 irq_i = &G.st_irq[current][cpu_index[cpu] + 1];
620 ic->interrupts = strtoul(cp, &next, 10);
621 /* Count only numerical IRQs */
622 if (isdigit(last_char)) {
623 irq_i->irq_nr += ic->interrupts;
624 //bb_error_msg("G.st_irq[%u][%u].irq_nr + %u = %lld",
625 // current, cpu_index[cpu] + 1, ic->interrupts, irq_i->irq_nr);
634 while (irq < irqs_per_cpu) {
635 /* Number of interrupts per CPU has changed */
636 ic = &per_cpu_stats[current][irq];
637 ic->irq_name[0] = '\0'; /* False interrupt */
642 static void get_uptime(data_t *uptime)
645 char buf[sizeof(long)*3 * 2 + 4]; /* enough for long.long */
646 unsigned long uptime_sec, decimal;
648 fp = fopen_for_read(PROCFS_UPTIME);
651 if (fgets(buf, sizeof(buf), fp)) {
652 if (sscanf(buf, "%lu.%lu", &uptime_sec, &decimal) == 2) {
653 *uptime = (data_t)uptime_sec * G.hz + decimal * G.hz / 100;
660 static void get_localtime(struct tm *tm)
664 localtime_r(&timer, tm);
667 static void alarm_handler(int sig UNUSED_PARAM)
669 signal(SIGALRM, alarm_handler);
673 static void main_loop(void)
680 G.per_cpu_uptime[0] = 0;
681 get_uptime(&G.per_cpu_uptime[0]);
684 get_cpu_statistics(G.st_cpu[0], &G.global_uptime[0], &G.per_cpu_uptime[0]);
686 if (display_opt(D_IRQ_SUM))
687 get_irqs_from_stat(G.st_irq[0]);
689 if (display_opt(D_IRQ_SUM | D_IRQ_CPU))
690 get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
693 if (display_opt(D_SOFTIRQS))
694 get_irqs_from_interrupts(PROCFS_SOFTIRQS, G.st_softirqcpu,
697 if (G.interval == 0) {
698 /* Display since boot time */
700 G.timestamp[1] = G.timestamp[0];
701 memset(G.st_cpu[1], 0, sizeof(G.st_cpu[1][0]) * cpus);
702 memset(G.st_irq[1], 0, sizeof(G.st_irq[1][0]) * cpus);
703 memset(G.st_irqcpu[1], 0, sizeof(G.st_irqcpu[1][0]) * cpus * G.irqcpu_nr);
704 memset(G.st_softirqcpu[1], 0, sizeof(G.st_softirqcpu[1][0]) * cpus * G.softirqcpu_nr);
712 /* Set a handler for SIGALRM */
715 /* Save the stats we already have. We need them to compute the average */
716 G.timestamp[2] = G.timestamp[0];
717 G.global_uptime[2] = G.global_uptime[0];
718 G.per_cpu_uptime[2] = G.per_cpu_uptime[0];
720 memcpy(G.st_cpu[2], G.st_cpu[0], sizeof(G.st_cpu[0][0]) * cpus);
721 memcpy(G.st_irq[2], G.st_irq[0], sizeof(G.st_irq[0][0]) * cpus);
722 memcpy(G.st_irqcpu[2], G.st_irqcpu[0], sizeof(G.st_irqcpu[0][0]) * cpus * G.irqcpu_nr);
723 if (display_opt(D_SOFTIRQS)) {
724 memcpy(G.st_softirqcpu[2], G.st_softirqcpu[0],
725 sizeof(G.st_softirqcpu[0][0]) * cpus * G.softirqcpu_nr);
730 /* Suspend until a signal is received */
733 /* Set structures to 0 to distinguish off/online CPUs */
734 memset(&G.st_cpu[current][/*cpu:*/ 1], 0, sizeof(G.st_cpu[0][0]) * G.cpu_nr);
736 get_localtime(&G.timestamp[current]);
740 G.per_cpu_uptime[current] = 0;
741 get_uptime(&G.per_cpu_uptime[current]);
743 get_cpu_statistics(G.st_cpu[current], &G.global_uptime[current], &G.per_cpu_uptime[current]);
745 if (display_opt(D_IRQ_SUM))
746 get_irqs_from_stat(G.st_irq[current]);
748 if (display_opt(D_IRQ_SUM | D_IRQ_CPU)) {
750 for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
751 G.st_irq[current][cpu].irq_nr = 0;
753 /* accumulates .irq_nr */
754 get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
755 G.irqcpu_nr, current);
758 if (display_opt(D_SOFTIRQS))
759 get_irqs_from_interrupts(PROCFS_SOFTIRQS,
761 G.softirqcpu_nr, current);
763 write_stats(current);
773 /* Print average statistics */
774 write_stats_avg(current);
779 /* Get number of clock ticks per sec */
780 static ALWAYS_INLINE unsigned get_hz(void)
782 return sysconf(_SC_CLK_TCK);
785 static void alloc_struct(int cpus)
788 for (i = 0; i < 3; i++) {
789 G.st_cpu[i] = xzalloc(sizeof(G.st_cpu[i][0]) * cpus);
790 G.st_irq[i] = xzalloc(sizeof(G.st_irq[i][0]) * cpus);
791 G.st_irqcpu[i] = xzalloc(sizeof(G.st_irqcpu[i][0]) * cpus * G.irqcpu_nr);
792 G.st_softirqcpu[i] = xzalloc(sizeof(G.st_softirqcpu[i][0]) * cpus * G.softirqcpu_nr);
794 G.cpu_bitmap_len = (cpus >> 3) + 1;
795 G.cpu_bitmap = xzalloc(G.cpu_bitmap_len);
798 static void print_header(struct tm *t)
803 /* Get system name, release number and hostname */
806 strftime(cur_date, sizeof(cur_date), "%x", t);
808 printf("%s %s (%s)\t%s\t_%s_\t(%u CPU)\n",
809 uts.sysname, uts.release, uts.nodename, cur_date, uts.machine, G.cpu_nr);
813 * Get number of interrupts available per processor
815 static int get_irqcpu_nr(const char *f, int max_irqs)
822 fp = fopen_for_read(f);
823 if (!fp) /* No interrupts file */
826 linelen = INTERRUPTS_LINE + 16 * G.cpu_nr;
827 line = xmalloc(linelen);
830 while (fgets(line, linelen, fp)
833 int p = strcspn(line, ":");
834 if ((p > 0) && (p < 16))
844 //usage:#define mpstat_trivial_usage
845 //usage: "[-A] [-I SUM|CPU|ALL|SCPU] [-u] [-P num|ALL] [INTERVAL [COUNT]]"
846 //usage:#define mpstat_full_usage "\n\n"
847 //usage: "Per-processor statistics\n"
848 //usage: "\nOptions:"
849 //usage: "\n -A Same as -I ALL -u -P ALL"
850 //usage: "\n -I SUM|CPU|ALL|SCPU Report interrupt statistics"
851 //usage: "\n -P num|ALL Processor to monitor"
852 //usage: "\n -u Report CPU utilization"
854 int mpstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
855 int mpstat_main(int UNUSED_PARAM argc, char **argv)
861 OPT_ALL = 1 << 0, /* -A */
862 OPT_INTS = 1 << 1, /* -I */
863 OPT_SETCPU = 1 << 2, /* -P */
864 OPT_UTIL = 1 << 3, /* -u */
867 /* Dont buffer data if redirected to a pipe */
868 setbuf(stdout, NULL);
874 /* Get number of processors */
875 G.cpu_nr = get_cpu_count();
877 /* Get number of clock ticks per sec */
880 /* Calculate number of interrupts per processor */
881 G.irqcpu_nr = get_irqcpu_nr(PROCFS_INTERRUPTS, NR_IRQS) + NR_IRQCPU_PREALLOC;
883 /* Calculate number of soft interrupts per processor */
884 G.softirqcpu_nr = get_irqcpu_nr(PROCFS_SOFTIRQS, NR_IRQS) + NR_IRQCPU_PREALLOC;
886 /* Allocate space for structures. + 1 for global structure. */
887 alloc_struct(G.cpu_nr + 1);
889 /* Parse and process arguments */
890 opt = getopt32(argv, "AI:P:u", &opt_irq_fmt, &opt_set_cpu);
895 G.interval = xatoi_positive(*argv);
899 /* Get count value */
902 G.count = xatoi_positive(*argv);
912 G.options |= D_CPU + D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS;
913 /* Select every CPU */
914 memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
917 if (opt & OPT_INTS) {
918 static const char v[] = {
919 D_IRQ_CPU, D_IRQ_SUM, D_SOFTIRQS,
920 D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS
922 i = index_in_strings("CPU\0SUM\0SCPU\0ALL\0", opt_irq_fmt);
928 if ((opt & OPT_UTIL) /* -u? */
929 || G.options == 0 /* nothing? (use default then) */
934 if (opt & OPT_SETCPU) {
938 for (t = strtok(opt_set_cpu, ","); t; t = strtok(NULL, ",")) {
939 if (strcmp(t, "ALL") == 0) {
940 /* Select every CPU */
941 memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
944 unsigned n = xatoi_positive(t);
946 bb_error_msg_and_die("not that many processors");
948 G.cpu_bitmap[n >> 3] |= 1 << (n & 7);
954 /* Display global stats */
958 get_localtime(&G.timestamp[0]);
961 print_header(&G.timestamp[0]);
966 if (ENABLE_FEATURE_CLEAN_UP) {
968 for (i = 0; i < 3; i++) {
971 free(G.st_irqcpu[i]);
972 free(G.st_softirqcpu[i]);