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.
24 #include <sys/types.h>
27 #include <pagemap/pagemap.h>
35 static void usage(char *myname);
36 static int getprocname(pid_t pid, char *buf, int len);
37 static int numcmp(uint64_t a, uint64_t b);
39 #define declare_sort(field) \
40 static int sort_by_ ## field (const void *a, const void *b)
48 int (*compfn)(const void *a, const void *b);
68 void get_mem_info(uint64_t mem[]) {
70 unsigned int numFound = 0;
72 int fd = open("/proc/meminfo", O_RDONLY);
75 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
79 const int len = read(fd, buffer, sizeof(buffer)-1);
83 printf("Empty /proc/meminfo");
88 static const char* const tags[] = {
97 "ZRam:", /* not read from meminfo but from /sys/block/zram0 */
104 static const int tagsLen[] = {
122 while (*p && (numFound < (sizeof(tagsLen) / sizeof(tagsLen[0])))) {
125 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
127 while (*p == ' ') p++;
129 while (*p >= '0' && *p <= '9') p++;
140 while (*p && *p != '\n') {
147 static uint64_t get_zram_mem_used() {
148 #define ZRAM_SYSFS "/sys/block/zram0/"
149 FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
151 uint64_t mem_used_total = 0;
153 int matched = fscanf(f, "%*d %*d %" SCNu64 " %*d %*d %*d %*d", &mem_used_total);
155 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mm_stat\n");
158 return mem_used_total;
161 f = fopen(ZRAM_SYSFS "mem_used_total", "r");
163 uint64_t mem_used_total = 0;
165 int matched = fscanf(f, "%" SCNu64, &mem_used_total);
167 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mem_used_total\n");
170 return mem_used_total;
176 int main(int argc, char *argv[]) {
180 struct proc_info **procs;
185 uint64_t total_pswap;
186 uint64_t total_uswap;
187 uint64_t total_zswap;
188 char cmdline[256]; // this must be within the range of int
190 bool has_swap = false, has_zram = false;
191 uint64_t required_flags = 0;
192 uint64_t flags_mask = 0;
202 uint64_t mem[MEMINFO_COUNT] = { };
203 pm_proportional_swap_t *p_swap;
206 signal(SIGPIPE, SIG_IGN);
207 compfn = &sort_by_pss;
211 for (arg = 1; arg < argc; arg++) {
212 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
213 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
214 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
215 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
216 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
217 if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = PM_PAGE_SWAPBACKED; continue; }
218 if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = PM_PAGE_SWAPBACKED; continue; }
219 if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = PM_PAGE_KSM; continue; }
220 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
221 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
222 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
223 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
224 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
230 p_swap = pm_memusage_pswap_create(mem[MEMINFO_SWAP_TOTAL] * 1024);
232 error = pm_kernel_create(&ker);
234 fprintf(stderr, "Error creating kernel interface -- "
235 "does this kernel have pagemap?\n");
239 error = pm_kernel_pids(ker, &pids, &num_procs);
241 fprintf(stderr, "Error listing processes.\n");
245 procs = calloc(num_procs, sizeof(struct proc_info*));
247 fprintf(stderr, "calloc: %s", strerror(errno));
251 for (i = 0; i < num_procs; i++) {
252 procs[i] = malloc(sizeof(struct proc_info));
253 if (procs[i] == NULL) {
254 fprintf(stderr, "malloc: %s\n", strerror(errno));
257 procs[i]->pid = pids[i];
258 pm_memusage_zero(&procs[i]->usage);
259 pm_memusage_pswap_init_handle(&procs[i]->usage, p_swap);
260 error = pm_process_create(ker, pids[i], &proc);
262 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
268 error = pm_process_usage_flags(proc, &procs[i]->usage, flags_mask,
272 error = pm_process_workingset(proc, &procs[i]->usage, 0);
275 error = pm_process_workingset(proc, NULL, 1);
280 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
283 if (ws != WS_RESET && procs[i]->usage.swap) {
287 pm_process_destroy(proc);
292 if (ws == WS_RESET) exit(0);
295 for (i = 0; i < num_procs; i++) {
296 if (procs[i]->usage.vss) {
297 procs[j++] = procs[i];
304 qsort(procs, num_procs, sizeof(procs[0]), compfn);
307 uint64_t zram_mem_used = get_zram_mem_used();
309 mem[MEMINFO_ZRAM_TOTAL] = zram_mem_used/1024;
310 zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] /
311 (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]);
316 printf("%5s ", "PID");
318 printf("%7s %7s %7s ", "WRss", "WPss", "WUss");
320 printf("%7s %7s %7s ", "WSwap", "WPSwap", "WUSwap");
322 printf("%7s ", "WZSwap");
326 printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
328 printf("%7s %7s %7s ", "Swap", "PSwap", "USwap");
330 printf("%7s ", "ZSwap");
335 printf("%s\n", "cmdline");
344 for (i = 0; i < num_procs; i++) {
345 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
347 * Something is probably seriously wrong if writing to the stack
354 total_pss += procs[i]->usage.pss;
355 total_uss += procs[i]->usage.uss;
356 total_swap += procs[i]->usage.swap;
358 printf("%5d ", procs[i]->pid);
361 printf("%6zuK %6zuK %6zuK ",
362 procs[i]->usage.rss / 1024,
363 procs[i]->usage.pss / 1024,
364 procs[i]->usage.uss / 1024
367 printf("%7zuK %6zuK %6zuK %6zuK ",
368 procs[i]->usage.vss / 1024,
369 procs[i]->usage.rss / 1024,
370 procs[i]->usage.pss / 1024,
371 procs[i]->usage.uss / 1024
378 pm_memusage_pswap_get_usage(&procs[i]->usage, &su);
379 printf("%6zuK ", procs[i]->usage.swap / 1024);
380 printf("%6zuK ", su.proportional / 1024);
381 printf("%6zuK ", su.unique / 1024);
382 total_pswap += su.proportional;
383 total_uswap += su.unique;
384 pm_memusage_pswap_free(&procs[i]->usage);
386 size_t zpswap = su.proportional * zram_cr;
387 printf("%6zuK ", zpswap / 1024);
388 total_zswap += zpswap;
392 printf("%s\n", cmdline);
398 pm_memusage_pswap_destroy(p_swap);
400 /* Print the separator line */
404 printf("%7s %7s %7s ", "", "------", "------");
406 printf("%8s %7s %7s %7s ", "", "", "------", "------");
410 printf("%7s %7s %7s ", "------", "------", "------");
412 printf("%7s ", "------");
416 printf("%s\n", "------");
418 /* Print the total line */
421 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
422 "", total_pss / 1024, total_uss / 1024);
424 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
425 "", "", total_pss / 1024, total_uss / 1024);
429 printf("%6" PRIu64 "K ", total_swap / 1024);
430 printf("%6" PRIu64 "K ", total_pswap / 1024);
431 printf("%6" PRIu64 "K ", total_uswap / 1024);
433 printf("%6" PRIu64 "K ", total_zswap / 1024);
442 printf("ZRAM: %" PRIu64 "K physical used for %" PRIu64 "K in swap "
443 "(%" PRIu64 "K total swap)\n",
444 mem[MEMINFO_ZRAM_TOTAL], (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]),
445 mem[MEMINFO_SWAP_TOTAL]);
447 printf(" RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, "
448 "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n",
449 mem[MEMINFO_TOTAL], mem[MEMINFO_FREE], mem[MEMINFO_BUFFERS],
450 mem[MEMINFO_CACHED], mem[MEMINFO_SHMEM], mem[MEMINFO_SLAB]);
455 static void usage(char *myname) {
456 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
461 " -s Sort by swap.\n"
462 " (Default sort order is PSS.)\n"
463 " -R Reverse sort order (default is descending).\n"
464 " -c Only show cached (storage backed) pages\n"
465 " -C Only show non-cached (ram/swap backed) pages\n"
466 " -k Only show pages collapsed by KSM\n"
467 " -w Display statistics for working set only.\n"
468 " -W Reset working set of all processes.\n"
469 " -h Display this help screen.\n",
474 * Get the process name for a given PID. Inserts the process name into buffer
475 * buf of length len. The size of the buffer must be greater than zero to get
478 * Note that fgets(3) only declares length as an int, so our buffer size is
479 * also declared as an int.
481 * Returns 0 on success, a positive value on partial success, and -1 on
482 * failure. Other interesting values:
483 * 1 on failure to create string to examine proc cmdline entry
484 * 2 on failure to open proc cmdline entry
485 * 3 on failure to read proc cmdline entry
487 static int getprocname(pid_t pid, char *buf, int len) {
491 static const char* unknown_cmdline = "<unknown>";
497 if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
502 f = fopen(filename, "r");
505 goto releasefilename;
508 if (fgets(buf, len, f) == NULL) {
520 * The process went away before we could read its process name. Try
521 * to give the user "<unknown>" here, but otherwise they get to look
524 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
532 static int numcmp(uint64_t a, uint64_t b) {
533 if (a < b) return -1;
538 #define create_sort(field, compfn) \
539 static int sort_by_ ## field (const void *a, const void *b) { \
540 return order * compfn( \
541 (*((struct proc_info**)a))->usage.field, \
542 (*((struct proc_info**)b))->usage.field \
546 create_sort(vss, numcmp)
547 create_sort(rss, numcmp)
548 create_sort(pss, numcmp)
549 create_sort(uss, numcmp)
550 create_sort(swap, numcmp)