From nobody Sat Apr 18 01:21:32 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 A02BFCCA47F for ; Tue, 19 Jul 2022 22:40:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238898AbiGSWkA (ORCPT ); Tue, 19 Jul 2022 18:40:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231350AbiGSWj5 (ORCPT ); Tue, 19 Jul 2022 18:39:57 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 375D85F103 for ; Tue, 19 Jul 2022 15:39:56 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31e62f7b377so14813617b3.17 for ; Tue, 19 Jul 2022 15:39:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=l87gG1WyC4n1XKCTQzQ6qazcqKsuewxndKvm1gZMqNQ=; b=GTxObVcma+OcUlt/RnVXBWHWI4KnrfiNrO5lDyplvM6yo7182eICqUB6x2Wl+NbEer M54pE1TuHvRTApLXtPeGwM5mpKetvKOXdWsMkq26IfDWWVzmpFooeBaY3cOUTimWCuTd M3/1azLothgNl6qSa+GrXNMdQEbROeVbsE+4r19QHWHYM3yJqg2Vae3L46SFFdc0x7I0 Xx9jzeBCNgBobbaltASj+3pvzt2xDcNX+P3M/ZCl3pe3kgkUavXfmrPnKlD6tsw3JS+2 IctNJddyGi7PfCvwcEa1mb0M3uy5zeibKR6Ci9+we7xQnS28r5fbELtewJ1Cgj94H2UI s4oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=l87gG1WyC4n1XKCTQzQ6qazcqKsuewxndKvm1gZMqNQ=; b=SM17Bb4/4LuSBSfLUov/y2kZFIWRFrg3nh125ySMedP9+rA93/ZnpSKXv3ennujiz5 7vUzdV92oEjAnLzad06qgIYNrOIhrOCbxiIz5gTs8kSTjE1CP6311SUZDIWlRnUPuckM r/+qzFJcyCtrymZnKS+8L/3OwOYRm8cDxcsjLxbvPpHSZRKQ98MtE7MqPH9PAPeY/+I3 Fm6s3IfPpWJFsI3GfPxclb/xqnj2ug/8Eu1WRENji+tm54JHNdznu0O6s80IrBhs6qPI Jcouvsb2KAYXIUV0p9NeV25KgGNYD8Yh8WZmcTBk251fpbpEfCYoM/M4d8bLtfBDSFeI 9yqg== X-Gm-Message-State: AJIora8hRNaul8qrl7vE5YhLK/6HD8Y4mAIKWCZ84vJgStLse3n3DyeO 7wyOKCN+ujjOxAs4cKmZE9/KihXXE0US X-Google-Smtp-Source: AGRyM1sdo635dOXyXBIo0JfgAEuPZQ6T6iA4rAc3JR3JXFDiiyUG5qbf5vTPHLLd3x4XmowuZLmFd2ee+J+p X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1251:e27b:f589:696c]) (user=irogers job=sendgmr) by 2002:a81:e44:0:b0:31e:6077:45c0 with SMTP id 65-20020a810e44000000b0031e607745c0mr4575757ywo.490.1658270395365; Tue, 19 Jul 2022 15:39:55 -0700 (PDT) Date: Tue, 19 Jul 2022 15:39:44 -0700 In-Reply-To: <20220719223946.176299-1-irogers@google.com> Message-Id: <20220719223946.176299-2-irogers@google.com> Mime-Version: 1.0 References: <20220719223946.176299-1-irogers@google.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog Subject: [PATCH v3 1/3] perf: Align user space counter reading with code From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Align the user space counter reading documentation with the code in perf_mmap__read_self. Previously the documentation was based on the perf rdpmc test, but now general purpose code is provided by libperf. Signed-off-by: Ian Rogers Reviewed-by: Rob Herring --- include/uapi/linux/perf_event.h | 35 +++++++++++++++++---------- tools/include/uapi/linux/perf_event.h | 35 +++++++++++++++++---------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index d37629dbad72..6826dabb7e03 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -538,9 +538,13 @@ struct perf_event_mmap_page { * * if (pc->cap_usr_time && enabled !=3D running) { * cyc =3D rdtsc(); - * time_offset =3D pc->time_offset; * time_mult =3D pc->time_mult; * time_shift =3D pc->time_shift; + * time_offset =3D pc->time_offset; + * if (pc->cap_user_time_short) { + * time_cycles =3D pc->time_cycles; + * time_mask =3D pc->time_mask; + * } * } * * index =3D pc->index; @@ -548,6 +552,9 @@ struct perf_event_mmap_page { * if (pc->cap_user_rdpmc && index) { * width =3D pc->pmc_width; * pmc =3D rdpmc(index - 1); + * pmc <<=3D 64 - width; + * pmc >>=3D 64 - width; + * count +=3D pmc; * } * * barrier(); @@ -590,25 +597,27 @@ struct perf_event_mmap_page { * If cap_usr_time the below fields can be used to compute the time * delta since time_enabled (in ns) using rdtsc or similar. * - * u64 quot, rem; - * u64 delta; - * - * quot =3D (cyc >> time_shift); - * rem =3D cyc & (((u64)1 << time_shift) - 1); - * delta =3D time_offset + quot * time_mult + - * ((rem * time_mult) >> time_shift); + * cyc =3D time_cycles + ((cyc - time_cycles) & time_mask); + * delta =3D time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift); * * Where time_offset,time_mult,time_shift and cyc are read in the - * seqcount loop described above. This delta can then be added to - * enabled and possible running (if index), improving the scaling: + * seqcount loop described above. mul_u64_u32_shr will compute: + * + * (u64)(((unsigned __int128)cyc * time_mult) >> time_shift) + * + * This delta can then be added to enabled and possible running (if + * index) to improve the scaling. Due to event multiplexing, running + * may be zero and so care is needed to avoid division by zero. * * enabled +=3D delta; * if (index) * running +=3D delta; * - * quot =3D count / running; - * rem =3D count % running; - * count =3D quot * enabled + (rem * enabled) / running; + * if (running !=3D 0) { + * quot =3D count / running; + * rem =3D count % running; + * count =3D quot * enabled + (rem * enabled) / running; + * } */ __u16 time_shift; __u32 time_mult; diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/lin= ux/perf_event.h index d37629dbad72..6826dabb7e03 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -538,9 +538,13 @@ struct perf_event_mmap_page { * * if (pc->cap_usr_time && enabled !=3D running) { * cyc =3D rdtsc(); - * time_offset =3D pc->time_offset; * time_mult =3D pc->time_mult; * time_shift =3D pc->time_shift; + * time_offset =3D pc->time_offset; + * if (pc->cap_user_time_short) { + * time_cycles =3D pc->time_cycles; + * time_mask =3D pc->time_mask; + * } * } * * index =3D pc->index; @@ -548,6 +552,9 @@ struct perf_event_mmap_page { * if (pc->cap_user_rdpmc && index) { * width =3D pc->pmc_width; * pmc =3D rdpmc(index - 1); + * pmc <<=3D 64 - width; + * pmc >>=3D 64 - width; + * count +=3D pmc; * } * * barrier(); @@ -590,25 +597,27 @@ struct perf_event_mmap_page { * If cap_usr_time the below fields can be used to compute the time * delta since time_enabled (in ns) using rdtsc or similar. * - * u64 quot, rem; - * u64 delta; - * - * quot =3D (cyc >> time_shift); - * rem =3D cyc & (((u64)1 << time_shift) - 1); - * delta =3D time_offset + quot * time_mult + - * ((rem * time_mult) >> time_shift); + * cyc =3D time_cycles + ((cyc - time_cycles) & time_mask); + * delta =3D time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift); * * Where time_offset,time_mult,time_shift and cyc are read in the - * seqcount loop described above. This delta can then be added to - * enabled and possible running (if index), improving the scaling: + * seqcount loop described above. mul_u64_u32_shr will compute: + * + * (u64)(((unsigned __int128)cyc * time_mult) >> time_shift) + * + * This delta can then be added to enabled and possible running (if + * index) to improve the scaling. Due to event multiplexing, running + * may be zero and so care is needed to avoid division by zero. * * enabled +=3D delta; * if (index) * running +=3D delta; * - * quot =3D count / running; - * rem =3D count % running; - * count =3D quot * enabled + (rem * enabled) / running; + * if (running !=3D 0) { + * quot =3D count / running; + * rem =3D count % running; + * count =3D quot * enabled + (rem * enabled) / running; + * } */ __u16 time_shift; __u32 time_mult; --=20 2.37.0.170.g444d1eabd0-goog From nobody Sat Apr 18 01:21:32 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 CD919C43334 for ; Tue, 19 Jul 2022 22:40:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239869AbiGSWkC (ORCPT ); Tue, 19 Jul 2022 18:40:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239322AbiGSWj7 (ORCPT ); Tue, 19 Jul 2022 18:39:59 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 626E7606B9 for ; Tue, 19 Jul 2022 15:39:58 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-31e63e48e49so13550477b3.5 for ; Tue, 19 Jul 2022 15:39:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=VRrF+dBSPDqcThaK0WqPXtrk76bsoHtC+fEeOWlRUzU=; b=pwfZ3CGHR27Qu5kUsnkQ8LbcGR9WKjs93MVlEqd8s9FGYtSL1N0bObEsuUkPfQw7eg Wu+p8oNFONOzXApB0PJ1V3d6o5HOSofXMWpEqSp0QMvBoOJ3+HZFANNz0F85qyZv38aF ex3+GiFbRGe9DdiHGAMetBrj0qqt4/CXFgOUwmcRdPbGvsWtDefJFUtmvvmewwG4Mf1c 1ELBwcDQdzvJwI5mQrqyExoywmtl6so4/uQfocLuCveeUXd5Tm1Tr9gNmlGZvYq1twUA wXdAFF5YPseidYMAF14jyrqT+l0BxJcYisnKotGXE7U/2N+xCsAzCIu/zYvcGcvke+4C Xg5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VRrF+dBSPDqcThaK0WqPXtrk76bsoHtC+fEeOWlRUzU=; b=PlkPeWbuk7952jb0BCImk5qjTrU/c4BDuIe9nbqnij6+9cbVhWXnpm++1+r+UHlv5T nDgsRdl3dzg2MKVqsIW0SiCdmBsFohqIuhMLhUyGbIjfW60zp05E4uSu3hr0ffGOpfnk zysui3/iKe9lZuBv5s4RjhsQJbwUksg+A2oBI+ZxQ6ncKBdZZF+OaQyIEXxrrIQbXqSg VPWb0E8l/JG3OrH8VMXIt/3fRKBrU5v3XmbzN03CqRLzJroEKORqt956E9Zsv4ia+m2k 2d8vOV3HXQ5Q+ldZDUsY6A0rtA6mQAE0jiSVlpza8eO+rEsktYgGYRSF1QvKnce3FuSf KFFQ== X-Gm-Message-State: AJIora/4ZAHg3sh4M8fGEJADS05nwfLNlCHBb34xZE8H6NBi61GRpoET ziFXSB1OknKsrWM8462uVDotLoDJoOFI X-Google-Smtp-Source: AGRyM1urOvWBOjiXSlx4irMb/Gn3dwgNWJ1brN64nNXliYOtdD7bolN1rdUdPa5p9mIBdqN0Edp6Nut/Bwkf X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1251:e27b:f589:696c]) (user=irogers job=sendgmr) by 2002:a25:76c9:0:b0:66f:f3f0:5feb with SMTP id r192-20020a2576c9000000b0066ff3f05febmr21322768ybc.23.1658270397678; Tue, 19 Jul 2022 15:39:57 -0700 (PDT) Date: Tue, 19 Jul 2022 15:39:45 -0700 In-Reply-To: <20220719223946.176299-1-irogers@google.com> Message-Id: <20220719223946.176299-3-irogers@google.com> Mime-Version: 1.0 References: <20220719223946.176299-1-irogers@google.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog Subject: [PATCH v3 2/3] perf test: Remove x86 rdpmc test From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This test has been superseded by test_stat_user_read in: tools/lib/perf/tests/test-evsel.c The updated test doesn't divide-by-0 when running time of a counter is 0. It also supports ARM64. Acked-by: Rob Herring Signed-off-by: Ian Rogers --- tools/perf/arch/x86/tests/Build | 1 - tools/perf/arch/x86/tests/arch-tests.c | 2 - tools/perf/arch/x86/tests/rdpmc.c | 182 ------------------------- 3 files changed, 185 deletions(-) delete mode 100644 tools/perf/arch/x86/tests/rdpmc.c diff --git a/tools/perf/arch/x86/tests/Build b/tools/perf/arch/x86/tests/Bu= ild index 28d793390198..70b5bcbc15df 100644 --- a/tools/perf/arch/x86/tests/Build +++ b/tools/perf/arch/x86/tests/Build @@ -2,7 +2,6 @@ perf-$(CONFIG_DWARF_UNWIND) +=3D regs_load.o perf-$(CONFIG_DWARF_UNWIND) +=3D dwarf-unwind.o =20 perf-y +=3D arch-tests.o -perf-y +=3D rdpmc.o perf-y +=3D sample-parsing.o perf-$(CONFIG_AUXTRACE) +=3D insn-x86.o intel-pt-pkt-decoder-test.o perf-$(CONFIG_X86_64) +=3D bp-modify.o diff --git a/tools/perf/arch/x86/tests/arch-tests.c b/tools/perf/arch/x86/t= ests/arch-tests.c index 64fb73d14d2f..04018b8aa85b 100644 --- a/tools/perf/arch/x86/tests/arch-tests.c +++ b/tools/perf/arch/x86/tests/arch-tests.c @@ -3,7 +3,6 @@ #include "tests/tests.h" #include "arch-tests.h" =20 -DEFINE_SUITE("x86 rdpmc", rdpmc); #ifdef HAVE_AUXTRACE_SUPPORT DEFINE_SUITE("x86 instruction decoder - new instructions", insn_x86); DEFINE_SUITE("Intel PT packet decoder", intel_pt_pkt_decoder); @@ -14,7 +13,6 @@ DEFINE_SUITE("x86 bp modify", bp_modify); DEFINE_SUITE("x86 Sample parsing", x86_sample_parsing); =20 struct test_suite *arch_tests[] =3D { - &suite__rdpmc, #ifdef HAVE_DWARF_UNWIND_SUPPORT &suite__dwarf_unwind, #endif diff --git a/tools/perf/arch/x86/tests/rdpmc.c b/tools/perf/arch/x86/tests/= rdpmc.c deleted file mode 100644 index 498413ad9c97..000000000000 --- a/tools/perf/arch/x86/tests/rdpmc.c +++ /dev/null @@ -1,182 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "perf-sys.h" -#include "debug.h" -#include "tests/tests.h" -#include "cloexec.h" -#include "event.h" -#include // page_size -#include "arch-tests.h" - -static u64 rdpmc(unsigned int counter) -{ - unsigned int low, high; - - asm volatile("rdpmc" : "=3Da" (low), "=3Dd" (high) : "c" (counter)); - - return low | ((u64)high) << 32; -} - -static u64 rdtsc(void) -{ - unsigned int low, high; - - asm volatile("rdtsc" : "=3Da" (low), "=3Dd" (high)); - - return low | ((u64)high) << 32; -} - -static u64 mmap_read_self(void *addr) -{ - struct perf_event_mmap_page *pc =3D addr; - u32 seq, idx, time_mult =3D 0, time_shift =3D 0; - u64 count, cyc =3D 0, time_offset =3D 0, enabled, running, delta; - - do { - seq =3D pc->lock; - barrier(); - - enabled =3D pc->time_enabled; - running =3D pc->time_running; - - if (enabled !=3D running) { - cyc =3D rdtsc(); - time_mult =3D pc->time_mult; - time_shift =3D pc->time_shift; - time_offset =3D pc->time_offset; - } - - idx =3D pc->index; - count =3D pc->offset; - if (idx) - count +=3D rdpmc(idx - 1); - - barrier(); - } while (pc->lock !=3D seq); - - if (enabled !=3D running) { - u64 quot, rem; - - quot =3D (cyc >> time_shift); - rem =3D cyc & (((u64)1 << time_shift) - 1); - delta =3D time_offset + quot * time_mult + - ((rem * time_mult) >> time_shift); - - enabled +=3D delta; - if (idx) - running +=3D delta; - - quot =3D count / running; - rem =3D count % running; - count =3D quot * enabled + (rem * enabled) / running; - } - - return count; -} - -/* - * If the RDPMC instruction faults then signal this back to the test paren= t task: - */ -static void segfault_handler(int sig __maybe_unused, - siginfo_t *info __maybe_unused, - void *uc __maybe_unused) -{ - exit(-1); -} - -static int __test__rdpmc(void) -{ - volatile int tmp =3D 0; - u64 i, loops =3D 1000; - int n; - int fd; - void *addr; - struct perf_event_attr attr =3D { - .type =3D PERF_TYPE_HARDWARE, - .config =3D PERF_COUNT_HW_INSTRUCTIONS, - .exclude_kernel =3D 1, - }; - u64 delta_sum =3D 0; - struct sigaction sa; - char sbuf[STRERR_BUFSIZE]; - - sigfillset(&sa.sa_mask); - sa.sa_sigaction =3D segfault_handler; - sa.sa_flags =3D 0; - sigaction(SIGSEGV, &sa, NULL); - - fd =3D sys_perf_event_open(&attr, 0, -1, -1, - perf_event_open_cloexec_flag()); - if (fd < 0) { - pr_err("Error: sys_perf_event_open() syscall returned " - "with %d (%s)\n", fd, - str_error_r(errno, sbuf, sizeof(sbuf))); - return -1; - } - - addr =3D mmap(NULL, page_size, PROT_READ, MAP_SHARED, fd, 0); - if (addr =3D=3D (void *)(-1)) { - pr_err("Error: mmap() syscall returned with (%s)\n", - str_error_r(errno, sbuf, sizeof(sbuf))); - goto out_close; - } - - for (n =3D 0; n < 6; n++) { - u64 stamp, now, delta; - - stamp =3D mmap_read_self(addr); - - for (i =3D 0; i < loops; i++) - tmp++; - - now =3D mmap_read_self(addr); - loops *=3D 10; - - delta =3D now - stamp; - pr_debug("%14d: %14Lu\n", n, (long long)delta); - - delta_sum +=3D delta; - } - - munmap(addr, page_size); - pr_debug(" "); -out_close: - close(fd); - - if (!delta_sum) - return -1; - - return 0; -} - -int test__rdpmc(struct test_suite *test __maybe_unused, int subtest __mayb= e_unused) -{ - int status =3D 0; - int wret =3D 0; - int ret; - int pid; - - pid =3D fork(); - if (pid < 0) - return -1; - - if (!pid) { - ret =3D __test__rdpmc(); - - exit(ret); - } - - wret =3D waitpid(pid, &status, 0); - if (wret < 0 || status) - return -1; - - return 0; -} --=20 2.37.0.170.g444d1eabd0-goog From nobody Sat Apr 18 01:21:32 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 07995C43334 for ; Tue, 19 Jul 2022 22:40:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232742AbiGSWkG (ORCPT ); Tue, 19 Jul 2022 18:40:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239825AbiGSWkC (ORCPT ); Tue, 19 Jul 2022 18:40:02 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0DE361106 for ; Tue, 19 Jul 2022 15:40:00 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id o135-20020a25738d000000b0066f58989d75so12039632ybc.13 for ; Tue, 19 Jul 2022 15:40:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=5gP4EEO7fRYa/ZF979Fgov75wP7f4iW36KSqNRKIqtw=; b=SwJwQtLnoHivzH0++nMp8mt8ZCFfoEnbSEoXrSJyltk4j04W3e82JZSDLRBDB+xR9L dN28kWWpdnYg0MIJ3gPWKC0KyKCIY+k2f+g7avfdFGEH9jkrHlHlwGIurZaLUVan3sb2 agy9JKX8NoVweUh5zqeIrNRB7P/oFfCkc705GUmKyYpHf6Rd4tLlx0d99mPBhys2QLXp Ks7zjxAjUhHF9j5LUsuSPr/tF8aL1KT7vTdcNbG3XrgX4mKa+tmY7pktuWjAnrPDWg00 nauhm79HTvDcpblf8dD/CLjHYn0yUWRlMDxCNj36gy0r5dRZxVycz9+P/VPTCJZDj+Sh XC8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=5gP4EEO7fRYa/ZF979Fgov75wP7f4iW36KSqNRKIqtw=; b=6NkfN6TaFPR06eD+Tug3OKjhiRPVeuaBS/QNvYNVlxvSSJoPXT203E63/22CexQyZZ nnLU2Ak2s7FXKkrFrnf6actvSsLwEDDC5kstkaFoGfLaD2GqNWDjpY70eLYm6cKIkStt kuQzz2mwZNoHEu9X8sEHa10ZjSVqP2fgxNnzYuxSyBkK/O6hTu1JMVYMlnyS8lG97X0V Tje5Afy99oqLcLSnxajPiZqFrNfKT/Kia8/cDn4NlN/vPTlyyGuS3UeA6boBt8QnM4MZ 8mUG/1d0o0CNFr8TOHi6dNrsrB0PpnJbaEMdLIU+4Lm1yjWKJp/IzqpPLmY3xWsdbnIz mUPw== X-Gm-Message-State: AJIora/CQc5YelFR1aamoIQacyz6ct15J7nC3oFBlRDv6c9LwEOIZYdp a66Yuah/LWGlPUgL0y6+BHtGQIQuSihU X-Google-Smtp-Source: AGRyM1tNy/joF8aD47Ke5JvFXb/uYXvn6I/gafU5dLvOrfVHieMGmMhOjcI5z648M2/csI2UEQWZ4Uws+Dsj X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:1251:e27b:f589:696c]) (user=irogers job=sendgmr) by 2002:a81:57ca:0:b0:31c:f774:42ff with SMTP id l193-20020a8157ca000000b0031cf77442ffmr37186820ywb.286.1658270400226; Tue, 19 Jul 2022 15:40:00 -0700 (PDT) Date: Tue, 19 Jul 2022 15:39:46 -0700 In-Reply-To: <20220719223946.176299-1-irogers@google.com> Message-Id: <20220719223946.176299-4-irogers@google.com> Mime-Version: 1.0 References: <20220719223946.176299-1-irogers@google.com> X-Mailer: git-send-email 2.37.0.170.g444d1eabd0-goog Subject: [PATCH v3 3/3] perf test: Add user space counter reading tests From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Kajol Jain , Andi Kleen , Adrian Hunter , Anshuman Khandual , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Rob Herring Cc: Stephane Eranian , Ian Rogers Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" These tests are based on test_stat_user_read in tools/lib/perf/tests/test-evsel.c. The tests are modified to skip if perf_event_open fails or rdpmc isn't supported. Signed-off-by: Ian Rogers --- tools/perf/tests/mmap-basic.c | 127 +++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-) diff --git a/tools/perf/tests/mmap-basic.c b/tools/perf/tests/mmap-basic.c index 30bbe144648a..dfb6173b2a82 100644 --- a/tools/perf/tests/mmap-basic.c +++ b/tools/perf/tests/mmap-basic.c @@ -170,14 +170,139 @@ static int test__basic_mmap(struct test_suite *test = __maybe_unused, int subtest return err; } =20 +static int test_stat_user_read(int event) +{ + struct perf_counts_values counts =3D { .val =3D 0 }; + struct perf_thread_map *threads; + struct perf_evsel *evsel; + struct perf_event_mmap_page *pc; + struct perf_event_attr attr =3D { + .type =3D PERF_TYPE_HARDWARE, + .config =3D event, +#ifdef __aarch64__ + .config1 =3D 0x2, /* Request user access */ +#endif + }; + int err, i, ret =3D TEST_FAIL; + bool opened =3D false, mapped =3D false; + + threads =3D perf_thread_map__new_dummy(); + TEST_ASSERT_VAL("failed to create threads", threads); + + perf_thread_map__set_pid(threads, 0, 0); + + evsel =3D perf_evsel__new(&attr); + TEST_ASSERT_VAL("failed to create evsel", evsel); + + err =3D perf_evsel__open(evsel, NULL, threads); + if (err) { + pr_err("failed to open evsel: %s\n", strerror(-err)); + ret =3D TEST_SKIP; + goto out; + } + opened =3D true; + + err =3D perf_evsel__mmap(evsel, 0); + if (err) { + pr_err("failed to mmap evsel: %s\n", strerror(-err)); + goto out; + } + mapped =3D true; + + pc =3D perf_evsel__mmap_base(evsel, 0, 0); + if (!pc) { + pr_err("failed to get mmapped address\n"); + goto out; + } + + if (!pc->cap_user_rdpmc || !pc->index) { + pr_err("userspace counter access not %s\n", + !pc->cap_user_rdpmc ? "supported" : "enabled"); + ret =3D TEST_SKIP; + goto out; + } + if (pc->pmc_width < 32) { + pr_err("userspace counter width not set (%d)\n", pc->pmc_width); + goto out; + } + + perf_evsel__read(evsel, 0, 0, &counts); + if (counts.val =3D=3D 0) { + pr_err("failed to read value for evsel\n"); + goto out; + } + + for (i =3D 0; i < 5; i++) { + volatile int count =3D 0x10000 << i; + __u64 start, end, last =3D 0; + + pr_debug("\tloop =3D %u, ", count); + + perf_evsel__read(evsel, 0, 0, &counts); + start =3D counts.val; + + while (count--) ; + + perf_evsel__read(evsel, 0, 0, &counts); + end =3D counts.val; + + if ((end - start) < last) { + pr_err("invalid counter data: end=3D%llu start=3D%llu last=3D %llu\n", + end, start, last); + goto out; + } + last =3D end - start; + pr_debug("count =3D %llu\n", end - start); + } + ret =3D TEST_OK; + +out: + if (mapped) + perf_evsel__munmap(evsel); + if (opened) + perf_evsel__close(evsel); + perf_evsel__delete(evsel); + + perf_thread_map__put(threads); + return ret; +} + +static int test__mmap_user_read_instr(struct test_suite *test __maybe_unus= ed, + int subtest __maybe_unused) +{ + return test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS); +} + +static int test__mmap_user_read_cycles(struct test_suite *test __maybe_unu= sed, + int subtest __maybe_unused) +{ + return test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES); +} + static struct test_case tests__basic_mmap[] =3D { TEST_CASE_REASON("Read samples using the mmap interface", basic_mmap, "permissions"), + TEST_CASE_REASON("User space counter reading of instructions", + mmap_user_read_instr, +#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) + "permissions" +#else + "unsupported" +#endif + ), + TEST_CASE_REASON("User space counter reading of cycles", + mmap_user_read_cycles, +#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) + "permissions" +#else + "unsupported" +#endif + ), { .name =3D NULL, } }; =20 struct test_suite suite__basic_mmap =3D { - .desc =3D "Read samples using the mmap interface", + .desc =3D "mmap interface tests", .test_cases =3D tests__basic_mmap, }; --=20 2.37.0.170.g444d1eabd0-goog