blob: acaff4dc0453e55d0fb9bdc247d8cddb50bd0f2d [file] [log] [blame]
Marek Polacek23e8c082010-07-21 10:29:07 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Per-processor statistics, based on sysstat version 9.1.2 by Sebastien Godard
4 *
5 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Marek Polacek23e8c082010-07-21 10:29:07 +02008 */
9
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010010//applet:IF_MPSTAT(APPLET(mpstat, BB_DIR_BIN, BB_SUID_DROP))
Denys Vlasenkoa759b222017-08-06 14:15:24 +020011/* shouldn't be noexec: "mpstat INTERVAL" runs indefinitely */
Marek Polacek23e8c082010-07-21 10:29:07 +020012
13//kbuild:lib-$(CONFIG_MPSTAT) += mpstat.o
14
15//config:config MPSTAT
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020016//config: bool "mpstat (10 kb)"
Marek Polacek23e8c082010-07-21 10:29:07 +020017//config: default y
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: Per-processor statistics
Marek Polacek23e8c082010-07-21 10:29:07 +020020
21#include "libbb.h"
Denys Vlasenkofb132e42010-10-29 11:46:52 +020022#include <sys/utsname.h> /* struct utsname */
Marek Polacek23e8c082010-07-21 10:29:07 +020023
24//#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
25#define debug(fmt, ...) ((void)0)
26
27/* Size of /proc/interrupts line, CPU data excluded */
28#define INTERRUPTS_LINE 64
29/* Maximum number of interrupts */
30#define NR_IRQS 256
31#define NR_IRQCPU_PREALLOC 3
Denys Vlasenko64c67892010-07-30 12:45:14 +020032#define MAX_IRQNAME_LEN 16
Marek Polacek23e8c082010-07-21 10:29:07 +020033#define MAX_PF_NAME 512
Denys Vlasenko64c67892010-07-30 12:45:14 +020034/* sysstat 9.0.6 uses width 8, but newer code which also prints /proc/softirqs
35 * data needs more: "interrupts" in /proc/softirqs have longer names,
36 * most are up to 8 chars, one (BLOCK_IOPOLL) is even longer.
37 * We are printing headers in the " IRQNAME/s" form, experimentally
38 * anything smaller than 10 chars looks ugly for /proc/softirqs stats.
39 */
Marek Polacek2a6d5982011-04-30 22:09:01 +020040#define INTRATE_SCRWIDTH 10
Denys Vlasenko64c67892010-07-30 12:45:14 +020041#define INTRATE_SCRWIDTH_STR "10"
Marek Polacek23e8c082010-07-21 10:29:07 +020042
43/* System files */
Marek Polacek23e8c082010-07-21 10:29:07 +020044#define PROCFS_STAT "/proc/stat"
45#define PROCFS_INTERRUPTS "/proc/interrupts"
46#define PROCFS_SOFTIRQS "/proc/softirqs"
47#define PROCFS_UPTIME "/proc/uptime"
48
49
50#if 1
51typedef unsigned long long data_t;
52typedef long long idata_t;
53#define FMT_DATA "ll"
54#define DATA_MAX ULLONG_MAX
55#else
56typedef unsigned long data_t;
57typedef long idata_t;
58#define FMT_DATA "l"
59#define DATA_MAX ULONG_MAX
60#endif
61
62
63struct stats_irqcpu {
Dan Fandrich159677c2010-08-23 22:23:04 -070064 unsigned interrupts;
Denys Vlasenko64c67892010-07-30 12:45:14 +020065 char irq_name[MAX_IRQNAME_LEN];
Marek Polacek23e8c082010-07-21 10:29:07 +020066};
67
Marek Polacek23e8c082010-07-21 10:29:07 +020068struct stats_cpu {
69 data_t cpu_user;
70 data_t cpu_nice;
71 data_t cpu_system;
72 data_t cpu_idle;
73 data_t cpu_iowait;
74 data_t cpu_steal;
75 data_t cpu_irq;
76 data_t cpu_softirq;
77 data_t cpu_guest;
78};
79
Marek Polacek23e8c082010-07-21 10:29:07 +020080struct stats_irq {
81 data_t irq_nr;
82};
83
84
Denys Vlasenko217df6e2010-07-21 11:54:33 +020085/* Globals. Sort by size and access frequency. */
Marek Polacek23e8c082010-07-21 10:29:07 +020086struct globals {
87 int interval;
88 int count;
89 unsigned cpu_nr; /* Number of CPUs */
90 unsigned irqcpu_nr; /* Number of interrupts per CPU */
91 unsigned softirqcpu_nr; /* Number of soft interrupts per CPU */
92 unsigned options;
93 unsigned hz;
94 unsigned cpu_bitmap_len;
95 smallint p_option;
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +020096 // 9.0.6 does not do it. Try "mpstat -A 1 2" - headers are repeated!
97 //smallint header_done;
98 //smallint avg_header_done;
Marek Polacek23e8c082010-07-21 10:29:07 +020099 unsigned char *cpu_bitmap; /* Bit 0: global, bit 1: 1st proc... */
100 data_t global_uptime[3];
101 data_t per_cpu_uptime[3];
102 struct stats_cpu *st_cpu[3];
103 struct stats_irq *st_irq[3];
104 struct stats_irqcpu *st_irqcpu[3];
105 struct stats_irqcpu *st_softirqcpu[3];
106 struct tm timestamp[3];
107};
108#define G (*ptr_to_globals)
109#define INIT_G() do { \
110 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
111} while (0)
112
113/* The selected interrupts statistics (bits in G.options) */
114enum {
115 D_CPU = 1 << 0,
116 D_IRQ_SUM = 1 << 1,
117 D_IRQ_CPU = 1 << 2,
118 D_SOFTIRQS = 1 << 3,
119};
120
121
Marek Polacek23e8c082010-07-21 10:29:07 +0200122/* Is option on? */
123static ALWAYS_INLINE int display_opt(int opt)
124{
125 return (opt & G.options);
126}
127
128#if DATA_MAX > 0xffffffff
129/*
130 * Handle overflow conditions properly for counters which can have
131 * less bits than data_t, depending on the kernel version.
132 */
133/* Surprisingly, on 32bit inlining is a size win */
134static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
135{
136 data_t v = curr - prev;
137
138 if ((idata_t)v < 0 /* curr < prev - counter overflow? */
139 && prev <= 0xffffffff /* kernel uses 32bit value for the counter? */
140 ) {
141 /* Add 33th bit set to 1 to curr, compensating for the overflow */
142 /* double shift defeats "warning: left shift count >= width of type" */
143 v += ((data_t)1 << 16) << 16;
144 }
145 return v;
146}
147#else
148static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
149{
150 return curr - prev;
151}
152#endif
153
154static double percent_value(data_t prev, data_t curr, data_t itv)
155{
156 return ((double)overflow_safe_sub(prev, curr)) / itv * 100;
157}
158
159static double hz_value(data_t prev, data_t curr, data_t itv)
160{
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200161 //bb_error_msg("curr:%lld prev:%lld G.hz:%u", curr, prev, G.hz);
Marek Polacek23e8c082010-07-21 10:29:07 +0200162 return ((double)overflow_safe_sub(prev, curr)) / itv * G.hz;
163}
164
165static ALWAYS_INLINE data_t jiffies_diff(data_t old, data_t new)
166{
167 data_t diff = new - old;
168 return (diff == 0) ? 1 : diff;
169}
170
171static int is_cpu_in_bitmap(unsigned cpu)
172{
173 return G.cpu_bitmap[cpu >> 3] & (1 << (cpu & 7));
174}
175
176static void write_irqcpu_stats(struct stats_irqcpu *per_cpu_stats[],
177 int total_irqs,
178 data_t itv,
179 int prev, int current,
180 const char *prev_str, const char *current_str)
181{
182 int j;
183 int offset, cpu;
184 struct stats_irqcpu *p0, *q0;
185
186 /* Check if number of IRQs has changed */
187 if (G.interval != 0) {
188 for (j = 0; j <= total_irqs; j++) {
189 p0 = &per_cpu_stats[current][j];
190 if (p0->irq_name[0] != '\0') {
191 q0 = &per_cpu_stats[prev][j];
192 if (strcmp(p0->irq_name, q0->irq_name) != 0) {
193 /* Strings are different */
194 break;
195 }
196 }
197 }
198 }
199
200 /* Print header */
201 printf("\n%-11s CPU", prev_str);
Denys Vlasenko64c67892010-07-30 12:45:14 +0200202 {
203 /* A bit complex code to "buy back" space if one header is too wide.
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200204 * Here's how it looks like. BLOCK_IOPOLL eats too much space,
Denys Vlasenko64c67892010-07-30 12:45:14 +0200205 * and latter headers use smaller width to compensate:
206 * ...BLOCK/s BLOCK_IOPOLL/s TASKLET/s SCHED/s HRTIMER/s RCU/s
207 * ... 2.32 0.00 0.01 17.58 0.14 141.96
208 */
209 int expected_len = 0;
210 int printed_len = 0;
211 for (j = 0; j < total_irqs; j++) {
212 p0 = &per_cpu_stats[current][j];
213 if (p0->irq_name[0] != '\0') {
214 int n = (INTRATE_SCRWIDTH-3) - (printed_len - expected_len);
215 printed_len += printf(" %*s/s", n > 0 ? n : 0, skip_whitespace(p0->irq_name));
216 expected_len += INTRATE_SCRWIDTH;
217 }
218 }
Marek Polacek23e8c082010-07-21 10:29:07 +0200219 }
220 bb_putchar('\n');
221
222 for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
223 /* Check if we want stats about this CPU */
224 if (!is_cpu_in_bitmap(cpu) && G.p_option) {
225 continue;
226 }
227
228 printf("%-11s %4u", current_str, cpu - 1);
229
230 for (j = 0; j < total_irqs; j++) {
231 /* IRQ field set only for proc 0 */
232 p0 = &per_cpu_stats[current][j];
233
234 /*
235 * An empty string for irq name means that
236 * interrupt is no longer used.
237 */
238 if (p0->irq_name[0] != '\0') {
239 offset = j;
240 q0 = &per_cpu_stats[prev][offset];
241
242 /*
243 * If we want stats for the time since boot
244 * we have p0->irq != q0->irq.
245 */
246 if (strcmp(p0->irq_name, q0->irq_name) != 0
247 && G.interval != 0
248 ) {
249 if (j) {
250 offset = j - 1;
251 q0 = &per_cpu_stats[prev][offset];
252 }
253 if (strcmp(p0->irq_name, q0->irq_name) != 0
254 && (j + 1 < total_irqs)
255 ) {
256 offset = j + 1;
257 q0 = &per_cpu_stats[prev][offset];
258 }
259 }
260
261 if (strcmp(p0->irq_name, q0->irq_name) == 0
262 || G.interval == 0
263 ) {
264 struct stats_irqcpu *p, *q;
265 p = &per_cpu_stats[current][(cpu - 1) * total_irqs + j];
266 q = &per_cpu_stats[prev][(cpu - 1) * total_irqs + offset];
Denys Vlasenko64c67892010-07-30 12:45:14 +0200267 printf("%"INTRATE_SCRWIDTH_STR".2f",
Dan Fandrich159677c2010-08-23 22:23:04 -0700268 (double)(p->interrupts - q->interrupts) / itv * G.hz);
Marek Polacek23e8c082010-07-21 10:29:07 +0200269 } else {
270 printf(" N/A");
271 }
272 }
273 }
274 bb_putchar('\n');
275 }
276}
277
278static data_t get_per_cpu_interval(const struct stats_cpu *scc,
279 const struct stats_cpu *scp)
280{
281 return ((scc->cpu_user + scc->cpu_nice +
282 scc->cpu_system + scc->cpu_iowait +
283 scc->cpu_idle + scc->cpu_steal +
284 scc->cpu_irq + scc->cpu_softirq) -
285 (scp->cpu_user + scp->cpu_nice +
286 scp->cpu_system + scp->cpu_iowait +
287 scp->cpu_idle + scp->cpu_steal +
288 scp->cpu_irq + scp->cpu_softirq));
289}
290
291static void print_stats_cpu_struct(const struct stats_cpu *p,
292 const struct stats_cpu *c,
293 data_t itv)
294{
295 printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
296 percent_value(p->cpu_user - p->cpu_guest,
297 /**/ c->cpu_user - c->cpu_guest, itv),
298 percent_value(p->cpu_nice , c->cpu_nice , itv),
299 percent_value(p->cpu_system , c->cpu_system , itv),
300 percent_value(p->cpu_iowait , c->cpu_iowait , itv),
301 percent_value(p->cpu_irq , c->cpu_irq , itv),
302 percent_value(p->cpu_softirq, c->cpu_softirq, itv),
303 percent_value(p->cpu_steal , c->cpu_steal , itv),
304 percent_value(p->cpu_guest , c->cpu_guest , itv),
305 percent_value(p->cpu_idle , c->cpu_idle , itv)
306 );
307}
308
309static void write_stats_core(int prev, int current,
310 const char *prev_str, const char *current_str)
311{
312 struct stats_cpu *scc, *scp;
313 data_t itv, global_itv;
314 int cpu;
315
316 /* Compute time interval */
317 itv = global_itv = jiffies_diff(G.global_uptime[prev], G.global_uptime[current]);
318
319 /* Reduce interval to one CPU */
320 if (G.cpu_nr > 1)
321 itv = jiffies_diff(G.per_cpu_uptime[prev], G.per_cpu_uptime[current]);
322
323 /* Print CPU stats */
324 if (display_opt(D_CPU)) {
325
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200326 ///* This is done exactly once */
327 //if (!G.header_done) {
Marek Polacek23e8c082010-07-21 10:29:07 +0200328 printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq %%soft %%steal %%guest %%idle\n",
329 prev_str
330 );
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200331 // G.header_done = 1;
332 //}
Marek Polacek23e8c082010-07-21 10:29:07 +0200333
334 for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
335 data_t per_cpu_itv;
336
337 /* Print stats about this particular CPU? */
338 if (!is_cpu_in_bitmap(cpu))
339 continue;
340
341 scc = &G.st_cpu[current][cpu];
342 scp = &G.st_cpu[prev][cpu];
343 per_cpu_itv = global_itv;
344
345 printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
346 if (cpu) {
347 double idle;
348 /*
349 * If the CPU is offline, then it isn't in /proc/stat,
350 * so all values are 0.
351 * NB: Guest time is already included in user time.
352 */
353 if ((scc->cpu_user | scc->cpu_nice | scc->cpu_system |
354 scc->cpu_iowait | scc->cpu_idle | scc->cpu_steal |
355 scc->cpu_irq | scc->cpu_softirq) == 0
356 ) {
357 /*
358 * Set current struct fields to values from prev.
359 * iteration. Then their values won't jump from
360 * zero, when the CPU comes back online.
361 */
362 *scc = *scp;
363 idle = 0.0;
364 goto print_zeros;
365 }
366 /* Compute interval again for current proc */
367 per_cpu_itv = get_per_cpu_interval(scc, scp);
368 if (per_cpu_itv == 0) {
369 /*
370 * If the CPU is tickless then there is no change in CPU values
371 * but the sum of values is not zero.
372 */
373 idle = 100.0;
374 print_zeros:
375 printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
376 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, idle);
377 continue;
378 }
379 }
380 print_stats_cpu_struct(scp, scc, per_cpu_itv);
381 }
382 }
383
384 /* Print total number of IRQs per CPU */
385 if (display_opt(D_IRQ_SUM)) {
386
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200387 ///* Print average header, this is done exactly once */
388 //if (!G.avg_header_done) {
Marek Polacek23e8c082010-07-21 10:29:07 +0200389 printf("\n%-11s CPU intr/s\n", prev_str);
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200390 // G.avg_header_done = 1;
391 //}
Marek Polacek23e8c082010-07-21 10:29:07 +0200392
393 for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
394 data_t per_cpu_itv;
395
396 /* Print stats about this CPU? */
397 if (!is_cpu_in_bitmap(cpu))
398 continue;
399
400 per_cpu_itv = itv;
401 printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
402 if (cpu) {
403 scc = &G.st_cpu[current][cpu];
404 scp = &G.st_cpu[prev][cpu];
405 /* Compute interval again for current proc */
406 per_cpu_itv = get_per_cpu_interval(scc, scp);
407 if (per_cpu_itv == 0) {
408 printf(" %9.2f\n", 0.0);
409 continue;
410 }
411 }
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200412 //bb_error_msg("G.st_irq[%u][%u].irq_nr:%lld - G.st_irq[%u][%u].irq_nr:%lld",
413 // current, cpu, G.st_irq[prev][cpu].irq_nr, prev, cpu, G.st_irq[current][cpu].irq_nr);
Marek Polacek23e8c082010-07-21 10:29:07 +0200414 printf(" %9.2f\n", hz_value(G.st_irq[prev][cpu].irq_nr, G.st_irq[current][cpu].irq_nr, per_cpu_itv));
415 }
416 }
417
418 if (display_opt(D_IRQ_CPU)) {
419 write_irqcpu_stats(G.st_irqcpu, G.irqcpu_nr,
420 itv,
421 prev, current,
422 prev_str, current_str
423 );
424 }
425
426 if (display_opt(D_SOFTIRQS)) {
427 write_irqcpu_stats(G.st_softirqcpu, G.softirqcpu_nr,
428 itv,
429 prev, current,
430 prev_str, current_str
431 );
432 }
433}
434
435/*
436 * Print the statistics
437 */
438static void write_stats(int current)
439{
440 char prev_time[16];
441 char curr_time[16];
442
443 strftime(prev_time, sizeof(prev_time), "%X", &G.timestamp[!current]);
444 strftime(curr_time, sizeof(curr_time), "%X", &G.timestamp[current]);
445
446 write_stats_core(!current, current, prev_time, curr_time);
447}
448
449static void write_stats_avg(int current)
450{
451 write_stats_core(2, current, "Average:", "Average:");
452}
453
454/*
455 * Read CPU statistics
456 */
457static void get_cpu_statistics(struct stats_cpu *cpu, data_t *up, data_t *up0)
458{
459 FILE *fp;
460 char buf[1024];
461
462 fp = xfopen_for_read(PROCFS_STAT);
463
464 while (fgets(buf, sizeof(buf), fp)) {
465 data_t sum;
466 unsigned cpu_number;
467 struct stats_cpu *cp;
468
469 if (!starts_with_cpu(buf))
470 continue; /* not "cpu" */
Denys Vlasenko217df6e2010-07-21 11:54:33 +0200471
472 cp = cpu; /* for "cpu " case */
473 if (buf[3] != ' ') {
474 /* "cpuN " */
Marek Polacek23e8c082010-07-21 10:29:07 +0200475 if (G.cpu_nr == 0
476 || sscanf(buf + 3, "%u ", &cpu_number) != 1
477 || cpu_number >= G.cpu_nr
478 ) {
479 continue;
480 }
481 cp = &cpu[cpu_number + 1];
482 }
483
Denys Vlasenko217df6e2010-07-21 11:54:33 +0200484 /* Read the counters, save them */
Marek Polacek23e8c082010-07-21 10:29:07 +0200485 /* Not all fields have to be present */
486 memset(cp, 0, sizeof(*cp));
Denys Vlasenko217df6e2010-07-21 11:54:33 +0200487 sscanf(buf, "%*s"
Marek Polacek23e8c082010-07-21 10:29:07 +0200488 " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
489 " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
490 " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u",
491 &cp->cpu_user, &cp->cpu_nice, &cp->cpu_system,
492 &cp->cpu_idle, &cp->cpu_iowait, &cp->cpu_irq,
Denys Vlasenko217df6e2010-07-21 11:54:33 +0200493 &cp->cpu_softirq, &cp->cpu_steal, &cp->cpu_guest
Marek Polacek23e8c082010-07-21 10:29:07 +0200494 );
495 /*
496 * Compute uptime in jiffies (1/HZ), it'll be the sum of
497 * individual CPU's uptimes.
498 * NB: We have to omit cpu_guest, because cpu_user includes it.
499 */
500 sum = cp->cpu_user + cp->cpu_nice + cp->cpu_system +
501 cp->cpu_idle + cp->cpu_iowait + cp->cpu_irq +
502 cp->cpu_softirq + cp->cpu_steal;
503
504 if (buf[3] == ' ') {
505 /* "cpu " */
506 *up = sum;
507 } else {
Denys Vlasenko217df6e2010-07-21 11:54:33 +0200508 /* "cpuN " */
Marek Polacek23e8c082010-07-21 10:29:07 +0200509 if (cpu_number == 0 && *up0 != 0) {
510 /* Compute uptime of single CPU */
511 *up0 = sum;
512 }
513 }
514 }
515 fclose(fp);
516}
517
518/*
519 * Read IRQs from /proc/stat
520 */
521static void get_irqs_from_stat(struct stats_irq *irq)
522{
523 FILE *fp;
524 char buf[1024];
525
Denys Vlasenko1ec49732015-01-05 18:39:23 +0100526 fp = xfopen_for_read(PROCFS_STAT);
Marek Polacek23e8c082010-07-21 10:29:07 +0200527
528 while (fgets(buf, sizeof(buf), fp)) {
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200529 //bb_error_msg("/proc/stat:'%s'", buf);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100530 if (is_prefixed_with(buf, "intr ")) {
Marek Polacek23e8c082010-07-21 10:29:07 +0200531 /* Read total number of IRQs since system boot */
532 sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200533 }
Marek Polacek23e8c082010-07-21 10:29:07 +0200534 }
535
536 fclose(fp);
537}
538
539/*
540 * Read stats from /proc/interrupts or /proc/softirqs
541 */
542static void get_irqs_from_interrupts(const char *fname,
543 struct stats_irqcpu *per_cpu_stats[],
544 int irqs_per_cpu, int current)
545{
546 FILE *fp;
547 struct stats_irq *irq_i;
548 struct stats_irqcpu *ic;
549 char *buf;
550 unsigned buflen;
551 unsigned cpu;
552 unsigned irq;
553 int cpu_index[G.cpu_nr];
554 int iindex;
Marek Polacek23e8c082010-07-21 10:29:07 +0200555
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200556// Moved to caller.
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200557// Otherwise reading of /proc/softirqs
558// was resetting counts to 0 after we painstakingly collected them from
559// /proc/interrupts. Which resulted in:
560// 01:32:47 PM CPU intr/s
561// 01:32:47 PM all 591.47
562// 01:32:47 PM 0 0.00 <= ???
563// 01:32:47 PM 1 0.00 <= ???
564// for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
565// G.st_irq[current][cpu].irq_nr = 0;
566// //bb_error_msg("G.st_irq[%u][%u].irq_nr=0", current, cpu);
567// }
Marek Polacek23e8c082010-07-21 10:29:07 +0200568
569 fp = fopen_for_read(fname);
570 if (!fp)
571 return;
572
573 buflen = INTERRUPTS_LINE + 16 * G.cpu_nr;
574 buf = xmalloc(buflen);
575
576 /* Parse header and determine, which CPUs are online */
577 iindex = 0;
578 while (fgets(buf, buflen, fp)) {
579 char *cp, *next;
580 next = buf;
581 while ((cp = strstr(next, "CPU")) != NULL
582 && iindex < G.cpu_nr
583 ) {
584 cpu = strtoul(cp + 3, &next, 10);
585 cpu_index[iindex++] = cpu;
586 }
587 if (iindex) /* We found header */
588 break;
589 }
590
591 irq = 0;
592 while (fgets(buf, buflen, fp)
593 && irq < irqs_per_cpu
594 ) {
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200595 int len;
596 char last_char;
Marek Polacek23e8c082010-07-21 10:29:07 +0200597 char *cp;
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200598
599 /* Skip over "IRQNAME:" */
Marek Polacek23e8c082010-07-21 10:29:07 +0200600 cp = strchr(buf, ':');
601 if (!cp)
602 continue;
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200603 last_char = cp[-1];
Marek Polacek23e8c082010-07-21 10:29:07 +0200604
605 ic = &per_cpu_stats[current][irq];
606 len = cp - buf;
Denys Vlasenko64c67892010-07-30 12:45:14 +0200607 if (len >= sizeof(ic->irq_name)) {
608 len = sizeof(ic->irq_name) - 1;
Marek Polacek23e8c082010-07-21 10:29:07 +0200609 }
Denys Vlasenko64c67892010-07-30 12:45:14 +0200610 safe_strncpy(ic->irq_name, buf, len + 1);
611 //bb_error_msg("%s: irq%d:'%s' buf:'%s'", fname, irq, ic->irq_name, buf);
Marek Polacek23e8c082010-07-21 10:29:07 +0200612 cp++;
613
614 for (cpu = 0; cpu < iindex; cpu++) {
615 char *next;
616 ic = &per_cpu_stats[current][cpu_index[cpu] * irqs_per_cpu + irq];
617 irq_i = &G.st_irq[current][cpu_index[cpu] + 1];
Dan Fandrich159677c2010-08-23 22:23:04 -0700618 ic->interrupts = strtoul(cp, &next, 10);
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200619 /* Count only numerical IRQs */
620 if (isdigit(last_char)) {
Dan Fandrich159677c2010-08-23 22:23:04 -0700621 irq_i->irq_nr += ic->interrupts;
Denys Vlasenko9bb05102010-07-30 13:38:46 +0200622 //bb_error_msg("G.st_irq[%u][%u].irq_nr + %u = %lld",
Dan Fandrich159677c2010-08-23 22:23:04 -0700623 // current, cpu_index[cpu] + 1, ic->interrupts, irq_i->irq_nr);
Marek Polacek23e8c082010-07-21 10:29:07 +0200624 }
625 cp = next;
626 }
627 irq++;
628 }
629 fclose(fp);
630 free(buf);
631
632 while (irq < irqs_per_cpu) {
633 /* Number of interrupts per CPU has changed */
634 ic = &per_cpu_stats[current][irq];
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200635 ic->irq_name[0] = '\0'; /* False interrupt */
Marek Polacek23e8c082010-07-21 10:29:07 +0200636 irq++;
637 }
638}
639
640static void get_uptime(data_t *uptime)
641{
642 FILE *fp;
643 char buf[sizeof(long)*3 * 2 + 4]; /* enough for long.long */
644 unsigned long uptime_sec, decimal;
645
Denys Vlasenko1ec49732015-01-05 18:39:23 +0100646 fp = xfopen_for_read(PROCFS_UPTIME);
Marek Polacek23e8c082010-07-21 10:29:07 +0200647 if (fgets(buf, sizeof(buf), fp)) {
648 if (sscanf(buf, "%lu.%lu", &uptime_sec, &decimal) == 2) {
649 *uptime = (data_t)uptime_sec * G.hz + decimal * G.hz / 100;
650 }
651 }
652
653 fclose(fp);
654}
655
656static void get_localtime(struct tm *tm)
657{
658 time_t timer;
659 time(&timer);
660 localtime_r(&timer, tm);
661}
662
663static void alarm_handler(int sig UNUSED_PARAM)
664{
665 signal(SIGALRM, alarm_handler);
666 alarm(G.interval);
667}
668
669static void main_loop(void)
670{
671 unsigned current;
672 unsigned cpus;
673
674 /* Read the stats */
675 if (G.cpu_nr > 1) {
676 G.per_cpu_uptime[0] = 0;
677 get_uptime(&G.per_cpu_uptime[0]);
678 }
679
680 get_cpu_statistics(G.st_cpu[0], &G.global_uptime[0], &G.per_cpu_uptime[0]);
681
682 if (display_opt(D_IRQ_SUM))
683 get_irqs_from_stat(G.st_irq[0]);
684
685 if (display_opt(D_IRQ_SUM | D_IRQ_CPU))
686 get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
687 G.irqcpu_nr, 0);
688
689 if (display_opt(D_SOFTIRQS))
690 get_irqs_from_interrupts(PROCFS_SOFTIRQS, G.st_softirqcpu,
691 G.softirqcpu_nr, 0);
692
693 if (G.interval == 0) {
694 /* Display since boot time */
695 cpus = G.cpu_nr + 1;
696 G.timestamp[1] = G.timestamp[0];
697 memset(G.st_cpu[1], 0, sizeof(G.st_cpu[1][0]) * cpus);
698 memset(G.st_irq[1], 0, sizeof(G.st_irq[1][0]) * cpus);
699 memset(G.st_irqcpu[1], 0, sizeof(G.st_irqcpu[1][0]) * cpus * G.irqcpu_nr);
700 memset(G.st_softirqcpu[1], 0, sizeof(G.st_softirqcpu[1][0]) * cpus * G.softirqcpu_nr);
701
702 write_stats(0);
703
704 /* And we're done */
705 return;
706 }
707
708 /* Set a handler for SIGALRM */
709 alarm_handler(0);
710
711 /* Save the stats we already have. We need them to compute the average */
712 G.timestamp[2] = G.timestamp[0];
713 G.global_uptime[2] = G.global_uptime[0];
714 G.per_cpu_uptime[2] = G.per_cpu_uptime[0];
715 cpus = G.cpu_nr + 1;
716 memcpy(G.st_cpu[2], G.st_cpu[0], sizeof(G.st_cpu[0][0]) * cpus);
717 memcpy(G.st_irq[2], G.st_irq[0], sizeof(G.st_irq[0][0]) * cpus);
718 memcpy(G.st_irqcpu[2], G.st_irqcpu[0], sizeof(G.st_irqcpu[0][0]) * cpus * G.irqcpu_nr);
719 if (display_opt(D_SOFTIRQS)) {
720 memcpy(G.st_softirqcpu[2], G.st_softirqcpu[0],
721 sizeof(G.st_softirqcpu[0][0]) * cpus * G.softirqcpu_nr);
722 }
723
724 current = 1;
725 while (1) {
726 /* Suspend until a signal is received */
727 pause();
728
729 /* Set structures to 0 to distinguish off/online CPUs */
730 memset(&G.st_cpu[current][/*cpu:*/ 1], 0, sizeof(G.st_cpu[0][0]) * G.cpu_nr);
731
732 get_localtime(&G.timestamp[current]);
733
734 /* Read stats */
735 if (G.cpu_nr > 1) {
736 G.per_cpu_uptime[current] = 0;
737 get_uptime(&G.per_cpu_uptime[current]);
738 }
739 get_cpu_statistics(G.st_cpu[current], &G.global_uptime[current], &G.per_cpu_uptime[current]);
740
741 if (display_opt(D_IRQ_SUM))
742 get_irqs_from_stat(G.st_irq[current]);
743
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200744 if (display_opt(D_IRQ_SUM | D_IRQ_CPU)) {
745 int cpu;
746 for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
747 G.st_irq[current][cpu].irq_nr = 0;
748 }
749 /* accumulates .irq_nr */
Marek Polacek23e8c082010-07-21 10:29:07 +0200750 get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
751 G.irqcpu_nr, current);
Denys Vlasenko3fb4a5e2010-07-30 13:55:35 +0200752 }
Marek Polacek23e8c082010-07-21 10:29:07 +0200753
754 if (display_opt(D_SOFTIRQS))
755 get_irqs_from_interrupts(PROCFS_SOFTIRQS,
756 G.st_softirqcpu,
757 G.softirqcpu_nr, current);
758
759 write_stats(current);
760
761 if (G.count > 0) {
762 if (--G.count == 0)
763 break;
764 }
765
766 current ^= 1;
767 }
768
769 /* Print average statistics */
770 write_stats_avg(current);
771}
772
773/* Initialization */
774
Marek Polacek23e8c082010-07-21 10:29:07 +0200775static void alloc_struct(int cpus)
776{
777 int i;
778 for (i = 0; i < 3; i++) {
779 G.st_cpu[i] = xzalloc(sizeof(G.st_cpu[i][0]) * cpus);
780 G.st_irq[i] = xzalloc(sizeof(G.st_irq[i][0]) * cpus);
781 G.st_irqcpu[i] = xzalloc(sizeof(G.st_irqcpu[i][0]) * cpus * G.irqcpu_nr);
782 G.st_softirqcpu[i] = xzalloc(sizeof(G.st_softirqcpu[i][0]) * cpus * G.softirqcpu_nr);
783 }
784 G.cpu_bitmap_len = (cpus >> 3) + 1;
785 G.cpu_bitmap = xzalloc(G.cpu_bitmap_len);
786}
787
788static void print_header(struct tm *t)
789{
790 char cur_date[16];
791 struct utsname uts;
792
793 /* Get system name, release number and hostname */
794 uname(&uts);
795
796 strftime(cur_date, sizeof(cur_date), "%x", t);
797
Denys Vlasenko217df6e2010-07-21 11:54:33 +0200798 printf("%s %s (%s)\t%s\t_%s_\t(%u CPU)\n",
Marek Polacek23e8c082010-07-21 10:29:07 +0200799 uts.sysname, uts.release, uts.nodename, cur_date, uts.machine, G.cpu_nr);
800}
801
802/*
Marek Polacek23e8c082010-07-21 10:29:07 +0200803 * Get number of interrupts available per processor
804 */
805static int get_irqcpu_nr(const char *f, int max_irqs)
806{
807 FILE *fp;
808 char *line;
809 unsigned linelen;
810 unsigned irq;
811
812 fp = fopen_for_read(f);
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200813 if (!fp) /* No interrupts file */
Marek Polacek23e8c082010-07-21 10:29:07 +0200814 return 0;
815
816 linelen = INTERRUPTS_LINE + 16 * G.cpu_nr;
817 line = xmalloc(linelen);
818
819 irq = 0;
820 while (fgets(line, linelen, fp)
821 && irq < max_irqs
822 ) {
823 int p = strcspn(line, ":");
824 if ((p > 0) && (p < 16))
825 irq++;
826 }
827
828 fclose(fp);
829 free(line);
830
831 return irq;
832}
833
834//usage:#define mpstat_trivial_usage
835//usage: "[-A] [-I SUM|CPU|ALL|SCPU] [-u] [-P num|ALL] [INTERVAL [COUNT]]"
836//usage:#define mpstat_full_usage "\n\n"
837//usage: "Per-processor statistics\n"
Marek Polacek23e8c082010-07-21 10:29:07 +0200838//usage: "\n -A Same as -I ALL -u -P ALL"
839//usage: "\n -I SUM|CPU|ALL|SCPU Report interrupt statistics"
840//usage: "\n -P num|ALL Processor to monitor"
841//usage: "\n -u Report CPU utilization"
842
843int mpstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
844int mpstat_main(int UNUSED_PARAM argc, char **argv)
845{
846 char *opt_irq_fmt;
847 char *opt_set_cpu;
848 int i, opt;
849 enum {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200850 OPT_ALL = 1 << 0, /* -A */
851 OPT_INTS = 1 << 1, /* -I */
852 OPT_SETCPU = 1 << 2, /* -P */
853 OPT_UTIL = 1 << 3, /* -u */
Marek Polacek23e8c082010-07-21 10:29:07 +0200854 };
855
856 /* Dont buffer data if redirected to a pipe */
857 setbuf(stdout, NULL);
858
859 INIT_G();
860
861 G.interval = -1;
862
863 /* Get number of processors */
Denys Vlasenkoc9b97502010-08-16 02:49:21 +0200864 G.cpu_nr = get_cpu_count();
Marek Polacek23e8c082010-07-21 10:29:07 +0200865
866 /* Get number of clock ticks per sec */
Bartosz Golaszewski5d2e4092014-06-22 14:01:13 +0200867 G.hz = bb_clk_tck();
Marek Polacek23e8c082010-07-21 10:29:07 +0200868
869 /* Calculate number of interrupts per processor */
870 G.irqcpu_nr = get_irqcpu_nr(PROCFS_INTERRUPTS, NR_IRQS) + NR_IRQCPU_PREALLOC;
871
872 /* Calculate number of soft interrupts per processor */
873 G.softirqcpu_nr = get_irqcpu_nr(PROCFS_SOFTIRQS, NR_IRQS) + NR_IRQCPU_PREALLOC;
874
875 /* Allocate space for structures. + 1 for global structure. */
876 alloc_struct(G.cpu_nr + 1);
877
878 /* Parse and process arguments */
879 opt = getopt32(argv, "AI:P:u", &opt_irq_fmt, &opt_set_cpu);
880 argv += optind;
881
882 if (*argv) {
883 /* Get interval */
Denys Vlasenko77832482010-08-12 14:14:45 +0200884 G.interval = xatoi_positive(*argv);
Marek Polacek23e8c082010-07-21 10:29:07 +0200885 G.count = -1;
886 argv++;
887 if (*argv) {
888 /* Get count value */
889 if (G.interval == 0)
890 bb_show_usage();
Denys Vlasenko77832482010-08-12 14:14:45 +0200891 G.count = xatoi_positive(*argv);
Marek Polacek23e8c082010-07-21 10:29:07 +0200892 //if (*++argv)
893 // bb_show_usage();
894 }
895 }
896 if (G.interval < 0)
897 G.interval = 0;
898
899 if (opt & OPT_ALL) {
900 G.p_option = 1;
901 G.options |= D_CPU + D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS;
902 /* Select every CPU */
903 memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
904 }
905
906 if (opt & OPT_INTS) {
Maksym Kryzhanovskyy5e87c2e2010-07-30 03:56:02 +0200907 static const char v[] = {
908 D_IRQ_CPU, D_IRQ_SUM, D_SOFTIRQS,
909 D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS
910 };
911 i = index_in_strings("CPU\0SUM\0SCPU\0ALL\0", opt_irq_fmt);
912 if (i == -1)
Marek Polacek23e8c082010-07-21 10:29:07 +0200913 bb_show_usage();
Maksym Kryzhanovskyy5e87c2e2010-07-30 03:56:02 +0200914 G.options |= v[i];
Marek Polacek23e8c082010-07-21 10:29:07 +0200915 }
916
917 if ((opt & OPT_UTIL) /* -u? */
918 || G.options == 0 /* nothing? (use default then) */
919 ) {
920 G.options |= D_CPU;
921 }
922
923 if (opt & OPT_SETCPU) {
924 char *t;
925 G.p_option = 1;
926
927 for (t = strtok(opt_set_cpu, ","); t; t = strtok(NULL, ",")) {
928 if (strcmp(t, "ALL") == 0) {
929 /* Select every CPU */
930 memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
931 } else {
932 /* Get CPU number */
Denys Vlasenko77832482010-08-12 14:14:45 +0200933 unsigned n = xatoi_positive(t);
Marek Polacek23e8c082010-07-21 10:29:07 +0200934 if (n >= G.cpu_nr)
935 bb_error_msg_and_die("not that many processors");
936 n++;
937 G.cpu_bitmap[n >> 3] |= 1 << (n & 7);
938 }
939 }
940 }
941
942 if (!G.p_option)
943 /* Display global stats */
944 G.cpu_bitmap[0] = 1;
945
946 /* Get time */
947 get_localtime(&G.timestamp[0]);
948
949 /* Display header */
950 print_header(&G.timestamp[0]);
951
952 /* The main loop */
953 main_loop();
954
955 if (ENABLE_FEATURE_CLEAN_UP) {
956 /* Clean up */
957 for (i = 0; i < 3; i++) {
958 free(G.st_cpu[i]);
959 free(G.st_irq[i]);
960 free(G.st_irqcpu[i]);
961 free(G.st_softirqcpu[i]);
962 }
963 free(G.cpu_bitmap);
964 free(&G);
965 }
966
967 return EXIT_SUCCESS;
968}