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.
22 #include <sys/types.h>
25 #include <pagemap/pagemap.h>
27 #define MAX_CMDLINE 256
31 char cmdline[MAX_CMDLINE];
35 struct process_info *proc;
40 struct library_info *next;
42 struct mapping_info **mappings;
45 pm_memusage_t total_usage;
48 static void usage(char *myname);
49 static int getprocname(pid_t pid, char *buf, size_t len);
50 static int numcmp(long long a, long long b);
51 static int licmp(const void *a, const void *b);
53 char *library_name_blacklist[] = { "[heap]", "[stack]", "", NULL };
55 #define declare_sort(field) \
56 static int sort_by_ ## field (const void *a, const void *b)
63 #define INIT_LIBRARIES 16
64 #define INIT_MAPPINGS 4
68 struct library_info **libraries;
72 struct library_info *get_library(char *name) {
74 struct library_info *library;
76 for (i = 0; library_name_blacklist[i]; i++)
77 if (!strcmp(name, library_name_blacklist[i]))
80 for (i = 0; i < libraries_count; i++) {
81 if (!strcmp(libraries[i]->name, name))
85 if (libraries_count >= libraries_size) {
86 libraries = realloc(libraries, 2 * libraries_size * sizeof(struct library_info *));
88 fprintf(stderr, "Couldn't resize libraries array: %s\n", strerror(errno));
91 libraries_size = 2 * libraries_size;
94 library = calloc(1, sizeof(*library));
96 fprintf(stderr, "Couldn't allocate space for library struct: %s\n", strerror(errno));
99 library->name = malloc(strlen(name) + 1);
100 if (!library->name) {
101 fprintf(stderr, "Couldn't allocate space for library name: %s\n", strerror(errno));
104 strcpy(library->name, name);
105 library->mappings = malloc(INIT_MAPPINGS * sizeof(struct mapping_info *));
106 if (!library->mappings) {
107 fprintf(stderr, "Couldn't allocate space for library mappings array: %s\n", strerror(errno));
110 library->mappings_count = 0; library->mappings_size = INIT_MAPPINGS;
111 pm_memusage_zero(&library->total_usage);
113 libraries[libraries_count++] = library;
118 struct mapping_info *get_mapping(struct library_info *library, struct process_info *proc) {
119 struct mapping_info *mapping;
122 for (i = 0; i < library->mappings_count; i++) {
123 if (library->mappings[i]->proc == proc)
124 return library->mappings[i];
127 if (library->mappings_count >= library->mappings_size) {
128 library->mappings = realloc(library->mappings,
129 2 * library->mappings_size * sizeof(struct mapping*));
130 if (!library->mappings) {
131 fprintf(stderr, "Couldn't resize mappings array: %s\n", strerror(errno));
134 library->mappings_size = 2 * library->mappings_size;
137 mapping = calloc(1, sizeof(*mapping));
139 fprintf(stderr, "Couldn't allocate space for mapping struct: %s\n", strerror(errno));
142 mapping->proc = proc;
143 pm_memusage_zero(&mapping->usage);
145 library->mappings[library->mappings_count++] = mapping;
150 struct process_info *get_process(pid_t pid) {
151 struct process_info *process;
153 process = calloc(1, sizeof(*process));
155 fprintf(stderr, "Couldn't allocate space for process struct: %s\n", strerror(errno));
160 getprocname(pid, process->cmdline, sizeof(process->cmdline));
165 int main(int argc, char *argv[]) {
169 int (*compfn)(const void *a, const void *b);
179 pm_memusage_t map_usage;
181 struct library_info *li, **lis;
182 struct mapping_info *mi, **mis;
183 struct process_info *pi;
187 compfn = &sort_by_pss;
192 for (i = 1; i < argc; i++) {
193 if (!strcmp(argv[i], "-P")) {
195 fprintf(stderr, "Option -P requires an argument.\n");
200 prefix_len = strlen(prefix);
203 if (!strcmp(argv[i], "-v")) { compfn = &sort_by_vss; continue; }
204 if (!strcmp(argv[i], "-r")) { compfn = &sort_by_rss; continue; }
205 if (!strcmp(argv[i], "-p")) { compfn = &sort_by_pss; continue; }
206 if (!strcmp(argv[i], "-u")) { compfn = &sort_by_uss; continue; }
207 if (!strcmp(argv[i], "-R")) { order *= -1; continue; }
208 if (!strcmp(argv[i], "-h")) { usage(argv[0]); exit(0); }
209 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
214 libraries = malloc(INIT_LIBRARIES * sizeof(struct library_info *));
215 libraries_count = 0; libraries_size = INIT_LIBRARIES;
217 error = pm_kernel_create(&ker);
219 fprintf(stderr, "Error initializing kernel interface -- "
220 "does this kernel have pagemap?\n");
224 error = pm_kernel_pids(ker, &pids, &num_procs);
226 fprintf(stderr, "Error listing processes.\n");
230 for (i = 0; i < num_procs; i++) {
231 error = pm_process_create(ker, pids[i], &proc);
233 fprintf(stderr, "warning: could not create process interface for %d\n", pids[i]);
237 pi = get_process(pids[i]);
239 error = pm_process_maps(proc, &maps, &num_maps);
241 fprintf(stderr, "Error listing maps for process %d.\n", proc->pid);
245 for (j = 0; j < num_maps; j++) {
246 if (prefix && (strncmp(pm_map_name(maps[j]), prefix, prefix_len)))
249 li = get_library(pm_map_name(maps[j]));
253 mi = get_mapping(li, pi);
255 error = pm_map_usage(maps[j], &map_usage);
257 fprintf(stderr, "Error getting map memory usage of "
258 "map %s in process %d.\n",
259 pm_map_name(maps[j]), proc->pid);
262 pm_memusage_add(&mi->usage, &map_usage);
263 pm_memusage_add(&li->total_usage, &map_usage);
267 printf( " %6s %6s %6s %6s %6s %s\n", "RSStot", "VSS", "RSS", "PSS", "USS", "Name/PID");
270 qsort(libraries, libraries_count, sizeof(libraries[0]), &licmp);
272 for (i = 0; i < libraries_count; i++) {
275 printf("%6dK %6s %6s %6s %6s %s\n", li->total_usage.pss / 1024, "", "", "", "", li->name);
278 qsort(li->mappings, li->mappings_count, sizeof(li->mappings[0]), compfn);
280 for (j = 0; j < li->mappings_count; j++) {
281 mi = li->mappings[j];
283 printf( " %6s %6dK %6dK %6dK %6dK %s [%d]\n", "",
284 mi->usage.vss / 1024,
285 mi->usage.rss / 1024,
286 mi->usage.pss / 1024,
287 mi->usage.uss / 1024,
298 static void usage(char *myname) {
299 fprintf(stderr, "Usage: %s [ -P | -L ] [ -v | -r | -p | -u | -h ]\n"
302 " -v Sort processes by VSS.\n"
303 " -r Sort processes by RSS.\n"
304 " -p Sort processes by PSS.\n"
305 " -u Sort processes by USS.\n"
306 " (Default sort order is PSS.)\n"
307 " -P /path Limit libraries displayed to those in path.\n"
308 " -R Reverse sort order (default is descending).\n"
309 " -h Display this help screen.\n",
313 static int getprocname(pid_t pid, char *buf, size_t len) {
317 sprintf(filename, "/proc/%d/cmdline", pid);
318 f = fopen(filename, "r");
319 if (!f) { *buf = '\0'; return 1; }
320 if (!fgets(buf, len, f)) { *buf = '\0'; return 2; }
325 static int numcmp(long long a, long long b) {
326 if (a < b) return -1;
331 static int licmp(const void *a, const void *b) {
332 return order * numcmp(
333 (*((struct library_info**)a))->total_usage.pss,
334 (*((struct library_info**)b))->total_usage.pss
338 #define create_sort(field, compfn) \
339 static int sort_by_ ## field (const void *a, const void *b) { \
340 return order * compfn( \
341 (*((struct mapping_info**)a))->usage.field, \
342 (*((struct mapping_info**)b))->usage.field \
346 create_sort(vss, numcmp)
347 create_sort(rss, numcmp)
348 create_sort(pss, numcmp)
349 create_sort(uss, numcmp)