]> granicus.if.org Git - sysstat/blob - mpstat.c
sadc now overwrites its daily data file when it is from a previous month.
[sysstat] / mpstat.c
1 /*
2  * mpstat: per-processor statistics
3  * (C) 2000-2011 by Sebastien GODARD (sysstat <at> orange.fr)
4  *
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.                                              *
10  *                                                                         *
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 *
14  * for more details.                                                       *
15  *                                                                         *
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  ***************************************************************************
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <sys/utsname.h>
30
31 #include "version.h"
32 #include "mpstat.h"
33 #include "common.h"
34 #include "rd_stats.h"
35
36 #ifdef USE_NLS
37 #include <locale.h>
38 #include <libintl.h>
39 #define _(string) gettext(string)
40 #else
41 #define _(string) (string)
42 #endif
43
44 #define SCCSID "@(#)sysstat-" VERSION ": "  __FILE__ " compiled " __DATE__ " " __TIME__
45 char *sccsid(void) { return (SCCSID); }
46
47 unsigned long long uptime[3] = {0, 0, 0};
48 unsigned long long uptime0[3] = {0, 0, 0};
49
50 /* NOTE: Use array of _char_ for bitmaps to avoid endianness problems...*/
51 unsigned char *cpu_bitmap;      /* Bit 0: Global; Bit 1: 1st proc; etc. */
52
53 /* Structures used to store stats */
54 struct stats_cpu *st_cpu[3];
55 struct stats_irq *st_irq[3];
56 struct stats_irqcpu *st_irqcpu[3];
57 struct stats_irqcpu *st_softirqcpu[3];
58
59 struct tm mp_tstamp[3];
60
61 /* Activity flag */
62 unsigned int actflags = 0;
63
64 unsigned int flags = 0;
65
66 /* Interval and count parameters */
67 long interval = -1, count = 0;
68
69 /* Nb of processors on the machine */
70 int cpu_nr = 0;
71 /* Nb of interrupts per processor */
72 int irqcpu_nr = 0;
73 /* Nb of soft interrupts per processor */
74 int softirqcpu_nr = 0;
75
76 /*
77  ***************************************************************************
78  * Print usage and exit
79  *
80  * IN:
81  * @progname    Name of sysstat command
82  ***************************************************************************
83  */
84 void usage(char *progname)
85 {
86         fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
87                 progname);
88
89         fprintf(stderr, _("Options are:\n"
90                           "[ -A ] [ -I { SUM | CPU | SCPU | ALL } ] [ -u ]\n"
91                           "[ -P { <cpu> [,...] | ON | ALL } ] [ -V ]\n"));
92         exit(1);
93 }
94
95 /*
96  ***************************************************************************
97  * SIGALRM signal handler
98  *
99  * IN:
100  * @sig Signal number. Set to 0 for the first time, then to SIGALRM.
101  ***************************************************************************
102  */
103 void alarm_handler(int sig)
104 {
105         signal(SIGALRM, alarm_handler);
106         alarm(interval);
107 }
108
109 /*
110  ***************************************************************************
111  * Allocate stats structures and cpu bitmap.
112  *
113  * IN:
114  * @nr_cpus     Number of CPUs. This is the real number of available CPUs + 1
115  *              because we also have to allocate a structure for CPU 'all'.
116  ***************************************************************************
117  */
118 void salloc_mp_struct(int nr_cpus)
119 {
120         int i;
121
122         for (i = 0; i < 3; i++) {
123
124                 if ((st_cpu[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * nr_cpus))
125                     == NULL) {
126                         perror("malloc");
127                         exit(4);
128                 }
129                 memset(st_cpu[i], 0, STATS_CPU_SIZE * nr_cpus);
130
131                 if ((st_irq[i] = (struct stats_irq *) malloc(STATS_IRQ_SIZE * nr_cpus))
132                     == NULL) {
133                         perror("malloc");
134                         exit(4);
135                 }
136                 memset(st_irq[i], 0, STATS_IRQ_SIZE * nr_cpus);
137
138                 if ((st_irqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr))
139                     == NULL) {
140                         perror("malloc");
141                         exit(4);
142                 }
143                 memset(st_irqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr);
144
145                 if ((st_softirqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr))
146                      == NULL) {
147                         perror("malloc");
148                         exit(4);
149                 }
150                 memset(st_softirqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr);
151         }
152
153         if ((cpu_bitmap = (unsigned char *) malloc((nr_cpus >> 3) + 1)) == NULL) {
154                 perror("malloc");
155                 exit(4);
156         }
157         memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1);
158 }
159
160 /*
161  ***************************************************************************
162  * Free structures and bitmap.
163  ***************************************************************************
164  */
165 void sfree_mp_struct(void)
166 {
167         int i;
168
169         for (i = 0; i < 3; i++) {
170
171                 if (st_cpu[i]) {
172                         free(st_cpu[i]);
173                 }
174                 if (st_irq[i]) {
175                         free(st_irq[i]);
176                 }
177                 if (st_irqcpu[i]) {
178                         free(st_irqcpu[i]);
179                 }
180                 if (st_softirqcpu[i]) {
181                         free(st_softirqcpu[i]);
182                 }
183         }
184
185         if (cpu_bitmap) {
186                 free(cpu_bitmap);
187         }
188 }
189
190 /*
191  ***************************************************************************
192  * Display per CPU statistics.
193  *
194  * IN:
195  * @st_ic       Array for per-CPU statistics.
196  * @ic_nr       Number of interrupts (hard or soft) per CPU.
197  * @dis         TRUE if a header line must be printed.
198  * @itv         Interval value.
199  * @prev        Position in array where statistics used as reference are.
200  *              Stats used as reference may be the previous ones read, or
201  *              the very first ones when calculating the average.
202  * @curr        Position in array where current statistics will be saved.
203  * @prev_string String displayed at the beginning of a header line. This is
204  *              the timestamp of the previous sample, or "Average" when
205  *              displaying average stats.
206  * @curr_string String displayed at the beginning of current sample stats.
207  *              This is the timestamp of the current sample, or "Average"
208  *              when displaying average stats.
209  ***************************************************************************
210  */
211 void write_irqcpu_stats(struct stats_irqcpu *st_ic[], int ic_nr, int dis,
212                         unsigned long long itv, int prev, int curr,
213                         char *prev_string, char *curr_string)
214 {
215         struct stats_cpu *scc, *scp;
216         int j = 0, offset, cpu;
217         struct stats_irqcpu *p, *q, *p0, *q0;
218
219         /*
220         * Check if number of interrupts has changed.
221         * NB: A null interval value indicates that we are
222          * displaying statistics since system startup.
223          */
224         if (!dis && interval) {
225                 do {
226                         p0 = st_ic[curr] + j;
227                         if (p0->irq_name[0] != '\0') {
228                                 q0 = st_ic[prev] + j;
229                                 if (strcmp(p0->irq_name, q0->irq_name)) {
230                                         /* These are two different irq */
231                                         j = -2;
232                                 }
233                         }
234                         j++;
235                 }
236                 while ((j > 0) && (j <= ic_nr));
237         }
238
239         if (dis || (j < 0)) {
240                 /* Print header */
241                 printf("\n%-11s  CPU", prev_string);
242                 for (j = 0; j < ic_nr; j++) {
243                         p0 = st_ic[curr] + j;
244                         if (p0->irq_name[0] != '\0') {  /* Nb of irq per proc may have varied... */
245                                 printf(" %8s/s", p0->irq_name);
246                         }
247                 }
248                 printf("\n");
249         }
250
251         for (cpu = 1; cpu <= cpu_nr; cpu++) {
252
253                 scc = st_cpu[curr] + cpu;
254                 scp = st_cpu[prev] + cpu;
255
256                 /*
257                  * Check if we want stats about this CPU.
258                  * CPU must have been explicitly selected using option -P,
259                  * else we display every CPU.
260                  */
261                 if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))) && USE_P_OPTION(flags))
262                         continue;
263
264                 if ((scc->cpu_user    + scc->cpu_nice + scc->cpu_sys   +
265                      scc->cpu_iowait  + scc->cpu_idle + scc->cpu_steal +
266                      scc->cpu_hardirq + scc->cpu_softirq) == 0) {
267
268                         /* Offline CPU found */
269
270                         if (DISPLAY_ONLINE_CPU(flags))
271                                 continue;
272                 }
273
274                 printf("%-11s  %3d", curr_string, cpu - 1);
275
276                 for (j = 0; j < ic_nr; j++) {
277                         p0 = st_ic[curr] + j;   /* irq field set only for proc #0 */
278                         /*
279                          * An empty string for irq name means it is a remaining interrupt
280                          * which is no longer used, for example because the
281                          * number of interrupts has decreased in /proc/interrupts.
282                          */
283                         if (p0->irq_name[0] != '\0') {
284                                 q0 = st_ic[prev] + j;
285                                 offset = j;
286
287                                 /*
288                                  * If we want stats for the time since system startup,
289                                  * we have p0->irq != q0->irq, since q0 structure is
290                                  * completely set to zero.
291                                  */
292                                 if (strcmp(p0->irq_name, q0->irq_name) && interval) {
293                                         if (j)
294                                                 offset = j - 1;
295                                         q0 = st_ic[prev] + offset;
296                                         if (strcmp(p0->irq_name, q0->irq_name) && (j + 1 < ic_nr))
297                                                 offset = j + 1;
298                                         q0 = st_ic[prev] + offset;
299                                 }
300
301                                 if (!strcmp(p0->irq_name, q0->irq_name) || !interval) {
302                                         p = st_ic[curr] + (cpu - 1) * ic_nr + j;
303                                         q = st_ic[prev] + (cpu - 1) * ic_nr + offset;
304                                         printf(" %10.2f",
305                                                S_VALUE(q->interrupt, p->interrupt, itv));
306                                 }
307                                 else
308                                         printf("        N/A");
309                         }
310                 }
311                 printf("\n");
312         }
313 }
314
315 /*
316  ***************************************************************************
317  * Core function used to display statistics.
318  *
319  * IN:
320  * @prev        Position in array where statistics used as reference are.
321  *              Stats used as reference may be the previous ones read, or
322  *              the very first ones when calculating the average.
323  * @curr        Position in array where statistics for current sample are.
324  * @dis         TRUE if a header line must be printed.
325  * @prev_string String displayed at the beginning of a header line. This is
326  *              the timestamp of the previous sample, or "Average" when
327  *              displaying average stats.
328  * @curr_string String displayed at the beginning of current sample stats.
329  *              This is the timestamp of the current sample, or "Average"
330  *              when displaying average stats.
331  ***************************************************************************
332  */
333 void write_stats_core(int prev, int curr, int dis,
334                       char *prev_string, char *curr_string)
335 {
336         struct stats_cpu *scc, *scp;
337         unsigned long long itv, pc_itv, g_itv;
338         int cpu;
339
340         /* Test stdout */
341         TEST_STDOUT(STDOUT_FILENO);
342
343         /* Compute time interval */
344         g_itv = get_interval(uptime[prev], uptime[curr]);
345
346         /* Reduce interval value to one processor */
347         if (cpu_nr > 1) {
348                 itv = get_interval(uptime0[prev], uptime0[curr]);
349         }
350         else {
351                 itv = g_itv;
352         }
353
354         /* Print CPU stats */
355         if (DISPLAY_CPU(actflags)) {
356                 if (dis) {
357                         printf("\n%-11s  CPU    %%usr   %%nice    %%sys %%iowait    %%irq   "
358                                "%%soft  %%steal  %%guest   %%idle\n",
359                                prev_string);
360                 }
361
362                 /* Check if we want global stats among all proc */
363                 if (*cpu_bitmap & 1) {
364
365                         printf("%-11s  all", curr_string);
366
367                         printf("  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f\n",
368                                (st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest) <
369                                (st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest) ?
370                                0.0 :
371                                ll_sp_value(st_cpu[prev]->cpu_user - st_cpu[prev]->cpu_guest,
372                                            st_cpu[curr]->cpu_user - st_cpu[curr]->cpu_guest,
373                                            g_itv),
374                                ll_sp_value(st_cpu[prev]->cpu_nice,
375                                            st_cpu[curr]->cpu_nice,
376                                            g_itv),
377                                ll_sp_value(st_cpu[prev]->cpu_sys,
378                                            st_cpu[curr]->cpu_sys,
379                                            g_itv),
380                                ll_sp_value(st_cpu[prev]->cpu_iowait,
381                                            st_cpu[curr]->cpu_iowait,
382                                            g_itv),
383                                ll_sp_value(st_cpu[prev]->cpu_hardirq,
384                                            st_cpu[curr]->cpu_hardirq,
385                                            g_itv),
386                                ll_sp_value(st_cpu[prev]->cpu_softirq,
387                                            st_cpu[curr]->cpu_softirq,
388                                            g_itv),
389                                ll_sp_value(st_cpu[prev]->cpu_steal,
390                                            st_cpu[curr]->cpu_steal,
391                                            g_itv),
392                                ll_sp_value(st_cpu[prev]->cpu_guest,
393                                            st_cpu[curr]->cpu_guest,
394                                            g_itv),
395                                (st_cpu[curr]->cpu_idle < st_cpu[prev]->cpu_idle) ?
396                                0.0 :
397                                ll_sp_value(st_cpu[prev]->cpu_idle,
398                                            st_cpu[curr]->cpu_idle,
399                                            g_itv));
400                 }
401
402                 for (cpu = 1; cpu <= cpu_nr; cpu++) {
403
404                         scc = st_cpu[curr] + cpu;
405                         scp = st_cpu[prev] + cpu;
406
407                         /* Check if we want stats about this proc */
408                         if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
409                                 continue;
410                         
411                         /*
412                          * If the CPU is offline then it is omited from /proc/stat
413                          * and the sum of all values is zero.
414                          * (Remember that guest time is already included in user mode.)
415                          */
416                         if ((scc->cpu_user    + scc->cpu_nice + scc->cpu_sys   +
417                              scc->cpu_iowait  + scc->cpu_idle + scc->cpu_steal +
418                              scc->cpu_hardirq + scc->cpu_softirq) == 0) {
419
420                                 if (!DISPLAY_ONLINE_CPU(flags)) {
421                                         printf("%-11s %4d"
422                                                "  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f"
423                                                "  %6.2f  %6.2f  %6.2f\n",
424                                                curr_string, cpu - 1,
425                                                0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
426                                 }
427                                 continue;
428                         }
429
430                         printf("%-11s %4d", curr_string, cpu - 1);
431
432                         /* Recalculate itv for current proc */
433                         pc_itv = get_per_cpu_interval(scc, scp);
434                         
435                         if (!pc_itv) {
436                                 /*
437                                  * If the CPU is tickless then there is no change in CPU values
438                                  * but the sum of values is not zero.
439                                  */
440                                 printf("  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f"
441                                        "  %6.2f  %6.2f  %6.2f\n",
442                                        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 100.0);
443                         }
444
445                         else {
446                                 printf("  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f  %6.2f"
447                                        "  %6.2f  %6.2f  %6.2f\n",
448                                        (scc->cpu_user - scc->cpu_guest) < (scp->cpu_user - scp->cpu_guest) ?
449                                        0.0 :
450                                        ll_sp_value(scp->cpu_user - scp->cpu_guest,
451                                                    scc->cpu_user - scc->cpu_guest,
452                                                    pc_itv),
453                                        ll_sp_value(scp->cpu_nice,
454                                                    scc->cpu_nice,
455                                                    pc_itv),
456                                        ll_sp_value(scp->cpu_sys,
457                                                    scc->cpu_sys,
458                                                    pc_itv),
459                                        ll_sp_value(scp->cpu_iowait,
460                                                    scc->cpu_iowait,
461                                                    pc_itv),
462                                        ll_sp_value(scp->cpu_hardirq,
463                                                    scc->cpu_hardirq,
464                                                    pc_itv),
465                                        ll_sp_value(scp->cpu_softirq,
466                                                    scc->cpu_softirq,
467                                                    pc_itv),
468                                        ll_sp_value(scp->cpu_steal,
469                                                    scc->cpu_steal,
470                                                    pc_itv),
471                                        ll_sp_value(scp->cpu_guest,
472                                                    scc->cpu_guest,
473                                                    pc_itv),
474                                        (scc->cpu_idle < scp->cpu_idle) ?
475                                        0.0 :
476                                        ll_sp_value(scp->cpu_idle,
477                                                    scc->cpu_idle,
478                                                    pc_itv));
479                         }
480                 }
481         }
482
483         /* Print total number of interrupts per processor */
484         if (DISPLAY_IRQ_SUM(actflags)) {
485                 struct stats_irq *sic, *sip;
486
487                 if (dis) {
488                         printf("\n%-11s  CPU    intr/s\n", prev_string);
489                 }
490
491                 if (*cpu_bitmap & 1) {
492                         printf("%-11s  all %9.2f\n", curr_string,
493                                ll_s_value(st_irq[prev]->irq_nr, st_irq[curr]->irq_nr, itv));
494                 }
495
496                 for (cpu = 1; cpu <= cpu_nr; cpu++) {
497
498                         sic = st_irq[curr] + cpu;
499                         sip = st_irq[prev] + cpu;
500
501                         scc = st_cpu[curr] + cpu;
502                         scp = st_cpu[prev] + cpu;
503
504                         /* Check if we want stats about this proc */
505                         if (!(*(cpu_bitmap + (cpu >> 3)) & (1 << (cpu & 0x07))))
506                                 continue;
507
508                         if ((scc->cpu_user    + scc->cpu_nice + scc->cpu_sys   +
509                              scc->cpu_iowait  + scc->cpu_idle + scc->cpu_steal +
510                              scc->cpu_hardirq + scc->cpu_softirq) == 0) {
511
512                                 /* This is an offline CPU */
513                                 
514                                 if (!DISPLAY_ONLINE_CPU(flags)) {
515                                         printf("%-11s %4d"
516                                                "  %9.2f\n",
517                                                curr_string, cpu - 1,
518                                                0.0);
519                                 }
520                                 continue;
521                         }
522
523                         printf("%-11s %4d", curr_string, cpu - 1);
524
525                         /* Recalculate itv for current proc */
526                         pc_itv = get_per_cpu_interval(scc, scp);
527
528                         if (!pc_itv) {
529                                 /* This is a tickless CPU */
530                                 printf(" %9.2f\n", 0.0);
531                         }
532                         else {
533                                 printf(" %9.2f\n",
534                                        ll_s_value(sip->irq_nr, sic->irq_nr, itv));
535                         }
536                 }
537         }
538
539         if (DISPLAY_IRQ_CPU(actflags)) {
540                 write_irqcpu_stats(st_irqcpu, irqcpu_nr, dis, itv, prev, curr,
541                                    prev_string, curr_string);
542         }
543
544         if (DISPLAY_SOFTIRQS(actflags)) {
545                 write_irqcpu_stats(st_softirqcpu, softirqcpu_nr, dis, itv, prev, curr,
546                                    prev_string, curr_string);
547         }
548
549         /* Fix CPU counter values for every offline CPU */
550         for (cpu = 1; cpu <= cpu_nr; cpu++) {
551
552                 scc = st_cpu[curr] + cpu;
553                 scp = st_cpu[prev] + cpu;
554
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) {
558                         /*
559                          * Offline CPU found.
560                          * Set current struct fields (which have been set to zero)
561                          * to values from previous iteration. Hence their values won't
562                          * jump from zero when the CPU comes back online.
563                          */
564                         *scc = *scp;
565                 }
566         }
567 }
568
569 /*
570  ***************************************************************************
571  * Print statistics average.
572  *
573  * IN:
574  * @curr        Position in array where statistics for current sample are.
575  * @dis         TRUE if a header line must be printed.
576  ***************************************************************************
577  */
578 void write_stats_avg(int curr, int dis)
579 {
580         char string[16];
581
582         strncpy(string, _("Average:"), 16);
583         string[15] = '\0';
584         write_stats_core(2, curr, dis, string, string);
585 }
586
587 /*
588  ***************************************************************************
589  * Print statistics.
590  *
591  * IN:
592  * @curr        Position in array where statistics for current sample are.
593  * @dis         TRUE if a header line must be printed.
594  ***************************************************************************
595  */
596 void write_stats(int curr, int dis)
597 {
598         char cur_time[2][16];
599
600         /* Get previous timestamp */
601         strftime(cur_time[!curr], 16, "%X", &(mp_tstamp[!curr]));
602
603         /* Get current timestamp */
604         strftime(cur_time[curr], 16, "%X", &(mp_tstamp[curr]));
605
606         write_stats_core(!curr, curr, dis, cur_time[!curr], cur_time[curr]);
607 }
608
609 /*
610  ***************************************************************************
611  * Read stats from /proc/interrupts or /proc/softirqs.
612  *
613  * IN:
614  * @file        /proc file to read (interrupts or softirqs).
615  * @ic_nr       Number of interrupts (hard or soft) per CPU.
616  * @curr        Position in array where current statistics will be saved.
617  *
618  * OUT:
619  * @st_ic       Array for per-CPU statistics.
620  ***************************************************************************
621  */
622 void read_interrupts_stat(char *file, struct stats_irqcpu *st_ic[], int ic_nr, int curr)
623 {
624         FILE *fp;
625         struct stats_irq *st_irq_i;
626         struct stats_irqcpu *p;
627         char *line = NULL;
628         unsigned long irq = 0;
629         unsigned int cpu;
630         int cpu_index[cpu_nr], index = 0, dgt, len;
631         char *cp, *next;
632
633         for (cpu = 0; cpu < cpu_nr; cpu++) {
634                 st_irq_i = st_irq[curr] + cpu + 1;
635                 st_irq_i->irq_nr = 0;
636         }
637
638         if ((fp = fopen(file, "r")) != NULL) {
639
640                 SREALLOC(line, char, INTERRUPTS_LINE + 11 * cpu_nr);
641
642                 /*
643                  * Parse header line to see which CPUs are online
644                  */
645                 while (fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) {
646                         next = line;
647                         while (((cp = strstr(next, "CPU")) != NULL) && (index < cpu_nr)) {
648                                 cpu = strtol(cp + 3, &next, 10);
649                                 cpu_index[index++] = cpu;
650                         }
651                         if (index)
652                                 /* Header line found */
653                                 break;
654                 }
655
656                 while ((fgets(line, INTERRUPTS_LINE + 11 * cpu_nr, fp) != NULL) &&
657                        (irq < ic_nr)) {
658
659                         /* Skip over "<irq>:" */
660                         if ((cp = strchr(line, ':')) == NULL)
661                                 continue;
662                         cp++;
663
664                         p = st_ic[curr] + irq;
665                         len = strcspn(line, ":");
666                         if (len >= MAX_IRQ_LEN) {
667                                 len = MAX_IRQ_LEN - 1;
668                         }
669                         strncpy(p->irq_name, line, len);
670                         p->irq_name[len] = '\0';
671                         dgt = isdigit(line[len - 1]);
672
673                         for (cpu = 0; cpu < index; cpu++) {
674                                 p = st_ic[curr] + cpu_index[cpu] * ic_nr + irq;
675                                 st_irq_i = st_irq[curr] + cpu_index[cpu] + 1;
676                                 /*
677                                  * No need to set (st_irqcpu + cpu * irqcpu_nr)->irq:
678                                  * This is the same as st_irqcpu->irq.
679                                  */
680                                 p->interrupt = strtoul(cp, &next, 10);
681                                 if (dgt) {
682                                         /* Sum only numerical irq (and not NMI, LOC, etc.) */
683                                         st_irq_i->irq_nr += p->interrupt;
684                                 }
685                                 cp = next;
686                         }
687                         irq++;
688                 }
689
690                 fclose(fp);
691                 
692                 if (line) {
693                         free(line);
694                 }
695         }
696
697         while (irq < ic_nr) {
698                 /* Nb of interrupts per processor has changed */
699                 p = st_ic[curr] + irq;
700                 p->irq_name[0] = '\0';  /* This value means this is a dummy interrupt */
701                 irq++;
702         }
703 }
704
705 /*
706  ***************************************************************************
707  * Main loop: Read stats from the relevant sources, and display them.
708  *
709  * IN:
710  * @dis_hdr     Set to TRUE if the header line must always be printed.
711  * @rows        Number of rows of screen.
712  ***************************************************************************
713  */
714 void rw_mpstat_loop(int dis_hdr, int rows)
715 {
716         struct stats_cpu *scc;
717         int cpu;
718         int curr = 1, dis = 1;
719         unsigned long lines = rows;
720
721         /* Dont buffer data if redirected to a pipe */
722         setbuf(stdout, NULL);
723
724         /* Read stats */
725         if (cpu_nr > 1) {
726                 /*
727                  * Init uptime0. So if /proc/uptime cannot fill it,
728                  * this will be done by /proc/stat.
729                  */
730                 uptime0[0] = 0;
731                 read_uptime(&(uptime0[0]));
732         }
733         read_stat_cpu(st_cpu[0], cpu_nr + 1, &(uptime[0]), &(uptime0[0]));
734
735         if (DISPLAY_IRQ_SUM(actflags)) {
736                 read_stat_irq(st_irq[0], 1);
737         }
738
739         if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
740                 /* Read this file to display int per CPU or total nr of int per CPU */
741                 read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, 0);
742         }
743         
744         if (DISPLAY_SOFTIRQS(actflags)) {
745                 read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, 0);
746         }
747
748         if (!interval) {
749                 /* Display since boot time */
750                 mp_tstamp[1] = mp_tstamp[0];
751                 memset(st_cpu[1], 0, STATS_CPU_SIZE * (cpu_nr + 1));
752                 memset(st_irq[1], 0, STATS_IRQ_SIZE * (cpu_nr + 1));
753                 memset(st_irqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
754                 if (DISPLAY_SOFTIRQS(actflags)) {
755                         memset(st_softirqcpu[1], 0, STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
756                 }
757                 write_stats(0, DISP_HDR);
758                 exit(0);
759         }
760
761         /* Set a handler for SIGALRM */
762         alarm_handler(0);
763
764         /* Save the first stats collected. Will be used to compute the average */
765         mp_tstamp[2] = mp_tstamp[0];
766         uptime[2] = uptime[0];
767         uptime0[2] = uptime0[0];
768         memcpy(st_cpu[2], st_cpu[0], STATS_CPU_SIZE * (cpu_nr + 1));
769         memcpy(st_irq[2], st_irq[0], STATS_IRQ_SIZE * (cpu_nr + 1));
770         memcpy(st_irqcpu[2], st_irqcpu[0], STATS_IRQCPU_SIZE * (cpu_nr + 1) * irqcpu_nr);
771         if (DISPLAY_SOFTIRQS(actflags)) {
772                 memcpy(st_softirqcpu[2], st_softirqcpu[0],
773                        STATS_IRQCPU_SIZE * (cpu_nr + 1) * softirqcpu_nr);
774         }
775
776         pause();
777
778         do {
779                 /*
780                  * Resetting the structure not needed since every fields will be set.
781                  * Exceptions are per-CPU structures: Some of them may not be filled
782                  * if corresponding processor is disabled (offline). We set them to zero
783                  * to be able to distinguish between offline and tickless CPUs.
784                  */
785                 for (cpu = 1; cpu <= cpu_nr; cpu++) {
786                         scc = st_cpu[curr] + cpu;
787                         memset(scc, 0, STATS_CPU_SIZE);
788                 }
789
790                 /* Get time */
791                 get_localtime(&(mp_tstamp[curr]));
792
793                 /* Read stats */
794                 if (cpu_nr > 1) {
795                         uptime0[curr] = 0;
796                         read_uptime(&(uptime0[curr]));
797                 }
798                 read_stat_cpu(st_cpu[curr], cpu_nr + 1, &(uptime[curr]), &(uptime0[curr]));
799
800                 if (DISPLAY_IRQ_SUM(actflags)) {
801                         read_stat_irq(st_irq[curr], 1);
802                 }
803
804                 if (DISPLAY_IRQ_SUM(actflags) || DISPLAY_IRQ_CPU(actflags)) {
805                         read_interrupts_stat(INTERRUPTS, st_irqcpu, irqcpu_nr, curr);
806                 }
807
808                 if (DISPLAY_SOFTIRQS(actflags)) {
809                         read_interrupts_stat(SOFTIRQS, st_softirqcpu, softirqcpu_nr, curr);
810                 }
811
812                 /* Write stats */
813                 if (!dis_hdr) {
814                         dis = lines / rows;
815                         if (dis) {
816                                 lines %= rows;
817                         }
818                         lines++;
819                 }
820                 write_stats(curr, dis);
821
822                 if (count > 0) {
823                         count--;
824                 }
825                 if (count) {
826                         curr ^= 1;
827                         pause();
828                 }
829         }
830         while (count);
831
832         /* Write stats average */
833         write_stats_avg(curr, dis_hdr);
834 }
835
836 /*
837  ***************************************************************************
838  * Main entry to the program
839  ***************************************************************************
840  */
841 int main(int argc, char **argv)
842 {
843         int opt = 0, i, actset = FALSE;
844         struct utsname header;
845         int dis_hdr = -1;
846         int rows = 23;
847         char *t;
848
849 #ifdef USE_NLS
850         /* Init National Language Support */
851         init_nls();
852 #endif
853
854         /* Get HZ */
855         get_HZ();
856
857         /* How many processors on this machine ? */
858         cpu_nr = get_cpu_nr(~0);
859         
860         /* Calculate number of interrupts per processor */
861         irqcpu_nr = get_irqcpu_nr(INTERRUPTS, NR_IRQS, cpu_nr) +
862                     NR_IRQCPU_PREALLOC;
863         /* Calculate number of soft interrupts per processor */
864         softirqcpu_nr = get_irqcpu_nr(SOFTIRQS, NR_IRQS, cpu_nr) +
865                         NR_IRQCPU_PREALLOC;
866
867         /*
868          * cpu_nr: a value of 2 means there are 2 processors (0 and 1).
869          * In this case, we have to allocate 3 structures: global, proc0 and proc1.
870          */
871         salloc_mp_struct(cpu_nr + 1);
872
873         while (++opt < argc) {
874
875                 if (!strcmp(argv[opt], "-I")) {
876                         if (argv[++opt]) {
877                                 actset = TRUE;
878                                 if (!strcmp(argv[opt], K_SUM)) {
879                                         /* Display total number of interrupts per CPU */
880                                         actflags |= M_D_IRQ_SUM;
881                                 }
882                                 else if (!strcmp(argv[opt], K_CPU)) {
883                                         /* Display interrupts per CPU */
884                                         actflags |= M_D_IRQ_CPU;
885                                 }
886                                 else if (!strcmp(argv[opt], K_SCPU)) {
887                                         /* Display soft interrupts per CPU */
888                                         actflags |= M_D_SOFTIRQS;
889                                 }
890                                 else if (!strcmp(argv[opt], K_ALL)) {
891                                         actflags |= M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
892                                 }
893                                 else {
894                                         usage(argv[0]);
895                                 }
896                         }
897                         else {
898                                 usage(argv[0]);
899                         }
900                 }
901
902                 else if (!strcmp(argv[opt], "-P")) {
903                         /* '-P ALL' can be used on UP machines */
904                         if (argv[++opt]) {
905                                 flags |= F_P_OPTION;
906                                 dis_hdr++;
907                                 
908                                 for (t = strtok(argv[opt], ","); t; t = strtok(NULL, ",")) {
909                                         if (!strcmp(t, K_ALL) || !strcmp(t, K_ON)) {
910                                                 if (cpu_nr) {
911                                                         dis_hdr = 9;
912                                                 }
913                                                 /*
914                                                  * Set bit for every processor.
915                                                  * Also indicate to display stats for CPU 'all'.
916                                                  */
917                                                 memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
918                                                 if (!strcmp(t, K_ON)) {
919                                                         /* Display stats only for online CPU */
920                                                         flags |= F_P_ON;
921                                                 }
922                                         }
923                                         else {
924                                                 if (strspn(t, DIGITS) != strlen(t)) {
925                                                         usage(argv[0]);
926                                                 }
927                                                 i = atoi(t);    /* Get cpu number */
928                                                 if (i >= cpu_nr) {
929                                                         fprintf(stderr, _("Not that many processors!\n"));
930                                                         exit(1);
931                                                 }
932                                                 i++;
933                                                 *(cpu_bitmap + (i >> 3)) |= 1 << (i & 0x07);
934                                         }
935                                 }
936                         }
937                         else {
938                                 usage(argv[0]);
939                         }
940                 }
941
942                 else if (!strncmp(argv[opt], "-", 1)) {
943                         for (i = 1; *(argv[opt] + i); i++) {
944
945                                 switch (*(argv[opt] + i)) {
946
947                                 case 'A':
948                                         actflags |= M_D_CPU + M_D_IRQ_SUM + M_D_IRQ_CPU + M_D_SOFTIRQS;
949                                         actset = TRUE;
950                                         /* Select all processors */
951                                         flags |= F_P_OPTION;
952                                         memset(cpu_bitmap, 0xff, ((cpu_nr + 1) >> 3) + 1);
953                                         break;
954                                         
955                                 case 'u':
956                                         /* Display CPU */
957                                         actflags |= M_D_CPU;
958                                         break;
959
960                                 case 'V':
961                                         /* Print version number */
962                                         print_version();
963                                         break;
964
965                                 default:
966                                         usage(argv[0]);
967                                 }
968                         }
969                 }
970
971                 else if (interval < 0) {
972                         /* Get interval */
973                         if (strspn(argv[opt], DIGITS) != strlen(argv[opt])) {
974                                 usage(argv[0]);
975                         }
976                         interval = atol(argv[opt]);
977                         if (interval < 0) {
978                                 usage(argv[0]);
979                         }
980                         count = -1;
981                 }
982
983                 else if (count <= 0) {
984                         /* Get count value */
985                         if ((strspn(argv[opt], DIGITS) != strlen(argv[opt])) ||
986                             !interval) {
987                                 usage(argv[0]);
988                         }
989                         count = atol(argv[opt]);
990                         if (count < 1) {
991                                 usage(argv[0]);
992                         }
993                 }
994
995                 else {
996                         usage(argv[0]);
997                 }
998         }
999
1000         /* Default: Display CPU */
1001         if (!actset) {
1002                 actflags |= M_D_CPU;
1003         }
1004
1005         if (count_bits(&actflags, sizeof(unsigned int)) > 1) {
1006                 dis_hdr = 9;
1007         }
1008         
1009         if (!USE_P_OPTION(flags)) {
1010                 /* Option -P not used: Set bit 0 (global stats among all proc) */
1011                 *cpu_bitmap = 1;
1012         }
1013         if (dis_hdr < 0) {
1014                 dis_hdr = 0;
1015         }
1016         if (!dis_hdr) {
1017                 /* Get window size */
1018                 rows = get_win_height();
1019         }
1020         if (interval < 0) {
1021                 /* Interval not set => display stats since boot time */
1022                 interval = 0;
1023         }
1024
1025         /* Get time */
1026         get_localtime(&(mp_tstamp[0]));
1027
1028         /* Get system name, release number and hostname */
1029         uname(&header);
1030         print_gal_header(&(mp_tstamp[0]), header.sysname, header.release,
1031                          header.nodename, header.machine, cpu_nr);
1032
1033         /* Main loop */
1034         rw_mpstat_loop(dis_hdr, rows);
1035
1036         /* Free structures */
1037         sfree_mp_struct();
1038
1039         return 0;
1040 }