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>
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);
-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;
file = fopen(path, "rb");
if (!file)
file = fopen(path, "rb");
if (!file)
maps = parse_maps(file, pid, tid);
fclose(file);
maps = parse_maps(file, pid, tid);
fclose(file);
file = fopen(path, "rb");
if (!file)
file = fopen(path, "rb");
if (!file)
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)))
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;
}
}
static int parse_pid(DIR **dir)
}
static int parse_pid(DIR **dir)
-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;
while (1) {
if (opts->with_threads)
while (1) {
if (opts->with_threads)
- 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;
}
-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;
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);
break;
read_pageframe_with_threads(pid, pf, process_list, opts);
}
break;
read_pageframe_with_threads(pid, pf, process_list, opts);
}
-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