]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
parser: Ignore unused pages
[scan-pagemap] / parse.c
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
7
8 #include "parse.h"
9 #include "pagemap.h"
10
11 static struct maps_list *alloc_maplist(void)
12 {
13         struct maps_list *map;
14
15         map = malloc(sizeof *map);
16         if (map == NULL)
17                 goto err;
18
19         memset(map, 0, sizeof(*map));
20         INIT_LIST_HEAD(&map->list);
21 err:
22         return map;
23 }
24
25 static struct maps *alloc_map(void)
26 {
27         struct maps *map;
28
29         map = malloc(sizeof *map);
30         if (map == NULL)
31                 goto err;
32
33         memset(map, 0, sizeof(*map));
34         INIT_LIST_HEAD(&map->list);
35 err:
36         return map;
37 }
38
39 static struct maps *parse_maps(FILE *file, int pid)
40 {
41         struct maps *the_map = NULL;
42         char line[1024];
43         int ret;
44
45         while(fgets(line, sizeof(line), file)) {
46                 struct maps *map = alloc_map();
47                 unsigned long start, end;
48                 char name[1024];
49
50                 if (map == NULL)
51                         return 0;
52
53                 if (the_map == NULL)
54                         the_map = map;
55
56                 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
57                              &start, &end, name);
58
59                 if (ret < 2) {
60                         printf("Error reading input: %s\n", line);
61                         break;
62                 }
63
64                 map->start = start;
65                 map->end = end;
66                 map->size = end - start;
67                 map->pid = pid;
68
69                 if (ret >= 3)
70                         strncpy(map->name, name, sizeof(map->name));
71
72                 list_add_tail(&map->list, &the_map->list);
73         }
74
75         return the_map;
76 }
77
78 static void clear_pageframe(struct pageframe *pf)
79 {
80         memset(pf, 0, sizeof(*pf));
81 }
82
83 static struct pageframe *alloc_pageframe(void)
84 {
85         struct pageframe *pageframe;
86
87         pageframe = malloc(sizeof *pageframe);
88         if (pageframe == NULL)
89                 goto err;
90
91         clear_pageframe(pageframe);
92 err:
93         return pageframe;
94 }
95
96 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
97
98 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
99 {
100         pf->page_present = !!(BITRANGE(63, 63) & p);
101         pf->page_swapped = !!(BITRANGE(62, 62) & p);
102         pf->page_shift   =   (BITRANGE(55, 60) & p) >> 55;
103         pf->pfn          =   (BITRANGE(0, 54) & p);
104         pf->swap_type    =   (BITRANGE(0, 4) & p);
105         pf->swap_offset  =   (BITRANGE(5, 54) & p) >> 5;
106 #if 0
107         printf("pfn: %lx shift: %d present: %d swapped %d\n",
108                 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
109 #endif
110 }
111
112 static int compare_pageframe(struct bintree *at, struct bintree *bt)
113 {
114         struct pageframe *a, *b;
115         a = tree_to_pageframe(at);
116         b = tree_to_pageframe(bt);
117
118         return a->pfn - b->pfn;
119 }
120
121 struct bintree_ops pageframe_ops = {
122         .compare = compare_pageframe,
123 };
124
125 /* Read data from the /proc/pid/pagemap file */
126 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
127                         struct maps *maps, int add_to_tree)
128 {
129         struct maps *map;
130         struct maps_list *tmp;
131         struct pageframe *match, *pageframe = NULL;
132         long start, len, i;
133         unsigned long long pf[10240];
134         int ret, error;
135
136         if (maps == NULL)
137                 return 0;
138
139         /* Go through the list of allocated memory areas */
140         list_for_each_entry(map, &maps->list, list) {
141                 start = map->start >> (PAGE_SHIFT - 3);
142                 len = map->size >> (PAGE_SHIFT - 3);
143
144                 ret = fseek(file, start, SEEK_SET);
145                 if (ret) {
146                         error = errno;
147                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
148                                 strerror(error));
149                         continue;
150                 }
151
152                 for (i = 0; i < len; i++) {
153                         if (!ret)
154                                 ret = fread(&pf, 1,
155                                         MIN(sizeof(pf), len - i), file);
156                         if (ret < 0) {
157                                 error = errno;
158                                 continue;
159                         }
160                         if (!pageframe)
161                                 pageframe = alloc_pageframe();
162                         ret -= sizeof(pf[0]);
163
164                         /* ignore unused pages */
165                         if (!pf[ret / sizeof(pf[0])])
166                                 continue;
167
168                         pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
169
170                         if (add_to_tree) {
171                                 match = tree_to_pageframe(
172                                         bintree_add(&pf_tree->tree,
173                                                 &pageframe->tree,
174                                                 &pageframe_ops));
175                         } else {
176                                 match = tree_to_pageframe(
177                                         bintree_find(&pf_tree->tree,
178                                                 &pageframe->tree,
179                                                 &pageframe_ops));
180                         }
181
182                         if (match == NULL)
183                                 continue;
184
185                         if (match == pageframe)
186                                 pageframe = NULL;
187
188                         match->refcount++;
189                         /*
190                          * Add a link from the physical page to this
191                          * process's page map
192                          */
193                         if (!match->ml) {
194                                 match->ml = alloc_maplist();
195                                 match->ml->map = map;
196                         } else {
197                                 tmp = alloc_maplist();
198                                 tmp->map = map;
199                                 list_add(&match->ml->list, &tmp->list);
200                         }
201
202                         if (match->page_present) {
203                                 map->pages_present++;
204                         } else if (match->page_swapped) {
205                                 map->pages_swapped++;
206                         }
207                 }
208         }
209
210         return 0;
211 }
212
213 void read_pageframe(int pid, struct pageframe *pageframe,
214                 struct process **process_list, int add_to_tree)
215 {
216         struct maps *maps;
217         struct process *process;
218         FILE *file;
219         char path[512];
220         int ret;
221
222         process = malloc(sizeof(*process));
223         memset(process, 0, sizeof(*process));
224         INIT_LIST_HEAD(&process->list);
225
226         if (*process_list == NULL)
227                 *process_list = process;
228
229         process->pid = pid;
230
231         list_add_tail(&process->list, &(*process_list)->list);
232
233         snprintf(path, sizeof(path), "/proc/%d/maps", pid);
234         file = fopen(path, "rb");
235
236         if (!file)
237                 return;
238
239         maps = parse_maps(file, pid);
240         fclose(file);
241         process->maps = maps;
242
243         snprintf(path, sizeof(path), "/proc/%d/pagemap", pid);
244         file = fopen(path, "rb");
245
246         if (!file)
247                 return;
248
249         parse_pageframe(file, pageframe, maps, add_to_tree);
250         fclose(file);
251
252         snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
253         file = fopen(path, "rb");
254
255         if (!file)
256                 return;
257
258         ret = fread(process->name, 1, sizeof(process->name), file);
259         if (ret > 0)
260                 process->name[ret - 1] = 0;
261         fclose(file);
262
263         if (maps == NULL)
264                 return;
265
266         list_for_each_entry(maps, &process->maps->list, list) {
267                 process->pages_present += maps->pages_present;
268                 process->pages_swapped += maps->pages_swapped;
269         }
270
271         return;
272 }
273
274 static int get_next_pid(void)
275 {
276         static DIR *dir = NULL;
277         struct dirent *dirent;
278         int error;
279
280         if (!dir) {
281                 dir = opendir("/proc");
282                 if (!dir) {
283                         error = errno;
284                         printf("Failed to open /proc directory: %s\n",
285                                 strerror(error));
286                         return -1;
287                 }
288         }
289
290 restart:
291         dirent = readdir(dir);
292         if (!dirent) {
293                 if (errno == 0) {
294                         closedir(dir);
295                         dir = NULL;
296                         return 0;
297                 }
298                 error = errno;
299                 printf("Failed to read /proc directory: %s\n", strerror(error));
300                 return -1;
301         }
302
303         if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
304                 goto restart;
305
306         return atoi(dirent->d_name);
307 }
308
309 void scan_all_pids(struct pageframe *pf, struct process **process_list,
310                 int interesting_pid)
311 {
312         int pid;
313
314         read_pageframe(interesting_pid, pf, process_list, 1);
315
316         while(1) {
317                 pid = get_next_pid();
318                 if (pid <= 0)
319                         break;
320                 read_pageframe(pid, pf, process_list, 0);
321         }
322 }