• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include <errno.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 #include <pagemap/pagemap.h>
24 
25 /* Information about a single mapping */
26 struct map_info {
27     pm_map_t *map;
28     pm_memusage_t usage;
29     /* page counts */
30     unsigned long shared_clean;
31     unsigned long shared_dirty;
32     unsigned long private_clean;
33     unsigned long private_dirty;
34 };
35 
36 /* display the help screen */
37 static void usage(const char *cmd);
38 
39 /* qsort compare function to compare maps by PSS */
40 int comp_pss(const void *a, const void *b);
41 
main(int argc,char * argv[])42 int main(int argc, char *argv[]) {
43     pid_t pid;
44 
45     /* libpagemap context */
46     pm_kernel_t *ker;
47     int pagesize; /* cached for speed */
48     pm_process_t *proc;
49 
50     /* maps and such */
51     pm_map_t **maps; size_t num_maps;
52 
53     struct map_info **mis;
54     struct map_info *mi;
55 
56     /* pagemap information */
57     uint64_t *pagemap; size_t num_pages;
58     unsigned long address; uint64_t mapentry;
59     uint64_t count, flags;
60 
61     /* totals */
62     unsigned long total_shared_clean, total_shared_dirty, total_private_clean, total_private_dirty;
63     pm_memusage_t total_usage;
64 
65     /* command-line options */
66     int ws;
67 #define WS_OFF (0)
68 #define WS_ONLY (1)
69 #define WS_RESET (2)
70     int (*compfn)(const void *a, const void *b);
71     int hide_zeros;
72 
73     /* temporary variables */
74     size_t i, j;
75     char *endptr;
76     int error;
77 
78     if (argc < 2) {
79         usage(argv[0]);
80         exit(EXIT_FAILURE);
81     }
82 
83     ws = WS_OFF;
84     compfn = NULL;
85     hide_zeros = 0;
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]);
93         usage(argv[0]);
94         exit(EXIT_FAILURE);
95     }
96 
97     pid = (pid_t)strtol(argv[argc - 1], &endptr, 10);
98     if (*endptr != '\0') {
99         fprintf(stderr, "Invalid PID \"%s\".\n", argv[argc - 1]);
100         exit(EXIT_FAILURE);
101     }
102 
103     error = pm_kernel_create(&ker);
104     if (error) {
105         fprintf(stderr, "error creating kernel interface -- "
106                         "does this kernel have pagemap?\n");
107         exit(EXIT_FAILURE);
108     }
109 
110     pagesize = pm_kernel_pagesize(ker);
111 
112     error = pm_process_create(ker, pid, &proc);
113     if (error) {
114         fprintf(stderr, "error creating process interface -- "
115                         "does process %d really exist?\n", pid);
116         exit(EXIT_FAILURE);
117     }
118 
119     if (ws == WS_RESET) {
120         error = pm_process_workingset(proc, NULL, 1);
121         if (error) {
122             fprintf(stderr, "error resetting working set for process.\n");
123             exit(EXIT_FAILURE);
124         }
125         exit(EXIT_SUCCESS);
126     }
127 
128     /* get maps, and allocate our map_info array */
129     error = pm_process_maps(proc, &maps, &num_maps);
130     if (error) {
131         fprintf(stderr, "error listing maps.\n");
132         exit(EXIT_FAILURE);
133     }
134 
135     mis = (struct map_info **)calloc(num_maps, sizeof(struct map_info *));
136     if (!mis) {
137         fprintf(stderr, "error allocating map_info array: %s\n", strerror(errno));
138         exit(EXIT_FAILURE);
139     }
140 
141     /* print header */
142     if (ws == WS_ONLY) {
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                "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
147     } else {
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                "-------", "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
152     }
153 
154     /* zero things */
155     pm_memusage_zero(&total_usage);
156     total_shared_clean = total_shared_dirty = total_private_clean = total_private_dirty = 0;
157 
158     for (i = 0; i < num_maps; i++) {
159         mi = (struct map_info *)calloc(1, sizeof(struct map_info));
160         if (!mi) {
161             fprintf(stderr, "error allocating map_info: %s\n", strerror(errno));
162             exit(EXIT_FAILURE);
163         }
164 
165         mi->map = maps[i];
166 
167         /* get, and sum, memory usage */
168 
169         if (ws == WS_ONLY)
170             error = pm_map_workingset(mi->map, &mi->usage);
171         else
172             error = pm_map_usage(mi->map, &mi->usage);
173         if (error) {
174             fflush(stdout);
175             fprintf(stderr, "error getting usage for map.\n");
176             continue;
177         }
178 
179         pm_memusage_add(&total_usage, &mi->usage);
180 
181         /* get, and sum, individual page counts */
182 
183         error = pm_map_pagemap(mi->map, &pagemap, &num_pages);
184         if (error) {
185             fflush(stdout);
186             fprintf(stderr, "error getting pagemap for map.\n");
187             continue;
188         }
189 
190         mi->shared_clean = mi->shared_dirty = mi->private_clean = mi->private_dirty = 0;
191 
192         for (j = 0; j < num_pages; j++) {
193             address = pm_map_start(mi->map) + j * ker->pagesize;
194             mapentry = pagemap[j];
195 
196             if (PM_PAGEMAP_PRESENT(mapentry) && !PM_PAGEMAP_SWAPPED(mapentry)) {
197 
198                 error = pm_kernel_count(ker, PM_PAGEMAP_PFN(mapentry), &count);
199                 if (error) {
200                     fflush(stdout);
201                     fprintf(stderr, "error getting count for frame.\n");
202                 }
203 
204                 error = pm_kernel_flags(ker, PM_PAGEMAP_PFN(mapentry), &flags);
205                 if (error) {
206                     fflush(stdout);
207                     fprintf(stderr, "error getting flags for frame.\n");
208                 }
209 
210                 if ((ws != WS_ONLY) || (flags & PM_PAGE_REFERENCED)) {
211                     if (count > 1) {
212                         if (flags & PM_PAGE_DIRTY)
213                             mi->shared_dirty++;
214                         else
215                             mi->shared_clean++;
216                     } else {
217                         if (flags & PM_PAGE_DIRTY)
218                             mi->private_dirty++;
219                         else
220                             mi->private_clean++;
221                     }
222                 }
223             }
224         }
225 
226         total_shared_clean += mi->shared_clean;
227         total_shared_dirty += mi->shared_dirty;
228         total_private_clean += mi->private_clean;
229         total_private_dirty += mi->private_dirty;
230 
231         /* add to array */
232         mis[i] = mi;
233     }
234 
235     /* sort the array, if requested (compfn == NULL for original order) */
236     if (compfn)
237         qsort(mis, num_maps, sizeof(mis[0]), compfn);
238 
239     for (i = 0; i < num_maps; i++) {
240         mi = mis[i];
241 
242         if ((!mi) || (hide_zeros && !mi->usage.rss))
243             continue;
244 
245         if (ws == WS_ONLY) {
246             printf("%6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %s\n",
247                 (long)mi->usage.rss / 1024,
248                 (long)mi->usage.pss / 1024,
249                 (long)mi->usage.uss / 1024,
250                 mi->shared_clean * pagesize / 1024,
251                 mi->shared_dirty * pagesize / 1024,
252                 mi->private_clean * pagesize / 1024,
253                 mi->private_dirty * pagesize / 1024,
254                 pm_map_name(mi->map)
255             );
256         } else {
257             printf("%6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %s\n",
258                 (long)mi->usage.vss / 1024,
259                 (long)mi->usage.rss / 1024,
260                 (long)mi->usage.pss / 1024,
261                 (long)mi->usage.uss / 1024,
262                 mi->shared_clean * pagesize / 1024,
263                 mi->shared_dirty * pagesize / 1024,
264                 mi->private_clean * pagesize / 1024,
265                 mi->private_dirty * pagesize / 1024,
266                 pm_map_name(mi->map)
267             );
268         }
269     }
270 
271     /* print totals */
272     if (ws == WS_ONLY) {
273         printf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
274                "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
275         printf("%6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %s\n",
276             (long)total_usage.rss / 1024,
277             (long)total_usage.pss / 1024,
278             (long)total_usage.uss / 1024,
279             total_shared_clean * pagesize / 1024,
280             total_shared_dirty * pagesize / 1024,
281             total_private_clean * pagesize / 1024,
282             total_private_dirty * pagesize / 1024,
283             "TOTAL"
284         );
285     } else {
286         printf("%7s  %7s  %7s  %7s  %7s  %7s  %7s  %7s  %s\n",
287                "-------", "-------", "-------", "-------", "-------", "-------", "-------", "-------", "");
288         printf("%6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %6ldK  %s\n",
289             (long)total_usage.vss / 1024,
290             (long)total_usage.rss / 1024,
291             (long)total_usage.pss / 1024,
292             (long)total_usage.uss / 1024,
293             total_shared_clean * pagesize / 1024,
294             total_shared_dirty * pagesize / 1024,
295             total_private_clean * pagesize / 1024,
296             total_private_dirty * pagesize / 1024,
297             "TOTAL"
298         );
299     }
300 
301     return 0;
302 }
303 
usage(const char * cmd)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"
308                     "    -p  Sort by PSS.\n"
309                     "    -m  Sort by mapping order (as read from /proc).\n"
310                     "    -h  Hide maps with no RSS.\n",
311         cmd);
312 }
313 
comp_pss(const void * a,const void * b)314 int comp_pss(const void *a, const void *b) {
315     struct map_info *ma, *mb;
316 
317     ma = *((struct map_info **)a);
318     mb = *((struct map_info **)b);
319 
320     if (mb->usage.pss < ma->usage.pss) return -1;
321     if (mb->usage.pss > ma->usage.pss) return 1;
322     return 0;
323 }
324