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