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);
51 void print_mem_info() {
55 int fd = open("/proc/meminfo", O_RDONLY);
58 printf("Unable to open /proc/meminfo: %s\n", strerror(errno));
62 const int len = read(fd, buffer, sizeof(buffer)-1);
66 printf("Empty /proc/meminfo");
71 static const char* const tags[] = {
80 static const int tagsLen[] = {
89 uint64_t mem[] = { 0, 0, 0, 0, 0, 0 };
92 while (*p && numFound < 6) {
95 if (strncmp(p, tags[i], tagsLen[i]) == 0) {
97 while (*p == ' ') p++;
99 while (*p >= '0' && *p <= '9') p++;
110 while (*p && *p != '\n') {
116 printf("RAM: %" PRIu64 "K total, %" PRIu64 "K free, %" PRIu64 "K buffers, "
117 "%" PRIu64 "K cached, %" PRIu64 "K shmem, %" PRIu64 "K slab\n",
118 mem[0], mem[1], mem[2], mem[3], mem[4], mem[5]);
121 int main(int argc, char *argv[]) {
125 struct proc_info **procs;
130 char cmdline[256]; // this must be within the range of int
132 bool has_swap = false;
133 uint64_t required_flags = 0;
134 uint64_t flags_mask = 0;
144 signal(SIGPIPE, SIG_IGN);
145 compfn = &sort_by_pss;
149 for (arg = 1; arg < argc; arg++) {
150 if (!strcmp(argv[arg], "-v")) { compfn = &sort_by_vss; continue; }
151 if (!strcmp(argv[arg], "-r")) { compfn = &sort_by_rss; continue; }
152 if (!strcmp(argv[arg], "-p")) { compfn = &sort_by_pss; continue; }
153 if (!strcmp(argv[arg], "-u")) { compfn = &sort_by_uss; continue; }
154 if (!strcmp(argv[arg], "-s")) { compfn = &sort_by_swap; continue; }
155 if (!strcmp(argv[arg], "-c")) { required_flags = 0; flags_mask = PM_PAGE_SWAPBACKED; continue; }
156 if (!strcmp(argv[arg], "-C")) { required_flags = flags_mask = PM_PAGE_SWAPBACKED; continue; }
157 if (!strcmp(argv[arg], "-k")) { required_flags = flags_mask = PM_PAGE_KSM; continue; }
158 if (!strcmp(argv[arg], "-w")) { ws = WS_ONLY; continue; }
159 if (!strcmp(argv[arg], "-W")) { ws = WS_RESET; continue; }
160 if (!strcmp(argv[arg], "-R")) { order *= -1; continue; }
161 if (!strcmp(argv[arg], "-h")) { usage(argv[0]); exit(0); }
162 fprintf(stderr, "Invalid argument \"%s\".\n", argv[arg]);
167 error = pm_kernel_create(&ker);
169 fprintf(stderr, "Error creating kernel interface -- "
170 "does this kernel have pagemap?\n");
174 error = pm_kernel_pids(ker, &pids, &num_procs);
176 fprintf(stderr, "Error listing processes.\n");
180 procs = calloc(num_procs, sizeof(struct proc_info*));
182 fprintf(stderr, "calloc: %s", strerror(errno));
186 for (i = 0; i < num_procs; i++) {
187 procs[i] = malloc(sizeof(struct proc_info));
188 if (procs[i] == NULL) {
189 fprintf(stderr, "malloc: %s\n", strerror(errno));
192 procs[i]->pid = pids[i];
193 pm_memusage_zero(&procs[i]->usage);
194 error = pm_process_create(ker, pids[i], &proc);
196 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
202 error = pm_process_usage_flags(proc, &procs[i]->usage, flags_mask,
206 error = pm_process_workingset(proc, &procs[i]->usage, 0);
209 error = pm_process_workingset(proc, NULL, 1);
214 fprintf(stderr, "warning: could not read usage for %d\n", pids[i]);
217 if (ws != WS_RESET && procs[i]->usage.swap) {
221 pm_process_destroy(proc);
226 if (ws == WS_RESET) exit(0);
229 for (i = 0; i < num_procs; i++) {
230 if (procs[i]->usage.vss) {
231 procs[j++] = procs[i];
238 qsort(procs, num_procs, sizeof(procs[0]), compfn);
240 printf("%5s ", "PID");
242 printf("%s %7s %7s ", "WRss", "WPss", "WUss");
244 printf("%7s ", "WSwap");
247 printf("%8s %7s %7s %7s ", "Vss", "Rss", "Pss", "Uss");
249 printf("%7s ", "Swap");
253 printf("%s\n", "cmdline");
259 for (i = 0; i < num_procs; i++) {
260 if (getprocname(procs[i]->pid, cmdline, (int)sizeof(cmdline)) < 0) {
262 * Something is probably seriously wrong if writing to the stack
269 total_pss += procs[i]->usage.pss;
270 total_uss += procs[i]->usage.uss;
271 total_swap += procs[i]->usage.swap;
273 printf("%5d ", procs[i]->pid);
276 printf("%6zuK %6zuK %6zuK ",
277 procs[i]->usage.rss / 1024,
278 procs[i]->usage.pss / 1024,
279 procs[i]->usage.uss / 1024
282 printf("%7zuK %6zuK %6zuK %6zuK ",
283 procs[i]->usage.vss / 1024,
284 procs[i]->usage.rss / 1024,
285 procs[i]->usage.pss / 1024,
286 procs[i]->usage.uss / 1024
291 printf("%6zuK ", procs[i]->usage.swap / 1024);
294 printf("%s\n", cmdline);
301 /* Print the separator line */
305 printf("%7s %7s %7s ", "", "------", "------");
307 printf("%8s %7s %7s %7s ", "", "", "------", "------");
311 printf("%7s ", "------");
314 printf("%s\n", "------");
316 /* Print the total line */
319 printf("%7s %6" PRIu64 "K %" PRIu64 "K ",
320 "", total_pss / 1024, total_uss / 1024);
322 printf("%8s %7s %6" PRIu64 "K %6" PRIu64 "K ",
323 "", "", total_pss / 1024, total_uss / 1024);
327 printf("%6" PRIu64 "K ", total_swap);
338 static void usage(char *myname) {
339 fprintf(stderr, "Usage: %s [ -W ] [ -v | -r | -p | -u | -s | -h ]\n"
344 " -s Sort by swap.\n"
345 " (Default sort order is PSS.)\n"
346 " -R Reverse sort order (default is descending).\n"
347 " -c Only show cached (storage backed) pages\n"
348 " -C Only show non-cached (ram/swap backed) pages\n"
349 " -k Only show pages collapsed by KSM\n"
350 " -w Display statistics for working set only.\n"
351 " -W Reset working set of all processes.\n"
352 " -h Display this help screen.\n",
357 * Get the process name for a given PID. Inserts the process name into buffer
358 * buf of length len. The size of the buffer must be greater than zero to get
361 * Note that fgets(3) only declares length as an int, so our buffer size is
362 * also declared as an int.
364 * Returns 0 on success, a positive value on partial success, and -1 on
365 * failure. Other interesting values:
366 * 1 on failure to create string to examine proc cmdline entry
367 * 2 on failure to open proc cmdline entry
368 * 3 on failure to read proc cmdline entry
370 static int getprocname(pid_t pid, char *buf, int len) {
374 static const char* unknown_cmdline = "<unknown>";
380 if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
385 f = fopen(filename, "r");
388 goto releasefilename;
391 if (fgets(buf, len, f) == NULL) {
403 * The process went away before we could read its process name. Try
404 * to give the user "<unknown>" here, but otherwise they get to look
407 if (strlcpy(buf, unknown_cmdline, (size_t)len) >= (size_t)len) {
415 static int numcmp(uint64_t a, uint64_t b) {
416 if (a < b) return -1;
421 #define create_sort(field, compfn) \
422 static int sort_by_ ## field (const void *a, const void *b) { \
423 return order * compfn( \
424 (*((struct proc_info**)a))->usage.field, \
425 (*((struct proc_info**)b))->usage.field \
429 create_sort(vss, numcmp)
430 create_sort(rss, numcmp)
431 create_sort(pss, numcmp)
432 create_sort(uss, numcmp)
433 create_sort(swap, numcmp)