3
0

mpstat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* 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. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. //applet:IF_MPSTAT(APPLET(mpstat, _BB_DIR_BIN, _BB_SUID_DROP))
  10. //kbuild:lib-$(CONFIG_MPSTAT) += mpstat.o
  11. //config:config MPSTAT
  12. //config: bool "mpstat"
  13. //config: default y
  14. //config: help
  15. //config: Per-processor statistics
  16. #include "libbb.h"
  17. #include <sys/utsname.h> /* struct utsname */
  18. //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
  19. #define debug(fmt, ...) ((void)0)
  20. /* Size of /proc/interrupts line, CPU data excluded */
  21. #define INTERRUPTS_LINE 64
  22. /* Maximum number of interrupts */
  23. #define NR_IRQS 256
  24. #define NR_IRQCPU_PREALLOC 3
  25. #define MAX_IRQNAME_LEN 16
  26. #define MAX_PF_NAME 512
  27. /* sysstat 9.0.6 uses width 8, but newer code which also prints /proc/softirqs
  28. * data needs more: "interrupts" in /proc/softirqs have longer names,
  29. * most are up to 8 chars, one (BLOCK_IOPOLL) is even longer.
  30. * We are printing headers in the " IRQNAME/s" form, experimentally
  31. * anything smaller than 10 chars looks ugly for /proc/softirqs stats.
  32. */
  33. #define INTRATE_SCRWIDTH 10
  34. #define INTRATE_SCRWIDTH_STR "10"
  35. /* System files */
  36. #define SYSFS_DEVCPU "/sys/devices/system/cpu"
  37. #define PROCFS_STAT "/proc/stat"
  38. #define PROCFS_INTERRUPTS "/proc/interrupts"
  39. #define PROCFS_SOFTIRQS "/proc/softirqs"
  40. #define PROCFS_UPTIME "/proc/uptime"
  41. #if 1
  42. typedef unsigned long long data_t;
  43. typedef long long idata_t;
  44. #define FMT_DATA "ll"
  45. #define DATA_MAX ULLONG_MAX
  46. #else
  47. typedef unsigned long data_t;
  48. typedef long idata_t;
  49. #define FMT_DATA "l"
  50. #define DATA_MAX ULONG_MAX
  51. #endif
  52. struct stats_irqcpu {
  53. unsigned interrupts;
  54. char irq_name[MAX_IRQNAME_LEN];
  55. };
  56. struct stats_cpu {
  57. data_t cpu_user;
  58. data_t cpu_nice;
  59. data_t cpu_system;
  60. data_t cpu_idle;
  61. data_t cpu_iowait;
  62. data_t cpu_steal;
  63. data_t cpu_irq;
  64. data_t cpu_softirq;
  65. data_t cpu_guest;
  66. };
  67. struct stats_irq {
  68. data_t irq_nr;
  69. };
  70. /* Globals. Sort by size and access frequency. */
  71. struct globals {
  72. int interval;
  73. int count;
  74. unsigned cpu_nr; /* Number of CPUs */
  75. unsigned irqcpu_nr; /* Number of interrupts per CPU */
  76. unsigned softirqcpu_nr; /* Number of soft interrupts per CPU */
  77. unsigned options;
  78. unsigned hz;
  79. unsigned cpu_bitmap_len;
  80. smallint p_option;
  81. // 9.0.6 does not do it. Try "mpstat -A 1 2" - headers are repeated!
  82. //smallint header_done;
  83. //smallint avg_header_done;
  84. unsigned char *cpu_bitmap; /* Bit 0: global, bit 1: 1st proc... */
  85. data_t global_uptime[3];
  86. data_t per_cpu_uptime[3];
  87. struct stats_cpu *st_cpu[3];
  88. struct stats_irq *st_irq[3];
  89. struct stats_irqcpu *st_irqcpu[3];
  90. struct stats_irqcpu *st_softirqcpu[3];
  91. struct tm timestamp[3];
  92. };
  93. #define G (*ptr_to_globals)
  94. #define INIT_G() do { \
  95. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  96. } while (0)
  97. /* The selected interrupts statistics (bits in G.options) */
  98. enum {
  99. D_CPU = 1 << 0,
  100. D_IRQ_SUM = 1 << 1,
  101. D_IRQ_CPU = 1 << 2,
  102. D_SOFTIRQS = 1 << 3,
  103. };
  104. /* Is option on? */
  105. static ALWAYS_INLINE int display_opt(int opt)
  106. {
  107. return (opt & G.options);
  108. }
  109. #if DATA_MAX > 0xffffffff
  110. /*
  111. * Handle overflow conditions properly for counters which can have
  112. * less bits than data_t, depending on the kernel version.
  113. */
  114. /* Surprisingly, on 32bit inlining is a size win */
  115. static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
  116. {
  117. data_t v = curr - prev;
  118. if ((idata_t)v < 0 /* curr < prev - counter overflow? */
  119. && prev <= 0xffffffff /* kernel uses 32bit value for the counter? */
  120. ) {
  121. /* Add 33th bit set to 1 to curr, compensating for the overflow */
  122. /* double shift defeats "warning: left shift count >= width of type" */
  123. v += ((data_t)1 << 16) << 16;
  124. }
  125. return v;
  126. }
  127. #else
  128. static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
  129. {
  130. return curr - prev;
  131. }
  132. #endif
  133. static double percent_value(data_t prev, data_t curr, data_t itv)
  134. {
  135. return ((double)overflow_safe_sub(prev, curr)) / itv * 100;
  136. }
  137. static double hz_value(data_t prev, data_t curr, data_t itv)
  138. {
  139. //bb_error_msg("curr:%lld prev:%lld G.hz:%u", curr, prev, G.hz);
  140. return ((double)overflow_safe_sub(prev, curr)) / itv * G.hz;
  141. }
  142. static ALWAYS_INLINE data_t jiffies_diff(data_t old, data_t new)
  143. {
  144. data_t diff = new - old;
  145. return (diff == 0) ? 1 : diff;
  146. }
  147. static int is_cpu_in_bitmap(unsigned cpu)
  148. {
  149. return G.cpu_bitmap[cpu >> 3] & (1 << (cpu & 7));
  150. }
  151. static void write_irqcpu_stats(struct stats_irqcpu *per_cpu_stats[],
  152. int total_irqs,
  153. data_t itv,
  154. int prev, int current,
  155. const char *prev_str, const char *current_str)
  156. {
  157. int j;
  158. int offset, cpu;
  159. struct stats_irqcpu *p0, *q0;
  160. /* Check if number of IRQs has changed */
  161. if (G.interval != 0) {
  162. for (j = 0; j <= total_irqs; j++) {
  163. p0 = &per_cpu_stats[current][j];
  164. if (p0->irq_name[0] != '\0') {
  165. q0 = &per_cpu_stats[prev][j];
  166. if (strcmp(p0->irq_name, q0->irq_name) != 0) {
  167. /* Strings are different */
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. /* Print header */
  174. printf("\n%-11s CPU", prev_str);
  175. {
  176. /* A bit complex code to "buy back" space if one header is too wide.
  177. * Here's how it looks like. BLOCK_IOPOLL eats too much space,
  178. * and latter headers use smaller width to compensate:
  179. * ...BLOCK/s BLOCK_IOPOLL/s TASKLET/s SCHED/s HRTIMER/s RCU/s
  180. * ... 2.32 0.00 0.01 17.58 0.14 141.96
  181. */
  182. int expected_len = 0;
  183. int printed_len = 0;
  184. for (j = 0; j < total_irqs; j++) {
  185. p0 = &per_cpu_stats[current][j];
  186. if (p0->irq_name[0] != '\0') {
  187. int n = (INTRATE_SCRWIDTH-3) - (printed_len - expected_len);
  188. printed_len += printf(" %*s/s", n > 0 ? n : 0, skip_whitespace(p0->irq_name));
  189. expected_len += INTRATE_SCRWIDTH;
  190. }
  191. }
  192. }
  193. bb_putchar('\n');
  194. for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
  195. /* Check if we want stats about this CPU */
  196. if (!is_cpu_in_bitmap(cpu) && G.p_option) {
  197. continue;
  198. }
  199. printf("%-11s %4u", current_str, cpu - 1);
  200. for (j = 0; j < total_irqs; j++) {
  201. /* IRQ field set only for proc 0 */
  202. p0 = &per_cpu_stats[current][j];
  203. /*
  204. * An empty string for irq name means that
  205. * interrupt is no longer used.
  206. */
  207. if (p0->irq_name[0] != '\0') {
  208. offset = j;
  209. q0 = &per_cpu_stats[prev][offset];
  210. /*
  211. * If we want stats for the time since boot
  212. * we have p0->irq != q0->irq.
  213. */
  214. if (strcmp(p0->irq_name, q0->irq_name) != 0
  215. && G.interval != 0
  216. ) {
  217. if (j) {
  218. offset = j - 1;
  219. q0 = &per_cpu_stats[prev][offset];
  220. }
  221. if (strcmp(p0->irq_name, q0->irq_name) != 0
  222. && (j + 1 < total_irqs)
  223. ) {
  224. offset = j + 1;
  225. q0 = &per_cpu_stats[prev][offset];
  226. }
  227. }
  228. if (strcmp(p0->irq_name, q0->irq_name) == 0
  229. || G.interval == 0
  230. ) {
  231. struct stats_irqcpu *p, *q;
  232. p = &per_cpu_stats[current][(cpu - 1) * total_irqs + j];
  233. q = &per_cpu_stats[prev][(cpu - 1) * total_irqs + offset];
  234. printf("%"INTRATE_SCRWIDTH_STR".2f",
  235. (double)(p->interrupts - q->interrupts) / itv * G.hz);
  236. } else {
  237. printf(" N/A");
  238. }
  239. }
  240. }
  241. bb_putchar('\n');
  242. }
  243. }
  244. static data_t get_per_cpu_interval(const struct stats_cpu *scc,
  245. const struct stats_cpu *scp)
  246. {
  247. return ((scc->cpu_user + scc->cpu_nice +
  248. scc->cpu_system + scc->cpu_iowait +
  249. scc->cpu_idle + scc->cpu_steal +
  250. scc->cpu_irq + scc->cpu_softirq) -
  251. (scp->cpu_user + scp->cpu_nice +
  252. scp->cpu_system + scp->cpu_iowait +
  253. scp->cpu_idle + scp->cpu_steal +
  254. scp->cpu_irq + scp->cpu_softirq));
  255. }
  256. static void print_stats_cpu_struct(const struct stats_cpu *p,
  257. const struct stats_cpu *c,
  258. data_t itv)
  259. {
  260. printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
  261. percent_value(p->cpu_user - p->cpu_guest,
  262. /**/ c->cpu_user - c->cpu_guest, itv),
  263. percent_value(p->cpu_nice , c->cpu_nice , itv),
  264. percent_value(p->cpu_system , c->cpu_system , itv),
  265. percent_value(p->cpu_iowait , c->cpu_iowait , itv),
  266. percent_value(p->cpu_irq , c->cpu_irq , itv),
  267. percent_value(p->cpu_softirq, c->cpu_softirq, itv),
  268. percent_value(p->cpu_steal , c->cpu_steal , itv),
  269. percent_value(p->cpu_guest , c->cpu_guest , itv),
  270. percent_value(p->cpu_idle , c->cpu_idle , itv)
  271. );
  272. }
  273. static void write_stats_core(int prev, int current,
  274. const char *prev_str, const char *current_str)
  275. {
  276. struct stats_cpu *scc, *scp;
  277. data_t itv, global_itv;
  278. int cpu;
  279. /* Compute time interval */
  280. itv = global_itv = jiffies_diff(G.global_uptime[prev], G.global_uptime[current]);
  281. /* Reduce interval to one CPU */
  282. if (G.cpu_nr > 1)
  283. itv = jiffies_diff(G.per_cpu_uptime[prev], G.per_cpu_uptime[current]);
  284. /* Print CPU stats */
  285. if (display_opt(D_CPU)) {
  286. ///* This is done exactly once */
  287. //if (!G.header_done) {
  288. printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq %%soft %%steal %%guest %%idle\n",
  289. prev_str
  290. );
  291. // G.header_done = 1;
  292. //}
  293. for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
  294. data_t per_cpu_itv;
  295. /* Print stats about this particular CPU? */
  296. if (!is_cpu_in_bitmap(cpu))
  297. continue;
  298. scc = &G.st_cpu[current][cpu];
  299. scp = &G.st_cpu[prev][cpu];
  300. per_cpu_itv = global_itv;
  301. printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
  302. if (cpu) {
  303. double idle;
  304. /*
  305. * If the CPU is offline, then it isn't in /proc/stat,
  306. * so all values are 0.
  307. * NB: Guest time is already included in user time.
  308. */
  309. if ((scc->cpu_user | scc->cpu_nice | scc->cpu_system |
  310. scc->cpu_iowait | scc->cpu_idle | scc->cpu_steal |
  311. scc->cpu_irq | scc->cpu_softirq) == 0
  312. ) {
  313. /*
  314. * Set current struct fields to values from prev.
  315. * iteration. Then their values won't jump from
  316. * zero, when the CPU comes back online.
  317. */
  318. *scc = *scp;
  319. idle = 0.0;
  320. goto print_zeros;
  321. }
  322. /* Compute interval again for current proc */
  323. per_cpu_itv = get_per_cpu_interval(scc, scp);
  324. if (per_cpu_itv == 0) {
  325. /*
  326. * If the CPU is tickless then there is no change in CPU values
  327. * but the sum of values is not zero.
  328. */
  329. idle = 100.0;
  330. print_zeros:
  331. printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
  332. 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, idle);
  333. continue;
  334. }
  335. }
  336. print_stats_cpu_struct(scp, scc, per_cpu_itv);
  337. }
  338. }
  339. /* Print total number of IRQs per CPU */
  340. if (display_opt(D_IRQ_SUM)) {
  341. ///* Print average header, this is done exactly once */
  342. //if (!G.avg_header_done) {
  343. printf("\n%-11s CPU intr/s\n", prev_str);
  344. // G.avg_header_done = 1;
  345. //}
  346. for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
  347. data_t per_cpu_itv;
  348. /* Print stats about this CPU? */
  349. if (!is_cpu_in_bitmap(cpu))
  350. continue;
  351. per_cpu_itv = itv;
  352. printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
  353. if (cpu) {
  354. scc = &G.st_cpu[current][cpu];
  355. scp = &G.st_cpu[prev][cpu];
  356. /* Compute interval again for current proc */
  357. per_cpu_itv = get_per_cpu_interval(scc, scp);
  358. if (per_cpu_itv == 0) {
  359. printf(" %9.2f\n", 0.0);
  360. continue;
  361. }
  362. }
  363. //bb_error_msg("G.st_irq[%u][%u].irq_nr:%lld - G.st_irq[%u][%u].irq_nr:%lld",
  364. // current, cpu, G.st_irq[prev][cpu].irq_nr, prev, cpu, G.st_irq[current][cpu].irq_nr);
  365. printf(" %9.2f\n", hz_value(G.st_irq[prev][cpu].irq_nr, G.st_irq[current][cpu].irq_nr, per_cpu_itv));
  366. }
  367. }
  368. if (display_opt(D_IRQ_CPU)) {
  369. write_irqcpu_stats(G.st_irqcpu, G.irqcpu_nr,
  370. itv,
  371. prev, current,
  372. prev_str, current_str
  373. );
  374. }
  375. if (display_opt(D_SOFTIRQS)) {
  376. write_irqcpu_stats(G.st_softirqcpu, G.softirqcpu_nr,
  377. itv,
  378. prev, current,
  379. prev_str, current_str
  380. );
  381. }
  382. }
  383. /*
  384. * Print the statistics
  385. */
  386. static void write_stats(int current)
  387. {
  388. char prev_time[16];
  389. char curr_time[16];
  390. strftime(prev_time, sizeof(prev_time), "%X", &G.timestamp[!current]);
  391. strftime(curr_time, sizeof(curr_time), "%X", &G.timestamp[current]);
  392. write_stats_core(!current, current, prev_time, curr_time);
  393. }
  394. static void write_stats_avg(int current)
  395. {
  396. write_stats_core(2, current, "Average:", "Average:");
  397. }
  398. /*
  399. * Read CPU statistics
  400. */
  401. static void get_cpu_statistics(struct stats_cpu *cpu, data_t *up, data_t *up0)
  402. {
  403. FILE *fp;
  404. char buf[1024];
  405. fp = xfopen_for_read(PROCFS_STAT);
  406. while (fgets(buf, sizeof(buf), fp)) {
  407. data_t sum;
  408. unsigned cpu_number;
  409. struct stats_cpu *cp;
  410. if (!starts_with_cpu(buf))
  411. continue; /* not "cpu" */
  412. cp = cpu; /* for "cpu " case */
  413. if (buf[3] != ' ') {
  414. /* "cpuN " */
  415. if (G.cpu_nr == 0
  416. || sscanf(buf + 3, "%u ", &cpu_number) != 1
  417. || cpu_number >= G.cpu_nr
  418. ) {
  419. continue;
  420. }
  421. cp = &cpu[cpu_number + 1];
  422. }
  423. /* Read the counters, save them */
  424. /* Not all fields have to be present */
  425. memset(cp, 0, sizeof(*cp));
  426. sscanf(buf, "%*s"
  427. " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
  428. " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
  429. " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u",
  430. &cp->cpu_user, &cp->cpu_nice, &cp->cpu_system,
  431. &cp->cpu_idle, &cp->cpu_iowait, &cp->cpu_irq,
  432. &cp->cpu_softirq, &cp->cpu_steal, &cp->cpu_guest
  433. );
  434. /*
  435. * Compute uptime in jiffies (1/HZ), it'll be the sum of
  436. * individual CPU's uptimes.
  437. * NB: We have to omit cpu_guest, because cpu_user includes it.
  438. */
  439. sum = cp->cpu_user + cp->cpu_nice + cp->cpu_system +
  440. cp->cpu_idle + cp->cpu_iowait + cp->cpu_irq +
  441. cp->cpu_softirq + cp->cpu_steal;
  442. if (buf[3] == ' ') {
  443. /* "cpu " */
  444. *up = sum;
  445. } else {
  446. /* "cpuN " */
  447. if (cpu_number == 0 && *up0 != 0) {
  448. /* Compute uptime of single CPU */
  449. *up0 = sum;
  450. }
  451. }
  452. }
  453. fclose(fp);
  454. }
  455. /*
  456. * Read IRQs from /proc/stat
  457. */
  458. static void get_irqs_from_stat(struct stats_irq *irq)
  459. {
  460. FILE *fp;
  461. char buf[1024];
  462. fp = fopen_for_read(PROCFS_STAT);
  463. if (!fp)
  464. return;
  465. while (fgets(buf, sizeof(buf), fp)) {
  466. //bb_error_msg("/proc/stat:'%s'", buf);
  467. if (strncmp(buf, "intr ", 5) == 0) {
  468. /* Read total number of IRQs since system boot */
  469. sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
  470. }
  471. }
  472. fclose(fp);
  473. }
  474. /*
  475. * Read stats from /proc/interrupts or /proc/softirqs
  476. */
  477. static void get_irqs_from_interrupts(const char *fname,
  478. struct stats_irqcpu *per_cpu_stats[],
  479. int irqs_per_cpu, int current)
  480. {
  481. FILE *fp;
  482. struct stats_irq *irq_i;
  483. struct stats_irqcpu *ic;
  484. char *buf;
  485. unsigned buflen;
  486. unsigned cpu;
  487. unsigned irq;
  488. int cpu_index[G.cpu_nr];
  489. int iindex;
  490. // Moved to caller.
  491. // Otherwise reading of /proc/softirqs
  492. // was resetting counts to 0 after we painstakingly collected them from
  493. // /proc/interrupts. Which resulted in:
  494. // 01:32:47 PM CPU intr/s
  495. // 01:32:47 PM all 591.47
  496. // 01:32:47 PM 0 0.00 <= ???
  497. // 01:32:47 PM 1 0.00 <= ???
  498. // for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
  499. // G.st_irq[current][cpu].irq_nr = 0;
  500. // //bb_error_msg("G.st_irq[%u][%u].irq_nr=0", current, cpu);
  501. // }
  502. fp = fopen_for_read(fname);
  503. if (!fp)
  504. return;
  505. buflen = INTERRUPTS_LINE + 16 * G.cpu_nr;
  506. buf = xmalloc(buflen);
  507. /* Parse header and determine, which CPUs are online */
  508. iindex = 0;
  509. while (fgets(buf, buflen, fp)) {
  510. char *cp, *next;
  511. next = buf;
  512. while ((cp = strstr(next, "CPU")) != NULL
  513. && iindex < G.cpu_nr
  514. ) {
  515. cpu = strtoul(cp + 3, &next, 10);
  516. cpu_index[iindex++] = cpu;
  517. }
  518. if (iindex) /* We found header */
  519. break;
  520. }
  521. irq = 0;
  522. while (fgets(buf, buflen, fp)
  523. && irq < irqs_per_cpu
  524. ) {
  525. int len;
  526. char last_char;
  527. char *cp;
  528. /* Skip over "IRQNAME:" */
  529. cp = strchr(buf, ':');
  530. if (!cp)
  531. continue;
  532. last_char = cp[-1];
  533. ic = &per_cpu_stats[current][irq];
  534. len = cp - buf;
  535. if (len >= sizeof(ic->irq_name)) {
  536. len = sizeof(ic->irq_name) - 1;
  537. }
  538. safe_strncpy(ic->irq_name, buf, len + 1);
  539. //bb_error_msg("%s: irq%d:'%s' buf:'%s'", fname, irq, ic->irq_name, buf);
  540. cp++;
  541. for (cpu = 0; cpu < iindex; cpu++) {
  542. char *next;
  543. ic = &per_cpu_stats[current][cpu_index[cpu] * irqs_per_cpu + irq];
  544. irq_i = &G.st_irq[current][cpu_index[cpu] + 1];
  545. ic->interrupts = strtoul(cp, &next, 10);
  546. /* Count only numerical IRQs */
  547. if (isdigit(last_char)) {
  548. irq_i->irq_nr += ic->interrupts;
  549. //bb_error_msg("G.st_irq[%u][%u].irq_nr + %u = %lld",
  550. // current, cpu_index[cpu] + 1, ic->interrupts, irq_i->irq_nr);
  551. }
  552. cp = next;
  553. }
  554. irq++;
  555. }
  556. fclose(fp);
  557. free(buf);
  558. while (irq < irqs_per_cpu) {
  559. /* Number of interrupts per CPU has changed */
  560. ic = &per_cpu_stats[current][irq];
  561. ic->irq_name[0] = '\0'; /* False interrupt */
  562. irq++;
  563. }
  564. }
  565. static void get_uptime(data_t *uptime)
  566. {
  567. FILE *fp;
  568. char buf[sizeof(long)*3 * 2 + 4]; /* enough for long.long */
  569. unsigned long uptime_sec, decimal;
  570. fp = fopen_for_read(PROCFS_UPTIME);
  571. if (!fp)
  572. return;
  573. if (fgets(buf, sizeof(buf), fp)) {
  574. if (sscanf(buf, "%lu.%lu", &uptime_sec, &decimal) == 2) {
  575. *uptime = (data_t)uptime_sec * G.hz + decimal * G.hz / 100;
  576. }
  577. }
  578. fclose(fp);
  579. }
  580. static void get_localtime(struct tm *tm)
  581. {
  582. time_t timer;
  583. time(&timer);
  584. localtime_r(&timer, tm);
  585. }
  586. static void alarm_handler(int sig UNUSED_PARAM)
  587. {
  588. signal(SIGALRM, alarm_handler);
  589. alarm(G.interval);
  590. }
  591. static void main_loop(void)
  592. {
  593. unsigned current;
  594. unsigned cpus;
  595. /* Read the stats */
  596. if (G.cpu_nr > 1) {
  597. G.per_cpu_uptime[0] = 0;
  598. get_uptime(&G.per_cpu_uptime[0]);
  599. }
  600. get_cpu_statistics(G.st_cpu[0], &G.global_uptime[0], &G.per_cpu_uptime[0]);
  601. if (display_opt(D_IRQ_SUM))
  602. get_irqs_from_stat(G.st_irq[0]);
  603. if (display_opt(D_IRQ_SUM | D_IRQ_CPU))
  604. get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
  605. G.irqcpu_nr, 0);
  606. if (display_opt(D_SOFTIRQS))
  607. get_irqs_from_interrupts(PROCFS_SOFTIRQS, G.st_softirqcpu,
  608. G.softirqcpu_nr, 0);
  609. if (G.interval == 0) {
  610. /* Display since boot time */
  611. cpus = G.cpu_nr + 1;
  612. G.timestamp[1] = G.timestamp[0];
  613. memset(G.st_cpu[1], 0, sizeof(G.st_cpu[1][0]) * cpus);
  614. memset(G.st_irq[1], 0, sizeof(G.st_irq[1][0]) * cpus);
  615. memset(G.st_irqcpu[1], 0, sizeof(G.st_irqcpu[1][0]) * cpus * G.irqcpu_nr);
  616. memset(G.st_softirqcpu[1], 0, sizeof(G.st_softirqcpu[1][0]) * cpus * G.softirqcpu_nr);
  617. write_stats(0);
  618. /* And we're done */
  619. return;
  620. }
  621. /* Set a handler for SIGALRM */
  622. alarm_handler(0);
  623. /* Save the stats we already have. We need them to compute the average */
  624. G.timestamp[2] = G.timestamp[0];
  625. G.global_uptime[2] = G.global_uptime[0];
  626. G.per_cpu_uptime[2] = G.per_cpu_uptime[0];
  627. cpus = G.cpu_nr + 1;
  628. memcpy(G.st_cpu[2], G.st_cpu[0], sizeof(G.st_cpu[0][0]) * cpus);
  629. memcpy(G.st_irq[2], G.st_irq[0], sizeof(G.st_irq[0][0]) * cpus);
  630. memcpy(G.st_irqcpu[2], G.st_irqcpu[0], sizeof(G.st_irqcpu[0][0]) * cpus * G.irqcpu_nr);
  631. if (display_opt(D_SOFTIRQS)) {
  632. memcpy(G.st_softirqcpu[2], G.st_softirqcpu[0],
  633. sizeof(G.st_softirqcpu[0][0]) * cpus * G.softirqcpu_nr);
  634. }
  635. current = 1;
  636. while (1) {
  637. /* Suspend until a signal is received */
  638. pause();
  639. /* Set structures to 0 to distinguish off/online CPUs */
  640. memset(&G.st_cpu[current][/*cpu:*/ 1], 0, sizeof(G.st_cpu[0][0]) * G.cpu_nr);
  641. get_localtime(&G.timestamp[current]);
  642. /* Read stats */
  643. if (G.cpu_nr > 1) {
  644. G.per_cpu_uptime[current] = 0;
  645. get_uptime(&G.per_cpu_uptime[current]);
  646. }
  647. get_cpu_statistics(G.st_cpu[current], &G.global_uptime[current], &G.per_cpu_uptime[current]);
  648. if (display_opt(D_IRQ_SUM))
  649. get_irqs_from_stat(G.st_irq[current]);
  650. if (display_opt(D_IRQ_SUM | D_IRQ_CPU)) {
  651. int cpu;
  652. for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
  653. G.st_irq[current][cpu].irq_nr = 0;
  654. }
  655. /* accumulates .irq_nr */
  656. get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
  657. G.irqcpu_nr, current);
  658. }
  659. if (display_opt(D_SOFTIRQS))
  660. get_irqs_from_interrupts(PROCFS_SOFTIRQS,
  661. G.st_softirqcpu,
  662. G.softirqcpu_nr, current);
  663. write_stats(current);
  664. if (G.count > 0) {
  665. if (--G.count == 0)
  666. break;
  667. }
  668. current ^= 1;
  669. }
  670. /* Print average statistics */
  671. write_stats_avg(current);
  672. }
  673. /* Initialization */
  674. /* Get number of clock ticks per sec */
  675. static ALWAYS_INLINE unsigned get_hz(void)
  676. {
  677. return sysconf(_SC_CLK_TCK);
  678. }
  679. static void alloc_struct(int cpus)
  680. {
  681. int i;
  682. for (i = 0; i < 3; i++) {
  683. G.st_cpu[i] = xzalloc(sizeof(G.st_cpu[i][0]) * cpus);
  684. G.st_irq[i] = xzalloc(sizeof(G.st_irq[i][0]) * cpus);
  685. G.st_irqcpu[i] = xzalloc(sizeof(G.st_irqcpu[i][0]) * cpus * G.irqcpu_nr);
  686. G.st_softirqcpu[i] = xzalloc(sizeof(G.st_softirqcpu[i][0]) * cpus * G.softirqcpu_nr);
  687. }
  688. G.cpu_bitmap_len = (cpus >> 3) + 1;
  689. G.cpu_bitmap = xzalloc(G.cpu_bitmap_len);
  690. }
  691. static void print_header(struct tm *t)
  692. {
  693. char cur_date[16];
  694. struct utsname uts;
  695. /* Get system name, release number and hostname */
  696. uname(&uts);
  697. strftime(cur_date, sizeof(cur_date), "%x", t);
  698. printf("%s %s (%s)\t%s\t_%s_\t(%u CPU)\n",
  699. uts.sysname, uts.release, uts.nodename, cur_date, uts.machine, G.cpu_nr);
  700. }
  701. /*
  702. * Get number of interrupts available per processor
  703. */
  704. static int get_irqcpu_nr(const char *f, int max_irqs)
  705. {
  706. FILE *fp;
  707. char *line;
  708. unsigned linelen;
  709. unsigned irq;
  710. fp = fopen_for_read(f);
  711. if (!fp) /* No interrupts file */
  712. return 0;
  713. linelen = INTERRUPTS_LINE + 16 * G.cpu_nr;
  714. line = xmalloc(linelen);
  715. irq = 0;
  716. while (fgets(line, linelen, fp)
  717. && irq < max_irqs
  718. ) {
  719. int p = strcspn(line, ":");
  720. if ((p > 0) && (p < 16))
  721. irq++;
  722. }
  723. fclose(fp);
  724. free(line);
  725. return irq;
  726. }
  727. //usage:#define mpstat_trivial_usage
  728. //usage: "[-A] [-I SUM|CPU|ALL|SCPU] [-u] [-P num|ALL] [INTERVAL [COUNT]]"
  729. //usage:#define mpstat_full_usage "\n\n"
  730. //usage: "Per-processor statistics\n"
  731. //usage: "\nOptions:"
  732. //usage: "\n -A Same as -I ALL -u -P ALL"
  733. //usage: "\n -I SUM|CPU|ALL|SCPU Report interrupt statistics"
  734. //usage: "\n -P num|ALL Processor to monitor"
  735. //usage: "\n -u Report CPU utilization"
  736. int mpstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  737. int mpstat_main(int UNUSED_PARAM argc, char **argv)
  738. {
  739. char *opt_irq_fmt;
  740. char *opt_set_cpu;
  741. int i, opt;
  742. enum {
  743. OPT_ALL = 1 << 0, /* -A */
  744. OPT_INTS = 1 << 1, /* -I */
  745. OPT_SETCPU = 1 << 2, /* -P */
  746. OPT_UTIL = 1 << 3, /* -u */
  747. };
  748. /* Dont buffer data if redirected to a pipe */
  749. setbuf(stdout, NULL);
  750. INIT_G();
  751. G.interval = -1;
  752. /* Get number of processors */
  753. G.cpu_nr = get_cpu_count();
  754. /* Get number of clock ticks per sec */
  755. G.hz = get_hz();
  756. /* Calculate number of interrupts per processor */
  757. G.irqcpu_nr = get_irqcpu_nr(PROCFS_INTERRUPTS, NR_IRQS) + NR_IRQCPU_PREALLOC;
  758. /* Calculate number of soft interrupts per processor */
  759. G.softirqcpu_nr = get_irqcpu_nr(PROCFS_SOFTIRQS, NR_IRQS) + NR_IRQCPU_PREALLOC;
  760. /* Allocate space for structures. + 1 for global structure. */
  761. alloc_struct(G.cpu_nr + 1);
  762. /* Parse and process arguments */
  763. opt = getopt32(argv, "AI:P:u", &opt_irq_fmt, &opt_set_cpu);
  764. argv += optind;
  765. if (*argv) {
  766. /* Get interval */
  767. G.interval = xatoi_positive(*argv);
  768. G.count = -1;
  769. argv++;
  770. if (*argv) {
  771. /* Get count value */
  772. if (G.interval == 0)
  773. bb_show_usage();
  774. G.count = xatoi_positive(*argv);
  775. //if (*++argv)
  776. // bb_show_usage();
  777. }
  778. }
  779. if (G.interval < 0)
  780. G.interval = 0;
  781. if (opt & OPT_ALL) {
  782. G.p_option = 1;
  783. G.options |= D_CPU + D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS;
  784. /* Select every CPU */
  785. memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
  786. }
  787. if (opt & OPT_INTS) {
  788. static const char v[] = {
  789. D_IRQ_CPU, D_IRQ_SUM, D_SOFTIRQS,
  790. D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS
  791. };
  792. i = index_in_strings("CPU\0SUM\0SCPU\0ALL\0", opt_irq_fmt);
  793. if (i == -1)
  794. bb_show_usage();
  795. G.options |= v[i];
  796. }
  797. if ((opt & OPT_UTIL) /* -u? */
  798. || G.options == 0 /* nothing? (use default then) */
  799. ) {
  800. G.options |= D_CPU;
  801. }
  802. if (opt & OPT_SETCPU) {
  803. char *t;
  804. G.p_option = 1;
  805. for (t = strtok(opt_set_cpu, ","); t; t = strtok(NULL, ",")) {
  806. if (strcmp(t, "ALL") == 0) {
  807. /* Select every CPU */
  808. memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
  809. } else {
  810. /* Get CPU number */
  811. unsigned n = xatoi_positive(t);
  812. if (n >= G.cpu_nr)
  813. bb_error_msg_and_die("not that many processors");
  814. n++;
  815. G.cpu_bitmap[n >> 3] |= 1 << (n & 7);
  816. }
  817. }
  818. }
  819. if (!G.p_option)
  820. /* Display global stats */
  821. G.cpu_bitmap[0] = 1;
  822. /* Get time */
  823. get_localtime(&G.timestamp[0]);
  824. /* Display header */
  825. print_header(&G.timestamp[0]);
  826. /* The main loop */
  827. main_loop();
  828. if (ENABLE_FEATURE_CLEAN_UP) {
  829. /* Clean up */
  830. for (i = 0; i < 3; i++) {
  831. free(G.st_cpu[i]);
  832. free(G.st_irq[i]);
  833. free(G.st_irqcpu[i]);
  834. free(G.st_softirqcpu[i]);
  835. }
  836. free(G.cpu_bitmap);
  837. free(&G);
  838. }
  839. return EXIT_SUCCESS;
  840. }