2 * mpstat: per-processor statistics
3 * (C) 2000-2013 by Sebastien GODARD (sysstat <at> orange.fr)
5 ***************************************************************************
6 * This program is free software; you can redistribute it and/or modify it *
7 * under the terms of the GNU General Public License as published by the *
8 * Free Software Foundation; either version 2 of the License, or (at your *
9 * option) any later version. *
11 * This program is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
16 * You should have received a copy of the GNU General Public License along *
17 * with this program; if not, write to the Free Software Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************
29 #include <sys/utsname.h>
40 #define _(string) gettext(string)
42 #define _(string) (string)
45 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
46 char *sccsid(void) { return (SCCSID); }
48 unsigned long long uptime[3] = {0, 0, 0};
49 unsigned long long uptime0[3] = {0, 0, 0};
51 /* NOTE: Use array of _char_ for bitmaps to avoid endianness problems...*/
52 unsigned char *cpu_bitmap; /* Bit 0: Global; Bit 1: 1st proc; etc. */
54 /* Structures used to store stats */
55 struct stats_cpu *st_cpu[3];
56 struct stats_irq *st_irq[3];
57 struct stats_irqcpu *st_irqcpu[3];
58 struct stats_irqcpu *st_softirqcpu[3];
60 struct tm mp_tstamp[3];
63 unsigned int actflags = 0;
65 unsigned int flags = 0;
67 /* Interval and count parameters */
68 long interval = -1, count = 0;
70 /* Nb of processors on the machine */
72 /* Nb of interrupts per processor */
74 /* Nb of soft interrupts per processor */
75 int softirqcpu_nr = 0;
77 struct sigaction alrm_act, int_act;
78 int sigint_caught = 0;
81 ***************************************************************************
82 * Print usage and exit
85 * @progname Name of sysstat command
86 ***************************************************************************
88 void usage(char *progname)
90 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
93 fprintf(stderr, _("Options are:\n"
94 "[ -A ] [ -u ] [ -V ] [ -I { SUM | CPU | SCPU | ALL } ]\n"
95 "[ -P { <cpu> [,...] | ON | ALL } ]\n"));
100 ***************************************************************************
101 * SIGALRM signal handler. No need to reset the handler here.
104 * @sig Signal number.
105 ***************************************************************************
107 void alarm_handler(int sig)
113 ***************************************************************************
114 * SIGINT signal handler.
117 * @sig Signal number.
118 **************************************************************************
120 void int_handler(int sig)
126 ***************************************************************************
127 * Allocate stats structures and cpu bitmap.
130 * @nr_cpus Number of CPUs. This is the real number of available CPUs + 1
131 * because we also have to allocate a structure for CPU 'all'.
132 ***************************************************************************
134 void salloc_mp_struct(int nr_cpus)
138 for (i = 0; i < 3; i++) {
140 if ((st_cpu[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * nr_cpus))
145 memset(st_cpu[i], 0, STATS_CPU_SIZE * nr_cpus);
147 if ((st_irq[i] = (struct stats_irq *) malloc(STATS_IRQ_SIZE * nr_cpus))
152 memset(st_irq[i], 0, STATS_IRQ_SIZE * nr_cpus);
154 if ((st_irqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr))
159 memset(st_irqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr);
161 if ((st_softirqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr))
166 memset(st_softirqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr);
169 if ((cpu_bitmap = (unsigned char *) malloc((nr_cpus >> 3) + 1)) == NULL) {
173 memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1);
177 ***************************************************************************
178 * Free structures and bitmap.
179 ***************************************************************************
181 void sfree_mp_struct(void)
185 for (i = 0; i < 3; i++) {
189 free(st_softirqcpu[i]);
196 ***************************************************************************
197 * Display per CPU statistics.
200 * @st_ic Array for per-CPU statistics.
201 * @ic_nr Number of interrupts (hard or soft) per CPU.
202 * @dis TRUE if a header line must be printed.
203 * @itv Interval value.
204 * @prev Position in array where statistics used as reference are.
205 * Stats used as reference may be the previous ones read, or
206 * the very first ones when calculating the average.
207 * @curr Position in array where current statistics will be saved.
208 * @prev_string String displayed at the beginning of a header line. This is
209 * the timestamp of the previous sample, or "Average" when
210 * displaying average stats.
211 * @curr_string String displayed at the beginning of current sample stats.
212 * This is the timestamp of the current sample, or "Average"
213 * when displaying average stats.
214 ***************************************************************************
216 void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis,
217 unsigned long long itv, int prev, int curr,
218 char *prev_string, char *curr_string)
220 struct stats_cpu *scc;
221 int j = 0, offset, cpu;
222 struct stats_irqcpu *p, *q, *p0, *q0;
225 * Check if number of interrupts has changed.
226 * NB: A null interval value indicates that we are
227 * displaying statistics since system startup.
229 if (!dis && interval) {
231 p0 = st_ic[curr] + j;
232 if (p0->irq_name[0] != '\0') {
233 q0 = st_ic[prev] + j;
234 if (strcmp(p0->irq_name, q0->irq_name)) {
235 /* These are two different irq */
241 while ((j > 0) && (j <= ic_nr));
244 if (dis || (j < 0)) {
246 printf("\n%-11s CPU", prev_string);
247 for (j = 0; j < ic_nr; j++) {
248 p0 = st_ic[curr] + j;
249 if (p0->irq_name[0] != '\0') { /* Nb of irq per proc may have varied... */
250 printf(" %8s/s", p0->irq_name);
256 for (cpu = 1; cpu <= cpu_nr; cpu++) {
258 scc = st_cpu[curr] + cpu;
260 /* Check CPU presence */
264 * Check if we want stats about this CPU.
265 * CPU must have been explicitly selected using option -P,
266 * else we display every CPU.
268 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))) && USE_P_OPTION(flags))
271 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
272 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
273 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
275 /* Offline CPU found */
277 if (DISPLAY_ONLINE_CPU(flags))
281 printf("%-11s %3d", curr_string, cpu - 1);
283 for (j = 0; j < ic_nr; j++) {
284 p0 = st_ic[curr] + j; /* irq field set only for proc #0 */
286 * An empty string for irq name means it is a remaining interrupt
287 * which is no longer used, for example because the
288 * number of interrupts has decreased in /proc/interrupts.
290 if (p0->irq_name[0] != '\0') {
291 q0 = st_ic[prev] + j;
295 * If we want stats for the time since system startup,
296 * we have p0->irq != q0->irq, since q0 structure is
297 * completely set to zero.
299 if (strcmp(p0->irq_name, q0->irq_name) && interval) {
302 q0 = st_ic[prev] + offset;
303 if (strcmp(p0->irq_name, q0->irq_name) && (j + 1 < ic_nr))
305 q0 = st_ic[prev] + offset;
308 if (!strcmp(p0->irq_name, q0->irq_name) || !interval) {
309 p = st_ic[curr] + (cpu - 1) * ic_nr + j;
310 q = st_ic[prev] + (cpu - 1) * ic_nr + offset;
312 S_VALUE(q->interrupt, p->interrupt, itv));
323 ***************************************************************************
324 * Core function used to display statistics.
327 * @prev Position in array where statistics used as reference are.
328 * Stats used as reference may be the previous ones read, or
329 * the very first ones when calculating the average.
330 * @curr Position in array where statistics for current sample are.
331 * @dis TRUE if a header line must be printed.
332 * @prev_string String displayed at the beginning of a header line. This is
333 * the timestamp of the previous sample, or "Average" when
334 * displaying average stats.
335 * @curr_string String displayed at the beginning of current sample stats.
336 * This is the timestamp of the current sample, or "Average"
337 * when displaying average stats.
338 ***************************************************************************
340 void write_stats_core(int prev, int curr, int dis,
341 char *prev_string, char *curr_string)
343 struct stats_cpu *scc, *scp;
344 unsigned long long itv, pc_itv, g_itv;
348 TEST_STDOUT(STDOUT_FILENO);
350 /* Compute time interval */
351 g_itv = get_interval(uptime[prev], uptime[curr]);
353 /* Reduce interval value to one processor */
355 itv = get_interval(uptime0[prev], uptime0[curr]);
361 /* Print CPU stats */
362 if (DISPLAY_CPU(actflags)) {
364 printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq "
365 "%%soft %%steal %%guest %%gnice %%idle\n",
369 /* Check if we want global stats among all proc */
370 if (*cpu_bitmap & 1) {
372 printf("%-11s all", curr_string);
374 printf(" %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n",
375 (st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest) <
376 (st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest) ?
378 ll_sp_value(st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest,
379 st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest,
381 (st_cpu[curr]->cpu_nice - st_cpu[curr]->cpu_guest_nice) <
382 (st_cpu[prev]->cpu_nice - st_cpu[prev]->cpu_guest_nice) ?
384 ll_sp_value(st_cpu[prev]->cpu_nice - st_cpu[prev]->cpu_guest_nice,
385 st_cpu[curr]->cpu_nice - st_cpu[curr]->cpu_guest_nice,
387 ll_sp_value(st_cpu[prev]->cpu_sys,
388 st_cpu[curr]->cpu_sys,
390 ll_sp_value(st_cpu[prev]->cpu_iowait,
391 st_cpu[curr]->cpu_iowait,
393 ll_sp_value(st_cpu[prev]->cpu_hardirq,
394 st_cpu[curr]->cpu_hardirq,
396 ll_sp_value(st_cpu[prev]->cpu_softirq,
397 st_cpu[curr]->cpu_softirq,
399 ll_sp_value(st_cpu[prev]->cpu_steal,
400 st_cpu[curr]->cpu_steal,
402 ll_sp_value(st_cpu[prev]->cpu_guest,
403 st_cpu[curr]->cpu_guest,
405 ll_sp_value(st_cpu[prev]->cpu_guest_nice,
406 st_cpu[curr]->cpu_guest_nice,
408 (st_cpu[curr]->cpu_idle < st_cpu[prev]->cpu_idle) ?
410 ll_sp_value(st_cpu[prev]->cpu_idle,
411 st_cpu[curr]->cpu_idle,
415 for (cpu = 1; cpu <= cpu_nr; cpu++) {
417 scc = st_cpu[curr] + cpu;
418 scp = st_cpu[prev] + cpu;
420 /* Check CPU presence */
423 /* Check if we want stats about this proc */
424 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
428 * If the CPU is offline then it is omited from /proc/stat
429 * and the sum of all values is zero.
430 * (Remember that guest/guest_nice times are already included in
433 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
434 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
435 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
437 if (!DISPLAY_ONLINE_CPU(flags)) {
439 " %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f"
440 " %6.2f %6.2f %6.2f %6.2f\n",
441 curr_string, cpu - 1,
442 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
447 printf("%-11s %4d", curr_string, cpu - 1);
449 /* Recalculate itv for current proc */
450 pc_itv = get_per_cpu_interval(scc, scp);
454 * If the CPU is tickless then there is no change in CPU values
455 * but the sum of values is not zero.
457 printf(" %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f"
458 " %6.2f %6.2f %6.2f %6.2f\n",
459 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
463 printf(" %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f"
464 " %6.2f %6.2f %6.2f %6.2f\n",
465 (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ?
467 ll_sp_value(scp->cpu_user - scp->cpu_guest,
468 scc->cpu_user - scc->cpu_guest,
470 (scc->cpu_nice - scc->cpu_guest_nice) < (scp->cpu_nice - scp->cpu_guest_nice) ?
472 ll_sp_value(scp->cpu_nice - scp->cpu_guest_nice,
473 scc->cpu_nice - scc->cpu_guest_nice,
475 ll_sp_value(scp->cpu_sys,
478 ll_sp_value(scp->cpu_iowait,
481 ll_sp_value(scp->cpu_hardirq,
484 ll_sp_value(scp->cpu_softirq,
487 ll_sp_value(scp->cpu_steal,
490 ll_sp_value(scp->cpu_guest,
493 ll_sp_value(scp->cpu_guest_nice,
496 (scc->cpu_idle < scp->cpu_idle) ?
498 ll_sp_value(scp->cpu_idle,
505 /* Print total number of interrupts per processor */
506 if (DISPLAY_IRQ_SUM(actflags)) {
507 struct stats_irq *sic, *sip;
510 printf("\n%-11s CPU intr/s\n", prev_string);
513 if (*cpu_bitmap & 1) {
514 printf("%-11s all %9.2f\n", curr_string,
515 ll_s_value(st_irq[prev]->irq_nr, st_irq[curr]->irq_nr, itv));
518 for (cpu = 1; cpu <= cpu_nr; cpu++) {
520 sic = st_irq[curr] + cpu;
521 sip = st_irq[prev] + cpu;
523 scc = st_cpu[curr] + cpu;
524 scp = st_cpu[prev] + cpu;
526 /* Check CPU presence */
529 /* Check if we want stats about this proc */
530 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
533 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
534 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
535 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
537 /* This is an offline CPU */
539 if (!DISPLAY_ONLINE_CPU(flags)) {
542 curr_string, cpu - 1,
548 printf("%-11s %4d", curr_string, cpu - 1);
550 /* Recalculate itv for current proc */
551 pc_itv = get_per_cpu_interval(scc, scp);
554 /* This is a tickless CPU */
555 printf(" %9.2f\n", 0.0);
559 ll_s_value(sip->irq_nr, sic->irq_nr, itv));
564 if (DISPLAY_IRQ_CPU(actflags)) {
565 write_irqcpu_stats(st_irqcpu, irqcpu_nr, dis, itv, prev, curr,
566 prev_string, curr_string);
569 if (DISPLAY_SOFTIRQS(actflags)) {
570 write_irqcpu_stats(st_softirqcpu, softirqcpu_nr, dis, itv, prev, curr,
571 prev_string, curr_string);
574 /* Fix CPU counter values for every offline CPU */
575 for (cpu = 1; cpu <= cpu_nr; cpu++) {
577 scc = st_cpu[curr] + cpu;
578 scp = st_cpu[prev] + cpu;
580 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
581 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
582 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
585 * Set current struct fields (which have been set to zero)
586 * to values from previous iteration. Hence their values won't
587 * jump from zero when the CPU comes back online.
595 ***************************************************************************
596 * Print statistics average.
599 * @curr Position in array where statistics for current sample are.
600 * @dis TRUE if a header line must be printed.
601 ***************************************************************************
603 void write_stats_avg(int curr, int dis)
607 strncpy(string, _("Average:"), 16);
609 write_stats_core(2, curr, dis, string, string);
613 ***************************************************************************
617 * @curr Position in array where statistics for current sample are.
618 * @dis TRUE if a header line must be printed.
619 ***************************************************************************
621 void write_stats(int curr, int dis)
623 char cur_time[2][16];
625 /* Get previous timestamp */
626 strftime(cur_time[!curr], 16, "%X", &(mp_tstamp[!curr]));
628 /* Get current timestamp */
629 strftime(cur_time[curr], 16, "%X", &(mp_tstamp[curr]));
631 write_stats_core(!curr, curr, dis, cur_time[!curr], cur_time[curr]);
635 ***************************************************************************
636 * Read stats from /proc/interrupts or /proc/softirqs.
639 * @file /proc file to read (interrupts or softirqs).
640 * @ic_nr Number of interrupts (hard or soft) per CPU.
641 * @curr Position in array where current statistics will be saved.
644 * @st_ic Array for per-CPU statistics.
645 ***************************************************************************
647 void read_interrupts_stat(char *file, struct stats_irqcpu *st_ic[], int ic_nr, int curr)
650 struct stats_irq *st_irq_i;
651 struct stats_irqcpu *p;
652 char *line = NULL, *li;
653 unsigned long irq = 0;
655 int cpu_index[cpu_nr], index = 0, len;
658 for (cpu = 0; cpu < cpu_nr; cpu++) {
659 st_irq_i = st_irq[curr] + cpu + 1;
660 st_irq_i->irq_nr = 0;
663 if ((fp = fopen(file, "r")) != NULL) {
665 SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
668 * Parse header line to see which CPUs are online
670 while (fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) {
672 while (((cp = strstr(next, "CPU")) != NULL) && (index < cpu_nr)) {
673 cpu = strtol(cp + 3, &next, 10);
674 cpu_index[index++] = cpu;
677 /* Header line found */
681 while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) &&
684 /* Skip over "<irq>:" */
685 if ((cp = strchr(line, ':')) == NULL)
686 /* Chr ':' not found */
690 p = st_ic[curr] + irq;
696 len = strcspn(li, ":");
697 if (len >= MAX_IRQ_LEN) {
698 len = MAX_IRQ_LEN - 1;
700 strncpy(p->irq_name, li, len);
701 p->irq_name[len] = '\0';
703 for (cpu = 0; cpu < index; cpu++) {
704 p = st_ic[curr] + cpu_index[cpu] * ic_nr + irq;
705 st_irq_i = st_irq[curr] + cpu_index[cpu] + 1;
707 * No need to set (st_irqcpu + cpu * irqcpu_nr)->irq:
708 * This is the same as st_irqcpu->irq.
710 p->interrupt = strtoul(cp, &next, 10);
711 st_irq_i->irq_nr += p->interrupt;
722 while (irq < ic_nr) {
723 /* Nb of interrupts per processor has changed */
724 p = st_ic[curr] + irq;
725 p->irq_name[0] = '\0'; /* This value means this is a dummy interrupt */
731 ***************************************************************************
732 * Main loop: Read stats from the relevant sources, and display them.
735 * @dis_hdr Set to TRUE if the header line must always be printed.
736 * @rows Number of rows of screen.
737 ***************************************************************************
739 void rw_mpstat_loop(int dis_hdr, int rows)
741 struct stats_cpu *scc;
743 int curr = 1, dis = 1;
744 unsigned long lines = rows;
746 /* Dont buffer data if redirected to a pipe */
747 setbuf(stdout, NULL);
752 * Init uptime0. So if /proc/uptime cannot fill it,
753 * this will be done by /proc/stat.
756 read_uptime(&(uptime0[0]));
758 read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]), &(uptime0[0]));
760 if (DISPLAY_IRQ_SUM(actflags)) {
761 read_stat_irq(st_irq[0], 1);
764 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
765 /* Read this file to display int per CPU or total nr of int per CPU */
766 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, 0);
769 if (DISPLAY_SOFTIRQS(actflags)) {
770 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, 0);
774 /* Display since boot time */
775 mp_tstamp[1] = mp_tstamp[0];
776 memset(st_cpu[1], 0, STATS_CPU_SIZE * (cpu_nr + 1));
777 memset(st_irq[1], 0, STATS_IRQ_SIZE * (cpu_nr + 1));
778 memset(st_irqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
779 if (DISPLAY_SOFTIRQS(actflags)) {
780 memset(st_softirqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
782 write_stats(0, DISP_HDR);
786 /* Set a handler for SIGALRM */
787 memset(&alrm_act, 0, sizeof(alrm_act));
788 alrm_act.sa_handler = (void *) alarm_handler;
789 sigaction(SIGALRM, &alrm_act, NULL);
792 /* Save the first stats collected. Will be used to compute the average */
793 mp_tstamp[2] = mp_tstamp[0];
794 uptime[2] = uptime[0];
795 uptime0[2] = uptime0[0];
796 memcpy(st_cpu[2], st_cpu[0], STATS_CPU_SIZE * (cpu_nr + 1));
797 memcpy(st_irq[2], st_irq[0], STATS_IRQ_SIZE * (cpu_nr + 1));
798 memcpy(st_irqcpu[2], st_irqcpu[0], STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
799 if (DISPLAY_SOFTIRQS(actflags)) {
800 memcpy(st_softirqcpu[2], st_softirqcpu[0],
801 STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
804 /* Set a handler for SIGINT */
805 memset(&int_act, 0, sizeof(int_act));
806 int_act.sa_handler = (void *) int_handler;
807 sigaction(SIGINT, &int_act, NULL);
812 /* SIGINT signal caught during first interval: Exit immediately */
817 * Resetting the structure not needed since every fields will be set.
818 * Exceptions are per-CPU structures: Some of them may not be filled
819 * if corresponding processor is disabled (offline). We set them to zero
820 * to be able to distinguish between offline and tickless CPUs.
822 for (cpu = 1; cpu <= cpu_nr; cpu++) {
823 scc = st_cpu[curr] + cpu;
824 memset(scc, 0, STATS_CPU_SIZE);
828 get_localtime(&(mp_tstamp[curr]), 0);
833 read_uptime(&(uptime0[curr]));
835 read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]), &(uptime0[curr]));
837 if (DISPLAY_IRQ_SUM(actflags)) {
838 read_stat_irq(st_irq[curr], 1);
841 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
842 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, curr);
845 if (DISPLAY_SOFTIRQS(actflags)) {
846 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, curr);
857 write_stats(curr, dis);
868 /* SIGINT signal caught => Display average stats */
870 printf("\n"); /* Skip "^C" displayed on screen */
879 /* Write stats average */
880 write_stats_avg(curr, dis_hdr);
884 ***************************************************************************
885 * Main entry to the program
886 ***************************************************************************
888 int main(int argc, char **argv)
890 int opt = 0, i, actset = FALSE;
891 struct utsname header;
897 /* Init National Language Support */
904 /* How many processors on this machine ? */
905 cpu_nr = get_cpu_nr(~0);
907 /* Calculate number of interrupts per processor */
908 irqcpu_nr = get_irqcpu_nr(INTERRUPTS, NR_IRQS, cpu_nr) +
910 /* Calculate number of soft interrupts per processor */
911 softirqcpu_nr = get_irqcpu_nr(SOFTIRQS, NR_IRQS, cpu_nr) +
915 * cpu_nr: a value of 2 means there are 2 processors (0 and 1).
916 * In this case, we have to allocate 3 structures: global, proc0 and proc1.
918 salloc_mp_struct(cpu_nr + 1);
920 while (++opt < argc) {
922 if (!strcmp(argv[opt], "-I")) {
925 if (!strcmp(argv[opt], K_SUM)) {
926 /* Display total number of interrupts per CPU */
927 actflags |= M_D_IRQ_SUM;
929 else if (!strcmp(argv[opt], K_CPU)) {
930 /* Display interrupts per CPU */
931 actflags |= M_D_IRQ_CPU;
933 else if (!strcmp(argv[opt], K_SCPU)) {
934 /* Display soft interrupts per CPU */
935 actflags |= M_D_SOFTIRQS;
937 else if (!strcmp(argv[opt], K_ALL)) {
938 actflags |= M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
949 else if (!strcmp(argv[opt], "-P")) {
950 /* '-P ALL' can be used on UP machines */
955 for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
956 if (!strcmp(t, K_ALL) || !strcmp(t, K_ON)) {
961 * Set bit for every processor.
962 * Also indicate to display stats for CPU 'all'.
964 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
965 if (!strcmp(t, K_ON)) {
966 /* Display stats only for online CPU */
971 if (strspn(t, DIGITS) != strlen(t)) {
974 i = atoi(t); /* Get cpu number */
976 fprintf(stderr, _("Not that many processors!\n"));
980 *(cpu_bitmap + (i >> 3)) |= 1 << (i & 0x07);
989 else if (!strncmp(argv[opt], "-", 1)) {
990 for (i = 1; *(argv[opt] + i); i++) {
992 switch (*(argv[opt] + i)) {
995 actflags |= M_D_CPU + M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
997 /* Select all processors */
999 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
1004 actflags |= M_D_CPU;
1008 /* Print version number */
1018 else if (interval < 0) {
1020 if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
1023 interval = atol(argv[opt]);
1030 else if (count <= 0) {
1031 /* Get count value */
1032 if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
1036 count = atol(argv[opt]);
1047 /* Default: Display CPU */
1049 actflags |= M_D_CPU;
1052 if (count_bits(&actflags, sizeof(unsigned int)) > 1) {
1056 if (!USE_P_OPTION(flags)) {
1057 /* Option -P not used: Set bit 0 (global stats among all proc) */
1064 /* Get window size */
1065 rows = get_win_height();
1068 /* Interval not set => display stats since boot time */
1073 get_localtime(&(mp_tstamp[0]), 0);
1075 /* Get system name, release number and hostname */
1077 print_gal_header(&(mp_tstamp[0]), header.sysname, header.release,
1078 header.nodename, header.machine, cpu_nr);
1081 rw_mpstat_loop(dis_hdr, rows);
1083 /* Free structures */