]> git.itanic.dy.fi Git - scan-pagemap/blob - parse.c
Add macro for testing parse option flags
[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 #include <libgen.h>
8
9 #include "parse.h"
10 #include "pagemap.h"
11
12 static struct maps_list *alloc_maplist(void)
13 {
14         struct maps_list *map;
15
16         map = malloc(sizeof *map);
17         if (map == NULL)
18                 goto err;
19
20         memset(map, 0, sizeof(*map));
21         INIT_LIST_HEAD(&map->list);
22 err:
23         return map;
24 }
25
26 static struct maps *alloc_map(void)
27 {
28         struct maps *map;
29
30         map = malloc(sizeof *map);
31         if (map == NULL)
32                 goto err;
33
34         memset(map, 0, sizeof(*map));
35         INIT_LIST_HEAD(&map->list);
36 err:
37         return map;
38 }
39
40 static struct maps *parse_maps(FILE *file, int pid, int tid)
41 {
42         struct maps *the_map = NULL;
43         char line[1024];
44         int ret;
45
46         while (fgets(line, sizeof(line), file)) {
47                 struct maps *map = alloc_map();
48                 unsigned long start, end;
49                 char name[1024];
50
51                 if (map == NULL)
52                         return 0;
53
54                 if (the_map == NULL)
55                         the_map = map;
56
57                 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
58                              &start, &end, name);
59
60                 if (ret < 2) {
61                         printf("Error reading input: %s\n", line);
62                         break;
63                 }
64
65                 map->start = start;
66                 map->end = end;
67                 map->size = end - start;
68                 map->pid = pid;
69                 map->tid = tid;
70
71                 if (ret >= 3)
72                         strncpy(map->name, name, sizeof(map->name));
73
74                 list_add_tail(&map->list, &the_map->list);
75         }
76
77         return the_map;
78 }
79
80 static void clear_pageframe(struct pageframe *pf)
81 {
82         memset(pf, 0, sizeof(*pf));
83 }
84
85 static struct pageframe *alloc_pageframe(void)
86 {
87         struct pageframe *pageframe;
88
89         pageframe = malloc(sizeof *pageframe);
90         if (pageframe == NULL)
91                 goto err;
92
93         clear_pageframe(pageframe);
94         INIT_LIST_HEAD(&pageframe->ml.list);
95 err:
96         return pageframe;
97 }
98
99 static int compare_pageframe(struct bintree *at, struct bintree *bt)
100 {
101         struct pageframe *a, *b;
102         a = tree_to_pageframe(at);
103         b = tree_to_pageframe(bt);
104
105         return a->pf - b->pf;
106 }
107
108 struct bintree_ops pageframe_ops = {
109         .compare = compare_pageframe,
110 };
111
112 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
113 {
114         FILE *file;
115         char path[512];
116         int ret;
117
118         snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
119         file = fopen(path, "rb");
120
121         if (!file)
122                 return -1;
123
124         ret = fread(cmdline, 1, len, file);
125         if (ret > 0)
126                 cmdline[ret - 1] = 0;
127         fclose(file);
128
129         return ret > 0 ? 0 : -1;
130 }
131
132 static char *get_name_by_pid(int pid)
133 {
134         static int last_pid;
135         static char cmdline[128];
136         static char *bname;
137
138         if (last_pid == pid)
139                 return bname;
140
141         if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
142                 bname = NULL;
143                 return NULL;
144         }
145
146         bname = basename(cmdline);
147
148         last_pid = pid;
149         return bname;
150 }
151
152 static int should_scan_process(struct parse_opts *opts, struct process *process)
153 {
154         int match = 0;
155         char *name;
156
157         if (is_parse_option(opts, PARSE_PROCESS_NAME)) {
158                 name = get_name_by_pid(process->pid);
159                 if (!strcmp(opts->name, name ? name : ""))
160                         match = 1;
161         }
162
163         if (is_parse_option(opts, PARSE_PID)) {
164                 if (opts->pid == process->pid)
165                         match = 1;
166         }
167
168         if (is_parse_option(opts, PARSE_MAP_NAME))
169                 match = 1;
170
171         if (is_parse_option(opts, PARSE_NOADD_TREE))
172                 match = !match;
173
174         return match;
175 }
176
177 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
178 {
179         int match = 0;
180
181         if (is_parse_option(opts, PARSE_MAP_NAME)) {
182                 if (!strcmp(opts->name, map->name))
183                         match = 1;
184
185                 if (is_parse_option(opts, PARSE_NOADD_TREE))
186                         match = !match;
187         } else
188                 match = 1;
189
190         return match;
191 }
192
193 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
194                 struct maps *map)
195 {
196         if (is_parse_option(opts, PARSE_NOADD_TREE))
197                 return 0;
198
199         return 1;
200 }
201
202 /* Read data from the /proc/pid/pagemap file */
203 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
204                         struct maps *maps, struct parse_opts *opts)
205 {
206         struct maps *map;
207         struct maps_list *tmp;
208         struct pageframe *match, *pageframe = NULL;
209         long start, len, i;
210         unsigned long long pf[10240];
211         int ret, error;
212
213         if (maps == NULL)
214                 return 0;
215
216         /* Go through the list of allocated memory areas */
217         list_for_each_entry(map, &maps->list, list) {
218                 start = map->start >> (PAGE_SHIFT - 3);
219                 len = map->size >> (PAGE_SHIFT);
220
221                 if (!should_scan_mapping(opts, map))
222                         continue;
223
224                 ret = fseek(file, start, SEEK_SET);
225                 if (ret) {
226                         error = errno;
227                         fprintf(stderr, "Error seeking to %lx: %s\n", start,
228                                 strerror(error));
229                         continue;
230                 }
231
232                 for (i = 0; i < len; i++) {
233                         if (!ret) {
234                                 ret = fread(&pf, 1,
235                                         MIN(sizeof(pf), (len - i) * 8), file);
236                         }
237                         if (ret < 0) {
238                                 error = errno;
239                                 continue;
240                         }
241                         if (!pageframe)
242                                 pageframe = alloc_pageframe();
243                         ret -= sizeof(pf[0]);
244
245                         /* ignore unused pages */
246                         if (!pf[ret / sizeof(pf[0])])
247                                 continue;
248
249                         pageframe->pf = (pf[ret / sizeof(pf[0])]);
250
251                         /* ignore unused pages */
252                         if (!(page_swapped(pageframe) ||
253                                         page_present(pageframe)))
254                                 continue;
255
256                         if (should_add_to_tree(opts, pageframe, map)) {
257                                 match = tree_to_pageframe(
258                                         bintree_add(&pf_tree->tree,
259                                                 &pageframe->tree,
260                                                 &pageframe_ops));
261                         } else {
262                                 match = tree_to_pageframe(
263                                         bintree_find(&pf_tree->tree,
264                                                 &pageframe->tree,
265                                                 &pageframe_ops));
266                         }
267
268                         if (match == NULL)
269                                 continue;
270
271                         if (match == pageframe)
272                                 pageframe = NULL;
273
274                         match->refcount++;
275                         /*
276                          * Add a link from the physical page to this
277                          * process's page map
278                          */
279                         tmp = alloc_maplist();
280                         tmp->map = map;
281                         list_add(&tmp->list, &match->ml.list);
282
283                         if (page_present(match))
284                                 map->pages_present++;
285                         else if (page_swapped(match))
286                                 map->pages_swapped++;
287                 }
288         }
289
290         return 0;
291 }
292
293 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
294                         struct process *process_list, struct parse_opts *opts)
295 {
296         struct maps *maps;
297         struct process *process;
298         FILE *file;
299         char path[512];
300
301         process = malloc(sizeof(*process));
302         memset(process, 0, sizeof(*process));
303         INIT_LIST_HEAD(&process->list);
304
305         process->pid = pid;
306         process->tid = tid;
307
308         if (!should_scan_process(opts, process))
309                 goto free;
310
311         snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
312         file = fopen(path, "rb");
313
314         if (!file)
315                 goto free;
316
317         maps = parse_maps(file, pid, tid);
318         fclose(file);
319         process->maps = maps;
320
321         snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
322         file = fopen(path, "rb");
323
324         if (!file)
325                 goto free;
326
327         parse_pageframe(file, pageframe, maps, opts);
328         fclose(file);
329
330         if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
331                 goto free;
332
333         if (maps != NULL) {
334                 list_for_each_entry(maps, &process->maps->list, list) {
335                         process->pages_present += maps->pages_present;
336                         process->pages_swapped += maps->pages_swapped;
337                 }
338         }
339
340         list_add_tail(&process->list, &process_list->list);
341
342         return 1;
343 free:
344         free(process);
345
346         return 0;
347 }
348
349 static int parse_pid(DIR **dir)
350 {
351         struct dirent *dirent;
352         int error;
353
354 restart:
355         dirent = readdir(*dir);
356         if (!dirent) {
357                 if (errno == 0) {
358                         closedir(*dir);
359                         *dir = NULL;
360                         return 0;
361                 }
362                 error = errno;
363                 printf("Failed to read /proc directory: %s\n", strerror(error));
364                 return -1;
365         }
366
367         if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
368                 goto restart;
369
370         return atoi(dirent->d_name);
371 }
372
373 static int opendir_check(DIR **dir, const char *path)
374 {
375         int error;
376
377         if (!*dir) {
378                 *dir = opendir(path);
379                 if (!dir) {
380                         error = errno;
381                         fprintf(stderr, "Failed to open %s directory: %s\n",
382                                 path, strerror(error));
383                         return -1;
384                 }
385         }
386
387         return 0;
388 }
389
390 static int get_next_tid(int pid, DIR **dir)
391 {
392         if (*dir == NULL) {
393                 char path[64];
394
395                 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
396                 if (opendir_check(dir, path))
397                         return -1;
398         }
399
400         return parse_pid(dir);
401 }
402
403 static int get_next_pid(DIR **dir)
404 {
405         if (opendir_check(dir, "/proc"))
406                 return -1;
407
408         return parse_pid(dir);
409 }
410
411 static int get_next_pid_by_name(DIR **dir, char *name)
412 {
413         int pid;
414         char *pname;
415
416         if (opendir_check(dir, "/proc"))
417                 return -1;
418
419         while (1) {
420                 pid = parse_pid(dir);
421                 if (pid <= 0)
422                         break;
423
424                 pname = get_name_by_pid(pid);
425                 if (pname == NULL)
426                         continue;
427                 if (strcmp(pname, name))
428                         continue;
429
430                 return pid;
431         }
432
433         return 0;
434 }
435
436 static int read_pageframe_with_threads(int pid,
437                                 struct pageframe *pageframe,
438                                 struct process *process_list,
439                                 struct parse_opts *opts)
440 {
441         DIR *dir = NULL;
442         int tid;
443         int count = 0;
444
445         while (1) {
446                 if (opts->with_threads)
447                         tid = get_next_tid(pid, &dir);
448                 else
449                         tid = pid;
450
451                 if (tid <= 0)
452                         return count;
453
454                 count += read_pageframe(pid, tid, pageframe, process_list,
455                                         opts);
456
457                 if (!opts->with_threads)
458                         break;
459         }
460
461         return count;
462 }
463
464 int scan_all_pids(struct pageframe *pf, struct process *process_list,
465                 struct parse_opts *opts)
466 {
467         DIR *dir = NULL;
468         int pid;
469         int count = 0;
470
471         if (is_parse_option(opts, PARSE_PROCESS_NAME)) {
472                 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
473                         count += read_pageframe_with_threads(pid, pf,
474                                                         process_list,
475                                                         opts);
476                 }
477                 dir = NULL;
478         }
479
480         if (is_parse_option(opts, PARSE_PID))
481                 count = read_pageframe_with_threads(opts->pid, pf, process_list,
482                                                 opts);
483
484         if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
485                 printf("Failed to find any matching processes "
486                         "with given arguments\n");
487                 return -1;
488         }
489
490         if (is_parse_option(opts, PARSE_DUMP))
491                 return 0;
492
493         if (is_parse_option(opts, PARSE_MAP_NAME)) {
494                 while (1) {
495                         pid = get_next_pid(&dir);
496                         if (pid <= 0)
497                                 break;
498                         read_pageframe_with_threads(pid, pf, process_list,
499                                                 opts);
500                 }
501         }
502         /* Do not add new pages in the tree after the initial scan */
503         opts->parse_mask |= PARSE_NOADD_TREE;
504
505         while (1) {
506                 pid = get_next_pid(&dir);
507                 if (pid <= 0)
508                         break;
509                 read_pageframe_with_threads(pid, pf, process_list, opts);
510         }
511
512         return 0;
513 }