11 static struct maps_list *alloc_maplist(void)
13 struct maps_list *map;
15 map = malloc(sizeof *map);
19 memset(map, 0, sizeof(*map));
20 INIT_LIST_HEAD(&map->list);
25 static struct maps *alloc_map(void)
29 map = malloc(sizeof *map);
33 memset(map, 0, sizeof(*map));
34 INIT_LIST_HEAD(&map->list);
39 static struct maps *parse_maps(FILE *file, int pid, int tid)
41 struct maps *the_map = NULL;
45 while(fgets(line, sizeof(line), file)) {
46 struct maps *map = alloc_map();
47 unsigned long start, end;
56 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
60 printf("Error reading input: %s\n", line);
66 map->size = end - start;
71 strncpy(map->name, name, sizeof(map->name));
73 list_add_tail(&map->list, &the_map->list);
79 static void clear_pageframe(struct pageframe *pf)
81 memset(pf, 0, sizeof(*pf));
84 static struct pageframe *alloc_pageframe(void)
86 struct pageframe *pageframe;
88 pageframe = malloc(sizeof *pageframe);
89 if (pageframe == NULL)
92 clear_pageframe(pageframe);
97 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
99 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
101 /* Refer Documentation/vm/pagemap.txt for the format */
102 pf->page_present = !!(BITRANGE(63, 63) & p);
103 pf->page_swapped = !!(BITRANGE(62, 62) & p);
104 pf->page_shift = (BITRANGE(55, 60) & p) >> 55;
105 pf->pfn = (BITRANGE(0, 54) & p);
106 pf->swap_type = (BITRANGE(0, 4) & p);
107 pf->swap_offset = (BITRANGE(5, 54) & p) >> 5;
109 printf("pfn: %lx shift: %d present: %d swapped %d\n",
110 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
114 static int compare_pageframe(struct bintree *at, struct bintree *bt)
116 struct pageframe *a, *b;
117 a = tree_to_pageframe(at);
118 b = tree_to_pageframe(bt);
120 return a->pfn - b->pfn;
123 struct bintree_ops pageframe_ops = {
124 .compare = compare_pageframe,
127 static int read_cmdline(int pid, int tid, char *cmdline, size_t len)
133 snprintf(path, sizeof(path), "/proc/%d/task/%d/cmdline", pid, tid);
134 file = fopen(path, "rb");
139 ret = fread(cmdline, 1, len, file);
141 cmdline[ret - 1] = 0;
144 return ret > 0 ? 0 : -1;
147 static int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
150 if (opts->parse_mask & PARSE_PID) {
151 if (opts->pid == map->pid)
155 if (opts->parse_mask & PARSE_MAP_NAME) {
156 if (!strcmp(opts->name, map->name))
163 /* Read data from the /proc/pid/pagemap file */
164 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
165 struct maps *maps, struct parse_opts *opts)
168 struct maps_list *tmp;
169 struct pageframe *match, *pageframe = NULL;
171 unsigned long long pf[10240];
177 /* Go through the list of allocated memory areas */
178 list_for_each_entry(map, &maps->list, list) {
179 start = map->start >> (PAGE_SHIFT - 3);
180 len = map->size >> (PAGE_SHIFT - 3);
182 ret = fseek(file, start, SEEK_SET);
185 fprintf(stderr, "Error seeking to %lx: %s\n", start,
190 for (i = 0; i < len; i++) {
193 MIN(sizeof(pf), (len - i) * 8), file);
200 pageframe = alloc_pageframe();
201 ret -= sizeof(pf[0]);
203 /* ignore unused pages */
204 if (!pf[ret / sizeof(pf[0])])
207 pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
209 /* ignore unused pages */
210 if (!(pageframe->page_swapped ||
211 pageframe->page_present))
214 if (check_parse_opts(opts, pageframe, map)) {
215 match = tree_to_pageframe(
216 bintree_add(&pf_tree->tree,
220 match = tree_to_pageframe(
221 bintree_find(&pf_tree->tree,
229 if (match == pageframe)
234 * Add a link from the physical page to this
238 match->ml = alloc_maplist();
239 match->ml->map = map;
241 tmp = alloc_maplist();
243 list_add(&match->ml->list, &tmp->list);
246 if (match->page_present) {
247 map->pages_present++;
248 } else if (match->page_swapped) {
249 map->pages_swapped++;
257 void read_pageframe(int pid, int tid, struct pageframe *pageframe,
258 struct process **process_list, struct parse_opts *opts)
261 struct process *process;
265 process = malloc(sizeof(*process));
266 memset(process, 0, sizeof(*process));
267 INIT_LIST_HEAD(&process->list);
269 if (*process_list == NULL)
270 *process_list = process;
275 list_add_tail(&process->list, &(*process_list)->list);
277 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
278 file = fopen(path, "rb");
283 maps = parse_maps(file, pid, tid);
285 process->maps = maps;
287 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
288 file = fopen(path, "rb");
293 parse_pageframe(file, pageframe, maps, opts);
296 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
302 list_for_each_entry(maps, &process->maps->list, list) {
303 process->pages_present += maps->pages_present;
304 process->pages_swapped += maps->pages_swapped;
310 static int parse_pid(DIR **dir)
312 struct dirent *dirent;
316 dirent = readdir(*dir);
324 printf("Failed to read /proc directory: %s\n", strerror(error));
328 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
331 return atoi(dirent->d_name);
334 static int opendir_check(DIR **dir, const char *path)
339 *dir = opendir("/proc");
342 fprintf(stderr, "Failed to open /proc directory: %s\n",
351 static int get_next_tid(int pid, DIR **dir)
356 snprintf(path, sizeof(path), "/proc/%d/task/", pid);
357 if (opendir_check(dir, path))
361 return parse_pid(dir);
364 static int get_next_pid(DIR **dir)
366 if (opendir_check(dir, "/proc"))
369 return parse_pid(dir);
372 static void read_pageframe_with_threads(int pid,
373 struct pageframe *pageframe,
374 struct process **process_list,
375 struct parse_opts *opts)
381 if (opts->with_threads)
382 tid = get_next_tid(pid, &dir);
389 read_pageframe(pid, tid, pageframe, process_list, opts);
391 if (!opts->with_threads)
396 void scan_all_pids(struct pageframe *pf, struct process **process_list,
397 struct parse_opts *opts)
402 if (opts->parse_mask & PARSE_PID)
403 read_pageframe_with_threads(opts->pid, pf, process_list, opts);
406 pid = get_next_pid(&dir);
409 read_pageframe_with_threads(pid, pf, process_list, opts);