From nobody Wed Apr 29 08:09:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E84CDCCA473 for ; Fri, 10 Jun 2022 02:33:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242557AbiFJCdi (ORCPT ); Thu, 9 Jun 2022 22:33:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242290AbiFJCd1 (ORCPT ); Thu, 9 Jun 2022 22:33:27 -0400 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93A472DA9C for ; Thu, 9 Jun 2022 19:33:25 -0700 (PDT) Received: by mail-pg1-x529.google.com with SMTP id f65so13326520pgc.7 for ; Thu, 09 Jun 2022 19:33:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mF4zImcP1gOdN8UvsBrqYtbmiABYejHvUvi0EZlW7oI=; b=hgWxeUrVNLD6xuo/sbHEeTPmXp2NhZny0ThVZKmw6RtWaSwUUbNpi4Z3Y1UdSL6b2M Tyta/BfIoBl9kOl2B/zs9N01bG8tebru/PXcZVTVYKmLpxr4KgRCBN01PyVINbeLGq8G +xk9qR/DHuSBXRXubnx2WMMk5XF7ghs4W1Hnn9KvEpqaBGW2vS7qaPcbBDAzDPaMNKb+ 4lkof0i8/ad2vw66cpalcjGxJoIoTFyyWjArfLLVtBweoiQhaqO9W6BzXkjjqZ7qcgyD ILHt2s74NhcSSB/4S843Mw3susyrmFUP/WRTUkFd1VqMlgwXijm/W297orMx59aVDDxu xp6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mF4zImcP1gOdN8UvsBrqYtbmiABYejHvUvi0EZlW7oI=; b=zjPNvU98MKIOc0CEvR4GhGrfjZEGcIeo5xvkQc8eT3BbbczYWsnZnco9FeTgYog3Av 6xj8VQbbmapkaxp/wDS8a33rJJb7VGbOmRWfq9+F4bKqZuWpLeBIuRjH2Ex+9NgeKoDd iyy0Ws/sqI+YbUhAIsflGwEm/zUJ1rZPuMJYkcAUtBSadZblkvf7Nj6gmE3O+ZIckXKp txbuxCA1vL5MXGQNbKZ8BD+BJEH4s1X8OIIUpwsCGnAsuHKruysq4j7nEoqMXqqAqxrJ vBvZSa8gEgtYr7Mgl/1L0de10DPJowzqAIYEGD2ua3Itm8eGcu+yQLOD0/a2VmM/FdeC BWBg== X-Gm-Message-State: AOAM531a0Aze3GNd6QBkogpl7vBr6bppOsZ8SeVm6z6HC/xsIotc5ZTr dtxlXSNL3nLq+TPDJenJmmr+3w== X-Google-Smtp-Source: ABdhPJzrHcDnf8fUQlMQt/1PC+8KzbX6TvprSOKOfQpymsCfKFAZv/REJxP3/uLeNIABQfdTs7g/9A== X-Received: by 2002:a05:6a00:1811:b0:51b:fec8:be7b with SMTP id y17-20020a056a00181100b0051bfec8be7bmr29391769pfa.22.1654828405113; Thu, 09 Jun 2022 19:33:25 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id o19-20020a170903009300b001620db30cd6sm17432481pld.201.2022.06.09.19.33.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Jun 2022 19:33:24 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, wangdongdong.6@bytedance.com, cong.wang@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH v6 1/2] bpf: avoid grabbing spin_locks of all cpus when no free elems Date: Fri, 10 Jun 2022 10:33:07 +0800 Message-Id: <20220610023308.93798-2-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220610023308.93798-1-zhoufeng.zf@bytedance.com> References: <20220610023308.93798-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Feng Zhou This patch use head->first in pcpu_freelist_head to check freelist having free or not. If having, grab spin_lock, or check next cpu's freelist. Before patch: hash_map performance ./map_perf_test 1 0:hash_map_perf pre-alloc 1043397 events per sec ... The average of the test results is around 1050000 events per sec. hash_map the worst: no free ./run_bench_bpf_hashmap_full_update.sh Setting up benchmark 'bpf-hashmap-ful-update'... Benchmark 'bpf-hashmap-ful-update' started. 1:hash_map_full_perf 15687 events per sec ... The average of the test results is around 16000 events per sec. ftrace trace: 0) | htab_map_update_elem() { 0) | __pcpu_freelist_pop() { 0) | _raw_spin_lock() 0) | _raw_spin_unlock() 0) | ... 0) + 25.188 us | } 0) + 28.439 us | } The test machine is 16C, trying to get spin_lock 17 times, in addition to 16c, there is an extralist. after patch: hash_map performance ./map_perf_test 1 0:hash_map_perf pre-alloc 1053298 events per sec ... The average of the test results is around 1050000 events per sec. hash_map worst: no free ./run_bench_bpf_hashmap_full_update.sh Setting up benchmark 'bpf-hashmap-ful-update'... Benchmark 'bpf-hashmap-ful-update' started. 1:hash_map_full_perf 555830 events per sec ... The average of the test results is around 550000 events per sec. ftrace trace: 0) | htab_map_update_elem() { 0) | alloc_htab_elem() { 0) 0.586 us | __pcpu_freelist_pop(); 0) 0.945 us | } 0) 8.669 us | } It can be seen that after adding this patch, the map performance is almost not degraded, and when free=3D0, first check head->first instead of directly acquiring spin_lock. Co-developed-by: Chengming Zhou Signed-off-by: Chengming Zhou Signed-off-by: Feng Zhou --- kernel/bpf/percpu_freelist.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/percpu_freelist.c b/kernel/bpf/percpu_freelist.c index 3d897de89061..00b874c8e889 100644 --- a/kernel/bpf/percpu_freelist.c +++ b/kernel/bpf/percpu_freelist.c @@ -31,7 +31,7 @@ static inline void pcpu_freelist_push_node(struct pcpu_fr= eelist_head *head, struct pcpu_freelist_node *node) { node->next =3D head->first; - head->first =3D node; + WRITE_ONCE(head->first, node); } =20 static inline void ___pcpu_freelist_push(struct pcpu_freelist_head *head, @@ -130,14 +130,17 @@ static struct pcpu_freelist_node *___pcpu_freelist_po= p(struct pcpu_freelist *s) orig_cpu =3D cpu =3D raw_smp_processor_id(); while (1) { head =3D per_cpu_ptr(s->freelist, cpu); + if (!READ_ONCE(head->first)) + goto next_cpu; raw_spin_lock(&head->lock); node =3D head->first; if (node) { - head->first =3D node->next; + WRITE_ONCE(head->first, node->next); raw_spin_unlock(&head->lock); return node; } raw_spin_unlock(&head->lock); +next_cpu: cpu =3D cpumask_next(cpu, cpu_possible_mask); if (cpu >=3D nr_cpu_ids) cpu =3D 0; @@ -146,10 +149,12 @@ static struct pcpu_freelist_node *___pcpu_freelist_po= p(struct pcpu_freelist *s) } =20 /* per cpu lists are all empty, try extralist */ + if (!READ_ONCE(s->extralist.first)) + return NULL; raw_spin_lock(&s->extralist.lock); node =3D s->extralist.first; if (node) - s->extralist.first =3D node->next; + WRITE_ONCE(s->extralist.first, node->next); raw_spin_unlock(&s->extralist.lock); return node; } @@ -164,15 +169,18 @@ ___pcpu_freelist_pop_nmi(struct pcpu_freelist *s) orig_cpu =3D cpu =3D raw_smp_processor_id(); while (1) { head =3D per_cpu_ptr(s->freelist, cpu); + if (!READ_ONCE(head->first)) + goto next_cpu; if (raw_spin_trylock(&head->lock)) { node =3D head->first; if (node) { - head->first =3D node->next; + WRITE_ONCE(head->first, node->next); raw_spin_unlock(&head->lock); return node; } raw_spin_unlock(&head->lock); } +next_cpu: cpu =3D cpumask_next(cpu, cpu_possible_mask); if (cpu >=3D nr_cpu_ids) cpu =3D 0; @@ -181,11 +189,11 @@ ___pcpu_freelist_pop_nmi(struct pcpu_freelist *s) } =20 /* cannot pop from per cpu lists, try extralist */ - if (!raw_spin_trylock(&s->extralist.lock)) + if (!READ_ONCE(s->extralist.first) || !raw_spin_trylock(&s->extralist.loc= k)) return NULL; node =3D s->extralist.first; if (node) - s->extralist.first =3D node->next; + WRITE_ONCE(s->extralist.first, node->next); raw_spin_unlock(&s->extralist.lock); return node; } --=20 2.20.1 From nobody Wed Apr 29 08:09:58 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA3FCC433EF for ; Fri, 10 Jun 2022 02:33:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240542AbiFJCdm (ORCPT ); Thu, 9 Jun 2022 22:33:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344794AbiFJCde (ORCPT ); Thu, 9 Jun 2022 22:33:34 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFE8A1660A8 for ; Thu, 9 Jun 2022 19:33:31 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id j7so22900756pjn.4 for ; Thu, 09 Jun 2022 19:33:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=h5wjvAfvmc3hQDHOJgTAUHot2+LF/XxihF052IFv5TI=; b=3VaZpqIvI/J1A3re8Htg7EDIVwMo6MeNwaxFNAqEj4v6OHRj8ES9oG9J5XtMQYG496 7fuaJxYoKdl+tN792AhGFiOy7Fpfc8Lr+haRviqE7FGOit3QVBh9Zmzl+k9zQNQMP2pi by/ZedpyoWU9HJSGy5YnZ3Zm/+U0Y85AFknQf7O8ttv1PicS3Qlz5/R1y3Nh9CZ6Npu2 BYZVTuHV28eow0HBJEnEmJHn/2hwO1XxtFIu0qxE4DmRKFSLUzK4rOjI+Ocd8Z+WDk0+ /pSV8ekFC2sP2zflSDn/kWpDXejSvoZsVzCq8tP8lHnzSYZiBUmRrM0y4GxkVl8c9Htt mQSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=h5wjvAfvmc3hQDHOJgTAUHot2+LF/XxihF052IFv5TI=; b=cY6jGV8jypRfIShXooLuBdMpKxOaKqqK9VBypul30T6Ph0eR1tRSEv6bqQ/RwfuDzV HOrCM8A5gujDT4Nw3en3vnVetoXi/ajoETn0LHn7ajAeMUqCKSHHPUDzBXrPyvsyy2RK +sec0fytEIqLrd08dOPpYWGSbk2cxQsWEAPPvlNIs0ZMGJfagW8NS8z9El3bXtapTeZR 80PuS2Whbb4Wnan4S8Od6QWqOBTXz+6A3xTUieEPw41yJqH8YlDAAGYWBCyT8jRBbk+D 4K/ZjQZE6z4AFS8pNFB2aKLT22GMVg6Go/wrgtCZSamq/1nqqakBraqbhIjwx4ZfFhkG mn9A== X-Gm-Message-State: AOAM533MCbLKkiGofznUuBogT7duQF6C9R9NX26Nfv+fc6ZMS7ryi3cg QsQw2OuI4fdiRoK+yDSQym4S2A== X-Google-Smtp-Source: ABdhPJwvi9kLXUS59J0d4sPwKM8mXEyRW0q+S6pGLzLrfPy+fr86kvrWBwDqb1tD8jUxoeJEn3wyww== X-Received: by 2002:a17:90b:3c4e:b0:1e3:36c8:8496 with SMTP id pm14-20020a17090b3c4e00b001e336c88496mr6322932pjb.82.1654828411413; Thu, 09 Jun 2022 19:33:31 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.225]) by smtp.gmail.com with ESMTPSA id o19-20020a170903009300b001620db30cd6sm17432481pld.201.2022.06.09.19.33.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Jun 2022 19:33:31 -0700 (PDT) From: Feng zhou To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, duanxiongchun@bytedance.com, songmuchun@bytedance.com, wangdongdong.6@bytedance.com, cong.wang@bytedance.com, zhouchengming@bytedance.com, zhoufeng.zf@bytedance.com Subject: [PATCH v6 2/2] selftest/bpf/benchs: Add bpf_map benchmark Date: Fri, 10 Jun 2022 10:33:08 +0800 Message-Id: <20220610023308.93798-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220610023308.93798-1-zhoufeng.zf@bytedance.com> References: <20220610023308.93798-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Feng Zhou Add benchmark for hash_map to reproduce the worst case that non-stop update when map's free is zero. Just like this: ./run_bench_bpf_hashmap_full_update.sh Setting up benchmark 'bpf-hashmap-ful-update'... Benchmark 'bpf-hashmap-ful-update' started. 1:hash_map_full_perf 555830 events per sec ... Signed-off-by: Feng Zhou --- tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 2 + .../benchs/bench_bpf_hashmap_full_update.c | 96 +++++++++++++++++++ .../run_bench_bpf_hashmap_full_update.sh | 11 +++ .../bpf/progs/bpf_hashmap_full_update_bench.c | 40 ++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_fu= ll_update.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_bpf_hashma= p_full_update.sh create mode 100644 tools/testing/selftests/bpf/progs/bpf_hashmap_full_upda= te_bench.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests= /bpf/Makefile index 2d3c8c8f558a..8ad7a733a505 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -560,6 +560,7 @@ $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.ske= l.h \ $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h $(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h $(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h +$(OUTPUT)/bench_bpf_hashmap_full_update.o: $(OUTPUT)/bpf_hashmap_full_upda= te_bench.skel.h $(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) $(OUTPUT)/bench: LDLIBS +=3D -lm $(OUTPUT)/bench: $(OUTPUT)/bench.o \ @@ -571,7 +572,8 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ $(OUTPUT)/bench_ringbufs.o \ $(OUTPUT)/bench_bloom_filter_map.o \ $(OUTPUT)/bench_bpf_loop.o \ - $(OUTPUT)/bench_strncmp.o + $(OUTPUT)/bench_strncmp.o \ + $(OUTPUT)/bench_bpf_hashmap_full_update.o $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@ =20 diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/= bpf/bench.c index f061cc20e776..d8aa62be996b 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -396,6 +396,7 @@ extern const struct bench bench_hashmap_with_bloom; extern const struct bench bench_bpf_loop; extern const struct bench bench_strncmp_no_helper; extern const struct bench bench_strncmp_helper; +extern const struct bench bench_bpf_hashmap_full_update; =20 static const struct bench *benchs[] =3D { &bench_count_global, @@ -430,6 +431,7 @@ static const struct bench *benchs[] =3D { &bench_bpf_loop, &bench_strncmp_no_helper, &bench_strncmp_helper, + &bench_bpf_hashmap_full_update, }; =20 static void setup_benchmark() diff --git a/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_upda= te.c b/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c new file mode 100644 index 000000000000..cec51e0ff4b8 --- /dev/null +++ b/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Bytedance */ + +#include +#include "bench.h" +#include "bpf_hashmap_full_update_bench.skel.h" +#include "bpf_util.h" + +/* BPF triggering benchmarks */ +static struct ctx { + struct bpf_hashmap_full_update_bench *skel; +} ctx; + +#define MAX_LOOP_NUM 10000 + +static void validate(void) +{ + if (env.consumer_cnt !=3D 1) { + fprintf(stderr, "benchmark doesn't support multi-consumer!\n"); + exit(1); + } +} + +static void *producer(void *input) +{ + while (true) { + /* trigger the bpf program */ + syscall(__NR_getpgid); + } + + return NULL; +} + +static void *consumer(void *input) +{ + return NULL; +} + +static void measure(struct bench_res *res) +{ +} + +static void setup(void) +{ + struct bpf_link *link; + int map_fd, i, max_entries; + + setup_libbpf(); + + ctx.skel =3D bpf_hashmap_full_update_bench__open_and_load(); + if (!ctx.skel) { + fprintf(stderr, "failed to open skeleton\n"); + exit(1); + } + + ctx.skel->bss->nr_loops =3D MAX_LOOP_NUM; + + link =3D bpf_program__attach(ctx.skel->progs.benchmark); + if (!link) { + fprintf(stderr, "failed to attach program!\n"); + exit(1); + } + + /* fill hash_map */ + map_fd =3D bpf_map__fd(ctx.skel->maps.hash_map_bench); + max_entries =3D bpf_map__max_entries(ctx.skel->maps.hash_map_bench); + for (i =3D 0; i < max_entries; i++) + bpf_map_update_elem(map_fd, &i, &i, BPF_ANY); +} + +void hashmap_report_final(struct bench_res res[], int res_cnt) +{ + unsigned int nr_cpus =3D bpf_num_possible_cpus(); + int i; + + for (i =3D 0; i < nr_cpus; i++) { + u64 time =3D ctx.skel->bss->percpu_time[i]; + + if (!time) + continue; + + printf("%d:hash_map_full_perf %lld events per sec\n", + i, ctx.skel->bss->nr_loops * 1000000000ll / time); + } +} + +const struct bench bench_bpf_hashmap_full_update =3D { + .name =3D "bpf-hashmap-ful-update", + .validate =3D validate, + .setup =3D setup, + .producer_thread =3D producer, + .consumer_thread =3D consumer, + .measure =3D measure, + .report_progress =3D NULL, + .report_final =3D hashmap_report_final, +}; diff --git a/tools/testing/selftests/bpf/benchs/run_bench_bpf_hashmap_full_= update.sh b/tools/testing/selftests/bpf/benchs/run_bench_bpf_hashmap_full_u= pdate.sh new file mode 100755 index 000000000000..1e2de838f9fa --- /dev/null +++ b/tools/testing/selftests/bpf/benchs/run_bench_bpf_hashmap_full_update.= sh @@ -0,0 +1,11 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source ./benchs/run_common.sh + +set -eufo pipefail + +nr_threads=3D`expr $(cat /proc/cpuinfo | grep "processor"| wc -l) - 1` +summary=3D$($RUN_BENCH -p $nr_threads bpf-hashmap-ful-update) +printf "$summary" +printf "\n" diff --git a/tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_benc= h.c b/tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c new file mode 100644 index 000000000000..56957557e3e1 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2022 Bytedance */ + +#include "vmlinux.h" +#include +#include "bpf_misc.h" + +char _license[] SEC("license") =3D "GPL"; + +#define MAX_ENTRIES 1000 + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __type(key, u32); + __type(value, u64); + __uint(max_entries, MAX_ENTRIES); +} hash_map_bench SEC(".maps"); + +u64 __attribute__((__aligned__(256))) percpu_time[256]; +u64 nr_loops; + +static int loop_update_callback(__u32 index, u32 *key) +{ + u64 init_val =3D 1; + + bpf_map_update_elem(&hash_map_bench, key, &init_val, BPF_ANY); + return 0; +} + +SEC("fentry/" SYS_PREFIX "sys_getpgid") +int benchmark(void *ctx) +{ + u32 cpu =3D bpf_get_smp_processor_id(); + u32 key =3D cpu + MAX_ENTRIES; + u64 start_time =3D bpf_ktime_get_ns(); + + bpf_loop(nr_loops, loop_update_callback, &key, 0); + percpu_time[cpu & 255] =3D bpf_ktime_get_ns() - start_time; + return 0; +} --=20 2.20.1