From patchwork Wed Mar 19 16:36:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vadim Fedorenko X-Patchwork-Id: 14022832 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 529C51DE881 for ; Wed, 19 Mar 2025 16:38:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.153.30 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742402286; cv=none; b=q7vy9blV1wHYPtLiqSvulClMZKoUDceRk0t3Y+9hRDhdWUqPcgmzddEm7ByeJlU/3ofoen2aQQachRc+Ri24Oig00I4cLVCYKWk4PAwo9qk1bQhez/OC6UH4ZGu+zXD0FSK311wf3HWUyybhHNbySK2K4u6rfoAn/iT7YfmlYZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742402286; c=relaxed/simple; bh=lYYql7QWyfM/V2S2dDWpxbFvwK4evLRvMyIhPkcjKeg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ErQGLOXfTjCjCjRSHA7qy10+QzF/JmgEyJBExKFBLL7yFoSLlMX3zKSw/CilBAAQcoDs04hWa3nk5f4i/xxPXgNzUlI+4xPNqxRWhwaN/7HTrgN+g8jFFj0vvqcv2Bru7hn/3QQwYkXW84YyNu2iZPhdjgFC4LCBlmr3ShdiR3Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b=P4Ha0vnC; arc=none smtp.client-ip=67.231.153.30 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=meta.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=meta.com header.i=@meta.com header.b="P4Ha0vnC" Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.18.1.2/8.18.1.2) with ESMTP id 52JGCVZ2020382; Wed, 19 Mar 2025 09:37:26 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meta.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=s2048-2021-q4; bh=pS46nJgPfSz7ph2B0rdWcYPLP3IYWmfeMsIQdAzzYRs=; b=P4Ha0vnCTu24 pRE8tLO/lukp/auw+os+xTd3Z+zWa60+sUZBQ5tLPtINAYUCcleZF5kDMj+Izd33 61WDxs595KbWKD77mSOoKgr64a7x5IsVdElOGUwcD3DeOcRspnIW76CLZ1EU2dUz ANsvEaQRTKZoi3+YkYXKWNVpfzKs4z9q6WwQOFTS7DJDRo48CjL48r+iQqa8SxCC WK9JF+iYqi9lGk3Vkr6DMbEAwPlCI1r678T5K5Tuz78nxRCKOI6HYhDZchG4Ayq/ ZFlwTafFdQfbAA6CfFdq5TuyXOjjXKTMX3cMf3AEiFkqO3P20FaWCCa/65jBWoWT PlDLbJ+e0Q== Received: from maileast.thefacebook.com ([163.114.135.16]) by m0089730.ppops.net (PPS) with ESMTPS id 45fn4mmb9n-16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT); Wed, 19 Mar 2025 09:37:25 -0700 (PDT) Received: from devvm4158.cln0.facebook.com (2620:10d:c0a8:1b::2d) by mail.thefacebook.com (2620:10d:c0a9:6f::237c) with Microsoft SMTP Server id 15.2.1544.14; Wed, 19 Mar 2025 16:36:53 +0000 From: Vadim Fedorenko To: Borislav Petkov , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Eduard Zingerman , Thomas Gleixner , Yonghong Song , Vadim Fedorenko , Mykola Lysenko CC: , , Peter Zijlstra , Vadim Fedorenko , Martin KaFai Lau Subject: [PATCH bpf-next v12 5/5] selftests/bpf: add usage example for cpu time counter kfuncs Date: Wed, 19 Mar 2025 09:36:38 -0700 Message-ID: <20250319163638.3607043-6-vadfed@meta.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250319163638.3607043-1-vadfed@meta.com> References: <20250319163638.3607043-1-vadfed@meta.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: LMzz39cR3H0Tphd4kvT7zKJvy9wQQXVi X-Proofpoint-GUID: LMzz39cR3H0Tphd4kvT7zKJvy9wQQXVi X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1093,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-03-19_06,2025-03-19_01,2024-11-22_01 X-Patchwork-Delegate: bpf@iogearbox.net The selftest provides an example of how to measure the latency of bpf kfunc/helper call using time stamp counter and how to convert measured value into nanoseconds. Signed-off-by: Vadim Fedorenko --- .../bpf/prog_tests/test_cpu_cycles.c | 35 +++++++++++++++++++ .../selftests/bpf/progs/test_cpu_cycles.c | 25 +++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_cpu_cycles.c create mode 100644 tools/testing/selftests/bpf/progs/test_cpu_cycles.c diff --git a/tools/testing/selftests/bpf/prog_tests/test_cpu_cycles.c b/tools/testing/selftests/bpf/prog_tests/test_cpu_cycles.c new file mode 100644 index 000000000000..067307f0c4c2 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_cpu_cycles.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Inc. */ + +#include +#include "test_cpu_cycles.skel.h" + +static void cpu_cycles(void) +{ + LIBBPF_OPTS(bpf_test_run_opts, opts); + struct test_cpu_cycles *skel; + int err, pfd; + + skel = test_cpu_cycles__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_cpu_cycles open and load")) + return; + + pfd = bpf_program__fd(skel->progs.bpf_cpu_cycles); + if (!ASSERT_GT(pfd, 0, "test_cpu_cycles fd")) + goto fail; + + err = bpf_prog_test_run_opts(pfd, &opts); + if (!ASSERT_OK(err, "test_cpu_cycles test run")) + goto fail; + + ASSERT_NEQ(skel->bss->cycles, 0, "test_cpu_cycles 0 cycles"); + ASSERT_NEQ(skel->bss->ns, 0, "test_cpu_cycles 0 ns"); +fail: + test_cpu_cycles__destroy(skel); +} + +void test_cpu_cycles(void) +{ + if (test__start_subtest("cpu_cycles")) + cpu_cycles(); +} diff --git a/tools/testing/selftests/bpf/progs/test_cpu_cycles.c b/tools/testing/selftests/bpf/progs/test_cpu_cycles.c new file mode 100644 index 000000000000..3c428f3be831 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_cpu_cycles.c @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Meta Inc. */ + +#include "vmlinux.h" +#include + +extern u64 bpf_cpu_time_counter_to_ns(u64 cycles) __weak __ksym; +extern u64 bpf_get_cpu_time_counter(void) __weak __ksym; + +__u64 cycles, ns; + +SEC("syscall") +int bpf_cpu_cycles(void) +{ + struct bpf_pidns_info pidns; + __u64 start; + + start = bpf_get_cpu_time_counter(); + bpf_get_ns_current_pid_tgid(0, 0, &pidns, sizeof(struct bpf_pidns_info)); + cycles = bpf_get_cpu_time_counter() - start; + ns = bpf_cpu_time_counter_to_ns(cycles); + return 0; +} + +char _license[] SEC("license") = "GPL";