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);
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)
158 if (is_parse_option(opts, PARSE_PROCESS_NAME)) {
159 name = get_name_by_pid(process->pid);
160 if (!strcmp(opts->name, name ? name : ""))
164 if (is_parse_option(opts, PARSE_PID)) {
165 list_for_each_entry(pid, &opts->pidlist, list) {
166 if (pid->pid == process->pid) {
173 if (is_parse_option(opts, PARSE_MAP_NAME))
176 if (is_parse_option(opts, PARSE_NOADD_TREE))
182 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
186 if (is_parse_option(opts, PARSE_MAP_NAME)) {
187 if (!strcmp(opts->name, map->name))
190 if (is_parse_option(opts, PARSE_NOADD_TREE))
198 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
201 if (is_parse_option(opts, PARSE_NOADD_TREE))
207 /* Read data from the /proc/pid/pagemap file */
208 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
209 struct maps *maps, struct parse_opts *opts)
212 struct maps_list *tmp;
213 struct pageframe *match, *pageframe = NULL;
215 unsigned long long pf[10240];
221 /* Go through the list of allocated memory areas */
222 list_for_each_entry(map, &maps->list, list) {
223 start = map->start >> (PAGE_SHIFT - 3);
224 len = map->size >> (PAGE_SHIFT);
226 if (!should_scan_mapping(opts, map))
229 ret = fseek(file, start, SEEK_SET);
232 fprintf(stderr, "Error seeking to %lx: %s\n", start,
237 for (i = 0; i < len; i++) {
240 MIN(sizeof(pf), (len - i) * 8), file);
247 pageframe = alloc_pageframe();
248 ret -= sizeof(pf[0]);
250 /* ignore unused pages */
251 if (!pf[ret / sizeof(pf[0])])
254 pageframe->pf = (pf[ret / sizeof(pf[0])]);
256 /* ignore unused pages */
257 if (!(page_swapped(pageframe) ||
258 page_present(pageframe)))
261 if (should_add_to_tree(opts, pageframe, map)) {
262 match = tree_to_pageframe(
263 bintree_add(&pf_tree->tree,
267 match = tree_to_pageframe(
268 bintree_find(&pf_tree->tree,
276 if (match == pageframe)
281 * Add a link from the physical page to this
284 tmp = alloc_maplist();
286 list_add(&tmp->list, &match->ml);
288 if (page_present(match))
289 map->pages_present++;
290 else if (page_swapped(match))
291 map->pages_swapped++;
298 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
299 struct process *process_list, struct parse_opts *opts)
302 struct process *process;
306 process = malloc(sizeof(*process));
307 memset(process, 0, sizeof(*process));
308 INIT_LIST_HEAD(&process->list);
313 if (!should_scan_process(opts, process))
316 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
317 file = fopen(path, "rb");
322 maps = parse_maps(file, pid, tid);
324 process->maps = maps;
326 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
327 file = fopen(path, "rb");
332 parse_pageframe(file, pageframe, maps, opts);
335 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
339 list_for_each_entry(maps, &process->maps->list, list) {
340 process->pages_present += maps->pages_present;
341 process->pages_swapped += maps->pages_swapped;
345 if (!is_parse_option(opts, PARSE_NOADD_TREE))
346 process->is_initial_pid = 1;
348 list_add_tail(&process->list, &process_list->list);
357 static int parse_pid(DIR **dir)
359 struct dirent *dirent;
363 dirent = readdir(*dir);
371 printf("Failed to read /proc directory: %s\n", strerror(error));
375 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
378 return atoi(dirent->d_name);
381 static int opendir_check(DIR **dir, const char *path)
386 *dir = opendir(path);
389 fprintf(stderr, "Failed to open %s directory: %s\n",
390 path, strerror(error));
398 static int get_next_tid(int pid, DIR **dir)
403 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
404 if (opendir_check(dir, path))
408 return parse_pid(dir);
411 static int get_next_pid(DIR **dir)
413 if (opendir_check(dir, "/proc"))
416 return parse_pid(dir);
419 static int get_next_pid_by_name(DIR **dir, char *name)
424 if (opendir_check(dir, "/proc"))
428 pid = parse_pid(dir);
432 pname = get_name_by_pid(pid);
435 if (strcmp(pname, name))
444 static int read_pageframe_with_threads(int pid,
445 struct pageframe *pageframe,
446 struct process *process_list,
447 struct parse_opts *opts)
454 if (opts->with_threads)
455 tid = get_next_tid(pid, &dir);
462 count += read_pageframe(pid, tid, pageframe, process_list,
465 if (!opts->with_threads)
472 int scan_all_pids(struct pageframe *pf, struct process *process_list,
473 struct parse_opts *opts)
475 struct pidlist *pidlist;
480 if (is_parse_option(opts, PARSE_PROCESS_NAME)) {
481 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
482 count += read_pageframe_with_threads(pid, pf,
489 if (is_parse_option(opts, PARSE_PID)) {
490 list_for_each_entry(pidlist, &opts->pidlist, list) {
491 count += read_pageframe_with_threads(pidlist->pid, pf,
496 if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
497 printf("Failed to find any matching processes "
498 "with given arguments\n");
502 if (is_parse_option(opts, PARSE_DUMP))
505 if (is_parse_option(opts, PARSE_MAP_NAME)) {
507 pid = get_next_pid(&dir);
510 read_pageframe_with_threads(pid, pf, process_list,
514 /* Do not add new pages in the tree after the initial scan */
515 opts->parse_mask |= PARSE_NOADD_TREE;
518 pid = get_next_pid(&dir);
521 read_pageframe_with_threads(pid, pf, process_list, opts);