| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [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 | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 12 | *    the documentation and/or other materials provided with the | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 25 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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> | 
| Elliott Hughes | 052d78f | 2014-10-08 11:03:27 -0700 | [diff] [blame] | 35 | #include <inttypes.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 36 | #include <pwd.h> | 
|  | 37 | #include <stdio.h> | 
|  | 38 | #include <stdlib.h> | 
|  | 39 | #include <string.h> | 
|  | 40 | #include <sys/types.h> | 
|  | 41 | #include <unistd.h> | 
|  | 42 |  | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 43 | #include <cutils/sched_policy.h> | 
|  | 44 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | struct cpu_info { | 
|  | 46 | long unsigned utime, ntime, stime, itime; | 
|  | 47 | long unsigned iowtime, irqtime, sirqtime; | 
|  | 48 | }; | 
|  | 49 |  | 
|  | 50 | #define PROC_NAME_LEN 64 | 
|  | 51 | #define THREAD_NAME_LEN 32 | 
| Glenn Kasten | 86c7cc8 | 2012-03-05 16:14:39 -0800 | [diff] [blame] | 52 | #define POLICY_NAME_LEN 4 | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | struct proc_info { | 
|  | 55 | struct proc_info *next; | 
|  | 56 | pid_t pid; | 
|  | 57 | pid_t tid; | 
|  | 58 | uid_t uid; | 
|  | 59 | gid_t gid; | 
|  | 60 | char name[PROC_NAME_LEN]; | 
|  | 61 | char tname[THREAD_NAME_LEN]; | 
|  | 62 | char state; | 
| Elliott Hughes | 052d78f | 2014-10-08 11:03:27 -0700 | [diff] [blame] | 63 | uint64_t utime; | 
|  | 64 | uint64_t stime; | 
|  | 65 | uint64_t delta_utime; | 
|  | 66 | uint64_t delta_stime; | 
|  | 67 | uint64_t delta_time; | 
|  | 68 | uint64_t vss; | 
|  | 69 | uint64_t rss; | 
| Dmitry Shmidt | 4358544 | 2010-08-30 16:39:14 -0700 | [diff] [blame] | 70 | int prs; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | int num_threads; | 
| Glenn Kasten | 86c7cc8 | 2012-03-05 16:14:39 -0800 | [diff] [blame] | 72 | char policy[POLICY_NAME_LEN]; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
|  | 75 | struct proc_list { | 
|  | 76 | struct proc_info **array; | 
|  | 77 | int size; | 
|  | 78 | }; | 
|  | 79 |  | 
|  | 80 | #define die(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); } | 
|  | 81 |  | 
|  | 82 | #define INIT_PROCS 50 | 
|  | 83 | #define THREAD_MULT 8 | 
|  | 84 | static struct proc_info **old_procs, **new_procs; | 
|  | 85 | static int num_old_procs, num_new_procs; | 
|  | 86 | static struct proc_info *free_procs; | 
|  | 87 | static int num_used_procs, num_free_procs; | 
|  | 88 |  | 
|  | 89 | static int max_procs, delay, iterations, threads; | 
|  | 90 |  | 
|  | 91 | static struct cpu_info old_cpu, new_cpu; | 
|  | 92 |  | 
|  | 93 | static struct proc_info *alloc_proc(void); | 
|  | 94 | static void free_proc(struct proc_info *proc); | 
|  | 95 | static void read_procs(void); | 
|  | 96 | static int read_stat(char *filename, struct proc_info *proc); | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 97 | static void read_policy(int pid, struct proc_info *proc); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | static void add_proc(int proc_num, struct proc_info *proc); | 
|  | 99 | static int read_cmdline(char *filename, struct proc_info *proc); | 
|  | 100 | static int read_status(char *filename, struct proc_info *proc); | 
|  | 101 | static void print_procs(void); | 
|  | 102 | static struct proc_info *find_old_proc(pid_t pid, pid_t tid); | 
|  | 103 | static void free_old_procs(void); | 
|  | 104 | static int (*proc_cmp)(const void *a, const void *b); | 
|  | 105 | static int proc_cpu_cmp(const void *a, const void *b); | 
|  | 106 | static int proc_vss_cmp(const void *a, const void *b); | 
|  | 107 | static int proc_rss_cmp(const void *a, const void *b); | 
|  | 108 | static int proc_thr_cmp(const void *a, const void *b); | 
|  | 109 | static int numcmp(long long a, long long b); | 
|  | 110 | static void usage(char *cmd); | 
|  | 111 |  | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 112 | int top_main(int argc, char *argv[]) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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; | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 119 | for (int i = 1; i < argc; i++) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | } | 
| Elliott Hughes | d7bd575 | 2015-11-13 08:30:12 -0800 | [diff] [blame] | 161 | if (!strcmp(argv[i], "-H") || !strcmp(argv[i], "-t")) { threads = 1; continue; } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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(); | 
| Elliott Hughes | d7bd575 | 2015-11-13 08:30:12 -0800 | [diff] [blame] | 190 | fflush(stdout); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 | } | 
|  | 192 |  | 
|  | 193 | return 0; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | static struct proc_info *alloc_proc(void) { | 
|  | 197 | struct proc_info *proc; | 
|  | 198 |  | 
|  | 199 | if (free_procs) { | 
|  | 200 | proc = free_procs; | 
|  | 201 | free_procs = free_procs->next; | 
|  | 202 | num_free_procs--; | 
|  | 203 | } else { | 
|  | 204 | proc = malloc(sizeof(*proc)); | 
|  | 205 | if (!proc) die("Could not allocate struct process_info.\n"); | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | num_used_procs++; | 
|  | 209 |  | 
|  | 210 | return proc; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | static void free_proc(struct proc_info *proc) { | 
|  | 214 | proc->next = free_procs; | 
|  | 215 | free_procs = proc; | 
|  | 216 |  | 
|  | 217 | num_used_procs--; | 
|  | 218 | num_free_procs++; | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | #define MAX_LINE 256 | 
|  | 222 |  | 
|  | 223 | static void read_procs(void) { | 
|  | 224 | DIR *proc_dir, *task_dir; | 
|  | 225 | struct dirent *pid_dir, *tid_dir; | 
|  | 226 | char filename[64]; | 
|  | 227 | FILE *file; | 
|  | 228 | int proc_num; | 
|  | 229 | struct proc_info *proc; | 
|  | 230 | pid_t pid, tid; | 
|  | 231 |  | 
|  | 232 | int i; | 
|  | 233 |  | 
|  | 234 | proc_dir = opendir("/proc"); | 
|  | 235 | if (!proc_dir) die("Could not open /proc.\n"); | 
|  | 236 |  | 
|  | 237 | new_procs = calloc(INIT_PROCS * (threads ? THREAD_MULT : 1), sizeof(struct proc_info *)); | 
|  | 238 | num_new_procs = INIT_PROCS * (threads ? THREAD_MULT : 1); | 
|  | 239 |  | 
|  | 240 | file = fopen("/proc/stat", "r"); | 
|  | 241 | if (!file) die("Could not open /proc/stat.\n"); | 
|  | 242 | fscanf(file, "cpu  %lu %lu %lu %lu %lu %lu %lu", &new_cpu.utime, &new_cpu.ntime, &new_cpu.stime, | 
|  | 243 | &new_cpu.itime, &new_cpu.iowtime, &new_cpu.irqtime, &new_cpu.sirqtime); | 
|  | 244 | fclose(file); | 
|  | 245 |  | 
|  | 246 | proc_num = 0; | 
|  | 247 | while ((pid_dir = readdir(proc_dir))) { | 
|  | 248 | if (!isdigit(pid_dir->d_name[0])) | 
|  | 249 | continue; | 
|  | 250 |  | 
|  | 251 | pid = atoi(pid_dir->d_name); | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 252 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 253 | struct proc_info cur_proc; | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 254 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | if (!threads) { | 
|  | 256 | proc = alloc_proc(); | 
|  | 257 |  | 
|  | 258 | proc->pid = proc->tid = pid; | 
|  | 259 |  | 
|  | 260 | sprintf(filename, "/proc/%d/stat", pid); | 
|  | 261 | read_stat(filename, proc); | 
|  | 262 |  | 
|  | 263 | sprintf(filename, "/proc/%d/cmdline", pid); | 
|  | 264 | read_cmdline(filename, proc); | 
|  | 265 |  | 
|  | 266 | sprintf(filename, "/proc/%d/status", pid); | 
|  | 267 | read_status(filename, proc); | 
|  | 268 |  | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 269 | read_policy(pid, proc); | 
|  | 270 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | proc->num_threads = 0; | 
|  | 272 | } else { | 
|  | 273 | sprintf(filename, "/proc/%d/cmdline", pid); | 
|  | 274 | read_cmdline(filename, &cur_proc); | 
|  | 275 |  | 
|  | 276 | sprintf(filename, "/proc/%d/status", pid); | 
|  | 277 | read_status(filename, &cur_proc); | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 278 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 279 | proc = NULL; | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | sprintf(filename, "/proc/%d/task", pid); | 
|  | 283 | task_dir = opendir(filename); | 
|  | 284 | if (!task_dir) continue; | 
|  | 285 |  | 
|  | 286 | while ((tid_dir = readdir(task_dir))) { | 
|  | 287 | if (!isdigit(tid_dir->d_name[0])) | 
|  | 288 | continue; | 
|  | 289 |  | 
|  | 290 | if (threads) { | 
|  | 291 | tid = atoi(tid_dir->d_name); | 
|  | 292 |  | 
|  | 293 | proc = alloc_proc(); | 
|  | 294 |  | 
|  | 295 | proc->pid = pid; proc->tid = tid; | 
|  | 296 |  | 
|  | 297 | sprintf(filename, "/proc/%d/task/%d/stat", pid, tid); | 
|  | 298 | read_stat(filename, proc); | 
|  | 299 |  | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 300 | read_policy(tid, proc); | 
|  | 301 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | strcpy(proc->name, cur_proc.name); | 
|  | 303 | proc->uid = cur_proc.uid; | 
|  | 304 | proc->gid = cur_proc.gid; | 
|  | 305 |  | 
|  | 306 | add_proc(proc_num++, proc); | 
|  | 307 | } else { | 
|  | 308 | proc->num_threads++; | 
|  | 309 | } | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | closedir(task_dir); | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 313 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 314 | if (!threads) | 
|  | 315 | add_proc(proc_num++, proc); | 
|  | 316 | } | 
|  | 317 |  | 
|  | 318 | for (i = proc_num; i < num_new_procs; i++) | 
|  | 319 | new_procs[i] = NULL; | 
|  | 320 |  | 
|  | 321 | closedir(proc_dir); | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | static int read_stat(char *filename, struct proc_info *proc) { | 
|  | 325 | FILE *file; | 
|  | 326 | char buf[MAX_LINE], *open_paren, *close_paren; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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; | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 341 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 342 | /* Scan rest of string. */ | 
| Elliott Hughes | 052d78f | 2014-10-08 11:03:27 -0700 | [diff] [blame] | 343 | sscanf(close_paren + 1, | 
|  | 344 | " %c " "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " | 
|  | 345 | "%" SCNu64 | 
|  | 346 | "%" SCNu64 "%*d %*d %*d %*d %*d %*d %*d " | 
|  | 347 | "%" SCNu64 | 
|  | 348 | "%" SCNu64 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " | 
|  | 349 | "%d", | 
|  | 350 | &proc->state, | 
|  | 351 | &proc->utime, | 
|  | 352 | &proc->stime, | 
|  | 353 | &proc->vss, | 
|  | 354 | &proc->rss, | 
|  | 355 | &proc->prs); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 |  | 
|  | 357 | return 0; | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | static void add_proc(int proc_num, struct proc_info *proc) { | 
|  | 361 | int i; | 
|  | 362 |  | 
|  | 363 | if (proc_num >= num_new_procs) { | 
|  | 364 | new_procs = realloc(new_procs, 2 * num_new_procs * sizeof(struct proc_info *)); | 
|  | 365 | if (!new_procs) die("Could not expand procs array.\n"); | 
|  | 366 | for (i = num_new_procs; i < 2 * num_new_procs; i++) | 
|  | 367 | new_procs[i] = NULL; | 
|  | 368 | num_new_procs = 2 * num_new_procs; | 
|  | 369 | } | 
|  | 370 | new_procs[proc_num] = proc; | 
|  | 371 | } | 
|  | 372 |  | 
|  | 373 | static int read_cmdline(char *filename, struct proc_info *proc) { | 
|  | 374 | FILE *file; | 
|  | 375 | char line[MAX_LINE]; | 
|  | 376 |  | 
|  | 377 | line[0] = '\0'; | 
|  | 378 | file = fopen(filename, "r"); | 
|  | 379 | if (!file) return 1; | 
|  | 380 | fgets(line, MAX_LINE, file); | 
|  | 381 | fclose(file); | 
|  | 382 | if (strlen(line) > 0) { | 
|  | 383 | strncpy(proc->name, line, PROC_NAME_LEN); | 
|  | 384 | proc->name[PROC_NAME_LEN-1] = 0; | 
|  | 385 | } else | 
|  | 386 | proc->name[0] = 0; | 
|  | 387 | return 0; | 
|  | 388 | } | 
|  | 389 |  | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 390 | static void read_policy(int pid, struct proc_info *proc) { | 
|  | 391 | SchedPolicy p; | 
|  | 392 | if (get_sched_policy(pid, &p) < 0) | 
| Glenn Kasten | 86c7cc8 | 2012-03-05 16:14:39 -0800 | [diff] [blame] | 393 | strlcpy(proc->policy, "unk", POLICY_NAME_LEN); | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 394 | else { | 
| Glenn Kasten | 86c7cc8 | 2012-03-05 16:14:39 -0800 | [diff] [blame] | 395 | strlcpy(proc->policy, get_sched_policy_name(p), POLICY_NAME_LEN); | 
|  | 396 | proc->policy[2] = '\0'; | 
| San Mehat | 3927441 | 2009-10-27 11:53:22 -0700 | [diff] [blame] | 397 | } | 
|  | 398 | } | 
|  | 399 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | static int read_status(char *filename, struct proc_info *proc) { | 
|  | 401 | FILE *file; | 
|  | 402 | char line[MAX_LINE]; | 
|  | 403 | unsigned int uid, gid; | 
|  | 404 |  | 
|  | 405 | file = fopen(filename, "r"); | 
|  | 406 | if (!file) return 1; | 
|  | 407 | while (fgets(line, MAX_LINE, file)) { | 
|  | 408 | sscanf(line, "Uid: %u", &uid); | 
|  | 409 | sscanf(line, "Gid: %u", &gid); | 
|  | 410 | } | 
|  | 411 | fclose(file); | 
|  | 412 | proc->uid = uid; proc->gid = gid; | 
|  | 413 | return 0; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | static void print_procs(void) { | 
|  | 417 | int i; | 
|  | 418 | struct proc_info *old_proc, *proc; | 
|  | 419 | long unsigned total_delta_time; | 
|  | 420 | struct passwd *user; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | char *user_str, user_buf[20]; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 |  | 
|  | 423 | for (i = 0; i < num_new_procs; i++) { | 
|  | 424 | if (new_procs[i]) { | 
|  | 425 | old_proc = find_old_proc(new_procs[i]->pid, new_procs[i]->tid); | 
|  | 426 | if (old_proc) { | 
|  | 427 | new_procs[i]->delta_utime = new_procs[i]->utime - old_proc->utime; | 
|  | 428 | new_procs[i]->delta_stime = new_procs[i]->stime - old_proc->stime; | 
|  | 429 | } else { | 
|  | 430 | new_procs[i]->delta_utime = 0; | 
|  | 431 | new_procs[i]->delta_stime = 0; | 
|  | 432 | } | 
|  | 433 | new_procs[i]->delta_time = new_procs[i]->delta_utime + new_procs[i]->delta_stime; | 
|  | 434 | } | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | total_delta_time = (new_cpu.utime + new_cpu.ntime + new_cpu.stime + new_cpu.itime | 
|  | 438 | + new_cpu.iowtime + new_cpu.irqtime + new_cpu.sirqtime) | 
|  | 439 | - (old_cpu.utime + old_cpu.ntime + old_cpu.stime + old_cpu.itime | 
|  | 440 | + old_cpu.iowtime + old_cpu.irqtime + old_cpu.sirqtime); | 
|  | 441 |  | 
|  | 442 | qsort(new_procs, num_new_procs, sizeof(struct proc_info *), proc_cmp); | 
|  | 443 |  | 
|  | 444 | printf("\n\n\n"); | 
|  | 445 | printf("User %ld%%, System %ld%%, IOW %ld%%, IRQ %ld%%\n", | 
|  | 446 | ((new_cpu.utime + new_cpu.ntime) - (old_cpu.utime + old_cpu.ntime)) * 100  / total_delta_time, | 
|  | 447 | ((new_cpu.stime ) - (old_cpu.stime)) * 100 / total_delta_time, | 
|  | 448 | ((new_cpu.iowtime) - (old_cpu.iowtime)) * 100 / total_delta_time, | 
|  | 449 | ((new_cpu.irqtime + new_cpu.sirqtime) | 
|  | 450 | - (old_cpu.irqtime + old_cpu.sirqtime)) * 100 / total_delta_time); | 
|  | 451 | printf("User %ld + Nice %ld + Sys %ld + Idle %ld + IOW %ld + IRQ %ld + SIRQ %ld = %ld\n", | 
|  | 452 | new_cpu.utime - old_cpu.utime, | 
|  | 453 | new_cpu.ntime - old_cpu.ntime, | 
|  | 454 | new_cpu.stime - old_cpu.stime, | 
|  | 455 | new_cpu.itime - old_cpu.itime, | 
|  | 456 | new_cpu.iowtime - old_cpu.iowtime, | 
|  | 457 | new_cpu.irqtime - old_cpu.irqtime, | 
|  | 458 | new_cpu.sirqtime - old_cpu.sirqtime, | 
|  | 459 | total_delta_time); | 
|  | 460 | printf("\n"); | 
| Elliott Hughes | 5922d3b | 2014-04-18 16:53:04 -0700 | [diff] [blame] | 461 | if (!threads) | 
| Dmitry Shmidt | 4358544 | 2010-08-30 16:39:14 -0700 | [diff] [blame] | 462 | printf("%5s %2s %4s %1s %5s %7s %7s %3s %-8s %s\n", "PID", "PR", "CPU%", "S", "#THR", "VSS", "RSS", "PCY", "UID", "Name"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | else | 
| Dmitry Shmidt | 4358544 | 2010-08-30 16:39:14 -0700 | [diff] [blame] | 464 | printf("%5s %5s %2s %4s %1s %7s %7s %3s %-8s %-15s %s\n", "PID", "TID", "PR", "CPU%", "S", "VSS", "RSS", "PCY", "UID", "Thread", "Proc"); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 465 |  | 
|  | 466 | for (i = 0; i < num_new_procs; i++) { | 
|  | 467 | proc = new_procs[i]; | 
|  | 468 |  | 
|  | 469 | if (!proc || (max_procs && (i >= max_procs))) | 
|  | 470 | break; | 
|  | 471 | user  = getpwuid(proc->uid); | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | if (user && user->pw_name) { | 
|  | 473 | user_str = user->pw_name; | 
|  | 474 | } else { | 
|  | 475 | snprintf(user_buf, 20, "%d", proc->uid); | 
|  | 476 | user_str = user_buf; | 
|  | 477 | } | 
| Elliott Hughes | 052d78f | 2014-10-08 11:03:27 -0700 | [diff] [blame] | 478 | if (!threads) { | 
|  | 479 | printf("%5d %2d %3" PRIu64 "%% %c %5d %6" PRIu64 "K %6" PRIu64 "K %3s %-8.8s %s\n", | 
|  | 480 | proc->pid, proc->prs, proc->delta_time * 100 / total_delta_time, proc->state, proc->num_threads, | 
|  | 481 | proc->vss / 1024, proc->rss * getpagesize() / 1024, proc->policy, user_str, proc->name[0] != 0 ? proc->name : proc->tname); | 
|  | 482 | } else { | 
|  | 483 | printf("%5d %5d %2d %3" PRIu64 "%% %c %6" PRIu64 "K %6" PRIu64 "K %3s %-8.8s %-15s %s\n", | 
|  | 484 | proc->pid, proc->tid, proc->prs, 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); | 
|  | 486 | } | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 487 | } | 
|  | 488 | } | 
|  | 489 |  | 
|  | 490 | static struct proc_info *find_old_proc(pid_t pid, pid_t tid) { | 
|  | 491 | int i; | 
|  | 492 |  | 
|  | 493 | for (i = 0; i < num_old_procs; i++) | 
|  | 494 | if (old_procs[i] && (old_procs[i]->pid == pid) && (old_procs[i]->tid == tid)) | 
|  | 495 | return old_procs[i]; | 
|  | 496 |  | 
|  | 497 | return NULL; | 
|  | 498 | } | 
|  | 499 |  | 
|  | 500 | static void free_old_procs(void) { | 
|  | 501 | int i; | 
|  | 502 |  | 
|  | 503 | for (i = 0; i < num_old_procs; i++) | 
|  | 504 | if (old_procs[i]) | 
|  | 505 | free_proc(old_procs[i]); | 
|  | 506 |  | 
|  | 507 | free(old_procs); | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | static int proc_cpu_cmp(const void *a, const void *b) { | 
|  | 511 | struct proc_info *pa, *pb; | 
|  | 512 |  | 
|  | 513 | pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); | 
|  | 514 |  | 
|  | 515 | if (!pa && !pb) return 0; | 
|  | 516 | if (!pa) return 1; | 
|  | 517 | if (!pb) return -1; | 
|  | 518 |  | 
|  | 519 | return -numcmp(pa->delta_time, pb->delta_time); | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | static int proc_vss_cmp(const void *a, const void *b) { | 
|  | 523 | struct proc_info *pa, *pb; | 
|  | 524 |  | 
|  | 525 | pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); | 
|  | 526 |  | 
|  | 527 | if (!pa && !pb) return 0; | 
|  | 528 | if (!pa) return 1; | 
|  | 529 | if (!pb) return -1; | 
|  | 530 |  | 
|  | 531 | return -numcmp(pa->vss, pb->vss); | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | static int proc_rss_cmp(const void *a, const void *b) { | 
|  | 535 | struct proc_info *pa, *pb; | 
|  | 536 |  | 
|  | 537 | pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); | 
|  | 538 |  | 
|  | 539 | if (!pa && !pb) return 0; | 
|  | 540 | if (!pa) return 1; | 
|  | 541 | if (!pb) return -1; | 
|  | 542 |  | 
|  | 543 | return -numcmp(pa->rss, pb->rss); | 
|  | 544 | } | 
|  | 545 |  | 
|  | 546 | static int proc_thr_cmp(const void *a, const void *b) { | 
|  | 547 | struct proc_info *pa, *pb; | 
|  | 548 |  | 
|  | 549 | pa = *((struct proc_info **)a); pb = *((struct proc_info **)b); | 
|  | 550 |  | 
|  | 551 | if (!pa && !pb) return 0; | 
|  | 552 | if (!pa) return 1; | 
|  | 553 | if (!pb) return -1; | 
|  | 554 |  | 
|  | 555 | return -numcmp(pa->num_threads, pb->num_threads); | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | static int numcmp(long long a, long long b) { | 
|  | 559 | if (a < b) return -1; | 
|  | 560 | if (a > b) return 1; | 
|  | 561 | return 0; | 
|  | 562 | } | 
|  | 563 |  | 
|  | 564 | static void usage(char *cmd) { | 
|  | 565 | fprintf(stderr, "Usage: %s [ -m max_procs ] [ -n iterations ] [ -d delay ] [ -s sort_column ] [ -t ] [ -h ]\n" | 
|  | 566 | "    -m num  Maximum number of processes to display.\n" | 
|  | 567 | "    -n num  Updates to show before exiting.\n" | 
|  | 568 | "    -d num  Seconds to wait between updates.\n" | 
|  | 569 | "    -s col  Column to sort by (cpu,vss,rss,thr).\n" | 
| Elliott Hughes | d7bd575 | 2015-11-13 08:30:12 -0800 | [diff] [blame] | 570 | "    -H      Show threads instead of processes.\n" | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 571 | "    -h      Display this help screen.\n", | 
|  | 572 | cmd); | 
|  | 573 | } |