Skip to content

Commit

Permalink
add read write result
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 16, 2024
1 parent 87a8901 commit 5cc74e6
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 38 deletions.
42 changes: 38 additions & 4 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,42 @@ You can use python script to run the benchmark:
python3 benchmark/tools/driving.py
```

## Benchmark runner
## Results on another machine

### Usage
- `make -C ./benchmark`
- `python3 ./benchmark/run_benchmark.py`
kernel:

```txt
Benchmarking __bench_uprobe_uretprobe in thread 1
Average time usage 3060.196770 ns, iter 100000 times
Benchmarking __bench_uretprobe in thread 1
Average time usage 2958.493390 ns, iter 100000 times
Benchmarking __bench_uprobe in thread 1
Average time usage 1910.731360 ns, iter 100000 times
Benchmarking __bench_read in thread 1
Average time usage 1957.552190 ns, iter 100000 times
Benchmarking __bench_write in thread 1
Average time usage 1955.735460 ns, iter 100000 times
```

Userspace:

```txt
Benchmarking __bench_uprobe_uretprobe in thread 1
Average time usage 412.607790 ns, iter 100000 times
Benchmarking __bench_uretprobe in thread 1
Average time usage 389.096230 ns, iter 100000 times
Benchmarking __bench_uprobe in thread 1
Average time usage 387.022160 ns, iter 100000 times
Benchmarking __bench_read in thread 1
Average time usage 415.350530 ns, iter 100000 times
Benchmarking __bench_write in thread 1
Average time usage 414.350230 ns, iter 100000 times
```
12 changes: 6 additions & 6 deletions benchmark/hash_map/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# benchmark of hash maps

- __benchmark_test_function1: hashmap bpf_map_lookup_elem
- __benchmark_test_function2: hashmap bpf_map_delete_elem
- __benchmark_test_function3: hashmap bpf_map_update_elem
- __bench_uprobe_uretprobe: hashmap bpf_map_lookup_elem
- __bench_uretprobe: hashmap bpf_map_delete_elem
- __bench_probe: hashmap bpf_map_update_elem

run the uprobe:

Expand All @@ -23,17 +23,17 @@ in another terminal, run the benchmark:
```console
$ LD_PRELOAD=build/runtime/agent/libbpftime-agent.so benchmark/test

Benchmarking __benchmark_test_function1
Benchmarking __bench_uprobe_uretprobe
a[b] + c for 100000 times
Elapsed time: 0.038217773 seconds
Average time usage 382.177730 ns

Benchmarking __benchmark_test_function2
Benchmarking __bench_uretprobe
a[b] + c for 100000 times
Elapsed time: 0.020004455 seconds
Average time usage 200.044550 ns

Benchmarking __benchmark_test_function3
Benchmarking __bench_probe
a[b] + c for 100000 times
Elapsed time: 0.047916014 seconds
Average time usage 479.160140 ns
Expand Down
6 changes: 3 additions & 3 deletions benchmark/hash_map/uprobe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct {
__type(value, u64);
} libc_malloc_calls_total SEC(".maps");

SEC("uprobe/benchmark/test:__benchmark_test_function3")
SEC("uprobe/benchmark/test:__bench_probe")
int test_update(struct pt_regs *ctx)
{
u32 key = 0;
Expand All @@ -20,7 +20,7 @@ int test_update(struct pt_regs *ctx)
return 0;
}

SEC("uprobe/benchmark/test:__benchmark_test_function2")
SEC("uprobe/benchmark/test:__bench_uretprobe")
int test_delete(struct pt_regs *ctx)
{
u32 key = 0;
Expand All @@ -30,7 +30,7 @@ int test_delete(struct pt_regs *ctx)
return 0;
}

SEC("uprobe/benchmark/test:__benchmark_test_function1")
SEC("uprobe/benchmark/test:__bench_uprobe_uretprobe")
int test_lookup(struct pt_regs *ctx)
{
u32 key = 0;
Expand Down
4 changes: 2 additions & 2 deletions benchmark/run_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def run_userspace_uprobe_test():
server.stdout,
should_exit,
"SERVER",
(server_start_cb, "__benchmark_test_function3 is for uprobe only"),
(server_start_cb, "__bench_probe is for uprobe only"),
)
)
await server_start_cb.wait()
Expand Down Expand Up @@ -131,7 +131,7 @@ async def run_kernel_uprobe_test():
server.stdout,
should_exit,
"SERVER",
(server_start_cb, "__benchmark_test_function3 is for uprobe only"),
(server_start_cb, "__bench_probe is for uprobe only"),
)
)
await server_start_cb.wait()
Expand Down
33 changes: 23 additions & 10 deletions benchmark/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,37 @@
#include <stdint.h>
#include <pthread.h>

