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
6c33a523
Commit
6c33a523
authored
Mar 09, 2018
by
Teng Qin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
C++ interface for attaching to perf event with raw perf_event_attr argument
parent
5b7c6783
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
3 deletions
+55
-3
src/cc/api/BPF.cc
src/cc/api/BPF.cc
+49
-2
src/cc/api/BPF.h
src/cc/api/BPF.h
+6
-1
No files found.
src/cc/api/BPF.cc
View file @
6c33a523
...
...
@@ -15,6 +15,7 @@
*/
#include <linux/bpf.h>
#include <linux/perf_event.h>
#include <unistd.h>
#include <cstdio>
#include <cstring>
...
...
@@ -292,12 +293,13 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
int
probe_fd
;
TRY2
(
load_func
(
probe_func
,
BPF_PROG_TYPE_PERF_EVENT
,
probe_fd
));
auto
fds
=
new
std
::
map
<
int
,
int
>
();
std
::
vector
<
int
>
cpus
;
if
(
cpu
>=
0
)
cpus
.
push_back
(
cpu
);
else
cpus
=
get_online_cpus
();
auto
fds
=
new
std
::
vector
<
std
::
pair
<
int
,
int
>>
();
fds
->
reserve
(
cpus
.
size
());
for
(
int
i
:
cpus
)
{
int
fd
=
bpf_attach_perf_event
(
probe_fd
,
ev_type
,
ev_config
,
sample_period
,
sample_freq
,
pid
,
i
,
group_fd
);
...
...
@@ -309,7 +311,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
return
StatusTuple
(
-
1
,
"Failed to attach perf event type %d config %d"
,
ev_type
,
ev_config
);
}
fds
->
emplace
(
i
,
fd
);
fds
->
emplace
_back
(
i
,
fd
);
}
open_probe_t
p
=
{};
...
...
@@ -319,6 +321,46 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
attach_perf_event_raw
(
void
*
perf_event_attr
,
const
std
::
string
&
probe_func
,
pid_t
pid
,
int
cpu
,
int
group_fd
)
{
auto
attr
=
static_cast
<
struct
perf_event_attr
*>
(
perf_event_attr
);
auto
ev_pair
=
std
::
make_pair
(
attr
->
type
,
attr
->
config
);
if
(
perf_events_
.
find
(
ev_pair
)
!=
perf_events_
.
end
())
return
StatusTuple
(
-
1
,
"Perf event type %d config %d already attached"
,
attr
->
type
,
attr
->
config
);
int
probe_fd
;
TRY2
(
load_func
(
probe_func
,
BPF_PROG_TYPE_PERF_EVENT
,
probe_fd
));
std
::
vector
<
int
>
cpus
;
if
(
cpu
>=
0
)
cpus
.
push_back
(
cpu
);
else
cpus
=
get_online_cpus
();
auto
fds
=
new
std
::
vector
<
std
::
pair
<
int
,
int
>>
();
fds
->
reserve
(
cpus
.
size
());
for
(
int
i
:
cpus
)
{
int
fd
=
bpf_attach_perf_event_raw
(
probe_fd
,
attr
,
pid
,
i
,
group_fd
);
if
(
fd
<
0
)
{
for
(
const
auto
&
it
:
*
fds
)
close
(
it
.
second
);
delete
fds
;
TRY2
(
unload_func
(
probe_func
));
return
StatusTuple
(
-
1
,
"Failed to attach perf event type %d config %d"
,
attr
->
type
,
attr
->
config
);
}
fds
->
emplace_back
(
i
,
fd
);
}
open_probe_t
p
=
{};
p
.
func
=
probe_func
;
p
.
per_cpu_fd
=
fds
;
perf_events_
[
ev_pair
]
=
std
::
move
(
p
);
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_kprobe
(
const
std
::
string
&
kernel_func
,
bpf_probe_attach_type
attach_type
)
{
std
::
string
event
=
get_kprobe_event
(
kernel_func
,
attach_type
);
...
...
@@ -394,6 +436,11 @@ StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) {
return
StatusTuple
(
0
);
}
StatusTuple
BPF
::
detach_perf_event_raw
(
void
*
perf_event_attr
)
{
auto
attr
=
static_cast
<
struct
perf_event_attr
*>
(
perf_event_attr
);
return
detach_perf_event
(
attr
->
type
,
attr
->
config
);
}
StatusTuple
BPF
::
open_perf_event
(
const
std
::
string
&
name
,
uint32_t
type
,
uint64_t
config
)
{
if
(
perf_event_arrays_
.
find
(
name
)
==
perf_event_arrays_
.
end
())
{
...
...
src/cc/api/BPF.h
View file @
6c33a523
...
...
@@ -36,7 +36,7 @@ namespace ebpf {
struct
open_probe_t
{
int
perf_event_fd
;
std
::
string
func
;
std
::
map
<
int
,
int
>*
per_cpu_fd
;
std
::
vector
<
std
::
pair
<
int
,
int
>
>*
per_cpu_fd
;
};
class
USDT
;
...
...
@@ -84,7 +84,12 @@ class BPF {
uint64_t
sample_period
,
uint64_t
sample_freq
,
pid_t
pid
=
-
1
,
int
cpu
=
-
1
,
int
group_fd
=
-
1
);
StatusTuple
attach_perf_event_raw
(
void
*
perf_event_attr
,
const
std
::
string
&
probe_func
,
pid_t
pid
=
-
1
,
int
cpu
=
-
1
,
int
group_fd
=
-
1
);
StatusTuple
detach_perf_event
(
uint32_t
ev_type
,
uint32_t
ev_config
);
StatusTuple
detach_perf_event_raw
(
void
*
perf_event_attr
);
BPFTable
get_table
(
const
std
::
string
&
name
)
{
TableStorage
::
iterator
it
;
...
...
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