Skip to content

Commit d1277aa

Browse files
namhyungacmel
authored andcommitted
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 <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent ba78c1c commit d1277aa

File tree

5 files changed

+109
-5
lines changed

5 files changed

+109
-5
lines changed

tools/perf/util/cgroup.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,83 @@ int parse_cgroups(const struct option *opt, const char *str,
191191
}
192192
return 0;
193193
}
194+
195+
static struct cgroup *__cgroup__findnew(struct rb_root *root, uint64_t id,
196+
bool create, const char *path)
197+
{
198+
struct rb_node **p = &root->rb_node;
199+
struct rb_node *parent = NULL;
200+
struct cgroup *cgrp;
201+
202+
while (*p != NULL) {
203+
parent = *p;
204+
cgrp = rb_entry(parent, struct cgroup, node);
205+
206+
if (cgrp->id == id)
207+
return cgrp;
208+
209+
if (cgrp->id < id)
210+
p = &(*p)->rb_left;
211+
else
212+
p = &(*p)->rb_right;
213+
}
214+
215+
if (!create)
216+
return NULL;
217+
218+
cgrp = malloc(sizeof(*cgrp));
219+
if (cgrp == NULL)
220+
return NULL;
221+
222+
cgrp->name = strdup(path);
223+
if (cgrp->name == NULL) {
224+
free(cgrp);
225+
return NULL;
226+
}
227+
228+
cgrp->fd = -1;
229+
cgrp->id = id;
230+
refcount_set(&cgrp->refcnt, 1);
231+
232+
rb_link_node(&cgrp->node, parent, p);
233+
rb_insert_color(&cgrp->node, root);
234+
235+
return cgrp;
236+
}
237+
238+
struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
239+
const char *path)
240+
{
241+
struct cgroup *cgrp;
242+
243+
down_write(&env->cgroups.lock);
244+
cgrp = __cgroup__findnew(&env->cgroups.tree, id, true, path);
245+
up_write(&env->cgroups.lock);
246+
return cgrp;
247+
}
248+
249+
struct cgroup *cgroup__find(struct perf_env *env, uint64_t id)
250+
{
251+
struct cgroup *cgrp;
252+
253+
down_read(&env->cgroups.lock);
254+
cgrp = __cgroup__findnew(&env->cgroups.tree, id, false, NULL);
255+
up_read(&env->cgroups.lock);
256+
return cgrp;
257+
}
258+
259+
void perf_env__purge_cgroups(struct perf_env *env)
260+
{
261+
struct rb_node *node;
262+
struct cgroup *cgrp;
263+
264+
down_write(&env->cgroups.lock);
265+
while (!RB_EMPTY_ROOT(&env->cgroups.tree)) {
266+
node = rb_first(&env->cgroups.tree);
267+
cgrp = rb_entry(node, struct cgroup, node);
268+
269+
rb_erase(node, &env->cgroups.tree);
270+
cgroup__put(cgrp);
271+
}
272+
up_write(&env->cgroups.lock);
273+
}

tools/perf/util/cgroup.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
#define __CGROUP_H__
44

55
#include <linux/refcount.h>
6+
#include <linux/rbtree.h>
7+
#include "util/env.h"
68

79
struct option;
810

911
struct cgroup {
10-
char *name;
11-
int fd;
12-
refcount_t refcnt;
12+
struct rb_node node;
13+
u64 id;
14+
char *name;
15+
int fd;
16+
refcount_t refcnt;
1317
};
1418

15-
1619
extern int nr_cgroups; /* number of explicit cgroups defined */
1720

1821
struct cgroup *cgroup__get(struct cgroup *cgroup);
@@ -26,4 +29,10 @@ void evlist__set_default_cgroup(struct evlist *evlist, struct cgroup *cgroup);
2629

2730
int parse_cgroups(const struct option *opt, const char *str, int unset);
2831

32+
struct cgroup *cgroup__findnew(struct perf_env *env, uint64_t id,
33+
const char *path);
34+
struct cgroup *cgroup__find(struct perf_env *env, uint64_t id);
35+
36+
void perf_env__purge_cgroups(struct perf_env *env);
37+
2938
#endif /* __CGROUP_H__ */

tools/perf/util/env.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <linux/ctype.h>
77
#include <linux/zalloc.h>
88
#include "bpf-event.h"
9+
#include "cgroup.h"
910
#include <errno.h>
1011
#include <sys/utsname.h>
1112
#include <bpf/libbpf.h>
@@ -168,6 +169,7 @@ void perf_env__exit(struct perf_env *env)
168169
int i;
169170

170171
perf_env__purge_bpf(env);
172+
perf_env__purge_cgroups(env);
171173
zfree(&env->hostname);
172174
zfree(&env->os_release);
173175
zfree(&env->version);

tools/perf/util/env.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ struct perf_env {
8888
u32 btfs_cnt;
8989
} bpf_progs;
9090

91+
/* same reason as above (for perf-top) */
92+
struct {
93+
struct rw_semaphore lock;
94+
struct rb_root tree;
95+
} cgroups;
96+
9197
/* For fast cpu to numa node lookup via perf_env__numa_node */
9298
int *numa_map;
9399
int nr_numa_map;

tools/perf/util/machine.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "asm/bug.h"
3434
#include "bpf-event.h"
3535
#include <internal/lib.h> // page_size
36+
#include "cgroup.h"
3637

3738
#include <linux/ctype.h>
3839
#include <symbol/kallsyms.h>
@@ -654,13 +655,19 @@ int machine__process_namespaces_event(struct machine *machine __maybe_unused,
654655
return err;
655656
}
656657

657-
int machine__process_cgroup_event(struct machine *machine __maybe_unused,
658+
int machine__process_cgroup_event(struct machine *machine,
658659
union perf_event *event,
659660
struct perf_sample *sample __maybe_unused)
660661
{
662+
struct cgroup *cgrp;
663+
661664
if (dump_trace)
662665
perf_event__fprintf_cgroup(event, stdout);
663666

667+
cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
668+
if (cgrp == NULL)
669+
return -ENOMEM;
670+
664671
return 0;
665672
}
666673

0 commit comments

Comments
 (0)