__attribute_noinline__ uint64_t __benchmark_test_function3(const char *a, int b,
__attribute_noinline__ uint64_t __bench_read(char *a, int b,
uint64_t c)
{
return a[b] + c;
}

__attribute_noinline__ uint64_t __benchmark_test_function2(const char *a, int b,
__attribute_noinline__ uint64_t __bench_write(char *a, int b,
uint64_t c)
{
static int i = 0;
__sync_fetch_and_add(&i, 1);
return a[b] + c;
}

__attribute_noinline__ uint64_t __benchmark_test_function1(const char *a, int b,
__attribute_noinline__ uint64_t __bench_uprobe(char *a, int b,
uint64_t c)
{
return a[b] + c;
}

typedef uint64_t (*benchmark_test_function_t)(const char *, int, uint64_t);
__attribute_noinline__ uint64_t __bench_uretprobe(char *a, int b,
uint64_t c)
{
return a[b] + c;
}

__attribute_noinline__ uint64_t __bench_uprobe_uretprobe(char *a, int b,
uint64_t c)
{
return a[b] + c;
}

typedef uint64_t (*benchmark_test_function_t)(char *, int, uint64_t);

void start_timer(struct timespec *start_time)
{
Expand Down Expand Up @@ -53,9 +63,10 @@ static double get_function_time(benchmark_test_function_t func, int iter)
// The timespec struct holds seconds and nanoseconds
struct timespec start_time, end_time;
start_timer(&start_time);
char buffer[20] = "hello world";
// test base line
for (int i = 0; i < iter; i++) {
func("hello", i % 4, i);
func(buffer, i % 4, i);
}
end_timer(&end_time);
double time = get_elapsed_time(start_time, end_time);
Expand Down Expand Up @@ -83,9 +94,11 @@ void *run_bench_functions(void *id_ptr)
{
int id = *(int *)id_ptr;
printf("id: %d\n", id);
do_benchmark_func(__benchmark_test_function1, iter, id);
do_benchmark_func(__benchmark_test_function2, iter, id);
do_benchmark_func(__benchmark_test_function3, iter, id);
do_benchmark_func(__bench_uprobe_uretprobe, iter, id);
do_benchmark_func(__bench_uretprobe, iter, id);
do_benchmark_func(__bench_uprobe, iter, id);
do_benchmark_func(__bench_read, iter, id);
do_benchmark_func(__bench_write, iter, id);
return NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions benchmark/test_embed.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void end_timer()
clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
}

__attribute_noinline__ uint64_t __benchmark_test_function3(const char *a, int b,
__attribute_noinline__ uint64_t __bench_probe(const char *a, int b,
uint64_t c)
{
return a[b] + c;
Expand All @@ -93,7 +93,7 @@ uint64_t test_func_wrapper(const char *a, int b, uint64_t c)
PT_REGS_PARM3(&regs) = c;
ebpf_exec(begin_vm, &regs, sizeof(regs), &ret);
}
uint64_t hook_func_ret = __benchmark_test_function3(a, b, c);
uint64_t hook_func_ret = __bench_probe(a, b, c);
if (enable_ebpf) {
memset(&regs, 0, sizeof(regs));
PT_REGS_PARM1(&regs) = hook_func_ret;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/uprobe/uprobe-override.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char **argv)
}
err = bpf_prog_attach_uprobe_with_override(
bpf_program__fd(skel->progs.do_uprobe_override_patch), "benchmark/test",
"__benchmark_test_function1");
"__bench_uprobe_uretprobe");
if (err) {
fprintf(stderr, "Failed to attach BPF program\n");
goto cleanup;
Expand Down
30 changes: 23 additions & 7 deletions benchmark/uprobe/uprobe.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,41 @@
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

SEC("uprobe/benchmark/test:__benchmark_test_function3")
int BPF_UPROBE(__benchmark_test_function3, const char *a, int b, uint64_t c)
SEC("uprobe/benchmark/test:__bench_write")
int BPF_UPROBE(__bench_write, char *a, int b, uint64_t c)
{
char buffer[5] = "text";
bpf_probe_write_user(a, buffer, sizeof(buffer));
return b + c;
}

SEC("uretprobe/benchmark/test:__benchmark_test_function2")
int BPF_URETPROBE(__benchmark_test_function2, int ret)
SEC("uprobe/benchmark/test:__bench_read")
int BPF_UPROBE(__bench_read, char *a, int b, uint64_t c)
{
char buffer[5];
int res = bpf_probe_read_user(buffer, sizeof(buffer), a);
return b + c + res + buffer[1];
}

SEC("uprobe/benchmark/test:__bench_uprobe")
int BPF_UPROBE(__bench_uprobe, char *a, int b, uint64_t c)
{
return b + c;
}

SEC("uretprobe/benchmark/test:__bench_uretprobe")
int BPF_URETPROBE(__bench_uretprobe, int ret)
{
return ret;
}

SEC("uprobe/benchmark/test:__benchmark_test_function1")
int BPF_UPROBE(__benchmark_test_function1_1, const char *a, int b, uint64_t c)
SEC("uprobe/benchmark/test:__bench_uprobe_uretprobe")
int BPF_UPROBE(__bench_uprobe_uretprobe_1, char *a, int b, uint64_t c)
{
return b + c;
}

SEC("uretprobe/benchmark/test:__benchmark_test_function1")
SEC("uretprobe/benchmark/test:__bench_uprobe_uretprobe")
int BPF_URETPROBE(__benchmark_test_function_1_2, int ret)
{
return ret;
Expand Down
6 changes: 3 additions & 3 deletions benchmark/uprobe/uprobe.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ int main(int argc, char **argv)
}

printf("Successfully started! Press Ctrl+C to stop.\n");
printf("__benchmark_test_function1 is for both uprobe and uretprobe\n");
printf("__benchmark_test_function2 is for uretprobe only\n");
printf("__benchmark_test_function3 is for uprobe only\n");
printf("__bench_uprobe_uretprobe is for both uprobe and uretprobe\n");
printf("__bench_uretprobe is for uretprobe only\n");
printf("__bench_probe is for uprobe only\n");
fflush(stdout);
while (!exiting) {
sleep(1);
Expand Down

0 comments on commit 5cc74e6

Please sign in to comment.