summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/libarena/benchs/bench_malloc.bpf.c
blob: e06397dc9a40dfacc3dcc731a43282dd36ed1398 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: LGPL-2.1 OR BSD-2-Clause
/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */
#include <libarena/common.h>

#include <libarena/asan.h>
#include <libarena/buddy.h>

u32 bench_alloc_size;
u32 bench_nallocs;
long bench_hits;
long bench_duration_ns;

SEC("syscall")
int bench_malloc(void)
{
	void __arena *mem;
	u64 start_ns;
	u32 i;

	start_ns = bpf_ktime_get_ns();
	for (i = zero; i < bench_nallocs && can_loop; i++) {
		mem = arena_malloc(bench_alloc_size);
		if (!mem)
			return -ENOMEM;
	}

	__sync_add_and_fetch(&bench_duration_ns,
			     bpf_ktime_get_ns() - start_ns);
	__sync_add_and_fetch(&bench_hits, i);
	return 0;
}

SEC("syscall")
int bench_calloc(void)
{
	void __arena *mem;
	u64 start_ns;
	u32 i;

	start_ns = bpf_ktime_get_ns();
	for (i = zero; i < bench_nallocs && can_loop; i++) {
		mem = arena_calloc(1, bench_alloc_size);
		if (!mem)
			return -ENOMEM;
	}

	__sync_add_and_fetch(&bench_duration_ns,
			     bpf_ktime_get_ns() - start_ns);
	__sync_add_and_fetch(&bench_hits, i);
	return 0;
}