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.
27 #define MAX_FILENAME 64
29 const char *EXPECTED_VERSION = "Latency Top version : v0.1\n";
30 const char *SYSCTL_FILE = "/proc/sys/kernel/latencytop";
31 const char *GLOBAL_STATS_FILE = "/proc/latency_stats";
32 const char *THREAD_STATS_FILE_FORMAT = "/proc/%d/task/%d/latency";
34 struct latency_entry {
35 struct latency_entry *next;
39 char reason[MAX_LINE];
42 static inline void check_latencytop() { }
44 static struct latency_entry *read_global_stats(struct latency_entry *list, int erase);
45 static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid);
46 static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal);
48 static struct latency_entry *alloc_latency_entry(void);
49 static void free_latency_entry(struct latency_entry *e);
51 static void set_latencytop(int on);
52 static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list);
53 static void erase_latency_file(FILE *f);
55 static struct latency_entry *find_latency_entry(struct latency_entry *e, char *reason);
56 static void print_latency_entries(struct latency_entry *head);
58 static void signal_handler(int sig);
59 static void disable_latencytop(void);
61 static int numcmp(const long long a, const long long b);
62 static int lat_cmp(const void *a, const void *b);
64 static void clear_screen(void);
65 static void usage(const char *cmd);
67 struct latency_entry *free_entries;
69 int main(int argc, char *argv[]) {
70 struct latency_entry *e;
71 int delay, iterations;
80 for (i = 1; i < argc; i++) {
81 if (!strcmp(argv[i], "-d")) {
83 fprintf(stderr, "Option -d expects an argument.\n");
86 delay = atoi(argv[++i]);
89 if (!strcmp(argv[i], "-n")) {
91 fprintf(stderr, "Option -n expects an argument.\n");
94 iterations = atoi(argv[++i]);
97 if (!strcmp(argv[i], "-h")) {
101 if (!strcmp(argv[i], "-p")) {
103 fprintf(stderr, "Option -p expects an argument.\n");
106 pid = atoi(argv[++i]);
109 if (!strcmp(argv[i], "-t")) {
111 fprintf(stderr, "Option -t expects an argument.\n");
114 tid = atoi(argv[++i]);
117 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
123 fprintf(stderr, "If you provide a thread ID with -t, you must provide a process ID with -p.\n");
131 signal(SIGINT, &signal_handler);
132 signal(SIGTERM, &signal_handler);
134 atexit(&disable_latencytop);
141 while ((iterations == 0) || (count++ < iterations)) {
148 e = read_thread_stats(e, erase, pid, tid, 1);
150 e = read_process_stats(e, erase, pid);
153 e = read_global_stats(e, erase);
160 printf("Latencies for thread %d in process %d:\n", tid, pid);
162 printf("Latencies for process %d:\n", pid);
165 printf("Latencies across all processes:\n");
167 print_latency_entries(e);
175 static struct latency_entry *read_global_stats(struct latency_entry *list, int erase) {
177 struct latency_entry *e;
180 f = fopen(GLOBAL_STATS_FILE, "w");
182 fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
185 fprintf(f, "erase\n");
189 f = fopen(GLOBAL_STATS_FILE, "r");
191 fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
195 e = read_latency_file(f, list);
202 static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid) {
203 char dirname[MAX_FILENAME];
206 struct latency_entry *e;
209 sprintf(dirname, "/proc/%d/task", pid);
210 dir = opendir(dirname);
212 fprintf(stderr, "Could not open task dir for process %d.\n", pid);
213 fprintf(stderr, "Perhaps the process has terminated?\n");
218 while ((ent = readdir(dir))) {
219 if (!isdigit(ent->d_name[0]))
222 tid = atoi(ent->d_name);
224 e = read_thread_stats(e, erase, pid, tid, 0);
232 static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal) {
233 char filename[MAX_FILENAME];
235 struct latency_entry *e;
237 sprintf(filename, THREAD_STATS_FILE_FORMAT, pid, tid);
240 f = fopen(filename, "w");
243 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
244 fprintf(stderr, "Perhaps the process or thread has terminated?\n");
250 fprintf(f, "erase\n");
254 f = fopen(GLOBAL_STATS_FILE, "r");
257 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
258 fprintf(stderr, "Perhaps the process or thread has terminated?\n");
265 e = read_latency_file(f, list);
272 static struct latency_entry *alloc_latency_entry(void) {
273 struct latency_entry *e;
277 free_entries = free_entries->next;
279 e = calloc(1, sizeof(struct latency_entry));
281 fprintf(stderr, "Could not allocate latency entry: %s\n", strerror(errno));
289 static void free_latency_entry(struct latency_entry *e) {
290 e->next = free_entries;
294 static struct latency_entry *find_latency_entry(struct latency_entry *head, char *reason) {
295 struct latency_entry *e;
300 if (!strcmp(e->reason, reason))
308 static void set_latencytop(int on) {
311 f = fopen(SYSCTL_FILE, "w");
313 fprintf(stderr, "Could not open %s: %s\n", SYSCTL_FILE, strerror(errno));
317 fprintf(f, "%d\n", on);
322 static void erase_latency_file(FILE *f) {
323 fprintf(f, "erase\n");
326 static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list) {
327 struct latency_entry *e, *head;
329 unsigned long count, max, total;
330 char reason[MAX_LINE];
334 if (!fgets(line, MAX_LINE, f)) {
335 fprintf(stderr, "Could not read latency file version: %s\n", strerror(errno));
339 if (strcmp(line, EXPECTED_VERSION) != 0) {
340 fprintf(stderr, "Expected version: %s\n", EXPECTED_VERSION);
341 fprintf(stderr, "But got version: %s", line);
345 while (fgets(line, MAX_LINE, f)) {
346 sscanf(line, "%ld %ld %ld %s", &count, &total, &max, reason);
347 if (max > 0 || total > 0) {
348 e = find_latency_entry(head, reason);
355 e = alloc_latency_entry();
359 strcpy(e->reason, reason);
369 static void print_latency_entries(struct latency_entry *head) {
370 struct latency_entry *e, **array;
371 unsigned long average;
382 array = calloc(count, sizeof(struct latency_entry *));
384 fprintf(stderr, "Error allocating array: %s\n", strerror(errno));
387 for (i = 0; i < count; i++) {
392 qsort(array, count, sizeof(struct latency_entry *), &lat_cmp);
394 printf("%10s %10s %7s %s\n", "Maximum", "Average", "Count", "Reason");
395 for (i = 0; i < count; i++) {
397 average = e->total / e->count;
398 printf("%4lu.%02lu ms %4lu.%02lu ms %7ld %s\n",
399 e->max / 1000, (e->max % 1000) / 10,
400 average / 1000, (average % 1000) / 10,
408 static void signal_handler(int sig) {
412 static void disable_latencytop(void) {
416 static void clear_screen(void) {
420 static void usage(const char *cmd) {
421 fprintf(stderr, "Usage: %s [ -d delay ] [ -n iterations ] [ -p pid [ -t tid ] ] [ -h ]\n"
422 " -d delay Time to sleep between updates.\n"
423 " -n iterations Number of updates to show (0 = infinite).\n"
424 " -p pid Process to monitor (default is all).\n"
425 " -t tid Thread (within specified process) to monitor (default is all).\n"
426 " -h Display this help screen.\n",
430 static int numcmp(const long long a, const long long b) {
431 if (a < b) return -1;
436 static int lat_cmp(const void *a, const void *b) {
437 const struct latency_entry *pa, *pb;
439 pa = (*((struct latency_entry **)a));
440 pb = (*((struct latency_entry **)b));
442 return numcmp(pb->max, pa->max);