2 * Copyright (C) 2010 Timo Kokkonen <kaapeli@itanic.dy.fi>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #include <sys/types.h>
31 static struct maps_list *alloc_maplist(void)
33 struct maps_list *map;
35 map = malloc(sizeof *map);
39 memset(map, 0, sizeof(*map));
40 INIT_LIST_HEAD(&map->list);
45 static struct maps *alloc_map(void)
49 map = malloc(sizeof *map);
53 memset(map, 0, sizeof(*map));
54 INIT_LIST_HEAD(&map->list);
59 static struct maps *parse_maps(FILE *file, int pid, int tid)
61 struct maps *the_map = NULL;
65 while (fgets(line, sizeof(line), file)) {
66 struct maps *map = alloc_map();
67 unsigned long start, end;
76 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
80 printf("Error reading input: %s\n", line);
86 map->size = end - start;
91 strncpy(map->name, name, sizeof(map->name));
93 list_add_tail(&map->list, &the_map->list);
99 static struct pageframe *alloc_pageframe(void)
101 struct pageframe *pageframe;
103 pageframe = malloc(sizeof *pageframe);
104 if (pageframe == NULL)
107 clear_pageframe(pageframe);
112 static int compare_pageframe(struct bintree *at, struct bintree *bt)
114 struct pageframe *a, *b;
115 a = tree_to_pageframe(at);
116 b = tree_to_pageframe(bt);
118 return a->pf - b->pf;
121 struct bintree_ops pageframe_ops = {
122 .compare = compare_pageframe,
125 static int pid_is_match(int pidn, struct list_head *pidlist)
129 list_for_each_entry(pid, pidlist, list) {
130 if (pid->pid == pidn)
137 static int should_scan_process(struct parse_opts *opts, struct process *process)
141 if (is_parse_option(opts, PARSE_PID) &&
142 is_parse_option(opts, PARSE_MAP_NAME)) {
143 if (pid_is_match(process->pid, &opts->pidlist))
146 if (is_parse_option(opts, PARSE_PID)) {
147 if (pid_is_match(process->pid, &opts->pidlist))
151 if (is_parse_option(opts, PARSE_MAP_NAME))
155 if (is_parse_option(opts, PARSE_NOADD_TREE))
161 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
165 if (is_parse_option(opts, PARSE_MAP_NAME)) {
166 if (strstr(map->name, opts->name))
169 if (is_parse_option(opts, PARSE_NOADD_TREE))
177 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
180 if (is_parse_option(opts, PARSE_NOADD_TREE))
186 /* Read data from the /proc/pid/pagemap file */
187 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
188 struct maps *maps, struct parse_opts *opts)
191 struct maps_list *tmp;
192 struct pageframe *match, *pageframe = NULL;
194 unsigned long long pf[10240];
200 /* Go through the list of allocated memory areas */
201 list_for_each_entry(map, &maps->list, list) {
202 start = map->start >> (PAGE_SHIFT - 3);
203 len = map->size >> (PAGE_SHIFT);
205 if (!should_scan_mapping(opts, map))
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->pf = (pf[ret / sizeof(pf[0])]);
235 /* ignore unused pages */
236 if (!(page_swapped(pageframe) ||
237 page_present(pageframe)))
240 if (should_add_to_tree(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
263 tmp = alloc_maplist();
265 list_add(&tmp->list, &match->ml);
267 if (page_present(match))
268 map->pages_present++;
269 else if (page_swapped(match))
270 map->pages_swapped++;
277 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
278 struct process *process_list, struct parse_opts *opts)
281 struct process *process;
282 struct pidlist *pidl, *n;
286 process = malloc(sizeof(*process));
287 memset(process, 0, sizeof(*process));
288 INIT_LIST_HEAD(&process->list);
293 if (!should_scan_process(opts, process))
296 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
297 file = fopen(path, "rb");
302 maps = parse_maps(file, pid, tid);
304 process->maps = maps;
306 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
307 file = fopen(path, "rb");
312 parse_pageframe(file, pageframe, maps, opts);
315 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
319 list_for_each_entry(maps, &process->maps->list, list) {
320 process->pages_present += maps->pages_present;
321 process->pages_swapped += maps->pages_swapped;
325 if (!is_parse_option(opts, PARSE_NOADD_TREE))
326 process->is_initial_pid = 1;
328 list_add_tail(&process->list, &process_list->list);
335 * Remove the pid from the list. It is no longer an
336 * interesting pid, since we can't access its data
338 list_for_each_entry_safe(pidl, n, &opts->pidlist, list) {
339 if (pidl->pid == pid) {
340 list_del(&pidl->list);
349 static int read_pageframe_with_threads(int pid,
350 struct pageframe *pageframe,
351 struct process *process_list,
352 struct parse_opts *opts)
359 if (opts->with_threads)
360 tid = get_next_tid(pid, &dir);
367 count += read_pageframe(pid, tid, pageframe, process_list,
370 if (!opts->with_threads)
377 int scan_all_pids(struct pageframe *pf, struct process *process_list,
378 struct parse_opts *opts)
380 struct pidlist *pidlist, *n;
385 if (is_parse_option(opts, PARSE_PID)) {
386 list_for_each_entry_safe(pidlist, n, &opts->pidlist, list) {
387 count += read_pageframe_with_threads(pidlist->pid, pf,
392 if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
393 printf("Failed to find any matching processes "
394 "with given arguments\n");
398 if (is_parse_option(opts, PARSE_DUMP))
401 if (is_parse_option(opts, PARSE_MAP_NAME) &&
402 !is_parse_option(opts, PARSE_PID)) {
404 pid = get_next_pid(&dir);
407 read_pageframe_with_threads(pid, pf, process_list,
411 /* Do not add new pages in the tree after the initial scan */
412 opts->parse_mask |= PARSE_NOADD_TREE;
415 pid = get_next_pid(&dir);
418 read_pageframe_with_threads(pid, pf, process_list, opts);
424 struct kpageflag_data {
425 struct bintree_ops ops;
431 #define bintree_ops_to_kpfd(bintree_ops) \
432 container_of((bintree_ops), struct kpageflag_data, ops)
434 static void _update_kpageflags(struct bintree *bt, struct bintree_ops *ops)
436 struct pageframe *pf = tree_to_pageframe(bt);
437 struct kpageflag_data *kpfd = bintree_ops_to_kpfd(ops);
439 long int pfnn = pfn(pf) * sizeof(pf->kpageflags);
441 ret = lseek(kpfd->kpageflags_fd, pfnn, SEEK_SET);
444 fprintf(stderr, "Error seeking to %lx: %s\n",
445 pfnn, strerror(error));
449 ret = read(kpfd->kpageflags_fd, &pf->kpageflags,
450 sizeof(pf->kpageflags));
453 fprintf(stderr, "Error reading from %llx: %s\n",
454 pf->pf * sizeof(pf->kpageflags),
459 ret = lseek(kpfd->kpagecount_fd, pfnn, SEEK_SET);
462 fprintf(stderr, "Error seeking to %lx: %s\n",
463 pfnn, strerror(error));
467 ret = read(kpfd->kpagecount_fd, &pf->kpagecount,
468 sizeof(pf->kpagecount));
471 fprintf(stderr, "Error reading from %llx: %s\n",
472 pf->pf * sizeof(pf->kpagecount),
478 int update_kpageflags(struct pageframe *pf)
480 struct kpageflag_data kpfd = {
482 .callback = _update_kpageflags,
484 .kpageflags_fd = open("/proc/kpageflags", O_RDONLY),
485 .kpagecount_fd = open("/proc/kpagecount", O_RDONLY),
488 if (kpfd.kpageflags_fd == -1 || kpfd.kpagecount_fd == -1)
491 bintree_walk(&pf->tree, &kpfd.ops);
493 close(kpfd.kpageflags_fd);
494 close(kpfd.kpagecount_fd);