]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
Make it possible to scan only interesting pids
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 4 Jul 2010 12:58:57 +0000 (15:58 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sun, 4 Jul 2010 12:58:57 +0000 (15:58 +0300)
When scanning "non-interesting" pids, the pageframes are not populated
in the tree. This makes it possible to reduce memory consumption
significantly by discarding uninterested data.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
main.c
parse.c
parse.h

diff --git a/main.c b/main.c
index c1b484cdd8128a0ad03bd242b3d67716a8e83599..078fccac275ef07a510032495f264758153f927a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -19,7 +19,7 @@ int main(int argc, char *argv[])
        pid = atoi(argv[1]);
 
        memset(&pf, 0, sizeof(pf));
-       scan_all_pids(&pf);
+       scan_all_pids(&pf, pid);
        print_page_stats(&pf);
 
        return 0;
diff --git a/parse.c b/parse.c
index 32660e2007e5217d02536b0bce937c075582117d..0eecb20a81bd92720a7ec191fe480bc9878b7ec5 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -127,11 +127,11 @@ struct bintree_ops pageframe_ops = {
 
 /* Read data from the /proc/pid/pagemap file */
 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
-                       struct maps *maps)
+                       struct maps *maps, int add_to_tree)
 {
        struct maps *map;
        struct maps_list *tmp;
-       struct pageframe *match, *pageframe;
+       struct pageframe *match, *pageframe = NULL;
        long start, len, i;
        unsigned long long pf;
        int ret, error;
@@ -163,14 +163,29 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
                                continue;
                                return -1;
                        }
-                       pageframe = alloc_pageframe();
+                       if (!pageframe)
+                               pageframe = alloc_pageframe();
+
                        pageframe_to_struct(pf, pageframe);
-                       match = tree_to_pageframe(
-                               bintree_add(&pf_tree->tree,
-                                       &pageframe->tree, &pageframe_ops));
 
-                       if (match != pageframe)
-                               free(pageframe);
+                       if (add_to_tree) {
+                               match = tree_to_pageframe(
+                                       bintree_add(&pf_tree->tree,
+                                               &pageframe->tree,
+                                               &pageframe_ops));
+                       } else {
+                               match = tree_to_pageframe(
+                                       bintree_find(&pf_tree->tree,
+                                               &pageframe->tree,
+                                               &pageframe_ops));
+                       }
+
+                       if (match == NULL)
+                               continue;
+
+                       if (match == pageframe)
+                               pageframe = NULL;
+
                        match->refcount++;
                        /*
                         * Add a link from the physical page to this
@@ -190,7 +205,7 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
        return 0;
 }
 
-void read_pageframe(int pid, struct pageframe *pageframe)
+void read_pageframe(int pid, struct pageframe *pageframe, int add_to_tree)
 {
        struct maps *maps;
        FILE *file;
@@ -211,7 +226,7 @@ void read_pageframe(int pid, struct pageframe *pageframe)
        if (!file)
                goto err_out;
 
-       parse_pageframe(file, pageframe, maps);
+       parse_pageframe(file, pageframe, maps, add_to_tree);
 
        return;
 err_out:
@@ -257,13 +272,16 @@ restart:
        return atoi(dirent->d_name);
 }
 
-void scan_all_pids(struct pageframe *pf)
+void scan_all_pids(struct pageframe *pf, int interesting_pid)
 {
        int pid;
+
+       read_pageframe(interesting_pid, pf, 1);
+
        while(1) {
                pid = get_next_pid();
                if (pid <= 0)
                        break;
-               read_pageframe(pid, pf);
+               read_pageframe(pid, pf, 0);
        }
 }
diff --git a/parse.h b/parse.h
index b3092d2f17c06afbea26714a5e27b82608b0a5dd..44842200da9b207bf5d8a5a4f9678a7c48de07c8 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -3,7 +3,6 @@
 
 #include "pagemap.h"
 
-void read_pageframe(int pid, struct pageframe *pageframe);
-void scan_all_pids(struct pageframe *pf);
+void scan_all_pids(struct pageframe *pf, int interesting_pid);
 
 #endif