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