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.
23 #include <pagemap/pagemap.h>
25 /* Information about a single mapping */
30 unsigned long shared_clean;
31 unsigned long shared_dirty;
32 unsigned long private_clean;
33 unsigned long private_dirty;
36 /* display the help screen */
37 static void usage(const char *cmd);
39 /* qsort compare function to compare maps by PSS */
40 int comp_pss(const void *a, const void *b);
42 int main(int argc, char *argv[]) {
45 /* libpagemap context */
47 int pagesize; /* cached for speed */
51 pm_map_t **maps; size_t num_maps;
53 struct map_info **mis = NULL;
56 /* pagemap information */
57 uint64_t *pagemap; size_t num_pages;
59 uint64_t count, flags;
62 unsigned long total_shared_clean, total_shared_dirty, total_private_clean, total_private_dirty;
63 pm_memusage_t total_usage;
65 /* command-line options */
70 int (*compfn)(const void *a, const void *b);
73 /* temporary variables */
86 for (i = 1; i < (size_t)(argc - 1); i++) {
87 if (!strcmp(argv[i], "-w")) { ws = WS_ONLY; continue; }
88 if (!strcmp(argv[i], "-W")) { ws = WS_RESET; continue; }
89 if (!strcmp(argv[i], "-m")) { compfn = NULL; continue; }
90 if (!strcmp(argv[i], "-p")) { compfn = &comp_pss; continue; }
91 if (!strcmp(argv[i], "-h")) { hide_zeros = 1; continue; }
92 fprintf(stderr, "Invalid argument \"%s\".\n", argv[i]);
97 pid = (pid_t)strtol(argv[argc - 1], &endptr, 10);
98 if (*endptr != '\0') {
99 fprintf(stderr, "Invalid PID \"%s\".\n", argv[argc - 1]);
103 error = pm_kernel_create(&ker);
105 fprintf(stderr, "error creating kernel interface -- "
106 "does this kernel have pagemap?\n");
110 pagesize = pm_kernel_pagesize(ker);
112 error = pm_process_create(ker, pid, &proc);
114 fprintf(stderr, "error creating process interface -- "
115 "does process %d really exist?\n", pid);
119 if (ws == WS_RESET) {
120 error = pm_process_workingset(proc, NULL, 1);
122 fprintf(stderr, "error resetting working set for process.\n");
128 /* get maps, and allocate our map_info array */
129 error = pm_process_maps(proc, &maps, &num_maps);
131 fprintf(stderr, "error listing maps.\n");
135 mis = (struct map_info **)calloc(num_maps, sizeof(struct map_info *));
137 fprintf(stderr, "error allocating map_info array: %s\n", strerror(errno));
143 printf("%7s %7s %7s %7s %7s %7s %7s %s\n",
144 "WRss", "WPss", "WUss", "WShCl", "WShDi", "WPrCl", "WPrDi", "Name");
145 printf("%7s %7s %7s %7s %7s %7s %7s %s\n",
146 "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
148 printf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n",
149 "Vss", "Rss", "Pss", "Uss", "ShCl", "ShDi", "PrCl", "PrDi", "Name");
150 printf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n",
151 "-------", "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
155 pm_memusage_zero(&total_usage);
156 total_shared_clean = total_shared_dirty = total_private_clean = total_private_dirty = 0;
158 for (i = 0; i < num_maps; i++) {
159 mi = (struct map_info *)calloc(1, sizeof(struct map_info));
161 fprintf(stderr, "error allocating map_info: %s\n", strerror(errno));
167 /* get, and sum, memory usage */
170 error = pm_map_workingset(mi->map, &mi->usage);
172 error = pm_map_usage(mi->map, &mi->usage);
175 fprintf(stderr, "error getting usage for map.\n");
179 pm_memusage_add(&total_usage, &mi->usage);
181 /* get, and sum, individual page counts */
183 error = pm_map_pagemap(mi->map, &pagemap, &num_pages);
186 fprintf(stderr, "error getting pagemap for map.\n");
190 mi->shared_clean = mi->shared_dirty = mi->private_clean = mi->private_dirty = 0;
192 for (j = 0; j < num_pages; j++) {
193 mapentry = pagemap[j];
195 if (PM_PAGEMAP_PRESENT(mapentry) && !PM_PAGEMAP_SWAPPED(mapentry)) {
197 error = pm_kernel_count(ker, PM_PAGEMAP_PFN(mapentry), &count);
200 fprintf(stderr, "error getting count for frame.\n");
203 error = pm_kernel_flags(ker, PM_PAGEMAP_PFN(mapentry), &flags);
206 fprintf(stderr, "error getting flags for frame.\n");
209 if ((ws != WS_ONLY) || (flags & PM_PAGE_REFERENCED)) {
211 if (flags & PM_PAGE_DIRTY)
216 if (flags & PM_PAGE_DIRTY)
225 total_shared_clean += mi->shared_clean;
226 total_shared_dirty += mi->shared_dirty;
227 total_private_clean += mi->private_clean;
228 total_private_dirty += mi->private_dirty;
234 /* sort the array, if requested (compfn == NULL for original order) */
236 qsort(mis, num_maps, sizeof(mis[0]), compfn);
238 for (i = 0; i < num_maps; i++) {
241 if ((!mi) || (hide_zeros && !mi->usage.rss))
245 printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n",
246 (long)mi->usage.rss / 1024,
247 (long)mi->usage.pss / 1024,
248 (long)mi->usage.uss / 1024,
249 mi->shared_clean * pagesize / 1024,
250 mi->shared_dirty * pagesize / 1024,
251 mi->private_clean * pagesize / 1024,
252 mi->private_dirty * pagesize / 1024,
256 printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n",
257 (long)mi->usage.vss / 1024,
258 (long)mi->usage.rss / 1024,
259 (long)mi->usage.pss / 1024,
260 (long)mi->usage.uss / 1024,
261 mi->shared_clean * pagesize / 1024,
262 mi->shared_dirty * pagesize / 1024,
263 mi->private_clean * pagesize / 1024,
264 mi->private_dirty * pagesize / 1024,
272 printf("%7s %7s %7s %7s %7s %7s %7s %s\n",
273 "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
274 printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n",
275 (long)total_usage.rss / 1024,
276 (long)total_usage.pss / 1024,
277 (long)total_usage.uss / 1024,
278 total_shared_clean * pagesize / 1024,
279 total_shared_dirty * pagesize / 1024,
280 total_private_clean * pagesize / 1024,
281 total_private_dirty * pagesize / 1024,
285 printf("%7s %7s %7s %7s %7s %7s %7s %7s %s\n",
286 "-------", "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
287 printf("%6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %6ldK %s\n",
288 (long)total_usage.vss / 1024,
289 (long)total_usage.rss / 1024,
290 (long)total_usage.pss / 1024,
291 (long)total_usage.uss / 1024,
292 total_shared_clean * pagesize / 1024,
293 total_shared_dirty * pagesize / 1024,
294 total_private_clean * pagesize / 1024,
295 total_private_dirty * pagesize / 1024,
304 static void usage(const char *cmd) {
305 fprintf(stderr, "Usage: %s [ -w | -W ] [ -p | -m ] [ -h ] pid\n"
306 " -w Displays statistics for the working set only.\n"
307 " -W Resets the working set of the process.\n"
309 " -m Sort by mapping order (as read from /proc).\n"
310 " -h Hide maps with no RSS.\n",
314 int comp_pss(const void *a, const void *b) {
315 struct map_info *ma, *mb;
317 ma = *((struct map_info **)a);
318 mb = *((struct map_info **)b);
320 if (mb->usage.pss < ma->usage.pss) return -1;
321 if (mb->usage.pss > ma->usage.pss) return 1;