From patchwork Fri Jan 27 18:14:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13119154 X-Patchwork-Delegate: bpf@iogearbox.net 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 E1214C38142 for ; Fri, 27 Jan 2023 18:15:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234067AbjA0SPy (ORCPT ); Fri, 27 Jan 2023 13:15:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234663AbjA0SPv (ORCPT ); Fri, 27 Jan 2023 13:15:51 -0500 Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F55887346 for ; Fri, 27 Jan 2023 10:15:13 -0800 (PST) Received: by mail-ed1-x533.google.com with SMTP id u21so5482170edv.3 for ; Fri, 27 Jan 2023 10:15:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MKRLfQHZlRLGYU7EeqTaJjmPKgZI+D7UIDAiJ92z6P0=; b=X7NLE9HyM3OlCUan7t+ejxD4dvMZu3wnDfyvgkFW+IKhzVOAtZRa4CgPgkT+g2qTpm IknkJVhqU2beEp/f3BQcABs8JeOQFVFu2Yjowc5A4D/1xvOgDuGXXgf1Y/Ta9/pdcJqB vVidacaqcPfVqL0nB8ENcytU9CL+VxJ5tfiiheKP68+Pn2oIbPMftIBy9b0wjAMlxe9v njoOJ0cQMQXEGO8wFEXE7+bb1N04gX6WY0vgTInuIAOwYfKQ1ghhNK/MuxxFO4LAAX+x cQrh+EwL8hkby8PPeR6v/u4Tii42HadTz3oq1cretIVSEd9ls4Clvr3BtdJx3WzGW9/j mA8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MKRLfQHZlRLGYU7EeqTaJjmPKgZI+D7UIDAiJ92z6P0=; b=QvYiTGnz0jmH/DG1jqXMlRWrfGZl/QB9jv4kLIbVFXHMJWwx0Srsk0lyzZvEud3gJH 1zNnTS0XOWX5vEjcWyNBLoO3KLKJNHBKHSsDfikf0ZENZ97pr3rO7EiCHxLfqlLYScpI FVnLWxvwiV91UnKAIhzu/aP5k0O6HVeNnn7nfacRIc7KLaQVYWy5JGyyQ3t31indoLHB Cb2DXo9MmKNZ6N6b0pQj2ekmIpNr0wZxLMWlL0ucN/q2MdzKWQ9gu8TzBLnt+q1T8KL8 lgNTPh0SZ4OrYn0U58OfNIreVnYn1RcUBGW4gtEeFwOG9ONRtx31ybwc0EkNopu+G9x/ erhw== X-Gm-Message-State: AFqh2kooo2Z3ImJ1dzkeV4Lijh8uCgwwcRqrjO9xdabTRy/0FhYsjASR lH1WFSq+JMCI8qm9QeqMmg0/qSdfr4mw1f10IkQ= X-Google-Smtp-Source: AMrXdXvDyyJF12/u1uC3svq/GWf007qnOFci2tro8I8ZsZwvZG5sSfuDvl53uCgn5pgawXD1lKZhrA== X-Received: by 2002:a05:6402:448:b0:492:798:385e with SMTP id p8-20020a056402044800b004920798385emr56534958edw.33.1674843285714; Fri, 27 Jan 2023 10:14:45 -0800 (PST) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d910000000b00463bc1ddc76sm2639651edr.28.2023.01.27.10.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:14:45 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend Cc: Anton Protopopov Subject: [PATCH bpf-next 1/6] selftest/bpf/benchs: fix a typo in bpf_hashmap_full_update Date: Fri, 27 Jan 2023 18:14:52 +0000 Message-Id: <20230127181457.21389-2-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127181457.21389-1-aspsk@isovalent.com> References: <20230127181457.21389-1-aspsk@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net To call the bpf_hashmap_full_update benchmark, one should say: bench bpf-hashmap-ful-update The patch adds a missing 'l' to the benchmark name. Signed-off-by: Anton Protopopov --- .../selftests/bpf/benchs/bench_bpf_hashmap_full_update.c | 2 +- .../selftests/bpf/benchs/run_bench_bpf_hashmap_full_update.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 index cec51e0ff4b8..44706acf632a 100644 --- a/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c +++ b/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c @@ -85,7 +85,7 @@ void hashmap_report_final(struct bench_res res[], int res_cnt) } const struct bench bench_bpf_hashmap_full_update = { - .name = "bpf-hashmap-ful-update", + .name = "bpf-hashmap-full-update", .validate = validate, .setup = setup, .producer_thread = producer, 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 index 1e2de838f9fa..cd2efd3fdef3 100755 --- 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 @@ -6,6 +6,6 @@ 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) +summary=$($RUN_BENCH -p $nr_threads bpf-hashmap-full-update) printf "$summary" printf "\n" From patchwork Fri Jan 27 18:14:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13119156 X-Patchwork-Delegate: bpf@iogearbox.net 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 6F22CC61D97 for ; Fri, 27 Jan 2023 18:15:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234152AbjA0SPz (ORCPT ); Fri, 27 Jan 2023 13:15:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50640 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234841AbjA0SPw (ORCPT ); Fri, 27 Jan 2023 13:15:52 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 684AA86EBF for ; Fri, 27 Jan 2023 10:15:14 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id fi26so5465114edb.7 for ; Fri, 27 Jan 2023 10:15:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ChnxwB6Dc/lwyAdFAAuKNn8NDjQ5N30CsuWqf+piBg8=; b=cBbatx4pdyR1KZZP32BJ569oBhktR+/K8hohRS59hR2Lg/uaT93jX7EUJF/2nUBgY2 IF2MmmHLcyHu7mZm9lhGgdm4L4JVi1EknP7O22cIufKkN8drTR/zg5FNW055yA0tsypd ZJKqpKMZZy2Hq3BawtAVaLiCW+TgfMebn2rrz0DPqbCcPVhZLu7KLyeLzCH1+RdYUX1q UTwAirKomj4MFnvBPpORFT4hCx95SmuFv9yTL2YOKP/AV8Fsha6aVsjvlX78G6wGJH7y tyy9IXwPwbLUofhBdGEeKLabnqvs1PwhtGjRjHe7NCIbrL5On33lRNFbp2MQbCzRSm2m PBiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ChnxwB6Dc/lwyAdFAAuKNn8NDjQ5N30CsuWqf+piBg8=; b=HGC+EZjxkkDWTXvac8VdEMdVvaqycqwPE50pEtLpPBEFOxQtTuLUFDU8vvplynjGV/ IGVk71hleTi9NVBzu0v0UxPENEGSX3BPJRrmvzSt9YNgY0KFPCPaURE1vWSpAQZna5K1 wLZEHVTUBikwU+f7ulJUzjuNQzmT8DZb8/PRdciS/td17W2xrmhCvnvnJcSSDe2D1HL4 eVbG6DuHG8r1xCM2Mv7CjkaM6p4oINR17m4Iu0TonvfiUb3NXRpapl8s9gL51oTWT7ST +RuSWdYRZ/Z7oz9C002egvL7xaYjCtCUb2OAZgqtfHny2GIWu3pEwZzOgTQV0qh4Ki+m iC7Q== X-Gm-Message-State: AFqh2krffQw5flzJeQ3rYJZMozEA51fz4cokEOSbVGSA7YYbmlRLQzqr m9S72g8RHhFicIc6Y1olAflf/VC2f6hbJcXYg9U= X-Google-Smtp-Source: AMrXdXvnUZZSvNGjAJ/h9EKrcvdG5jHaJjWcOeFKzUzWboNevwd0xIECFP0hXgOINvpaU5VnkjY9Jw== X-Received: by 2002:aa7:de8f:0:b0:498:e0be:318b with SMTP id j15-20020aa7de8f000000b00498e0be318bmr43929402edv.38.1674843286527; Fri, 27 Jan 2023 10:14:46 -0800 (PST) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d910000000b00463bc1ddc76sm2639651edr.28.2023.01.27.10.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:14:46 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend Cc: Anton Protopopov Subject: [PATCH bpf-next 2/6] selftest/bpf/benchs: make a function static in bpf_hashmap_full_update Date: Fri, 27 Jan 2023 18:14:53 +0000 Message-Id: <20230127181457.21389-3-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127181457.21389-1-aspsk@isovalent.com> References: <20230127181457.21389-1-aspsk@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The hashmap_report_final callback function defined in the benchs/bench_bpf_hashmap_full_update.c file should be static. Signed-off-by: Anton Protopopov --- .../selftests/bpf/benchs/bench_bpf_hashmap_full_update.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 44706acf632a..67f76415a362 100644 --- a/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c +++ b/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_full_update.c @@ -68,7 +68,7 @@ static void setup(void) bpf_map_update_elem(map_fd, &i, &i, BPF_ANY); } -void hashmap_report_final(struct bench_res res[], int res_cnt) +static void hashmap_report_final(struct bench_res res[], int res_cnt) { unsigned int nr_cpus = bpf_num_possible_cpus(); int i; From patchwork Fri Jan 27 18:14:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13119157 X-Patchwork-Delegate: bpf@iogearbox.net 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 301FFC54EAA for ; Fri, 27 Jan 2023 18:15:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234094AbjA0SP5 (ORCPT ); Fri, 27 Jan 2023 13:15:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234422AbjA0SP4 (ORCPT ); Fri, 27 Jan 2023 13:15:56 -0500 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 859F787177 for ; Fri, 27 Jan 2023 10:15:15 -0800 (PST) Received: by mail-ed1-x52c.google.com with SMTP id n6so2827938edo.9 for ; Fri, 27 Jan 2023 10:15:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cAnF66GqnHDThXgNxRl7zxxyYBLiayHFrzNuPtMq5KE=; b=RcehqYsp1yiaNV2VzRob+8IkNsFDEEcvyEKYBrkjiBe9o5wCEBrVkJBtmw+JBlLYA3 Gqej3BhdPNQ9YFmNLOFxz1XgjJsH/xRTg2anyiKp1BU7HhC+2EAJvUOFnG4+vqLOtKtP ShrMwK0DOF+sZZyZDw+ki4EMzWPNecroYm5fxsiMMKAe8XKq1RvKful0jUvgI0FKTxJf U4TTICE6orsTiGfSAHpAilGaQs0O9y9z9L97G56NalhdLRw1eOS1zOqFyRvCNFdnLo08 L+dSLN3rzRpJjRSGODQbEfnjOiPuvV3dajLzgjQmPj63Rvltf2IZbHi7L8vAc1E04AUf EVHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cAnF66GqnHDThXgNxRl7zxxyYBLiayHFrzNuPtMq5KE=; b=SZ+uY3kW/TJtim17HFt8kHVWpycY8P2oaAS+iWs0L2wFBz+++t3+NGwFlZ/azF66cq WvgvHcwgomGJZ7Vb4xDaItfAGNOHyfapwFsMBLCETv0XmLm5s/+MPjy/xCTEkRFQstmI l6A5gZKrF405nLQTyAFOQ1NXsD0Yp6c2zUzP3mFvL8sUSFT+RFszEzEk2ZJ1vC5jwQhJ 3yWuwz7wjnk9+qZl7TNvfGPjdCt4iYSB2D7TrFVA43r0HmWkglq0+2NwiM74Pr3a/HHu NRFJNwCIxhZvZ8wTB05SPQywLKuKY78j9DNDftdN8beaSJdAy50cCflb2XGPKxQurCTM Keiw== X-Gm-Message-State: AO0yUKVF+p7XJprdH899/3zgI9GTCvBJGSBKxHXiM05ww8VWrrnniM7L ox4VsMmpC1Sv6MywSwdqFwKkveBC6LtXCrZ9IPE= X-Google-Smtp-Source: AK7set+vbCdfYRjB86w0npguS+2quGe0zY9jjKutfJcuq0KvMSHJXDmhRkYDCUIcVQMLIe4KGXizNw== X-Received: by 2002:a50:cb87:0:b0:4a0:e0f0:b2e5 with SMTP id k7-20020a50cb87000000b004a0e0f0b2e5mr6638028edi.6.1674843287238; Fri, 27 Jan 2023 10:14:47 -0800 (PST) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d910000000b00463bc1ddc76sm2639651edr.28.2023.01.27.10.14.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:14:46 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend Cc: Anton Protopopov Subject: [PATCH bpf-next 3/6] selftest/bpf/benchs: enhance argp parsing Date: Fri, 27 Jan 2023 18:14:54 +0000 Message-Id: <20230127181457.21389-4-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127181457.21389-1-aspsk@isovalent.com> References: <20230127181457.21389-1-aspsk@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net To parse command line the bench utility uses the argp_parse() function. This function takes as an argument a parent 'struct argp' structure which defines common command line options and an array of children 'struct argp' structures which defines additional command line options for particular benchmarks. This implementation doesn't allow benchmarks to share option names, e.g., if two benchmarks want to use, say, the --option option, then only one of them will succeed (the first one encountered in the array). This will be convenient if we could use the same option names in different benchmarks (with the same semantics, e.g., --nr_loops=N). Fix this by calling the argp_parse() function twice. The first call is needed to find the benchmark name. Given the name, we can patch the list of argp children to only include the correct list of option. This way the right parser will be executed. (If there's no a specific list of arguments, then only one call is enough.) As was mentioned above, arguments with same names should have the same semantics (which means in this case "taking a parameter or not"), but can have different description and will have different parsers. As we now can share option names, this also makes sense to share the option ids. Previously every benchmark defined a separate enum, like enum { ARG_SMTH = 9000, ARG_SMTH_ELSE = 9001, }; These ids were later used to distinguish between command line options: static const struct argp_option opts[] = { { "smth", ARG_SMTH, "SMTH", 0, "Set number of smth to configure smth"}, { "smth_else", ARG_SMTH_ELSE, "SMTH_ELSE", 0, "Set number of smth_else to configure smth else"}, ... Move arguments id definitions to bench.h such that they are visible to every benchmark (and such that there's no need to grep if this number is defined already or not). Signed-off-by: Anton Protopopov --- tools/testing/selftests/bpf/bench.c | 98 +++++++++++++++++-- tools/testing/selftests/bpf/bench.h | 20 ++++ .../bpf/benchs/bench_bloom_filter_map.c | 6 -- .../selftests/bpf/benchs/bench_bpf_loop.c | 4 - .../bpf/benchs/bench_local_storage.c | 5 - .../bench_local_storage_rcu_tasks_trace.c | 6 -- .../selftests/bpf/benchs/bench_ringbufs.c | 8 -- .../selftests/bpf/benchs/bench_strncmp.c | 4 - 8 files changed, 110 insertions(+), 41 deletions(-) diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c index c1f20a147462..ba93f1b268e1 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -249,11 +249,6 @@ const char argp_program_doc[] = " # run 'count-local' with 16 producer and 8 consumer thread, pinned to CPUs\n" " benchmark -p16 -c8 -a count-local\n"; -enum { - ARG_PROD_AFFINITY_SET = 1000, - ARG_CONS_AFFINITY_SET = 1001, -}; - static const struct argp_option opts[] = { { "list", 'l', NULL, 0, "List available benchmarks"}, { "duration", 'd', "SEC", 0, "Duration of benchmark, seconds"}, @@ -276,7 +271,7 @@ extern struct argp bench_local_storage_argp; extern struct argp bench_local_storage_rcu_tasks_trace_argp; extern struct argp bench_strncmp_argp; -static const struct argp_child bench_parsers[] = { +static struct argp_child bench_parsers[] = { { &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 }, { &bench_bloom_map_argp, 0, "Bloom filter map benchmark", 0 }, { &bench_bpf_loop_argp, 0, "bpf_loop helper benchmark", 0 }, @@ -287,9 +282,10 @@ static const struct argp_child bench_parsers[] = { {}, }; +static int pos_args; + static error_t parse_arg(int key, char *arg, struct argp_state *state) { - static int pos_args; switch (key) { case 'v': @@ -359,6 +355,69 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) return 0; } +struct argp *bench_name_to_argp(const char *bench_name) +{ + +#define _SCMP(NAME) (!strcmp(bench_name, NAME)) + + if (_SCMP("bloom-lookup") || + _SCMP("bloom-update") || + _SCMP("bloom-false-positive") || + _SCMP("hashmap-without-bloom") || + _SCMP("hashmap-with-bloom")) + return &bench_bloom_map_argp; + + if (_SCMP("rb-libbpf") || + _SCMP("rb-custom") || + _SCMP("pb-libbpf") || + _SCMP("pb-custom")) + return &bench_ringbufs_argp; + + if (_SCMP("local-storage-cache-seq-get") || + _SCMP("local-storage-cache-int-get") || + _SCMP("local-storage-cache-hashmap-control")) + return &bench_local_storage_argp; + + if (_SCMP("local-storage-tasks-trace")) + return &bench_local_storage_rcu_tasks_trace_argp; + + if (_SCMP("strncmp-no-helper") || + _SCMP("strncmp-helper")) + return &bench_strncmp_argp; + + if (_SCMP("bpf-loop")) + return &bench_bpf_loop_argp; + + /* no extra arguments */ + if (_SCMP("count-global") || + _SCMP("count-local") || + _SCMP("rename-base") || + _SCMP("rename-kprobe") || + _SCMP("rename-kretprobe") || + _SCMP("rename-rawtp") || + _SCMP("rename-fentry") || + _SCMP("rename-fexit") || + _SCMP("trig-base") || + _SCMP("trig-tp") || + _SCMP("trig-rawtp") || + _SCMP("trig-kprobe") || + _SCMP("trig-fentry") || + _SCMP("trig-fentry-sleep") || + _SCMP("trig-fmodret") || + _SCMP("trig-uprobe-base") || + _SCMP("trig-uprobe-with-nop") || + _SCMP("trig-uretprobe-with-nop") || + _SCMP("trig-uprobe-without-nop") || + _SCMP("trig-uretprobe-without-nop") || + _SCMP("bpf-hashmap-full-update")) + return NULL; + +#undef _SCMP + + fprintf(stderr, "%s: bench %s is unknown\n", __func__, bench_name); + exit(1); +} + static void parse_cmdline_args(int argc, char **argv) { static const struct argp argp = { @@ -367,12 +426,35 @@ static void parse_cmdline_args(int argc, char **argv) .doc = argp_program_doc, .children = bench_parsers, }; + static struct argp *bench_argp; + + /* Parse args for the first time to get bench name */ if (argp_parse(&argp, argc, argv, 0, NULL, NULL)) exit(1); - if (!env.list && !env.bench_name) { + + if (env.list) + return; + + if (!env.bench_name) { argp_help(&argp, stderr, ARGP_HELP_DOC, "bench"); exit(1); } + + /* Now check if there are custom options available. If not, then + * everything is done, if yes, then we need to patch bench_parsers + * so that bench_parsers[0] points to the right 'struct argp', and + * bench_parsers[1] terminates the list. + */ + bench_argp = bench_name_to_argp(env.bench_name); + if (bench_argp) { + bench_parsers[0].argp = bench_argp; + bench_parsers[0].header = env.bench_name; + memset(&bench_parsers[1], 0, sizeof(bench_parsers[1])); + + pos_args = 0; + if (argp_parse(&argp, argc, argv, 0, NULL, NULL)) + exit(1); + } } static void collect_measurements(long delta_ns); diff --git a/tools/testing/selftests/bpf/bench.h b/tools/testing/selftests/bpf/bench.h index d748255877e2..316ba0589bf7 100644 --- a/tools/testing/selftests/bpf/bench.h +++ b/tools/testing/selftests/bpf/bench.h @@ -101,3 +101,23 @@ static inline long atomic_swap(long *value, long n) { return __atomic_exchange_n(value, n, __ATOMIC_RELAXED); } + +enum { + ARG_PROD_AFFINITY_SET = 1000, + ARG_CONS_AFFINITY_SET, + ARG_RB_BACK2BACK, + ARG_RB_USE_OUTPUT, + ARG_RB_BATCH_CNT, + ARG_RB_SAMPLED, + ARG_RB_SAMPLE_RATE, + ARG_NR_ENTRIES, + ARG_NR_HASH_FUNCS, + ARG_VALUE_SIZE, + ARG_NR_LOOPS, + ARG_CMP_STR_LEN, + ARG_NR_MAPS, + ARG_HASHMAP_NR_KEYS_USED, + ARG_NR_PROCS, + ARG_KTHREAD_PID, + ARG_QUIET, +}; diff --git a/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c b/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c index 5bcb8a8cdeb2..bef8999d4a9e 100644 --- a/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c +++ b/tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c @@ -45,12 +45,6 @@ static struct { .value_size = 8, }; -enum { - ARG_NR_ENTRIES = 3000, - ARG_NR_HASH_FUNCS = 3001, - ARG_VALUE_SIZE = 3002, -}; - static const struct argp_option opts[] = { { "nr_entries", ARG_NR_ENTRIES, "NR_ENTRIES", 0, "Set number of expected unique entries in the bloom filter"}, diff --git a/tools/testing/selftests/bpf/benchs/bench_bpf_loop.c b/tools/testing/selftests/bpf/benchs/bench_bpf_loop.c index d0a6572bfab6..47630a9a0a4b 100644 --- a/tools/testing/selftests/bpf/benchs/bench_bpf_loop.c +++ b/tools/testing/selftests/bpf/benchs/bench_bpf_loop.c @@ -16,10 +16,6 @@ static struct { .nr_loops = 10, }; -enum { - ARG_NR_LOOPS = 4000, -}; - static const struct argp_option opts[] = { { "nr_loops", ARG_NR_LOOPS, "nr_loops", 0, "Set number of loops for the bpf_loop helper"}, diff --git a/tools/testing/selftests/bpf/benchs/bench_local_storage.c b/tools/testing/selftests/bpf/benchs/bench_local_storage.c index 5a378c84e81f..dd19ddf24fde 100644 --- a/tools/testing/selftests/bpf/benchs/bench_local_storage.c +++ b/tools/testing/selftests/bpf/benchs/bench_local_storage.c @@ -17,11 +17,6 @@ static struct { .hashmap_nr_keys_used = 1000, }; -enum { - ARG_NR_MAPS = 6000, - ARG_HASHMAP_NR_KEYS_USED = 6001, -}; - static const struct argp_option opts[] = { { "nr_maps", ARG_NR_MAPS, "NR_MAPS", 0, "Set number of local_storage maps"}, diff --git a/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c b/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c index 43f109d93130..bcbcb8b90c61 100644 --- a/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c +++ b/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c @@ -19,12 +19,6 @@ static struct { .quiet = false, }; -enum { - ARG_NR_PROCS = 7000, - ARG_KTHREAD_PID = 7001, - ARG_QUIET = 7002, -}; - static const struct argp_option opts[] = { { "nr_procs", ARG_NR_PROCS, "NR_PROCS", 0, "Set number of user processes to spin up"}, diff --git a/tools/testing/selftests/bpf/benchs/bench_ringbufs.c b/tools/testing/selftests/bpf/benchs/bench_ringbufs.c index c2554f9695ff..b31ae31d1fea 100644 --- a/tools/testing/selftests/bpf/benchs/bench_ringbufs.c +++ b/tools/testing/selftests/bpf/benchs/bench_ringbufs.c @@ -29,14 +29,6 @@ static struct { .perfbuf_sz = 128, }; -enum { - ARG_RB_BACK2BACK = 2000, - ARG_RB_USE_OUTPUT = 2001, - ARG_RB_BATCH_CNT = 2002, - ARG_RB_SAMPLED = 2003, - ARG_RB_SAMPLE_RATE = 2004, -}; - static const struct argp_option opts[] = { { "rb-b2b", ARG_RB_BACK2BACK, NULL, 0, "Back-to-back mode"}, { "rb-use-output", ARG_RB_USE_OUTPUT, NULL, 0, "Use bpf_ringbuf_output() instead of bpf_ringbuf_reserve()"}, diff --git a/tools/testing/selftests/bpf/benchs/bench_strncmp.c b/tools/testing/selftests/bpf/benchs/bench_strncmp.c index 494b591c0289..e7d48a65511b 100644 --- a/tools/testing/selftests/bpf/benchs/bench_strncmp.c +++ b/tools/testing/selftests/bpf/benchs/bench_strncmp.c @@ -14,10 +14,6 @@ static struct strncmp_args { .cmp_str_len = 32, }; -enum { - ARG_CMP_STR_LEN = 5000, -}; - static const struct argp_option opts[] = { { "cmp-str-len", ARG_CMP_STR_LEN, "CMP_STR_LEN", 0, "Set the length of compared string" }, From patchwork Fri Jan 27 18:14:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13119155 X-Patchwork-Delegate: bpf@iogearbox.net 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 D7378C61DB3 for ; Fri, 27 Jan 2023 18:15:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234841AbjA0SP4 (ORCPT ); Fri, 27 Jan 2023 13:15:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234094AbjA0SP4 (ORCPT ); Fri, 27 Jan 2023 13:15:56 -0500 Received: from mail-ed1-x52e.google.com (mail-ed1-x52e.google.com [IPv6:2a00:1450:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0AFA7F6AB for ; Fri, 27 Jan 2023 10:15:16 -0800 (PST) Received: by mail-ed1-x52e.google.com with SMTP id cw4so339271edb.13 for ; Fri, 27 Jan 2023 10:15:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1UdC6iL1WXgPlBjfSFCUng9yr2/8HZEfENJqnmw7pYg=; b=tsRbHNkJXvutjD/9ArbF+64gQlT32In5jJ465w0826cfQNroc7rleT54fTSJXl8u+U DMciOTpDyz27TcNQ7flg5ShQLUfGAgof6Khn3FtccNg/h5/i5VwbGMlNpZ65wfkniqh/ Y7TZ6C1bST7LAxnMJuugvDj7+//RYyOzWZA9loj/iuEfwCrruNLtOhiMXvTPQlxGMEPC 1xmEMnq5lrvhBqiAdmjn3aIUqX7VRPFT0gjICc5woV83JnabaCdlHN49QHX60tL1jlLb Nxp1K3JQxrrt5Vn8y9EiFkOHWLa9Sa6cp/LM/SepMC5fhBfvIT+nefJvxeVmiVeCcSWH MSvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1UdC6iL1WXgPlBjfSFCUng9yr2/8HZEfENJqnmw7pYg=; b=UqyTX3W09D7KkIP8buY6YKOW6lPy7vh0DUniAI3aeYvljCzIMgCKXTFOuqQaXzgTiT un18t6TajeY9zIzG7V32v47gXQcwQWrpYFv/YeorEBa0Gn5nHFWoUtDyEv7Zr5JCvmeU YR8I+GsJ9j75zq/MYdjLW7ZxFlWCUhrEH5pCg/vPnIZIfWM6o0UaC+Zrb+e8GZxqv/lH PH1OYvrp3zhztukTtoWj1ynTGjzOnxawwp5QNZ7xWdy8g22j8xM40ZZqtgsSGLz3vgZN fu/8gXKv48gGZGc2x4em5CtYTVElsKmSB7Ld6qUYTSkFV8lps1VSOMuG9rK+iq2epQE8 JL0A== X-Gm-Message-State: AFqh2krBAPAm6NJRyKWOCXhPhoHhML/5K1u2X1SeffhzuxI+CGw9KOMU 3iOFgM3DrFCUCOlpfdvr6HCCGhwlfdjWqrEE4wY= X-Google-Smtp-Source: AMrXdXs0SHh7jQC3JZJxwPbOag380CWtfYk3oYFi1AB69i5WmxA+dDahoidBDTeOknZCegDT67y3Uw== X-Received: by 2002:a05:6402:28c7:b0:47e:f535:e9a0 with SMTP id ef7-20020a05640228c700b0047ef535e9a0mr44845028edb.24.1674843287940; Fri, 27 Jan 2023 10:14:47 -0800 (PST) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d910000000b00463bc1ddc76sm2639651edr.28.2023.01.27.10.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:14:47 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend Cc: Anton Protopopov Subject: [PATCH bpf-next 4/6] selftest/bpf/benchs: make quiet option common Date: Fri, 27 Jan 2023 18:14:55 +0000 Message-Id: <20230127181457.21389-5-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127181457.21389-1-aspsk@isovalent.com> References: <20230127181457.21389-1-aspsk@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The "local-storage-tasks-trace" benchmark has a `--quiet` option. Move it to the list of common options, so that the main code and other benchmarks can use (now global) env.quiet as well. Signed-off-by: Anton Protopopov --- tools/testing/selftests/bpf/bench.c | 15 +++++++++++++++ tools/testing/selftests/bpf/bench.h | 1 + .../benchs/bench_local_storage_rcu_tasks_trace.c | 14 +------------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c index ba93f1b268e1..42bf41a9385e 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -16,6 +16,7 @@ struct env env = { .warmup_sec = 1, .duration_sec = 5, .affinity = false, + .quiet = false, .consumer_cnt = 1, .producer_cnt = 1, }; @@ -257,6 +258,7 @@ static const struct argp_option opts[] = { { "consumers", 'c', "NUM", 0, "Number of consumer threads"}, { "verbose", 'v', NULL, 0, "Verbose debug output"}, { "affinity", 'a', NULL, 0, "Set consumer/producer thread affinity"}, + { "quiet", 'q', "{0,1}", OPTION_ARG_OPTIONAL, "If true, be quiet"}, { "prod-affinity", ARG_PROD_AFFINITY_SET, "CPUSET", 0, "Set of CPUs for producer threads; implies --affinity"}, { "cons-affinity", ARG_CONS_AFFINITY_SET, "CPUSET", 0, @@ -286,6 +288,7 @@ static int pos_args; static error_t parse_arg(int key, char *arg, struct argp_state *state) { + long ret; switch (key) { case 'v': @@ -325,6 +328,18 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) case 'a': env.affinity = true; break; + case 'q': + if (!arg) { + env.quiet = true; + } else { + ret = strtol(arg, NULL, 10); + if (ret < 0 || ret > 1) { + fprintf(stderr, "invalid quiet %ld\n", ret); + argp_usage(state); + } + env.quiet = ret; + } + break; case ARG_PROD_AFFINITY_SET: env.affinity = true; if (parse_num_list(arg, &env.prod_cpus.cpus, diff --git a/tools/testing/selftests/bpf/bench.h b/tools/testing/selftests/bpf/bench.h index 316ba0589bf7..e322654b5e8a 100644 --- a/tools/testing/selftests/bpf/bench.h +++ b/tools/testing/selftests/bpf/bench.h @@ -24,6 +24,7 @@ struct env { bool verbose; bool list; bool affinity; + bool quiet; int consumer_cnt; int producer_cnt; struct cpu_set prod_cpus; diff --git a/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c b/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c index bcbcb8b90c61..51d4b1a690f9 100644 --- a/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c +++ b/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c @@ -12,11 +12,9 @@ static struct { __u32 nr_procs; __u32 kthread_pid; - bool quiet; } args = { .nr_procs = 1000, .kthread_pid = 0, - .quiet = false, }; static const struct argp_option opts[] = { @@ -24,8 +22,6 @@ static const struct argp_option opts[] = { "Set number of user processes to spin up"}, { "kthread_pid", ARG_KTHREAD_PID, "PID", 0, "Pid of rcu_tasks_trace kthread for ticks tracking"}, - { "quiet", ARG_QUIET, "{0,1}", 0, - "If true, don't report progress"}, {}, }; @@ -50,14 +46,6 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) } args.kthread_pid = ret; break; - case ARG_QUIET: - ret = strtol(arg, NULL, 10); - if (ret < 0 || ret > 1) { - fprintf(stderr, "invalid quiet %ld\n", ret); - argp_usage(state); - } - args.quiet = ret; - break; break; default: return ARGP_ERR_UNKNOWN; @@ -224,7 +212,7 @@ static void report_progress(int iter, struct bench_res *res, long delta_ns) exit(1); } - if (args.quiet) + if (env.quiet) return; printf("Iter %d\t avg tasks_trace grace period latency\t%lf ns\n", From patchwork Fri Jan 27 18:14:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13119158 X-Patchwork-Delegate: bpf@iogearbox.net 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 D9CFEC38142 for ; Fri, 27 Jan 2023 18:15:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234663AbjA0SP5 (ORCPT ); Fri, 27 Jan 2023 13:15:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233585AbjA0SP4 (ORCPT ); Fri, 27 Jan 2023 13:15:56 -0500 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE75786265 for ; Fri, 27 Jan 2023 10:15:16 -0800 (PST) Received: by mail-ej1-x632.google.com with SMTP id v6so15949697ejg.6 for ; Fri, 27 Jan 2023 10:15:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zL1Jf51NGgGd/pmhJ2BM230YZwf+Bd3fcH9fNZ4uAcc=; b=1/wMahZjqGEIsu+02OkhkbnKktNakrmKzxpCp4DV+f12SfLl/8PQ5K7GurhKss7WdH cZ6yArgNLay6Z5JVAH7jDcpxKt6Pryyit3fi+j/4hqLzQTV4h2wX+ZVvXSs0WnyMdp1D pJ06oQgPrvy5qsnYD0UpwQgKwH+sE9uCUGlgrPggGLELX6RGcyisl6PBdJ44pOOmdl1N PmjKxwHFl/7Lkjdl3I/nHfsom8PpdEkTEZ7DL5dVPFscU/JvXnbFQhVGEqhwbbw5TIlO zdfOLeFZNXDfzvh/hl3HWvPNLe8/w25aVKPAK61BlgNnOXPbrGpBt60kh66NpRDysfVH 9yEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zL1Jf51NGgGd/pmhJ2BM230YZwf+Bd3fcH9fNZ4uAcc=; b=r3J/t0xs4RGklhAUUIR9xAeyNwXcktMKE1fFki6UtuOEhrqYfmdB/6uQuXJ6W5ML9I /HbZRF5z9oaiCU/VBlq3fVpNFProuLnQt9Tmo1iQiloGywmlvOguqfUXyFoOD+fl+LRM cLD/Zl57caVQWLuvjyg3X+wSzlLPwdwnSqNQ2pm5x1cA7Hho1mrfuK0H5TlMPYJfP+Ux bfiIbXKmuN8cX6RVn6glaq3gG/pp+5tzConmv8d1RrGz79EZszgKB9cO0ne5aW4jF0v7 hEllh9g9V9T4E+Jv0EvfF7JR6dNN1fRYNpZIKTL7LUufNy4OeaF+LGK4C+AS/FD8MFdH tN6A== X-Gm-Message-State: AO0yUKWHiJRBTCSEu26C3+jhspSGlOXIpVa6KELRf/JUbIYgv4nVSvNu /dm7xi4NCrYfjFDOXezrI80KO6X37Wk1sQkL4No= X-Google-Smtp-Source: AK7set8bcBPkOQqgITQgyYug09D11c7iaQ/Od4f8Z6peLe/NXnT6ipjkveDaPT1rDegD7hdXXAwqzg== X-Received: by 2002:a17:907:205a:b0:878:4e5a:18b8 with SMTP id pg26-20020a170907205a00b008784e5a18b8mr8136495ejb.66.1674843288698; Fri, 27 Jan 2023 10:14:48 -0800 (PST) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d910000000b00463bc1ddc76sm2639651edr.28.2023.01.27.10.14.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:14:48 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend Cc: Anton Protopopov Subject: [PATCH bpf-next 5/6] selftest/bpf/benchs: print less if the quiet option is set Date: Fri, 27 Jan 2023 18:14:56 +0000 Message-Id: <20230127181457.21389-6-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127181457.21389-1-aspsk@isovalent.com> References: <20230127181457.21389-1-aspsk@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net The bench utility will print Setting up benchmark ''... Benchmark '' started. on startup to stdout. Suppress this output if --quiet option if given. This makes it simpler to parse benchmark output by a script. Signed-off-by: Anton Protopopov --- tools/testing/selftests/bpf/bench.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c index 42bf41a9385e..2f34db60f819 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -648,7 +648,8 @@ static void setup_benchmark() exit(1); } - printf("Setting up benchmark '%s'...\n", bench->name); + if (!env.quiet) + printf("Setting up benchmark '%s'...\n", bench->name); state.producers = calloc(env.producer_cnt, sizeof(*state.producers)); state.consumers = calloc(env.consumer_cnt, sizeof(*state.consumers)); @@ -694,7 +695,8 @@ static void setup_benchmark() next_cpu(&env.prod_cpus)); } - printf("Benchmark '%s' started.\n", bench->name); + if (!env.quiet) + printf("Benchmark '%s' started.\n", bench->name); } static pthread_mutex_t bench_done_mtx = PTHREAD_MUTEX_INITIALIZER; From patchwork Fri Jan 27 18:14:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Anton Protopopov X-Patchwork-Id: 13119159 X-Patchwork-Delegate: bpf@iogearbox.net 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 44830C61DA4 for ; Fri, 27 Jan 2023 18:16:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234422AbjA0SP7 (ORCPT ); Fri, 27 Jan 2023 13:15:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50732 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233585AbjA0SP6 (ORCPT ); Fri, 27 Jan 2023 13:15:58 -0500 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 984DE8307D for ; Fri, 27 Jan 2023 10:15:18 -0800 (PST) Received: by mail-ed1-x52d.google.com with SMTP id z11so5494944ede.1 for ; Fri, 27 Jan 2023 10:15:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=isovalent-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=r0aG3rKeM3mzxelAxjJ4UBjSX+BrfDgZCtL7fzgLO58=; b=X03/e7d0mOB958S9IcfZsW2b6+h0qqoL0aqBoJbF3nBmApwZDGLFEayKzbJe5LJTGC 8VNWipypdTIBOhhAxF1nkxAYKlDUu6Ga1jy784b0I70o92uJ+FmZwZO3Kbpu4vzT9Zd2 VHAmxaI7OtslsU+rFM6Jd1+Di+L7PF8Q9H5GTBZGCn4JMCW5oGnGOYQG7kLbhwK9zdX+ JUpxsyi2T6PeLPQeNtpz0VedEM7qCF16lb9EkTJi9pUzI1KxGeyE6pXLIZACZ4Xg7Oaf upiXRUuR6IRGAVcLtXWvPfvLlrb3l1JzBviXk2gFYC0jIbmu7i5GID4msbwNTRw70bnK iVLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=r0aG3rKeM3mzxelAxjJ4UBjSX+BrfDgZCtL7fzgLO58=; b=6gkAQS8/2yfuSOv4KVZzX1AGa2g7MsSV1+f1Rd2LhjoGDavSn3WjsGqvLFxTRIiBx3 Xwud0cuTuCRDocIcUtcFCzt47Ev+443VYnhTsJy+Gzl/NrYemjbj0qfqO+/Dh8ublBkf L4qhDgJ0BX+hTsXlob9qy7H9/W4lQ3JiQtMH/9CDleaXuo/hybtsXuC+cf7KOi0hwwNq AKUUw29iJ14PXefB2jU/m52pb3jt2lhP9nsomijd4aCAQnaXyF+v7Gl5uEe1GGylpHlU C/kmtPbQlEJSkkh1KMo1tYlPe94mwo/k1yZbuAlJ+grD14UiCoYWzKBpypnjO7HbU4HA 9QCg== X-Gm-Message-State: AFqh2kp9el9ko/KNhVoKBNMYJfcH2uc7kN8CG+SDiKqk5YEMhMhU6tER aEvVTb/4FXVkTW2Tgqegl+5hELeiE37GYp2DcIE= X-Google-Smtp-Source: AMrXdXs0/kGUr29QNgqF2cS+MMXQ2aAJETUK87Rl9TP2xet/dBAYhwfmGgm/F426b6jj7PHpmrXhMA== X-Received: by 2002:a05:6402:3906:b0:49b:c516:72ee with SMTP id fe6-20020a056402390600b0049bc51672eemr49082896edb.41.1674843289478; Fri, 27 Jan 2023 10:14:49 -0800 (PST) Received: from zh-lab-node-5.home ([2a02:168:f656:0:1ac0:4dff:fe0f:3782]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d910000000b00463bc1ddc76sm2639651edr.28.2023.01.27.10.14.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Jan 2023 10:14:49 -0800 (PST) From: Anton Protopopov To: bpf@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , John Fastabend Cc: Anton Protopopov Subject: [PATCH bpf-next 6/6] selftest/bpf/benchs: Add benchmark for hashmap lookups Date: Fri, 27 Jan 2023 18:14:57 +0000 Message-Id: <20230127181457.21389-7-aspsk@isovalent.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230127181457.21389-1-aspsk@isovalent.com> References: <20230127181457.21389-1-aspsk@isovalent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Add a new benchmark which measures hashmap lookup operations speed. A user can control the following parameters of the benchmark: * key_size (max 1024): the key size to use * max_entries: the hashmap max entries * nr_entries: the number of entries to insert/lookup * nr_loops: the number of loops for the benchmark * map_flags The hashmap flags passed to BPF_MAP_CREATE The BPF program performing the benchmarks calls two nested bpf_loop: bpf_loop(nr_loops/nr_entries) bpf_loop(nr_entries) bpf_map_lookup() So the nr_loops determines the number of actual map lookups. All lookups are successful. Example (the output is generated on a AMD Ryzen 9 3950X machine): for nr_entries in `seq 4096 4096 65536`; do echo -n "$((nr_entries*100/65536))% full: "; sudo ./bench -d2 -a bpf-hashmap-lookup --key_size=4 --nr_entries=$nr_entries --max_entries=65536 --nr_loops=1000000 --map_flags=0x40 | grep cpu; done 6% full: cpu01: lookup 50.739M ± 0.018M events/sec (approximated from 32 samples of ~19ms) 12% full: cpu01: lookup 47.751M ± 0.015M events/sec (approximated from 32 samples of ~20ms) 18% full: cpu01: lookup 45.153M ± 0.013M events/sec (approximated from 32 samples of ~22ms) 25% full: cpu01: lookup 43.826M ± 0.014M events/sec (approximated from 32 samples of ~22ms) 31% full: cpu01: lookup 41.971M ± 0.012M events/sec (approximated from 32 samples of ~23ms) 37% full: cpu01: lookup 41.034M ± 0.015M events/sec (approximated from 32 samples of ~24ms) 43% full: cpu01: lookup 39.946M ± 0.012M events/sec (approximated from 32 samples of ~25ms) 50% full: cpu01: lookup 38.256M ± 0.014M events/sec (approximated from 32 samples of ~26ms) 56% full: cpu01: lookup 36.580M ± 0.018M events/sec (approximated from 32 samples of ~27ms) 62% full: cpu01: lookup 36.252M ± 0.012M events/sec (approximated from 32 samples of ~27ms) 68% full: cpu01: lookup 35.200M ± 0.012M events/sec (approximated from 32 samples of ~28ms) 75% full: cpu01: lookup 34.061M ± 0.009M events/sec (approximated from 32 samples of ~29ms) 81% full: cpu01: lookup 34.374M ± 0.010M events/sec (approximated from 32 samples of ~29ms) 87% full: cpu01: lookup 33.244M ± 0.011M events/sec (approximated from 32 samples of ~30ms) 93% full: cpu01: lookup 32.182M ± 0.013M events/sec (approximated from 32 samples of ~31ms) 100% full: cpu01: lookup 31.497M ± 0.016M events/sec (approximated from 32 samples of ~31ms) Signed-off-by: Anton Protopopov --- tools/testing/selftests/bpf/Makefile | 5 +- tools/testing/selftests/bpf/bench.c | 7 + tools/testing/selftests/bpf/bench.h | 3 + .../bpf/benchs/bench_bpf_hashmap_lookup.c | 277 ++++++++++++++++++ .../selftests/bpf/progs/bpf_hashmap_lookup.c | 61 ++++ 5 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_lookup.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_hashmap_lookup.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index a554b41fe872..cf08587b8639 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -604,6 +604,7 @@ $(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_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o: $(OUTPUT)/local_storage_rcu_tasks_trace_bench.skel.h +$(OUTPUT)/bench_bpf_hashmap_lookup.o: $(OUTPUT)/bpf_hashmap_lookup.skel.h $(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) $(OUTPUT)/bench: LDLIBS += -lm $(OUTPUT)/bench: $(OUTPUT)/bench.o \ @@ -618,7 +619,9 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ $(OUTPUT)/bench_strncmp.o \ $(OUTPUT)/bench_bpf_hashmap_full_update.o \ $(OUTPUT)/bench_local_storage.o \ - $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o + $(OUTPUT)/bench_local_storage_rcu_tasks_trace.o \ + $(OUTPUT)/bench_bpf_hashmap_lookup.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 2f34db60f819..568a0d12197d 100644 --- a/tools/testing/selftests/bpf/bench.c +++ b/tools/testing/selftests/bpf/bench.c @@ -272,6 +272,7 @@ extern struct argp bench_bpf_loop_argp; extern struct argp bench_local_storage_argp; extern struct argp bench_local_storage_rcu_tasks_trace_argp; extern struct argp bench_strncmp_argp; +extern struct argp bench_hashmap_lookup_argp; static struct argp_child bench_parsers[] = { { &bench_ringbufs_argp, 0, "Ring buffers benchmark", 0 }, @@ -281,6 +282,7 @@ static struct argp_child bench_parsers[] = { { &bench_strncmp_argp, 0, "bpf_strncmp helper benchmark", 0 }, { &bench_local_storage_rcu_tasks_trace_argp, 0, "local_storage RCU Tasks Trace slowdown benchmark", 0 }, + { &bench_hashmap_lookup_argp, 0, "Hashmap lookup benchmark", 0 }, {}, }; @@ -403,6 +405,9 @@ struct argp *bench_name_to_argp(const char *bench_name) if (_SCMP("bpf-loop")) return &bench_bpf_loop_argp; + if (_SCMP("bpf-hashmap-lookup")) + return &bench_hashmap_lookup_argp; + /* no extra arguments */ if (_SCMP("count-global") || _SCMP("count-local") || @@ -587,6 +592,7 @@ extern const struct bench bench_local_storage_cache_seq_get; extern const struct bench bench_local_storage_cache_interleaved_get; extern const struct bench bench_local_storage_cache_hashmap_control; extern const struct bench bench_local_storage_tasks_trace; +extern const struct bench bench_bpf_hashmap_lookup; static const struct bench *benchs[] = { &bench_count_global, @@ -626,6 +632,7 @@ static const struct bench *benchs[] = { &bench_local_storage_cache_interleaved_get, &bench_local_storage_cache_hashmap_control, &bench_local_storage_tasks_trace, + &bench_bpf_hashmap_lookup, }; static void setup_benchmark() diff --git a/tools/testing/selftests/bpf/bench.h b/tools/testing/selftests/bpf/bench.h index e322654b5e8a..f0e477518ab9 100644 --- a/tools/testing/selftests/bpf/bench.h +++ b/tools/testing/selftests/bpf/bench.h @@ -121,4 +121,7 @@ enum { ARG_NR_PROCS, ARG_KTHREAD_PID, ARG_QUIET, + ARG_KEY_SIZE, + ARG_MAP_FLAGS, + ARG_MAX_ENTRIES, }; diff --git a/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_lookup.c b/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_lookup.c new file mode 100644 index 000000000000..79805d008125 --- /dev/null +++ b/tools/testing/selftests/bpf/benchs/bench_bpf_hashmap_lookup.c @@ -0,0 +1,277 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Isovalent */ + +#include +#include +#include "bench.h" +#include "bpf_hashmap_lookup.skel.h" +#include "bpf_util.h" + +/* BPF triggering benchmarks */ +static struct ctx { + struct bpf_hashmap_lookup *skel; +} ctx; + +/* only available to kernel, so define it here */ +#define BPF_MAX_LOOPS (1<<23) + +#define MAX_KEY_SIZE 1024 /* the size of the key map */ + +static struct { + __u32 key_size; + __u32 map_flags; + __u32 max_entries; + __u32 nr_entries; + __u32 nr_loops; +} args = { + .key_size = 4, + .map_flags = 0, + .max_entries = 1000, + .nr_entries = 500, + .nr_loops = 1000000, +}; + +static const struct argp_option opts[] = { + { "key_size", ARG_KEY_SIZE, "KEY_SIZE", 0, + "The hashmap key size (max 1024)"}, + { "map_flags", ARG_MAP_FLAGS, "MAP_FLAGS", 0, + "The hashmap flags passed to BPF_MAP_CREATE"}, + { "max_entries", ARG_MAX_ENTRIES, "MAX_ENTRIES", 0, + "The hashmap max entries"}, + { "nr_entries", ARG_NR_ENTRIES, "NR_ENTRIES", 0, + "The number of entries to insert/lookup"}, + { "nr_loops", ARG_NR_LOOPS, "NR_LOOPS", 0, + "The number of loops for the benchmark"}, + {}, +}; + +static error_t parse_arg(int key, char *arg, struct argp_state *state) +{ + long ret; + + switch (key) { + case ARG_KEY_SIZE: + ret = strtol(arg, NULL, 10); + if (ret < 1 || ret > MAX_KEY_SIZE) { + fprintf(stderr, "invalid key_size"); + argp_usage(state); + } + args.key_size = ret; + break; + case ARG_MAP_FLAGS: + if (!strncasecmp(arg, "0x", 2)) + ret = strtol(arg, NULL, 0x10); + else + ret = strtol(arg, NULL, 10); + if (ret < 0 || ret > UINT_MAX) { + fprintf(stderr, "invalid map_flags"); + argp_usage(state); + } + args.map_flags = ret; + break; + case ARG_MAX_ENTRIES: + ret = strtol(arg, NULL, 10); + if (ret < 1 || ret > UINT_MAX) { + fprintf(stderr, "invalid max_entries"); + argp_usage(state); + } + args.max_entries = ret; + break; + case ARG_NR_ENTRIES: + ret = strtol(arg, NULL, 10); + if (ret < 1 || ret > UINT_MAX) { + fprintf(stderr, "invalid nr_entries"); + argp_usage(state); + } + args.nr_entries = ret; + break; + case ARG_NR_LOOPS: + ret = strtol(arg, NULL, 10); + if (ret < 1 || ret > BPF_MAX_LOOPS) { + fprintf(stderr, "invalid nr_loops: %ld (min=1 max=%u)\n", + ret, BPF_MAX_LOOPS); + argp_usage(state); + } + args.nr_loops = ret; + break; + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +const struct argp bench_hashmap_lookup_argp = { + .options = opts, + .parser = parse_arg, +}; + +static void validate(void) +{ + if (env.consumer_cnt != 1) { + fprintf(stderr, "benchmark doesn't support multi-consumer!\n"); + exit(1); + } + + if (args.nr_entries > args.max_entries) { + fprintf(stderr, "args.nr_entries is too big! (max %u, got %u)\n", + args.max_entries, args.nr_entries); + 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 inline void patch_key(u32 i, u32 *key) +{ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + *key = i + 1; +#else + *key = __builtin_bswap32(i + 1); +#endif + /* the rest of key is random */ +} + +static void setup(void) +{ + struct bpf_link *link; + int map_fd; + int ret; + int i; + + setup_libbpf(); + + ctx.skel = bpf_hashmap_lookup__open(); + if (!ctx.skel) { + fprintf(stderr, "failed to open skeleton\n"); + exit(1); + } + + bpf_map__set_max_entries(ctx.skel->maps.hash_map_bench, args.max_entries); + bpf_map__set_key_size(ctx.skel->maps.hash_map_bench, args.key_size); + bpf_map__set_value_size(ctx.skel->maps.hash_map_bench, 8); + bpf_map__set_map_flags(ctx.skel->maps.hash_map_bench, args.map_flags); + + ctx.skel->bss->nr_entries = args.nr_entries; + ctx.skel->bss->nr_loops = args.nr_loops / args.nr_entries; + + if (args.key_size > 4) { + for (i = 1; i < args.key_size/4; i++) + ctx.skel->bss->key[i] = 2654435761 * i; + } + + ret = bpf_hashmap_lookup__load(ctx.skel); + if (ret) { + bpf_hashmap_lookup__destroy(ctx.skel); + fprintf(stderr, "failed to load map: %s", strerror(-ret)); + exit(1); + } + + /* fill in the hash_map */ + map_fd = bpf_map__fd(ctx.skel->maps.hash_map_bench); + for (u64 i = 0; i < args.nr_entries; i++) { + patch_key(i, ctx.skel->bss->key); + bpf_map_update_elem(map_fd, ctx.skel->bss->key, &i, BPF_ANY); + } + + link = bpf_program__attach(ctx.skel->progs.benchmark); + if (!link) { + fprintf(stderr, "failed to attach program!\n"); + exit(1); + } +} + +static inline double events_from_time(u64 time) +{ + if (time) + return args.nr_loops * 1000000000llu / time / 1000000.0L; + + return 0; +} + +static int compute_events(u64 *times, double *events_mean, double *events_stddev, u64 *mean_time) +{ + int i, n = 0; + + *events_mean = 0; + *events_stddev = 0; + *mean_time = 0; + + for (i = 0; i < 32; i++) { + if (!times[i]) + break; + *mean_time += times[i]; + *events_mean += events_from_time(times[i]); + n += 1; + } + if (!n) + return 0; + + *mean_time /= n; + *events_mean /= n; + + if (n > 1) { + for (i = 0; i < n; i++) { + double events_i = *events_mean - events_from_time(times[i]); + *events_stddev += events_i * events_i / (n - 1); + } + *events_stddev = sqrt(*events_stddev); + } + + return n; +} + +static void hashmap_report_final(struct bench_res res[], int res_cnt) +{ + unsigned int nr_cpus = bpf_num_possible_cpus(); + double events_mean, events_stddev; + u64 mean_time; + int i, n; + + for (i = 0; i < nr_cpus; i++) { + n = compute_events(ctx.skel->bss->percpu_times[i], &events_mean, + &events_stddev, &mean_time); + if (n == 0) + continue; + + if (env.quiet) { + /* we expect only one cpu to be present */ + if (env.affinity) + printf("%.3lf\n", events_mean); + else + printf("cpu%02d %.3lf\n", i, events_mean); + } else { + printf("cpu%02d: lookup %.3lfM ± %.3lfM events/sec" + " (approximated from %d samples of ~%lums)\n", + i, events_mean, 2*events_stddev, + n, mean_time / 1000000); + } + } +} + +const struct bench bench_bpf_hashmap_lookup = { + .name = "bpf-hashmap-lookup", + .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/progs/bpf_hashmap_lookup.c b/tools/testing/selftests/bpf/progs/bpf_hashmap_lookup.c new file mode 100644 index 000000000000..79e41e3fb1bf --- /dev/null +++ b/tools/testing/selftests/bpf/progs/bpf_hashmap_lookup.c @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2023 Isovalent */ + +#include "vmlinux.h" + +#include +#include "bpf_misc.h" + +char _license[] SEC("license") = "GPL"; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); +} hash_map_bench SEC(".maps"); + +/* The number of slots to store times */ +#define NR_SLOTS 32 + +/* Configured by userspace */ +u64 nr_entries; +u64 nr_loops; +u32 __attribute__((__aligned__(8))) key[256]; + +/* Filled by us */ +u64 __attribute__((__aligned__(256))) percpu_times_index[NR_SLOTS]; +u64 __attribute__((__aligned__(256))) percpu_times[256][NR_SLOTS]; + +static inline void patch_key(u32 i) +{ +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + key[0] = i + 1; +#else + key[0] = __builtin_bswap32(i + 1); +#endif + /* the rest of key is random and is configured by userspace */ +} + +static int lookup_callback(__u32 index, u32 *unused) +{ + patch_key(index); + return bpf_map_lookup_elem(&hash_map_bench, key) ? 0 : 1; +} + +static int loop_lookup_callback(__u32 index, u32 *unused) +{ + return bpf_loop(nr_entries, lookup_callback, NULL, 0) ? 0 : 1; +} + +SEC("fentry/" SYS_PREFIX "sys_getpgid") +int benchmark(void *ctx) +{ + u32 cpu = bpf_get_smp_processor_id(); + u32 times_index; + u64 start_time; + + times_index = percpu_times_index[cpu & 255] % NR_SLOTS; + start_time = bpf_ktime_get_ns(); + bpf_loop(nr_loops, loop_lookup_callback, NULL, 0); + percpu_times[cpu & 255][times_index] = bpf_ktime_get_ns() - start_time; + percpu_times_index[cpu & 255] += 1; + return 0; +}