12 static struct maps_list *alloc_maplist(void)
14 struct maps_list *map;
16 map = malloc(sizeof *map);
20 memset(map, 0, sizeof(*map));
21 INIT_LIST_HEAD(&map->list);
26 static struct maps *alloc_map(void)
30 map = malloc(sizeof *map);
34 memset(map, 0, sizeof(*map));
35 INIT_LIST_HEAD(&map->list);
40 static struct maps *parse_maps(FILE *file, int pid, int tid)
42 struct maps *the_map = NULL;
46 while (fgets(line, sizeof(line), file)) {
47 struct maps *map = alloc_map();
48 unsigned long start, end;
57 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
61 printf("Error reading input: %s\n", line);
67 map->size = end - start;
72 strncpy(map->name, name, sizeof(map->name));
74 list_add_tail(&map->list, &the_map->list);
80 static void clear_pageframe(struct pageframe *pf)
82 memset(pf, 0, sizeof(*pf));
85 static struct pageframe *alloc_pageframe(void)
87 struct pageframe *pageframe;
89 pageframe = malloc(sizeof *pageframe);
90 if (pageframe == NULL)
93 clear_pageframe(pageframe);
94 INIT_LIST_HEAD(&pageframe->ml.list);
99 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
101 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
103 /* Refer Documentation/vm/pagemap.txt for the format */
104 pf->page_present = !!(BITRANGE(63, 63) & p);
105 pf->page_swapped = !!(BITRANGE(62, 62) & p);
106 pf->page_shift = (BITRANGE(55, 60) & p) >> 55;
107 pf->pfn = (BITRANGE(0, 54) & p);
108 pf->swap_type = (BITRANGE(0, 4) & p);
109 pf->swap_offset = (BITRANGE(5, 54) & p) >> 5;
111 printf("pfn: %lx shift: %d present: %d swapped %d\n",
112 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
116 static int compare_pageframe(struct bintree *at, struct bintree *bt)
118 struct pageframe *a, *b;
119 a = tree_to_pageframe(at);
120 b = tree_to_pageframe(bt);
122 return a->pfn - b->pfn;
125 struct bintree_ops pageframe_ops = {
126 .compare = compare_pageframe,
129 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
135 snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
136 file = fopen(path, "rb");
141 ret = fread(cmdline, 1, len, file);
143 cmdline[ret - 1] = 0;
146 return ret > 0 ? 0 : -1;
149 static char *get_name_by_pid(int pid)
152 static char cmdline[128];
158 if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
163 bname = basename(cmdline);
169 static int should_scan_process(struct parse_opts *opts, struct process *process)
174 if (opts->parse_mask & PARSE_PROCESS_NAME) {
175 name = get_name_by_pid(process->pid);
176 if (!strcmp(opts->name, name ? name : ""))
180 if (opts->parse_mask & PARSE_PID) {
181 if (opts->pid == process->pid)
185 if (opts->parse_mask & PARSE_MAP_NAME)
188 if (opts->parse_mask & PARSE_NOADD_TREE)
194 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
198 if (opts->parse_mask & PARSE_MAP_NAME) {
199 if (!strcmp(opts->name, map->name))
202 if (opts->parse_mask & PARSE_NOADD_TREE)
210 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
213 if (opts->parse_mask & PARSE_NOADD_TREE)
219 /* Read data from the /proc/pid/pagemap file */
220 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
221 struct maps *maps, struct parse_opts *opts)
224 struct maps_list *tmp;
225 struct pageframe *match, *pageframe = NULL;
227 unsigned long long pf[10240];
233 /* Go through the list of allocated memory areas */
234 list_for_each_entry(map, &maps->list, list) {
235 start = map->start >> (PAGE_SHIFT - 3);
236 len = map->size >> (PAGE_SHIFT);
238 if (!should_scan_mapping(opts, map))
241 ret = fseek(file, start, SEEK_SET);
244 fprintf(stderr, "Error seeking to %lx: %s\n", start,
249 for (i = 0; i < len; i++) {
252 MIN(sizeof(pf), (len - i) * 8), file);
259 pageframe = alloc_pageframe();
260 ret -= sizeof(pf[0]);
262 /* ignore unused pages */
263 if (!pf[ret / sizeof(pf[0])])
266 pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
268 /* ignore unused pages */
269 if (!(pageframe->page_swapped ||
270 pageframe->page_present))
273 if (should_add_to_tree(opts, pageframe, map)) {
274 match = tree_to_pageframe(
275 bintree_add(&pf_tree->tree,
279 match = tree_to_pageframe(
280 bintree_find(&pf_tree->tree,
288 if (match == pageframe)
293 * Add a link from the physical page to this
296 tmp = alloc_maplist();
298 list_add(&tmp->list, &match->ml.list);
300 if (match->page_present)
301 map->pages_present++;
302 else if (match->page_swapped)
303 map->pages_swapped++;
310 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
311 struct process *process_list, struct parse_opts *opts)
314 struct process *process;
318 process = malloc(sizeof(*process));
319 memset(process, 0, sizeof(*process));
320 INIT_LIST_HEAD(&process->list);
325 if (!should_scan_process(opts, process))
328 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
329 file = fopen(path, "rb");
334 maps = parse_maps(file, pid, tid);
336 process->maps = maps;
338 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
339 file = fopen(path, "rb");
344 parse_pageframe(file, pageframe, maps, opts);
347 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
351 list_for_each_entry(maps, &process->maps->list, list) {
352 process->pages_present += maps->pages_present;
353 process->pages_swapped += maps->pages_swapped;
357 list_add_tail(&process->list, &process_list->list);
366 static int parse_pid(DIR **dir)
368 struct dirent *dirent;
372 dirent = readdir(*dir);
380 printf("Failed to read /proc directory: %s\n", strerror(error));
384 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
387 return atoi(dirent->d_name);
390 static int opendir_check(DIR **dir, const char *path)
395 *dir = opendir(path);
398 fprintf(stderr, "Failed to open %s directory: %s\n",
399 path, strerror(error));
407 static int get_next_tid(int pid, DIR **dir)
412 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
413 if (opendir_check(dir, path))
417 return parse_pid(dir);
420 static int get_next_pid(DIR **dir)
422 if (opendir_check(dir, "/proc"))
425 return parse_pid(dir);
428 static int get_next_pid_by_name(DIR **dir, char *name)
433 if (opendir_check(dir, "/proc"))
437 pid = parse_pid(dir);
441 pname = get_name_by_pid(pid);
444 if (strcmp(pname, name))
453 static int read_pageframe_with_threads(int pid,
454 struct pageframe *pageframe,
455 struct process *process_list,
456 struct parse_opts *opts)
463 if (opts->with_threads)
464 tid = get_next_tid(pid, &dir);
471 count += read_pageframe(pid, tid, pageframe, process_list,
474 if (!opts->with_threads)
481 int scan_all_pids(struct pageframe *pf, struct process *process_list,
482 struct parse_opts *opts)
488 if (opts->parse_mask & PARSE_PROCESS_NAME) {
489 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
490 count += read_pageframe_with_threads(pid, pf,
497 if (opts->parse_mask & PARSE_PID)
498 count = read_pageframe_with_threads(opts->pid, pf, process_list,
501 if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) {
502 printf("Failed to find any matching processes "
503 "with given arguments\n");
507 if (opts->parse_mask & PARSE_DUMP)
510 if (opts->parse_mask & PARSE_MAP_NAME) {
512 pid = get_next_pid(&dir);
515 read_pageframe_with_threads(pid, pf, process_list,
519 /* Do not add new pages in the tree after the initial scan */
520 opts->parse_mask |= PARSE_NOADD_TREE;
523 pid = get_next_pid(&dir);
526 read_pageframe_with_threads(pid, pf, process_list, opts);