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 int main(int argc, char *argv[]) {
151 struct proc_info **procs;
156 uint64_t total_pswap;
157 uint64_t total_uswap;
158 uint64_t total_zswap;
159 char cmdline[256]; // this must be within the range of int
161 bool has_swap = false, has_zram = false;
162 uint64_t required_flags = 0;
163 uint64_t flags_mask = 0;
173 uint64_t mem[MEMINFO_COUNT] = { };
174 pm_proportional_swap_t *p_swap;
179 signal(SIGPIPE, SIG_IGN);
180 compfn = &sort_by_pss;
184 for (arg = 1; arg < argc; arg++) {
185 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
186 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
187 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
188 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
189 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
190 if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = PM_PAGE_SWAPBACKED; continue; }
191 if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = PM_PAGE_SWAPBACKED; continue; }
192 if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = PM_PAGE_KSM; continue; }
193 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
194 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
195 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
196 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
197 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
203 p_swap = pm_memusage_pswap_create(mem[MEMINFO_SWAP_TOTAL] * 1024);
205 error = pm_kernel_create(&ker);
207 fprintf(stderr, "Error creating kernel interface -- "
208 "does this kernel have pagemap?\n");
212 error = pm_kernel_pids(ker, &pids, &num_procs);
214 fprintf(stderr, "Error listing processes.\n");
218 procs = calloc(num_procs, sizeof(struct proc_info*));
220 fprintf(stderr, "calloc: %s", strerror(errno));
224 for (i = 0; i < num_procs; i++) {
225 procs[i] = malloc(sizeof(struct proc_info));
226 if (procs[i] == NULL) {
227 fprintf(stderr, "malloc: %s\n", strerror(errno));
230 procs[i]->pid = pids[i];
231 pm_memusage_zero(&procs[i]->usage);
232 pm_memusage_pswap_init_handle(&procs[i]->usage, p_swap);
233 error = pm_process_create(ker, pids[i], &proc);
235 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
241 error = pm_process_usage_flags(proc, &procs[i]->usage, flags_mask,
245 error = pm_process_workingset(proc, &procs[i]->usage, 0);
248 error = pm_process_workingset(proc, NULL, 1);
253 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
256 if (ws != WS_RESET && procs[i]->usage.swap) {
260 pm_process_destroy(proc);
265 if (ws == WS_RESET) exit(0);
268 for (i = 0; i < num_procs; i++) {
269 if (procs[i]->usage.vss) {
270 procs[j++] = procs[i];
277 qsort(procs, num_procs, sizeof(procs[0]), compfn);
280 fd = open("/sys/block/zram0/mem_used_total", O_RDONLY);
282 len = read(fd, buffer, sizeof(buffer)-1);
286 mem[MEMINFO_ZRAM_TOTAL] = atoll(buffer)/1024;
287 zram_cr = (float) mem[MEMINFO_ZRAM_TOTAL] /
288 (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]);
294 printf("%5s ", "PID");
296 printf("%7s %7s %7s ", "WRss", "WPss", "WUss");
298 printf("%7s %7s %7s ", "WSwap", "WPSwap", "WUSwap");
300 printf("%7s ", "WZSwap");
304 printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
306 printf("%7s %7s %7s ", "Swap", "PSwap", "USwap");
308 printf("%7s ", "ZSwap");
313 printf("%s\n", "cmdline");
322 for (i = 0; i < num_procs; i++) {
323 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
325 * Something is probably seriously wrong if writing to the stack
332 total_pss += procs[i]->usage.pss;
333 total_uss += procs[i]->usage.uss;
334 total_swap += procs[i]->usage.swap;
336 printf("%5d ", procs[i]->pid);
339 printf("%6zuK %6zuK %6zuK ",
340 procs[i]->usage.rss / 1024,
341 procs[i]->usage.pss / 1024,
342 procs[i]->usage.uss / 1024
345 printf("%7zuK %6zuK %6zuK %6zuK ",
346 procs[i]->usage.vss / 1024,
347 procs[i]->usage.rss / 1024,
348 procs[i]->usage.pss / 1024,
349 procs[i]->usage.uss / 1024
356 pm_memusage_pswap_get_usage(&procs[i]->usage, &su);
357 printf("%6zuK ", procs[i]->usage.swap / 1024);
358 printf("%6zuK ", su.proportional / 1024);
359 printf("%6zuK ", su.unique / 1024);
360 total_pswap += su.proportional;
361 total_uswap += su.unique;
362 pm_memusage_pswap_free(&procs[i]->usage);
364 size_t zpswap = su.proportional * zram_cr;
365 printf("%6zuK ", zpswap / 1024);
366 total_zswap += zpswap;
370 printf("%s\n", cmdline);
376 pm_memusage_pswap_destroy(p_swap);
378 /* Print the separator line */
382 printf("%7s %7s %7s ", "", "------", "------");
384 printf("%8s %7s %7s %7s ", "", "", "------", "------");
388 printf("%7s %7s %7s ", "------", "------", "------");
390 printf("%7s ", "------");
394 printf("%s\n", "------");
396 /* Print the total line */
399 printf("%7s %6" PRIu64 "K %6" PRIu64 "K ",
400 "", total_pss / 1024, total_uss / 1024);
402 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
403 "", "", total_pss / 1024, total_uss / 1024);
407 printf("%6" PRIu64 "K ", total_swap / 1024);
408 printf("%6" PRIu64 "K ", total_pswap / 1024);
409 printf("%6" PRIu64 "K ", total_uswap / 1024);
411 printf("%6" PRIu64 "K ", total_zswap / 1024);
420 printf("ZRAM: %" PRIu64 "K physical used for %" PRIu64 "K in swap "
421 "(%" PRIu64 "K total swap)\n",
422 mem[MEMINFO_ZRAM_TOTAL], (mem[MEMINFO_SWAP_TOTAL] - mem[MEMINFO_SWAP_FREE]),
423 mem[MEMINFO_SWAP_TOTAL]);
425 printf(" RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, "
426 "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n",
427 mem[MEMINFO_TOTAL], mem[MEMINFO_FREE], mem[MEMINFO_BUFFERS],
428 mem[MEMINFO_CACHED], mem[MEMINFO_SHMEM], mem[MEMINFO_SLAB]);
433 static void usage(char *myname) {
434 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
439 " -s Sort by swap.\n"
440 " (Default sort order is PSS.)\n"
441 " -R Reverse sort order (default is descending).\n"
442 " -c Only show cached (storage backed) pages\n"
443 " -C Only show non-cached (ram/swap backed) pages\n"
444 " -k Only show pages collapsed by KSM\n"
445 " -w Display statistics for working set only.\n"
446 " -W Reset working set of all processes.\n"
447 " -h Display this help screen.\n",
452 * Get the process name for a given PID. Inserts the process name into buffer
453 * buf of length len. The size of the buffer must be greater than zero to get
456 * Note that fgets(3) only declares length as an int, so our buffer size is
457 * also declared as an int.
459 * Returns 0 on success, a positive value on partial success, and -1 on
460 * failure. Other interesting values:
461 * 1 on failure to create string to examine proc cmdline entry
462 * 2 on failure to open proc cmdline entry
463 * 3 on failure to read proc cmdline entry
465 static int getprocname(pid_t pid, char *buf, int len) {
469 static const char* unknown_cmdline = "<unknown>";
475 if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
480 f = fopen(filename, "r");
483 goto releasefilename;
486 if (fgets(buf, len, f) == NULL) {
498 * The process went away before we could read its process name. Try
499 * to give the user "<unknown>" here, but otherwise they get to look
502 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
510 static int numcmp(uint64_t a, uint64_t b) {
511 if (a < b) return -1;
516 #define create_sort(field, compfn) \
517 static int sort_by_ ## field (const void *a, const void *b) { \
518 return order * compfn( \
519 (*((struct proc_info**)a))->usage.field, \
520 (*((struct proc_info**)b))->usage.field \
524 create_sort(vss, numcmp)
525 create_sort(rss, numcmp)
526 create_sort(pss, numcmp)
527 create_sort(uss, numcmp)
528 create_sort(swap, numcmp)