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