From: Timo Kokkonen Date: Fri, 23 Jul 2010 12:38:59 +0000 (+0300) Subject: Break parsing if no processes are found X-Git-Url: http://git.itanic.dy.fi/?p=scan-pagemap;a=commitdiff_plain;h=be25768cb39c9af0646eb2bd893af9d4f95ec372 Break parsing if no processes are found 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 --- diff --git a/main.c b/main.c index 0115453..3e88ecd 100644 --- a/main.c +++ b/main.c @@ -80,7 +80,9 @@ int main(int argc, char *argv[]) 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); diff --git a/parse.c b/parse.c index 78311d9..c35fbe1 100644 --- a/parse.c +++ b/parse.c @@ -279,8 +279,8 @@ static int parse_pageframe(FILE *file, struct pageframe *pf_tree, 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; @@ -303,7 +303,7 @@ void read_pageframe(int pid, int tid, struct pageframe *pageframe, file = fopen(path, "rb"); if (!file) - return; + return 0; 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) - return; + return 0; parse_pageframe(file, pageframe, maps, opts); fclose(file); if (read_cmdline(pid, tid, process->name, sizeof(process->name))) - return; + return 1; 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; } - return; + return 1; } static int parse_pid(DIR **dir) @@ -419,13 +419,14 @@ static int get_next_pid_by_name(DIR **dir, char *name) 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; + int count = 0; while (1) { if (opts->with_threads) @@ -434,31 +435,43 @@ static void read_pageframe_with_threads(int pid, 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; } + + 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; + int count = 0; 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) - 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); @@ -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); } + + return 0; } diff --git a/parse.h b/parse.h index 962eb58..af90435 100644 --- a/parse.h +++ b/parse.h @@ -3,7 +3,7 @@ #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