Home | History | Annotate | Download | only in cpustats
      1 /*
      2  * Copyright (c) 2012, The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *  * Neither the name of Google, Inc. nor the names of its contributors
     15  *    may be used to endorse or promote products derived from this
     16  *    software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     22  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <unistd.h>
     35 
     36 #define MAX_BUF_SIZE 64
     37 
     38 struct freq_info {
     39     unsigned freq;
     40     long unsigned time;
     41 };
     42 
     43 struct cpu_info {
     44     long unsigned utime, ntime, stime, itime, iowtime, irqtime, sirqtime;
     45     struct freq_info *freqs;
     46     int freq_count;
     47 };
     48 
     49 #define die(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); }
     50 
     51 static struct cpu_info old_total_cpu, new_total_cpu, *old_cpus, *new_cpus;
     52 static int cpu_count, delay, iterations;
     53 static char minimal, aggregate_freq_stats;
     54 
     55 static int get_cpu_count();
     56 static int get_cpu_count_from_file(char *filename);
     57 static long unsigned get_cpu_total_time(struct cpu_info *cpu);
     58 static int get_freq_scales_count(int cpu);
     59 static void print_stats();
     60 static void print_cpu_stats(char *label, struct cpu_info *new_cpu, struct cpu_info *old_cpu,
     61         char print_freq);
     62 static void print_freq_stats(struct cpu_info *new_cpu, struct cpu_info *old_cpu);
     63 static void read_stats();
     64 static void read_freq_stats(int cpu);
     65 static char should_aggregate_freq_stats();
     66 static char should_print_freq_stats();
     67 static void usage(char *cmd);
     68 
     69 int main(int argc, char *argv[]) {
     70     struct cpu_info *tmp_cpus, tmp_total_cpu;
     71     int i, freq_count;
     72 
     73     delay = 3;
     74     iterations = -1;
     75     minimal = 0;
     76     aggregate_freq_stats = 0;
     77 
     78     for (i = 0; i < argc; i++) {
     79         if (!strcmp(argv[i], "-n")) {
     80             if (i + 1 >= argc) {
     81                 fprintf(stderr, "Option -n expects an argument.\n");
     82                 usage(argv[0]);
     83                 exit(EXIT_FAILURE);
     84             }
     85             iterations = atoi(argv[++i]);
     86             continue;
     87         }
     88         if (!strcmp(argv[i], "-d")) {
     89             if (i + 1 >= argc) {
     90                 fprintf(stderr, "Option -d expects an argument.\n");
     91                 usage(argv[0]);
     92                 exit(EXIT_FAILURE);
     93             }
     94             delay = atoi(argv[++i]);
     95             continue;
     96         }
     97         if (!strcmp(argv[i], "-m")) {
     98             minimal = 1;
     99         }
    100         if (!strcmp(argv[i], "-h")) {
    101             usage(argv[0]);
    102             exit(EXIT_SUCCESS);
    103         }
    104     }
    105 
    106     cpu_count = get_cpu_count();
    107 
    108     old_cpus = malloc(sizeof(struct cpu_info) * cpu_count);
    109     if (!old_cpus) die("Could not allocate struct cpu_info\n");
    110     new_cpus = malloc(sizeof(struct cpu_info) * cpu_count);
    111     if (!new_cpus) die("Could not allocate struct cpu_info\n");
    112 
    113     for (i = 0; i < cpu_count; i++) {
    114         old_cpus[i].freq_count = new_cpus[i].freq_count = get_freq_scales_count(i);
    115         new_cpus[i].freqs = malloc(sizeof(struct freq_info) * new_cpus[i].freq_count);
    116         if (!new_cpus[i].freqs) die("Could not allocate struct freq_info\n");
    117         old_cpus[i].freqs = malloc(sizeof(struct freq_info) * old_cpus[i].freq_count);
    118         if (!old_cpus[i].freqs) die("Could not allocate struct freq_info\n");
    119     }
    120 
    121     // Read stats without aggregating freq stats in the total cpu
    122     read_stats();
    123 
    124     aggregate_freq_stats = should_aggregate_freq_stats();
    125     if (aggregate_freq_stats) {
    126         old_total_cpu.freq_count = new_total_cpu.freq_count = new_cpus[0].freq_count;
    127         new_total_cpu.freqs = malloc(sizeof(struct freq_info) * new_total_cpu.freq_count);
    128         if (!new_total_cpu.freqs) die("Could not allocate struct freq_info\n");
    129         old_total_cpu.freqs = malloc(sizeof(struct freq_info) * old_total_cpu.freq_count);
    130         if (!old_total_cpu.freqs) die("Could not allocate struct freq_info\n");
    131 
    132         // Read stats again with aggregating freq stats in the total cpu
    133         read_stats();
    134     }
    135 
    136     while ((iterations == -1) || (iterations-- > 0)) {
    137         // Swap new and old cpu buffers;
    138         tmp_total_cpu = old_total_cpu;
    139         old_total_cpu = new_total_cpu;
    140         new_total_cpu = tmp_total_cpu;
    141 
    142         tmp_cpus = old_cpus;
    143         old_cpus = new_cpus;
    144         new_cpus = tmp_cpus;
    145 
    146         sleep(delay);
    147         read_stats();
    148         print_stats();
    149     }
    150 
    151     // Clean up
    152     if (aggregate_freq_stats) {
    153         free(new_total_cpu.freqs);
    154         free(old_total_cpu.freqs);
    155     }
    156     for (i = 0; i < cpu_count; i++) {
    157         free(new_cpus[i].freqs);
    158         free(old_cpus[i].freqs);
    159     }
    160     free(new_cpus);
    161     free(old_cpus);
    162 
    163     return 0;
    164 }
    165 
    166 /*
    167  * Get the number of CPUs of the system.
    168  *
    169  * Uses the two files /sys/devices/system/cpu/present and
    170  * /sys/devices/system/cpu/online to determine the number of CPUs. Expects the
    171  * format of both files to be either 0 or 0-N where N+1 is the number of CPUs.
    172  *
    173  * Exits if the present CPUs is not equal to the online CPUs
    174  */
    175 static int get_cpu_count() {
    176     int cpu_count = get_cpu_count_from_file("/sys/devices/system/cpu/present");
    177     if (cpu_count != get_cpu_count_from_file("/sys/devices/system/cpu/online")) {
    178         die("present cpus != online cpus\n");
    179     }
    180     return cpu_count;
    181 }
    182 
    183 /*
    184  * Get the number of CPUs from a given filename.
    185  */
    186 static int get_cpu_count_from_file(char *filename) {
    187     FILE *file;
    188     char line[MAX_BUF_SIZE];
    189     int cpu_count;
    190 
    191     file = fopen(filename, "r");
    192     if (!file) die("Could not open %s\n", filename);
    193     if (!fgets(line, MAX_BUF_SIZE, file)) die("Could not get %s contents\n", filename);
    194     fclose(file);
    195 
    196     if (strcmp(line, "0\n") == 0) {
    197         return 1;
    198     }
    199 
    200     if (1 == sscanf(line, "0-%d\n", &cpu_count)) {
    201         return cpu_count + 1;
    202     }
    203 
    204     die("Unexpected input in file %s (%s).\n", filename, line);
    205     return -1;
    206 }
    207 
    208 /*
    209  * Get the number of frequency states a given CPU can be scaled to.
    210  */
    211 static int get_freq_scales_count(int cpu) {
    212     FILE *file;
    213     char filename[MAX_BUF_SIZE];
    214     long unsigned freq;
    215     int count = 0;
    216 
    217     sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
    218     file = fopen(filename, "r");
    219     if (!file) die("Could not open %s\n", filename);
    220     do {
    221         freq = 0;
    222         fscanf(file, "%lu %*d\n", &freq);
    223         if (freq) count++;
    224     } while(freq);
    225     fclose(file);
    226 
    227     return count;
    228 }
    229 
    230 /*
    231  * Read the CPU and frequency stats for all cpus.
    232  */
    233 static void read_stats() {
    234     FILE *file;
    235     char scanline[MAX_BUF_SIZE];
    236     int i;
    237 
    238     file = fopen("/proc/stat", "r");
    239     if (!file) die("Could not open /proc/stat.\n");
    240     fscanf(file, "cpu  %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n",
    241            &new_total_cpu.utime, &new_total_cpu.ntime, &new_total_cpu.stime, &new_total_cpu.itime,
    242            &new_total_cpu.iowtime, &new_total_cpu.irqtime, &new_total_cpu.sirqtime);
    243     if (aggregate_freq_stats) {
    244         for (i = 0; i < new_total_cpu.freq_count; i++) {
    245             new_total_cpu.freqs[i].time = 0;
    246         }
    247     }
    248 
    249     for (i = 0; i < cpu_count; i++) {
    250         sprintf(scanline, "cpu%d %%lu %%lu %%lu %%lu %%lu %%lu %%lu %%*d %%*d %%*d\n", i);
    251         fscanf(file, scanline, &new_cpus[i].utime, &new_cpus[i].ntime, &new_cpus[i].stime,
    252                &new_cpus[i].itime, &new_cpus[i].iowtime, &new_cpus[i].irqtime,
    253                &new_cpus[i].sirqtime);
    254         read_freq_stats(i);
    255     }
    256     fclose(file);
    257 }
    258 
    259 /*
    260  * Read the frequency stats for a given cpu.
    261  */
    262 static void read_freq_stats(int cpu) {
    263     FILE *file;
    264     char filename[MAX_BUF_SIZE];
    265     int i;
    266 
    267     sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
    268     file = fopen(filename, "r");
    269     if (!file) die("Could not open %s\n", filename);
    270     for (i = 0; i < new_cpus[cpu].freq_count; i++) {
    271         fscanf(file, "%u %lu\n", &new_cpus[cpu].freqs[i].freq,
    272                &new_cpus[cpu].freqs[i].time);
    273         if (aggregate_freq_stats) {
    274             new_total_cpu.freqs[i].freq = new_cpus[cpu].freqs[i].freq;
    275             new_total_cpu.freqs[i].time += new_cpus[cpu].freqs[i].time;
    276         }
    277     }
    278     fclose(file);
    279 }
    280 
    281 /*
    282  * Get the sum of the cpu time from all categories.
    283  */
    284 static long unsigned get_cpu_total_time(struct cpu_info *cpu) {
    285     return (cpu->utime + cpu->ntime + cpu->stime + cpu->itime + cpu->iowtime + cpu->irqtime +
    286             cpu->sirqtime);
    287 }
    288 
    289 /*
    290  * Print the stats for all CPUs.
    291  */
    292 static void print_stats() {
    293     char label[8];
    294     int i, j;
    295     char print_freq;
    296 
    297     print_freq = should_print_freq_stats();
    298 
    299     print_cpu_stats("Total", &new_total_cpu, &old_total_cpu, 1);
    300     for (i = 0; i < cpu_count; i++) {
    301         sprintf(label, "cpu%d", i);
    302         print_cpu_stats(label, &new_cpus[i], &old_cpus[i], print_freq);
    303     }
    304     printf("\n");
    305 }
    306 
    307 /*
    308  * Print the stats for a single CPU.
    309  */
    310 static void print_cpu_stats(char *label, struct cpu_info *new_cpu, struct cpu_info *old_cpu,
    311         char print_freq) {
    312     long int total_delta_time;
    313 
    314     if (!minimal) {
    315         total_delta_time = get_cpu_total_time(new_cpu) - get_cpu_total_time(old_cpu);
    316         printf("%s: User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = "
    317                 "%ld\n", label,
    318                 new_cpu->utime - old_cpu->utime,
    319                 new_cpu->ntime - old_cpu->ntime,
    320                 new_cpu->stime - old_cpu->stime,
    321                 new_cpu->itime - old_cpu->itime,
    322                 new_cpu->iowtime - old_cpu->iowtime,
    323                 new_cpu->irqtime - old_cpu->irqtime,
    324                 new_cpu->sirqtime - old_cpu->sirqtime,
    325                 total_delta_time);
    326         if (print_freq) {
    327             print_freq_stats(new_cpu, old_cpu);
    328         }
    329     } else {
    330         printf("%s,%ld,%ld,%ld,%ld,%ld,%ld,%ld", label,
    331                 new_cpu->utime - old_cpu->utime,
    332                 new_cpu->ntime - old_cpu->ntime,
    333                 new_cpu->stime - old_cpu->stime,
    334                 new_cpu->itime - old_cpu->itime,
    335                 new_cpu->iowtime - old_cpu->iowtime,
    336                 new_cpu->irqtime - old_cpu->irqtime,
    337                 new_cpu->sirqtime - old_cpu->sirqtime);
    338         print_freq_stats(new_cpu, old_cpu);
    339         printf("\n");
    340     }
    341 }
    342 
    343 /*
    344  * Print the CPU stats for a single CPU.
    345  */
    346 static void print_freq_stats(struct cpu_info *new_cpu, struct cpu_info *old_cpu) {
    347     long int delta_time, total_delta_time;
    348     int i;
    349 
    350     if (new_cpu->freq_count > 0) {
    351         if (!minimal) {
    352             total_delta_time = 0;
    353             printf("  ");
    354             for (i = 0; i < new_cpu->freq_count; i++) {
    355                 delta_time = new_cpu->freqs[i].time - old_cpu->freqs[i].time;
    356                 total_delta_time += delta_time;
    357                 printf("%ukHz %ld", new_cpu->freqs[i].freq, delta_time);
    358                 if (i + 1 != new_cpu->freq_count) {
    359                     printf(" + \n  ");
    360                 } else {
    361                     printf(" = ");
    362                 }
    363             }
    364             printf("%ld\n", total_delta_time);
    365         } else {
    366             for (i = 0; i < new_cpu->freq_count; i++) {
    367                 printf(",%u,%ld", new_cpu->freqs[i].freq,
    368                         new_cpu->freqs[i].time - old_cpu->freqs[i].time);
    369             }
    370         }
    371     }
    372 }
    373 
    374 /*
    375  * Determine if frequency stats should be printed.
    376  *
    377  * If the frequency stats are different between CPUs, the stats should be
    378  * printed for each CPU, else only the aggregate frequency stats should be
    379  * printed.
    380  */
    381 static char should_print_freq_stats() {
    382     int i, j;
    383 
    384     for (i = 1; i < cpu_count; i++) {
    385         for (j = 0; j < new_cpus[i].freq_count; j++) {
    386             if (new_cpus[i].freqs[j].time - old_cpus[i].freqs[j].time !=
    387                     new_cpus[0].freqs[j].time - old_cpus[0].freqs[j].time) {
    388                 return 1;
    389             }
    390         }
    391     }
    392     return 0;
    393 }
    394 
    395 /*
    396  * Determine if the frequency stats should be aggregated.
    397  *
    398  * Only aggregate the frequency stats in the total cpu stats if the frequencies
    399  * reported by all CPUs are identical.  Must be called after read_stats() has
    400  * been called once.
    401  */
    402 static char should_aggregate_freq_stats() {
    403     int i, j;
    404 
    405     for (i = 1; i < cpu_count; i++) {
    406         if (new_cpus[i].freq_count != new_cpus[0].freq_count) {
    407             return 0;
    408         }
    409         for (j = 0; j < new_cpus[i].freq_count; j++) {
    410             if (new_cpus[i].freqs[j].freq != new_cpus[0].freqs[j].freq) {
    411                 return 0;
    412             }
    413         }
    414     }
    415 
    416     return 1;
    417 }
    418 
    419 /*
    420  * Print the usage message.
    421  */
    422 static void usage(char *cmd) {
    423     fprintf(stderr, "Usage %s [ -n iterations ] [ -d delay ] [ -c cpu ] [ -m ] [ -h ]\n"
    424             "    -n num  Updates to show before exiting.\n"
    425             "    -d num  Seconds to wait between updates.\n"
    426             "    -m      Display minimal output.\n"
    427             "    -h      Display this help screen.\n",
    428             cmd);
    429 }
    430