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>
29 #define MAX_CMDLINE 256
33 char cmdline[MAX_CMDLINE];
37 struct process_info *proc;
42 struct library_info *next;
44 struct mapping_info **mappings;
45 size_t mappings_count;
47 pm_memusage_t total_usage;
50 static void usage(char *myname);
51 static int getprocname(pid_t pid, char *buf, size_t len);
52 static int numcmp(long long a, long long b);
53 static int licmp(const void *a, const void *b);
55 char *library_name_blacklist[] = { "[heap]", "[stack]", "", NULL };
57 #define declare_sort(field) \
58 static int sort_by_ ## field (const void *a, const void *b)
66 #define INIT_LIBRARIES 16
67 #define INIT_MAPPINGS 4
71 struct library_info **libraries;
72 size_t libraries_count;
73 size_t libraries_size;
75 struct library_info *get_library(const char *name, bool all) {
77 struct library_info *library;
80 for (i = 0; library_name_blacklist[i]; i++)
81 if (!strcmp(name, library_name_blacklist[i]))
89 for (i = 0; i < libraries_count; i++) {
90 if (!strcmp(libraries[i]->name, name))
94 if (libraries_count >= libraries_size) {
95 libraries = realloc(libraries, 2 * libraries_size * sizeof(struct library_info *));
97 fprintf(stderr, "Couldn't resize libraries array: %s\n", strerror(errno));
100 libraries_size = 2 * libraries_size;
103 library = calloc(1, sizeof(*library));
105 fprintf(stderr, "Couldn't allocate space for library struct: %s\n", strerror(errno));
108 library->name = malloc(strlen(name) + 1);
109 if (!library->name) {
110 fprintf(stderr, "Couldn't allocate space for library name: %s\n", strerror(errno));
113 strcpy(library->name, name);
114 library->mappings = malloc(INIT_MAPPINGS * sizeof(struct mapping_info *));
115 if (!library->mappings) {
116 fprintf(stderr, "Couldn't allocate space for library mappings array: %s\n", strerror(errno));
119 library->mappings_count = 0; library->mappings_size = INIT_MAPPINGS;
120 pm_memusage_zero(&library->total_usage);
122 libraries[libraries_count++] = library;
127 struct mapping_info *get_mapping(struct library_info *library, struct process_info *proc) {
128 struct mapping_info *mapping;
131 for (i = 0; i < library->mappings_count; i++) {
132 if (library->mappings[i]->proc == proc)
133 return library->mappings[i];
136 if (library->mappings_count >= library->mappings_size) {
137 library->mappings = realloc(library->mappings,
138 2 * library->mappings_size * sizeof(struct mapping_info*));
139 if (!library->mappings) {
140 fprintf(stderr, "Couldn't resize mappings array: %s\n", strerror(errno));
143 library->mappings_size = 2 * library->mappings_size;
146 mapping = calloc(1, sizeof(*mapping));
148 fprintf(stderr, "Couldn't allocate space for mapping struct: %s\n", strerror(errno));
151 mapping->proc = proc;
152 pm_memusage_zero(&mapping->usage);
154 library->mappings[library->mappings_count++] = mapping;
159 struct process_info *get_process(pid_t pid) {
160 struct process_info *process;
162 process = calloc(1, sizeof(*process));
164 fprintf(stderr, "Couldn't allocate space for process struct: %s\n", strerror(errno));
169 getprocname(pid, process->cmdline, sizeof(process->cmdline));
174 static int parse_perm(const char *perm)
190 fprintf(stderr, "Unknown permission '%c'\n", *perm);
198 int main(int argc, char *argv[]) {
202 int (*compfn)(const void *a, const void *b);
212 pm_memusage_t map_usage;
214 struct library_info *li, **lis;
215 struct mapping_info *mi, **mis;
216 struct process_info *pi;
222 uint64_t required_flags;
225 bool has_swap = false;
227 signal(SIGPIPE, SIG_IGN);
228 compfn = &sort_by_pss;
240 const struct option longopts[] = {
242 {"cached", 0, 0, 'c'},
243 {"nocached", 0, 0, 'C'},
251 {"reverse", 0, 0, 'R'},
252 {"path", required_argument, 0, 'P'},
253 {"perm", required_argument, 0, 'm'},
256 c = getopt_long(argc, argv, "acChkm:pP:uvrsR", longopts, NULL);
260 /* Alphabetical cases */
267 flags_mask = KPF_SWAPBACKED;
270 required_flags = KPF_SWAPBACKED;
271 flags_mask = KPF_SWAPBACKED;
274 required_flags = KPF_KSM;
275 flags_mask = KPF_KSM;
281 perm = parse_perm(optarg);
284 compfn = &sort_by_pss;
288 prefix_len = strlen(prefix);
291 compfn = &sort_by_uss;
294 compfn = &sort_by_vss;
297 compfn = &sort_by_rss;
300 compfn = &sort_by_swap;
306 fprintf(stderr, "Invalid argument \"%s\".\n", argv[optind - 1]);
317 libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *));
318 libraries_count = 0; libraries_size = INIT_LIBRARIES;
319 pm_memusage_zero(&map_usage);
321 error = pm_kernel_create(&ker);
323 fprintf(stderr, "Error initializing kernel interface -- "
324 "does this kernel have pagemap?\n");
328 error = pm_kernel_pids(ker, &pids, &num_procs);
330 fprintf(stderr, "Error listing processes.\n");
334 for (i = 0; i < num_procs; i++) {
335 error = pm_process_create(ker, pids[i], &proc);
337 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
341 pi = get_process(pids[i]);
343 error = pm_process_maps(proc, &maps, &num_maps);
345 fprintf(stderr, "Error listing maps for process %d.\n", proc->pid);
349 for (j = 0; j < num_maps; j++) {
350 if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len)))
353 if (perm && (pm_map_flags(maps[j]) & PM_MAP_PERMISSIONS) != perm)
356 li = get_library(pm_map_name(maps[j]), all);
360 mi = get_mapping(li, pi);
362 error = pm_map_usage_flags(maps[j], &map_usage, flags_mask,
365 fprintf(stderr, "Error getting map memory usage of "
366 "map %s in process %d.\n",
367 pm_map_name(maps[j]), proc->pid);
371 if (map_usage.swap) {
375 pm_memusage_add(&mi->usage, &map_usage);
376 pm_memusage_add(&li->total_usage, &map_usage);
380 printf(" %6s %7s %6s %6s %6s ", "RSStot", "VSS", "RSS", "PSS", "USS");
383 printf(" %6s ", "Swap");
386 printf("Name/PID\n");
389 qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp);
391 for (i = 0; i < libraries_count; i++) {
394 printf("%6zdK %7s %6s %6s %6s ", li->total_usage.pss / 1024, "", "", "", "");
398 printf("%s\n", li->name);
401 qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn);
403 for (j = 0; j < li->mappings_count; j++) {
404 mi = li->mappings[j];
406 printf( " %6s %7zdK %6zdK %6zdK %6zdK ", "",
407 mi->usage.vss / 1024,
408 mi->usage.rss / 1024,
409 mi->usage.pss / 1024,
410 mi->usage.uss / 1024);
412 printf("%6zdK ", mi->usage.swap / 1024);
425 static void usage(char *myname) {
426 fprintf(stderr, "Usage: %s [ -P | -L ] [ -v | -r | -p | -u | -s | -h ]\n"
429 " -v Sort processes by VSS.\n"
430 " -r Sort processes by RSS.\n"
431 " -p Sort processes by PSS.\n"
432 " -u Sort processes by USS.\n"
433 " -s Sort processes by swap.\n"
434 " (Default sort order is PSS.)\n"
435 " -a Show all mappings, including stack, heap and anon.\n"
436 " -P /path Limit libraries displayed to those in path.\n"
437 " -R Reverse sort order (default is descending).\n"
438 " -m [r][w][x] Only list pages that exactly match permissions\n"
439 " -c Only show cached (storage backed) pages\n"
440 " -C Only show non-cached (ram/swap backed) pages\n"
441 " -k Only show pages collapsed by KSM\n"
442 " -h Display this help screen.\n",
446 static int getprocname(pid_t pid, char *buf, size_t len) {
450 snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
451 f = fopen(filename, "r");
456 if (!fgets(buf, len, f)) {
465 static int numcmp(long long a, long long b) {
466 if (a < b) return -1;
471 static int licmp(const void *a, const void *b) {
472 return order * numcmp(
473 (*((struct library_info**)a))->total_usage.pss,
474 (*((struct library_info**)b))->total_usage.pss
478 #define create_sort(field, compfn) \
479 static int sort_by_ ## field (const void *a, const void *b) { \
480 return order * compfn( \
481 (*((struct mapping_info**)a))->usage.field, \
482 (*((struct mapping_info**)b))->usage.field \
486 create_sort(vss, numcmp)
487 create_sort(rss, numcmp)
488 create_sort(pss, numcmp)
489 create_sort(uss, numcmp)
490 create_sort(swap, numcmp)