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 = calloc(sizeof *map, 1);
39 INIT_LIST_HEAD(&map->list);
44 static struct maps *alloc_map(void)
48 map = calloc(sizeof(*map), 1);
52 memset(map, 0, sizeof(*map));
53 INIT_LIST_HEAD(&map->list);
58 static struct maps *parse_maps(FILE *file, int pid, int tid)
60 struct maps *the_map = NULL;
64 while (fgets(line, sizeof(line), file)) {
65 struct maps *map = alloc_map();
66 unsigned long start, end;
75 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
79 printf("Error reading input: %s\n", line);
85 map->size = end - start;
90 strncpy(map->name, name, sizeof(map->name));
92 list_add_tail(&map->list, &the_map->list);
98 static struct pageframe *alloc_pageframe(void)
100 struct pageframe *pageframe;
102 pageframe = malloc(sizeof *pageframe);
103 if (pageframe == NULL)
106 clear_pageframe(pageframe);
111 static int compare_pageframe(struct bintree *at, struct bintree *bt)
113 struct pageframe *a, *b;
114 a = tree_to_pageframe(at);
115 b = tree_to_pageframe(bt);
117 return a->pf - b->pf;
120 struct bintree_ops pageframe_ops = {
121 .compare = compare_pageframe,
124 static int pid_is_match(int pidn, struct list_head *pidlist)
128 list_for_each_entry(pid, pidlist, list) {
129 if (pid->pid == pidn)
136 static int should_scan_process(struct parse_opts *opts, struct process *process)
140 if (is_parse_option(opts, PARSE_PID) &&
141 is_parse_option(opts, PARSE_MAP_NAME)) {
142 if (pid_is_match(process->pid, &opts->pidlist))
145 if (is_parse_option(opts, PARSE_PID)) {
146 if (pid_is_match(process->pid, &opts->pidlist))
150 if (is_parse_option(opts, PARSE_MAP_NAME))
154 if (is_parse_option(opts, PARSE_NOADD_TREE))
160 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
164 if (is_parse_option(opts, PARSE_MAP_NAME)) {
165 if (strstr(map->name, opts->name))
168 if (is_parse_option(opts, PARSE_NOADD_TREE))
176 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
179 if (is_parse_option(opts, PARSE_NOADD_TREE))
185 /* Read data from the /proc/pid/pagemap file */
186 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
187 struct maps *maps, struct parse_opts *opts)
190 struct maps_list *tmp;
191 struct pageframe *match, *pageframe = NULL;
193 unsigned long long pf[10240];
199 /* Go through the list of allocated memory areas */
200 list_for_each_entry(map, &maps->list, list) {
201 start = map->start >> (PAGE_SHIFT - 3);
202 len = map->size >> (PAGE_SHIFT);
204 if (!should_scan_mapping(opts, map))
207 ret = fseek(file, start, SEEK_SET);
210 fprintf(stderr, "Error seeking to %lx: %s\n", start,
215 for (i = 0; i < len; i++) {
218 MIN(sizeof(pf), (len - i) * 8), file);
225 pageframe = alloc_pageframe();
226 ret -= sizeof(pf[0]);
228 /* ignore unused pages */
229 if (!pf[ret / sizeof(pf[0])])
232 pageframe->pf = (pf[ret / sizeof(pf[0])]);
234 /* ignore unused pages */
235 if (!(page_swapped(pageframe) ||
236 page_present(pageframe)))
239 if (should_add_to_tree(opts, pageframe, map)) {
240 match = tree_to_pageframe(
241 bintree_add(&pf_tree->tree,
245 match = tree_to_pageframe(
246 bintree_find(&pf_tree->tree,
254 if (match == pageframe)
259 * Add a link from the physical page to this
262 tmp = alloc_maplist();
264 list_add(&tmp->list, &match->ml);
266 if (page_present(match))
267 map->pages_present++;
268 else if (page_swapped(match))
269 map->pages_swapped++;
276 static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
277 struct process *process_list, struct parse_opts *opts)
280 struct process *process;
281 struct pidlist *pidl, *n;
285 process = calloc(sizeof(*process), 1);
286 INIT_LIST_HEAD(&process->list);
291 if (!should_scan_process(opts, process))
294 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
295 file = fopen(path, "rb");
300 maps = parse_maps(file, pid, tid);
302 process->maps = maps;
304 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
305 file = fopen(path, "rb");
310 parse_pageframe(file, pageframe, maps, opts);
313 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
317 list_for_each_entry(maps, &process->maps->list, list) {
318 process->pages_present += maps->pages_present;
319 process->pages_swapped += maps->pages_swapped;
323 if (!is_parse_option(opts, PARSE_NOADD_TREE))
324 process->is_initial_pid = 1;
326 list_add_tail(&process->list, &process_list->list);
333 * Remove the pid from the list. It is no longer an
334 * interesting pid, since we can't access its data
336 list_for_each_entry_safe(pidl, n, &opts->pidlist, list) {
337 if (pidl->pid == pid) {
338 list_del(&pidl->list);
347 static int read_pageframe_with_threads(int pid,
348 struct pageframe *pageframe,
349 struct process *process_list,
350 struct parse_opts *opts)
357 if (opts->with_threads)
358 tid = get_next_tid(pid, &dir);
365 count += read_pageframe(pid, tid, pageframe, process_list,
368 if (!opts->with_threads)
375 int scan_all_pids(struct pageframe *pf, struct process *process_list,
376 struct parse_opts *opts)
378 struct pidlist *pidlist, *n;
383 if (is_parse_option(opts, PARSE_PID)) {
384 list_for_each_entry_safe(pidlist, n, &opts->pidlist, list) {
385 count += read_pageframe_with_threads(pidlist->pid, pf,
390 if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
391 printf("Failed to find any matching processes "
392 "with given arguments\n");
396 if (is_parse_option(opts, PARSE_DUMP))
399 if (is_parse_option(opts, PARSE_MAP_NAME) &&
400 !is_parse_option(opts, PARSE_PID)) {
402 pid = get_next_pid(&dir);
405 read_pageframe_with_threads(pid, pf, process_list,
409 /* Do not add new pages in the tree after the initial scan */
410 opts->parse_mask |= PARSE_NOADD_TREE;
413 pid = get_next_pid(&dir);
416 read_pageframe_with_threads(pid, pf, process_list, opts);
422 struct kpageflag_data {
423 struct bintree_ops ops;
429 #define bintree_ops_to_kpfd(bintree_ops) \
430 container_of((bintree_ops), struct kpageflag_data, ops)
432 static void _update_kpageflags(struct bintree *bt, struct bintree_ops *ops)
434 struct pageframe *pf = tree_to_pageframe(bt);
435 struct kpageflag_data *kpfd = bintree_ops_to_kpfd(ops);
437 long int pfnn = pfn(pf) * sizeof(pf->kpageflags);
439 ret = lseek(kpfd->kpageflags_fd, pfnn, SEEK_SET);
442 fprintf(stderr, "Error seeking to %lx: %s\n",
443 pfnn, strerror(error));
447 ret = read(kpfd->kpageflags_fd, &pf->kpageflags,
448 sizeof(pf->kpageflags));
451 fprintf(stderr, "Error reading from %llx: %s\n",
452 pf->pf * sizeof(pf->kpageflags),
457 ret = lseek(kpfd->kpagecount_fd, pfnn, SEEK_SET);
460 fprintf(stderr, "Error seeking to %lx: %s\n",
461 pfnn, strerror(error));
465 ret = read(kpfd->kpagecount_fd, &pf->kpagecount,
466 sizeof(pf->kpagecount));
469 fprintf(stderr, "Error reading from %llx: %s\n",
470 pf->pf * sizeof(pf->kpagecount),
476 int update_kpageflags(struct pageframe *pf)
478 struct kpageflag_data kpfd = {
480 .callback = _update_kpageflags,
482 .kpageflags_fd = open("/proc/kpageflags", O_RDONLY),
483 .kpagecount_fd = open("/proc/kpagecount", O_RDONLY),
486 if (kpfd.kpageflags_fd == -1 || kpfd.kpagecount_fd == -1)
489 bintree_walk(&pf->tree, &kpfd.ops);
491 close(kpfd.kpageflags_fd);
492 close(kpfd.kpagecount_fd);