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>
25 #include <pagemap/pagemap.h>
33 static void usage(char *myname);
34 static int getprocname(pid_t pid, char *buf, int len);
35 static int numcmp(long long a, long long b);
37 #define declare_sort(field) \
38 static int sort_by_ ## field (const void *a, const void *b)
45 int (*compfn)(const void *a, const void *b);
48 void print_mem_info() {
52 int fd = open("/proc/meminfo", O_RDONLY);
55 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
59 const int len = read(fd, buffer, sizeof(buffer)-1);
63 printf("Empty /proc/meminfo");
68 static const char* const tags[] = {
77 static const int tagsLen[] = {
86 long mem[] = { 0, 0, 0, 0, 0, 0 };
89 while (*p && numFound < 6) {
92 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
94 while (*p == ' ') p++;
96 while (*p >= '0' && *p <= '9') p++;
107 while (*p && *p != '\n') {
113 printf("RAM: %ldK total, %ldK free, %ldK buffers, %ldK cached, %ldK shmem, %ldK slab\n",
114 mem[0], mem[1], mem[2], mem[3], mem[4], mem[5]);
117 int main(int argc, char *argv[]) {
121 struct proc_info **procs;
123 unsigned long total_pss;
124 unsigned long total_uss;
125 char cmdline[256]; // this must be within the range of int
136 compfn = &sort_by_pss;
140 for (arg = 1; arg < argc; arg++) {
141 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
142 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
143 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
144 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
145 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
146 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
147 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
148 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
149 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
154 error = pm_kernel_create(&ker);
156 fprintf(stderr, "Error creating kernel interface -- "
157 "does this kernel have pagemap?\n");
161 error = pm_kernel_pids(ker, &pids, &num_procs);
163 fprintf(stderr, "Error listing processes.\n");
167 procs = calloc(num_procs, sizeof(struct proc_info*));
169 fprintf(stderr, "calloc: %s", strerror(errno));
173 for (i = 0; i < num_procs; i++) {
174 procs[i] = malloc(sizeof(struct proc_info));
175 if (procs[i] == NULL) {
176 fprintf(stderr, "malloc: %s\n", strerror(errno));
179 procs[i]->pid = pids[i];
180 pm_memusage_zero(&procs[i]->usage);
181 error = pm_process_create(ker, pids[i], &proc);
183 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
189 error = pm_process_usage(proc, &procs[i]->usage);
192 error = pm_process_workingset(proc, &procs[i]->usage, 0);
195 error = pm_process_workingset(proc, NULL, 1);
200 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
203 pm_process_destroy(proc);
208 if (ws == WS_RESET) exit(0);
211 for (i = 0; i < num_procs; i++) {
212 if (procs[i]->usage.vss) {
213 procs[j++] = procs[i];
220 qsort(procs, num_procs, sizeof(procs[0]), compfn);
223 printf("%5s %7s %7s %7s %s\n", "PID", "WRss", "WPss", "WUss", "cmdline");
225 printf("%5s %7s %7s %7s %7s %s\n", "PID", "Vss", "Rss", "Pss", "Uss", "cmdline");
230 for (i = 0; i < num_procs; i++) {
231 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
233 * Something is probably seriously wrong if writing to the stack
240 total_pss += procs[i]->usage.pss;
241 total_uss += procs[i]->usage.uss;
244 printf("%5d %6dK %6dK %6dK %s\n",
246 procs[i]->usage.rss / 1024,
247 procs[i]->usage.pss / 1024,
248 procs[i]->usage.uss / 1024,
252 printf("%5d %6dK %6dK %6dK %6dK %s\n",
254 procs[i]->usage.vss / 1024,
255 procs[i]->usage.rss / 1024,
256 procs[i]->usage.pss / 1024,
257 procs[i]->usage.uss / 1024,
267 printf("%5s %7s %7s %7s %s\n",
268 "", "", "------", "------", "------");
269 printf("%5s %7s %6ldK %6ldK %s\n",
270 "", "", total_pss / 1024, total_uss / 1024, "TOTAL");
272 printf("%5s %7s %7s %7s %7s %s\n",
273 "", "", "", "------", "------", "------");
274 printf("%5s %7s %7s %6ldK %6ldK %s\n",
275 "", "", "", total_pss / 1024, total_uss / 1024, "TOTAL");
284 static void usage(char *myname) {
285 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -h ]\n"
290 " (Default sort order is PSS.)\n"
291 " -R Reverse sort order (default is descending).\n"
292 " -w Display statistics for working set only.\n"
293 " -W Reset working set of all processes.\n"
294 " -h Display this help screen.\n",
299 * Get the process name for a given PID. Inserts the process name into buffer
300 * buf of length len. The size of the buffer must be greater than zero to get
303 * Note that fgets(3) only declares length as an int, so our buffer size is
304 * also declared as an int.
306 * Returns 0 on success, a positive value on partial success, and -1 on
307 * failure. Other interesting values:
308 * 1 on failure to create string to examine proc cmdline entry
309 * 2 on failure to open proc cmdline entry
310 * 3 on failure to read proc cmdline entry
312 static int getprocname(pid_t pid, char *buf, int len) {
316 static const char* unknown_cmdline = "<unknown>";
322 if (asprintf(&filename, "/proc/%zd/cmdline", pid) < 0) {
327 f = fopen(filename, "r");
330 goto releasefilename;
333 if (fgets(buf, len, f) == NULL) {
345 * The process went away before we could read its process name. Try
346 * to give the user "<unknown>" here, but otherwise they get to look
349 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
357 static int numcmp(long long a, long long b) {
358 if (a < b) return -1;
363 #define create_sort(field, compfn) \
364 static int sort_by_ ## field (const void *a, const void *b) { \
365 return order * compfn( \
366 (*((struct proc_info**)a))->usage.field, \
367 (*((struct proc_info**)b))->usage.field \
371 create_sort(vss, numcmp)
372 create_sort(rss, numcmp)
373 create_sort(pss, numcmp)
374 create_sort(uss, numcmp)