2 * Copyright (c) 2008, The Android Open Source Project
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/types.h>
42 #include <cutils/sched_policy.h>
45 long unsigned utime, ntime, stime, itime;
46 long unsigned iowtime, irqtime, sirqtime;
49 #define PROC_NAME_LEN 64
50 #define THREAD_NAME_LEN 32
53 struct proc_info *next;
58 char name[PROC_NAME_LEN];
59 char tname[THREAD_NAME_LEN];
63 long unsigned delta_utime;
64 long unsigned delta_stime;
65 long unsigned delta_time;
73 struct proc_info **array;
77 #define die(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); }
81 static struct proc_info **old_procs, **new_procs;
82 static int num_old_procs, num_new_procs;
83 static struct proc_info *free_procs;
84 static int num_used_procs, num_free_procs;
86 static int max_procs, delay, iterations, threads;
88 static struct cpu_info old_cpu, new_cpu;
90 static struct proc_info *alloc_proc(void);
91 static void free_proc(struct proc_info *proc);
92 static void read_procs(void);
93 static int read_stat(char *filename, struct proc_info *proc);
94 static void read_policy(int pid, struct proc_info *proc);
95 static void add_proc(int proc_num, struct proc_info *proc);
96 static int read_cmdline(char *filename, struct proc_info *proc);
97 static int read_status(char *filename, struct proc_info *proc);
98 static void print_procs(void);
99 static struct proc_info *find_old_proc(pid_t pid, pid_t tid);
100 static void free_old_procs(void);
101 static int (*proc_cmp)(const void *a, const void *b);
102 static int proc_cpu_cmp(const void *a, const void *b);
103 static int proc_vss_cmp(const void *a, const void *b);
104 static int proc_rss_cmp(const void *a, const void *b);
105 static int proc_thr_cmp(const void *a, const void *b);
106 static int numcmp(long long a, long long b);
107 static void usage(char *cmd);
109 int top_main(int argc, char *argv[]) {
112 num_used_procs = num_free_procs = 0;
117 proc_cmp = &proc_cpu_cmp;
118 for (i = 1; i < argc; i++) {
119 if (!strcmp(argv[i], "-m")) {
121 fprintf(stderr, "Option -m expects an argument.\n");
125 max_procs = atoi(argv[++i]);
128 if (!strcmp(argv[i], "-n")) {
130 fprintf(stderr, "Option -n expects an argument.\n");
134 iterations = atoi(argv[++i]);
137 if (!strcmp(argv[i], "-d")) {
139 fprintf(stderr, "Option -d expects an argument.\n");
143 delay = atoi(argv[++i]);
146 if (!strcmp(argv[i], "-s")) {
148 fprintf(stderr, "Option -s expects an argument.\n");
153 if (!strcmp(argv[i], "cpu")) { proc_cmp = &proc_cpu_cmp; continue; }
154 if (!strcmp(argv[i], "vss")) { proc_cmp = &proc_vss_cmp; continue; }
155 if (!strcmp(argv[i], "rss")) { proc_cmp = &proc_rss_cmp; continue; }
156 if (!strcmp(argv[i], "thr")) { proc_cmp = &proc_thr_cmp; continue; }
157 fprintf(stderr, "Invalid argument \"%s\" for option -s.\n", argv[i]);
160 if (!strcmp(argv[i], "-t")) { threads = 1; continue; }
161 if (!strcmp(argv[i], "-h")) {
165 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
170 if (threads && proc_cmp == &proc_thr_cmp) {
171 fprintf(stderr, "Sorting by threads per thread makes no sense!\n");
177 num_new_procs = num_old_procs = 0;
178 new_procs = old_procs = NULL;
181 while ((iterations == -1) || (iterations-- > 0)) {
182 old_procs = new_procs;
183 num_old_procs = num_new_procs;
184 memcpy(&old_cpu, &new_cpu, sizeof(old_cpu));
194 static struct proc_info *alloc_proc(void) {
195 struct proc_info *proc;
199 free_procs = free_procs->next;
202 proc = malloc(sizeof(*proc));
203 if (!proc) die("Could not allocate struct process_info.\n");
211 static void free_proc(struct proc_info *proc) {
212 proc->next = free_procs;
221 static void read_procs(void) {
222 DIR *proc_dir, *task_dir;
223 struct dirent *pid_dir, *tid_dir;
227 struct proc_info *proc;
232 proc_dir = opendir("/proc");
233 if (!proc_dir) die("Could not open /proc.\n");
235 new_procs = calloc(INIT_PROCS * (threads ? THREAD_MULT : 1), sizeof(struct proc_info *));
236 num_new_procs = INIT_PROCS * (threads ? THREAD_MULT : 1);
238 file = fopen("/proc/stat", "r");
239 if (!file) die("Could not open /proc/stat.\n");
240 fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime,
241 &new_cpu.itime, &new_cpu.iowtime, &new_cpu.irqtime, &new_cpu.sirqtime);
245 while ((pid_dir = readdir(proc_dir))) {
246 if (!isdigit(pid_dir->d_name[0]))
249 pid = atoi(pid_dir->d_name);
251 struct proc_info cur_proc;
256 proc->pid = proc->tid = pid;
258 sprintf(filename, "/proc/%d/stat", pid);
259 read_stat(filename, proc);
261 sprintf(filename, "/proc/%d/cmdline", pid);
262 read_cmdline(filename, proc);
264 sprintf(filename, "/proc/%d/status", pid);
265 read_status(filename, proc);
267 read_policy(pid, proc);
269 proc->num_threads = 0;
271 sprintf(filename, "/proc/%d/cmdline", pid);
272 read_cmdline(filename, &cur_proc);
274 sprintf(filename, "/proc/%d/status", pid);
275 read_status(filename, &cur_proc);
280 sprintf(filename, "/proc/%d/task", pid);
281 task_dir = opendir(filename);
282 if (!task_dir) continue;
284 while ((tid_dir = readdir(task_dir))) {
285 if (!isdigit(tid_dir->d_name[0]))
289 tid = atoi(tid_dir->d_name);
293 proc->pid = pid; proc->tid = tid;
295 sprintf(filename, "/proc/%d/task/%d/stat", pid, tid);
296 read_stat(filename, proc);
298 read_policy(tid, proc);
300 strcpy(proc->name, cur_proc.name);
301 proc->uid = cur_proc.uid;
302 proc->gid = cur_proc.gid;
304 add_proc(proc_num++, proc);
313 add_proc(proc_num++, proc);
316 for (i = proc_num; i < num_new_procs; i++)
322 static int read_stat(char *filename, struct proc_info *proc) {
324 char buf[MAX_LINE], *open_paren, *close_paren;
327 file = fopen(filename, "r");
329 fgets(buf, MAX_LINE, file);
332 /* Split at first '(' and last ')' to get process name. */
333 open_paren = strchr(buf, '(');
334 close_paren = strrchr(buf, ')');
335 if (!open_paren || !close_paren) return 1;
337 *open_paren = *close_paren = '\0';
338 strncpy(proc->tname, open_paren + 1, THREAD_NAME_LEN);
339 proc->tname[THREAD_NAME_LEN-1] = 0;
341 /* Scan rest of string. */
342 sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
343 "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld",
344 &proc->state, &proc->utime, &proc->stime, &proc->vss, &proc->rss);
349 static void add_proc(int proc_num, struct proc_info *proc) {
352 if (proc_num >= num_new_procs) {
353 new_procs = realloc(new_procs, 2 * num_new_procs * sizeof(struct proc_info *));
354 if (!new_procs) die("Could not expand procs array.\n");
355 for (i = num_new_procs; i < 2 * num_new_procs; i++)
357 num_new_procs = 2 * num_new_procs;
359 new_procs[proc_num] = proc;
362 static int read_cmdline(char *filename, struct proc_info *proc) {
367 file = fopen(filename, "r");
369 fgets(line, MAX_LINE, file);
371 if (strlen(line) > 0) {
372 strncpy(proc->name, line, PROC_NAME_LEN);
373 proc->name[PROC_NAME_LEN-1] = 0;
379 static void read_policy(int pid, struct proc_info *proc) {
381 if (get_sched_policy(pid, &p) < 0)
382 strcpy(proc->policy, "unk");
384 if (p == SP_BACKGROUND)
385 strcpy(proc->policy, "bg");
386 else if (p == SP_FOREGROUND)
387 strcpy(proc->policy, "fg");
389 strcpy(proc->policy, "er");
393 static int read_status(char *filename, struct proc_info *proc) {
396 unsigned int uid, gid;
398 file = fopen(filename, "r");
400 while (fgets(line, MAX_LINE, file)) {
401 sscanf(line, "Uid: %u", &uid);
402 sscanf(line, "Gid: %u", &gid);
405 proc->uid = uid; proc->gid = gid;
409 static void print_procs(void) {
411 struct proc_info *old_proc, *proc;
412 long unsigned total_delta_time;
415 char *user_str, user_buf[20];
416 char *group_str, group_buf[20];
418 for (i = 0; i < num_new_procs; i++) {
420 old_proc = find_old_proc(new_procs[i]->pid, new_procs[i]->tid);
422 new_procs[i]->delta_utime = new_procs[i]->utime - old_proc->utime;
423 new_procs[i]->delta_stime = new_procs[i]->stime - old_proc->stime;
425 new_procs[i]->delta_utime = 0;
426 new_procs[i]->delta_stime = 0;
428 new_procs[i]->delta_time = new_procs[i]->delta_utime + new_procs[i]->delta_stime;
432 total_delta_time = (new_cpu.utime + new_cpu.ntime + new_cpu.stime + new_cpu.itime
433 + new_cpu.iowtime + new_cpu.irqtime + new_cpu.sirqtime)
434 - (old_cpu.utime + old_cpu.ntime + old_cpu.stime + old_cpu.itime
435 + old_cpu.iowtime + old_cpu.irqtime + old_cpu.sirqtime);
437 qsort(new_procs, num_new_procs, sizeof(struct proc_info *), proc_cmp);
440 printf("User %ld%%, System %ld%%, IOW %ld%%, IRQ %ld%%\n",
441 ((new_cpu.utime + new_cpu.ntime) - (old_cpu.utime + old_cpu.ntime)) * 100 / total_delta_time,
442 ((new_cpu.stime ) - (old_cpu.stime)) * 100 / total_delta_time,
443 ((new_cpu.iowtime) - (old_cpu.iowtime)) * 100 / total_delta_time,
444 ((new_cpu.irqtime + new_cpu.sirqtime)
445 - (old_cpu.irqtime + old_cpu.sirqtime)) * 100 / total_delta_time);
446 printf("User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = %ld\n",
447 new_cpu.utime - old_cpu.utime,
448 new_cpu.ntime - old_cpu.ntime,
449 new_cpu.stime - old_cpu.stime,
450 new_cpu.itime - old_cpu.itime,
451 new_cpu.iowtime - old_cpu.iowtime,
452 new_cpu.irqtime - old_cpu.irqtime,
453 new_cpu.sirqtime - old_cpu.sirqtime,
457 printf("%5s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name");
459 printf("%5s %5s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc");
461 for (i = 0; i < num_new_procs; i++) {
464 if (!proc || (max_procs && (i >= max_procs)))
466 user = getpwuid(proc->uid);
467 group = getgrgid(proc->gid);
468 if (user && user->pw_name) {
469 user_str = user->pw_name;
471 snprintf(user_buf, 20, "%d", proc->uid);
474 if (group && group->gr_name) {
475 group_str = group->gr_name;
477 snprintf(group_buf, 20, "%d", proc->gid);
478 group_str = group_buf;
481 printf("%5d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads,
482 proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname);
484 printf("%5d %5d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->delta_time * 100 / total_delta_time, proc->state,
485 proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name);
489 static struct proc_info *find_old_proc(pid_t pid, pid_t tid) {
492 for (i = 0; i < num_old_procs; i++)
493 if (old_procs[i] && (old_procs[i]->pid == pid) && (old_procs[i]->tid == tid))
499 static void free_old_procs(void) {
502 for (i = 0; i < num_old_procs; i++)
504 free_proc(old_procs[i]);
509 static int proc_cpu_cmp(const void *a, const void *b) {
510 struct proc_info *pa, *pb;
512 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);
514 if (!pa && !pb) return 0;
518 return -numcmp(pa->delta_time, pb->delta_time);
521 static int proc_vss_cmp(const void *a, const void *b) {
522 struct proc_info *pa, *pb;
524 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);
526 if (!pa && !pb) return 0;
530 return -numcmp(pa->vss, pb->vss);
533 static int proc_rss_cmp(const void *a, const void *b) {
534 struct proc_info *pa, *pb;
536 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);
538 if (!pa && !pb) return 0;
542 return -numcmp(pa->rss, pb->rss);
545 static int proc_thr_cmp(const void *a, const void *b) {
546 struct proc_info *pa, *pb;
548 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b);
550 if (!pa && !pb) return 0;
554 return -numcmp(pa->num_threads, pb->num_threads);
557 static int numcmp(long long a, long long b) {
558 if (a < b) return -1;
563 static void usage(char *cmd) {
564 fprintf(stderr, "Usage: %s [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]\n"
565 " -m num Maximum number of processes to display.\n"
566 " -n num Updates to show before exiting.\n"
567 " -d num Seconds to wait between updates.\n"
568 " -s col Column to sort by (cpu,vss,rss,thr).\n"
569 " -t Show threads instead of processes.\n"
570 " -h Display this help screen.\n",