Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2020 Facebook
0003 
0004 #include <linux/bpf.h>
0005 #include <stdint.h>
0006 #include <bpf/bpf_helpers.h>
0007 #include "bpf_misc.h"
0008 
0009 char _license[] SEC("license") = "GPL";
0010 
0011 struct {
0012     __uint(type, BPF_MAP_TYPE_RINGBUF);
0013 } ringbuf SEC(".maps");
0014 
0015 const volatile int batch_cnt = 0;
0016 const volatile long use_output = 0;
0017 
0018 long sample_val = 42;
0019 long dropped __attribute__((aligned(128))) = 0;
0020 
0021 const volatile long wakeup_data_size = 0;
0022 
0023 static __always_inline long get_flags()
0024 {
0025     long sz;
0026 
0027     if (!wakeup_data_size)
0028         return 0;
0029 
0030     sz = bpf_ringbuf_query(&ringbuf, BPF_RB_AVAIL_DATA);
0031     return sz >= wakeup_data_size ? BPF_RB_FORCE_WAKEUP : BPF_RB_NO_WAKEUP;
0032 }
0033 
0034 SEC("fentry/" SYS_PREFIX "sys_getpgid")
0035 int bench_ringbuf(void *ctx)
0036 {
0037     long *sample, flags;
0038     int i;
0039 
0040     if (!use_output) {
0041         for (i = 0; i < batch_cnt; i++) {
0042             sample = bpf_ringbuf_reserve(&ringbuf,
0043                                  sizeof(sample_val), 0);
0044             if (!sample) {
0045                 __sync_add_and_fetch(&dropped, 1);
0046             } else {
0047                 *sample = sample_val;
0048                 flags = get_flags();
0049                 bpf_ringbuf_submit(sample, flags);
0050             }
0051         }
0052     } else {
0053         for (i = 0; i < batch_cnt; i++) {
0054             flags = get_flags();
0055             if (bpf_ringbuf_output(&ringbuf, &sample_val,
0056                            sizeof(sample_val), flags))
0057                 __sync_add_and_fetch(&dropped, 1);
0058         }
0059     }
0060     return 0;
0061 }