2 * Copyright (C) 2010 Timo Kokkonen <kaapeli@itanic.dy.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 struct kpageflag_str kpageflag_str[] = {
25 { .flag = LOCKED, .str = "locked", },
26 { .flag = ERROR, .str = "error", },
27 { .flag = REFERENCED, .str = "referenced", },
28 { .flag = UPTODATE, .str = "uptodate", },
29 { .flag = DIRTY, .str = "dirty", },
30 { .flag = LRU, .str = "lru", },
31 { .flag = ACTIVE, .str = "active", },
32 { .flag = SLAB, .str = "slab", },
33 { .flag = WRITEBACK, .str = "writeback", },
34 { .flag = RECLAIM, .str = "reclaim", },
35 { .flag = BUDDY, .str = "buddy", },
36 { .flag = MMAP, .str = "mmap", },
37 { .flag = ANON, .str = "anon", },
38 { .flag = SWAPCACHE, .str = "swapcache", },
39 { .flag = SWAPBACKED, .str = "swapbacked", },
40 { .flag = COMPOUND_HEAD, .str = "compound_head", },
41 { .flag = COMPOUND_TAIL, .str = "compound_tail", },
42 { .flag = HUGE, .str = "huge", },
43 { .flag = UNEVICTABLE, .str = "unevictable", },
44 { .flag = HWPOISON, .str = "hwpoison", },
45 { .flag = NOPAGE, .str = "nopage", },
46 { .flag = KSM, .str = "ksm", },
50 #define SI_M (SI_k * SI_k)
51 #define SI_G (SI_M * SI_k)
53 #define PRETTY_THRESH 97
55 ((a) < SI_k * 4 ? (a) : \
56 (a < SI_M * PRETTY_THRESH ? ((a) / SI_k) : \
57 (a < SI_G * PRETTY_THRESH ? ((a) / SI_M) : \
60 #define NICE_UNIT(a) \
61 ((a) < (SI_k * 4) ? " " : \
62 ((a) < (SI_M * PRETTY_THRESH) ? "k" : \
63 ((a) < (SI_G * PRETTY_THRESH) ? "M" : "G")))
65 #define PAGE_TO_NICE(a) NICE_DIV((long long)a * PAGE_SIZE)
66 #define PAGE_TO_NICE_UNIT(a) NICE_UNIT((long long)a * PAGE_SIZE)
68 struct analyze_frames {
71 struct list_head *pidlist;
75 /* General counters */
76 long int pages_present;
77 long int pages_swapped;
78 long int pages_unique;
80 /* kpageflag stats counters */
81 long int kpageflag[KPAGEFLAGS_NUM];
84 #define bintree_ops_to_af(bintree_ops) \
85 container_of((bintree_ops), struct analyze_frames, ops)
87 static int count_pages(struct rb_root *root, struct analyze_frames *af)
93 pf = rb_to_pageframe(rb_first(root));
99 /* Find pages which reference at least once a pid */
100 list_for_each_entry(ml, &pf->ml, list) {
101 if (ml->map->pid == af->pid)
105 } else if (af->pidlist && af->map) {
107 * Find pages that reference at least once all
108 * of the given pids and a given mapping
114 * Check that we reference the given mapping
117 list_for_each_entry(ml, &pf->ml, list) {
118 if (ml->map == af->map) {
129 * Check that we reference all of the given
130 * pids too. The order of the loops is
131 * important here. We must scan through all
132 * the references and test for a given pid. If
133 * we would iterate through the references in
134 * the outer loop, we might get duplicate
135 * matches for a pid since it is possible that
136 * a page is mapped multiple times in a
137 * process's addrses space.
139 list_for_each_entry(pid, af->pidlist, list) {
140 list_for_each_entry(ml, &pf->ml, list) {
141 if (ml->map->pid == pid->pid) {
148 * If we have found as many matches as ther
149 * are pids, we will count the stats
151 if (matches == af->pids)
158 if (page_present(pf))
160 else if (page_swapped(pf))
162 if (pf->kpagecount == 1)
165 for (i = 0; i < KPAGEFLAGS_NUM; i++)
166 if (kpageflag_is_set(pf, i))
170 pf = rb_to_pageframe(rb_next(&pf->tree));
177 * print_page_stats - Prints system wide page stats
179 void print_page_stats(struct rb_root *root)
181 struct analyze_frames af;
184 memset(&af, 0, sizeof(af));
186 count = count_pages(root, &af);
190 for (i = 0; i < KPAGEFLAGS_NUM; i++) {
191 if (!af.kpageflag[i])
194 printf("%13s pages: %6ld, %5lld %sB\n",
197 PAGE_TO_NICE(af.kpageflag[i]),
198 PAGE_TO_NICE_UNIT(af.kpageflag[i]));
201 printf(" present pages: %6ld, %5lld %sB\n"
202 " swapped pages: %6ld, %5lld %sB\n"
203 " unique pages: %6ld, %5lld %sB\n"
204 " total pages: %6ld, %5lld %sB\n",
206 PAGE_TO_NICE(af.pages_present),
207 PAGE_TO_NICE_UNIT(af.pages_present),
209 PAGE_TO_NICE(af.pages_swapped),
210 PAGE_TO_NICE_UNIT(af.pages_swapped),
212 PAGE_TO_NICE(af.pages_unique),
213 PAGE_TO_NICE_UNIT(af.pages_unique),
216 PAGE_TO_NICE_UNIT(count));
219 void print_pid_stats(struct rb_root *root, struct process *process_list,
220 struct parse_opts *opts)
222 struct analyze_frames af;
224 long int swapped, present, unique, total;
225 long int biggest = 0, second_biggest;
226 int count, processes = 0;
230 * walk through all processes, find the one with most present
233 printf("\rAnalyzing pages for process: ");
234 list_for_each_entry(ps, &process_list->list, list) {
235 for (i = 0; i < len; i++)
237 len = printf("% 5d", ps->pid);
240 memset(&af, 0, sizeof(af));
243 count_pages(root, &af);
244 ps->pages_present = af.pages_present;
245 ps->pages_swapped = af.pages_swapped;
246 ps->pages_unique = af.pages_unique;
247 biggest = MAX(biggest, ps->pages_present + ps->pages_swapped);
249 for (i = 0; i < len; i++)
253 printf(" RSS swapped USS total pid");
254 if (opts->with_threads)
261 list_for_each_entry(ps, &process_list->list, list) {
263 present = ps->pages_present;
264 swapped = ps->pages_swapped;
265 unique = ps->pages_unique;
266 total = present + swapped;
268 second_biggest = (total < biggest) &&
269 (second_biggest < total) ?
270 total : second_biggest;
272 if (total != biggest)
278 printf("%5lld %sB %5lld %sB %5lld %sB %5lld %sB %5d ",
279 PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
280 PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
281 PAGE_TO_NICE(unique), PAGE_TO_NICE_UNIT(unique),
282 PAGE_TO_NICE(total), PAGE_TO_NICE_UNIT(total),
285 if (opts->with_threads)
286 printf("%5d ", ps->tid);
289 ps->is_initial_pid ? '*' : ' ',
297 biggest = second_biggest;
301 printf("Total %d processes\n", processes);
304 static void _dump_process_maps(struct rb_root *root, struct process *ps,
305 struct parse_opts *opts)
308 long int swapped, present, total;
309 long int biggest = 0, second_biggest;
310 int count, processes = 0, pids = 0;
312 if (is_parse_option(opts, PARSE_SHARED_MAPPING)) {
314 list_for_each_entry(pid, &opts->pidlist, list)
318 list_for_each_entry(map, &ps->maps->list, list) {
319 struct analyze_frames af;
321 if (is_parse_option(opts, PARSE_SHARED_MAPPING)) {
322 memset(&af, 0, sizeof(af));
323 af.pidlist = &opts->pidlist;
327 count_pages(root, &af);
328 map->pages_present = af.pages_present;
329 map->pages_swapped = af.pages_swapped;
332 biggest = MAX(biggest, map->pages_present + map->pages_swapped);
335 printf("process: [%d] %s\n", ps->pid, ps->name);
336 printf(" size RSS swapped total name\n");
340 list_for_each_entry(map, &ps->maps->list, list) {
342 present = map->pages_present;
343 swapped = map->pages_swapped;
344 total = present + swapped;
346 second_biggest = (total < biggest) &&
347 (second_biggest < total) ?
348 total : second_biggest;
350 if (total != biggest)
354 * Do not print zero sized mappings if
355 * --shared-mappings is enabled
357 if (is_parse_option(opts, PARSE_SHARED_MAPPING) && total == 0)
360 printf("%5lld %sB %5lld %sB %5lld %sB %5lld %sB %s\n",
361 NICE_DIV(map->size), NICE_UNIT(map->size),
362 PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
363 PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
364 PAGE_TO_NICE(total), PAGE_TO_NICE_UNIT(total),
371 if (count > 0 && biggest > 0) {
372 biggest = second_biggest;
378 void dump_process_maps(struct rb_root *root, struct process *process_list,
379 struct parse_opts *opts)
383 list_for_each_entry(ps, &process_list->list, list) {
384 _dump_process_maps(root, ps, opts);