Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp658679pxa; Sat, 1 Aug 2020 01:53:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVfLTIURnLsPEGhnV/Zy8Fv0IbgSbFqbLUZ7ZfyVkB4naQtWQ9jlv5wHKfzGF24BllgBVj X-Received: by 2002:a05:6402:1841:: with SMTP id v1mr7441126edy.198.1596271994824; Sat, 01 Aug 2020 01:53:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596271994; cv=none; d=google.com; s=arc-20160816; b=X1fFwdaGrNzY7POyeP0P3NvadfQp4KY07fzZ/Rh3rdDFkRSMRkxQyQlowbJ3gg1RCN B6B0tBtHIQFTwAv79LeNzPuaVYm+L579vwMtt4riLErcdeIFTHDniPyZ5Qj7AA6aKv/L 7qlI/0yWMTnwF/B2XxUFAOq7FKn5FFrYsel1EQGoyJpujQ6to5yCybihi4TxZK3BHjQ8 k9LVcejbHG6tFDhaJzRqe185bDTk1kOmarBQl+/momvfMZ57+gBMZfsxLa0i9vcPANTM sDainP0pnbOW+3JiTiu/YDq5EOvTEsrNG+f/6ol7FcaLA03x988dUPK917LsVIrm3u0Q 7isQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:smtp-origin-cluster :cc:to:smtp-origin-hostname:from:smtp-origin-hostprefix :dkim-signature; bh=5RPWFtVuXAxEQqbG8nvPzo+ekxrYzxIosVhRDbbFtRc=; b=SOyJfjd4nE9U1MpCkvehE78WJaxeKiCGGkfTdyipGm4hDcrHgYbiPeeRScNUhJtsA2 0sbzuT+JjjFddW6CBhKdwq8MfcDohxNuYkaFm7Y8Zk/elSJDmzq2Af1WlU6ofdlP1jr5 3XS/wOqTRByygZmjIhpNHITAhhrtaJe+7e6FchKtI88DIjDhmGToqgyL+Oq6xYq97opW M2YoRtya4aLaJBGlWAk6SVFGyj36C1sq3SCU5+kgHpt1z41vsGE0QLP5Gxi7CmaZjEl4 iJwNXrJVg0BS12YsNsRgblUCCad8RSPZxWNTKfT/+DahnwE91Qnh4kYwk0kSwQf19tRt TMBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=TQhOkKHS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e26si7616117edv.66.2020.08.01.01.52.52; Sat, 01 Aug 2020 01:53:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=TQhOkKHS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728532AbgHAItx (ORCPT + 99 others); Sat, 1 Aug 2020 04:49:53 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:16480 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728490AbgHAItw (ORCPT ); Sat, 1 Aug 2020 04:49:52 -0400 Received: from pps.filterd (m0148460.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0718evfk027206 for ; Sat, 1 Aug 2020 01:49:51 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=facebook; bh=5RPWFtVuXAxEQqbG8nvPzo+ekxrYzxIosVhRDbbFtRc=; b=TQhOkKHSxPnkUVPxz91FcLwJvwey5tZM7TUz1+PwNo+ehPKmNc1xYDZrpq1OEI1T9hyh s0sLm7PKtMTS1ZMEMEolruK6FEosPR6W++9wMqWCnSrFFuhUzNvP00glO2531JocY36h 7s/cqIPeWr9Fx4PWvXvpEVHi0JQrf6FZ0LA= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 32m8dyeg62-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Sat, 01 Aug 2020 01:49:51 -0700 Received: from intmgw002.03.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.1979.3; Sat, 1 Aug 2020 01:49:49 -0700 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 52B3862E53D6; Sat, 1 Aug 2020 01:47:33 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Song Liu Smtp-Origin-Hostname: devbig006.ftw2.facebook.com To: , , CC: , , , , , , , Song Liu Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next 5/5] selftests/bpf: add benchmark for uprobe vs. user_prog Date: Sat, 1 Aug 2020 01:47:21 -0700 Message-ID: <20200801084721.1812607-6-songliubraving@fb.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200801084721.1812607-1-songliubraving@fb.com> References: <20200801084721.1812607-1-songliubraving@fb.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.235,18.0.687 definitions=2020-08-01_07:2020-07-31,2020-08-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 bulkscore=0 mlxlogscore=999 malwarescore=0 lowpriorityscore=0 impostorscore=0 adultscore=0 priorityscore=1501 suspectscore=0 spamscore=0 phishscore=0 clxscore=1015 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2008010067 X-FB-Internal: deliver Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a benchmark to compare performance of 1) uprobe; 2) user program w/o args; 3) user program w/ args; 4) user program w/ args on random cpu. Sample output: ./test_progs -t uprobe_vs_user_prog -v test_uprobe_vs_user_prog:PASS:uprobe_vs_user_prog__open_and_load 0 nsec test_uprobe_vs_user_prog:PASS:get_base_addr 0 nsec test_uprobe_vs_user_prog:PASS:attach_uprobe 0 nsec run_perf_test:PASS:uprobe 0 nsec Each uprobe uses 1419 nanoseconds run_perf_test:PASS:user_prog_no_args 0 nsec Each user_prog_no_args uses 313 nanoseconds run_perf_test:PASS:user_prog_with_args 0 nsec Each user_prog_with_args uses 335 nanoseconds run_perf_test:PASS:user_prog_with_args_on_cpu 0 nsec Each user_prog_with_args_on_cpu uses 2821 nanoseconds Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Song Liu --- .../bpf/prog_tests/uprobe_vs_user_prog.c | 101 ++++++++++++++++++ .../selftests/bpf/progs/uprobe_vs_user_prog.c | 21 ++++ 2 files changed, 122 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/uprobe_vs_user= _prog.c create mode 100644 tools/testing/selftests/bpf/progs/uprobe_vs_user_prog= .c diff --git a/tools/testing/selftests/bpf/prog_tests/uprobe_vs_user_prog.c= b/tools/testing/selftests/bpf/prog_tests/uprobe_vs_user_prog.c new file mode 100644 index 0000000000000..dadd7b56e69ec --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/uprobe_vs_user_prog.c @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include "uprobe_vs_user_prog.skel.h" + +#define REPEAT_CNT 10000ULL + +static int duration; + +static noinline void uprobe_target(void) +{ + asm (""); +} + +struct bpf_prog_test_run_attr attr; + +static void call_user_prog(void) +{ + bpf_prog_test_run_xattr(&attr); +} + +static int numcpu; + +static void call_user_prog_on_cpu(void) +{ + static int cpu =3D 0; + + attr.cpu_plus =3D cpu + 1; + bpf_prog_test_run_xattr(&attr); + cpu =3D (cpu + 1) % numcpu; +} + +typedef void (__run_func)(void); + +static void run_perf_test(struct uprobe_vs_user_prog *skel, + __run_func func, const char *name) +{ + __u64 start_time, total_time; + int i; + + skel->bss->sum =3D 0; + + start_time =3D time_get_ns(); + for (i =3D 0; i < REPEAT_CNT; i++) + func(); + total_time =3D time_get_ns() - start_time; + + CHECK(skel->bss->sum !=3D REPEAT_CNT, name, + "missed %llu times\n", REPEAT_CNT - skel->bss->sum); + printf("Each %s uses %llu nanoseconds\n", name, total_time / REPEAT_CNT= ); +} + +void test_uprobe_vs_user_prog(void) +{ + struct bpf_user_prog_args args =3D {}; + struct uprobe_vs_user_prog *skel; + struct bpf_link *uprobe_link; + size_t uprobe_offset; + ssize_t base_addr; + + skel =3D uprobe_vs_user_prog__open_and_load(); + + if (CHECK(!skel, "uprobe_vs_user_prog__open_and_load", + "skeleton open_and_laod failed\n")) + return; + + base_addr =3D get_base_addr(); + if (CHECK(base_addr < 0, "get_base_addr", + "failed to find base addr: %zd", base_addr)) + return; + uprobe_offset =3D (size_t)&uprobe_target - base_addr; + uprobe_link =3D bpf_program__attach_uprobe(skel->progs.handle_uprobe, + false /* retprobe */, + 0 /* self pid */, + "/proc/self/exe", + uprobe_offset); + + if (CHECK(IS_ERR(uprobe_link), "attach_uprobe", + "err %ld\n", PTR_ERR(uprobe_link))) + goto cleanup; + skel->links.handle_uprobe =3D uprobe_link; + + run_perf_test(skel, uprobe_target, "uprobe"); + + attr.prog_fd =3D bpf_program__fd(skel->progs.user_prog); + run_perf_test(skel, call_user_prog, "user_prog_no_args"); + + attr.data_size_in =3D sizeof(args); + attr.data_in =3D &args; + run_perf_test(skel, call_user_prog, "user_prog_with_args"); + + numcpu =3D libbpf_num_possible_cpus(); + + if (numcpu <=3D 0) + goto cleanup; + + run_perf_test(skel, call_user_prog_on_cpu, + "user_prog_with_args_on_cpu"); + +cleanup: + uprobe_vs_user_prog__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/uprobe_vs_user_prog.c b/to= ols/testing/selftests/bpf/progs/uprobe_vs_user_prog.c new file mode 100644 index 0000000000000..8b327b7cee30d --- /dev/null +++ b/tools/testing/selftests/bpf/progs/uprobe_vs_user_prog.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2017 Facebook +#include "vmlinux.h" +#include +#include + +volatile __u64 sum =3D 0; + +SEC("uprobe/func") +int handle_uprobe(struct pt_regs *ctx) +{ + sum++; + return 0; +} + +SEC("user") +int user_prog(struct pt_regs *ctx) +{ + sum++; + return 0; +} --=20 2.24.1