Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
bcc
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
bcc
Commits
206b020e
Commit
206b020e
authored
Oct 18, 2016
by
Teng Qin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add bpf_attach_perf_event in libbpf
parent
d1c78ca8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
0 deletions
+64
-0
src/cc/libbpf.c
src/cc/libbpf.c
+57
-0
src/cc/libbpf.h
src/cc/libbpf.h
+7
-0
No files found.
src/cc/libbpf.c
View file @
206b020e
...
...
@@ -570,3 +570,60 @@ cleanup:
close
(
sock
);
return
ret
;
}
int
bpf_attach_perf_event
(
int
progfd
,
uint32_t
ev_type
,
uint32_t
ev_config
,
uint64_t
sample_period
,
uint64_t
sample_freq
,
pid_t
pid
,
int
cpu
,
int
group_fd
)
{
if
(
ev_type
!=
PERF_TYPE_HARDWARE
&&
ev_type
!=
PERF_TYPE_SOFTWARE
)
{
fprintf
(
stderr
,
"Unsupported perf event type
\n
"
);
return
-
1
;
}
if
((
ev_type
==
PERF_TYPE_HARDWARE
&&
ev_config
>=
PERF_COUNT_HW_MAX
)
||
(
ev_type
==
PERF_TYPE_SOFTWARE
&&
ev_config
>=
PERF_COUNT_SW_MAX
))
{
fprintf
(
stderr
,
"Invalid perf event config
\n
"
);
return
-
1
;
}
if
(
!
((
sample_period
>
0
)
^
(
sample_freq
>
0
)))
{
fprintf
(
stderr
,
"Exactly one of sample_period / sample_freq should be set
\n
"
);
return
-
1
;
}
struct
perf_event_attr
attr
=
{};
attr
.
type
=
ev_type
;
attr
.
config
=
ev_config
;
attr
.
inherit
=
1
;
if
(
sample_freq
>
0
)
{
attr
.
freq
=
1
;
attr
.
sample_freq
=
sample_freq
;
}
else
{
attr
.
sample_period
=
sample_period
;
}
int
fd
=
syscall
(
__NR_perf_event_open
,
&
attr
,
pid
,
cpu
,
group_fd
,
PERF_FLAG_FD_CLOEXEC
);
if
(
fd
<
0
)
{
perror
(
"perf_event_open failed"
);
return
-
1
;
}
if
(
ioctl
(
fd
,
PERF_EVENT_IOC_SET_BPF
,
progfd
)
!=
0
)
{
perror
(
"ioctl(PERF_EVENT_IOC_SET_BPF) failed"
);
close
(
fd
);
return
-
1
;
}
if
(
ioctl
(
fd
,
PERF_EVENT_IOC_ENABLE
,
0
)
!=
0
)
{
perror
(
"ioctl(PERF_EVENT_IOC_ENABLE) failed"
);
close
(
fd
);
return
-
1
;
}
return
fd
;
}
int
bpf_detach_perf_event
(
uint32_t
ev_type
,
uint32_t
ev_config
)
{
// Right now, there is nothing to do, but it's a good idea to encourage
// callers to detach anything they attach.
return
0
;
}
src/cc/libbpf.h
View file @
206b020e
...
...
@@ -64,6 +64,13 @@ void * bpf_open_perf_buffer(perf_reader_raw_cb raw_cb, void *cb_cookie, int pid,
/* attached a prog expressed by progfd to the device specified in dev_name */
int
bpf_attach_xdp
(
const
char
*
dev_name
,
int
progfd
);
// attach a prog expressed by progfd to run on a specific perf event, with
// certain sample period or sample frequency
int
bpf_attach_perf_event
(
int
progfd
,
uint32_t
ev_type
,
uint32_t
ev_config
,
uint64_t
sample_period
,
uint64_t
sample_freq
,
pid_t
pid
,
int
cpu
,
int
group_fd
);
int
bpf_detach_perf_event
(
uint32_t
ev_type
,
uint32_t
ev_config
);
#define LOG_BUF_SIZE 65536
// Put non-static/inline functions in their own section with this prefix +
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment