File Index
Files and directories
Sign in
FILE

strncmp_bench.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/bpf/progs/strncmp_bench.c C
 1// SPDX-License-Identifier: GPL-2.0
 2/* Copyright (C) 2021. Huawei Technologies Co., Ltd */
 3#include <linux/types.h>
 4#include <linux/bpf.h>
 5#include <bpf/bpf_helpers.h>
 6#include <bpf/bpf_tracing.h>
 7
 8#define STRNCMP_STR_SZ 4096
 9
10/* Will be updated by benchmark before program loading */
11const volatile unsigned int cmp_str_len = 1;
12const char target[STRNCMP_STR_SZ];
13
14long hits = 0;
15char str[STRNCMP_STR_SZ];
16
17char _license[] SEC("license") = "GPL";
18
19static __always_inline int local_strncmp(const char *s1, unsigned int sz,
20					 const char *s2)
21{
22	int ret = 0;
23	unsigned int i;
24
25	for (i = 0; i < sz; i++) {
26		/* E.g. 0xff > 0x31 */
27		ret = (unsigned char)s1[i] - (unsigned char)s2[i];
28		if (ret || !s1[i])
29			break;
30	}
31
32	return ret;
33}
34
35SEC("tp/syscalls/sys_enter_getpgid")
36int strncmp_no_helper(void *ctx)
37{
38	const char *target_str = target;
39
40	barrier_var(target_str);
41	if (local_strncmp(str, cmp_str_len + 1, target_str) < 0)
42		__sync_add_and_fetch(&hits, 1);
43	return 0;
44}
45
46SEC("tp/syscalls/sys_enter_getpgid")
47int strncmp_helper(void *ctx)
48{
49	if (bpf_strncmp(str, cmp_str_len + 1, target) < 0)
50		__sync_add_and_fetch(&hits, 1);
51	return 0;
52}
53
~/ Read-only file preview.