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
9840a7b3
Commit
9840a7b3
authored
Dec 15, 2016
by
Teng Qin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Example of using USDT
parent
6e682236
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
0 deletions
+109
-0
examples/cpp/CMakeLists.txt
examples/cpp/CMakeLists.txt
+4
-0
examples/cpp/FollyRequestContextSwitch.cc
examples/cpp/FollyRequestContextSwitch.cc
+105
-0
No files found.
examples/cpp/CMakeLists.txt
View file @
9840a7b3
...
...
@@ -26,3 +26,7 @@ install (TARGETS RandomRead DESTINATION share/bcc/examples/cpp)
add_executable
(
LLCStat LLCStat.cc
)
target_link_libraries
(
LLCStat bcc-static
)
install
(
TARGETS LLCStat DESTINATION share/bcc/examples/cpp
)
add_executable
(
FollyRequestContextSwitch FollyRequestContextSwitch.cc
)
target_link_libraries
(
FollyRequestContextSwitch bcc-static
)
install
(
TARGETS FollyRequestContextSwitch DESTINATION share/bcc/examples/cpp
)
examples/cpp/FollyRequestContextSwitch.cc
0 → 100644
View file @
9840a7b3
/*
* FollyRequestContextSwitch Monitor RequestContext switch events for any binary
* uses the class from [folly](http://bit.ly/2h6S1yx).
* For Linux, uses BCC, eBPF. Embedded C.
*
* Basic example of using USDT with BCC.
*
* USAGE: FollyRequestContextSwitch PATH_TO_BINARY
*
* Copyright (c) Facebook, Inc.
* Licensed under the Apache License, Version 2.0 (the "License")
*/
#include <signal.h>
#include <iostream>
#include <vector>
#include "BPF.h"
const
std
::
string
BPF_PROGRAM
=
R"(
#include <linux/sched.h>
#include <uapi/linux/ptrace.h>
struct event_t {
int pid;
char name[16];
uint64_t old_addr;
uint64_t new_addr;
};
BPF_PERF_OUTPUT(events);
int on_context_switch(struct pt_regs *ctx) {
struct event_t event = {};
event.pid = bpf_get_current_pid_tgid();
bpf_get_current_comm(&event.name, sizeof(event.name));
bpf_usdt_readarg(1, ctx, &event.old_addr);
bpf_usdt_readarg(2, ctx, &event.new_addr);
events.perf_submit(ctx, &event, sizeof(event));
return 0;
}
)"
;
// Define the same struct to use in user space.
struct
event_t
{
int
pid
;
char
name
[
16
];
uint64_t
old_addr
;
uint64_t
new_addr
;
};
void
handle_output
(
void
*
cb_cookie
,
void
*
data
,
int
data_size
)
{
auto
event
=
static_cast
<
event_t
*>
(
data
);
std
::
cout
<<
"PID "
<<
event
->
pid
<<
" ("
<<
event
->
name
<<
") "
;
std
::
cout
<<
"folly::RequestContext switch from "
<<
event
->
old_addr
<<
" to "
<<
event
->
new_addr
<<
std
::
endl
;
}
ebpf
::
BPF
*
bpf
;
void
signal_handler
(
int
s
)
{
std
::
cerr
<<
"Terminating..."
<<
std
::
endl
;
delete
bpf
;
exit
(
0
);
}
int
main
(
int
argc
,
char
**
argv
)
{
if
(
argc
!=
2
)
{
std
::
cout
<<
"USAGE: FollyRequestContextSwitch PATH_TO_BINARY"
<<
std
::
endl
;
exit
(
1
);
}
std
::
string
binary_path
(
argv
[
1
]);
bpf
=
new
ebpf
::
BPF
();
std
::
vector
<
ebpf
::
USDT
>
u
;
u
.
emplace_back
(
binary_path
,
"folly"
,
"request_context_switch_before"
,
"on_context_switch"
);
auto
init_res
=
bpf
->
init
(
BPF_PROGRAM
,
{},
u
);
if
(
init_res
.
code
()
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
()
<<
std
::
endl
;
return
1
;
}
auto
attach_res
=
bpf
->
attach_usdt
(
u
[
0
]);
if
(
attach_res
.
code
()
!=
0
)
{
std
::
cerr
<<
attach_res
.
msg
()
<<
std
::
endl
;
return
1
;
}
auto
open_res
=
bpf
->
open_perf_buffer
(
"events"
,
&
handle_output
);
if
(
open_res
.
code
()
!=
0
)
{
std
::
cerr
<<
open_res
.
msg
()
<<
std
::
endl
;
return
1
;
}
signal
(
SIGINT
,
signal_handler
);
std
::
cout
<<
"Started tracing, hit Ctrl-C to terminate."
<<
std
::
endl
;
while
(
true
)
bpf
->
poll_perf_buffer
(
"events"
);
return
0
;
}
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