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 100
55 ((a) < SI_k * 4 ? (a) : \
56 (a < SI_k * PRETTY_THRESH * 1000 ? ((a) / SI_k) : \
57 (a < SI_M * PRETTY_THRESH * 1000 ? ((a) / SI_M) : \
60 #define NICE_UNIT(a) \
61 ((a) < (SI_k * 4) ? " " : \
62 ((a) < (SI_k * PRETTY_THRESH * 1000) ? "k" : \
63 ((a) < (SI_M * PRETTY_THRESH * 1000) ? "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 void count_page(struct pageframe *pf, struct analyze_frames *af)
93 /* Find pages which reference at least once a pid */
94 list_for_each_entry(ml, &pf->ml, list) {
95 if (ml->map->pid == af->pid)
99 } else if (af->pidlist && af->map) {
101 * Find pages that reference at least once all of the
102 * given pids and a given mapping
108 * Check that we reference the given mapping at least
111 list_for_each_entry(ml, &pf->ml, list) {
112 if (ml->map == af->map) {
123 * Check that we reference all of the given pids
124 * too. The order of the loops is important here. We
125 * must scan through all the references and test for a
126 * given pid. If we would iterate through the
127 * references in the outer loop, we might get
128 * duplicate matches for a pid since it is possible
129 * that a page is mapped multiple times in a process's
132 list_for_each_entry(pid, af->pidlist, list) {
133 list_for_each_entry(ml, &pf->ml, list) {
134 if (ml->map->pid == pid->pid) {
141 * If we have found as many matches as ther
142 * are pids, we will count the stats
144 if (matches == af->pids)
151 if (page_present(pf))
153 else if (page_swapped(pf))
155 if (pf->kpagecount == 1)
158 for (i = 0; i < KPAGEFLAGS_NUM; i++)
159 if (kpageflag_is_set(pf, i))
163 static int count_pages(struct rb_root *root, struct analyze_frames *af)
165 struct pageframe *pf;
168 pf = rb_to_pageframe(rb_first(root));
174 pf = rb_to_pageframe(rb_next(&pf->tree));
181 * print_page_stats - Prints system wide page stats
183 void print_page_stats(struct rb_root *root)
185 struct analyze_frames af;
188 memset(&af, 0, sizeof(af));
190 count = count_pages(root, &af);
194 for (i = 0; i < KPAGEFLAGS_NUM; i++) {
195 if (!af.kpageflag[i])
198 printf("%13s pages: %6ld, %5lld %sB\n",
201 PAGE_TO_NICE(af.kpageflag[i]),
202 PAGE_TO_NICE_UNIT(af.kpageflag[i]));
205 printf(" present pages: %6ld, %5lld %sB\n"
206 " swapped pages: %6ld, %5lld %sB\n"
207 " unique pages: %6ld, %5lld %sB\n"
208 " total pages: %6ld, %5lld %sB\n",
210 PAGE_TO_NICE(af.pages_present),
211 PAGE_TO_NICE_UNIT(af.pages_present),
213 PAGE_TO_NICE(af.pages_swapped),
214 PAGE_TO_NICE_UNIT(af.pages_swapped),
216 PAGE_TO_NICE(af.pages_unique),
217 PAGE_TO_NICE_UNIT(af.pages_unique),
220 PAGE_TO_NICE_UNIT(count));
223 void print_pid_stats(struct rb_root *root, struct process *process_list,
224 struct parse_opts *opts)
226 struct analyze_frames af;
228 struct pageframe *pf;
229 long int swapped, present, unique, total;
230 long int biggest = 0, second_biggest;
231 int count, processes = 0;
234 * walk through all processes, find the one with most present
237 pf = rb_to_pageframe(rb_first(root));
239 list_for_each_entry(ps, &process_list->list, list) {
240 memset(&af, 0, sizeof(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 pf = rb_to_pageframe(rb_next(&pf->tree));
252 printf(" RSS swapped USS total pid");
253 if (opts->with_threads)
260 list_for_each_entry(ps, &process_list->list, list) {
262 present = ps->pages_present;
263 swapped = ps->pages_swapped;
264 unique = ps->pages_unique;
265 total = present + swapped;
267 second_biggest = (total < biggest) &&
268 (second_biggest < total) ?
269 total : second_biggest;
271 if (total != biggest)
277 printf("%5lld %sB %5lld %sB %5lld %sB %5lld %sB %5d ",
278 PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
279 PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
280 PAGE_TO_NICE(unique), PAGE_TO_NICE_UNIT(unique),
281 PAGE_TO_NICE(total), PAGE_TO_NICE_UNIT(total),
284 if (opts->with_threads)
285 printf("%5d ", ps->tid);
288 ps->is_initial_pid ? '*' : ' ',
296 biggest = second_biggest;
300 printf("Total %d processes\n", processes);
303 static void _dump_process_maps(struct rb_root *root, struct process *ps,
304 struct parse_opts *opts)
307 long int swapped, present, total;
308 long int biggest = 0, second_biggest;
309 int count, processes = 0, pids = 0;
311 if (is_parse_option(opts, PARSE_SHARED_MAPPING)) {
313 list_for_each_entry(pid, &opts->pidlist, list)
317 list_for_each_entry(map, &ps->maps->list, list) {
318 struct analyze_frames af;
320 if (is_parse_option(opts, PARSE_SHARED_MAPPING)) {
321 memset(&af, 0, sizeof(af));
322 af.pidlist = &opts->pidlist;
326 count_pages(root, &af);
327 map->pages_present += af.pages_present;
328 map->pages_swapped += af.pages_swapped;
331 biggest = MAX(biggest, map->pages_present + map->pages_swapped);
334 printf("process: [%d] %s\n", ps->pid, ps->name);
335 printf(" size RSS swapped total name\n");
339 list_for_each_entry(map, &ps->maps->list, list) {
341 present = map->pages_present;
342 swapped = map->pages_swapped;
343 total = present + swapped;
345 second_biggest = (total < biggest) &&
346 (second_biggest < total) ?
347 total : second_biggest;
349 if (total != biggest)
353 * Do not print zero sized mappings if
354 * --shared-mappings is enabled
356 if (is_parse_option(opts, PARSE_SHARED_MAPPING) && total == 0)
359 printf("%5lld %sB %5lld %sB %5lld %sB %5lld %sB %s\n",
360 NICE_DIV(map->size), NICE_UNIT(map->size),
361 PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
362 PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
363 PAGE_TO_NICE(total), PAGE_TO_NICE_UNIT(total),
370 if (count > 0 && biggest > 0) {
371 biggest = second_biggest;
377 void dump_process_maps(struct rb_root *root, struct process *process_list,
378 struct parse_opts *opts)
382 list_for_each_entry(ps, &process_list->list, list) {
383 _dump_process_maps(root, ps, opts);