procrank.c revision 44ed630233afb3f334bf6e21f95027a3bf19971e
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17#include <dirent.h> 18#include <errno.h> 19#include <stdlib.h> 20#include <sys/types.h> 21#include <unistd.h> 22 23#include <pagemap/pagemap.h> 24 25struct proc_info { 26 pid_t pid; 27 pm_memusage_t usage; 28 unsigned long wss; 29}; 30 31static void usage(char *myname); 32static int getprocname(pid_t pid, char *buf, size_t len); 33static int numcmp(long long a, long long b); 34 35#define declare_sort(field) \ 36 static int sort_by_ ## field (const void *a, const void *b) 37 38declare_sort(vss); 39declare_sort(rss); 40declare_sort(pss); 41declare_sort(uss); 42 43int (*compfn)(const void *a, const void *b); 44static int order; 45 46int main(int argc, char *argv[]) { 47 pm_kernel_t *ker; 48 pm_process_t *proc; 49 pid_t *pids; 50 struct proc_info **procs; 51 size_t num_procs; 52 char cmdline[256]; 53 int error; 54 55 #define WS_OFF 0 56 #define WS_ONLY 1 57 #define WS_RESET 2 58 int ws; 59 60 int i, j; 61 62 compfn = &sort_by_pss; 63 order = -1; 64 ws = WS_OFF; 65 66 for (i = 1; i < argc; i++) { 67 if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; } 68 if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; } 69 if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; } 70 if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; } 71 if (!strcmp(argv[i], "-w")) { ws = WS_ONLY; continue; } 72 if (!strcmp(argv[i], "-W")) { ws = WS_RESET; continue; } 73 if (!strcmp(argv[i], "-R")) { order *= -1; continue; } 74 if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); } 75 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]); 76 usage(argv[0]); 77 exit(EXIT_FAILURE); 78 } 79 80 error = pm_kernel_create(&ker); 81 if (error) { 82 fprintf(stderr, "Error creating kernel interface -- " 83 "does this kernel have pagemap?\n"); 84 exit(EXIT_FAILURE); 85 } 86 87 error = pm_kernel_pids(ker, &pids, &num_procs); 88 if (error) { 89 fprintf(stderr, "Error listing processes.\n"); 90 exit(EXIT_FAILURE); 91 } 92 93 procs = malloc(num_procs * sizeof(struct proc_info*)); 94 if (!procs) { 95 fprintf(stderr, "malloc: %s\n", strerror(errno)); 96 exit(EXIT_FAILURE); 97 } 98 99 for (i = 0; i < num_procs; i++) { 100 procs[i] = malloc(sizeof(struct proc_info)); 101 if (!procs[i]) { 102 fprintf(stderr, "malloc: %s\n", strerror(errno)); 103 exit(EXIT_FAILURE); 104 } 105 procs[i]->pid = pids[i]; 106 error = pm_process_create(ker, pids[i], &proc); 107 if (!error) { 108 switch (ws) { 109 case WS_OFF: 110 pm_process_usage(proc, &procs[i]->usage); 111 break; 112 case WS_ONLY: 113 pm_process_workingset(proc, &procs[i]->usage, 0); 114 break; 115 case WS_RESET: 116 pm_process_workingset(proc, NULL, 1); 117 break; 118 } 119 pm_process_destroy(proc); 120 } else { 121 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]); 122 pm_memusage_zero(&procs[i]->usage); 123 } 124 } 125 126 free(pids); 127 128 if (ws == WS_RESET) exit(0); 129 130 j = 0; 131 for (i = 0; i < num_procs; i++) { 132 if (procs[i]->usage.vss) 133 procs[j++] = procs[i]; 134 } 135 num_procs = j; 136 137 qsort(procs, num_procs, sizeof(procs[0]), compfn); 138 139 if (ws) 140 printf("%5s %7s %7s %7s %s\n", "PID", "WRss", "WPss", "WUss", "cmdline"); 141 else 142 printf("%5s %7s %7s %7s %7s %s\n", "PID", "Vss", "Rss", "Pss", "Uss", "cmdline"); 143 for (i = 0; i < num_procs; i++) { 144 getprocname(procs[i]->pid, cmdline, sizeof(cmdline)); 145 if (ws) 146 printf("%5d %6dK %6dK %6dK %s\n", 147 procs[i]->pid, 148 procs[i]->usage.rss / 1024, 149 procs[i]->usage.pss / 1024, 150 procs[i]->usage.uss / 1024, 151 cmdline 152 ); 153 else 154 printf("%5d %6dK %6dK %6dK %6dK %s\n", 155 procs[i]->pid, 156 procs[i]->usage.vss / 1024, 157 procs[i]->usage.rss / 1024, 158 procs[i]->usage.pss / 1024, 159 procs[i]->usage.uss / 1024, 160 cmdline 161 ); 162 } 163 164 return 0; 165} 166 167static void usage(char *myname) { 168 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -h ]\n" 169 " -v Sort by VSS.\n" 170 " -r Sort by RSS.\n" 171 " -p Sort by PSS.\n" 172 " -u Sort by USS.\n" 173 " (Default sort order is PSS.)\n" 174 " -R Reverse sort order (default is descending).\n" 175 " -w Display statistics for working set only.\n" 176 " -W Reset working set of all processes.\n" 177 " -h Display this help screen.\n", 178 myname); 179} 180 181static int getprocname(pid_t pid, char *buf, size_t len) { 182 char filename[20]; 183 FILE *f; 184 185 sprintf(filename, "/proc/%d/cmdline", pid); 186 f = fopen(filename, "r"); 187 if (!f) { *buf = '\0'; return 1; } 188 if (!fgets(buf, len, f)) { *buf = '\0'; return 2; } 189 fclose(f); 190 return 0; 191} 192 193static int numcmp(long long a, long long b) { 194 if (a < b) return -1; 195 if (a > b) return 1; 196 return 0; 197} 198 199#define create_sort(field, compfn) \ 200 static int sort_by_ ## field (const void *a, const void *b) { \ 201 return order * compfn( \ 202 (*((struct proc_info**)a))->usage.field, \ 203 (*((struct proc_info**)b))->usage.field \ 204 ); \ 205 } 206 207create_sort(vss, numcmp) 208create_sort(rss, numcmp) 209create_sort(pss, numcmp) 210create_sort(uss, numcmp) 211