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 <string.h>
35 #include <unistd.h>
36
37 #define MAX_BUF_SIZE 64
38
39 struct freq_info {
40 unsigned freq;
41 long unsigned time;
42 };
43
44 struct cpu_info {
45 long unsigned utime, ntime, stime, itime, iowtime, irqtime, sirqtime;
46 struct freq_info* freqs;
47 int freq_count;
48 };
49
50 #define die(...) \
51 { \
52 fprintf(stderr, __VA_ARGS__); \
53 exit(EXIT_FAILURE); \
54 }
55
56 static struct cpu_info old_total_cpu, new_total_cpu, *old_cpus, *new_cpus;
57 static int cpu_count, delay, iterations;
58 static char minimal, aggregate_freq_stats;
59
60 static int get_cpu_count();
61 static int get_cpu_count_from_file(char* filename);
62 static long unsigned get_cpu_total_time(struct cpu_info* cpu);
63 static int get_freq_scales_count(int cpu);
64 static void print_stats();
65 static void print_cpu_stats(char* label, struct cpu_info* new_cpu, struct cpu_info* old_cpu,
66 char print_freq);
67 static void print_freq_stats(struct cpu_info* new_cpu, struct cpu_info* old_cpu);
68 static void read_stats();
69 static void read_freq_stats(int cpu);
70 static char should_aggregate_freq_stats();
71 static char should_print_freq_stats();
72 static void usage(char* cmd);
73
main(int argc,char * argv[])74 int main(int argc, char* argv[]) {
75 struct cpu_info *tmp_cpus, tmp_total_cpu;
76 int i, freq_count;
77
78 delay = 3;
79 iterations = -1;
80 minimal = 0;
81 aggregate_freq_stats = 0;
82
83 for (i = 0; i < argc; i++) {
84 if (!strcmp(argv[i], "-n")) {
85 if (i + 1 >= argc) {
86 fprintf(stderr, "Option -n expects an argument.\n");
87 usage(argv[0]);
88 exit(EXIT_FAILURE);
89 }
90 iterations = atoi(argv[++i]);
91 continue;
92 }
93 if (!strcmp(argv[i], "-d")) {
94 if (i + 1 >= argc) {
95 fprintf(stderr, "Option -d expects an argument.\n");
96 usage(argv[0]);
97 exit(EXIT_FAILURE);
98 }
99 delay = atoi(argv[++i]);
100 continue;
101 }
102 if (!strcmp(argv[i], "-m")) {
103 minimal = 1;
104 }
105 if (!strcmp(argv[i], "-h")) {
106 usage(argv[0]);
107 exit(EXIT_SUCCESS);
108 }
109 }
110
111 cpu_count = get_cpu_count();
112 if (cpu_count < 1) die("Unexpected cpu count\n");
113
114 old_cpus = malloc(sizeof(struct cpu_info) * cpu_count);
115 if (!old_cpus) die("Could not allocate struct cpu_info\n");
116 new_cpus = malloc(sizeof(struct cpu_info) * cpu_count);
117 if (!new_cpus) die("Could not allocate struct cpu_info\n");
118
119 for (i = 0; i < cpu_count; i++) {
120 freq_count = get_freq_scales_count(i);
121 if (freq_count < 1) die("Unexpected frequency scale count\n");
122 old_cpus[i].freq_count = new_cpus[i].freq_count = freq_count;
123 new_cpus[i].freqs = malloc(sizeof(struct freq_info) * new_cpus[i].freq_count);
124 if (!new_cpus[i].freqs) die("Could not allocate struct freq_info\n");
125 old_cpus[i].freqs = malloc(sizeof(struct freq_info) * old_cpus[i].freq_count);
126 if (!old_cpus[i].freqs) die("Could not allocate struct freq_info\n");
127 }
128
129 // Read stats without aggregating freq stats in the total cpu
130 read_stats();
131
132 aggregate_freq_stats = should_aggregate_freq_stats();
133 if (aggregate_freq_stats) {
134 old_total_cpu.freq_count = new_total_cpu.freq_count = new_cpus[0].freq_count;
135 new_total_cpu.freqs = malloc(sizeof(struct freq_info) * new_total_cpu.freq_count);
136 if (!new_total_cpu.freqs) die("Could not allocate struct freq_info\n");
137 old_total_cpu.freqs = malloc(sizeof(struct freq_info) * old_total_cpu.freq_count);
138 if (!old_total_cpu.freqs) die("Could not allocate struct freq_info\n");
139
140 // Read stats again with aggregating freq stats in the total cpu
141 read_stats();
142 }
143
144 while ((iterations == -1) || (iterations-- > 0)) {
145 // Swap new and old cpu buffers;
146 tmp_total_cpu = old_total_cpu;
147 old_total_cpu = new_total_cpu;
148 new_total_cpu = tmp_total_cpu;
149
150 tmp_cpus = old_cpus;
151 old_cpus = new_cpus;
152 new_cpus = tmp_cpus;
153
154 sleep(delay);
155 read_stats();
156 print_stats();
157 }
158
159 // Clean up
160 if (aggregate_freq_stats) {
161 free(new_total_cpu.freqs);
162 free(old_total_cpu.freqs);
163 }
164 for (i = 0; i < cpu_count; i++) {
165 free(new_cpus[i].freqs);
166 free(old_cpus[i].freqs);
167 }
168 free(new_cpus);
169 free(old_cpus);
170
171 return 0;
172 }
173
174 /*
175 * Get the number of CPUs of the system.
176 *
177 * Uses the two files /sys/devices/system/cpu/present and
178 * /sys/devices/system/cpu/online to determine the number of CPUs. Expects the
179 * format of both files to be either 0 or 0-N where N+1 is the number of CPUs.
180 *
181 * Exits if the present CPUs is not equal to the online CPUs
182 */
get_cpu_count()183 static int get_cpu_count() {
184 int cpu_count = get_cpu_count_from_file("/sys/devices/system/cpu/present");
185 if (cpu_count != get_cpu_count_from_file("/sys/devices/system/cpu/online")) {
186 die("present cpus != online cpus\n");
187 }
188 return cpu_count;
189 }
190
191 /*
192 * Get the number of CPUs from a given filename.
193 */
get_cpu_count_from_file(char * filename)194 static int get_cpu_count_from_file(char* filename) {
195 FILE* file;
196 char line[MAX_BUF_SIZE];
197 int cpu_count;
198
199 file = fopen(filename, "r");
200 if (!file) die("Could not open %s\n", filename);
201 if (!fgets(line, MAX_BUF_SIZE, file)) die("Could not get %s contents\n", filename);
202 fclose(file);
203
204 if (strcmp(line, "0\n") == 0) {
205 return 1;
206 }
207
208 if (1 == sscanf(line, "0-%d\n", &cpu_count)) {
209 return cpu_count + 1;
210 }
211
212 die("Unexpected input in file %s (%s).\n", filename, line);
213 return -1;
214 }
215
216 /*
217 * Get the number of frequency states a given CPU can be scaled to.
218 */
get_freq_scales_count(int cpu)219 static int get_freq_scales_count(int cpu) {
220 FILE* file;
221 char filename[MAX_BUF_SIZE];
222 long unsigned freq;
223 int count = 0;
224
225 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
226 file = fopen(filename, "r");
227 if (!file) die("Could not open %s\n", filename);
228 do {
229 freq = 0;
230 fscanf(file, "%lu %*d\n", &freq);
231 if (freq) count++;
232 } while (freq);
233 fclose(file);
234
235 return count;
236 }
237
238 /*
239 * Read the CPU and frequency stats for all cpus.
240 */
read_stats()241 static void read_stats() {
242 FILE* file;
243 char scanline[MAX_BUF_SIZE];
244 int i;
245
246 file = fopen("/proc/stat", "r");
247 if (!file) die("Could not open /proc/stat.\n");
248 fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n", &new_total_cpu.utime,
249 &new_total_cpu.ntime, &new_total_cpu.stime, &new_total_cpu.itime, &new_total_cpu.iowtime,
250 &new_total_cpu.irqtime, &new_total_cpu.sirqtime);
251 if (aggregate_freq_stats) {
252 for (i = 0; i < new_total_cpu.freq_count; i++) {
253 new_total_cpu.freqs[i].time = 0;
254 }
255 }
256
257 for (i = 0; i < cpu_count; i++) {
258 sprintf(scanline, "cpu%d %%lu %%lu %%lu %%lu %%lu %%lu %%lu %%*d %%*d %%*d\n", i);
259 fscanf(file, scanline, &new_cpus[i].utime, &new_cpus[i].ntime, &new_cpus[i].stime,
260 &new_cpus[i].itime, &new_cpus[i].iowtime, &new_cpus[i].irqtime,
261 &new_cpus[i].sirqtime);
262 read_freq_stats(i);
263 }
264 fclose(file);
265 }
266
267 /*
268 * Read the frequency stats for a given cpu.
269 */
read_freq_stats(int cpu)270 static void read_freq_stats(int cpu) {
271 FILE* file;
272 char filename[MAX_BUF_SIZE];
273 int i;
274
275 sprintf(filename, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpu);
276 file = fopen(filename, "r");
277 for (i = 0; i < new_cpus[cpu].freq_count; i++) {
278 if (file) {
279 fscanf(file, "%u %lu\n", &new_cpus[cpu].freqs[i].freq, &new_cpus[cpu].freqs[i].time);
280 } else {
281 /* The CPU has been off lined for some reason */
282 new_cpus[cpu].freqs[i].freq = old_cpus[cpu].freqs[i].freq;
283 new_cpus[cpu].freqs[i].time = old_cpus[cpu].freqs[i].time;
284 }
285 if (aggregate_freq_stats) {
286 new_total_cpu.freqs[i].freq = new_cpus[cpu].freqs[i].freq;
287 new_total_cpu.freqs[i].time += new_cpus[cpu].freqs[i].time;
288 }
289 }
290 if (file) fclose(file);
291 }
292
293 /*
294 * Get the sum of the cpu time from all categories.
295 */
get_cpu_total_time(struct cpu_info * cpu)296 static long unsigned get_cpu_total_time(struct cpu_info* cpu) {
297 return (cpu->utime + cpu->ntime + cpu->stime + cpu->itime + cpu->iowtime + cpu->irqtime +
298 cpu->sirqtime);
299 }
300
301 /*
302 * Print the stats for all CPUs.
303 */
print_stats()304 static void print_stats() {
305 char label[8];
306 int i;
307 char print_freq;
308
309 print_freq = should_print_freq_stats();
310
311 print_cpu_stats("Total", &new_total_cpu, &old_total_cpu, 1);
312 for (i = 0; i < cpu_count; i++) {
313 sprintf(label, "cpu%d", i);
314 print_cpu_stats(label, &new_cpus[i], &old_cpus[i], print_freq);
315 }
316 printf("\n");
317 }
318
319 /*
320 * Print the stats for a single CPU.
321 */
print_cpu_stats(char * label,struct cpu_info * new_cpu,struct cpu_info * old_cpu,char print_freq)322 static void print_cpu_stats(char* label, struct cpu_info* new_cpu, struct cpu_info* old_cpu,
323 char print_freq) {
324 long int total_delta_time;
325
326 if (!minimal) {
327 total_delta_time = get_cpu_total_time(new_cpu) - get_cpu_total_time(old_cpu);
328 printf("%s: User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = "
329 "%ld\n",
330 label, new_cpu->utime - old_cpu->utime, new_cpu->ntime - old_cpu->ntime,
331 new_cpu->stime - old_cpu->stime, new_cpu->itime - old_cpu->itime,
332 new_cpu->iowtime - old_cpu->iowtime, new_cpu->irqtime - old_cpu->irqtime,
333 new_cpu->sirqtime - old_cpu->sirqtime, total_delta_time);
334 if (print_freq) {
335 print_freq_stats(new_cpu, old_cpu);
336 }
337 } else {
338 printf("%s,%ld,%ld,%ld,%ld,%ld,%ld,%ld", label, new_cpu->utime - old_cpu->utime,
339 new_cpu->ntime - old_cpu->ntime, new_cpu->stime - old_cpu->stime,
340 new_cpu->itime - old_cpu->itime, new_cpu->iowtime - old_cpu->iowtime,
341 new_cpu->irqtime - old_cpu->irqtime, new_cpu->sirqtime - old_cpu->sirqtime);
342 print_freq_stats(new_cpu, old_cpu);
343 printf("\n");
344 }
345 }
346
347 /*
348 * Print the CPU stats for a single CPU.
349 */
print_freq_stats(struct cpu_info * new_cpu,struct cpu_info * old_cpu)350 static void print_freq_stats(struct cpu_info* new_cpu, struct cpu_info* old_cpu) {
351 long int delta_time, total_delta_time;
352 int i;
353
354 if (new_cpu->freq_count > 0) {
355 if (!minimal) {
356 total_delta_time = 0;
357 printf(" ");
358 for (i = 0; i < new_cpu->freq_count; i++) {
359 delta_time = new_cpu->freqs[i].time - old_cpu->freqs[i].time;
360 total_delta_time += delta_time;
361 printf("%ukHz %ld", new_cpu->freqs[i].freq, delta_time);
362 if (i + 1 != new_cpu->freq_count) {
363 printf(" + \n ");
364 } else {
365 printf(" = ");
366 }
367 }
368 printf("%ld\n", total_delta_time);
369 } else {
370 for (i = 0; i < new_cpu->freq_count; i++) {
371 printf(",%u,%ld", new_cpu->freqs[i].freq,
372 new_cpu->freqs[i].time - old_cpu->freqs[i].time);
373 }
374 }
375 }
376 }
377
378 /*
379 * Determine if frequency stats should be printed.
380 *
381 * If the frequency stats are different between CPUs, the stats should be
382 * printed for each CPU, else only the aggregate frequency stats should be
383 * printed.
384 */
should_print_freq_stats()385 static char should_print_freq_stats() {
386 int i, j;
387
388 for (i = 1; i < cpu_count; i++) {
389 for (j = 0; j < new_cpus[i].freq_count; j++) {
390 if (new_cpus[i].freqs[j].time - old_cpus[i].freqs[j].time !=
391 new_cpus[0].freqs[j].time - old_cpus[0].freqs[j].time) {
392 return 1;
393 }
394 }
395 }
396 return 0;
397 }
398
399 /*
400 * Determine if the frequency stats should be aggregated.
401 *
402 * Only aggregate the frequency stats in the total cpu stats if the frequencies
403 * reported by all CPUs are identical. Must be called after read_stats() has
404 * been called once.
405 */
should_aggregate_freq_stats()406 static char should_aggregate_freq_stats() {
407 int i, j;
408
409 for (i = 1; i < cpu_count; i++) {
410 if (new_cpus[i].freq_count != new_cpus[0].freq_count) {
411 return 0;
412 }
413 for (j = 0; j < new_cpus[i].freq_count; j++) {
414 if (new_cpus[i].freqs[j].freq != new_cpus[0].freqs[j].freq) {
415 return 0;
416 }
417 }
418 }
419
420 return 1;
421 }
422
423 /*
424 * Print the usage message.
425 */
usage(char * cmd)426 static void usage(char* cmd) {
427 fprintf(stderr,
428 "Usage %s [ -n iterations ] [ -d delay ] [ -c cpu ] [ -m ] [ -h ]\n"
429 " -n num Updates to show before exiting.\n"
430 " -d num Seconds to wait between updates.\n"
431 " -m Display minimal output.\n"
432 " -h Display this help screen.\n",
433 cmd);
434 }
435