Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1259172lfc; Wed, 1 Jun 2022 13:21:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx9gel2Od/e/ufBtKGt8tmyMaN2spQfV0VkzU6k6GC9tQntiZK+5PQRSJqYlXtgeOE+wycJ X-Received: by 2002:a62:544:0:b0:51b:a90d:64d3 with SMTP id 65-20020a620544000000b0051ba90d64d3mr1348348pff.40.1654114870231; Wed, 01 Jun 2022 13:21:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654114870; cv=none; d=google.com; s=arc-20160816; b=otxn3MSmCQRZsfE614sk+tQz95q3JFos0N7wgRMSsBipWyskJx05Y1m61jLOYHXQ30 evfqvIc97fjcSsb6E/thGx+2YJ+Qhxdv1m0bHorB+CDtMIJjjoWpIazb6/PIv41qe6yQ 9UF5tK7FntzQUnmfkWtrpV0iZZcYs4+RItFGSggVjlTBHKKdT0DxxHKFuFxfpThU2/cd W8Y7tf4HW4o3zIQZrYE8TdW/R6QsZetVcZqHACLgyYkUovefQ5kfI+flArJTlGxNemjl 2Xa4Ut2d1hIwsAsTlwx0V3gnCnlMU2cIFmbIHjha5AtUsPR9liH9SqadBFV7yNXzhiWX S/2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=B9vwKfbKHzDOEY1WrU024PdJUi8+ydTG61v0lkvD1q4=; b=unE2U29wsZ3IiKxJEFNoc9nWJ++3160/3WYzNbQq42QX9Zq0ra27p2In8jbfdgM8rH ziBu2QVFyJLiZVnsH4Axn65Usaki8r9bd/X8MCOIHH7shvS3cgZtmT1gCvj4GQ24oAd6 AQIV/xhfQFuY+vrCp29/ZLnoxr3F3NYcwgzYw5lRJgqTMa0HB5DQYijTCh2+JpRa/pIB LkVTRhsVWZZA1jWfzR/j8sUgSXvygMMkqIhEdDAZ0+dxfDdxO5uf1hrGyOFjqOnnA6A+ CHaD8VlgI9QY+OAzBk9JarCCJQUAMAZvh4TAr5PAbAvpDc8oJdcUIK1PCfFev6FjKCQk +xrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=nDKbEO4t; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id m193-20020a633fca000000b003fa8e745a88si2892046pga.147.2022.06.01.13.21.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 13:21:10 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=nDKbEO4t; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D13EA196AAC; Wed, 1 Jun 2022 12:33:25 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242983AbiFAImW (ORCPT + 99 others); Wed, 1 Jun 2022 04:42:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348845AbiFAImQ (ORCPT ); Wed, 1 Jun 2022 04:42:16 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12989694AF for ; Wed, 1 Jun 2022 01:42:14 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id n13-20020a17090a394d00b001e30a60f82dso5515699pjf.5 for ; Wed, 01 Jun 2022 01:42:14 -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=B9vwKfbKHzDOEY1WrU024PdJUi8+ydTG61v0lkvD1q4=; b=nDKbEO4tZCSRRrSzh63hDu/iN425iuSK9EBeOyAKxNfyRH/f2IOm+NoRQDRvBCREVO PaRk0fuPmkOM2XoCmt3ulyij1N/Wrq7ee5hpTaYj1D3wnxOi2+qZdY92EbCrrcPRkbzI rxYow6zzLPDBClNz5/llPR7yXHPukzhuv6T4ANUlJVUhmXe+KU+Hk7gIMH5ozSGyYk05 g08oIUWKvs+S71KmJDx4CgEg/vOMbXnPuiviyZIPStoxVPAyHRV8woDEUiVBZ1WcI2II lsLH3BEK++XLOpH7R958TXbBY9M53ykeS6W7vLCSe0WWesao4ZyfcktoCALqEpJStcAL pygg== 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=B9vwKfbKHzDOEY1WrU024PdJUi8+ydTG61v0lkvD1q4=; b=K+vXR8XD5bjYT3NP8eFdykLyvXGbIoKGaAURC6YTqFZ/ZQsfrBddtjsweiWIh5a70o XxJzKm7cthYgT0rJDab2I9kOUZSliUSsSocLnUkaL6NfXFYoalZI8z5X4HS+EhN4jBW/ ismok7PBF2apIvCmsdMPsV3sz01wlsMTPbq9/v5Se8+2Y2VJzsg8T1Pbky9TEoc1rE5o q6bgA3pyjaaQtHl/fLY4J2vxFG6IL4dojW3hQ3fQ9FxE8JVy6RFr6FmHU0orcxzfq2qc /9y8sMkksQh4LFtUTXkrJkHyU9btH6vi0tsKNFlhMZ1nODBAeHYAVDSL5GCZkQHadD9k l0Ww== X-Gm-Message-State: AOAM531OhgIqw2YpJWZ/t3+JI52JttHW5SoYAVYPhYufg2w7Pi2V4f66 v7msP9WWs/a1UL1ffVQsesL9Og== X-Received: by 2002:a17:902:7048:b0:15f:34c1:bae0 with SMTP id h8-20020a170902704800b0015f34c1bae0mr64511498plt.71.1654072934453; Wed, 01 Jun 2022 01:42:14 -0700 (PDT) Received: from C02F52LSML85.bytedance.net ([139.177.225.241]) by smtp.gmail.com with ESMTPSA id u11-20020a63d34b000000b003c14af505f6sm827952pgi.14.2022.06.01.01.42.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 01 Jun 2022 01:42:14 -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 v4 2/2] selftest/bpf/benchs: Add bpf_map benchmark Date: Wed, 1 Jun 2022 16:41:49 +0800 Message-Id: <20220601084149.13097-3-zhoufeng.zf@bytedance.com> X-Mailer: git-send-email 2.30.1 (Apple Git-130) In-Reply-To: <20220601084149.13097-1-zhoufeng.zf@bytedance.com> References: <20220601084149.13097-1-zhoufeng.zf@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Feng Zhou Add benchmark for hash_map to reproduce the worst case that non-stop update when map's free is zero. before patch: Setting up benchmark 'bpf-hashmap-ful-update'... Benchmark 'bpf-hashmap-ful-update' started. 1:hash_map_full_perf 107796 events per sec 2:hash_map_full_perf 108072 events per sec 3:hash_map_full_perf 112169 events per sec 4:hash_map_full_perf 111423 events per sec 5:hash_map_full_perf 110778 events per sec 6:hash_map_full_perf 121336 events per sec 7:hash_map_full_perf 98676 events per sec 8:hash_map_full_perf 105860 events per sec 9:hash_map_full_perf 109930 events per sec 10:hash_map_full_perf 123434 events per sec 11:hash_map_full_perf 125374 events per sec 12:hash_map_full_perf 121979 events per sec 13:hash_map_full_perf 123014 events per sec 14:hash_map_full_perf 126219 events per sec 15:hash_map_full_perf 104793 events per sec after patch: Setting up benchmark 'bpf-hashmap-ful-update'... Benchmark 'bpf-hashmap-ful-update' started. 0:hash_map_full_perf 1219230 events per sec 1:hash_map_full_perf 1320256 events per sec 2:hash_map_full_perf 1196550 events per sec 3:hash_map_full_perf 1375684 events per sec 4:hash_map_full_perf 1365551 events per sec 5:hash_map_full_perf 1318432 events per sec 6:hash_map_full_perf 1222007 events per sec 7:hash_map_full_perf 1240786 events per sec 8:hash_map_full_perf 1190005 events per sec 9:hash_map_full_perf 1562336 events per sec 10:hash_map_full_perf 1385241 events per sec 11:hash_map_full_perf 1387909 events per sec 12:hash_map_full_perf 1371877 events per sec 13:hash_map_full_perf 1561836 events per sec 14:hash_map_full_perf 1388895 events per sec 15:hash_map_full_perf 1579054 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_full_update.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_bpf_hashmap_full_update.sh create mode 100644 tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 3820608faf57..b968649c7aa1 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -549,6 +549,7 @@ $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.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_update_bench.skel.h $(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) $(OUTPUT)/bench: LDLIBS += -lm $(OUTPUT)/bench: $(OUTPUT)/bench.o \ @@ -560,7 +561,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 $@ diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c index f973320e6dbf..35de886d9a05 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -397,6 +397,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; static const struct bench *benchs[] = { &bench_count_global, @@ -431,6 +432,7 @@ static const struct bench *benchs[] = { &bench_bpf_loop, &bench_strncmp_no_helper, &bench_strncmp_helper, + &bench_bpf_hashmap_full_update, }; static void setup_benchmark() diff --git a/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.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 != 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 = 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 = MAX_LOOP_NUM; + + link = bpf_program__attach(ctx.skel->progs.benchmark); + if (!link) { + fprintf(stderr, "failed to attach program!\n"); + exit(1); + } + + /* fill hash_map */ + map_fd = bpf_map__fd(ctx.skel->maps.hash_map_bench); + max_entries = bpf_map__max_entries(ctx.skel->maps.hash_map_bench); + for (i = 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 = bpf_num_possible_cpus(); + int i; + + for (i = 0; i < nr_cpus; i++) { + u64 time = 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 = { + .name = "bpf-hashmap-ful-update", + .validate = validate, + .setup = setup, + .producer_thread = producer, + .consumer_thread = consumer, + .measure = measure, + .report_progress = NULL, + .report_final = 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_update.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=`expr $(cat /proc/cpuinfo | grep "processor"| wc -l) - 1` +summary=$($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_bench.c b/tools/testing/selftests/bpf/progs/bpf_hashmap_full_update_bench.c new file mode 100644 index 000000000000..aa93a03f961d --- /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") = "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 = 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 key = bpf_get_prandom_u32() % MAX_ENTRIES + MAX_ENTRIES; + u32 cpu = bpf_get_smp_processor_id(); + u64 start_time = bpf_ktime_get_ns(); + + bpf_loop(nr_loops, loop_update_callback, &key, 0); + percpu_time[cpu & 255] = bpf_ktime_get_ns() - start_time; + return 0; +} -- 2.20.1