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
a4feb694
Commit
a4feb694
authored
Dec 09, 2016
by
Teng Qin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Perf event example using C++ API
parent
dfd9c63b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
123 additions
and
0 deletions
+123
-0
examples/cpp/CMakeLists.txt
examples/cpp/CMakeLists.txt
+4
-0
examples/cpp/LLCStat.cc
examples/cpp/LLCStat.cc
+119
-0
No files found.
examples/cpp/CMakeLists.txt
View file @
a4feb694
...
...
@@ -22,3 +22,7 @@ install (TARGETS TCPSendStack DESTINATION share/bcc/examples/cpp)
add_executable
(
RandomRead RandomRead.cc
)
target_link_libraries
(
RandomRead bcc-static
)
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
)
examples/cpp/LLCStat.cc
0 → 100644
View file @
a4feb694
/*
* LLCStat Show LLC hit ratio for each process on each CPU core.
* For Linux, uses BCC, eBPF. Embedded C.
*
* Basic example of BCC timed sampling perf event.
*
* USAGE: LLCStat [duration]
*
* Copyright (c) Facebook, Inc.
* Licensed under the Apache License, Version 2.0 (the "License")
*/
#include <linux/perf_event.h>
#include <unistd.h>
#include <iomanip>
#include <iostream>
#include <string>
#include "BPF.h"
const
std
::
string
BPF_PROGRAM
=
R"(
#include <linux/ptrace.h>
#include <uapi/linux/bpf_perf_event.h>
struct event_t {
int cpu;
int pid;
char name[16];
};
BPF_HASH(ref_count, struct event_t);
BPF_HASH(miss_count, struct event_t);
static inline __attribute__((always_inline)) void get_key(struct event_t* key) {
key->cpu = bpf_get_smp_processor_id();
key->pid = bpf_get_current_pid_tgid();
bpf_get_current_comm(&(key->name), sizeof(key->name));
}
int on_cache_miss(struct bpf_perf_event_data *ctx) {
struct event_t key = {};
get_key(&key);
u64 zero = 0, *val;
val = miss_count.lookup_or_init(&key, &zero);
(*val) += ctx->sample_period;
return 0;
}
int on_cache_ref(struct bpf_perf_event_data *ctx) {
struct event_t key = {};
get_key(&key);
u64 zero = 0, *val;
val = ref_count.lookup_or_init(&key, &zero);
(*val) += ctx->sample_period;
return 0;
}
)"
;
struct
event_t
{
int
cpu
;
int
pid
;
char
name
[
16
];
};
int
main
(
int
argc
,
char
**
argv
)
{
ebpf
::
BPF
bpf
;
auto
init_res
=
bpf
.
init
(
BPF_PROGRAM
);
if
(
init_res
.
code
()
!=
0
)
{
std
::
cerr
<<
init_res
.
msg
()
<<
std
::
endl
;
return
1
;
}
auto
attach_ref_res
=
bpf
.
attach_perf_event
(
PERF_TYPE_HARDWARE
,
PERF_COUNT_HW_CACHE_REFERENCES
,
"on_cache_ref"
,
100
,
0
);
if
(
attach_ref_res
.
code
()
!=
0
)
{
std
::
cerr
<<
attach_ref_res
.
msg
()
<<
std
::
endl
;
return
1
;
}
auto
attach_miss_res
=
bpf
.
attach_perf_event
(
PERF_TYPE_HARDWARE
,
PERF_COUNT_HW_CACHE_MISSES
,
"on_cache_miss"
,
100
,
0
);
if
(
attach_miss_res
.
code
()
!=
0
)
{
std
::
cerr
<<
attach_miss_res
.
msg
()
<<
std
::
endl
;
return
1
;
}
int
probe_time
=
10
;
if
(
argc
==
2
)
{
probe_time
=
atoi
(
argv
[
1
]);
}
std
::
cout
<<
"Probing for "
<<
probe_time
<<
" seconds"
<<
std
::
endl
;
sleep
(
probe_time
);
bpf
.
detach_perf_event
(
PERF_TYPE_HARDWARE
,
PERF_COUNT_HW_CACHE_REFERENCES
);
bpf
.
detach_perf_event
(
PERF_TYPE_HARDWARE
,
PERF_COUNT_HW_CACHE_MISSES
);
auto
refs
=
bpf
.
get_hash_table
<
event_t
,
uint64_t
>
(
"ref_count"
);
auto
misses
=
bpf
.
get_hash_table
<
event_t
,
uint64_t
>
(
"miss_count"
);
for
(
auto
it
:
refs
.
get_table_offline
())
{
uint64_t
hit
;
try
{
auto
miss
=
misses
[
it
.
first
];
hit
=
miss
<=
it
.
second
?
it
.
second
-
miss
:
0
;
}
catch
(...)
{
hit
=
it
.
second
;
}
double
ratio
=
(
double
(
hit
)
/
double
(
it
.
second
))
*
100.0
;
std
::
cout
<<
"PID "
<<
std
::
setw
(
8
)
<<
std
::
setfill
(
' '
)
<<
it
.
first
.
pid
;
std
::
cout
<<
std
::
setw
(
20
)
<<
std
::
setfill
(
' '
)
<<
std
::
left
<<
" ("
+
std
::
string
(
it
.
first
.
name
)
+
") "
<<
std
::
right
;
std
::
cout
<<
"on CPU "
<<
std
::
setw
(
2
)
<<
std
::
setfill
(
' '
)
<<
it
.
first
.
cpu
;
std
::
cout
<<
" Hit Rate "
<<
std
::
setprecision
(
4
)
<<
ratio
<<
"% "
;
std
::
cout
<<
"("
<<
hit
<<
"/"
<<
it
.
second
<<
")"
<<
std
::
endl
;
}
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