2 ** Copyright 2010 The Android Open Source Project
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
8 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 /* Opens /proc/sched_stat and diff's the counters.
19 Currently support version 15, modify parse() to support other
34 /* sched_yield() stats */
35 unsigned int yld_count; /* sched_yield() called */
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 */
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
46 unsigned int ttwu_local;
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 */
54 struct cpu_stat cpu_prev[MAX_CPU];
55 struct cpu_stat cpu_delta[MAX_CPU];
58 static const char *next_line(const char *b) {
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",
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);
84 static int parse_cpu_v15(const char *b) {
87 if (sscanf(b, "cpu%d %u %u %u %u %u %u %llu %llu %lu\n",
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);
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;
111 static int parse(const char *b) {
112 unsigned int version;
113 unsigned long long ts;
115 if (sscanf(b, "version %u\n", &version) != 1) {
116 printf("Could not parse version\n");
122 if (!b || sscanf(b, "timestamp %llu\n", &ts) != 1) {
123 printf("Could not parse timestamp\n");
130 if (parse_cpu_v15(b)) return -1;
135 printf("Can not handle version %u\n", version);
141 int main(int argc, char **argv) {
147 fd = open("/proc/schedstat", O_RDONLY);
148 if (fd < 0) return -1;
149 i = read(fd, buf, sizeof(buf) - 1);
152 if (parse(buf)) return -1;