Skip to content

Commit

Permalink
selftests/bpf: Move spintest from samples/bpf to selftests
Browse files Browse the repository at this point in the history
According to the discussions in [1] everything from the samples/bpf
directory should be moved out or deleted.

Move the spintest program from BPF samples to BPF selftests.
There are no functional changes. BPF skeleton has been used for loading,
etc.

[1] https://lore.kernel.org/all/CAEf4BzYv3ONhy-JnQUtknzgBSK0gpm9GBJYtbAiJQe50_eX7Uw@mail.gmail.com/

Signed-off-by: Puranjay Mohan <[email protected]>
  • Loading branch information
puranjaymohan authored and Nobody committed Apr 1, 2022
1 parent a64fd67 commit 1f31601
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
3 changes: 0 additions & 3 deletions samples/bpf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ tprogs-y += test_probe_write_user
tprogs-y += trace_output
tprogs-y += lathist
tprogs-y += offwaketime
tprogs-y += spintest
tprogs-y += map_perf_test
tprogs-y += test_overhead
tprogs-y += test_cgrp2_array_pin
Expand Down Expand Up @@ -86,7 +85,6 @@ test_probe_write_user-objs := test_probe_write_user_user.o
trace_output-objs := trace_output_user.o
lathist-objs := lathist_user.o
offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS)
spintest-objs := spintest_user.o $(TRACE_HELPERS)
map_perf_test-objs := map_perf_test_user.o
test_overhead-objs := test_overhead_user.o
test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
Expand Down Expand Up @@ -144,7 +142,6 @@ always-y += tcbpf1_kern.o
always-y += tc_l2_redirect_kern.o
always-y += lathist_kern.o
always-y += offwaketime_kern.o
always-y += spintest_kern.o
always-y += map_perf_test_kern.o
always-y += test_overhead_tp_kern.o
always-y += test_overhead_raw_tp_kern.o
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,43 @@
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include <test_progs.h>
#include "trace_helpers.h"
#include "test_spin.skel.h"

int main(int ac, char **argv)
void test_spin(void)
{
char filename[256], symbol[256];
struct bpf_object *obj = NULL;
struct bpf_link *links[20];
long key, next_key, value;
struct bpf_program *prog;
struct test_spin *skel;
int map_fd, i, j = 0;
const char *section;
struct ksym *sym;
char symbol[256];
int err;

if (load_kallsyms()) {
printf("failed to process /proc/kallsyms\n");
return 2;
}
err = load_kallsyms();
if (!ASSERT_OK(err, "load_kallsyms"))
return;
skel = test_spin__open_and_load();

snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
obj = bpf_object__open_file(filename, NULL);
if (libbpf_get_error(obj)) {
fprintf(stderr, "ERROR: opening BPF object file failed\n");
obj = NULL;
goto cleanup;
}
if (!ASSERT_OK_PTR(skel, "test_spin__open_and_load"))
return;

/* load BPF program */
if (bpf_object__load(obj)) {
fprintf(stderr, "ERROR: loading BPF object file failed\n");
goto cleanup;
}

map_fd = bpf_object__find_map_fd_by_name(obj, "my_map");
if (map_fd < 0) {
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
goto cleanup;
}
map_fd = bpf_map__fd(skel->maps.my_map);

bpf_object__for_each_program(prog, obj) {
bpf_object__for_each_program(prog, skel->obj) {
section = bpf_program__section_name(prog);
if (sscanf(section, "kprobe/%s", symbol) != 1)
continue;

/* Attach prog only when symbol exists */
if (ksym_get_addr(symbol)) {
links[j] = bpf_program__attach(prog);
if (libbpf_get_error(links[j])) {
err = libbpf_get_error(links[j]);
if (!ASSERT_OK(err, "bpf_program__attach")) {
fprintf(stderr, "bpf_program__attach failed\n");
links[j] = NULL;
goto cleanup;
Expand Down Expand Up @@ -89,5 +79,4 @@ int main(int ac, char **argv)
bpf_link__destroy(links[j]);

bpf_object__close(obj);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*/
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/version.h>
#include <uapi/linux/bpf.h>
#include <uapi/linux/perf_event.h>
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

#ifndef PERF_MAX_STACK_DEPTH
#define PERF_MAX_STACK_DEPTH 127
#endif

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, long);
Expand All @@ -27,8 +27,8 @@ struct {

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
__uint(key_size, sizeof(u32));
__uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64));
__uint(key_size, sizeof(__u32));
__uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(__u64));
__uint(max_entries, 10000);
} stackmap SEC(".maps");

Expand Down Expand Up @@ -66,4 +66,3 @@ SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16)
SEC("kprobe/htab_map_alloc")PROG(p17)

char _license[] SEC("license") = "GPL";
u32 _version SEC("version") = LINUX_VERSION_CODE;

0 comments on commit 1f31601

Please sign in to comment.