123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967 |
- #include "libbb.h"
- #include <sys/utsname.h> /* struct utsname */
- #define debug(fmt, ...) ((void)0)
- #define INTERRUPTS_LINE 64
- #define NR_IRQS 256
- #define NR_IRQCPU_PREALLOC 3
- #define MAX_IRQNAME_LEN 16
- #define MAX_PF_NAME 512
- #define INTRATE_SCRWIDTH 10
- #define INTRATE_SCRWIDTH_STR "10"
- #define PROCFS_STAT "/proc/stat"
- #define PROCFS_INTERRUPTS "/proc/interrupts"
- #define PROCFS_SOFTIRQS "/proc/softirqs"
- #define PROCFS_UPTIME "/proc/uptime"
- #if 1
- typedef unsigned long long data_t;
- typedef long long idata_t;
- #define FMT_DATA "ll"
- #define DATA_MAX ULLONG_MAX
- #else
- typedef unsigned long data_t;
- typedef long idata_t;
- #define FMT_DATA "l"
- #define DATA_MAX ULONG_MAX
- #endif
- struct stats_irqcpu {
- unsigned interrupts;
- char irq_name[MAX_IRQNAME_LEN];
- };
- struct stats_cpu {
- data_t cpu_user;
- data_t cpu_nice;
- data_t cpu_system;
- data_t cpu_idle;
- data_t cpu_iowait;
- data_t cpu_steal;
- data_t cpu_irq;
- data_t cpu_softirq;
- data_t cpu_guest;
- };
- struct stats_irq {
- data_t irq_nr;
- };
- struct globals {
- int interval;
- int count;
- unsigned cpu_nr;
- unsigned irqcpu_nr;
- unsigned softirqcpu_nr;
- unsigned options;
- unsigned hz;
- unsigned cpu_bitmap_len;
- smallint p_option;
-
-
-
- unsigned char *cpu_bitmap;
- data_t global_uptime[3];
- data_t per_cpu_uptime[3];
- struct stats_cpu *st_cpu[3];
- struct stats_irq *st_irq[3];
- struct stats_irqcpu *st_irqcpu[3];
- struct stats_irqcpu *st_softirqcpu[3];
- struct tm timestamp[3];
- };
- #define G (*ptr_to_globals)
- #define INIT_G() do { \
- SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
- } while (0)
- enum {
- D_CPU = 1 << 0,
- D_IRQ_SUM = 1 << 1,
- D_IRQ_CPU = 1 << 2,
- D_SOFTIRQS = 1 << 3,
- };
- static ALWAYS_INLINE int display_opt(int opt)
- {
- return (opt & G.options);
- }
- #if DATA_MAX > 0xffffffff
- static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
- {
- data_t v = curr - prev;
- if ((idata_t)v < 0
- && prev <= 0xffffffff
- ) {
-
-
- v += ((data_t)1 << 16) << 16;
- }
- return v;
- }
- #else
- static ALWAYS_INLINE data_t overflow_safe_sub(data_t prev, data_t curr)
- {
- return curr - prev;
- }
- #endif
- static double percent_value(data_t prev, data_t curr, data_t itv)
- {
- return ((double)overflow_safe_sub(prev, curr)) / itv * 100;
- }
- static double hz_value(data_t prev, data_t curr, data_t itv)
- {
-
- return ((double)overflow_safe_sub(prev, curr)) / itv * G.hz;
- }
- static ALWAYS_INLINE data_t jiffies_diff(data_t old, data_t new)
- {
- data_t diff = new - old;
- return (diff == 0) ? 1 : diff;
- }
- static int is_cpu_in_bitmap(unsigned cpu)
- {
- return G.cpu_bitmap[cpu >> 3] & (1 << (cpu & 7));
- }
- static void write_irqcpu_stats(struct stats_irqcpu *per_cpu_stats[],
- int total_irqs,
- data_t itv,
- int prev, int current,
- const char *prev_str, const char *current_str)
- {
- int j;
- int offset, cpu;
- struct stats_irqcpu *p0, *q0;
-
- if (G.interval != 0) {
- for (j = 0; j <= total_irqs; j++) {
- p0 = &per_cpu_stats[current][j];
- if (p0->irq_name[0] != '\0') {
- q0 = &per_cpu_stats[prev][j];
- if (strcmp(p0->irq_name, q0->irq_name) != 0) {
-
- break;
- }
- }
- }
- }
-
- printf("\n%-11s CPU", prev_str);
- {
-
- int expected_len = 0;
- int printed_len = 0;
- for (j = 0; j < total_irqs; j++) {
- p0 = &per_cpu_stats[current][j];
- if (p0->irq_name[0] != '\0') {
- int n = (INTRATE_SCRWIDTH-3) - (printed_len - expected_len);
- printed_len += printf(" %*s/s", n > 0 ? n : 0, skip_whitespace(p0->irq_name));
- expected_len += INTRATE_SCRWIDTH;
- }
- }
- }
- bb_putchar('\n');
- for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
-
- if (!is_cpu_in_bitmap(cpu) && G.p_option) {
- continue;
- }
- printf("%-11s %4u", current_str, cpu - 1);
- for (j = 0; j < total_irqs; j++) {
-
- p0 = &per_cpu_stats[current][j];
-
- if (p0->irq_name[0] != '\0') {
- offset = j;
- q0 = &per_cpu_stats[prev][offset];
-
- if (strcmp(p0->irq_name, q0->irq_name) != 0
- && G.interval != 0
- ) {
- if (j) {
- offset = j - 1;
- q0 = &per_cpu_stats[prev][offset];
- }
- if (strcmp(p0->irq_name, q0->irq_name) != 0
- && (j + 1 < total_irqs)
- ) {
- offset = j + 1;
- q0 = &per_cpu_stats[prev][offset];
- }
- }
- if (strcmp(p0->irq_name, q0->irq_name) == 0
- || G.interval == 0
- ) {
- struct stats_irqcpu *p, *q;
- p = &per_cpu_stats[current][(cpu - 1) * total_irqs + j];
- q = &per_cpu_stats[prev][(cpu - 1) * total_irqs + offset];
- printf("%"INTRATE_SCRWIDTH_STR".2f",
- (double)(p->interrupts - q->interrupts) / itv * G.hz);
- } else {
- printf(" N/A");
- }
- }
- }
- bb_putchar('\n');
- }
- }
- static data_t get_per_cpu_interval(const struct stats_cpu *scc,
- const struct stats_cpu *scp)
- {
- return ((scc->cpu_user + scc->cpu_nice +
- scc->cpu_system + scc->cpu_iowait +
- scc->cpu_idle + scc->cpu_steal +
- scc->cpu_irq + scc->cpu_softirq) -
- (scp->cpu_user + scp->cpu_nice +
- scp->cpu_system + scp->cpu_iowait +
- scp->cpu_idle + scp->cpu_steal +
- scp->cpu_irq + scp->cpu_softirq));
- }
- static void print_stats_cpu_struct(const struct stats_cpu *p,
- const struct stats_cpu *c,
- data_t itv)
- {
- printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
- percent_value(p->cpu_user - p->cpu_guest,
- c->cpu_user - c->cpu_guest, itv),
- percent_value(p->cpu_nice , c->cpu_nice , itv),
- percent_value(p->cpu_system , c->cpu_system , itv),
- percent_value(p->cpu_iowait , c->cpu_iowait , itv),
- percent_value(p->cpu_irq , c->cpu_irq , itv),
- percent_value(p->cpu_softirq, c->cpu_softirq, itv),
- percent_value(p->cpu_steal , c->cpu_steal , itv),
- percent_value(p->cpu_guest , c->cpu_guest , itv),
- percent_value(p->cpu_idle , c->cpu_idle , itv)
- );
- }
- static void write_stats_core(int prev, int current,
- const char *prev_str, const char *current_str)
- {
- struct stats_cpu *scc, *scp;
- data_t itv, global_itv;
- int cpu;
-
- itv = global_itv = jiffies_diff(G.global_uptime[prev], G.global_uptime[current]);
-
- if (G.cpu_nr > 1)
- itv = jiffies_diff(G.per_cpu_uptime[prev], G.per_cpu_uptime[current]);
-
- if (display_opt(D_CPU)) {
-
-
- printf("\n%-11s CPU %%usr %%nice %%sys %%iowait %%irq %%soft %%steal %%guest %%idle\n",
- prev_str
- );
-
-
- for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
- data_t per_cpu_itv;
-
- if (!is_cpu_in_bitmap(cpu))
- continue;
- scc = &G.st_cpu[current][cpu];
- scp = &G.st_cpu[prev][cpu];
- per_cpu_itv = global_itv;
- printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
- if (cpu) {
- double idle;
-
- if ((scc->cpu_user | scc->cpu_nice | scc->cpu_system |
- scc->cpu_iowait | scc->cpu_idle | scc->cpu_steal |
- scc->cpu_irq | scc->cpu_softirq) == 0
- ) {
-
- *scc = *scp;
- idle = 0.0;
- goto print_zeros;
- }
-
- per_cpu_itv = get_per_cpu_interval(scc, scp);
- if (per_cpu_itv == 0) {
-
- idle = 100.0;
- print_zeros:
- printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
- 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, idle);
- continue;
- }
- }
- print_stats_cpu_struct(scp, scc, per_cpu_itv);
- }
- }
-
- if (display_opt(D_IRQ_SUM)) {
-
-
- printf("\n%-11s CPU intr/s\n", prev_str);
-
-
- for (cpu = 0; cpu <= G.cpu_nr; cpu++) {
- data_t per_cpu_itv;
-
- if (!is_cpu_in_bitmap(cpu))
- continue;
- per_cpu_itv = itv;
- printf((cpu ? "%-11s %4u" : "%-11s all"), current_str, cpu - 1);
- if (cpu) {
- scc = &G.st_cpu[current][cpu];
- scp = &G.st_cpu[prev][cpu];
-
- per_cpu_itv = get_per_cpu_interval(scc, scp);
- if (per_cpu_itv == 0) {
- printf(" %9.2f\n", 0.0);
- continue;
- }
- }
-
-
- printf(" %9.2f\n", hz_value(G.st_irq[prev][cpu].irq_nr, G.st_irq[current][cpu].irq_nr, per_cpu_itv));
- }
- }
- if (display_opt(D_IRQ_CPU)) {
- write_irqcpu_stats(G.st_irqcpu, G.irqcpu_nr,
- itv,
- prev, current,
- prev_str, current_str
- );
- }
- if (display_opt(D_SOFTIRQS)) {
- write_irqcpu_stats(G.st_softirqcpu, G.softirqcpu_nr,
- itv,
- prev, current,
- prev_str, current_str
- );
- }
- }
- static void write_stats(int current)
- {
- char prev_time[16];
- char curr_time[16];
- strftime(prev_time, sizeof(prev_time), "%X", &G.timestamp[!current]);
- strftime(curr_time, sizeof(curr_time), "%X", &G.timestamp[current]);
- write_stats_core(!current, current, prev_time, curr_time);
- }
- static void write_stats_avg(int current)
- {
- write_stats_core(2, current, "Average:", "Average:");
- }
- static void get_cpu_statistics(struct stats_cpu *cpu, data_t *up, data_t *up0)
- {
- FILE *fp;
- char buf[1024];
- fp = xfopen_for_read(PROCFS_STAT);
- while (fgets(buf, sizeof(buf), fp)) {
- data_t sum;
- unsigned cpu_number;
- struct stats_cpu *cp;
- if (!starts_with_cpu(buf))
- continue;
- cp = cpu;
- if (buf[3] != ' ') {
-
- if (G.cpu_nr == 0
- || sscanf(buf + 3, "%u ", &cpu_number) != 1
- || cpu_number >= G.cpu_nr
- ) {
- continue;
- }
- cp = &cpu[cpu_number + 1];
- }
-
-
- memset(cp, 0, sizeof(*cp));
- sscanf(buf, "%*s"
- " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
- " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u"
- " %"FMT_DATA"u %"FMT_DATA"u %"FMT_DATA"u",
- &cp->cpu_user, &cp->cpu_nice, &cp->cpu_system,
- &cp->cpu_idle, &cp->cpu_iowait, &cp->cpu_irq,
- &cp->cpu_softirq, &cp->cpu_steal, &cp->cpu_guest
- );
-
- sum = cp->cpu_user + cp->cpu_nice + cp->cpu_system +
- cp->cpu_idle + cp->cpu_iowait + cp->cpu_irq +
- cp->cpu_softirq + cp->cpu_steal;
- if (buf[3] == ' ') {
-
- *up = sum;
- } else {
-
- if (cpu_number == 0 && *up0 != 0) {
-
- *up0 = sum;
- }
- }
- }
- fclose(fp);
- }
- static void get_irqs_from_stat(struct stats_irq *irq)
- {
- FILE *fp;
- char buf[1024];
- fp = xfopen_for_read(PROCFS_STAT);
- while (fgets(buf, sizeof(buf), fp)) {
-
- if (is_prefixed_with(buf, "intr ")) {
-
- sscanf(buf + 5, "%"FMT_DATA"u", &irq->irq_nr);
- }
- }
- fclose(fp);
- }
- static void get_irqs_from_interrupts(const char *fname,
- struct stats_irqcpu *per_cpu_stats[],
- int irqs_per_cpu, int current)
- {
- FILE *fp;
- struct stats_irq *irq_i;
- struct stats_irqcpu *ic;
- char *buf;
- unsigned buflen;
- unsigned cpu;
- unsigned irq;
- int cpu_index[G.cpu_nr];
- int iindex;
- fp = fopen_for_read(fname);
- if (!fp)
- return;
- buflen = INTERRUPTS_LINE + 16 * G.cpu_nr;
- buf = xmalloc(buflen);
-
- iindex = 0;
- while (fgets(buf, buflen, fp)) {
- char *cp, *next;
- next = buf;
- while ((cp = strstr(next, "CPU")) != NULL
- && iindex < G.cpu_nr
- ) {
- cpu = strtoul(cp + 3, &next, 10);
- cpu_index[iindex++] = cpu;
- }
- if (iindex)
- break;
- }
- irq = 0;
- while (fgets(buf, buflen, fp)
- && irq < irqs_per_cpu
- ) {
- int len;
- char last_char;
- char *cp;
-
- cp = strchr(buf, ':');
- if (!cp)
- continue;
- last_char = cp[-1];
- ic = &per_cpu_stats[current][irq];
- len = cp - buf;
- if (len >= sizeof(ic->irq_name)) {
- len = sizeof(ic->irq_name) - 1;
- }
- safe_strncpy(ic->irq_name, buf, len + 1);
-
- cp++;
- for (cpu = 0; cpu < iindex; cpu++) {
- char *next;
- ic = &per_cpu_stats[current][cpu_index[cpu] * irqs_per_cpu + irq];
- irq_i = &G.st_irq[current][cpu_index[cpu] + 1];
- ic->interrupts = strtoul(cp, &next, 10);
-
- if (isdigit(last_char)) {
- irq_i->irq_nr += ic->interrupts;
-
-
- }
- cp = next;
- }
- irq++;
- }
- fclose(fp);
- free(buf);
- while (irq < irqs_per_cpu) {
-
- ic = &per_cpu_stats[current][irq];
- ic->irq_name[0] = '\0';
- irq++;
- }
- }
- static void get_uptime(data_t *uptime)
- {
- FILE *fp;
- char buf[sizeof(long)*3 * 2 + 4];
- unsigned long uptime_sec, decimal;
- fp = xfopen_for_read(PROCFS_UPTIME);
- if (fgets(buf, sizeof(buf), fp)) {
- if (sscanf(buf, "%lu.%lu", &uptime_sec, &decimal) == 2) {
- *uptime = (data_t)uptime_sec * G.hz + decimal * G.hz / 100;
- }
- }
- fclose(fp);
- }
- static void get_localtime(struct tm *tm)
- {
- time_t timer;
- time(&timer);
- localtime_r(&timer, tm);
- }
- static void alarm_handler(int sig UNUSED_PARAM)
- {
- signal(SIGALRM, alarm_handler);
- alarm(G.interval);
- }
- static void main_loop(void)
- {
- unsigned current;
- unsigned cpus;
-
- if (G.cpu_nr > 1) {
- G.per_cpu_uptime[0] = 0;
- get_uptime(&G.per_cpu_uptime[0]);
- }
- get_cpu_statistics(G.st_cpu[0], &G.global_uptime[0], &G.per_cpu_uptime[0]);
- if (display_opt(D_IRQ_SUM))
- get_irqs_from_stat(G.st_irq[0]);
- if (display_opt(D_IRQ_SUM | D_IRQ_CPU))
- get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
- G.irqcpu_nr, 0);
- if (display_opt(D_SOFTIRQS))
- get_irqs_from_interrupts(PROCFS_SOFTIRQS, G.st_softirqcpu,
- G.softirqcpu_nr, 0);
- if (G.interval == 0) {
-
- cpus = G.cpu_nr + 1;
- G.timestamp[1] = G.timestamp[0];
- memset(G.st_cpu[1], 0, sizeof(G.st_cpu[1][0]) * cpus);
- memset(G.st_irq[1], 0, sizeof(G.st_irq[1][0]) * cpus);
- memset(G.st_irqcpu[1], 0, sizeof(G.st_irqcpu[1][0]) * cpus * G.irqcpu_nr);
- memset(G.st_softirqcpu[1], 0, sizeof(G.st_softirqcpu[1][0]) * cpus * G.softirqcpu_nr);
- write_stats(0);
-
- return;
- }
-
- alarm_handler(0);
-
- G.timestamp[2] = G.timestamp[0];
- G.global_uptime[2] = G.global_uptime[0];
- G.per_cpu_uptime[2] = G.per_cpu_uptime[0];
- cpus = G.cpu_nr + 1;
- memcpy(G.st_cpu[2], G.st_cpu[0], sizeof(G.st_cpu[0][0]) * cpus);
- memcpy(G.st_irq[2], G.st_irq[0], sizeof(G.st_irq[0][0]) * cpus);
- memcpy(G.st_irqcpu[2], G.st_irqcpu[0], sizeof(G.st_irqcpu[0][0]) * cpus * G.irqcpu_nr);
- if (display_opt(D_SOFTIRQS)) {
- memcpy(G.st_softirqcpu[2], G.st_softirqcpu[0],
- sizeof(G.st_softirqcpu[0][0]) * cpus * G.softirqcpu_nr);
- }
- current = 1;
- while (1) {
-
- pause();
-
- memset(&G.st_cpu[current][ 1], 0, sizeof(G.st_cpu[0][0]) * G.cpu_nr);
- get_localtime(&G.timestamp[current]);
-
- if (G.cpu_nr > 1) {
- G.per_cpu_uptime[current] = 0;
- get_uptime(&G.per_cpu_uptime[current]);
- }
- get_cpu_statistics(G.st_cpu[current], &G.global_uptime[current], &G.per_cpu_uptime[current]);
- if (display_opt(D_IRQ_SUM))
- get_irqs_from_stat(G.st_irq[current]);
- if (display_opt(D_IRQ_SUM | D_IRQ_CPU)) {
- int cpu;
- for (cpu = 1; cpu <= G.cpu_nr; cpu++) {
- G.st_irq[current][cpu].irq_nr = 0;
- }
-
- get_irqs_from_interrupts(PROCFS_INTERRUPTS, G.st_irqcpu,
- G.irqcpu_nr, current);
- }
- if (display_opt(D_SOFTIRQS))
- get_irqs_from_interrupts(PROCFS_SOFTIRQS,
- G.st_softirqcpu,
- G.softirqcpu_nr, current);
- write_stats(current);
- if (G.count > 0) {
- if (--G.count == 0)
- break;
- }
- current ^= 1;
- }
-
- write_stats_avg(current);
- }
- static void alloc_struct(int cpus)
- {
- int i;
- for (i = 0; i < 3; i++) {
- G.st_cpu[i] = xzalloc(sizeof(G.st_cpu[i][0]) * cpus);
- G.st_irq[i] = xzalloc(sizeof(G.st_irq[i][0]) * cpus);
- G.st_irqcpu[i] = xzalloc(sizeof(G.st_irqcpu[i][0]) * cpus * G.irqcpu_nr);
- G.st_softirqcpu[i] = xzalloc(sizeof(G.st_softirqcpu[i][0]) * cpus * G.softirqcpu_nr);
- }
- G.cpu_bitmap_len = (cpus >> 3) + 1;
- G.cpu_bitmap = xzalloc(G.cpu_bitmap_len);
- }
- static void print_header(struct tm *t)
- {
- char cur_date[16];
- struct utsname uts;
-
- uname(&uts);
- strftime(cur_date, sizeof(cur_date), "%x", t);
- printf("%s %s (%s)\t%s\t_%s_\t(%u CPU)\n",
- uts.sysname, uts.release, uts.nodename, cur_date, uts.machine, G.cpu_nr);
- }
- static int get_irqcpu_nr(const char *f, int max_irqs)
- {
- FILE *fp;
- char *line;
- unsigned linelen;
- unsigned irq;
- fp = fopen_for_read(f);
- if (!fp)
- return 0;
- linelen = INTERRUPTS_LINE + 16 * G.cpu_nr;
- line = xmalloc(linelen);
- irq = 0;
- while (fgets(line, linelen, fp)
- && irq < max_irqs
- ) {
- int p = strcspn(line, ":");
- if ((p > 0) && (p < 16))
- irq++;
- }
- fclose(fp);
- free(line);
- return irq;
- }
- int mpstat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int mpstat_main(int UNUSED_PARAM argc, char **argv)
- {
- char *opt_irq_fmt;
- char *opt_set_cpu;
- int i, opt;
- enum {
- OPT_ALL = 1 << 0,
- OPT_INTS = 1 << 1,
- OPT_SETCPU = 1 << 2,
- OPT_UTIL = 1 << 3,
- };
-
- setbuf(stdout, NULL);
- INIT_G();
- G.interval = -1;
-
- G.cpu_nr = get_cpu_count();
-
- G.hz = bb_clk_tck();
-
- G.irqcpu_nr = get_irqcpu_nr(PROCFS_INTERRUPTS, NR_IRQS) + NR_IRQCPU_PREALLOC;
-
- G.softirqcpu_nr = get_irqcpu_nr(PROCFS_SOFTIRQS, NR_IRQS) + NR_IRQCPU_PREALLOC;
-
- alloc_struct(G.cpu_nr + 1);
-
- opt = getopt32(argv, "AI:P:u", &opt_irq_fmt, &opt_set_cpu);
- argv += optind;
- if (*argv) {
-
- G.interval = xatoi_positive(*argv);
- G.count = -1;
- argv++;
- if (*argv) {
-
- if (G.interval == 0)
- bb_show_usage();
- G.count = xatoi_positive(*argv);
-
-
- }
- }
- if (G.interval < 0)
- G.interval = 0;
- if (opt & OPT_ALL) {
- G.p_option = 1;
- G.options |= D_CPU + D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS;
-
- memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
- }
- if (opt & OPT_INTS) {
- static const char v[] = {
- D_IRQ_CPU, D_IRQ_SUM, D_SOFTIRQS,
- D_IRQ_SUM + D_IRQ_CPU + D_SOFTIRQS
- };
- i = index_in_strings("CPU\0SUM\0SCPU\0ALL\0", opt_irq_fmt);
- if (i == -1)
- bb_show_usage();
- G.options |= v[i];
- }
- if ((opt & OPT_UTIL)
- || G.options == 0
- ) {
- G.options |= D_CPU;
- }
- if (opt & OPT_SETCPU) {
- char *t;
- G.p_option = 1;
- for (t = strtok(opt_set_cpu, ","); t; t = strtok(NULL, ",")) {
- if (strcmp(t, "ALL") == 0) {
-
- memset(G.cpu_bitmap, 0xff, G.cpu_bitmap_len);
- } else {
-
- unsigned n = xatoi_positive(t);
- if (n >= G.cpu_nr)
- bb_error_msg_and_die("not that many processors");
- n++;
- G.cpu_bitmap[n >> 3] |= 1 << (n & 7);
- }
- }
- }
- if (!G.p_option)
-
- G.cpu_bitmap[0] = 1;
-
- get_localtime(&G.timestamp[0]);
-
- print_header(&G.timestamp[0]);
-
- main_loop();
- if (ENABLE_FEATURE_CLEAN_UP) {
-
- for (i = 0; i < 3; i++) {
- free(G.st_cpu[i]);
- free(G.st_irq[i]);
- free(G.st_irqcpu[i]);
- free(G.st_softirqcpu[i]);
- }
- free(G.cpu_bitmap);
- free(&G);
- }
- return EXIT_SUCCESS;
- }
|