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:
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;
DIR *dir;
u32 *t = NULL;
int ret = -1, i = 0;
ret = -1;
int nr = 1;
dir = opendir_proc("%d/task", pid);
if (!dir) {
pr_perror("Can't open %d/task", pid);
goto err;
return -1;
}
t = xmalloc(nr_threads);
if (!t)
goto err;
while ((de = readdir(dir))) {
/* We expect numbers only here */
if (de->d_name[0] == '.')
continue;
if (i >= nr_threads) {
pr_err("Threads inconsistency, kernel bug?\n");
goto err;
}
t[i++] = atoi(de->d_name);
t = xrealloc(t, nr * sizeof(u32));
t[nr - 1] = atoi(de->d_name);
nr++;
}
closedir(dir);
*threads = t, t = NULL;
ret = 0;
err:
xfree(t);
return ret;
*threads = t;
*nr_threads = nr - 1;
return 0;
}
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 nr_allocated = 0;
u32 nr_children = 0;
u32 nr_threads = 0;
int nr_threads = 0;
char *children_str = NULL;
FILE *file;
char *tok;
......@@ -756,33 +743,8 @@ static struct pstree_item *find_pstree_entry(pid_t pid)
}
children_str = xstrdup(loc_buf);
if (!children_str)
goto err;
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) {
pr_err("Children marker is not found\n");
goto err;
......@@ -792,7 +754,7 @@ static struct pstree_item *find_pstree_entry(pid_t pid)
if (!item)
goto err;
if (parse_threads(pid, nr_threads, &threads))
if (parse_threads(pid, &nr_threads, &threads))
goto err_free;
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