]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
Store parse options in a structure
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 14 Jul 2010 08:23:14 +0000 (11:23 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Wed, 14 Jul 2010 08:23:14 +0000 (11:23 +0300)
This makes it much easier to decide what kind of data to parse

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

diff --git a/main.c b/main.c
index 2bd223a66b9a4c6780125eb2810ba99e2ec5b73c..45d479677d127fa86a0231833e23095ddcb1ce6b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -10,6 +10,7 @@ int main(int argc, char *argv[])
 {
        struct pageframe pf;
        struct process *process_list = NULL;
+       struct parse_opts opts;
        int pid;
 
        if (argc < 2) {
@@ -20,7 +21,10 @@ int main(int argc, char *argv[])
        pid = atoi(argv[1]);
 
        memset(&pf, 0, sizeof(pf));
-       scan_all_pids(&pf, &process_list, pid);
+       opts.parse_mask = PARSE_PID;
+       opts.pid = pid;
+
+       scan_all_pids(&pf, &process_list, &opts);
        print_pid_stats(&pf, process_list);
        print_page_stats(&pf);
 
index b38ff8c048c8aafdd7ee43d724c7eae59380f529..414f4fecd55ad27f36b54d66f58a622eb11e9f6c 100644 (file)
--- a/pagemap.h
+++ b/pagemap.h
@@ -59,4 +59,13 @@ struct process {
        long int pages_swapped;
 };
 
+#define PARSE_PID      0x1
+#define PARSE_MAP_NAME 0x2
+
+struct parse_opts {
+       int parse_mask;
+       int pid;
+       char *map_name;
+};
+
 #endif
diff --git a/parse.c b/parse.c
index b2739ef55deef5b2f1acae1f29f5ee65100af8b2..c4a0210b2d91de60933a02afd64dc1e097c0a232 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -97,6 +97,7 @@ err:
 
 static void pageframe_to_struct(unsigned long long p, struct pageframe *pf)
 {
+       /* Refer Documentation/vm/pagemap.txt for the format */
        pf->page_present = !!(BITRANGE(63, 63) & p);
        pf->page_swapped = !!(BITRANGE(62, 62) & p);
        pf->page_shift   =   (BITRANGE(55, 60) & p) >> 55;
@@ -122,9 +123,25 @@ struct bintree_ops pageframe_ops = {
        .compare = compare_pageframe,
 };
 
+static int check_parse_opts(struct parse_opts *opts, struct pageframe *pf,
+               struct maps *map)
+{
+       if (opts->parse_mask & PARSE_PID) {
+               if (opts->pid == map->pid)
+                       return 1;
+       }
+
+       if (opts->parse_mask & PARSE_MAP_NAME) {
+               if (!strcmp(opts->map_name, map->name))
+                       return 1;
+       }
+
+       return 0;
+}
+
 /* Read data from the /proc/pid/pagemap file */
 static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
-                       struct maps *maps, int add_to_tree)
+                       struct maps *maps, struct parse_opts *opts)
 {
        struct maps *map;
        struct maps_list *tmp;
@@ -167,7 +184,7 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
 
                        pageframe_to_struct(pf[ret / sizeof(pf[0])], pageframe);
 
-                       if (add_to_tree) {
+                       if (check_parse_opts(opts, pageframe, map)) {
                                match = tree_to_pageframe(
                                        bintree_add(&pf_tree->tree,
                                                &pageframe->tree,
@@ -211,7 +228,7 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
 }
 
 void read_pageframe(int pid, struct pageframe *pageframe,
-               struct process **process_list, int add_to_tree)
+               struct process **process_list, struct parse_opts *opts)
 {
        struct maps *maps;
        struct process *process;
@@ -246,7 +263,7 @@ void read_pageframe(int pid, struct pageframe *pageframe,
        if (!file)
                return;
 
-       parse_pageframe(file, pageframe, maps, add_to_tree);
+       parse_pageframe(file, pageframe, maps, opts);
        fclose(file);
 
        snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
@@ -307,16 +324,14 @@ restart:
 }
 
 void scan_all_pids(struct pageframe *pf, struct process **process_list,
-               int interesting_pid)
+               struct parse_opts *opts)
 {
        int pid;
 
-       read_pageframe(interesting_pid, pf, process_list, 1);
-
        while(1) {
                pid = get_next_pid();
                if (pid <= 0)
                        break;
-               read_pageframe(pid, pf, process_list, 0);
+               read_pageframe(pid, pf, process_list, opts);
        }
 }
diff --git a/parse.h b/parse.h
index 072d65d9c2bfbd2506edf05a8e3149d5db4a3eee..962eb586b573d8748fb4d6bcfe9fcd818c0f960b 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -4,6 +4,6 @@
 #include "pagemap.h"
 
 void scan_all_pids(struct pageframe *pf, struct process **process_list,
-               int interesting_pid);
+               struct parse_opts *opts);
 
 #endif