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 */
26struct 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 */
37static void usage(const char *cmd);
38
39/* qsort compare function to compare maps by PSS */
40int comp_pss(const void *a, const void *b);
41
42int 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 = NULL;
54    struct map_info *mi;
55
56    /* pagemap information */
57    uint64_t *pagemap; size_t num_pages;
58    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            mapentry = pagemap[j];
194
195            if (PM_PAGEMAP_PRESENT(mapentry) && !PM_PAGEMAP_SWAPPED(mapentry)) {
196
197                error = pm_kernel_count(ker, PM_PAGEMAP_PFN(mapentry), &count);
198                if (error) {
199                    fflush(stdout);
200                    fprintf(stderr, "error getting count for frame.\n");
201                }
202
203                error = pm_kernel_flags(ker, PM_PAGEMAP_PFN(mapentry), &flags);
204                if (error) {
205                    fflush(stdout);
206                    fprintf(stderr, "error getting flags for frame.\n");
207                }
208
209                if ((ws != WS_ONLY) || (flags & KPF_REFERENCED)) {
210                    if (count > 1) {
211                        if (flags & KPF_DIRTY)
212                            mi->shared_dirty++;
213                        else
214                            mi->shared_clean++;
215                    } else {
216                        if (flags & KPF_DIRTY)
217                            mi->private_dirty++;
218                        else
219                            mi->private_clean++;
220                    }
221                }
222            }
223        }
224
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;
229
230        /* add to array */
231        mis[i] = mi;
232    }
233
234    /* sort the array, if requested (compfn == NULL for original order) */
235    if (compfn)
236        qsort(mis, num_maps, sizeof(mis[0]), compfn);
237
238    for (i = 0; i < num_maps; i++) {
239        mi = mis[i];
240
241        if ((!mi) || (hide_zeros && !mi->usage.rss))
242            continue;
243
244        if (ws == WS_ONLY) {
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,
253                pm_map_name(mi->map)
254            );
255        } else {
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,
265                pm_map_name(mi->map)
266            );
267        }
268    }
269
270    /* print totals */
271    if (ws == WS_ONLY) {
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,
282            "TOTAL"
283        );
284    } else {
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,
296            "TOTAL"
297        );
298    }
299
300    free(mis);
301    return 0;
302}
303
304static 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
314int 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