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>
29 #include <android-base/file.h>
30 #include <android-base/stringprintf.h>
31 #include <android-base/strings.h>
32 #include <pagemap/pagemap.h>
41 static void usage(char *myname);
42 static std::string getprocname(pid_t pid);
43 static int getoomadj(pid_t pid);
44 static bool getminfree(std::vector<uint64_t>* minfree, std::vector<int>* adj);
45 static int numcmp(uint64_t a, uint64_t b);
47 #define declare_sort(field) \
48 static int sort_by_ ## field (const void *a, const void *b)
57 int (*compfn)(const void *a, const void *b);
77 void get_mem_info(uint64_t mem[]) {
79 unsigned int numFound = 0;
81 int fd = open("/proc/meminfo", O_RDONLY);
84 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
88 const int len = read(fd, buffer, sizeof(buffer)-1);
92 printf("Empty /proc/meminfo");
97 static const char* const tags[] = {
106 "ZRam:", // not read from meminfo but from /sys/block/zram0
113 static const int tagsLen[] = {
131 while (*p && (numFound < (sizeof(tagsLen) / sizeof(tagsLen[0])))) {
134 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
136 while (*p == ' ') p++;
138 while (*p >= '0' && *p <= '9') p++;
149 while (*p && *p != '\n') {
156 static uint64_t get_zram_mem_used() {
157 #define ZRAM_SYSFS "/sys/block/zram0/"
158 FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
160 uint64_t mem_used_total = 0;
162 int matched = fscanf(f, "%*d %*d %" SCNu64 " %*d %*d %*d %*d", &mem_used_total);
164 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mm_stat\n");
167 return mem_used_total;
170 f = fopen(ZRAM_SYSFS "mem_used_total", "r");
172 uint64_t mem_used_total = 0;
174 int matched = fscanf(f, "%" SCNu64, &mem_used_total);
176 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mem_used_total\n");
179 return mem_used_total;
185 int main(int argc, char *argv[]) {
193 uint64_t total_pswap;
194 uint64_t total_uswap;
195 uint64_t total_zswap;
197 bool has_swap = false, has_zram = false;
198 uint64_t required_flags = 0;
199 uint64_t flags_mask = 0;
210 uint64_t mem[MEMINFO_COUNT] = { };
211 pm_proportional_swap_t *p_swap;
214 signal(SIGPIPE, SIG_IGN);
215 compfn = &sort_by_pss;
220 for (arg = 1; arg < argc; arg++) {
221 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
222 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
223 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
224 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
225 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
226 if (!strcmp(argv[arg], "-o")) { compfn = &sort_by_oomadj; oomadj = true; continue; }
227 if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = KPF_SWAPBACKED; continue; }
228 if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = KPF_SWAPBACKED; continue; }
229 if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = KPF_KSM; continue; }
230 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
231 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
232 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
233 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
234 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
240 p_swap = pm_memusage_pswap_create(mem[MEMINFO_SWAP_TOTAL] * 1024);
242 error = pm_kernel_create(&ker);
244 fprintf(stderr, "Error creating kernel interface -- "
245 "does this kernel have pagemap?\n");
249 error = pm_kernel_pids(ker, &pids, &num_procs);
251 fprintf(stderr, "Error listing processes.\n");
255 std::vector<proc_info> procs(num_procs);
256 for (i = 0; i < num_procs; i++) {
257 procs[i].pid = pids[i];
258 procs[i].oomadj = getoomadj(pids[i]);
259 pm_memusage_zero(&procs[i].usage);
260 pm_memusage_pswap_init_handle(&procs[i].usage, p_swap);
261 error = pm_process_create(ker, pids[i], &proc);
263 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
269 error = pm_process_usage_flags(proc, &procs[i].usage, flags_mask,
273 error = pm_process_workingset(proc, &procs[i].usage, 0);
276 error = pm_process_workingset(proc, NULL, 1);
281 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
284 if (ws != WS_RESET && procs[i].usage.swap) {
288 pm_process_destroy(proc);
293 if (ws == WS_RESET) exit(0);
295 procs.erase(std::remove_if(procs.begin(),
298 return proc.usage.vss == 0;
302 qsort(procs.data(), procs.size(), sizeof(procs[0]), compfn);
305 uint64_t zram_mem_used = get_zram_mem_used();
307 mem[MEMINFO_ZRAM_TOTAL] = zram_mem_used/1024;
308 zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] /
309 (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]);
314 printf("%5s ", "PID");
316 printf("%5s ", "oom");
319 printf("%7s %7s %7s ", "WRss", "WPss", "WUss");
321 printf("%7s %7s %7s ", "WSwap", "WPSwap", "WUSwap");
323 printf("%7s ", "WZSwap");
327 printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
329 printf("%7s %7s %7s ", "Swap", "PSwap", "USwap");
331 printf("%7s ", "ZSwap");
336 printf("%s\n", "cmdline");
345 std::vector<uint64_t> lmk_minfree;
346 std::vector<int> lmk_adj;
348 getminfree(&lmk_minfree, &lmk_adj);
350 auto lmk_minfree_it = lmk_minfree.cbegin();
351 auto lmk_adj_it = lmk_adj.cbegin();
353 auto print_oomadj_totals = [&](int adj){
354 for (; lmk_adj_it != lmk_adj.cend() && lmk_minfree_it != lmk_minfree.cend() &&
355 adj > *lmk_adj_it; lmk_adj_it++, lmk_minfree_it++) {
356 // Print the cumulative total line
357 printf("%5s ", ""); // pid
359 printf("%5s ", ""); // oomadj
362 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
363 "", total_pss / 1024, total_uss / 1024);
365 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
366 "", "", total_pss / 1024, total_uss / 1024);
370 printf("%6" PRIu64 "K ", total_swap / 1024);
371 printf("%6" PRIu64 "K ", total_pswap / 1024);
372 printf("%6" PRIu64 "K ", total_uswap / 1024);
374 printf("%6" PRIu64 "K ", total_zswap / 1024);
378 printf("TOTAL for oomadj < %d (%6" PRIu64 "K)\n", *lmk_adj_it, *lmk_minfree_it / 1024);
382 for (auto& proc: procs) {
384 print_oomadj_totals(proc.oomadj);
387 std::string cmdline = getprocname(proc.pid);
389 total_pss += proc.usage.pss;
390 total_uss += proc.usage.uss;
391 total_swap += proc.usage.swap;
393 printf("%5d ", proc.pid);
396 printf("%5d ", proc.oomadj);
400 printf("%6zuK %6zuK %6zuK ",
401 proc.usage.rss / 1024,
402 proc.usage.pss / 1024,
403 proc.usage.uss / 1024
406 printf("%7zuK %6zuK %6zuK %6zuK ",
407 proc.usage.vss / 1024,
408 proc.usage.rss / 1024,
409 proc.usage.pss / 1024,
410 proc.usage.uss / 1024
417 pm_memusage_pswap_get_usage(&proc.usage, &su);
418 printf("%6zuK ", proc.usage.swap / 1024);
419 printf("%6zuK ", su.proportional / 1024);
420 printf("%6zuK ", su.unique / 1024);
421 total_pswap += su.proportional;
422 total_uswap += su.unique;
423 pm_memusage_pswap_free(&proc.usage);
425 size_t zpswap = su.proportional * zram_cr;
426 printf("%6zuK ", zpswap / 1024);
427 total_zswap += zpswap;
431 printf("%s\n", cmdline.c_str());
434 pm_memusage_pswap_destroy(p_swap);
437 print_oomadj_totals(INT_MAX);
440 // Print the separator line
448 printf("%7s %7s %7s ", "", "------", "------");
450 printf("%8s %7s %7s %7s ", "", "", "------", "------");
454 printf("%7s %7s %7s ", "------", "------", "------");
456 printf("%7s ", "------");
460 printf("%s\n", "------");
462 // Print the total line
470 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
471 "", total_pss / 1024, total_uss / 1024);
473 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
474 "", "", total_pss / 1024, total_uss / 1024);
478 printf("%6" PRIu64 "K ", total_swap / 1024);
479 printf("%6" PRIu64 "K ", total_pswap / 1024);
480 printf("%6" PRIu64 "K ", total_uswap / 1024);
482 printf("%6" PRIu64 "K ", total_zswap / 1024);
491 printf("ZRAM: %" PRIu64 "K physical used for %" PRIu64 "K in swap "
492 "(%" PRIu64 "K total swap)\n",
493 mem[MEMINFO_ZRAM_TOTAL], (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]),
494 mem[MEMINFO_SWAP_TOTAL]);
496 printf(" RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, "
497 "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n",
498 mem[MEMINFO_TOTAL], mem[MEMINFO_FREE], mem[MEMINFO_BUFFERS],
499 mem[MEMINFO_CACHED], mem[MEMINFO_SHMEM], mem[MEMINFO_SLAB]);
504 static void usage(char *myname) {
505 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
510 " -s Sort by swap.\n"
511 " (Default sort order is PSS.)\n"
512 " -R Reverse sort order (default is descending).\n"
513 " -c Only show cached (storage backed) pages\n"
514 " -C Only show non-cached (ram/swap backed) pages\n"
515 " -k Only show pages collapsed by KSM\n"
516 " -w Display statistics for working set only.\n"
517 " -W Reset working set of all processes.\n"
518 " -o Show and sort by oom score against lowmemorykiller thresholds.\n"
519 " -h Display this help screen.\n",
523 // Get the process name for a given PID.
524 static std::string getprocname(pid_t pid) {
525 std::string filename = android::base::StringPrintf("/proc/%d/cmdline", pid);
527 std::string procname;
529 if (!android::base::ReadFileToString(filename, &procname)) {
530 // The process went away before we could read its process name.
531 procname = "<unknown>";
537 static int getoomadj(pid_t pid) {
538 std::string filename = android::base::StringPrintf("/proc/%d/oom_score_adj", pid);
541 if (!android::base::ReadFileToString(filename, &oomadj)) {
545 return strtol(oomadj.c_str(), NULL, 10);
548 static bool getminfree(std::vector<uint64_t>* minfree, std::vector<int>* adj) {
549 std::string minfree_str;
552 if (!android::base::ReadFileToString("/sys/module/lowmemorykiller/parameters/minfree", &minfree_str)) {
556 if (!android::base::ReadFileToString("/sys/module/lowmemorykiller/parameters/adj", &adj_str)) {
560 std::vector<std::string> minfree_vec = android::base::Split(minfree_str, ",");
561 std::vector<std::string> adj_vec = android::base::Split(adj_str, ",");
564 minfree->resize(minfree_vec.size());
566 adj->resize(adj_vec.size());
568 std::transform(minfree_vec.begin(), minfree_vec.end(), minfree->begin(),
569 [](const std::string& s) -> uint64_t {
570 return strtoull(s.c_str(), NULL, 10) * PAGE_SIZE;
573 std::transform(adj_vec.begin(), adj_vec.end(), adj->begin(),
574 [](const std::string& s) -> int {
575 return strtol(s.c_str(), NULL, 10);
581 static int numcmp(uint64_t a, uint64_t b) {
582 if (a < b) return -1;
587 static int snumcmp(int64_t a, int64_t b) {
588 if (a < b) return -1;
593 #define create_sort(field, compfn) \
594 static int sort_by_ ## field (const void *a, const void *b) { \
595 return order * compfn( \
596 ((struct proc_info*)(a))->usage.field, \
597 ((struct proc_info*)(b))->usage.field \
601 create_sort(vss, numcmp)
602 create_sort(rss, numcmp)
603 create_sort(pss, numcmp)
604 create_sort(uss, numcmp)
605 create_sort(swap, numcmp)
607 static int sort_by_oomadj (const void *a, const void *b) {
608 // Negative oomadj is higher priority, reverse the sort order
609 return -1 * order * snumcmp(
610 ((struct proc_info*)a)->oomadj,
611 ((struct proc_info*)b)->oomadj