top.c revision 4358544a3ffde4ffd1e9434b99b2a7179f05cce4
1/* 2 * Copyright (c) 2008, The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 13 * distribution. 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. 17 * 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 29 * SUCH DAMAGE. 30 */ 31 32#include <ctype.h> 33#include <dirent.h> 34#include <grp.h> 35#include <pwd.h> 36#include <stdio.h> 37#include <stdlib.h> 38#include <string.h> 39#include <sys/types.h> 40#include <unistd.h> 41 42#include <cutils/sched_policy.h> 43 44struct cpu_info { 45 long unsigned utime, ntime, stime, itime; 46 long unsigned iowtime, irqtime, sirqtime; 47}; 48 49#define PROC_NAME_LEN 64 50#define THREAD_NAME_LEN 32 51 52struct proc_info { 53 struct proc_info *next; 54 pid_t pid; 55 pid_t tid; 56 uid_t uid; 57 gid_t gid; 58 char name[PROC_NAME_LEN]; 59 char tname[THREAD_NAME_LEN]; 60 char state; 61 long unsigned utime; 62 long unsigned stime; 63 long unsigned delta_utime; 64 long unsigned delta_stime; 65 long unsigned delta_time; 66 long vss; 67 long rss; 68 int prs; 69 int num_threads; 70 char policy[32]; 71}; 72 73struct proc_list { 74 struct proc_info **array; 75 int size; 76}; 77 78#define die(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); } 79 80#define INIT_PROCS 50 81#define THREAD_MULT 8 82static struct proc_info **old_procs, **new_procs; 83static int num_old_procs, num_new_procs; 84static struct proc_info *free_procs; 85static int num_used_procs, num_free_procs; 86 87static int max_procs, delay, iterations, threads; 88 89static struct cpu_info old_cpu, new_cpu; 90 91static struct proc_info *alloc_proc(void); 92static void free_proc(struct proc_info *proc); 93static void read_procs(void); 94static int read_stat(char *filename, struct proc_info *proc); 95static void read_policy(int pid, struct proc_info *proc); 96static void add_proc(int proc_num, struct proc_info *proc); 97static int read_cmdline(char *filename, struct proc_info *proc); 98static int read_status(char *filename, struct proc_info *proc); 99static void print_procs(void); 100static struct proc_info *find_old_proc(pid_t pid, pid_t tid); 101static void free_old_procs(void); 102static int (*proc_cmp)(const void *a, const void *b); 103static int proc_cpu_cmp(const void *a, const void *b); 104static int proc_vss_cmp(const void *a, const void *b); 105static int proc_rss_cmp(const void *a, const void *b); 106static int proc_thr_cmp(const void *a, const void *b); 107static int numcmp(long long a, long long b); 108static void usage(char *cmd); 109 110int top_main(int argc, char *argv[]) { 111 int i; 112 113 num_used_procs = num_free_procs = 0; 114 115 max_procs = 0; 116 delay = 3; 117 iterations = -1; 118 proc_cmp = &proc_cpu_cmp; 119 for (i = 1; i < argc; i++) { 120 if (!strcmp(argv[i], "-m")) { 121 if (i + 1 >= argc) { 122 fprintf(stderr, "Option -m expects an argument.\n"); 123 usage(argv[0]); 124 exit(EXIT_FAILURE); 125 } 126 max_procs = atoi(argv[++i]); 127 continue; 128 } 129 if (!strcmp(argv[i], "-n")) { 130 if (i + 1 >= argc) { 131 fprintf(stderr, "Option -n expects an argument.\n"); 132 usage(argv[0]); 133 exit(EXIT_FAILURE); 134 } 135 iterations = atoi(argv[++i]); 136 continue; 137 } 138 if (!strcmp(argv[i], "-d")) { 139 if (i + 1 >= argc) { 140 fprintf(stderr, "Option -d expects an argument.\n"); 141 usage(argv[0]); 142 exit(EXIT_FAILURE); 143 } 144 delay = atoi(argv[++i]); 145 continue; 146 } 147 if (!strcmp(argv[i], "-s")) { 148 if (i + 1 >= argc) { 149 fprintf(stderr, "Option -s expects an argument.\n"); 150 usage(argv[0]); 151 exit(EXIT_FAILURE); 152 } 153 ++i; 154 if (!strcmp(argv[i], "cpu")) { proc_cmp = &proc_cpu_cmp; continue; } 155 if (!strcmp(argv[i], "vss")) { proc_cmp = &proc_vss_cmp; continue; } 156 if (!strcmp(argv[i], "rss")) { proc_cmp = &proc_rss_cmp; continue; } 157 if (!strcmp(argv[i], "thr")) { proc_cmp = &proc_thr_cmp; continue; } 158 fprintf(stderr, "Invalid argument \"%s\" for option -s.\n", argv[i]); 159 exit(EXIT_FAILURE); 160 } 161 if (!strcmp(argv[i], "-t")) { threads = 1; continue; } 162 if (!strcmp(argv[i], "-h")) { 163 usage(argv[0]); 164 exit(EXIT_SUCCESS); 165 } 166 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]); 167 usage(argv[0]); 168 exit(EXIT_FAILURE); 169 } 170 171 if (threads && proc_cmp == &proc_thr_cmp) { 172 fprintf(stderr, "Sorting by threads per thread makes no sense!\n"); 173 exit(EXIT_FAILURE); 174 } 175 176 free_procs = NULL; 177 178 num_new_procs = num_old_procs = 0; 179 new_procs = old_procs = NULL; 180 181 read_procs(); 182 while ((iterations == -1) || (iterations-- > 0)) { 183 old_procs = new_procs; 184 num_old_procs = num_new_procs; 185 memcpy(&old_cpu, &new_cpu, sizeof(old_cpu)); 186 sleep(delay); 187 read_procs(); 188 print_procs(); 189 free_old_procs(); 190 } 191 192 return 0; 193} 194 195static struct proc_info *alloc_proc(void) { 196 struct proc_info *proc; 197 198 if (free_procs) { 199 proc = free_procs; 200 free_procs = free_procs->next; 201 num_free_procs--; 202 } else { 203 proc = malloc(sizeof(*proc)); 204 if (!proc) die("Could not allocate struct process_info.\n"); 205 } 206 207 num_used_procs++; 208 209 return proc; 210} 211 212static void free_proc(struct proc_info *proc) { 213 proc->next = free_procs; 214 free_procs = proc; 215 216 num_used_procs--; 217 num_free_procs++; 218} 219 220#define MAX_LINE 256 221 222static void read_procs(void) { 223 DIR *proc_dir, *task_dir; 224 struct dirent *pid_dir, *tid_dir; 225 char filename[64]; 226 FILE *file; 227 int proc_num; 228 struct proc_info *proc; 229 pid_t pid, tid; 230 231 int i; 232 233 proc_dir = opendir("/proc"); 234 if (!proc_dir) die("Could not open /proc.\n"); 235 236 new_procs = calloc(INIT_PROCS * (threads ? THREAD_MULT : 1), sizeof(struct proc_info *)); 237 num_new_procs = INIT_PROCS * (threads ? THREAD_MULT : 1); 238 239 file = fopen("/proc/stat", "r"); 240 if (!file) die("Could not open /proc/stat.\n"); 241 fscanf(file, "cpu %lu %lu %lu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime, 242 &new_cpu.itime, &new_cpu.iowtime, &new_cpu.irqtime, &new_cpu.sirqtime); 243 fclose(file); 244 245 proc_num = 0; 246 while ((pid_dir = readdir(proc_dir))) { 247 if (!isdigit(pid_dir->d_name[0])) 248 continue; 249 250 pid = atoi(pid_dir->d_name); 251 252 struct proc_info cur_proc; 253 254 if (!threads) { 255 proc = alloc_proc(); 256 257 proc->pid = proc->tid = pid; 258 259 sprintf(filename, "/proc/%d/stat", pid); 260 read_stat(filename, proc); 261 262 sprintf(filename, "/proc/%d/cmdline", pid); 263 read_cmdline(filename, proc); 264 265 sprintf(filename, "/proc/%d/status", pid); 266 read_status(filename, proc); 267 268 read_policy(pid, proc); 269 270 proc->num_threads = 0; 271 } else { 272 sprintf(filename, "/proc/%d/cmdline", pid); 273 read_cmdline(filename, &cur_proc); 274 275 sprintf(filename, "/proc/%d/status", pid); 276 read_status(filename, &cur_proc); 277 278 proc = NULL; 279 } 280 281 sprintf(filename, "/proc/%d/task", pid); 282 task_dir = opendir(filename); 283 if (!task_dir) continue; 284 285 while ((tid_dir = readdir(task_dir))) { 286 if (!isdigit(tid_dir->d_name[0])) 287 continue; 288 289 if (threads) { 290 tid = atoi(tid_dir->d_name); 291 292 proc = alloc_proc(); 293 294 proc->pid = pid; proc->tid = tid; 295 296 sprintf(filename, "/proc/%d/task/%d/stat", pid, tid); 297 read_stat(filename, proc); 298 299 read_policy(tid, proc); 300 301 strcpy(proc->name, cur_proc.name); 302 proc->uid = cur_proc.uid; 303 proc->gid = cur_proc.gid; 304 305 add_proc(proc_num++, proc); 306 } else { 307 proc->num_threads++; 308 } 309 } 310 311 closedir(task_dir); 312 313 if (!threads) 314 add_proc(proc_num++, proc); 315 } 316 317 for (i = proc_num; i < num_new_procs; i++) 318 new_procs[i] = NULL; 319 320 closedir(proc_dir); 321} 322 323static int read_stat(char *filename, struct proc_info *proc) { 324 FILE *file; 325 char buf[MAX_LINE], *open_paren, *close_paren; 326 int res, idx; 327 328 file = fopen(filename, "r"); 329 if (!file) return 1; 330 fgets(buf, MAX_LINE, file); 331 fclose(file); 332 333 /* Split at first '(' and last ')' to get process name. */ 334 open_paren = strchr(buf, '('); 335 close_paren = strrchr(buf, ')'); 336 if (!open_paren || !close_paren) return 1; 337 338 *open_paren = *close_paren = '\0'; 339 strncpy(proc->tname, open_paren + 1, THREAD_NAME_LEN); 340 proc->tname[THREAD_NAME_LEN-1] = 0; 341 342 /* Scan rest of string. */ 343 sscanf(close_paren + 1, " %c %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " 344 "%lu %lu %*d %*d %*d %*d %*d %*d %*d %lu %ld " 345 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %d", 346 &proc->state, &proc->utime, &proc->stime, &proc->vss, &proc->rss, &proc->prs); 347 348 return 0; 349} 350 351static void add_proc(int proc_num, struct proc_info *proc) { 352 int i; 353 354 if (proc_num >= num_new_procs) { 355 new_procs = realloc(new_procs, 2 * num_new_procs * sizeof(struct proc_info *)); 356 if (!new_procs) die("Could not expand procs array.\n"); 357 for (i = num_new_procs; i < 2 * num_new_procs; i++) 358 new_procs[i] = NULL; 359 num_new_procs = 2 * num_new_procs; 360 } 361 new_procs[proc_num] = proc; 362} 363 364static int read_cmdline(char *filename, struct proc_info *proc) { 365 FILE *file; 366 char line[MAX_LINE]; 367 368 line[0] = '\0'; 369 file = fopen(filename, "r"); 370 if (!file) return 1; 371 fgets(line, MAX_LINE, file); 372 fclose(file); 373 if (strlen(line) > 0) { 374 strncpy(proc->name, line, PROC_NAME_LEN); 375 proc->name[PROC_NAME_LEN-1] = 0; 376 } else 377 proc->name[0] = 0; 378 return 0; 379} 380 381static void read_policy(int pid, struct proc_info *proc) { 382 SchedPolicy p; 383 if (get_sched_policy(pid, &p) < 0) 384 strcpy(proc->policy, "unk"); 385 else { 386 if (p == SP_BACKGROUND) 387 strcpy(proc->policy, "bg"); 388 else if (p == SP_FOREGROUND) 389 strcpy(proc->policy, "fg"); 390 else 391 strcpy(proc->policy, "er"); 392 } 393} 394 395static int read_status(char *filename, struct proc_info *proc) { 396 FILE *file; 397 char line[MAX_LINE]; 398 unsigned int uid, gid; 399 400 file = fopen(filename, "r"); 401 if (!file) return 1; 402 while (fgets(line, MAX_LINE, file)) { 403 sscanf(line, "Uid: %u", &uid); 404 sscanf(line, "Gid: %u", &gid); 405 } 406 fclose(file); 407 proc->uid = uid; proc->gid = gid; 408 return 0; 409} 410 411static void print_procs(void) { 412 int i; 413 struct proc_info *old_proc, *proc; 414 long unsigned total_delta_time; 415 struct passwd *user; 416 struct group *group; 417 char *user_str, user_buf[20]; 418 char *group_str, group_buf[20]; 419 420 for (i = 0; i < num_new_procs; i++) { 421 if (new_procs[i]) { 422 old_proc = find_old_proc(new_procs[i]->pid, new_procs[i]->tid); 423 if (old_proc) { 424 new_procs[i]->delta_utime = new_procs[i]->utime - old_proc->utime; 425 new_procs[i]->delta_stime = new_procs[i]->stime - old_proc->stime; 426 } else { 427 new_procs[i]->delta_utime = 0; 428 new_procs[i]->delta_stime = 0; 429 } 430 new_procs[i]->delta_time = new_procs[i]->delta_utime + new_procs[i]->delta_stime; 431 } 432 } 433 434 total_delta_time = (new_cpu.utime + new_cpu.ntime + new_cpu.stime + new_cpu.itime 435 + new_cpu.iowtime + new_cpu.irqtime + new_cpu.sirqtime) 436 - (old_cpu.utime + old_cpu.ntime + old_cpu.stime + old_cpu.itime 437 + old_cpu.iowtime + old_cpu.irqtime + old_cpu.sirqtime); 438 439 qsort(new_procs, num_new_procs, sizeof(struct proc_info *), proc_cmp); 440 441 printf("\n\n\n"); 442 printf("User %ld%%, System %ld%%, IOW %ld%%, IRQ %ld%%\n", 443 ((new_cpu.utime + new_cpu.ntime) - (old_cpu.utime + old_cpu.ntime)) * 100 / total_delta_time, 444 ((new_cpu.stime ) - (old_cpu.stime)) * 100 / total_delta_time, 445 ((new_cpu.iowtime) - (old_cpu.iowtime)) * 100 / total_delta_time, 446 ((new_cpu.irqtime + new_cpu.sirqtime) 447 - (old_cpu.irqtime + old_cpu.sirqtime)) * 100 / total_delta_time); 448 printf("User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = %ld\n", 449 new_cpu.utime - old_cpu.utime, 450 new_cpu.ntime - old_cpu.ntime, 451 new_cpu.stime - old_cpu.stime, 452 new_cpu.itime - old_cpu.itime, 453 new_cpu.iowtime - old_cpu.iowtime, 454 new_cpu.irqtime - old_cpu.irqtime, 455 new_cpu.sirqtime - old_cpu.sirqtime, 456 total_delta_time); 457 printf("\n"); 458 if (!threads) 459 printf("%5s %2s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "PR", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name"); 460 else 461 printf("%5s %5s %2s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "PR", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc"); 462 463 for (i = 0; i < num_new_procs; i++) { 464 proc = new_procs[i]; 465 466 if (!proc || (max_procs && (i >= max_procs))) 467 break; 468 user = getpwuid(proc->uid); 469 group = getgrgid(proc->gid); 470 if (user && user->pw_name) { 471 user_str = user->pw_name; 472 } else { 473 snprintf(user_buf, 20, "%d", proc->uid); 474 user_str = user_buf; 475 } 476 if (group && group->gr_name) { 477 group_str = group->gr_name; 478 } else { 479 snprintf(group_buf, 20, "%d", proc->gid); 480 group_str = group_buf; 481 } 482 if (!threads) 483 printf("%5d %2d %3ld%% %c %5d %6ldK %6ldK %3s %-8.8s %s\n", proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads, 484 proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname); 485 else 486 printf("%5d %5d %2d %3ld%% %c %6ldK %6ldK %3s %-8.8s %-15s %s\n", proc->pid, proc->tid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, 487 proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->tname, proc->name); 488 } 489} 490 491static struct proc_info *find_old_proc(pid_t pid, pid_t tid) { 492 int i; 493 494 for (i = 0; i < num_old_procs; i++) 495 if (old_procs[i] && (old_procs[i]->pid == pid) && (old_procs[i]->tid == tid)) 496 return old_procs[i]; 497 498 return NULL; 499} 500 501static void free_old_procs(void) { 502 int i; 503 504 for (i = 0; i < num_old_procs; i++) 505 if (old_procs[i]) 506 free_proc(old_procs[i]); 507 508 free(old_procs); 509} 510 511static int proc_cpu_cmp(const void *a, const void *b) { 512 struct proc_info *pa, *pb; 513 514 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); 515 516 if (!pa && !pb) return 0; 517 if (!pa) return 1; 518 if (!pb) return -1; 519 520 return -numcmp(pa->delta_time, pb->delta_time); 521} 522 523static int proc_vss_cmp(const void *a, const void *b) { 524 struct proc_info *pa, *pb; 525 526 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); 527 528 if (!pa && !pb) return 0; 529 if (!pa) return 1; 530 if (!pb) return -1; 531 532 return -numcmp(pa->vss, pb->vss); 533} 534 535static int proc_rss_cmp(const void *a, const void *b) { 536 struct proc_info *pa, *pb; 537 538 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); 539 540 if (!pa && !pb) return 0; 541 if (!pa) return 1; 542 if (!pb) return -1; 543 544 return -numcmp(pa->rss, pb->rss); 545} 546 547static int proc_thr_cmp(const void *a, const void *b) { 548 struct proc_info *pa, *pb; 549 550 pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); 551 552 if (!pa && !pb) return 0; 553 if (!pa) return 1; 554 if (!pb) return -1; 555 556 return -numcmp(pa->num_threads, pb->num_threads); 557} 558 559static int numcmp(long long a, long long b) { 560 if (a < b) return -1; 561 if (a > b) return 1; 562 return 0; 563} 564 565static void usage(char *cmd) { 566 fprintf(stderr, "Usage: %s [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]\n" 567 " -m num Maximum number of processes to display.\n" 568 " -n num Updates to show before exiting.\n" 569 " -d num Seconds to wait between updates.\n" 570 " -s col Column to sort by (cpu,vss,rss,thr).\n" 571 " -t Show threads instead of processes.\n" 572 " -h Display this help screen.\n", 573 cmd); 574} 575