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);
50 static void set_latencytop(int on);
51 static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list);
53 static struct latency_entry *find_latency_entry(struct latency_entry *e, char *reason);
54 static void print_latency_entries(struct latency_entry *head);
56 static void signal_handler(int sig);
57 static void disable_latencytop(void);
59 static int numcmp(const long long a, const long long b);
60 static int lat_cmp(const void *a, const void *b);
62 static void clear_screen(void);
63 static void usage(const char *cmd);
65 struct latency_entry *free_entries;
67 int main(int argc, char *argv[]) {
68 struct latency_entry *e;
69 int delay, iterations;
78 for (i = 1; i < argc; i++) {
79 if (!strcmp(argv[i], "-d")) {
81 fprintf(stderr, "Option -d expects an argument.\n");
84 delay = atoi(argv[++i]);
87 if (!strcmp(argv[i], "-n")) {
89 fprintf(stderr, "Option -n expects an argument.\n");
92 iterations = atoi(argv[++i]);
95 if (!strcmp(argv[i], "-h")) {
99 if (!strcmp(argv[i], "-p")) {
101 fprintf(stderr, "Option -p expects an argument.\n");
104 pid = atoi(argv[++i]);
107 if (!strcmp(argv[i], "-t")) {
109 fprintf(stderr, "Option -t expects an argument.\n");
112 tid = atoi(argv[++i]);
115 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
121 fprintf(stderr, "If you provide a thread ID with -t, you must provide a process ID with -p.\n");
129 signal(SIGINT, &signal_handler);
130 signal(SIGTERM, &signal_handler);
132 atexit(&disable_latencytop);
139 while ((iterations == 0) || (count++ < iterations)) {
146 e = read_thread_stats(e, erase, pid, tid, 1);
148 e = read_process_stats(e, erase, pid);
151 e = read_global_stats(e, erase);
158 printf("Latencies for thread %d in process %d:\n", tid, pid);
160 printf("Latencies for process %d:\n", pid);
163 printf("Latencies across all processes:\n");
165 print_latency_entries(e);
173 static struct latency_entry *read_global_stats(struct latency_entry *list, int erase) {
175 struct latency_entry *e;
178 f = fopen(GLOBAL_STATS_FILE, "w");
180 fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
183 fprintf(f, "erase\n");
187 f = fopen(GLOBAL_STATS_FILE, "r");
189 fprintf(stderr, "Could not open global latency stats file: %s\n", strerror(errno));
193 e = read_latency_file(f, list);
200 static struct latency_entry *read_process_stats(struct latency_entry *list, int erase, int pid) {
201 char dirname[MAX_FILENAME];
204 struct latency_entry *e;
207 sprintf(dirname, "/proc/%d/task", pid);
208 dir = opendir(dirname);
210 fprintf(stderr, "Could not open task dir for process %d.\n", pid);
211 fprintf(stderr, "Perhaps the process has terminated?\n");
216 while ((ent = readdir(dir))) {
217 if (!isdigit(ent->d_name[0]))
220 tid = atoi(ent->d_name);
222 e = read_thread_stats(e, erase, pid, tid, 0);
230 static struct latency_entry *read_thread_stats(struct latency_entry *list, int erase, int pid, int tid, int fatal) {
231 char filename[MAX_FILENAME];
233 struct latency_entry *e;
235 sprintf(filename, THREAD_STATS_FILE_FORMAT, pid, tid);
238 f = fopen(filename, "w");
241 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
242 fprintf(stderr, "Perhaps the process or thread has terminated?\n");
248 fprintf(f, "erase\n");
252 f = fopen(GLOBAL_STATS_FILE, "r");
255 fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
256 fprintf(stderr, "Perhaps the process or thread has terminated?\n");
263 e = read_latency_file(f, list);
270 static struct latency_entry *alloc_latency_entry(void) {
271 struct latency_entry *e;
275 free_entries = free_entries->next;
277 e = calloc(1, sizeof(struct latency_entry));
279 fprintf(stderr, "Could not allocate latency entry: %s\n", strerror(errno));
287 static struct latency_entry *find_latency_entry(struct latency_entry *head, char *reason) {
288 struct latency_entry *e;
293 if (!strcmp(e->reason, reason))
301 static void set_latencytop(int on) {
304 f = fopen(SYSCTL_FILE, "w");
306 fprintf(stderr, "Could not open %s: %s\n", SYSCTL_FILE, strerror(errno));
310 fprintf(f, "%d\n", on);
315 static struct latency_entry *read_latency_file(FILE *f, struct latency_entry *list) {
316 struct latency_entry *e, *head;
318 unsigned long count, max, total;
319 char reason[MAX_LINE];
323 if (!fgets(line, MAX_LINE, f)) {
324 fprintf(stderr, "Could not read latency file version: %s\n", strerror(errno));
328 if (strcmp(line, EXPECTED_VERSION) != 0) {
329 fprintf(stderr, "Expected version: %s\n", EXPECTED_VERSION);
330 fprintf(stderr, "But got version: %s", line);
334 while (fgets(line, MAX_LINE, f)) {
335 sscanf(line, "%ld %ld %ld %s", &count, &total, &max, reason);
336 if (max > 0 || total > 0) {
337 e = find_latency_entry(head, reason);
344 e = alloc_latency_entry();
348 strcpy(e->reason, reason);
358 static void print_latency_entries(struct latency_entry *head) {
359 struct latency_entry *e, **array;
360 unsigned long average;
371 array = calloc(count, sizeof(struct latency_entry *));
373 fprintf(stderr, "Error allocating array: %s\n", strerror(errno));
376 for (i = 0; i < count; i++) {
381 qsort(array, count, sizeof(struct latency_entry *), &lat_cmp);
383 printf("%10s %10s %7s %s\n", "Maximum", "Average", "Count", "Reason");
384 for (i = 0; i < count; i++) {
386 average = e->total / e->count;
387 printf("%4lu.%02lu ms %4lu.%02lu ms %7ld %s\n",
388 e->max / 1000, (e->max % 1000) / 10,
389 average / 1000, (average % 1000) / 10,
397 static void signal_handler(int sig) {
401 static void disable_latencytop(void) {
405 static void clear_screen(void) {
409 static void usage(const char *cmd) {
410 fprintf(stderr, "Usage: %s [ -d delay ] [ -n iterations ] [ -p pid [ -t tid ] ] [ -h ]\n"
411 " -d delay Time to sleep between updates.\n"
412 " -n iterations Number of updates to show (0 = infinite).\n"
413 " -p pid Process to monitor (default is all).\n"
414 " -t tid Thread (within specified process) to monitor (default is all).\n"
415 " -h Display this help screen.\n",
419 static int numcmp(const long long a, const long long b) {
420 if (a < b) return -1;
425 static int lat_cmp(const void *a, const void *b) {
426 const struct latency_entry *pa, *pb;
428 pa = (*((struct latency_entry **)a));
429 pb = (*((struct latency_entry **)b));
431 return numcmp(pb->max, pa->max);