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)
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;
70 strncpy(map->name, name, sizeof(map->name));
72 list_add_tail(&map->list, &the_map->list);
78 static void clear_pageframe(struct pageframe *pf)
80 memset(pf, 0, sizeof(*pf));
83 static struct pageframe *alloc_pageframe(void)
85 struct pageframe *pageframe;
87 pageframe = malloc(sizeof *pageframe);
88 if (pageframe == NULL)
91 clear_pageframe(pageframe);
96 #define BITRANGE(first, last) (((2ll << (last - first)) - 1) << first)
98 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
100 /* Refer Documentation/vm/pagemap.txt for the format */
101 pf->page_present = !!(BITRANGE(63, 63) & p);
102 pf->page_swapped = !!(BITRANGE(62, 62) & p);
103 pf->page_shift = (BITRANGE(55, 60) & p) >> 55;
104 pf->pfn = (BITRANGE(0, 54) & p);
105 pf->swap_type = (BITRANGE(0, 4) & p);
106 pf->swap_offset = (BITRANGE(5, 54) & p) >> 5;
108 printf("pfn: %lx shift: %d present: %d swapped %d\n",
109 pf->pfn, pf->page_shift, pf->page_present, pf->page_swapped);
113 static int compare_pageframe(struct bintree *at, struct bintree *bt)
115 struct pageframe *a, *b;
116 a = tree_to_pageframe(at);
117 b = tree_to_pageframe(bt);
119 return a->pfn - b->pfn;
122 struct bintree_ops pageframe_ops = {
123 .compare = compare_pageframe,
126 static int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
129 if (opts->parse_mask & PARSE_PID) {
130 if (opts->pid == map->pid)
134 if (opts->parse_mask & PARSE_MAP_NAME) {
135 if (!strcmp(opts->map_name, map->name))
142 /* Read data from the /proc/pid/pagemap file */
143 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
144 struct maps *maps, struct parse_opts *opts)
147 struct maps_list *tmp;
148 struct pageframe *match, *pageframe = NULL;
150 unsigned long long pf[10240];
156 /* Go through the list of allocated memory areas */
157 list_for_each_entry(map, &maps->list, list) {
158 start = map->start >> (PAGE_SHIFT - 3);
159 len = map->size >> (PAGE_SHIFT - 3);
161 ret = fseek(file, start, SEEK_SET);
164 fprintf(stderr, "Error seeking to %lx: %s\n", start,
169 for (i = 0; i < len; i++) {
172 MIN(sizeof(pf), (len - i) * 8), file);
179 pageframe = alloc_pageframe();
180 ret -= sizeof(pf[0]);
182 /* ignore unused pages */
183 if (!pf[ret / sizeof(pf[0])])
186 pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
188 /* ignore unused pages */
189 if (!(pageframe->page_swapped ||
190 pageframe->page_present))
193 if (check_parse_opts(opts, pageframe, map)) {
194 match = tree_to_pageframe(
195 bintree_add(&pf_tree->tree,
199 match = tree_to_pageframe(
200 bintree_find(&pf_tree->tree,
208 if (match == pageframe)
213 * Add a link from the physical page to this
217 match->ml = alloc_maplist();
218 match->ml->map = map;
220 tmp = alloc_maplist();
222 list_add(&match->ml->list, &tmp->list);
225 if (match->page_present) {
226 map->pages_present++;
227 } else if (match->page_swapped) {
228 map->pages_swapped++;
236 void read_pageframe(int pid, struct pageframe *pageframe,
237 struct process **process_list, struct parse_opts *opts)
240 struct process *process;
245 process = malloc(sizeof(*process));
246 memset(process, 0, sizeof(*process));
247 INIT_LIST_HEAD(&process->list);
249 if (*process_list == NULL)
250 *process_list = process;
254 list_add_tail(&process->list, &(*process_list)->list);
256 snprintf(path, sizeof(path), "/proc/%d/maps", pid);
257 file = fopen(path, "rb");
262 maps = parse_maps(file, pid);
264 process->maps = maps;
266 snprintf(path, sizeof(path), "/proc/%d/pagemap", pid);
267 file = fopen(path, "rb");
272 parse_pageframe(file, pageframe, maps, opts);
275 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
276 file = fopen(path, "rb");
281 ret = fread(process->name, 1, sizeof(process->name), file);
283 process->name[ret - 1] = 0;
289 list_for_each_entry(maps, &process->maps->list, list) {
290 process->pages_present += maps->pages_present;
291 process->pages_swapped += maps->pages_swapped;
297 static int get_next_pid(void)
299 static DIR *dir = NULL;
300 struct dirent *dirent;
304 dir = opendir("/proc");
307 printf("Failed to open /proc directory: %s\n",
314 dirent = readdir(dir);
322 printf("Failed to read /proc directory: %s\n", strerror(error));
326 if (dirent->d_name[0] < '0' || dirent->d_name[0] > '9')
329 return atoi(dirent->d_name);
332 void scan_all_pids(struct pageframe *pf, struct process **process_list,
333 struct parse_opts *opts)
337 if (opts->parse_mask & PARSE_PID)
338 read_pageframe(opts->pid, pf, process_list, opts);
341 pid = get_next_pid();
344 read_pageframe(pid, pf, process_list, opts);