2 * mpstat: per-processor statistics
3 * (C) 2000-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 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 /* Structure used to save CPU stats */
55 struct stats_cpu *st_cpu[3];
57 * Structure used to save total number of interrupts received
58 * among all CPU and for each CPU.
60 struct stats_irq *st_irq[3];
62 * Structures used to save, for each interrupt, the number
63 * received by each CPU.
65 struct stats_irqcpu *st_irqcpu[3];
66 struct stats_irqcpu *st_softirqcpu[3];
68 struct tm mp_tstamp[3];
71 unsigned int actflags = 0;
73 unsigned int flags = 0;
75 /* Interval and count parameters */
76 long interval = -1, count = 0;
78 /* Nb of processors on the machine */
80 /* Nb of interrupts per processor */
82 /* Nb of soft interrupts per processor */
83 int softirqcpu_nr = 0;
85 struct sigaction alrm_act, int_act;
86 int sigint_caught = 0;
89 ***************************************************************************
90 * Print usage and exit
93 * @progname Name of sysstat command
94 ***************************************************************************
96 void usage(char *progname)
98 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
101 fprintf(stderr, _("Options are:\n"
102 "[ -A ] [ -u ] [ -V ] [ -I { SUM | CPU | SCPU | ALL } ]\n"
103 "[ -P { <cpu> [,...] | ON | ALL } ]\n"));
108 ***************************************************************************
109 * SIGALRM signal handler. No need to reset the handler here.
112 * @sig Signal number.
113 ***************************************************************************
115 void alarm_handler(int sig)
121 ***************************************************************************
122 * SIGINT signal handler.
125 * @sig Signal number.
126 **************************************************************************
128 void int_handler(int sig)
134 ***************************************************************************
135 * Allocate stats structures and cpu bitmap.
138 * @nr_cpus Number of CPUs. This is the real number of available CPUs + 1
139 * because we also have to allocate a structure for CPU 'all'.
140 ***************************************************************************
142 void salloc_mp_struct(int nr_cpus)
146 for (i = 0; i < 3; i++) {
148 if ((st_cpu[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * nr_cpus))
153 memset(st_cpu[i], 0, STATS_CPU_SIZE * nr_cpus);
155 if ((st_irq[i] = (struct stats_irq *) malloc(STATS_IRQ_SIZE * nr_cpus))
160 memset(st_irq[i], 0, STATS_IRQ_SIZE * nr_cpus);
162 if ((st_irqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr))
167 memset(st_irqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr);
169 if ((st_softirqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr))
174 memset(st_softirqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr);
177 if ((cpu_bitmap = (unsigned char *) malloc((nr_cpus >> 3) + 1)) == NULL) {
181 memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1);
185 ***************************************************************************
186 * Free structures and bitmap.
187 ***************************************************************************
189 void sfree_mp_struct(void)
193 for (i = 0; i < 3; i++) {
197 free(st_softirqcpu[i]);
204 ***************************************************************************
205 * Display CPU statistics.
208 * @dis TRUE if a header line must be printed.
209 * @g_itv Interval value in jiffies multiplied by the number of CPU.
210 * @prev Position in array where statistics used as reference are.
211 * Stats used as reference may be the previous ones read, or
212 * the very first ones when calculating the average.
213 * @curr Position in array where current statistics will be saved.
214 * @prev_string String displayed at the beginning of a header line. This is
215 * the timestamp of the previous sample, or "Average" when
216 * displaying average stats.
217 * @curr_string String displayed at the beginning of current sample stats.
218 * This is the timestamp of the current sample, or "Average"
219 * when displaying average stats.
220 ***************************************************************************
222 void write_cpu_stats(int dis, unsigned long long g_itv, int prev, int curr,
223 char *prev_string, char *curr_string)
225 struct stats_cpu *scc, *scp;
226 unsigned long long pc_itv;
230 printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq "
231 "%%soft %%steal %%guest %%gnice %%idle\n",
235 /* Check if we want global stats among all proc */
236 if (*cpu_bitmap & 1) {
238 printf("%-11s", curr_string);
239 cprintf_in(IS_STR, " %s", " all", 0);
242 (st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest) <
243 (st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest) ?
245 ll_sp_value(st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest,
246 st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest,
248 (st_cpu[curr]->cpu_nice - st_cpu[curr]->cpu_guest_nice) <
249 (st_cpu[prev]->cpu_nice - st_cpu[prev]->cpu_guest_nice) ?
251 ll_sp_value(st_cpu[prev]->cpu_nice - st_cpu[prev]->cpu_guest_nice,
252 st_cpu[curr]->cpu_nice - st_cpu[curr]->cpu_guest_nice,
254 ll_sp_value(st_cpu[prev]->cpu_sys,
255 st_cpu[curr]->cpu_sys,
257 ll_sp_value(st_cpu[prev]->cpu_iowait,
258 st_cpu[curr]->cpu_iowait,
260 ll_sp_value(st_cpu[prev]->cpu_hardirq,
261 st_cpu[curr]->cpu_hardirq,
263 ll_sp_value(st_cpu[prev]->cpu_softirq,
264 st_cpu[curr]->cpu_softirq,
266 ll_sp_value(st_cpu[prev]->cpu_steal,
267 st_cpu[curr]->cpu_steal,
269 ll_sp_value(st_cpu[prev]->cpu_guest,
270 st_cpu[curr]->cpu_guest,
272 ll_sp_value(st_cpu[prev]->cpu_guest_nice,
273 st_cpu[curr]->cpu_guest_nice,
275 (st_cpu[curr]->cpu_idle < st_cpu[prev]->cpu_idle) ?
277 ll_sp_value(st_cpu[prev]->cpu_idle,
278 st_cpu[curr]->cpu_idle,
283 for (cpu = 1; cpu <= cpu_nr; cpu++) {
285 scc = st_cpu[curr] + cpu;
286 scp = st_cpu[prev] + cpu;
288 /* Check if we want stats about this proc */
289 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
293 * If the CPU is offline then it is omited from /proc/stat
294 * and the sum of all values is zero.
295 * (Remember that guest/guest_nice times are already included in
298 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
299 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
300 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
302 if (!DISPLAY_ONLINE_CPU(flags)) {
303 printf("%-11s", curr_string);
304 cprintf_in(IS_INT, " %4d", "", cpu - 1);
306 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
312 printf("%-11s", curr_string);
313 cprintf_in(IS_INT, " %4d", "", cpu - 1);
315 /* Recalculate itv for current proc */
316 pc_itv = get_per_cpu_interval(scc, scp);
320 * If the CPU is tickless then there is no change in CPU values
321 * but the sum of values is not zero.
324 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
330 (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ?
332 ll_sp_value(scp->cpu_user - scp->cpu_guest,
333 scc->cpu_user - scc->cpu_guest,
335 (scc->cpu_nice - scc->cpu_guest_nice) < (scp->cpu_nice - scp->cpu_guest_nice) ?
337 ll_sp_value(scp->cpu_nice - scp->cpu_guest_nice,
338 scc->cpu_nice - scc->cpu_guest_nice,
340 ll_sp_value(scp->cpu_sys,
343 ll_sp_value(scp->cpu_iowait,
346 ll_sp_value(scp->cpu_hardirq,
349 ll_sp_value(scp->cpu_softirq,
352 ll_sp_value(scp->cpu_steal,
355 ll_sp_value(scp->cpu_guest,
358 ll_sp_value(scp->cpu_guest_nice,
361 (scc->cpu_idle < scp->cpu_idle) ?
363 ll_sp_value(scp->cpu_idle,
372 ***************************************************************************
373 * Display total number of interrupts per CPU.
376 * @dis TRUE if a header line must be printed.
377 * @itv Interval value.
378 * @prev Position in array where statistics used as reference are.
379 * Stats used as reference may be the previous ones read, or
380 * the very first ones when calculating the average.
381 * @curr Position in array where current statistics will be saved.
382 * @prev_string String displayed at the beginning of a header line. This is
383 * the timestamp of the previous sample, or "Average" when
384 * displaying average stats.
385 * @curr_string String displayed at the beginning of current sample stats.
386 * This is the timestamp of the current sample, or "Average"
387 * when displaying average stats.
388 ***************************************************************************
390 void write_isumcpu_stats(int dis, unsigned long long itv, int prev, int curr,
391 char *prev_string, char *curr_string)
393 struct stats_cpu *scc, *scp;
394 struct stats_irq *sic, *sip;
395 unsigned long long pc_itv;
399 printf("\n%-11s CPU intr/s\n", prev_string);
402 if (*cpu_bitmap & 1) {
403 printf("%-11s", curr_string);
404 cprintf_in(IS_STR, " %s", " all", 0);
405 /* Print total number of interrupts among all cpu */
407 S_VALUE(st_irq[prev]->irq_nr, st_irq[curr]->irq_nr, itv));
411 for (cpu = 1; cpu <= cpu_nr; cpu++) {
413 sic = st_irq[curr] + cpu;
414 sip = st_irq[prev] + cpu;
416 scc = st_cpu[curr] + cpu;
417 scp = st_cpu[prev] + cpu;
419 /* Check if we want stats about this CPU */
420 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
423 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
424 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
425 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
427 /* This is an offline CPU */
429 if (!DISPLAY_ONLINE_CPU(flags)) {
431 * Display offline CPU if requested by the user.
432 * Value displayed is 0.00.
434 printf("%-11s", curr_string);
435 cprintf_in(IS_INT, " %4d", "", cpu- 1);
436 cprintf_f(1, 9, 2, 0.0);
442 printf("%-11s", curr_string);
443 cprintf_in(IS_INT, " %4d", "", cpu - 1);
445 /* Recalculate itv for current proc */
446 pc_itv = get_per_cpu_interval(scc, scp);
449 /* This is a tickless CPU: Value displayed is 0.00 */
450 cprintf_f(1, 9, 2, 0.0);
454 /* Display total number of interrupts for current CPU */
456 S_VALUE(sip->irq_nr, sic->irq_nr, itv));
463 ***************************************************************************
464 * Display interrupts statistics for each CPU.
467 * @st_ic Array for per-CPU statistics.
468 * @ic_nr Number of interrupts (hard or soft) per CPU.
469 * @dis TRUE if a header line must be printed.
470 * @itv Interval value.
471 * @prev Position in array where statistics used as reference are.
472 * Stats used as reference may be the previous ones read, or
473 * the very first ones when calculating the average.
474 * @curr Position in array where current statistics will be saved.
475 * @prev_string String displayed at the beginning of a header line. This is
476 * the timestamp of the previous sample, or "Average" when
477 * displaying average stats.
478 * @curr_string String displayed at the beginning of current sample stats.
479 * This is the timestamp of the current sample, or "Average"
480 * when displaying average stats.
481 ***************************************************************************
483 void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis,
484 unsigned long long itv, int prev, int curr,
485 char *prev_string, char *curr_string)
487 struct stats_cpu *scc;
488 int j = ic_nr, offset, cpu, colwidth[NR_IRQS];
489 struct stats_irqcpu *p, *q, *p0, *q0;
492 * Check if number of interrupts has changed.
493 * If this is the case, the header line will be printed again.
494 * NB: A zero interval value indicates that we are
495 * displaying statistics since system startup.
497 if (!dis && interval) {
498 for (j = 0; j < ic_nr; j++) {
499 p0 = st_ic[curr] + j;
500 q0 = st_ic[prev] + j;
501 if (strcmp(p0->irq_name, q0->irq_name))
503 * These are two different interrupts: The header must be displayed
504 * (maybe an interrupt has disappeared, or a new one has just been registered).
505 * Note that we compare even empty strings for the case where
506 * a disappearing interrupt would be the last one in the list.
512 if (dis || (j < ic_nr)) {
514 printf("\n%-11s CPU", prev_string);
515 for (j = 0; j < ic_nr; j++) {
516 p0 = st_ic[curr] + j;
517 if (p0->irq_name[0] == '\0')
518 /* End of the list of interrupts */
520 printf(" %8s/s", p0->irq_name);
525 /* Calculate column widths */
526 for (j = 0; j < ic_nr; j++) {
527 p0 = st_ic[curr] + j;
529 * Width is IRQ name + 2 for the trailing "/s".
530 * Width is calculated even for "undefined" interrupts (with
531 * an empty irq_name string) to quiet code analysis tools.
533 colwidth[j] = strlen(p0->irq_name) + 2;
535 * Normal space for printing a number is 11 chars
536 * (space + 10 digits including the period).
538 if (colwidth[j] < 10) {
543 for (cpu = 1; cpu <= cpu_nr; cpu++) {
545 scc = st_cpu[curr] + cpu;
548 * Check if we want stats about this CPU.
549 * CPU must have been explicitly selected using option -P,
550 * else we display every CPU.
552 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))) && USE_P_OPTION(flags))
555 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
556 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
557 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
559 /* Offline CPU found */
561 if (DISPLAY_ONLINE_CPU(flags))
565 printf("%-11s", curr_string);
566 cprintf_in(IS_INT, " %3d", "", cpu - 1);
568 for (j = 0; j < ic_nr; j++) {
569 p0 = st_ic[curr] + j; /* irq_name set only for CPU#0 */
571 * An empty string for irq_name means it is a remaining interrupt
572 * which is no longer used, for example because the
573 * number of interrupts has decreased in /proc/interrupts.
575 if (p0->irq_name[0] == '\0')
576 /* End of the list of interrupts */
578 q0 = st_ic[prev] + j;
582 * If we want stats for the time since system startup,
583 * we have p0->irq_name != q0->irq_name, since q0 structure
584 * is completely set to zero.
586 if (strcmp(p0->irq_name, q0->irq_name) && interval) {
587 /* Check if interrupt exists elsewhere in list */
588 for (offset = 0; offset < ic_nr; offset++) {
589 q0 = st_ic[prev] + offset;
590 if (!strcmp(p0->irq_name, q0->irq_name))
591 /* Interrupt found at another position */
596 p = st_ic[curr] + (cpu - 1) * ic_nr + j;
598 if (!strcmp(p0->irq_name, q0->irq_name) || !interval) {
599 q = st_ic[prev] + (cpu - 1) * ic_nr + offset;
600 cprintf_f(1, colwidth[j], 2,
601 S_VALUE(q->interrupt, p->interrupt, itv));
605 * Instead of printing "N/A", assume that previous value
606 * for this new interrupt was zero.
608 cprintf_f(1, colwidth[j], 2,
609 S_VALUE(0, p->interrupt, itv));
617 ***************************************************************************
618 * Core function used to display statistics.
621 * @prev Position in array where statistics used as reference are.
622 * Stats used as reference may be the previous ones read, or
623 * the very first ones when calculating the average.
624 * @curr Position in array where statistics for current sample are.
625 * @dis TRUE if a header line must be printed.
626 * @prev_string String displayed at the beginning of a header line. This is
627 * the timestamp of the previous sample, or "Average" when
628 * displaying average stats.
629 * @curr_string String displayed at the beginning of current sample stats.
630 * This is the timestamp of the current sample, or "Average"
631 * when displaying average stats.
632 ***************************************************************************
634 void write_stats_core(int prev, int curr, int dis,
635 char *prev_string, char *curr_string)
637 struct stats_cpu *scc, *scp;
638 unsigned long long itv, g_itv;
642 TEST_STDOUT(STDOUT_FILENO);
644 /* Compute time interval */
645 g_itv = get_interval(uptime[prev], uptime[curr]);
647 /* Reduce interval value to one processor */
649 itv = get_interval(uptime0[prev], uptime0[curr]);
655 /* Print CPU stats */
656 if (DISPLAY_CPU(actflags)) {
657 write_cpu_stats(dis, g_itv, prev, curr, prev_string, curr_string);
660 /* Print total number of interrupts per processor */
661 if (DISPLAY_IRQ_SUM(actflags)) {
662 write_isumcpu_stats(dis, itv, prev, curr, prev_string, curr_string);
665 /* Display each interrupt value for each CPU */
666 if (DISPLAY_IRQ_CPU(actflags)) {
667 write_irqcpu_stats(st_irqcpu, irqcpu_nr, dis, itv, prev, curr,
668 prev_string, curr_string);
670 if (DISPLAY_SOFTIRQS(actflags)) {
671 write_irqcpu_stats(st_softirqcpu, softirqcpu_nr, dis, itv, prev, curr,
672 prev_string, curr_string);
675 /* Fix CPU counter values for every offline CPU */
676 for (cpu = 1; cpu <= cpu_nr; cpu++) {
678 scc = st_cpu[curr] + cpu;
679 scp = st_cpu[prev] + cpu;
681 if ((scc->cpu_user + scc->cpu_nice + scc->cpu_sys +
682 scc->cpu_iowait + scc->cpu_idle + scc->cpu_steal +
683 scc->cpu_hardirq + scc->cpu_softirq) == 0) {
686 * Set current struct fields (which have been set to zero)
687 * to values from previous iteration. Hence their values won't
688 * jump from zero when the CPU comes back online.
696 ***************************************************************************
697 * Print statistics average.
700 * @curr Position in array where statistics for current sample are.
701 * @dis TRUE if a header line must be printed.
702 ***************************************************************************
704 void write_stats_avg(int curr, int dis)
708 strncpy(string, _("Average:"), 16);
710 write_stats_core(2, curr, dis, string, string);
714 ***************************************************************************
718 * @curr Position in array where statistics for current sample are.
719 * @dis TRUE if a header line must be printed.
720 ***************************************************************************
722 void write_stats(int curr, int dis)
724 char cur_time[2][16];
726 /* Get previous timestamp */
727 if (is_iso_time_fmt()) {
728 strftime(cur_time[!curr], sizeof(cur_time[!curr]), "%H:%M:%S", &mp_tstamp[!curr]);
731 strftime(cur_time[!curr], sizeof(cur_time[!curr]), "%X", &(mp_tstamp[!curr]));
734 /* Get current timestamp */
735 if (is_iso_time_fmt()) {
736 strftime(cur_time[curr], sizeof(cur_time[curr]), "%H:%M:%S", &mp_tstamp[curr]);
739 strftime(cur_time[curr], sizeof(cur_time[curr]), "%X", &(mp_tstamp[curr]));
742 write_stats_core(!curr, curr, dis, cur_time[!curr], cur_time[curr]);
746 ***************************************************************************
747 * Read stats from /proc/interrupts or /proc/softirqs.
750 * @file /proc file to read (interrupts or softirqs).
751 * @ic_nr Number of interrupts (hard or soft) per CPU.
752 * @curr Position in array where current statistics will be saved.
755 * @st_ic Array for per-CPU interrupts statistics.
756 ***************************************************************************
758 void read_interrupts_stat(char *file, struct stats_irqcpu *st_ic[], int ic_nr, int curr)
761 struct stats_irq *st_irq_i;
762 struct stats_irqcpu *p;
763 char *line = NULL, *li;
764 unsigned long irq = 0;
766 int cpu_index[cpu_nr], index = 0, len;
769 /* Reset total number of interrupts received by each CPU */
770 for (cpu = 0; cpu < cpu_nr; cpu++) {
771 st_irq_i = st_irq[curr] + cpu + 1;
772 st_irq_i->irq_nr = 0;
775 if ((fp = fopen(file, "r")) != NULL) {
777 SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
780 * Parse header line to see which CPUs are online
782 while (fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) {
784 while (((cp = strstr(next, "CPU")) != NULL) && (index < cpu_nr)) {
785 cpu = strtol(cp + 3, &next, 10);
786 cpu_index[index++] = cpu;
789 /* Header line found */
793 /* Parse each line of interrupts statistics data */
794 while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) &&
797 /* Skip over "<irq>:" */
798 if ((cp = strchr(line, ':')) == NULL)
799 /* Chr ':' not found */
803 p = st_ic[curr] + irq;
805 /* Remove possible heading spaces in interrupt's name... */
810 len = strcspn(li, ":");
811 if (len >= MAX_IRQ_LEN) {
812 len = MAX_IRQ_LEN - 1;
814 /* ...then save its name */
815 strncpy(p->irq_name, li, len);
816 p->irq_name[len] = '\0';
818 /* For each interrupt: Get number received by each CPU */
819 for (cpu = 0; cpu < index; cpu++) {
820 p = st_ic[curr] + cpu_index[cpu] * ic_nr + irq;
821 st_irq_i = st_irq[curr] + cpu_index[cpu] + 1;
823 * No need to set (st_irqcpu + cpu * irqcpu_nr)->irq_name:
824 * This is the same as st_irqcpu->irq_name.
825 * Now save current interrupt value for current CPU (in stats_irqcpu structure)
826 * and total number of interrupts received by current CPU (in stats_irq structure).
828 p->interrupt = strtoul(cp, &next, 10);
829 st_irq_i->irq_nr += p->interrupt;
840 while (irq < ic_nr) {
841 /* Nb of interrupts per processor has changed */
842 p = st_ic[curr] + irq;
843 p->irq_name[0] = '\0'; /* This value means this is a dummy interrupt */
849 ***************************************************************************
850 * Main loop: Read stats from the relevant sources, and display them.
853 * @dis_hdr Set to TRUE if the header line must always be printed.
854 * @rows Number of rows of screen.
855 ***************************************************************************
857 void rw_mpstat_loop(int dis_hdr, int rows)
859 struct stats_cpu *scc;
861 int curr = 1, dis = 1;
862 unsigned long lines = rows;
864 /* Dont buffer data if redirected to a pipe */
865 setbuf(stdout, NULL);
867 /* Read uptime and CPU stats */
870 * Init uptime0. So if /proc/uptime cannot fill it,
871 * this will be done by /proc/stat.
874 read_uptime(&(uptime0[0]));
876 read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]), &(uptime0[0]));
879 * Read total number of interrupts received among all CPU.
880 * (this is the first value on the line "intr:" in the /proc/stat file).
882 if (DISPLAY_IRQ_SUM(actflags)) {
883 read_stat_irq(st_irq[0], 1);
887 * Read number of interrupts received by each CPU, for each interrupt,
888 * and compute the total number of interrupts received by each CPU.
890 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
891 /* Read this file to display int per CPU or total nr of int per CPU */
892 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, 0);
894 if (DISPLAY_SOFTIRQS(actflags)) {
895 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, 0);
899 /* Display since boot time */
900 mp_tstamp[1] = mp_tstamp[0];
901 memset(st_cpu[1], 0, STATS_CPU_SIZE * (cpu_nr + 1));
902 memset(st_irq[1], 0, STATS_IRQ_SIZE * (cpu_nr + 1));
903 memset(st_irqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
904 if (DISPLAY_SOFTIRQS(actflags)) {
905 memset(st_softirqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
907 write_stats(0, DISP_HDR);
911 /* Set a handler for SIGALRM */
912 memset(&alrm_act, 0, sizeof(alrm_act));
913 alrm_act.sa_handler = alarm_handler;
914 sigaction(SIGALRM, &alrm_act, NULL);
917 /* Save the first stats collected. Will be used to compute the average */
918 mp_tstamp[2] = mp_tstamp[0];
919 uptime[2] = uptime[0];
920 uptime0[2] = uptime0[0];
921 memcpy(st_cpu[2], st_cpu[0], STATS_CPU_SIZE * (cpu_nr + 1));
922 memcpy(st_irq[2], st_irq[0], STATS_IRQ_SIZE * (cpu_nr + 1));
923 memcpy(st_irqcpu[2], st_irqcpu[0], STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
924 if (DISPLAY_SOFTIRQS(actflags)) {
925 memcpy(st_softirqcpu[2], st_softirqcpu[0],
926 STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
929 /* Set a handler for SIGINT */
930 memset(&int_act, 0, sizeof(int_act));
931 int_act.sa_handler = int_handler;
932 sigaction(SIGINT, &int_act, NULL);
937 /* SIGINT signal caught during first interval: Exit immediately */
942 * Resetting the structure not needed since every fields will be set.
943 * Exceptions are per-CPU structures: Some of them may not be filled
944 * if corresponding processor is disabled (offline). We set them to zero
945 * to be able to distinguish between offline and tickless CPUs.
947 for (cpu = 1; cpu <= cpu_nr; cpu++) {
948 scc = st_cpu[curr] + cpu;
949 memset(scc, 0, STATS_CPU_SIZE);
953 get_localtime(&(mp_tstamp[curr]), 0);
955 /* Read uptime and CPU stats */
958 read_uptime(&(uptime0[curr]));
960 read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]), &(uptime0[curr]));
962 /* Read total number of interrupts received among all CPU */
963 if (DISPLAY_IRQ_SUM(actflags)) {
964 read_stat_irq(st_irq[curr], 1);
968 * Read number of interrupts received by each CPU, for each interrupt,
969 * and compute the total number of interrupts received by each CPU.
971 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
972 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, curr);
974 if (DISPLAY_SOFTIRQS(actflags)) {
975 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, curr);
986 write_stats(curr, dis);
997 /* SIGINT signal caught => Display average stats */
999 printf("\n"); /* Skip "^C" displayed on screen */
1008 /* Write stats average */
1009 write_stats_avg(curr, dis_hdr);
1013 ***************************************************************************
1014 * Main entry to the program
1015 ***************************************************************************
1017 int main(int argc, char **argv)
1019 int opt = 0, i, actset = FALSE;
1020 struct utsname header;
1026 /* Init National Language Support */
1030 /* Init color strings */
1036 /* What is the highest processor number on this machine? */
1037 cpu_nr = get_cpu_nr(~0, TRUE);
1039 /* Calculate number of interrupts per processor */
1040 irqcpu_nr = get_irqcpu_nr(INTERRUPTS, NR_IRQS, cpu_nr) +
1042 /* Calculate number of soft interrupts per processor */
1043 softirqcpu_nr = get_irqcpu_nr(SOFTIRQS, NR_IRQS, cpu_nr) +
1047 * cpu_nr: a value of 2 means there are 2 processors (0 and 1).
1048 * In this case, we have to allocate 3 structures: global, proc0 and proc1.
1050 salloc_mp_struct(cpu_nr + 1);
1052 while (++opt < argc) {
1054 if (!strcmp(argv[opt], "-I")) {
1058 for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
1059 if (!strcmp(t, K_SUM)) {
1060 /* Display total number of interrupts per CPU */
1061 actflags |= M_D_IRQ_SUM;
1063 else if (!strcmp(t, K_CPU)) {
1064 /* Display interrupts per CPU */
1065 actflags |= M_D_IRQ_CPU;
1067 else if (!strcmp(t, K_SCPU)) {
1068 /* Display soft interrupts per CPU */
1069 actflags |= M_D_SOFTIRQS;
1071 else if (!strcmp(t, K_ALL)) {
1072 actflags |= M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
1084 else if (!strcmp(argv[opt], "-P")) {
1085 /* '-P ALL' can be used on UP machines */
1087 flags |= F_P_OPTION;
1090 for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
1091 if (!strcmp(t, K_ALL) || !strcmp(t, K_ON)) {
1096 * Set bit for every processor.
1097 * Also indicate to display stats for CPU 'all'.
1099 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
1100 if (!strcmp(t, K_ON)) {
1101 /* Display stats only for online CPU */
1106 if (strspn(t, DIGITS) != strlen(t)) {
1109 i = atoi(t); /* Get cpu number */
1111 fprintf(stderr, _("Not that many processors!\n"));
1115 *(cpu_bitmap + (i >> 3)) |= 1 << (i & 0x07);
1124 else if (!strncmp(argv[opt], "-", 1)) {
1125 for (i = 1; *(argv[opt] + i); i++) {
1127 switch (*(argv[opt] + i)) {
1130 actflags |= M_D_CPU + M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
1132 /* Select all processors */
1133 flags |= F_P_OPTION;
1134 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
1139 actflags |= M_D_CPU;
1143 /* Print version number */
1153 else if (interval < 0) {
1155 if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
1158 interval = atol(argv[opt]);
1165 else if (count <= 0) {
1166 /* Get count value */
1167 if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
1171 count = atol(argv[opt]);
1182 /* Default: Display CPU */
1184 actflags |= M_D_CPU;
1187 if (count_bits(&actflags, sizeof(unsigned int)) > 1) {
1191 if (!USE_P_OPTION(flags)) {
1192 /* Option -P not used: Set bit 0 (global stats among all proc) */
1199 /* Get window size */
1200 rows = get_win_height();
1203 /* Interval not set => display stats since boot time */
1208 get_localtime(&(mp_tstamp[0]), 0);
1210 /* Get system name, release number and hostname */
1212 print_gal_header(&(mp_tstamp[0]), header.sysname, header.release,
1213 header.nodename, header.machine, get_cpu_nr(~0, FALSE),
1217 rw_mpstat_loop(dis_hdr, rows);
1219 /* Free structures */