1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2020 Facebook 3 4 #include <stdbool.h> 5 #include <linux/bpf.h> 6 #include <stdint.h> 7 #include <bpf/bpf_helpers.h> 8 #include "bpf_misc.h" 9 10 char _license[] SEC("license") = "GPL"; 11 12 struct { 13 __uint(type, BPF_MAP_TYPE_RINGBUF); 14 } ringbuf SEC(".maps"); 15 16 const volatile int batch_cnt = 0; 17 const volatile long use_output = 0; 18 const volatile bool bench_producer = false; 19 20 long sample_val = 42; 21 long dropped __attribute__((aligned(128))) = 0; 22 long hits __attribute__((aligned(128))) = 0; 23 24 const volatile long wakeup_data_size = 0; 25 26 static __always_inline long get_flags() 27 { 28 long sz; 29 30 if (bench_producer) 31 return BPF_RB_NO_WAKEUP; 32 33 if (!wakeup_data_size) 34 return 0; 35 36 sz = bpf_ringbuf_query(&ringbuf, BPF_RB_AVAIL_DATA); 37 return sz >= wakeup_data_size ? BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP; 38 } 39 40 SEC("fentry/" SYS_PREFIX "sys_getpgid") 41 int bench_ringbuf(void *ctx) 42 { 43 long *sample, flags; 44 int i; 45 46 if (!use_output) { 47 for (i = 0; i < batch_cnt; i++) { 48 sample = bpf_ringbuf_reserve(&ringbuf, 49 sizeof(sample_val), 0); 50 if (!sample) { 51 __sync_add_and_fetch(&dropped, 1); 52 } else { 53 *sample = sample_val; 54 flags = get_flags(); 55 bpf_ringbuf_submit(sample, flags); 56 if (bench_producer) 57 __sync_add_and_fetch(&hits, 1); 58 } 59 } 60 } else { 61 for (i = 0; i < batch_cnt; i++) { 62 flags = get_flags(); 63 if (bpf_ringbuf_output(&ringbuf, &sample_val, 64 sizeof(sample_val), flags)) 65 __sync_add_and_fetch(&dropped, 1); 66 else if (bench_producer) 67 __sync_add_and_fetch(&hits, 1); 68 69 } 70 } 71 return 0; 72 } 73