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>
29 static struct maps_list *alloc_maplist(void)
31 struct maps_list *map;
33 map = calloc(sizeof *map, 1);
37 INIT_LIST_HEAD(&map->list);
42 static struct maps *alloc_map(void)
46 map = calloc(sizeof(*map), 1);
50 INIT_LIST_HEAD(&map->list);
55 static struct maps *parse_maps(FILE *file, int pid, int tid)
57 struct maps *the_map = NULL;
61 while (fgets(line, sizeof(line), file)) {
62 struct maps *map = alloc_map();
63 unsigned long start, end;
72 ret = sscanf(line, "%lx-%lx %*s %*s %*s %*s %s",
76 printf("Error reading input: %s\n", line);
82 map->size = end - start;
87 strncpy(map->name, name, sizeof(map->name));
89 list_add_tail(&map->list, &the_map->list);
95 static struct pageframe *alloc_pageframe(void)
97 struct pageframe *pageframe;
99 pageframe = malloc(sizeof *pageframe);
100 if (pageframe == NULL)
103 clear_pageframe(pageframe);
108 static int pid_is_match(int pidn, struct list_head *pidlist)
112 list_for_each_entry(pid, pidlist, list) {
113 if (pid->pid == pidn)
120 static int should_scan_process(struct parse_opts *opts, struct process *process)
124 if (is_parse_option(opts, PARSE_PID) &&
125 is_parse_option(opts, PARSE_MAP_NAME)) {
126 if (pid_is_match(process->pid, &opts->pidlist))
129 if (is_parse_option(opts, PARSE_PID)) {
130 if (pid_is_match(process->pid, &opts->pidlist))
134 if (is_parse_option(opts, PARSE_MAP_NAME))
138 if (is_parse_option(opts, PARSE_NOADD_TREE))
144 static int should_scan_mapping(struct parse_opts *opts, struct maps *map)
148 if (is_parse_option(opts, PARSE_MAP_NAME)) {
149 if (strstr(map->name, opts->name))
152 if (is_parse_option(opts, PARSE_NOADD_TREE))
160 static int should_add_to_tree(struct parse_opts *opts, struct pageframe *pf,
163 if (is_parse_option(opts, PARSE_NOADD_TREE))
169 /* Read data from the /proc/pid/pagemap file */
170 static int parse_pageframe(FILE *file, struct rb_root *root,
171 struct maps *maps, struct parse_opts *opts)
174 struct maps_list *tmp;
175 struct pageframe *match, *pageframe = NULL;
177 unsigned long long pf[10240];
183 /* Go through the list of allocated memory areas */
184 list_for_each_entry(map, &maps->list, list) {
185 start = map->start >> (PAGE_SHIFT - 3);
186 len = map->size >> (PAGE_SHIFT);
188 if (!should_scan_mapping(opts, map))
191 ret = fseek(file, start, SEEK_SET);
193 fprintf(stderr, "Error seeking to %lx: %m\n", start);
197 for (i = 0; i < len; i++) {
200 MIN(sizeof(pf), (len - i) * 8), file);
206 pageframe = alloc_pageframe();
207 ret -= sizeof(pf[0]);
209 /* ignore unused pages */
210 if (!pf[ret / sizeof(pf[0])])
213 pageframe->pf = (pf[ret / sizeof(pf[0])]);
215 /* ignore unused pages */
216 if (!(page_swapped(pageframe) ||
217 page_present(pageframe)))
220 if (should_add_to_tree(opts, pageframe, map))
221 match = pf_insert(root, pageframe);
223 match = pf_search(root, pageframe);
228 if (match == pageframe)
233 * Add a link from the physical page to this
236 tmp = alloc_maplist();
238 list_add(&tmp->list, &match->ml);
240 if (page_present(match))
241 map->pages_present++;
242 else if (page_swapped(match))
243 map->pages_swapped++;
250 static int read_pageframe(int pid, int tid, struct rb_root *root,
251 struct process *process_list, struct parse_opts *opts)
254 struct process *process;
255 struct pidlist *pidl, *n;
259 process = calloc(sizeof(*process), 1);
260 INIT_LIST_HEAD(&process->list);
265 if (!should_scan_process(opts, process))
268 snprintf(path, sizeof(path), "/proc/%d/task/%d/maps", pid, tid);
269 file = fopen(path, "rb");
274 maps = parse_maps(file, pid, tid);
276 process->maps = maps;
278 snprintf(path, sizeof(path), "/proc/%d/task/%d/pagemap", pid, tid);
279 file = fopen(path, "rb");
284 parse_pageframe(file, root, maps, opts);
287 if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
290 if (!is_parse_option(opts, PARSE_NOADD_TREE))
291 process->is_initial_pid = 1;
293 list_add_tail(&process->list, &process_list->list);
300 * Remove the pid from the list. It is no longer an
301 * interesting pid, since we can't access its data
303 list_for_each_entry_safe(pidl, n, &opts->pidlist, list) {
304 if (pidl->pid == pid) {
305 list_del(&pidl->list);
314 static int read_pageframe_with_threads(int pid,
315 struct rb_root *root,
316 struct process *process_list,
317 struct parse_opts *opts)
324 if (opts->with_threads)
325 tid = get_next_tid(pid, &dir);
332 count += read_pageframe(pid, tid, root, process_list, opts);
334 if (!opts->with_threads)
341 int scan_all_pids(struct rb_root *root, struct process *process_list,
342 struct parse_opts *opts)
344 struct pidlist *pidlist, *n;
350 if (is_parse_option(opts, PARSE_PID)) {
351 list_for_each_entry_safe(pidlist, n, &opts->pidlist, list) {
352 count += read_pageframe_with_threads(pidlist->pid, root,
357 if ((count == 0) && !(is_parse_option(opts, PARSE_MAP_NAME))) {
358 printf("Failed to find any matching processes "
359 "with given arguments\n");
363 if (is_parse_option(opts, PARSE_DUMP))
366 if (is_parse_option(opts, PARSE_MAP_NAME) &&
367 !is_parse_option(opts, PARSE_PID)) {
368 printf("Scanning page mappings for process: ");
370 pid = get_next_pid(&dir);
374 for (i = 0; i < len; i++)
376 len = printf("% 5d", pid);
379 read_pageframe_with_threads(pid, root, process_list,
383 for (i = 0; i < len; i++)
388 /* Do not add new pages in the tree after the initial scan */
389 opts->parse_mask |= PARSE_NOADD_TREE;
391 printf("Scanning page mappings for process: ");
393 pid = get_next_pid(&dir);
397 for (i = 0; i < len; i++)
399 len = printf("% 5d", pid);
402 read_pageframe_with_threads(pid, root, process_list, opts);
404 for (i = 0; i < len; i++)
411 int update_kpageflags(struct rb_root *root)
413 struct pageframe *pf;
418 kpageflags_fd = open("/proc/kpageflags", O_RDONLY);
419 if (kpageflags_fd < 0)
422 kpagecount_fd = open("/proc/kpagecount", O_RDONLY);
423 if (kpagecount_fd < 0)
426 pf = rb_to_pageframe(rb_first(root));
429 long int pfnn = pfn(pf) * sizeof(pf->kpageflags);
431 ret = lseek(kpageflags_fd, pfnn, SEEK_SET);
433 fprintf(stderr, "Error seeking to %lx: %m\n", pfnn);
437 ret = read(kpageflags_fd, &pf->kpageflags,
438 sizeof(pf->kpageflags));
440 fprintf(stderr, "Error reading from %llx: %m\n",
441 pf->pf * sizeof(pf->kpageflags));
445 ret = lseek(kpagecount_fd, pfnn, SEEK_SET);
447 fprintf(stderr, "Error seeking to %lx: %m\n", pfnn);
451 ret = read(kpagecount_fd, &pf->kpagecount,
452 sizeof(pf->kpagecount));
454 fprintf(stderr, "Error reading from %llx: %m\n",
455 pf->pf * sizeof(pf->kpagecount));
459 pf = rb_to_pageframe(rb_next(&pf->tree));
462 close(kpageflags_fd);
463 close(kpagecount_fd);