Skip to content

Commit ec8149f

Browse files
liu-song-6acmel
authored andcommitted
perf util: Move bpf_perf definitions to a libperf header
By following the same protocol, other tools can share hardware PMCs with perf. Move perf_event_attr_map_entry and BPF_PERF_DEFAULT_ATTR_MAP_PATH to bpf_perf.h for other tools to use. Signed-off-by: Song Liu <song@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Song Liu <songliubraving@fb.com> Cc: kernel-team@fb.com Link: https://lore.kernel.org/r/20210425214333.1090950-2-song@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 26bda3c commit ec8149f

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2+
#ifndef __LIBPERF_BPF_PERF_H
3+
#define __LIBPERF_BPF_PERF_H
4+
5+
#include <linux/types.h> /* for __u32 */
6+
7+
/*
8+
* bpf_perf uses a hashmap, the attr_map, to track all the leader programs.
9+
* The hashmap is pinned in bpffs. flock() on this file is used to ensure
10+
* no concurrent access to the attr_map. The key of attr_map is struct
11+
* perf_event_attr, and the value is struct perf_event_attr_map_entry.
12+
*
13+
* struct perf_event_attr_map_entry contains two __u32 IDs, bpf_link of the
14+
* leader prog, and the diff_map. Each perf-stat session holds a reference
15+
* to the bpf_link to make sure the leader prog is attached to sched_switch
16+
* tracepoint.
17+
*
18+
* Since the hashmap only contains IDs of the bpf_link and diff_map, it
19+
* does not hold any references to the leader program. Once all perf-stat
20+
* sessions of these events exit, the leader prog, its maps, and the
21+
* perf_events will be freed.
22+
*/
23+
struct perf_event_attr_map_entry {
24+
__u32 link_id;
25+
__u32 diff_map_id;
26+
};
27+
28+
/* default attr_map name */
29+
#define BPF_PERF_DEFAULT_ATTR_MAP_PATH "perf_attr_map"
30+
31+
#endif /* __LIBPERF_BPF_PERF_H */

tools/perf/util/bpf_counter.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <bpf/btf.h>
1515
#include <bpf/libbpf.h>
1616
#include <api/fs/fs.h>
17+
#include <perf/bpf_perf.h>
1718

1819
#include "bpf_counter.h"
1920
#include "counts.h"
@@ -29,28 +30,6 @@
2930
#include "bpf_skel/bperf_leader.skel.h"
3031
#include "bpf_skel/bperf_follower.skel.h"
3132

32-
/*
33-
* bperf uses a hashmap, the attr_map, to track all the leader programs.
34-
* The hashmap is pinned in bpffs. flock() on this file is used to ensure
35-
* no concurrent access to the attr_map. The key of attr_map is struct
36-
* perf_event_attr, and the value is struct perf_event_attr_map_entry.
37-
*
38-
* struct perf_event_attr_map_entry contains two __u32 IDs, bpf_link of the
39-
* leader prog, and the diff_map. Each perf-stat session holds a reference
40-
* to the bpf_link to make sure the leader prog is attached to sched_switch
41-
* tracepoint.
42-
*
43-
* Since the hashmap only contains IDs of the bpf_link and diff_map, it
44-
* does not hold any references to the leader program. Once all perf-stat
45-
* sessions of these events exit, the leader prog, its maps, and the
46-
* perf_events will be freed.
47-
*/
48-
struct perf_event_attr_map_entry {
49-
__u32 link_id;
50-
__u32 diff_map_id;
51-
};
52-
53-
#define DEFAULT_ATTR_MAP_PATH "fs/bpf/perf_attr_map"
5433
#define ATTR_MAP_SIZE 16
5534

5635
static inline void *u64_to_ptr(__u64 ptr)
@@ -341,8 +320,8 @@ static int bperf_lock_attr_map(struct target *target)
341320
if (target->attr_map) {
342321
scnprintf(path, PATH_MAX, "%s", target->attr_map);
343322
} else {
344-
scnprintf(path, PATH_MAX, "%s/%s", sysfs__mountpoint(),
345-
DEFAULT_ATTR_MAP_PATH);
323+
scnprintf(path, PATH_MAX, "%s/fs/bpf/%s", sysfs__mountpoint(),
324+
BPF_PERF_DEFAULT_ATTR_MAP_PATH);
346325
}
347326

348327
if (access(path, F_OK)) {

0 commit comments

Comments
 (0)