2 * mpstat: per-processor statistics
3 * (C) 2000-2015 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 /* 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 strftime(cur_time[!curr], sizeof(cur_time[!curr]), "%X", &(mp_tstamp[!curr]));
729 /* Get current timestamp */
730 strftime(cur_time[curr], sizeof(cur_time[curr]), "%X", &(mp_tstamp[curr]));
732 write_stats_core(!curr, curr, dis, cur_time[!curr], cur_time[curr]);
736 ***************************************************************************
737 * Read stats from /proc/interrupts or /proc/softirqs.
740 * @file /proc file to read (interrupts or softirqs).
741 * @ic_nr Number of interrupts (hard or soft) per CPU.
742 * @curr Position in array where current statistics will be saved.
745 * @st_ic Array for per-CPU interrupts statistics.
746 ***************************************************************************
748 void read_interrupts_stat(char *file, struct stats_irqcpu *st_ic[], int ic_nr, int curr)
751 struct stats_irq *st_irq_i;
752 struct stats_irqcpu *p;
753 char *line = NULL, *li;
754 unsigned long irq = 0;
756 int cpu_index[cpu_nr], index = 0, len;
759 /* Reset total number of interrupts received by each CPU */
760 for (cpu = 0; cpu < cpu_nr; cpu++) {
761 st_irq_i = st_irq[curr] + cpu + 1;
762 st_irq_i->irq_nr = 0;
765 if ((fp = fopen(file, "r")) != NULL) {
767 SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
770 * Parse header line to see which CPUs are online
772 while (fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) {
774 while (((cp = strstr(next, "CPU")) != NULL) && (index < cpu_nr)) {
775 cpu = strtol(cp + 3, &next, 10);
776 cpu_index[index++] = cpu;
779 /* Header line found */
783 /* Parse each line of interrupts statistics data */
784 while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) &&
787 /* Skip over "<irq>:" */
788 if ((cp = strchr(line, ':')) == NULL)
789 /* Chr ':' not found */
793 p = st_ic[curr] + irq;
795 /* Remove possible heading spaces in interrupt's name... */
800 len = strcspn(li, ":");
801 if (len >= MAX_IRQ_LEN) {
802 len = MAX_IRQ_LEN - 1;
804 /* ...then save its name */
805 strncpy(p->irq_name, li, len);
806 p->irq_name[len] = '\0';
808 /* For each interrupt: Get number received by each CPU */
809 for (cpu = 0; cpu < index; cpu++) {
810 p = st_ic[curr] + cpu_index[cpu] * ic_nr + irq;
811 st_irq_i = st_irq[curr] + cpu_index[cpu] + 1;
813 * No need to set (st_irqcpu + cpu * irqcpu_nr)->irq_name:
814 * This is the same as st_irqcpu->irq_name.
815 * Now save current interrupt value for current CPU (in stats_irqcpu structure)
816 * and total number of interrupts received by current CPU (in stats_irq structure).
818 p->interrupt = strtoul(cp, &next, 10);
819 st_irq_i->irq_nr += p->interrupt;
830 while (irq < ic_nr) {
831 /* Nb of interrupts per processor has changed */
832 p = st_ic[curr] + irq;
833 p->irq_name[0] = '\0'; /* This value means this is a dummy interrupt */
839 ***************************************************************************
840 * Main loop: Read stats from the relevant sources, and display them.
843 * @dis_hdr Set to TRUE if the header line must always be printed.
844 * @rows Number of rows of screen.
845 ***************************************************************************
847 void rw_mpstat_loop(int dis_hdr, int rows)
849 struct stats_cpu *scc;
851 int curr = 1, dis = 1;
852 unsigned long lines = rows;
854 /* Dont buffer data if redirected to a pipe */
855 setbuf(stdout, NULL);
857 /* Read uptime and CPU stats */
860 * Init uptime0. So if /proc/uptime cannot fill it,
861 * this will be done by /proc/stat.
864 read_uptime(&(uptime0[0]));
866 read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]), &(uptime0[0]));
869 * Read total number of interrupts received among all CPU.
870 * (this is the first value on the line "intr:" in the /proc/stat file).
872 if (DISPLAY_IRQ_SUM(actflags)) {
873 read_stat_irq(st_irq[0], 1);
877 * Read number of interrupts received by each CPU, for each interrupt,
878 * and compute the total number of interrupts received by each CPU.
880 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
881 /* Read this file to display int per CPU or total nr of int per CPU */
882 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, 0);
884 if (DISPLAY_SOFTIRQS(actflags)) {
885 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, 0);
889 /* Display since boot time */
890 mp_tstamp[1] = mp_tstamp[0];
891 memset(st_cpu[1], 0, STATS_CPU_SIZE * (cpu_nr + 1));
892 memset(st_irq[1], 0, STATS_IRQ_SIZE * (cpu_nr + 1));
893 memset(st_irqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
894 if (DISPLAY_SOFTIRQS(actflags)) {
895 memset(st_softirqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
897 write_stats(0, DISP_HDR);
901 /* Set a handler for SIGALRM */
902 memset(&alrm_act, 0, sizeof(alrm_act));
903 alrm_act.sa_handler = alarm_handler;
904 sigaction(SIGALRM, &alrm_act, NULL);
907 /* Save the first stats collected. Will be used to compute the average */
908 mp_tstamp[2] = mp_tstamp[0];
909 uptime[2] = uptime[0];
910 uptime0[2] = uptime0[0];
911 memcpy(st_cpu[2], st_cpu[0], STATS_CPU_SIZE * (cpu_nr + 1));
912 memcpy(st_irq[2], st_irq[0], STATS_IRQ_SIZE * (cpu_nr + 1));
913 memcpy(st_irqcpu[2], st_irqcpu[0], STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
914 if (DISPLAY_SOFTIRQS(actflags)) {
915 memcpy(st_softirqcpu[2], st_softirqcpu[0],
916 STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
919 /* Set a handler for SIGINT */
920 memset(&int_act, 0, sizeof(int_act));
921 int_act.sa_handler = int_handler;
922 sigaction(SIGINT, &int_act, NULL);
927 /* SIGINT signal caught during first interval: Exit immediately */
932 * Resetting the structure not needed since every fields will be set.
933 * Exceptions are per-CPU structures: Some of them may not be filled
934 * if corresponding processor is disabled (offline). We set them to zero
935 * to be able to distinguish between offline and tickless CPUs.
937 for (cpu = 1; cpu <= cpu_nr; cpu++) {
938 scc = st_cpu[curr] + cpu;
939 memset(scc, 0, STATS_CPU_SIZE);
943 get_localtime(&(mp_tstamp[curr]), 0);
945 /* Read uptime and CPU stats */
948 read_uptime(&(uptime0[curr]));
950 read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]), &(uptime0[curr]));
952 /* Read total number of interrupts received among all CPU */
953 if (DISPLAY_IRQ_SUM(actflags)) {
954 read_stat_irq(st_irq[curr], 1);
958 * Read number of interrupts received by each CPU, for each interrupt,
959 * and compute the total number of interrupts received by each CPU.
961 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
962 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, curr);
964 if (DISPLAY_SOFTIRQS(actflags)) {
965 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, curr);
976 write_stats(curr, dis);
987 /* SIGINT signal caught => Display average stats */
989 printf("\n"); /* Skip "^C" displayed on screen */
998 /* Write stats average */
999 write_stats_avg(curr, dis_hdr);
1003 ***************************************************************************
1004 * Main entry to the program
1005 ***************************************************************************
1007 int main(int argc, char **argv)
1009 int opt = 0, i, actset = FALSE;
1010 struct utsname header;
1016 /* Init National Language Support */
1020 /* Init color strings */
1026 /* What is the highest processor number on this machine? */
1027 cpu_nr = get_cpu_nr(~0, TRUE);
1029 /* Calculate number of interrupts per processor */
1030 irqcpu_nr = get_irqcpu_nr(INTERRUPTS, NR_IRQS, cpu_nr) +
1032 /* Calculate number of soft interrupts per processor */
1033 softirqcpu_nr = get_irqcpu_nr(SOFTIRQS, NR_IRQS, cpu_nr) +
1037 * cpu_nr: a value of 2 means there are 2 processors (0 and 1).
1038 * In this case, we have to allocate 3 structures: global, proc0 and proc1.
1040 salloc_mp_struct(cpu_nr + 1);
1042 while (++opt < argc) {
1044 if (!strcmp(argv[opt], "-I")) {
1047 if (!strcmp(argv[opt], K_SUM)) {
1048 /* Display total number of interrupts per CPU */
1049 actflags |= M_D_IRQ_SUM;
1051 else if (!strcmp(argv[opt], K_CPU)) {
1052 /* Display interrupts per CPU */
1053 actflags |= M_D_IRQ_CPU;
1055 else if (!strcmp(argv[opt], K_SCPU)) {
1056 /* Display soft interrupts per CPU */
1057 actflags |= M_D_SOFTIRQS;
1059 else if (!strcmp(argv[opt], K_ALL)) {
1060 actflags |= M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
1071 else if (!strcmp(argv[opt], "-P")) {
1072 /* '-P ALL' can be used on UP machines */
1074 flags |= F_P_OPTION;
1077 for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
1078 if (!strcmp(t, K_ALL) || !strcmp(t, K_ON)) {
1083 * Set bit for every processor.
1084 * Also indicate to display stats for CPU 'all'.
1086 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
1087 if (!strcmp(t, K_ON)) {
1088 /* Display stats only for online CPU */
1093 if (strspn(t, DIGITS) != strlen(t)) {
1096 i = atoi(t); /* Get cpu number */
1098 fprintf(stderr, _("Not that many processors!\n"));
1102 *(cpu_bitmap + (i >> 3)) |= 1 << (i & 0x07);
1111 else if (!strncmp(argv[opt], "-", 1)) {
1112 for (i = 1; *(argv[opt] + i); i++) {
1114 switch (*(argv[opt] + i)) {
1117 actflags |= M_D_CPU + M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
1119 /* Select all processors */
1120 flags |= F_P_OPTION;
1121 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
1126 actflags |= M_D_CPU;
1130 /* Print version number */
1140 else if (interval < 0) {
1142 if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
1145 interval = atol(argv[opt]);
1152 else if (count <= 0) {
1153 /* Get count value */
1154 if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
1158 count = atol(argv[opt]);
1169 /* Default: Display CPU */
1171 actflags |= M_D_CPU;
1174 if (count_bits(&actflags, sizeof(unsigned int)) > 1) {
1178 if (!USE_P_OPTION(flags)) {
1179 /* Option -P not used: Set bit 0 (global stats among all proc) */
1186 /* Get window size */
1187 rows = get_win_height();
1190 /* Interval not set => display stats since boot time */
1195 get_localtime(&(mp_tstamp[0]), 0);
1197 /* Get system name, release number and hostname */
1199 print_gal_header(&(mp_tstamp[0]), header.sysname, header.release,
1200 header.nodename, header.machine, get_cpu_nr(~0, FALSE));
1203 rw_mpstat_loop(dis_hdr, rows);
1205 /* Free structures */