strncmp_bench.c (1079B)
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 if (local_strncmp(str, cmp_str_len + 1, target) < 0) 39 __sync_add_and_fetch(&hits, 1); 40 return 0; 41} 42 43SEC("tp/syscalls/sys_enter_getpgid") 44int strncmp_helper(void *ctx) 45{ 46 if (bpf_strncmp(str, cmp_str_len + 1, target) < 0) 47 __sync_add_and_fetch(&hits, 1); 48 return 0; 49} 50