blob: b8ebd31cda0edffbb5d1f69af61165b71e03b683 [file] [log] [blame]
Colin Crossbaa84a22016-05-23 16:50:59 -07001//
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//
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080016
17#include <dirent.h>
18#include <errno.h>
Colin Cross4d2488a2014-06-17 14:38:34 -070019#include <fcntl.h>
20#include <inttypes.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080021#include <stdlib.h>
Colin Cross4d2488a2014-06-17 14:38:34 -070022#include <string.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080023#include <sys/types.h>
24#include <unistd.h>
25
Colin Crossbaa84a22016-05-23 16:50:59 -070026#include <vector>
27
28#include <android-base/file.h>
29#include <android-base/stringprintf.h>
Colin Crossf8d8f042016-05-24 11:05:31 -070030#include <android-base/strings.h>
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080031#include <pagemap/pagemap.h>
32
33struct proc_info {
34 pid_t pid;
35 pm_memusage_t usage;
Colin Cross4d2488a2014-06-17 14:38:34 -070036 uint64_t wss;
Colin Crossf8d8f042016-05-24 11:05:31 -070037 int oomadj;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080038};
39
40static void usage(char *myname);
Colin Crossbaa84a22016-05-23 16:50:59 -070041static std::string getprocname(pid_t pid);
Colin Crossf8d8f042016-05-24 11:05:31 -070042static int getoomadj(pid_t pid);
43static bool getminfree(std::vector<uint64_t>* minfree, std::vector<int>* adj);
Colin Cross4d2488a2014-06-17 14:38:34 -070044static int numcmp(uint64_t a, uint64_t b);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080045
46#define declare_sort(field) \
47 static int sort_by_ ## field (const void *a, const void *b)
48
49declare_sort(vss);
50declare_sort(rss);
51declare_sort(pss);
52declare_sort(uss);
Colin Cross6f5b13c2013-06-21 12:54:13 -070053declare_sort(swap);
Colin Crossf8d8f042016-05-24 11:05:31 -070054declare_sort(oomadj);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -080055
56int (*compfn)(const void *a, const void *b);
57static int order;
58
Thierry Strudel08ea6e72015-11-20 18:07:15 -080059enum {
60 MEMINFO_TOTAL,
61 MEMINFO_FREE,
62 MEMINFO_BUFFERS,
63 MEMINFO_CACHED,
64 MEMINFO_SHMEM,
65 MEMINFO_SLAB,
66 MEMINFO_SWAP_TOTAL,
67 MEMINFO_SWAP_FREE,
68 MEMINFO_ZRAM_TOTAL,
69 MEMINFO_MAPPED,
70 MEMINFO_VMALLOC_USED,
71 MEMINFO_PAGE_TABLES,
72 MEMINFO_KERNEL_STACK,
73 MEMINFO_COUNT
74};
75
76void get_mem_info(uint64_t mem[]) {
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070077 char buffer[1024];
Thierry Strudel08ea6e72015-11-20 18:07:15 -080078 unsigned int numFound = 0;
Dianne Hackborne9eeec82011-07-18 12:41:50 -070079
80 int fd = open("/proc/meminfo", O_RDONLY);
81
82 if (fd < 0) {
83 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
84 return;
85 }
86
87 const int len = read(fd, buffer, sizeof(buffer)-1);
88 close(fd);
89
90 if (len < 0) {
91 printf("Empty /proc/meminfo");
92 return;
93 }
94 buffer[len] = 0;
95
96 static const char* const tags[] = {
97 "MemTotal:",
98 "MemFree:",
99 "Buffers:",
100 "Cached:",
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700101 "Shmem:",
102 "Slab:",
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800103 "SwapTotal:",
104 "SwapFree:",
Colin Crossbaa84a22016-05-23 16:50:59 -0700105 "ZRam:", // not read from meminfo but from /sys/block/zram0
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800106 "Mapped:",
107 "VmallocUsed:",
108 "PageTables:",
109 "KernelStack:",
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700110 NULL
111 };
112 static const int tagsLen[] = {
113 9,
114 8,
115 8,
116 7,
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700117 6,
118 5,
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800119 10,
120 9,
121 5,
122 7,
123 12,
124 11,
125 12,
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700126 0
127 };
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700128
129 char* p = buffer;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800130 while (*p && (numFound < (sizeof(tagsLen) / sizeof(tagsLen[0])))) {
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700131 int i = 0;
132 while (tags[i]) {
133 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
134 p += tagsLen[i];
135 while (*p == ' ') p++;
136 char* num = p;
137 while (*p >= '0' && *p <= '9') p++;
138 if (*p != 0) {
139 *p = 0;
140 p++;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700141 }
142 mem[i] = atoll(num);
143 numFound++;
144 break;
145 }
146 i++;
147 }
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700148 while (*p && *p != '\n') {
149 p++;
150 }
151 if (*p) p++;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700152 }
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700153}
154
Greg Hackmannfbc3d452015-12-09 14:55:53 -0800155static uint64_t get_zram_mem_used() {
156#define ZRAM_SYSFS "/sys/block/zram0/"
157 FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
158 if (f) {
159 uint64_t mem_used_total = 0;
160
161 int matched = fscanf(f, "%*d %*d %" SCNu64 " %*d %*d %*d %*d", &mem_used_total);
162 if (matched != 1)
163 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mm_stat\n");
164
165 fclose(f);
166 return mem_used_total;
167 }
168
169 f = fopen(ZRAM_SYSFS "mem_used_total", "r");
170 if (f) {
171 uint64_t mem_used_total = 0;
172
173 int matched = fscanf(f, "%" SCNu64, &mem_used_total);
174 if (matched != 1)
175 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mem_used_total\n");
176
177 fclose(f);
178 return mem_used_total;
179 }
180
181 return 0;
182}
183
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800184int main(int argc, char *argv[]) {
185 pm_kernel_t *ker;
186 pm_process_t *proc;
187 pid_t *pids;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800188 size_t num_procs;
Colin Cross4d2488a2014-06-17 14:38:34 -0700189 uint64_t total_pss;
190 uint64_t total_uss;
191 uint64_t total_swap;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800192 uint64_t total_pswap;
193 uint64_t total_uswap;
194 uint64_t total_zswap;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800195 int error;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800196 bool has_swap = false, has_zram = false;
Colin Cross8a807c32013-06-21 17:02:05 -0700197 uint64_t required_flags = 0;
198 uint64_t flags_mask = 0;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800199
Kenny Root16abe7a2010-09-21 10:46:57 -0700200 int arg;
Colin Crossbaa84a22016-05-23 16:50:59 -0700201 size_t i;
202
203 enum {
204 WS_OFF,
205 WS_ONLY,
206 WS_RESET,
207 } ws;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800208
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800209 uint64_t mem[MEMINFO_COUNT] = { };
210 pm_proportional_swap_t *p_swap;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800211 float zram_cr = 0.0;
212
JP Abgrall80cb1552012-05-11 14:09:59 -0700213 signal(SIGPIPE, SIG_IGN);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800214 compfn = &sort_by_pss;
215 order = -1;
216 ws = WS_OFF;
Colin Crossf8d8f042016-05-24 11:05:31 -0700217 bool oomadj = false;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800218
Kenny Root16abe7a2010-09-21 10:46:57 -0700219 for (arg = 1; arg < argc; arg++) {
220 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
221 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
222 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
223 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700224 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
Colin Crossf8d8f042016-05-24 11:05:31 -0700225 if (!strcmp(argv[arg], "-o")) { compfn = &sort_by_oomadj; oomadj = true; continue; }
Elliott Hughes9eed6242016-08-23 17:47:49 -0700226 if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = KPF_SWAPBACKED; continue; }
227 if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = KPF_SWAPBACKED; continue; }
228 if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = KPF_KSM; continue; }
Kenny Root16abe7a2010-09-21 10:46:57 -0700229 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
230 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
231 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
232 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
233 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800234 usage(argv[0]);
235 exit(EXIT_FAILURE);
236 }
237
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800238 get_mem_info(mem);
239 p_swap = pm_memusage_pswap_create(mem[MEMINFO_SWAP_TOTAL] * 1024);
240
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800241 error = pm_kernel_create(&ker);
242 if (error) {
243 fprintf(stderr, "Error creating kernel interface -- "
244 "does this kernel have pagemap?\n");
245 exit(EXIT_FAILURE);
246 }
247
248 error = pm_kernel_pids(ker, &pids, &num_procs);
249 if (error) {
250 fprintf(stderr, "Error listing processes.\n");
251 exit(EXIT_FAILURE);
252 }
253
Colin Crossbaa84a22016-05-23 16:50:59 -0700254 std::vector<proc_info> procs(num_procs);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800255 for (i = 0; i < num_procs; i++) {
Colin Crossbaa84a22016-05-23 16:50:59 -0700256 procs[i].pid = pids[i];
Colin Crossf8d8f042016-05-24 11:05:31 -0700257 procs[i].oomadj = getoomadj(pids[i]);
Colin Crossbaa84a22016-05-23 16:50:59 -0700258 pm_memusage_zero(&procs[i].usage);
259 pm_memusage_pswap_init_handle(&procs[i].usage, p_swap);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800260 error = pm_process_create(ker, pids[i], &proc);
Colin Crosseff78882011-07-12 22:30:14 -0700261 if (error) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800262 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
Colin Crosseff78882011-07-12 22:30:14 -0700263 continue;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800264 }
Colin Crosseff78882011-07-12 22:30:14 -0700265
266 switch (ws) {
267 case WS_OFF:
Colin Crossbaa84a22016-05-23 16:50:59 -0700268 error = pm_process_usage_flags(proc, &procs[i].usage, flags_mask,
Colin Cross8a807c32013-06-21 17:02:05 -0700269 required_flags);
Colin Crosseff78882011-07-12 22:30:14 -0700270 break;
271 case WS_ONLY:
Colin Crossbaa84a22016-05-23 16:50:59 -0700272 error = pm_process_workingset(proc, &procs[i].usage, 0);
Colin Crosseff78882011-07-12 22:30:14 -0700273 break;
274 case WS_RESET:
275 error = pm_process_workingset(proc, NULL, 1);
276 break;
277 }
278
279 if (error) {
280 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
281 }
282
Colin Crossbaa84a22016-05-23 16:50:59 -0700283 if (ws != WS_RESET && procs[i].usage.swap) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700284 has_swap = true;
285 }
286
Colin Crosseff78882011-07-12 22:30:14 -0700287 pm_process_destroy(proc);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800288 }
289
290 free(pids);
291
292 if (ws == WS_RESET) exit(0);
293
Colin Crossbaa84a22016-05-23 16:50:59 -0700294 procs.erase(std::remove_if(procs.begin(),
295 procs.end(),
296 [](auto proc){
297 return proc.usage.vss == 0;
298 }),
299 procs.end());
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800300
Colin Crossbaa84a22016-05-23 16:50:59 -0700301 qsort(procs.data(), procs.size(), sizeof(procs[0]), compfn);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800302
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800303 if (has_swap) {
Greg Hackmannfbc3d452015-12-09 14:55:53 -0800304 uint64_t zram_mem_used = get_zram_mem_used();
305 if (zram_mem_used) {
306 mem[MEMINFO_ZRAM_TOTAL] = zram_mem_used/1024;
307 zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] /
308 (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]);
309 has_zram = true;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800310 }
311 }
312
Colin Cross6f5b13c2013-06-21 12:54:13 -0700313 printf("%5s ", "PID");
Colin Crossf8d8f042016-05-24 11:05:31 -0700314 if (oomadj) {
315 printf("%5s ", "oom");
316 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700317 if (ws) {
Thierry Strudel5e20c252015-11-20 12:45:09 -0800318 printf("%7s %7s %7s ", "WRss", "WPss", "WUss");
Colin Cross6f5b13c2013-06-21 12:54:13 -0700319 if (has_swap) {
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800320 printf("%7s %7s %7s ", "WSwap", "WPSwap", "WUSwap");
321 if (has_zram) {
322 printf("%7s ", "WZSwap");
323 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700324 }
325 } else {
326 printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
327 if (has_swap) {
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800328 printf("%7s %7s %7s ", "Swap", "PSwap", "USwap");
329 if (has_zram) {
330 printf("%7s ", "ZSwap");
331 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700332 }
333 }
334
335 printf("%s\n", "cmdline");
Kenny Root16abe7a2010-09-21 10:46:57 -0700336
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700337 total_pss = 0;
338 total_uss = 0;
Colin Cross6f5b13c2013-06-21 12:54:13 -0700339 total_swap = 0;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800340 total_pswap = 0;
341 total_uswap = 0;
342 total_zswap = 0;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700343
Colin Crossf8d8f042016-05-24 11:05:31 -0700344 std::vector<uint64_t> lmk_minfree;
345 std::vector<int> lmk_adj;
346 if (oomadj) {
347 getminfree(&lmk_minfree, &lmk_adj);
348 }
349 auto lmk_minfree_it = lmk_minfree.cbegin();
350 auto lmk_adj_it = lmk_adj.cbegin();
351
352 auto print_oomadj_totals = [&](int adj){
353 for (; lmk_adj_it != lmk_adj.cend() && lmk_minfree_it != lmk_minfree.cend() &&
354 adj > *lmk_adj_it; lmk_adj_it++, lmk_minfree_it++) {
355 // Print the cumulative total line
356 printf("%5s ", ""); // pid
357
358 printf("%5s ", ""); // oomadj
359
360 if (ws) {
361 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
362 "", total_pss / 1024, total_uss / 1024);
363 } else {
364 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
365 "", "", total_pss / 1024, total_uss / 1024);
366 }
367
368 if (has_swap) {
369 printf("%6" PRIu64 "K ", total_swap / 1024);
370 printf("%6" PRIu64 "K ", total_pswap / 1024);
371 printf("%6" PRIu64 "K ", total_uswap / 1024);
372 if (has_zram) {
373 printf("%6" PRIu64 "K ", total_zswap / 1024);
374 }
375 }
376
377 printf("TOTAL for oomadj < %d (%6" PRIu64 "K)\n", *lmk_adj_it, *lmk_minfree_it / 1024);
378 }
379 };
380
Colin Crossbaa84a22016-05-23 16:50:59 -0700381 for (auto& proc: procs) {
Colin Crossf8d8f042016-05-24 11:05:31 -0700382 if (oomadj) {
383 print_oomadj_totals(proc.oomadj);
Kenny Root16abe7a2010-09-21 10:46:57 -0700384 }
385
Colin Crossbaa84a22016-05-23 16:50:59 -0700386 std::string cmdline = getprocname(proc.pid);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700387
Colin Crossbaa84a22016-05-23 16:50:59 -0700388 total_pss += proc.usage.pss;
389 total_uss += proc.usage.uss;
390 total_swap += proc.usage.swap;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800391
Colin Crossbaa84a22016-05-23 16:50:59 -0700392 printf("%5d ", proc.pid);
Colin Cross6f5b13c2013-06-21 12:54:13 -0700393
Colin Crossf8d8f042016-05-24 11:05:31 -0700394 if (oomadj) {
395 printf("%5d ", proc.oomadj);
396 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700397
398 if (ws) {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000399 printf("%6zuK %6zuK %6zuK ",
Colin Crossbaa84a22016-05-23 16:50:59 -0700400 proc.usage.rss / 1024,
401 proc.usage.pss / 1024,
402 proc.usage.uss / 1024
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800403 );
Colin Cross6f5b13c2013-06-21 12:54:13 -0700404 } else {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000405 printf("%7zuK %6zuK %6zuK %6zuK ",
Colin Crossbaa84a22016-05-23 16:50:59 -0700406 proc.usage.vss / 1024,
407 proc.usage.rss / 1024,
408 proc.usage.pss / 1024,
409 proc.usage.uss / 1024
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800410 );
Colin Cross6f5b13c2013-06-21 12:54:13 -0700411 }
412
413 if (has_swap) {
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800414 pm_swapusage_t su;
415
Colin Crossbaa84a22016-05-23 16:50:59 -0700416 pm_memusage_pswap_get_usage(&proc.usage, &su);
417 printf("%6zuK ", proc.usage.swap / 1024);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800418 printf("%6zuK ", su.proportional / 1024);
419 printf("%6zuK ", su.unique / 1024);
420 total_pswap += su.proportional;
421 total_uswap += su.unique;
Colin Crossbaa84a22016-05-23 16:50:59 -0700422 pm_memusage_pswap_free(&proc.usage);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800423 if (has_zram) {
424 size_t zpswap = su.proportional * zram_cr;
425 printf("%6zuK ", zpswap / 1024);
426 total_zswap += zpswap;
427 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700428 }
429
Colin Crossbaa84a22016-05-23 16:50:59 -0700430 printf("%s\n", cmdline.c_str());
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800431 }
432
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800433 pm_memusage_pswap_destroy(p_swap);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700434
Colin Crossf8d8f042016-05-24 11:05:31 -0700435 if (oomadj) {
436 print_oomadj_totals(INT_MAX);
437 }
438
Colin Crossbaa84a22016-05-23 16:50:59 -0700439 // Print the separator line
Colin Cross6f5b13c2013-06-21 12:54:13 -0700440 printf("%5s ", "");
441
Colin Crossf8d8f042016-05-24 11:05:31 -0700442 if (oomadj) {
443 printf("%5s ", "");
444 }
445
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700446 if (ws) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700447 printf("%7s %7s %7s ", "", "------", "------");
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700448 } else {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700449 printf("%8s %7s %7s %7s ", "", "", "------", "------");
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700450 }
451
Colin Cross6f5b13c2013-06-21 12:54:13 -0700452 if (has_swap) {
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800453 printf("%7s %7s %7s ", "------", "------", "------");
454 if (has_zram) {
455 printf("%7s ", "------");
456 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700457 }
458
459 printf("%s\n", "------");
460
Colin Crossbaa84a22016-05-23 16:50:59 -0700461 // Print the total line
Colin Cross6f5b13c2013-06-21 12:54:13 -0700462 printf("%5s ", "");
Colin Crossf8d8f042016-05-24 11:05:31 -0700463
464 if (oomadj) {
465 printf("%5s ", "");
466 }
467
Colin Cross6f5b13c2013-06-21 12:54:13 -0700468 if (ws) {
Thierry Strudel5e20c252015-11-20 12:45:09 -0800469 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
Colin Cross6f5b13c2013-06-21 12:54:13 -0700470 "", total_pss / 1024, total_uss / 1024);
471 } else {
Colin Cross4d2488a2014-06-17 14:38:34 -0700472 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
Colin Cross6f5b13c2013-06-21 12:54:13 -0700473 "", "", total_pss / 1024, total_uss / 1024);
474 }
475
476 if (has_swap) {
Yu Liuaa5a7df2015-06-18 10:46:29 -0700477 printf("%6" PRIu64 "K ", total_swap / 1024);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800478 printf("%6" PRIu64 "K ", total_pswap / 1024);
479 printf("%6" PRIu64 "K ", total_uswap / 1024);
480 if (has_zram) {
481 printf("%6" PRIu64 "K ", total_zswap / 1024);
482 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700483 }
484
485 printf("TOTAL\n");
486
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700487 printf("\n");
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800488
489 if (has_swap) {
490 printf("ZRAM: %" PRIu64 "K physical used for %" PRIu64 "K in swap "
491 "(%" PRIu64 "K total swap)\n",
492 mem[MEMINFO_ZRAM_TOTAL], (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]),
493 mem[MEMINFO_SWAP_TOTAL]);
494 }
495 printf(" RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, "
496 "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n",
497 mem[MEMINFO_TOTAL], mem[MEMINFO_FREE], mem[MEMINFO_BUFFERS],
498 mem[MEMINFO_CACHED], mem[MEMINFO_SHMEM], mem[MEMINFO_SLAB]);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700499
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800500 return 0;
501}
502
503static void usage(char *myname) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700504 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800505 " -v Sort by VSS.\n"
506 " -r Sort by RSS.\n"
507 " -p Sort by PSS.\n"
508 " -u Sort by USS.\n"
Colin Cross6f5b13c2013-06-21 12:54:13 -0700509 " -s Sort by swap.\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800510 " (Default sort order is PSS.)\n"
511 " -R Reverse sort order (default is descending).\n"
Colin Cross8a807c32013-06-21 17:02:05 -0700512 " -c Only show cached (storage backed) pages\n"
513 " -C Only show non-cached (ram/swap backed) pages\n"
514 " -k Only show pages collapsed by KSM\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800515 " -w Display statistics for working set only.\n"
516 " -W Reset working set of all processes.\n"
Colin Crossf8d8f042016-05-24 11:05:31 -0700517 " -o Show and sort by oom score against lowmemorykiller thresholds.\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800518 " -h Display this help screen.\n",
519 myname);
520}
521
Colin Crossbaa84a22016-05-23 16:50:59 -0700522// Get the process name for a given PID.
523static std::string getprocname(pid_t pid) {
524 std::string filename = android::base::StringPrintf("/proc/%d/cmdline", pid);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800525
Colin Crossbaa84a22016-05-23 16:50:59 -0700526 std::string procname;
527
528 if (!android::base::ReadFileToString(filename, &procname)) {
529 // The process went away before we could read its process name.
530 procname = "<unknown>";
Kenny Root16abe7a2010-09-21 10:46:57 -0700531 }
532
Colin Crossbaa84a22016-05-23 16:50:59 -0700533 return procname;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800534}
535
Colin Crossf8d8f042016-05-24 11:05:31 -0700536static int getoomadj(pid_t pid) {
537 std::string filename = android::base::StringPrintf("/proc/%d/oom_score_adj", pid);
538 std::string oomadj;
539
540 if (!android::base::ReadFileToString(filename, &oomadj)) {
541 return -1001;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800542 }
543
Colin Crossf8d8f042016-05-24 11:05:31 -0700544 return strtol(oomadj.c_str(), NULL, 10);
545}
546
547static bool getminfree(std::vector<uint64_t>* minfree, std::vector<int>* adj) {
548 std::string minfree_str;
549 std::string adj_str;
550
551 if (!android::base::ReadFileToString("/sys/module/lowmemorykiller/parameters/minfree", &minfree_str)) {
552 return false;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800553 }
554
Colin Crossf8d8f042016-05-24 11:05:31 -0700555 if (!android::base::ReadFileToString("/sys/module/lowmemorykiller/parameters/adj", &adj_str)) {
556 return false;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800557 }
558
Colin Crossf8d8f042016-05-24 11:05:31 -0700559 std::vector<std::string> minfree_vec = android::base::Split(minfree_str, ",");
560 std::vector<std::string> adj_vec = android::base::Split(adj_str, ",");
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800561
Colin Crossf8d8f042016-05-24 11:05:31 -0700562 minfree->clear();
563 minfree->resize(minfree_vec.size());
564 adj->clear();
565 adj->resize(adj_vec.size());
566
567 std::transform(minfree_vec.begin(), minfree_vec.end(), minfree->begin(),
568 [](const std::string& s) -> uint64_t {
569 return strtoull(s.c_str(), NULL, 10) * PAGE_SIZE;
570 });
571
572 std::transform(adj_vec.begin(), adj_vec.end(), adj->begin(),
573 [](const std::string& s) -> int {
574 return strtol(s.c_str(), NULL, 10);
575 });
576
577 return true;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800578}
579
Colin Cross4d2488a2014-06-17 14:38:34 -0700580static int numcmp(uint64_t a, uint64_t b) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800581 if (a < b) return -1;
582 if (a > b) return 1;
583 return 0;
584}
585
Colin Crossf8d8f042016-05-24 11:05:31 -0700586static int snumcmp(int64_t a, int64_t b) {
587 if (a < b) return -1;
588 if (a > b) return 1;
589 return 0;
590}
591
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800592#define create_sort(field, compfn) \
593 static int sort_by_ ## field (const void *a, const void *b) { \
594 return order * compfn( \
Colin Crossbaa84a22016-05-23 16:50:59 -0700595 ((struct proc_info*)(a))->usage.field, \
596 ((struct proc_info*)(b))->usage.field \
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800597 ); \
598 }
599
600create_sort(vss, numcmp)
601create_sort(rss, numcmp)
602create_sort(pss, numcmp)
603create_sort(uss, numcmp)
Colin Cross6f5b13c2013-06-21 12:54:13 -0700604create_sort(swap, numcmp)
Colin Crossf8d8f042016-05-24 11:05:31 -0700605
606static int sort_by_oomadj (const void *a, const void *b) {
607 // Negative oomadj is higher priority, reverse the sort order
608 return -1 * order * snumcmp(
609 ((struct proc_info*)a)->oomadj,
610 ((struct proc_info*)b)->oomadj
611 );
612}