blob: a6f834298e07aff944c06906df972a2980ed3e32 [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
Thierry Strudel08ea6e72015-11-20 18:07:15 -080051enum {
52 MEMINFO_TOTAL,
53 MEMINFO_FREE,
54 MEMINFO_BUFFERS,
55 MEMINFO_CACHED,
56 MEMINFO_SHMEM,
57 MEMINFO_SLAB,
58 MEMINFO_SWAP_TOTAL,
59 MEMINFO_SWAP_FREE,
60 MEMINFO_ZRAM_TOTAL,
61 MEMINFO_MAPPED,
62 MEMINFO_VMALLOC_USED,
63 MEMINFO_PAGE_TABLES,
64 MEMINFO_KERNEL_STACK,
65 MEMINFO_COUNT
66};
67
68void get_mem_info(uint64_t mem[]) {
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070069 char buffer[1024];
Thierry Strudel08ea6e72015-11-20 18:07:15 -080070 unsigned int numFound = 0;
Dianne Hackborne9eeec82011-07-18 12:41:50 -070071
72 int fd = open("/proc/meminfo", O_RDONLY);
73
74 if (fd < 0) {
75 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
76 return;
77 }
78
79 const int len = read(fd, buffer, sizeof(buffer)-1);
80 close(fd);
81
82 if (len < 0) {
83 printf("Empty /proc/meminfo");
84 return;
85 }
86 buffer[len] = 0;
87
88 static const char* const tags[] = {
89 "MemTotal:",
90 "MemFree:",
91 "Buffers:",
92 "Cached:",
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -070093 "Shmem:",
94 "Slab:",
Thierry Strudel08ea6e72015-11-20 18:07:15 -080095 "SwapTotal:",
96 "SwapFree:",
97 "ZRam:", /* not read from meminfo but from /sys/block/zram0 */
98 "Mapped:",
99 "VmallocUsed:",
100 "PageTables:",
101 "KernelStack:",
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700102 NULL
103 };
104 static const int tagsLen[] = {
105 9,
106 8,
107 8,
108 7,
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700109 6,
110 5,
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800111 10,
112 9,
113 5,
114 7,
115 12,
116 11,
117 12,
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700118 0
119 };
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700120
121 char* p = buffer;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800122 while (*p && (numFound < (sizeof(tagsLen) / sizeof(tagsLen[0])))) {
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700123 int i = 0;
124 while (tags[i]) {
125 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
126 p += tagsLen[i];
127 while (*p == ' ') p++;
128 char* num = p;
129 while (*p >= '0' && *p <= '9') p++;
130 if (*p != 0) {
131 *p = 0;
132 p++;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700133 }
134 mem[i] = atoll(num);
135 numFound++;
136 break;
137 }
138 i++;
139 }
Dianne Hackborn83b0b0a2011-11-04 20:08:19 -0700140 while (*p && *p != '\n') {
141 p++;
142 }
143 if (*p) p++;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700144 }
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700145}
146
Greg Hackmannfbc3d452015-12-09 14:55:53 -0800147static uint64_t get_zram_mem_used() {
148#define ZRAM_SYSFS "/sys/block/zram0/"
149 FILE *f = fopen(ZRAM_SYSFS "mm_stat", "r");
150 if (f) {
151 uint64_t mem_used_total = 0;
152
153 int matched = fscanf(f, "%*d %*d %" SCNu64 " %*d %*d %*d %*d", &mem_used_total);
154 if (matched != 1)
155 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mm_stat\n");
156
157 fclose(f);
158 return mem_used_total;
159 }
160
161 f = fopen(ZRAM_SYSFS "mem_used_total", "r");
162 if (f) {
163 uint64_t mem_used_total = 0;
164
165 int matched = fscanf(f, "%" SCNu64, &mem_used_total);
166 if (matched != 1)
167 fprintf(stderr, "warning: failed to parse " ZRAM_SYSFS "mem_used_total\n");
168
169 fclose(f);
170 return mem_used_total;
171 }
172
173 return 0;
174}
175
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800176int main(int argc, char *argv[]) {
177 pm_kernel_t *ker;
178 pm_process_t *proc;
179 pid_t *pids;
Kenny Root16abe7a2010-09-21 10:46:57 -0700180 struct proc_info **procs;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800181 size_t num_procs;
Colin Cross4d2488a2014-06-17 14:38:34 -0700182 uint64_t total_pss;
183 uint64_t total_uss;
184 uint64_t total_swap;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800185 uint64_t total_pswap;
186 uint64_t total_uswap;
187 uint64_t total_zswap;
Kenny Root16abe7a2010-09-21 10:46:57 -0700188 char cmdline[256]; // this must be within the range of int
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800189 int error;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800190 bool has_swap = false, has_zram = false;
Colin Cross8a807c32013-06-21 17:02:05 -0700191 uint64_t required_flags = 0;
192 uint64_t flags_mask = 0;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800193
194 #define WS_OFF 0
195 #define WS_ONLY 1
196 #define WS_RESET 2
197 int ws;
198
Kenny Root16abe7a2010-09-21 10:46:57 -0700199 int arg;
200 size_t i, j;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800201
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800202 uint64_t mem[MEMINFO_COUNT] = { };
203 pm_proportional_swap_t *p_swap;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800204 float zram_cr = 0.0;
205
JP Abgrall80cb1552012-05-11 14:09:59 -0700206 signal(SIGPIPE, SIG_IGN);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800207 compfn = &sort_by_pss;
208 order = -1;
209 ws = WS_OFF;
210
Kenny Root16abe7a2010-09-21 10:46:57 -0700211 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; }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700216 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
Colin Cross8a807c32013-06-21 17:02:05 -0700217 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; }
Kenny Root16abe7a2010-09-21 10:46:57 -0700220 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]);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800225 usage(argv[0]);
226 exit(EXIT_FAILURE);
227 }
228
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800229 get_mem_info(mem);
230 p_swap = pm_memusage_pswap_create(mem[MEMINFO_SWAP_TOTAL] * 1024);
231
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800232 error = pm_kernel_create(&ker);
233 if (error) {
234 fprintf(stderr, "Error creating kernel interface -- "
235 "does this kernel have pagemap?\n");
236 exit(EXIT_FAILURE);
237 }
238
239 error = pm_kernel_pids(ker, &pids, &num_procs);
240 if (error) {
241 fprintf(stderr, "Error listing processes.\n");
242 exit(EXIT_FAILURE);
243 }
244
Kenny Root16abe7a2010-09-21 10:46:57 -0700245 procs = calloc(num_procs, sizeof(struct proc_info*));
246 if (procs == NULL) {
247 fprintf(stderr, "calloc: %s", strerror(errno));
248 exit(EXIT_FAILURE);
249 }
250
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800251 for (i = 0; i < num_procs; i++) {
252 procs[i] = malloc(sizeof(struct proc_info));
Kenny Root16abe7a2010-09-21 10:46:57 -0700253 if (procs[i] == NULL) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800254 fprintf(stderr, "malloc: %s\n", strerror(errno));
255 exit(EXIT_FAILURE);
256 }
257 procs[i]->pid = pids[i];
Colin Crosseff78882011-07-12 22:30:14 -0700258 pm_memusage_zero(&procs[i]->usage);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800259 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 Cross8a807c32013-06-21 17:02:05 -0700268 error = pm_process_usage_flags(proc, &procs[i]->usage, flags_mask,
269 required_flags);
Colin Crosseff78882011-07-12 22:30:14 -0700270 break;
271 case WS_ONLY:
272 error = pm_process_workingset(proc, &procs[i]->usage, 0);
273 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 Cross6f5b13c2013-06-21 12:54:13 -0700283 if (ws != WS_RESET && procs[i]->usage.swap) {
284 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
294 j = 0;
295 for (i = 0; i < num_procs; i++) {
Kenny Root16abe7a2010-09-21 10:46:57 -0700296 if (procs[i]->usage.vss) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800297 procs[j++] = procs[i];
Kenny Root16abe7a2010-09-21 10:46:57 -0700298 } else {
299 free(procs[i]);
300 }
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800301 }
302 num_procs = j;
303
304 qsort(procs, num_procs, sizeof(procs[0]), compfn);
305
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800306 if (has_swap) {
Greg Hackmannfbc3d452015-12-09 14:55:53 -0800307 uint64_t zram_mem_used = get_zram_mem_used();
308 if (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]);
312 has_zram = true;
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800313 }
314 }
315
Colin Cross6f5b13c2013-06-21 12:54:13 -0700316 printf("%5s ", "PID");
317 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
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800344 for (i = 0; i < num_procs; i++) {
Kenny Root16abe7a2010-09-21 10:46:57 -0700345 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
346 /*
347 * Something is probably seriously wrong if writing to the stack
348 * failed.
349 */
350 free(procs[i]);
351 continue;
352 }
353
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700354 total_pss += procs[i]->usage.pss;
355 total_uss += procs[i]->usage.uss;
Colin Cross6f5b13c2013-06-21 12:54:13 -0700356 total_swap += procs[i]->usage.swap;
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700357
Colin Cross6f5b13c2013-06-21 12:54:13 -0700358 printf("%5d ", procs[i]->pid);
359
360 if (ws) {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000361 printf("%6zuK %6zuK %6zuK ",
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800362 procs[i]->usage.rss / 1024,
363 procs[i]->usage.pss / 1024,
Colin Cross6f5b13c2013-06-21 12:54:13 -0700364 procs[i]->usage.uss / 1024
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800365 );
Colin Cross6f5b13c2013-06-21 12:54:13 -0700366 } else {
Ashok Bhataf3263f2013-01-10 15:52:55 +0000367 printf("%7zuK %6zuK %6zuK %6zuK ",
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800368 procs[i]->usage.vss / 1024,
369 procs[i]->usage.rss / 1024,
370 procs[i]->usage.pss / 1024,
Colin Cross6f5b13c2013-06-21 12:54:13 -0700371 procs[i]->usage.uss / 1024
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800372 );
Colin Cross6f5b13c2013-06-21 12:54:13 -0700373 }
374
375 if (has_swap) {
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800376 pm_swapusage_t su;
377
378 pm_memusage_pswap_get_usage(&procs[i]->usage, &su);
Ashok Bhataf3263f2013-01-10 15:52:55 +0000379 printf("%6zuK ", procs[i]->usage.swap / 1024);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800380 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);
385 if (has_zram) {
386 size_t zpswap = su.proportional * zram_cr;
387 printf("%6zuK ", zpswap / 1024);
388 total_zswap += zpswap;
389 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700390 }
391
392 printf("%s\n", cmdline);
Kenny Root16abe7a2010-09-21 10:46:57 -0700393
394 free(procs[i]);
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800395 }
396
Kenny Root16abe7a2010-09-21 10:46:57 -0700397 free(procs);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800398 pm_memusage_pswap_destroy(p_swap);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700399
Colin Cross6f5b13c2013-06-21 12:54:13 -0700400 /* Print the separator line */
401 printf("%5s ", "");
402
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700403 if (ws) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700404 printf("%7s %7s %7s ", "", "------", "------");
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700405 } else {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700406 printf("%8s %7s %7s %7s ", "", "", "------", "------");
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700407 }
408
Colin Cross6f5b13c2013-06-21 12:54:13 -0700409 if (has_swap) {
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800410 printf("%7s %7s %7s ", "------", "------", "------");
411 if (has_zram) {
412 printf("%7s ", "------");
413 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700414 }
415
416 printf("%s\n", "------");
417
418 /* Print the total line */
419 printf("%5s ", "");
420 if (ws) {
Thierry Strudel5e20c252015-11-20 12:45:09 -0800421 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
Colin Cross6f5b13c2013-06-21 12:54:13 -0700422 "", total_pss / 1024, total_uss / 1024);
423 } else {
Colin Cross4d2488a2014-06-17 14:38:34 -0700424 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
Colin Cross6f5b13c2013-06-21 12:54:13 -0700425 "", "", total_pss / 1024, total_uss / 1024);
426 }
427
428 if (has_swap) {
Yu Liuaa5a7df2015-06-18 10:46:29 -0700429 printf("%6" PRIu64 "K ", total_swap / 1024);
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800430 printf("%6" PRIu64 "K ", total_pswap / 1024);
431 printf("%6" PRIu64 "K ", total_uswap / 1024);
432 if (has_zram) {
433 printf("%6" PRIu64 "K ", total_zswap / 1024);
434 }
Colin Cross6f5b13c2013-06-21 12:54:13 -0700435 }
436
437 printf("TOTAL\n");
438
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700439 printf("\n");
Thierry Strudel08ea6e72015-11-20 18:07:15 -0800440
441 if (has_swap) {
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]);
446 }
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]);
Dianne Hackborne9eeec82011-07-18 12:41:50 -0700451
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800452 return 0;
453}
454
455static void usage(char *myname) {
Colin Cross6f5b13c2013-06-21 12:54:13 -0700456 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800457 " -v Sort by VSS.\n"
458 " -r Sort by RSS.\n"
459 " -p Sort by PSS.\n"
460 " -u Sort by USS.\n"
Colin Cross6f5b13c2013-06-21 12:54:13 -0700461 " -s Sort by swap.\n"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800462 " (Default sort order is PSS.)\n"
463 " -R Reverse sort order (default is descending).\n"
Colin Cross8a807c32013-06-21 17:02:05 -0700464 " -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"
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800467 " -w Display statistics for working set only.\n"
468 " -W Reset working set of all processes.\n"
469 " -h Display this help screen.\n",
470 myname);
471}
472
Kenny Root16abe7a2010-09-21 10:46:57 -0700473/*
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
476 * any useful output.
477 *
478 * Note that fgets(3) only declares length as an int, so our buffer size is
479 * also declared as an int.
480 *
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
486 */
487static int getprocname(pid_t pid, char *buf, int len) {
488 char *filename;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800489 FILE *f;
Kenny Root16abe7a2010-09-21 10:46:57 -0700490 int rc = 0;
491 static const char* unknown_cmdline = "<unknown>";
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800492
Kenny Root16abe7a2010-09-21 10:46:57 -0700493 if (len <= 0) {
494 return -1;
495 }
496
Ashok Bhataf3263f2013-01-10 15:52:55 +0000497 if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
Kenny Root16abe7a2010-09-21 10:46:57 -0700498 rc = 1;
499 goto exit;
500 }
501
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800502 f = fopen(filename, "r");
Kenny Root16abe7a2010-09-21 10:46:57 -0700503 if (f == NULL) {
504 rc = 2;
505 goto releasefilename;
506 }
507
508 if (fgets(buf, len, f) == NULL) {
509 rc = 3;
510 goto closefile;
511 }
512
513closefile:
514 (void) fclose(f);
515releasefilename:
516 free(filename);
517exit:
518 if (rc != 0) {
519 /*
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
522 * at a blank.
523 */
524 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
525 rc = 4;
526 }
527 }
528
529 return rc;
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800530}
531
Colin Cross4d2488a2014-06-17 14:38:34 -0700532static int numcmp(uint64_t a, uint64_t b) {
The Android Open Source Projecte16cb842009-03-03 19:32:58 -0800533 if (a < b) return -1;
534 if (a > b) return 1;
535 return 0;
536}
537
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 \
543 ); \
544 }
545
546create_sort(vss, numcmp)
547create_sort(rss, numcmp)
548create_sort(pss, numcmp)
549create_sort(uss, numcmp)
Colin Cross6f5b13c2013-06-21 12:54:13 -0700550create_sort(swap, numcmp)