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