2021-09-26 02:18:03

by Jinshan Xiong

[permalink] [raw]
Subject: [PATCH] tools/lib/perf: add a flag to specify cgroup fd

This patch adds an API perf_thread_map__set_flags() so that we can use
it to pass down a flag to specific cgroup fd in sys_perf_event_open().

This API can be used in the following way:
int cgroup_fd = open("/sys/fs/cgroup/perf_event", O_RDONLY);
struct thread_map *threadmap = perf_thread_map__new_dummy();
perf_thread_map__set_pid(threadmap, 0, cgroup_fd);
perf_thread_map__set_flags(threadmap, 0, PERF_FLAG_PID_CGROUP);

Therefore, we can only collect perf events in a specific docker
container.

Signed-off-by: Jinshan Xiong <[email protected]>
---
tools/lib/perf/evsel.c | 3 ++-
tools/lib/perf/include/internal/threadmap.h | 1 +
tools/lib/perf/include/perf/threadmap.h | 1 +
tools/lib/perf/threadmap.c | 5 +++++
4 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/tools/lib/perf/evsel.c b/tools/lib/perf/evsel.c
index 8441e3e1aaac..67f86513a816 100644
--- a/tools/lib/perf/evsel.c
+++ b/tools/lib/perf/evsel.c
@@ -153,7 +153,8 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,

fd = sys_perf_event_open(&evsel->attr,
threads->map[thread].pid,
- cpus->map[cpu], group_fd, 0);
+ cpus->map[cpu], group_fd,
+ threads->map[thread].flags);

if (fd < 0)
return -errno;
diff --git a/tools/lib/perf/include/internal/threadmap.h b/tools/lib/perf/include/internal/threadmap.h
index df748baf9eda..5d7bd6968563 100644
--- a/tools/lib/perf/include/internal/threadmap.h
+++ b/tools/lib/perf/include/internal/threadmap.h
@@ -8,6 +8,7 @@

struct thread_map_data {
pid_t pid;
+ int flags;
char *comm;
};

diff --git a/tools/lib/perf/include/perf/threadmap.h b/tools/lib/perf/include/perf/threadmap.h
index a7c50de8d010..5fea1a403e28 100644
--- a/tools/lib/perf/include/perf/threadmap.h
+++ b/tools/lib/perf/include/perf/threadmap.h
@@ -10,6 +10,7 @@ struct perf_thread_map;
LIBPERF_API struct perf_thread_map *perf_thread_map__new_dummy(void);

LIBPERF_API void perf_thread_map__set_pid(struct perf_thread_map *map, int thread, pid_t pid);
+LIBPERF_API void perf_thread_map__set_flags(struct perf_thread_map *map, int thread, int flags);
LIBPERF_API char *perf_thread_map__comm(struct perf_thread_map *map, int thread);
LIBPERF_API int perf_thread_map__nr(struct perf_thread_map *threads);
LIBPERF_API pid_t perf_thread_map__pid(struct perf_thread_map *map, int thread);
diff --git a/tools/lib/perf/threadmap.c b/tools/lib/perf/threadmap.c
index e92c368b0a6c..3b8ec4f944b9 100644
--- a/tools/lib/perf/threadmap.c
+++ b/tools/lib/perf/threadmap.c
@@ -37,6 +37,11 @@ void perf_thread_map__set_pid(struct perf_thread_map *map, int thread, pid_t pid
map->map[thread].pid = pid;
}

+void perf_thread_map__set_flags(struct perf_thread_map *map, int thread, int flags)
+{
+ map->map[thread].flags = flags;
+}
+
char *perf_thread_map__comm(struct perf_thread_map *map, int thread)
{
return map->map[thread].comm;
--
2.30.2