Commit 6de20ba9 authored by Pavel Emelyanov's avatar Pavel Emelyanov Committed by Cyrill Gorcunov

crtools: Merge pstree collecting into showing

1. There's no need in collecting children list in shower
2. There's no need in reading file twice -- we can collect and
   show everything in one go.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent 0d34b270
......@@ -215,7 +215,7 @@ out:
pr_img_tail(CR_FD_SIGACT);
}
static void show_pstree(int fd_pstree)
static int show_pstree(int fd_pstree, struct list_head *collect)
{
struct pstree_entry e;
......@@ -224,6 +224,7 @@ static void show_pstree(int fd_pstree)
while (1) {
u32 pid;
int ret;
struct pstree_item *item = NULL;
ret = read_ptr_safe_eof(fd_pstree, &e, out);
if (!ret)
......@@ -231,6 +232,22 @@ static void show_pstree(int fd_pstree)
pr_info("pid: %8d nr_children: %8d nr_threads: %8d\n",
e.pid, e.nr_children, e.nr_threads);
if (collect) {
item = xzalloc(sizeof(struct pstree_item));
if (!item)
return -1;
item->pid = e.pid;
item->nr_threads = e.nr_threads;
item->threads = xzalloc(sizeof(u32) * e.nr_threads);
if (!item->threads) {
xfree(item);
return -1;
}
list_add_tail(&item->list, collect);
}
if (e.nr_children) {
pr_info("\\\n");
pr_info(" +--- children: ");
......@@ -251,6 +268,8 @@ static void show_pstree(int fd_pstree)
if (!ret)
goto out;
pr_info(" %6d", pid);
if (item)
item->threads[e.nr_threads] = pid;
}
pr_info("\n");
}
......@@ -259,6 +278,7 @@ static void show_pstree(int fd_pstree)
out:
pr_img_tail(CR_FD_PSTREE);
return 0;
}
static void show_core_regs(int fd_core)
......@@ -403,7 +423,7 @@ static int cr_parse_file(struct cr_options *opts)
show_shmem(fd);
break;
case PSTREE_MAGIC:
show_pstree(fd);
show_pstree(fd, NULL);
break;
case PIPES_MAGIC:
show_pipes(fd);
......@@ -425,71 +445,6 @@ err:
return ret;
}
static int read_pstree(struct list_head *head, struct cr_fdset *cr_fdset)
{
int fd = cr_fdset->fds[CR_FD_PSTREE];
struct pstree_item *item = NULL;
struct pstree_entry e;
int ret = -1;
for (;;) {
size_t size_children, size_threads;
ret = read(fd, &e, sizeof(e));
if (ret && ret != sizeof(e)) {
pr_perror("Wrong pstree entry\n");
goto err;
}
if (!ret)
break;
item = xzalloc(sizeof(*item));
if (!item)
goto err;
size_children = sizeof(u32) * e.nr_children;
size_threads = sizeof(u32) * e.nr_threads;
item->pid = e.pid;
item->nr_children = e.nr_children;
item->nr_threads = e.nr_threads;
item->children = xmalloc(size_children);
item->threads = xmalloc(size_threads);
if (!item->children || !item->threads) {
pr_err("No memory for children/thread pids\n");
goto err;
}
ret = read(fd, item->children, size_children);
if (ret != size_children) {
pr_err("An error in reading children pids\n");
goto err;
}
ret = read(fd, item->threads, size_threads);
if (ret != size_threads) {
pr_err("An error in reading threads pids\n");
goto err;
}
list_add_tail(&item->list, head);
}
item = NULL;
ret = 0;
err:
if (item) {
xfree(item->children);
xfree(item->threads);
}
xfree(item);
return ret;
}
static int cr_show_all(unsigned long pid, struct cr_options *opts)
{
struct cr_fdset *cr_fdset = NULL;
......@@ -501,17 +456,10 @@ static int cr_show_all(unsigned long pid, struct cr_options *opts)
if (!cr_fdset)
goto out;
ret = read_pstree(&pstree_list, cr_fdset);
ret = show_pstree(cr_fdset->fds[CR_FD_PSTREE], &pstree_list);
if (ret)
goto out;
/*
* Yeah, I know we read the same file for second
* time here, but this saves us from code duplication.
*/
lseek(cr_fdset->fds[CR_FD_PSTREE], MAGIC_OFFSET, SEEK_SET);
show_pstree(cr_fdset->fds[CR_FD_PSTREE]);
close_cr_fdset(cr_fdset);
list_for_each_entry(item, &pstree_list, list) {
......
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