1 /*
2 ** Copyright 2010 The Android Open Source Project
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 ** http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17
18 /* Opens /proc/sched_stat and diff's the counters.
19 Currently support version 15, modify parse() to support other
20 versions
21 */
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <errno.h>
26 #include <sys/uio.h>
27 #include <unistd.h>
28 #include <sys/time.h>
29 #include <fcntl.h>
30
31 #define MAX_CPU 2
32
33 struct cpu_stat {
34 /* sched_yield() stats */
35 unsigned int yld_count; /* sched_yield() called */
36
37 /* schedule() stats */
38 unsigned int sched_switch; /* switched to expired queue and reused it */
39 unsigned int sched_count; /* schedule() called */
40 unsigned int sched_goidle; /* schedule() left the cpu idle */
41
42 /* try_to_wake_up() stats */
43 unsigned int ttwu_count; /* try_to_wake_up() called */
44 /* try_to_wake_up() called and found the process being awakened last ran on
45 * the waking cpu */
46 unsigned int ttwu_local;
47
48 /* latency stats */
49 unsigned long long cpu_time; /* time spent running by tasks (ms) */
50 unsigned long long run_delay; /* time spent waiting to run by tasks (ms) */
51 unsigned long pcount; /* number of tasks (not necessarily unique) given */
52 };
53
54 struct cpu_stat cpu_prev[MAX_CPU];
55 struct cpu_stat cpu_delta[MAX_CPU];
56 struct cpu_stat tmp;
57
next_line(const char * b)58 static const char *next_line(const char *b) {
59 while (1) {
60 switch (*b) {
61 case '\n':
62 return b + 1;
63 case '\0':
64 return NULL;
65 }
66 b++;
67 }
68 }
print()69 static int print() {
70 int i;
71
72 printf("CPU yield() schedule() switch idle ttwu() local cpu_time wait_time timeslices\n");
73 for (i=0; i<MAX_CPU; i++) {
74 printf(" %2d %7u %10u %6u %4u %8u %5u %9llu %9llu %10lu\n",
75 i,
76 cpu_delta[i].yld_count,
77 cpu_delta[i].sched_count, cpu_delta[i].sched_switch, cpu_delta[i].sched_goidle,
78 cpu_delta[i].ttwu_count, cpu_delta[i].ttwu_local,
79 cpu_delta[i].cpu_time / 1000000, cpu_delta[i].run_delay / 1000000, cpu_delta[i].pcount);
80 }
81 return 0;
82 }
83
parse_cpu_v15(const char * b)84 static int parse_cpu_v15(const char *b) {
85 int cpu;
86
87 if (sscanf(b, "cpu%d %u %u %u %u %u %u %llu %llu %lu\n",
88 &cpu, &tmp.yld_count,
89 &tmp.sched_switch, &tmp.sched_count, &tmp.sched_goidle,
90 &tmp.ttwu_count, &tmp.ttwu_local,
91 &tmp.cpu_time, &tmp.run_delay, &tmp.pcount) != 10) {
92 printf("Could not parse %s\n", b);
93 return -1;
94 }
95
96 cpu_delta[cpu].yld_count = tmp.yld_count - cpu_prev[cpu].yld_count;
97 cpu_delta[cpu].sched_switch = tmp.sched_switch - cpu_prev[cpu].sched_switch;
98 cpu_delta[cpu].sched_count = tmp.sched_count - cpu_prev[cpu].sched_count;
99 cpu_delta[cpu].sched_goidle = tmp.sched_goidle - cpu_prev[cpu].sched_goidle;
100 cpu_delta[cpu].ttwu_count = tmp.ttwu_count - cpu_prev[cpu].ttwu_count;
101 cpu_delta[cpu].ttwu_local = tmp.ttwu_local - cpu_prev[cpu].ttwu_local;
102 cpu_delta[cpu].cpu_time = tmp.cpu_time - cpu_prev[cpu].cpu_time;
103 cpu_delta[cpu].run_delay = tmp.run_delay - cpu_prev[cpu].run_delay;
104 cpu_delta[cpu].pcount = tmp.pcount - cpu_prev[cpu].pcount;
105
106 cpu_prev[cpu] = tmp;
107 return 0;
108 }
109
110
parse(const char * b)111 static int parse(const char *b) {
112 unsigned int version;
113 unsigned long long ts;
114
115 if (sscanf(b, "version %u\n", &version) != 1) {
116 printf("Could not parse version\n");
117 return -1;
118 }
119 switch (version) {
120 case 15:
121 b = next_line(b);
122 if (!b || sscanf(b, "timestamp %llu\n", &ts) != 1) {
123 printf("Could not parse timestamp\n");
124 return -1;
125 }
126 while (1) {
127 b = next_line(b);
128 if (!b) break;
129 if (b[0] == 'c') {
130 if (parse_cpu_v15(b)) return -1;
131 }
132 }
133 break;
134 default:
135 printf("Can not handle version %u\n", version);
136 return -1;
137 }
138 return 0;
139 }
140
main(int argc,char ** argv)141 int main(int argc, char **argv) {
142 int i;
143 int fd;
144 char buf[4096];
145
146 while (1) {
147 fd = open("/proc/schedstat", O_RDONLY);
148 if (fd < 0) return -1;
149 i = read(fd, buf, sizeof(buf) - 1);
150 close(fd);
151 buf[i] = '\0';
152 if (parse(buf)) return -1;
153 print();
154 sleep(1);
155 }
156 return 0;
157 }
158