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
35771cf9
Commit
35771cf9
authored
Dec 15, 2017
by
Teng Qin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Perf Event tests
parent
4186f5ec
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
141 additions
and
0 deletions
+141
-0
tests/cc/CMakeLists.txt
tests/cc/CMakeLists.txt
+1
-0
tests/cc/test_bpf_table.cc
tests/cc/test_bpf_table.cc
+2
-0
tests/cc/test_perf_event.cc
tests/cc/test_perf_event.cc
+138
-0
No files found.
tests/cc/CMakeLists.txt
View file @
35771cf9
...
...
@@ -16,6 +16,7 @@ add_executable(test_libbcc
test_array_table.cc
test_bpf_table.cc
test_hash_table.cc
test_perf_event.cc
test_usdt_args.cc
test_usdt_probes.cc
utils.cc
)
...
...
tests/cc/test_bpf_table.cc
View file @
35771cf9
...
...
@@ -128,6 +128,8 @@ TEST_CASE("test bpf stack table", "[bpf_stack_table]") {
int zero = 0, *val;
val = id.lookup_or_init(&zero, &stack_id);
(*val) = stack_id;
return 0;
}
)"
;
...
...
tests/cc/test_perf_event.cc
0 → 100644
View file @
35771cf9
/*
* Copyright (c) 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <linux/perf_event.h>
#include <linux/version.h>
#include <unistd.h>
#include <string>
#include "BPF.h"
#include "catch.hpp"
TEST_CASE
(
"test read perf event"
,
"[bpf_perf_event]"
)
{
// The basic bpf_perf_event_read is supported since Kernel 4.3. However in that
// version it only supported HARDWARE and RAW events. On the other hand, our
// tests running on Jenkins won't have availiable HARDWARE counters since they
// are running on VMs. The support of other types of events such as SOFTWARE are
// only added since Kernel 4.13, hence we can only run the test since that.
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 13, 0)
const
std
::
string
BPF_PROGRAM
=
R"(
BPF_PERF_ARRAY(cnt, NUM_CPUS);
BPF_HASH(val, int, u64, 1);
BPF_HASH(ret, int, int, 1);
BPF_HASH(counter, int, struct bpf_perf_event_value, 1);
int on_sys_getuid(void *ctx) {
int zero = 0;
u64 v = cnt.perf_read(CUR_CPU_IDENTIFIER);
if (((s64)v < 0) && ((s64)v > -256))
return 0;
val.update(&zero, &v);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
u32 cpu = bpf_get_smp_processor_id();
struct bpf_perf_event_value c = {0};
int r = cnt.perf_counter_value(cpu, &c, sizeof(c));
ret.update(&zero, &r);
counter.update(&zero, &c);
#endif
return 0;
}
)"
;
ebpf
::
BPF
bpf
;
ebpf
::
StatusTuple
res
(
0
);
res
=
bpf
.
init
(
BPF_PROGRAM
,
{
"-DNUM_CPUS="
+
std
::
to_string
(
sysconf
(
_SC_NPROCESSORS_ONLN
))},
{});
REQUIRE
(
res
.
code
()
==
0
);
res
=
bpf
.
open_perf_event
(
"cnt"
,
PERF_TYPE_SOFTWARE
,
PERF_COUNT_SW_CPU_CLOCK
);
REQUIRE
(
res
.
code
()
==
0
);
res
=
bpf
.
attach_kprobe
(
"sys_getuid"
,
"on_sys_getuid"
);
REQUIRE
(
res
.
code
()
==
0
);
REQUIRE
(
getuid
()
>=
0
);
res
=
bpf
.
detach_kprobe
(
"sys_getuid"
);
REQUIRE
(
res
.
code
()
==
0
);
res
=
bpf
.
close_perf_event
(
"cnt"
);
REQUIRE
(
res
.
code
()
==
0
);
auto
val
=
bpf
.
get_hash_table
<
int
,
uint64_t
>
(
"val"
);
REQUIRE
(
val
[
0
]
>=
0
);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
auto
counter_table
=
bpf
.
get_hash_table
<
int
,
struct
bpf_perf_event_value
>
(
"counter"
);
auto
counter
=
counter_table
[
0
];
auto
ret
=
bpf
.
get_hash_table
<
int
,
int
>
(
"ret"
);
REQUIRE
(
ret
[
0
]
==
0
);
REQUIRE
(
counter
.
counter
>=
0
);
REQUIRE
(
counter
.
enabled
>
0
);
REQUIRE
(
counter
.
running
>=
0
);
REQUIRE
(
counter
.
running
<=
counter
.
enabled
);
#endif
}
TEST_CASE
(
"test attach perf event"
,
"[bpf_perf_event]"
)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
const
std
::
string
BPF_PROGRAM
=
R"(
BPF_HASH(pid, int, u64, 1);
BPF_HASH(ret, int, int, 1);
BPF_HASH(counter, int, struct bpf_perf_event_value, 1);
int on_event(void *ctx) {
int zero = 0;
u64 p = bpf_get_current_pid_tgid();
pid.update(&zero, &p);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
struct bpf_perf_event_value c = {0};
int r = bpf_perf_prog_read_value(ctx, &c, sizeof(c));
ret.update(&zero, &r);
counter.update(&zero, &c);
#endif
return 0;
}
)"
;
ebpf
::
BPF
bpf
;
ebpf
::
StatusTuple
res
(
0
);
res
=
bpf
.
init
(
BPF_PROGRAM
);
REQUIRE
(
res
.
code
()
==
0
);
res
=
bpf
.
attach_perf_event
(
PERF_TYPE_SOFTWARE
,
PERF_COUNT_SW_CPU_CLOCK
,
"on_event"
,
0
,
1000
);
REQUIRE
(
res
.
code
()
==
0
);
sleep
(
1
);
res
=
bpf
.
detach_perf_event
(
PERF_TYPE_SOFTWARE
,
PERF_COUNT_SW_CPU_CLOCK
);
REQUIRE
(
res
.
code
()
==
0
);
auto
pid
=
bpf
.
get_hash_table
<
int
,
uint64_t
>
(
"pid"
);
REQUIRE
(
pid
[
0
]
>=
0
);
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 15, 0)
auto
counter_table
=
bpf
.
get_hash_table
<
int
,
struct
bpf_perf_event_value
>
(
"counter"
);
auto
counter
=
counter_table
[
0
];
auto
ret
=
bpf
.
get_hash_table
<
int
,
int
>
(
"ret"
);
REQUIRE
(
ret
[
0
]
==
0
);
REQUIRE
(
counter
.
counter
>=
0
);
REQUIRE
(
counter
.
enabled
>=
1000000000
);
REQUIRE
(
counter
.
running
>=
0
);
REQUIRE
(
counter
.
running
<=
counter
.
enabled
);
#endif
}
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