Commit 5e47e233 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by Cyrill Gorcunov

crtools: Cleanup threads stuff parsing

Move the whole code dealing with parsing threads at collect_pstree
stage into one function.

The nr_threads calculation is performed inside it based on the amount
of directories in /proc/pid/task, not the Threads: count in status.

Error from xrealloc is ignored by now.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent 04f36aed
...@@ -687,47 +687,34 @@ err: ...@@ -687,47 +687,34 @@ err:
return ret; return ret;
} }
static int parse_threads(pid_t pid, int nr_threads, u32 **threads) static int parse_threads(pid_t pid, int *nr_threads, u32 **threads)
{ {
struct dirent *de; struct dirent *de;
DIR *dir; DIR *dir;
u32 *t = NULL; u32 *t = NULL;
int ret = -1, i = 0; int nr = 1;
ret = -1;
dir = opendir_proc("%d/task", pid); dir = opendir_proc("%d/task", pid);
if (!dir) { if (!dir) {
pr_perror("Can't open %d/task", pid); pr_perror("Can't open %d/task", pid);
goto err; return -1;
} }
t = xmalloc(nr_threads);
if (!t)
goto err;
while ((de = readdir(dir))) { while ((de = readdir(dir))) {
/* We expect numbers only here */ /* We expect numbers only here */
if (de->d_name[0] == '.') if (de->d_name[0] == '.')
continue; continue;
if (i >= nr_threads) { t = xrealloc(t, nr * sizeof(u32));
pr_err("Threads inconsistency, kernel bug?\n"); t[nr - 1] = atoi(de->d_name);
goto err; nr++;
}
t[i++] = atoi(de->d_name);
} }
closedir(dir); closedir(dir);
*threads = t, t = NULL; *threads = t;
ret = 0; *nr_threads = nr - 1;
return 0;
err:
xfree(t);
return ret;
} }
static struct pstree_item *find_pstree_entry(pid_t pid) static struct pstree_item *find_pstree_entry(pid_t pid)
...@@ -737,7 +724,7 @@ static struct pstree_item *find_pstree_entry(pid_t pid) ...@@ -737,7 +724,7 @@ static struct pstree_item *find_pstree_entry(pid_t pid)
u32 *threads = NULL; u32 *threads = NULL;
u32 nr_allocated = 0; u32 nr_allocated = 0;
u32 nr_children = 0; u32 nr_children = 0;
u32 nr_threads = 0; int nr_threads = 0;
char *children_str = NULL; char *children_str = NULL;
FILE *file; FILE *file;
char *tok; char *tok;
...@@ -756,33 +743,8 @@ static struct pstree_item *find_pstree_entry(pid_t pid) ...@@ -756,33 +743,8 @@ static struct pstree_item *find_pstree_entry(pid_t pid)
} }
children_str = xstrdup(loc_buf); children_str = xstrdup(loc_buf);
if (!children_str)
goto err;
fclose(file), file = NULL; fclose(file), file = NULL;
file = fopen_proc("%d/status", "r", pid);
if (!file) {
pr_perror("Can't open %d status", pid);
goto err;
}
while ((fgets(loc_buf, sizeof(loc_buf), file))) {
if (!strncmp(loc_buf, "Threads:", 8)) {
nr_threads = atoi(&loc_buf[9]);
if (children_str)
break;
} else
continue;
}
fclose(file), file = NULL;
if (nr_threads < 1) {
pr_err("Unable to find out how many threads are used\n");
goto err;
}
if (!children_str) { if (!children_str) {
pr_err("Children marker is not found\n"); pr_err("Children marker is not found\n");
goto err; goto err;
...@@ -792,7 +754,7 @@ static struct pstree_item *find_pstree_entry(pid_t pid) ...@@ -792,7 +754,7 @@ static struct pstree_item *find_pstree_entry(pid_t pid)
if (!item) if (!item)
goto err; goto err;
if (parse_threads(pid, nr_threads, &threads)) if (parse_threads(pid, &nr_threads, &threads))
goto err_free; goto err_free;
tok = strtok(children_str, " \n"); tok = strtok(children_str, " \n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment