2 * Copyright (C) 2008 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.
26 #define MAX_FILENAME 64
28 const char *EXPECTED_VERSION = "Latency Top version : v0.1\n";
29 const char *SYSCTL_FILE = "/proc/sys/kernel/latencytop";
30 const char *GLOBAL_STATS_FILE = "/proc/latency_stats";
31 const char *THREAD_STATS_FILE_FORMAT = "/proc/%d/task/%d/latency";
33 struct latency_entry {
34 struct latency_entry *next;
38 char reason[MAX_LINE];
41 static inline void check_latencytop() { }
43 static struct latency_entry *read_global_stats(struct latency_entry *list, int erase);
44 static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid);
45 static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal);
47 static struct latency_entry *alloc_latency_entry(void);
48 static void free_latency_entry(struct latency_entry *e);
50 static void set_latencytop(int on);
51 static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list);
52 static void erase_latency_file(FILE *f);
54 static struct latency_entry *find_latency_entry(struct latency_entry *e, char *reason);
55 static void print_latency_entries(struct latency_entry *head);
57 static void signal_handler(int sig);
58 static void disable_latencytop(void);
60 static int numcmp(const long long a, const long long b);
61 static int lat_cmp(const void *a, const void *b);
63 static void clear_screen(void);
64 static void usage(const char *cmd);
66 struct latency_entry *free_entries;
68 int main(int argc, char *argv[]) {
69 struct latency_entry *e;
70 int delay, iterations;
79 for (i = 1; i < argc; i++) {
80 if (!strcmp(argv[i], "-d")) {
82 fprintf(stderr, "Option -d expects an argument.\n");
85 delay = atoi(argv[++i]);
88 if (!strcmp(argv[i], "-n")) {
90 fprintf(stderr, "Option -n expects an argument.\n");
93 iterations = atoi(argv[++i]);
96 if (!strcmp(argv[i], "-h")) {
100 if (!strcmp(argv[i], "-p")) {
102 fprintf(stderr, "Option -p expects an argument.\n");
105 pid = atoi(argv[++i]);
108 if (!strcmp(argv[i], "-t")) {
110 fprintf(stderr, "Option -t expects an argument.\n");
113 tid = atoi(argv[++i]);
116 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
122 fprintf(stderr, "If you provide a thread ID with -t, you must provide a process ID with -p.\n");
130 signal(SIGINT, &signal_handler);
131 signal(SIGTERM, &signal_handler);
133 atexit(&disable_latencytop);
140 while ((iterations == 0) || (count++ < iterations)) {
147 e = read_thread_stats(e, erase, pid, tid, 1);
149 e = read_process_stats(e, erase, pid);
152 e = read_global_stats(e, erase);
159 printf("Latencies for thread %d in process %d:\n", tid, pid);
161 printf("Latencies for process %d:\n", pid);
164 printf("Latencies across all processes:\n");
166 print_latency_entries(e);
174 static struct latency_entry *read_global_stats(struct latency_entry *list, int erase) {
176 struct latency_entry *e;
179 f = fopen(GLOBAL_STATS_FILE, "w");
181 fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
184 fprintf(f, "erase\n");
188 f = fopen(GLOBAL_STATS_FILE, "r");
190 fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
194 e = read_latency_file(f, list);
201 static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid) {
202 char dirname[MAX_FILENAME];
205 struct latency_entry *e;
208 sprintf(dirname, "/proc/%d/task", pid);
209 dir = opendir(dirname);
211 fprintf(stderr, "Could not open task dir for process %d.\n", pid);
212 fprintf(stderr, "Perhaps the process has terminated?\n");
217 while ((ent = readdir(dir))) {
218 if (!isdigit(ent->d_name[0]))
221 tid = atoi(ent->d_name);
223 e = read_thread_stats(e, erase, pid, tid, 0);
231 static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal) {
232 char filename[MAX_FILENAME];
234 struct latency_entry *e;
236 sprintf(filename, THREAD_STATS_FILE_FORMAT, pid, tid);
239 f = fopen(filename, "w");
242 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
243 fprintf(stderr, "Perhaps the process or thread has terminated?\n");
249 fprintf(f, "erase\n");
253 f = fopen(GLOBAL_STATS_FILE, "r");
256 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
257 fprintf(stderr, "Perhaps the process or thread has terminated?\n");
264 e = read_latency_file(f, list);
271 static struct latency_entry *alloc_latency_entry(void) {
272 struct latency_entry *e;
276 free_entries = free_entries->next;
278 e = calloc(1, sizeof(struct latency_entry));
280 fprintf(stderr, "Could not allocate latency entry: %s\n", strerror(errno));
288 static void free_latency_entry(struct latency_entry *e) {
289 e->next = free_entries;
293 static struct latency_entry *find_latency_entry(struct latency_entry *head, char *reason) {
294 struct latency_entry *e;
299 if (!strcmp(e->reason, reason))
307 static void set_latencytop(int on) {
310 f = fopen(SYSCTL_FILE, "w");
312 fprintf(stderr, "Could not open %s: %s\n", SYSCTL_FILE, strerror(errno));
316 fprintf(f, "%d\n", on);
321 static void erase_latency_file(FILE *f) {
322 fprintf(f, "erase\n");
325 static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list) {
326 struct latency_entry *e, *head;
328 unsigned long count, max, total;
329 char reason[MAX_LINE];
333 if (!fgets(line, MAX_LINE, f)) {
334 fprintf(stderr, "Could not read latency file version: %s\n", strerror(errno));
338 if (strcmp(line, EXPECTED_VERSION) != 0) {
339 fprintf(stderr, "Expected version: %s\n", EXPECTED_VERSION);
340 fprintf(stderr, "But got version: %s", line);
344 while (fgets(line, MAX_LINE, f)) {
345 sscanf(line, "%ld %ld %ld %s", &count, &total, &max, reason);
346 if (max > 0 || total > 0) {
347 e = find_latency_entry(head, reason);
354 e = alloc_latency_entry();
358 strcpy(e->reason, reason);
368 static void print_latency_entries(struct latency_entry *head) {
369 struct latency_entry *e, **array;
370 unsigned long average;
381 array = calloc(count, sizeof(struct latency_entry *));
383 fprintf(stderr, "Error allocating array: %s\n", strerror(errno));
386 for (i = 0; i < count; i++) {
391 qsort(array, count, sizeof(struct latency_entry *), &lat_cmp);
393 printf("%10s %10s %7s %s\n", "Maximum", "Average", "Count", "Reason");
394 for (i = 0; i < count; i++) {
396 average = e->total / e->count;
397 printf("%4lu.%02lu ms %4lu.%02lu ms %7ld %s\n",
398 e->max / 1000, (e->max % 1000) / 10,
399 average / 1000, (average % 1000) / 10,
407 static void signal_handler(int sig) {
411 static void disable_latencytop(void) {
415 static void clear_screen(void) {
419 static void usage(const char *cmd) {
420 fprintf(stderr, "Usage: %s [ -d delay ] [ -n iterations ] [ -p pid [ -t tid ] ] [ -h ]\n"
421 " -d delay Time to sleep between updates.\n"
422 " -n iterations Number of updates to show (0 = infinite).\n"
423 " -p pid Process to monitor (default is all).\n"
424 " -t tid Thread (within specified process) to monitor (default is all).\n"
425 " -h Display this help screen.\n",
429 static int numcmp(const long long a, const long long b) {
430 if (a < b) return -1;
435 static int lat_cmp(const void *a, const void *b) {
436 const struct latency_entry *pa, *pb;
438 pa = (*((struct latency_entry **)a));
439 pb = (*((struct latency_entry **)b));
441 return numcmp(pb->max, pa->max);