blob: de26cd13b5ec826f34e1e4fc840d1955406b026e [file] [log] [blame]
The Android Open Source Projecte16cb842009-03-03 19:32:58 -08001/*
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>
Colin Cross4d2488a2014-06-17 14:38:34 -070019#include <fcntl.h>
20#include <inttypes.h>
Colin Cross6f5b13c2013-06-21 12:54:13 -070021#include <stdbool.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080022#include <stdlib.h>
Colin Cross4d2488a2014-06-17 14:38:34 -070023#include <string.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080024#include <sys/types.h>
25#include <unistd.h>
26
27#include <pagemap/pagemap.h>
28
29struct proc_info {
30 pid_t pid;
31 pm_memusage_t usage;
Colin Cross4d2488a2014-06-17 14:38:34 -070032 uint64_t wss;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080033};
34
35static void usage(char *myname);
Kenny Root16abe7a2010-09-21 10:46:57 -070036static int getprocname(pid_t pid, char *buf, int len);
Colin Cross4d2488a2014-06-17 14:38:34 -070037static int numcmp(uint64_t a, uint64_t b);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080038
39#define declare_sort(field) \
40 static int sort_by_ ## field (const void *a, const void *b)
41
42declare_sort(vss);
43declare_sort(rss);
44declare_sort(pss);
45declare_sort(uss);
Colin Cross6f5b13c2013-06-21 12:54:13 -070046declare_sort(swap);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080047
48int (*compfn)(const void *a, const void *b);
49static int order;
50
Dianne Hackborne9eeec82011-07-18 12:41:50 -070051void print_mem_info() {
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070052 char buffer[1024];
Dianne Hackborne9eeec82011-07-18 12:41:50 -070053 int numFound = 0;
54
55 int fd = open("/proc/meminfo", O_RDONLY);
56
57 if (fd < 0) {
58 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
59 return;
60 }
61
62 const int len = read(fd, buffer, sizeof(buffer)-1);
63 close(fd);
64
65 if (len < 0) {
66 printf("Empty /proc/meminfo");
67 return;
68 }
69 buffer[len] = 0;
70
71 static const char* const tags[] = {
72 "MemTotal:",
73 "MemFree:",
74 "Buffers:",
75 "Cached:",
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070076 "Shmem:",
77 "Slab:",
Dianne Hackborne9eeec82011-07-18 12:41:50 -070078 NULL
79 };
80 static const int tagsLen[] = {
81 9,
82 8,
83 8,
84 7,
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070085 6,
86 5,
Dianne Hackborne9eeec82011-07-18 12:41:50 -070087 0
88 };
Colin Cross4d2488a2014-06-17 14:38:34 -070089 uint64_t mem[] = { 0, 0, 0, 0, 0, 0 };
Dianne Hackborne9eeec82011-07-18 12:41:50 -070090
91 char* p = buffer;
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070092 while (*p && numFound < 6) {
Dianne Hackborne9eeec82011-07-18 12:41:50 -070093 int i = 0;
94 while (tags[i]) {
95 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
96 p += tagsLen[i];
97 while (*p == ' ') p++;
98 char* num = p;
99 while (*p >= '0' && *p <= '9') p++;
100 if (*p != 0) {
101 *p = 0;
102 p++;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700103 }
104 mem[i] = atoll(num);
105 numFound++;
106 break;
107 }
108 i++;
109 }
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700110 while (*p && *p != '\n') {
111 p++;
112 }
113 if (*p) p++;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700114 }
115
Colin Cross4d2488a2014-06-17 14:38:34 -0700116 printf("RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, "
117 "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n",
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700118 mem[0], mem[1], mem[2], mem[3], mem[4], mem[5]);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700119}
120
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800121int main(int argc, char *argv[]) {
122 pm_kernel_t *ker;
123 pm_process_t *proc;
124 pid_t *pids;
Kenny Root16abe7a2010-09-21 10:46:57 -0700125 struct proc_info **procs;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800126 size_t num_procs;
Colin Cross4d2488a2014-06-17 14:38:34 -0700127 uint64_t total_pss;
128 uint64_t total_uss;
129 uint64_t total_swap;
Kenny Root16abe7a2010-09-21 10:46:57 -0700130 char cmdline[256]; // this must be within the range of int
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800131 int error;
Colin Cross6f5b13c2013-06-21 12:54:13 -0700132 bool has_swap = false;
Colin Cross8a807c32013-06-21 17:02:05 -0700133 uint64_t required_flags = 0;
134 uint64_t flags_mask = 0;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800135
136 #define WS_OFF 0
137 #define WS_ONLY 1
138 #define WS_RESET 2
139 int ws;
140
Kenny Root16abe7a2010-09-21 10:46:57 -0700141 int arg;
142 size_t i, j;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800143
JP Abgrall80cb1552012-05-11 14:09:59 -0700144 signal(SIGPIPE, SIG_IGN);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800145 compfn = &sort_by_pss;
146 order = -1;
147 ws = WS_OFF;
148
Kenny Root16abe7a2010-09-21 10:46:57 -0700149 for (arg = 1; arg < argc; arg++) {
150 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
151 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
152 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
153 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700154 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
Colin Cross8a807c32013-06-21 17:02:05 -0700155 if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = PM_PAGE_SWAPBACKED; continue; }
156 if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = PM_PAGE_SWAPBACKED; continue; }
157 if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = PM_PAGE_KSM; continue; }
Kenny Root16abe7a2010-09-21 10:46:57 -0700158 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
159 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
160 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
161 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
162 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800163 usage(argv[0]);
164 exit(EXIT_FAILURE);
165 }
166
167 error = pm_kernel_create(&ker);
168 if (error) {
169 fprintf(stderr, "Error creating kernel interface -- "
170 "does this kernel have pagemap?\n");
171 exit(EXIT_FAILURE);
172 }
173
174 error = pm_kernel_pids(ker, &pids, &num_procs);
175 if (error) {
176 fprintf(stderr, "Error listing processes.\n");
177 exit(EXIT_FAILURE);
178 }
179
Kenny Root16abe7a2010-09-21 10:46:57 -0700180 procs = calloc(num_procs, sizeof(struct proc_info*));
181 if (procs == NULL) {
182 fprintf(stderr, "calloc: %s", strerror(errno));
183 exit(EXIT_FAILURE);
184 }
185
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800186 for (i = 0; i < num_procs; i++) {
187 procs[i] = malloc(sizeof(struct proc_info));
Kenny Root16abe7a2010-09-21 10:46:57 -0700188 if (procs[i] == NULL) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800189 fprintf(stderr, "malloc: %s\n", strerror(errno));
190 exit(EXIT_FAILURE);
191 }
192 procs[i]->pid = pids[i];
Colin Crosseff78882011-07-12 22:30:14 -0700193 pm_memusage_zero(&procs[i]->usage);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800194 error = pm_process_create(ker, pids[i], &proc);
Colin Crosseff78882011-07-12 22:30:14 -0700195 if (error) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800196 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
Colin Crosseff78882011-07-12 22:30:14 -0700197 continue;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800198 }
Colin Crosseff78882011-07-12 22:30:14 -0700199
200 switch (ws) {
201 case WS_OFF:
Colin Cross8a807c32013-06-21 17:02:05 -0700202 error = pm_process_usage_flags(proc, &procs[i]->usage, flags_mask,
203 required_flags);
Colin Crosseff78882011-07-12 22:30:14 -0700204 break;
205 case WS_ONLY:
206 error = pm_process_workingset(proc, &procs[i]->usage, 0);
207 break;
208 case WS_RESET:
209 error = pm_process_workingset(proc, NULL, 1);
210 break;
211 }
212
213 if (error) {
214 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
215 }
216
Colin Cross6f5b13c2013-06-21 12:54:13 -0700217 if (ws != WS_RESET && procs[i]->usage.swap) {
218 has_swap = true;
219 }
220
Colin Crosseff78882011-07-12 22:30:14 -0700221 pm_process_destroy(proc);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800222 }
223
224 free(pids);
225
226 if (ws == WS_RESET) exit(0);
227
228 j = 0;
229 for (i = 0; i < num_procs; i++) {
Kenny Root16abe7a2010-09-21 10:46:57 -0700230 if (procs[i]->usage.vss) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800231 procs[j++] = procs[i];
Kenny Root16abe7a2010-09-21 10:46:57 -0700232 } else {
233 free(procs[i]);
234 }
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800235 }
236 num_procs = j;
237
238 qsort(procs, num_procs, sizeof(procs[0]), compfn);
239
Colin Cross6f5b13c2013-06-21 12:54:13 -0700240 printf("%5s ", "PID");
241 if (ws) {
242 printf("%s %7s %7s ", "WRss", "WPss", "WUss");
243 if (has_swap) {
244 printf("%7s ", "WSwap");
245 }
246 } else {
247 printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
248 if (has_swap) {
249 printf("%7s ", "Swap");
250 }
251 }
252
253 printf("%s\n", "cmdline");
Kenny Root16abe7a2010-09-21 10:46:57 -0700254
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700255 total_pss = 0;
256 total_uss = 0;
Colin Cross6f5b13c2013-06-21 12:54:13 -0700257 total_swap = 0;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700258
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800259 for (i = 0; i < num_procs; i++) {
Kenny Root16abe7a2010-09-21 10:46:57 -0700260 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
261 /*
262 * Something is probably seriously wrong if writing to the stack
263 * failed.
264 */
265 free(procs[i]);
266 continue;
267 }
268
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700269 total_pss += procs[i]->usage.pss;
270 total_uss += procs[i]->usage.uss;
Colin Cross6f5b13c2013-06-21 12:54:13 -0700271 total_swap += procs[i]->usage.swap;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700272
Colin Cross6f5b13c2013-06-21 12:54:13 -0700273 printf("%5d ", procs[i]->pid);
274
275 if (ws) {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000276 printf("%6zuK %6zuK %6zuK ",
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800277 procs[i]->usage.rss / 1024,
278 procs[i]->usage.pss / 1024,
Colin Cross6f5b13c2013-06-21 12:54:13 -0700279 procs[i]->usage.uss / 1024
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800280 );
Colin Cross6f5b13c2013-06-21 12:54:13 -0700281 } else {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000282 printf("%7zuK %6zuK %6zuK %6zuK ",
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800283 procs[i]->usage.vss / 1024,
284 procs[i]->usage.rss / 1024,
285 procs[i]->usage.pss / 1024,
Colin Cross6f5b13c2013-06-21 12:54:13 -0700286 procs[i]->usage.uss / 1024
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800287 );
Colin Cross6f5b13c2013-06-21 12:54:13 -0700288 }
289
290 if (has_swap) {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000291 printf("%6zuK ", procs[i]->usage.swap / 1024);
Colin Cross6f5b13c2013-06-21 12:54:13 -0700292 }
293
294 printf("%s\n", cmdline);
Kenny Root16abe7a2010-09-21 10:46:57 -0700295
296 free(procs[i]);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800297 }
298
Kenny Root16abe7a2010-09-21 10:46:57 -0700299 free(procs);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700300
Colin Cross6f5b13c2013-06-21 12:54:13 -0700301 /* Print the separator line */
302 printf("%5s ", "");
303
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700304 if (ws) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700305 printf("%7s %7s %7s ", "", "------", "------");
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700306 } else {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700307 printf("%8s %7s %7s %7s ", "", "", "------", "------");
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700308 }
309
Colin Cross6f5b13c2013-06-21 12:54:13 -0700310 if (has_swap) {
311 printf("%7s ", "------");
312 }
313
314 printf("%s\n", "------");
315
316 /* Print the total line */
317 printf("%5s ", "");
318 if (ws) {
Colin Cross4d2488a2014-06-17 14:38:34 -0700319 printf("%7s %6" PRIu64 "K %" PRIu64 "K ",
Colin Cross6f5b13c2013-06-21 12:54:13 -0700320 "", total_pss / 1024, total_uss / 1024);
321 } else {
Colin Cross4d2488a2014-06-17 14:38:34 -0700322 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
Colin Cross6f5b13c2013-06-21 12:54:13 -0700323 "", "", total_pss / 1024, total_uss / 1024);
324 }
325
326 if (has_swap) {
Colin Cross4d2488a2014-06-17 14:38:34 -0700327 printf("%6" PRIu64 "K ", total_swap);
Colin Cross6f5b13c2013-06-21 12:54:13 -0700328 }
329
330 printf("TOTAL\n");
331
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700332 printf("\n");
333 print_mem_info();
334
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800335 return 0;
336}
337
338static void usage(char *myname) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700339 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800340 " -v Sort by VSS.\n"
341 " -r Sort by RSS.\n"
342 " -p Sort by PSS.\n"
343 " -u Sort by USS.\n"
Colin Cross6f5b13c2013-06-21 12:54:13 -0700344 " -s Sort by swap.\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800345 " (Default sort order is PSS.)\n"
346 " -R Reverse sort order (default is descending).\n"
Colin Cross8a807c32013-06-21 17:02:05 -0700347 " -c Only show cached (storage backed) pages\n"
348 " -C Only show non-cached (ram/swap backed) pages\n"
349 " -k Only show pages collapsed by KSM\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800350 " -w Display statistics for working set only.\n"
351 " -W Reset working set of all processes.\n"
352 " -h Display this help screen.\n",
353 myname);
354}
355
Kenny Root16abe7a2010-09-21 10:46:57 -0700356/*
357 * Get the process name for a given PID. Inserts the process name into buffer
358 * buf of length len. The size of the buffer must be greater than zero to get
359 * any useful output.
360 *
361 * Note that fgets(3) only declares length as an int, so our buffer size is
362 * also declared as an int.
363 *
364 * Returns 0 on success, a positive value on partial success, and -1 on
365 * failure. Other interesting values:
366 * 1 on failure to create string to examine proc cmdline entry
367 * 2 on failure to open proc cmdline entry
368 * 3 on failure to read proc cmdline entry
369 */
370static int getprocname(pid_t pid, char *buf, int len) {
371 char *filename;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800372 FILE *f;
Kenny Root16abe7a2010-09-21 10:46:57 -0700373 int rc = 0;
374 static const char* unknown_cmdline = "<unknown>";
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800375
Kenny Root16abe7a2010-09-21 10:46:57 -0700376 if (len <= 0) {
377 return -1;
378 }
379
Ashok Bhataf3263f2013-01-10 15:52:55 +0000380 if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
Kenny Root16abe7a2010-09-21 10:46:57 -0700381 rc = 1;
382 goto exit;
383 }
384
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800385 f = fopen(filename, "r");
Kenny Root16abe7a2010-09-21 10:46:57 -0700386 if (f == NULL) {
387 rc = 2;
388 goto releasefilename;
389 }
390
391 if (fgets(buf, len, f) == NULL) {
392 rc = 3;
393 goto closefile;
394 }
395
396closefile:
397 (void) fclose(f);
398releasefilename:
399 free(filename);
400exit:
401 if (rc != 0) {
402 /*
403 * The process went away before we could read its process name. Try
404 * to give the user "<unknown>" here, but otherwise they get to look
405 * at a blank.
406 */
407 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
408 rc = 4;
409 }
410 }
411
412 return rc;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800413}
414
Colin Cross4d2488a2014-06-17 14:38:34 -0700415static int numcmp(uint64_t a, uint64_t b) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800416 if (a < b) return -1;
417 if (a > b) return 1;
418 return 0;
419}
420
421#define create_sort(field, compfn) \
422 static int sort_by_ ## field (const void *a, const void *b) { \
423 return order * compfn( \
424 (*((struct proc_info**)a))->usage.field, \
425 (*((struct proc_info**)b))->usage.field \
426 ); \
427 }
428
429create_sort(vss, numcmp)
430create_sort(rss, numcmp)
431create_sort(pss, numcmp)
432create_sort(uss, numcmp)
Colin Cross6f5b13c2013-06-21 12:54:13 -0700433create_sort(swap, numcmp)