Skip to content

Commit 69e874d

Browse files
namhyungacmel
authored andcommitted
perf tools: Add read_cgroup_id() function
The read_cgroup_id() is to read a cgroup id from a file handle using name_to_handle_at(2) for the given cgroup. It'll be used by bperf cgroup stat later. Committer notes: -int read_cgroup_id(struct cgroup *cgrp) +static inline int read_cgroup_id(struct cgroup *cgrp __maybe_unused) To fix the build when HAVE_FILE_HANDLE is not defined. Signed-off-by: Namhyung Kim <[email protected]> Cc: Andi Kleen <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Song Liu <[email protected]> Cc: Stephane Eranian <[email protected]> Link: http://lore.kernel.org/lkml/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent f20510d commit 69e874d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

tools/perf/util/cgroup.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,31 @@ static int open_cgroup(const char *name)
4545
return fd;
4646
}
4747

48+
#ifdef HAVE_FILE_HANDLE
49+
int read_cgroup_id(struct cgroup *cgrp)
50+
{
51+
char path[PATH_MAX + 1];
52+
char mnt[PATH_MAX + 1];
53+
struct {
54+
struct file_handle fh;
55+
uint64_t cgroup_id;
56+
} handle;
57+
int mount_id;
58+
59+
if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, "perf_event"))
60+
return -1;
61+
62+
scnprintf(path, PATH_MAX, "%s/%s", mnt, cgrp->name);
63+
64+
handle.fh.handle_bytes = sizeof(handle.cgroup_id);
65+
if (name_to_handle_at(AT_FDCWD, path, &handle.fh, &mount_id, 0) < 0)
66+
return -1;
67+
68+
cgrp->id = handle.cgroup_id;
69+
return 0;
70+
}
71+
#endif /* HAVE_FILE_HANDLE */
72+
4873
static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
4974
{
5075
struct evsel *counter;

tools/perf/util/cgroup.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#ifndef __CGROUP_H__
33
#define __CGROUP_H__
44

5+
#include <linux/compiler.h>
56
#include <linux/refcount.h>
67
#include <linux/rbtree.h>
78
#include "util/env.h"
@@ -38,4 +39,13 @@ struct cgroup *cgroup__find(struct perf_env *env, uint64_t id);
3839

3940
void perf_env__purge_cgroups(struct perf_env *env);
4041

42+
#ifdef HAVE_FILE_HANDLE
43+
int read_cgroup_id(struct cgroup *cgrp);
44+
#else
45+
static inline int read_cgroup_id(struct cgroup *cgrp __maybe_unused)
46+
{
47+
return -1;
48+
}
49+
#endif /* HAVE_FILE_HANDLE */
50+
4151
#endif /* __CGROUP_H__ */

0 commit comments

Comments
 (0)