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);
98 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
100 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
102 /* Refer Documentation/vm/pagemap.txt for the format */
103 pf->page_present = !!(BITRANGE(63, 63) & p);
104 pf->page_swapped = !!(BITRANGE(62, 62) & p);
105 pf->page_shift = (BITRANGE(55, 60) & p) >> 55;
106 pf->pfn = (BITRANGE(0, 54) & p);
107 pf->swap_type = (BITRANGE(0, 4) & p);
108 pf->swap_offset = (BITRANGE(5, 54) & p) >> 5;
110 printf("pfn: %lx shift: %d present: %d swapped %d\n",
111 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
115 static int compare_pageframe(struct bintree *at, struct bintree *bt)
117 struct pageframe *a, *b;
118 a = tree_to_pageframe(at);
119 b = tree_to_pageframe(bt);
121 return a->pfn - b->pfn;
124 struct bintree_ops pageframe_ops = {
125 .compare = compare_pageframe,
128 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
134 snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
135 file = fopen(path, "rb");
140 ret = fread(cmdline, 1, len, file);
142 cmdline[ret - 1] = 0;
145 return ret > 0 ? 0 : -1;
148 static char *get_name_by_pid(int pid)
151 static char cmdline[128];
157 if (read_cmdline(pid, pid, cmdline, sizeof(cmdline))) {
162 bname = basename(cmdline);
168 static int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
171 if (opts->parse_mask & PARSE_PID) {
172 if (opts->pid == map->pid)
176 if (opts->parse_mask & PARSE_MAP_NAME) {
177 if (!strcmp(opts->name, map->name))
181 if (opts->parse_mask & PARSE_PROCESS_NAME) {
182 if (!strcmp(opts->name, get_name_by_pid(map->pid)))
189 /* Read data from the /proc/pid/pagemap file */
190 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
191 struct maps *maps, struct parse_opts *opts)
194 struct maps_list *tmp;
195 struct pageframe *match, *pageframe = NULL;
197 unsigned long long pf[10240];
203 /* Go through the list of allocated memory areas */
204 list_for_each_entry(map, &maps->list, list) {
205 start = map->start >> (PAGE_SHIFT - 3);
206 len = map->size >> (PAGE_SHIFT);
208 ret = fseek(file, start, SEEK_SET);
211 fprintf(stderr, "Error seeking to %lx: %s\n", start,
216 for (i = 0; i < len; i++) {
219 MIN(sizeof(pf), (len - i) * 8), file);
226 pageframe = alloc_pageframe();
227 ret -= sizeof(pf[0]);
229 /* ignore unused pages */
230 if (!pf[ret / sizeof(pf[0])])
233 pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
235 /* ignore unused pages */
236 if (!(pageframe->page_swapped ||
237 pageframe->page_present))
240 if (check_parse_opts(opts, pageframe, map)) {
241 match = tree_to_pageframe(
242 bintree_add(&pf_tree->tree,
246 match = tree_to_pageframe(
247 bintree_find(&pf_tree->tree,
255 if (match == pageframe)
260 * Add a link from the physical page to this
264 match->ml = alloc_maplist();
265 match->ml->map = map;
267 tmp = alloc_maplist();
269 list_add(&tmp->list, &match->ml->list);
272 if (match->page_present)
273 map->pages_present++;
274 else if (match->page_swapped)
275 map->pages_swapped++;
282 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
283 struct process **process_list, struct parse_opts *opts)
286 struct process *process;
290 process = malloc(sizeof(*process));
291 memset(process, 0, sizeof(*process));
292 INIT_LIST_HEAD(&process->list);
294 if (*process_list == NULL)
295 *process_list = process;
300 list_add_tail(&process->list, &(*process_list)->list);
302 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
303 file = fopen(path, "rb");
308 maps = parse_maps(file, pid, tid);
310 process->maps = maps;
312 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
313 file = fopen(path, "rb");
318 parse_pageframe(file, pageframe, maps, opts);
321 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
327 list_for_each_entry(maps, &process->maps->list, list) {
328 process->pages_present += maps->pages_present;
329 process->pages_swapped += maps->pages_swapped;
335 static int parse_pid(DIR **dir)
337 struct dirent *dirent;
341 dirent = readdir(*dir);
349 printf("Failed to read /proc directory: %s\n", strerror(error));
353 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
356 return atoi(dirent->d_name);
359 static int opendir_check(DIR **dir, const char *path)
364 *dir = opendir(path);
367 fprintf(stderr, "Failed to open %s directory: %s\n",
368 path, strerror(error));
376 static int get_next_tid(int pid, DIR **dir)
381 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
382 if (opendir_check(dir, path))
386 return parse_pid(dir);
389 static int get_next_pid(DIR **dir)
391 if (opendir_check(dir, "/proc"))
394 return parse_pid(dir);
397 static int get_next_pid_by_name(DIR **dir, char *name)
402 if (opendir_check(dir, "/proc"))
406 pid = parse_pid(dir);
410 pname = get_name_by_pid(pid);
413 if (strcmp(pname, name))
422 static int read_pageframe_with_threads(int pid,
423 struct pageframe *pageframe,
424 struct process **process_list,
425 struct parse_opts *opts)
432 if (opts->with_threads)
433 tid = get_next_tid(pid, &dir);
440 count += read_pageframe(pid, tid, pageframe, process_list,
443 if (!opts->with_threads)
450 int scan_all_pids(struct pageframe *pf, struct process **process_list,
451 struct parse_opts *opts)
457 if (opts->parse_mask & PARSE_PROCESS_NAME) {
458 while ((pid = get_next_pid_by_name(&dir, opts->name))) {
459 count += read_pageframe_with_threads(pid, pf,
466 if (opts->parse_mask & PARSE_PID)
467 count = read_pageframe_with_threads(opts->pid, pf, process_list,
470 if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) {
471 printf("Failed to find any matching processes "
472 "with given arguments\n");
476 if (opts->parse_mask & PARSE_DUMP)
480 pid = get_next_pid(&dir);
483 read_pageframe_with_threads(pid, pf, process_list, opts);