2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <sys/types.h>
23 #include <pagemap/pagemap.h>
31 static void usage(char *myname);
32 static int getprocname(pid_t pid, char *buf, int len);
33 static int numcmp(long long a, long long b);
35 #define declare_sort(field) \
36 static int sort_by_ ## field (const void *a, const void *b)
43 int (*compfn)(const void *a, const void *b);
46 int main(int argc, char *argv[]) {
50 struct proc_info **procs;
52 char cmdline[256]; // this must be within the range of int
63 compfn = &sort_by_pss;
67 for (arg = 1; arg < argc; arg++) {
68 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
69 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
70 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
71 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
72 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
73 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
74 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
75 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
76 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
81 error = pm_kernel_create(&ker);
83 fprintf(stderr, "Error creating kernel interface -- "
84 "does this kernel have pagemap?\n");
88 error = pm_kernel_pids(ker, &pids, &num_procs);
90 fprintf(stderr, "Error listing processes.\n");
94 procs = calloc(num_procs, sizeof(struct proc_info*));
96 fprintf(stderr, "calloc: %s", strerror(errno));
100 for (i = 0; i < num_procs; i++) {
101 procs[i] = malloc(sizeof(struct proc_info));
102 if (procs[i] == NULL) {
103 fprintf(stderr, "malloc: %s\n", strerror(errno));
106 procs[i]->pid = pids[i];
107 error = pm_process_create(ker, pids[i], &proc);
111 pm_process_usage(proc, &procs[i]->usage);
114 pm_process_workingset(proc, &procs[i]->usage, 0);
117 pm_process_workingset(proc, NULL, 1);
120 pm_process_destroy(proc);
122 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
123 pm_memusage_zero(&procs[i]->usage);
129 if (ws == WS_RESET) exit(0);
132 for (i = 0; i < num_procs; i++) {
133 if (procs[i]->usage.vss) {
134 procs[j++] = procs[i];
141 qsort(procs, num_procs, sizeof(procs[0]), compfn);
144 printf("%5s %7s %7s %7s %s\n", "PID", "WRss", "WPss", "WUss", "cmdline");
146 printf("%5s %7s %7s %7s %7s %s\n", "PID", "Vss", "Rss", "Pss", "Uss", "cmdline");
148 for (i = 0; i < num_procs; i++) {
149 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
151 * Something is probably seriously wrong if writing to the stack
159 printf("%5d %6dK %6dK %6dK %s\n",
161 procs[i]->usage.rss / 1024,
162 procs[i]->usage.pss / 1024,
163 procs[i]->usage.uss / 1024,
167 printf("%5d %6dK %6dK %6dK %6dK %s\n",
169 procs[i]->usage.vss / 1024,
170 procs[i]->usage.rss / 1024,
171 procs[i]->usage.pss / 1024,
172 procs[i]->usage.uss / 1024,
183 static void usage(char *myname) {
184 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -h ]\n"
189 " (Default sort order is PSS.)\n"
190 " -R Reverse sort order (default is descending).\n"
191 " -w Display statistics for working set only.\n"
192 " -W Reset working set of all processes.\n"
193 " -h Display this help screen.\n",
198 * Get the process name for a given PID. Inserts the process name into buffer
199 * buf of length len. The size of the buffer must be greater than zero to get
202 * Note that fgets(3) only declares length as an int, so our buffer size is
203 * also declared as an int.
205 * Returns 0 on success, a positive value on partial success, and -1 on
206 * failure. Other interesting values:
207 * 1 on failure to create string to examine proc cmdline entry
208 * 2 on failure to open proc cmdline entry
209 * 3 on failure to read proc cmdline entry
211 static int getprocname(pid_t pid, char *buf, int len) {
215 static const char* unknown_cmdline = "<unknown>";
221 if (asprintf(&filename, "/proc/%zd/cmdline", pid) < 0) {
226 f = fopen(filename, "r");
229 goto releasefilename;
232 if (fgets(buf, len, f) == NULL) {
244 * The process went away before we could read its process name. Try
245 * to give the user "<unknown>" here, but otherwise they get to look
248 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
256 static int numcmp(long long a, long long b) {
257 if (a < b) return -1;
262 #define create_sort(field, compfn) \
263 static int sort_by_ ## field (const void *a, const void *b) { \
264 return order * compfn( \
265 (*((struct proc_info**)a))->usage.field, \
266 (*((struct proc_info**)b))->usage.field \
270 create_sort(vss, numcmp)
271 create_sort(rss, numcmp)
272 create_sort(pss, numcmp)
273 create_sort(uss, numcmp)