]> git.itanic.dy.fi Git - scan-pagemap/blob - analyze.c
309043179c97c4fb321b0e1f63c874aed7278388
[scan-pagemap] / analyze.c
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "analyze.h"
5 #include "utils.h"
6 #include "bintree.h"
7
8 #define SI_k    1024ll
9 #define SI_M    (SI_k * SI_k)
10 #define SI_G    (SI_M * SI_k)
11
12 #define PRETTY_THRESH   98
13 #define NICE_DIV(a)                                                     \
14         ((a) < SI_k * 4 ? (a) :                         \
15                 (a < SI_M * PRETTY_THRESH ? ((a) / SI_k) :              \
16                         (a < SI_G * PRETTY_THRESH ? ((a) / SI_M) :      \
17                                 ((a) / SI_G))))
18
19 #define NICE_UNIT(a)                                                    \
20         ((a) < (SI_k * 4) ? " " :                               \
21                 ((a) < (SI_M * PRETTY_THRESH) ? "k" :                   \
22                         ((a) < (SI_G * PRETTY_THRESH) ? "M" : "G")))
23
24 #define PAGE_TO_NICE(a)         NICE_DIV((long long)a * PAGE_SIZE)
25 #define PAGE_TO_NICE_UNIT(a)    NICE_UNIT((long long)a * PAGE_SIZE)
26
27 struct analyze_frames {
28         struct bintree_ops ops;
29         long int pages_present;
30         long int pages_swapped;
31         long int pages_unique;
32         int pid;
33 };
34
35 #define bintree_ops_to_af(bintree_ops)                          \
36         container_of((bintree_ops), struct analyze_frames, ops)
37
38 static void count_pages(struct bintree *b, struct bintree_ops *ops)
39 {
40         struct pageframe *pf = tree_to_pageframe(b);
41         struct analyze_frames *af = bintree_ops_to_af(ops);
42         struct maps_list *ml;
43
44         if (af->pid) {
45                 list_for_each_entry(ml, &pf->ml, list) {
46                         if (ml->map->pid == af->pid)
47                                 goto get_stats;
48                 }
49                 return;
50         }
51
52 get_stats:
53         if (page_present(pf))
54                 af->pages_present++;
55         else if (page_swapped(pf))
56                 af->pages_swapped++;
57         if (pf->refcount == 1)
58                 af->pages_unique++;
59 }
60
61 /*
62  * print_page_stats - Prints system wide page stats
63  */
64 void print_page_stats(struct pageframe *pf)
65 {
66         struct analyze_frames af;
67         long count;
68         memset(&af, 0, sizeof(af));
69
70         af.ops.callback = count_pages;
71
72         count = bintree_walk(&pf->tree, &af.ops);
73         printf("present pages: %ld, %lld %sB\n"
74                 "Swapped pages: %ld, %lld %sB\n"
75                 "Unique pages: %ld, %lld %sB\n"
76                 "Total %ld physical pages, %lld %sB\n",
77                 af.pages_present,
78                 PAGE_TO_NICE(af.pages_present),
79                 PAGE_TO_NICE_UNIT(af.pages_present),
80                 af.pages_swapped,
81                 PAGE_TO_NICE(af.pages_swapped),
82                 PAGE_TO_NICE_UNIT(af.pages_swapped),
83                 af.pages_unique,
84                 PAGE_TO_NICE(af.pages_unique),
85                 PAGE_TO_NICE_UNIT(af.pages_unique),
86                 count,
87                 PAGE_TO_NICE(count),
88                 PAGE_TO_NICE_UNIT(count));
89 }
90
91 void print_pid_stats(struct pageframe *pf, struct process *process_list,
92                 struct parse_opts *opts)
93 {
94         struct analyze_frames af;
95         struct process *ps;
96         long int swapped, present, total;
97         long int biggest = 0, second_biggest;
98         int count, processes = 0;
99
100         /*
101          * walk through all processes, find the one with most present
102          * pages
103          */
104         list_for_each_entry(ps, &process_list->list, list) {
105                 memset(&af, 0, sizeof(af));
106                 af.ops.callback = count_pages;
107                 af.pid = ps->pid;
108
109                 bintree_walk(&pf->tree, &af.ops);
110                 ps->pages_present = af.pages_present;
111                 ps->pages_swapped = af.pages_swapped;
112                 biggest = MAX(biggest, ps->pages_present + ps->pages_swapped);
113         }
114
115         printf("  in ram  swapped    total   pid");
116         if (opts->with_threads)
117                 printf("   tid");
118         printf("   name\n");
119
120 restart:
121         second_biggest = 0;
122         count = 0;
123         list_for_each_entry(ps, &process_list->list, list) {
124
125                 present = ps->pages_present;
126                 swapped = ps->pages_swapped;
127                 total = present + swapped;
128
129                 second_biggest = (total < biggest) &&
130                         (second_biggest < total) ?
131                         total : second_biggest;
132
133                 if (total != biggest)
134                         continue;
135
136                 if (total == 0)
137                         continue;
138
139                 printf("%5lld %sB %5lld %sB %5lld %sB %5d ",
140                         PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
141                         PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
142                         PAGE_TO_NICE(total), PAGE_TO_NICE_UNIT(total),
143                         ps->pid);
144
145                 if (opts->with_threads)
146                         printf("%5d ", ps->tid);
147
148                 printf("%c %s\n",
149                         ps->is_initial_pid ? '*' : ' ',
150                         ps->name);
151
152                 count++;
153                 processes++;
154         }
155
156         if (count > 0) {
157                 biggest = second_biggest;
158                 goto restart;
159         }
160
161         printf("Total %d processes\n", processes);
162 }
163
164 static void _dump_process_maps(struct process *ps)
165 {
166         struct maps *map;
167         long int swapped, present, total;
168         long int biggest = 0, second_biggest;
169         int count, processes = 0;
170
171         list_for_each_entry(map, &ps->maps->list, list) {
172                 biggest = MAX(biggest, map->pages_present + map->pages_swapped);
173         }
174
175         printf("process: [%d] %s\n", ps->pid, ps->name);
176         printf("    size   in ram  swapped    total name\n");
177 restart:
178         second_biggest = 0;
179         count = 0;
180         list_for_each_entry(map, &ps->maps->list, list) {
181
182                 present = map->pages_present;
183                 swapped = map->pages_swapped;
184                 total = present + swapped;
185
186                 second_biggest = (total < biggest) &&
187                         (second_biggest < total) ?
188                         total : second_biggest;
189
190                 if (total != biggest)
191                         continue;
192
193                 printf("%5lld %sB %5lld %sB %5lld %sB %5lld %sB %s\n",
194                         NICE_DIV(map->size), NICE_UNIT(map->size),
195                         PAGE_TO_NICE(present), PAGE_TO_NICE_UNIT(present),
196                         PAGE_TO_NICE(swapped), PAGE_TO_NICE_UNIT(swapped),
197                         PAGE_TO_NICE(total), PAGE_TO_NICE_UNIT(total),
198                         map->name);
199
200                 count++;
201                 processes++;
202         }
203
204         if (count > 0 && biggest > 0) {
205                 biggest = second_biggest;
206                 goto restart;
207         }
208         printf("\n");
209 }
210
211 void dump_process_maps(struct process *process_list)
212 {
213         struct process *ps;
214
215         list_for_each_entry(ps, &process_list->list, list) {
216                 _dump_process_maps(ps);
217         }
218 }