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, size_t 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);
48 int main(int argc, char *argv[]) {
52 struct proc_info *procs[MAX_PROCS];
64 compfn = &sort_by_pss;
68 for (i = 1; i < argc; i++) {
69 if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; }
70 if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; }
71 if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; }
72 if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; }
73 if (!strcmp(argv[i], "-w")) { ws = WS_ONLY; continue; }
74 if (!strcmp(argv[i], "-W")) { ws = WS_RESET; continue; }
75 if (!strcmp(argv[i], "-R")) { order *= -1; continue; }
76 if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); }
77 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
82 error = pm_kernel_create(&ker);
84 fprintf(stderr, "Error creating kernel interface -- "
85 "does this kernel have pagemap?\n");
89 error = pm_kernel_pids(ker, &pids, &num_procs);
91 fprintf(stderr, "Error listing processes.\n");
95 for (i = 0; i < num_procs; i++) {
96 procs[i] = malloc(sizeof(struct proc_info));
98 fprintf(stderr, "malloc: %s\n", strerror(errno));
101 procs[i]->pid = pids[i];
102 error = pm_process_create(ker, pids[i], &proc);
106 pm_process_usage(proc, &procs[i]->usage);
109 pm_process_workingset(proc, &procs[i]->usage, 0);
112 pm_process_workingset(proc, NULL, 1);
115 pm_process_destroy(proc);
117 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
118 pm_memusage_zero(&procs[i]->usage);
124 if (ws == WS_RESET) exit(0);
127 for (i = 0; i < num_procs; i++) {
128 if (procs[i]->usage.vss)
129 procs[j++] = procs[i];
133 qsort(procs, num_procs, sizeof(procs[0]), compfn);
136 printf("%5s %7s %7s %7s %s\n", "PID", "WRss", "WPss", "WUss", "cmdline");
138 printf("%5s %7s %7s %7s %7s %s\n", "PID", "Vss", "Rss", "Pss", "Uss", "cmdline");
139 for (i = 0; i < num_procs; i++) {
140 getprocname(procs[i]->pid, cmdline, sizeof(cmdline));
142 printf("%5d %6dK %6dK %6dK %s\n",
144 procs[i]->usage.rss / 1024,
145 procs[i]->usage.pss / 1024,
146 procs[i]->usage.uss / 1024,
150 printf("%5d %6dK %6dK %6dK %6dK %s\n",
152 procs[i]->usage.vss / 1024,
153 procs[i]->usage.rss / 1024,
154 procs[i]->usage.pss / 1024,
155 procs[i]->usage.uss / 1024,
163 static void usage(char *myname) {
164 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -h ]\n"
169 " (Default sort order is PSS.)\n"
170 " -R Reverse sort order (default is descending).\n"
171 " -w Display statistics for working set only.\n"
172 " -W Reset working set of all processes.\n"
173 " -h Display this help screen.\n",
177 static int getprocname(pid_t pid, char *buf, size_t len) {
181 sprintf(filename, "/proc/%d/cmdline", pid);
182 f = fopen(filename, "r");
183 if (!f) { *buf = '\0'; return 1; }
184 if (!fgets(buf, len, f)) { *buf = '\0'; return 2; }
189 static int numcmp(long long a, long long b) {
190 if (a < b) return -1;
195 #define create_sort(field, compfn) \
196 static int sort_by_ ## field (const void *a, const void *b) { \
197 return order * compfn( \
198 (*((struct proc_info**)a))->usage.field, \
199 (*((struct proc_info**)b))->usage.field \
203 create_sort(vss, numcmp)
204 create_sort(rss, numcmp)
205 create_sort(pss, numcmp)
206 create_sort(uss, numcmp)