perf cgroup: Maintain cgroup hierarchy
Each cgroup is kept in the perf_env's cgroup_tree sorted by the cgroup id. Hist entries have cgroup id can compare it directly and later it can be used to find a group name using this tree. Signed-off-by: Namhyung Kim <namhyung@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lore.kernel.org/lkml/20200325124536.2800725-5-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
committed by
Arnaldo Carvalho de Melo
parent
ba78c1c546
commit
d1277aa36b
@@ -191,3 +191,83 @@ int parse_cgroups(const struct option *opt, const char *str,
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
|
||||
bool create, const char *path)
|
||||
{
|
||||
struct rb_node **p = &root->rb_node;
|
||||
struct rb_node *parent = NULL;
|
||||
struct cgroup *cgrp;
|
||||
|
||||
while (*p != NULL) {
|
||||
parent = *p;
|
||||
cgrp = rb_entry(parent, struct cgroup, node);
|
||||
|
||||
if (cgrp->id == id)
|
||||
return cgrp;
|
||||
|
||||
if (cgrp->id < id)
|
||||
p = &(*p)->rb_left;
|
||||
else
|
||||
p = &(*p)->rb_right;
|
||||
}
|
||||
|
||||
if (!create)
|
||||
return NULL;
|
||||
|
||||
cgrp = malloc(sizeof(*cgrp));
|
||||
if (cgrp == NULL)
|
||||
return NULL;
|
||||
|
||||
cgrp->name = strdup(path);
|
||||
if (cgrp->name == NULL) {
|
||||
free(cgrp);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
cgrp->fd = -1;
|
||||
cgrp->id = id;
|
||||
refcount_set(&cgrp->refcnt, 1);
|
||||
|
||||
rb_link_node(&cgrp->node, parent, p);
|
||||
rb_insert_color(&cgrp->node, root);
|
||||
|
||||
return cgrp;
|
||||
}
|
||||
|
||||
struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
|
||||
const char *path)
|
||||
{
|
||||
struct cgroup *cgrp;
|
||||
|
||||
down_write(&env->cgroups.lock);
|
||||
cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
|
||||
up_write(&env->cgroups.lock);
|
||||
return cgrp;
|
||||
}
|
||||
|
||||
struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
|
||||
{
|
||||
struct cgroup *cgrp;
|
||||
|
||||
down_read(&env->cgroups.lock);
|
||||
cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
|
||||
up_read(&env->cgroups.lock);
|
||||
return cgrp;
|
||||
}
|
||||
|
||||
void perf_env__purge_cgroups(struct perf_env *env)
|
||||
{
|
||||
struct rb_node *node;
|
||||
struct cgroup *cgrp;
|
||||
|
||||
down_write(&env->cgroups.lock);
|
||||
while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
|
||||
node = rb_first(&env->cgroups.tree);
|
||||
cgrp = rb_entry(node, struct cgroup, node);
|
||||
|
||||
rb_erase(node, &env->cgroups.tree);
|
||||
cgroup__put(cgrp);
|
||||
}
|
||||
up_write(&env->cgroups.lock);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user