]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
Scan threads only when requested
[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, int tid)
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                 map->tid = tid;
69
70                 if (ret >= 3)
71                         strncpy(map->name, name, sizeof(map->name));
72
73                 list_add_tail(&map->list, &the_map->list);
74         }
75
76         return the_map;
77 }
78
79 static void clear_pageframe(struct pageframe *pf)
80 {
81         memset(pf, 0, sizeof(*pf));
82 }
83
84 static struct pageframe *alloc_pageframe(void)
85 {
86         struct pageframe *pageframe;
87
88         pageframe = malloc(sizeof *pageframe);
89         if (pageframe == NULL)
90                 goto err;
91
92         clear_pageframe(pageframe);
93 err:
94         return pageframe;
95 }
96
97 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
98
99 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
100 {
101         /* Refer Documentation/vm/pagemap.txt for the format */
102         pf->page_present = !!(BITRANGE(63, 63) & p);
103         pf->page_swapped = !!(BITRANGE(62, 62) & p);
104         pf->page_shift   =   (BITRANGE(55, 60) & p) >> 55;
105         pf->pfn          =   (BITRANGE(0, 54) & p);
106         pf->swap_type    =   (BITRANGE(0, 4) & p);
107         pf->swap_offset  =   (BITRANGE(5, 54) & p) >> 5;
108 #if 0
109         printf("pfn: %lx shift: %d present: %d swapped %d\n",
110                 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
111 #endif
112 }
113
114 static int compare_pageframe(struct bintree *at, struct bintree *bt)
115 {
116         struct pageframe *a, *b;
117         a = tree_to_pageframe(at);
118         b = tree_to_pageframe(bt);
119
120         return a->pfn - b->pfn;
121 }
122
123 struct bintree_ops pageframe_ops = {
124         .compare = compare_pageframe,
125 };
126
127 static int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
128                 struct maps *map)
129 {
130         if (opts->parse_mask & PARSE_PID) {
131                 if (opts->pid == map->pid)
132                         return 1;
133         }
134
135         if (opts->parse_mask & PARSE_MAP_NAME) {
136                 if (!strcmp(opts->map_name, map->name))
137                         return 1;
138         }
139
140         return 0;
141 }
142
143 /* Read data from the /proc/pid/pagemap file */
144 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
145                         struct maps *maps, struct parse_opts *opts)
146 {
147         struct maps *map;
148         struct maps_list *tmp;
149         struct pageframe *match, *pageframe = NULL;
150         long start, len, i;
151         unsigned long long pf[10240];
152         int ret, error;
153
154         if (maps == NULL)
155                 return 0;
156
157         /* Go through the list of allocated memory areas */
158         list_for_each_entry(map, &maps->list, list) {
159                 start = map->start >> (PAGE_SHIFT - 3);
160                 len = map->size >> (PAGE_SHIFT - 3);
161
162                 ret = fseek(file, start, SEEK_SET);
163                 if (ret) {
164                         error = errno;
165                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
166                                 strerror(error));
167                         continue;
168                 }
169
170                 for (i = 0; i < len; i++) {
171                         if (!ret) {
172                                 ret = fread(&pf, 1,
173                                         MIN(sizeof(pf), (len - i) * 8), file);
174                         }
175                         if (ret < 0) {
176                                 error = errno;
177                                 continue;
178                         }
179                         if (!pageframe)
180                                 pageframe = alloc_pageframe();
181                         ret -= sizeof(pf[0]);
182
183                         /* ignore unused pages */
184                         if (!pf[ret / sizeof(pf[0])])
185                                 continue;
186
187                         pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
188
189                         /* ignore unused pages */
190                         if (!(pageframe->page_swapped ||
191                                         pageframe->page_present))
192                                 continue;
193
194                         if (check_parse_opts(opts, pageframe, map)) {
195                                 match = tree_to_pageframe(
196                                         bintree_add(&pf_tree->tree,
197                                                 &pageframe->tree,
198                                                 &pageframe_ops));
199                         } else {
200                                 match = tree_to_pageframe(
201                                         bintree_find(&pf_tree->tree,
202                                                 &pageframe->tree,
203                                                 &pageframe_ops));
204                         }
205
206                         if (match == NULL)
207                                 continue;
208
209                         if (match == pageframe)
210                                 pageframe = NULL;
211
212                         match->refcount++;
213                         /*
214                          * Add a link from the physical page to this
215                          * process's page map
216                          */
217                         if (!match->ml) {
218                                 match->ml = alloc_maplist();
219                                 match->ml->map = map;
220                         } else {
221                                 tmp = alloc_maplist();
222                                 tmp->map = map;
223                                 list_add(&match->ml->list, &tmp->list);
224                         }
225
226                         if (match->page_present) {
227                                 map->pages_present++;
228                         } else if (match->page_swapped) {
229                                 map->pages_swapped++;
230                         }
231                 }
232         }
233
234         return 0;
235 }
236
237 void read_pageframe(int pid, int tid, struct pageframe *pageframe,
238                 struct process **process_list, struct parse_opts *opts)
239 {
240         struct maps *maps;
241         struct process *process;
242         FILE *file;
243         char path[512];
244         int ret;
245
246         process = malloc(sizeof(*process));
247         memset(process, 0, sizeof(*process));
248         INIT_LIST_HEAD(&process->list);
249
250         if (*process_list == NULL)
251                 *process_list = process;
252
253         process->pid = pid;
254         process->tid = tid;
255
256         list_add_tail(&process->list, &(*process_list)->list);
257
258         snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
259         file = fopen(path, "rb");
260
261         if (!file)
262                 return;
263
264         maps = parse_maps(file, pid, tid);
265         fclose(file);
266         process->maps = maps;
267
268         snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
269         file = fopen(path, "rb");
270
271         if (!file)
272                 return;
273
274         parse_pageframe(file, pageframe, maps, opts);
275         fclose(file);
276
277         snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
278         file = fopen(path, "rb");
279
280         if (!file)
281                 return;
282
283         ret = fread(process->name, 1, sizeof(process->name), file);
284         if (ret > 0)
285                 process->name[ret - 1] = 0;
286         fclose(file);
287
288         if (maps == NULL)
289                 return;
290
291         list_for_each_entry(maps, &process->maps->list, list) {
292                 process->pages_present += maps->pages_present;
293                 process->pages_swapped += maps->pages_swapped;
294         }
295
296         return;
297 }
298
299 static int parse_pid(DIR **dir)
300 {
301         struct dirent *dirent;
302         int error;
303
304 restart:
305         dirent = readdir(*dir);
306         if (!dirent) {
307                 if (errno == 0) {
308                         closedir(*dir);
309                         *dir = NULL;
310                         return 0;
311                 }
312                 error = errno;
313                 printf("Failed to read /proc directory: %s\n", strerror(error));
314                 return -1;
315         }
316
317         if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
318                 goto restart;
319
320         return atoi(dirent->d_name);
321 }
322
323 static int get_next_tid(int pid, DIR **dir)
324 {
325         int error;
326
327         if (*dir == NULL) {
328                 char path[64];
329
330                 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
331                 *dir = opendir(path);
332                 if (!*dir) {
333                         error = errno;
334                         printf("Failed to open %s directory: %s\n",
335                                 path,
336                                 strerror(error));
337                         return -1;
338                 }
339         }
340
341         return parse_pid(dir);
342 }
343
344 static int get_next_pid(void)
345 {
346         static DIR *dir = NULL;
347         int error;
348
349         if (!dir) {
350                 dir = opendir("/proc");
351                 if (!dir) {
352                         error = errno;
353                         printf("Failed to open /proc directory: %s\n",
354                                 strerror(error));
355                         return -1;
356                 }
357         }
358
359         return parse_pid(&dir);
360 }
361
362 static void read_pageframe_with_threads(int pid,
363                                         struct pageframe *pageframe,
364                                         struct process **process_list,
365                                         struct parse_opts *opts)
366 {
367         DIR *dir = NULL;
368         int tid;
369
370         while (1) {
371                 if (opts->with_threads)
372                         tid = get_next_tid(pid, &dir);
373                 else
374                         tid = pid;
375
376                 if (tid <= 0)
377                         return;
378
379                 read_pageframe(pid, tid, pageframe, process_list, opts);
380
381                 if (!opts->with_threads)
382                         break;
383         }
384 }
385
386 void scan_all_pids(struct pageframe *pf, struct process **process_list,
387                 struct parse_opts *opts)
388 {
389         int pid;
390
391         if (opts->parse_mask & PARSE_PID)
392                 read_pageframe_with_threads(opts->pid, pf, process_list, opts);
393
394         while(1) {
395                 pid = get_next_pid();
396                 if (pid <= 0)
397                         break;
398                 read_pageframe_with_threads(pid, pf, process_list, opts);
399         }
400 }