]> git.itanic.dy.fi Git - scan-pagemap/commitdiff
Break parsing if no processes are found
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 23 Jul 2010 12:38:59 +0000 (15:38 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 23 Jul 2010 12:38:59 +0000 (15:38 +0300)
Scanning through all pids in the system can be a time consuming task,
especially with slower CPUs. Therefore, if the user gives a
nonexistent process argument it can tage significant amount of time
until the user gets the response that there will be no useful results
available.

This patch counts the number of processes that have been encoutered
during the prescan. If the count is zero, then it is no good to
proceed scanning all of the rest processes. As an extra benefit, the
number of prescanned processes can be used for something useful..

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

diff --git a/main.c b/main.c
index 0115453bf98421c5357570e5bf2207eb4e06bc17..3e88ecd9612c948ac043ebe36fa8172c1e6b12e8 100644 (file)
--- a/main.c
+++ b/main.c
@@ -80,7 +80,9 @@ int main(int argc, char *argv[])
 
        memset(&pf, 0, sizeof(pf));
 
 
        memset(&pf, 0, sizeof(pf));
 
-       scan_all_pids(&pf, &process_list, &opts);
+       if (scan_all_pids(&pf, &process_list, &opts))
+               return 1;
+
        print_pid_stats(&pf, process_list, &opts);
        print_page_stats(&pf);
 
        print_pid_stats(&pf, process_list, &opts);
        print_page_stats(&pf);
 
diff --git a/parse.c b/parse.c
index 78311d9844cd0faa7553c59588445912631c35af..c35fbe19bafb6f6c3548f31ba045f23a9182fb5d 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -279,8 +279,8 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree,
        return 0;
 }
 
        return 0;
 }
 
-void read_pageframe(int pid, int tid, struct pageframe *pageframe,
-               struct process **process_list, struct parse_opts *opts)
+static int read_pageframe(int pid, int tid, struct pageframe *pageframe,
+                       struct process **process_list, struct parse_opts *opts)
 {
        struct maps *maps;
        struct process *process;
 {
        struct maps *maps;
        struct process *process;
@@ -303,7 +303,7 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe,
        file = fopen(path, "rb");
 
        if (!file)
        file = fopen(path, "rb");
 
        if (!file)
-               return;
+               return 0;
 
        maps = parse_maps(file, pid, tid);
        fclose(file);
 
        maps = parse_maps(file, pid, tid);
        fclose(file);
@@ -313,23 +313,23 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe,
        file = fopen(path, "rb");
 
        if (!file)
        file = fopen(path, "rb");
 
        if (!file)
-               return;
+               return 0;
 
        parse_pageframe(file, pageframe, maps, opts);
        fclose(file);
 
        if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
 
        parse_pageframe(file, pageframe, maps, opts);
        fclose(file);
 
        if (read_cmdline(pid, tid, process->name, sizeof(process->name)))
-               return;
+               return 1;
 
        if (maps == NULL)
 
        if (maps == NULL)
-               return;
+               return 1;
 
        list_for_each_entry(maps, &process->maps->list, list) {
                process->pages_present += maps->pages_present;
                process->pages_swapped += maps->pages_swapped;
        }
 
 
        list_for_each_entry(maps, &process->maps->list, list) {
                process->pages_present += maps->pages_present;
                process->pages_swapped += maps->pages_swapped;
        }
 
-       return;
+       return 1;
 }
 
 static int parse_pid(DIR **dir)
 }
 
 static int parse_pid(DIR **dir)
@@ -419,13 +419,14 @@ static int get_next_pid_by_name(DIR **dir, char *name)
        return 0;
 }
 
        return 0;
 }
 
-static void read_pageframe_with_threads(int pid,
-                                       struct pageframe *pageframe,
-                                       struct process **process_list,
-                                       struct parse_opts *opts)
+static int read_pageframe_with_threads(int pid,
+                               struct pageframe *pageframe,
+                               struct process **process_list,
+                               struct parse_opts *opts)
 {
        DIR *dir = NULL;
        int tid;
 {
        DIR *dir = NULL;
        int tid;
+       int count = 0;
 
        while (1) {
                if (opts->with_threads)
 
        while (1) {
                if (opts->with_threads)
@@ -434,31 +435,43 @@ static void read_pageframe_with_threads(int pid,
                        tid = pid;
 
                if (tid <= 0)
                        tid = pid;
 
                if (tid <= 0)
-                       return;
+                       return count;
 
 
-               read_pageframe(pid, tid, pageframe, process_list, opts);
+               count += read_pageframe(pid, tid, pageframe, process_list,
+                                       opts);
 
                if (!opts->with_threads)
                        break;
        }
 
                if (!opts->with_threads)
                        break;
        }
+
+       return count;
 }
 
 }
 
-void scan_all_pids(struct pageframe *pf, struct process **process_list,
+int scan_all_pids(struct pageframe *pf, struct process **process_list,
                struct parse_opts *opts)
 {
        DIR *dir = NULL;
        int pid;
                struct parse_opts *opts)
 {
        DIR *dir = NULL;
        int pid;
+       int count = 0;
 
        if (opts->parse_mask & PARSE_PROCESS_NAME) {
                while ((pid = get_next_pid_by_name(&dir, opts->name))) {
 
        if (opts->parse_mask & PARSE_PROCESS_NAME) {
                while ((pid = get_next_pid_by_name(&dir, opts->name))) {
-                       read_pageframe_with_threads(pid, pf, process_list,
-                                               opts);
+                       count += read_pageframe_with_threads(pid, pf,
+                                                       process_list,
+                                                       opts);
                }
                dir = NULL;
        }
 
        if (opts->parse_mask & PARSE_PID)
                }
                dir = NULL;
        }
 
        if (opts->parse_mask & PARSE_PID)
-               read_pageframe_with_threads(opts->pid, pf, process_list, opts);
+               count = read_pageframe_with_threads(opts->pid, pf, process_list,
+                                               opts);
+
+       if ((count == 0) && !(opts->parse_mask & PARSE_MAP_NAME)) {
+               printf("Failed to find any matching processes "
+                       "with given arguments\n");
+               return -1;
+       }
 
        while (1) {
                pid = get_next_pid(&dir);
 
        while (1) {
                pid = get_next_pid(&dir);
@@ -466,4 +479,6 @@ void scan_all_pids(struct pageframe *pf, struct process **process_list,
                        break;
                read_pageframe_with_threads(pid, pf, process_list, opts);
        }
                        break;
                read_pageframe_with_threads(pid, pf, process_list, opts);
        }
+
+       return 0;
 }
 }
diff --git a/parse.h b/parse.h
index 962eb586b573d8748fb4d6bcfe9fcd818c0f960b..af904355f051c1f1f097503c61c54548857fa70c 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -3,7 +3,7 @@
 
 #include "pagemap.h"
 
 
 #include "pagemap.h"
 
-void scan_all_pids(struct pageframe *pf, struct process **process_list,
+int scan_all_pids(struct pageframe *pf, struct process **process_list,
                struct parse_opts *opts);
 
 #endif
                struct parse_opts *opts);
 
 #endif