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 static int compare_pageframe(struct bintree *at, struct bintree *bt)
101 struct pageframe *a, *b;
102 a = tree_to_pageframe(at);
103 b = tree_to_pageframe(bt);
105 return a->pf - b->pf;
108 struct bintree_ops pageframe_ops = {
109 .compare = compare_pageframe,
112 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
118 snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
119 file = fopen(path, "rb");
124 ret = fread(cmdline, 1, len, file);
126 cmdline[ret - 1] = 0;
129 return ret > 0 ? 0 : -1;
132 static char *get_name_by_pid(int pid)
135 static char cmdline[128];
141 if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
146 bname = basename(cmdline);
152 static int should_scan_process(struct parse_opts *opts, struct process *process)
157 if (is_parse_option(opts, PARSE_PROCESS_NAME)) {
158 name = get_name_by_pid(process->pid);
159 if (!strcmp(opts->name, name ? name : ""))
163 if (is_parse_option(opts, PARSE_PID)) {
164 if (opts->pid == process->pid)
168 if (is_parse_option(opts, PARSE_MAP_NAME))
171 if (is_parse_option(opts, PARSE_NOADD_TREE))
177 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
181 if (is_parse_option(opts, PARSE_MAP_NAME)) {
182 if (!strcmp(opts->name, map->name))
185 if (is_parse_option(opts, PARSE_NOADD_TREE))
193 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
196 if (is_parse_option(opts, PARSE_NOADD_TREE))
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)
207 struct maps_list *tmp;
208 struct pageframe *match, *pageframe = NULL;
210 unsigned long long pf[10240];
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);
221 if (!should_scan_mapping(opts, map))
224 ret = fseek(file, start, SEEK_SET);
227 fprintf(stderr, "Error seeking to %lx: %s\n", start,
232 for (i = 0; i < len; i++) {
235 MIN(sizeof(pf), (len - i) * 8), file);
242 pageframe = alloc_pageframe();
243 ret -= sizeof(pf[0]);
245 /* ignore unused pages */
246 if (!pf[ret / sizeof(pf[0])])
249 pageframe->pf = (pf[ret / sizeof(pf[0])]);
251 /* ignore unused pages */
252 if (!(page_swapped(pageframe) ||
253 page_present(pageframe)))
256 if (should_add_to_tree(opts, pageframe, map)) {
257 match = tree_to_pageframe(
258 bintree_add(&pf_tree->tree,
262 match = tree_to_pageframe(
263 bintree_find(&pf_tree->tree,
271 if (match == pageframe)
276 * Add a link from the physical page to this
279 tmp = alloc_maplist();
281 list_add(&tmp->list, &match->ml.list);
283 if (page_present(match))
284 map->pages_present++;
285 else if (page_swapped(match))
286 map->pages_swapped++;
293 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
294 struct process *process_list, struct parse_opts *opts)
297 struct process *process;
301 process = malloc(sizeof(*process));
302 memset(process, 0, sizeof(*process));
303 INIT_LIST_HEAD(&process->list);
308 if (!should_scan_process(opts, process))
311 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
312 file = fopen(path, "rb");
317 maps = parse_maps(file, pid, tid);
319 process->maps = maps;
321 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
322 file = fopen(path, "rb");
327 parse_pageframe(file, pageframe, maps, opts);
330 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
334 list_for_each_entry(maps, &process->maps->list, list) {
335 process->pages_present += maps->pages_present;
336 process->pages_swapped += maps->pages_swapped;
340 if (!is_parse_option(opts, PARSE_NOADD_TREE))
341 process->is_initial_pid = 1;
343 list_add_tail(&process->list, &process_list->list);
352 static int parse_pid(DIR **dir)
354 struct dirent *dirent;
358 dirent = readdir(*dir);
366 printf("Failed to read /proc directory: %s\n", strerror(error));
370 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
373 return atoi(dirent->d_name);
376 static int opendir_check(DIR **dir, const char *path)
381 *dir = opendir(path);
384 fprintf(stderr, "Failed to open %s directory: %s\n",
385 path, strerror(error));
393 static int get_next_tid(int pid, DIR **dir)
398 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
399 if (opendir_check(dir, path))
403 return parse_pid(dir);
406 static int get_next_pid(DIR **dir)
408 if (opendir_check(dir, "/proc"))
411 return parse_pid(dir);
414 static int get_next_pid_by_name(DIR **dir, char *name)
419 if (opendir_check(dir, "/proc"))
423 pid = parse_pid(dir);
427 pname = get_name_by_pid(pid);
430 if (strcmp(pname, name))
439 static int read_pageframe_with_threads(int pid,
440 struct pageframe *pageframe,
441 struct process *process_list,
442 struct parse_opts *opts)
449 if (opts->with_threads)
450 tid = get_next_tid(pid, &dir);
457 count += read_pageframe(pid, tid, pageframe, process_list,
460 if (!opts->with_threads)
467 int scan_all_pids(struct pageframe *pf, struct process *process_list,
468 struct parse_opts *opts)
474 if (is_parse_option(opts, PARSE_PROCESS_NAME)) {
475 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
476 count += read_pageframe_with_threads(pid, pf,
483 if (is_parse_option(opts, PARSE_PID))
484 count = read_pageframe_with_threads(opts->pid, pf, process_list,
487 if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
488 printf("Failed to find any matching processes "
489 "with given arguments\n");
493 if (is_parse_option(opts, PARSE_DUMP))
496 if (is_parse_option(opts, PARSE_MAP_NAME)) {
498 pid = get_next_pid(&dir);
501 read_pageframe_with_threads(pid, pf, process_list,
505 /* Do not add new pages in the tree after the initial scan */
506 opts->parse_mask |= PARSE_NOADD_TREE;
509 pid = get_next_pid(&dir);
512 read_pageframe_with_threads(pid, pf, process_list, opts);