Received: by 2002:a5d:925a:0:0:0:0:0 with SMTP id e26csp77916iol; Wed, 8 Jun 2022 22:29:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwCeInT1J2wzlZPDHalEQRltwTwHWthKo1caquxdANHoTJMMyINi40RDRZWnDfRY6z8ITKR X-Received: by 2002:aa7:dcc4:0:b0:42d:c666:62b5 with SMTP id w4-20020aa7dcc4000000b0042dc66662b5mr44250961edu.206.1654752586040; Wed, 08 Jun 2022 22:29:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654752586; cv=none; d=google.com; s=arc-20160816; b=SbIyMLI7qIGQKIf1+sLdkIDVLekoF+7wIcEWw19fUeSUQAVEX8Bfz5uwZFDwhX61VT /nu4IzLc8DTND4SZeMIiHIsTvyKzZS1nvpNS5YVfltxU3lYRbtASBpRZX6ebqfJU3YjW YmbUWDECgllP9TL3zx7nzFWWEp2rzaIlutIAGVRO/MYPoOzOa9rR9FNgT5i4W8aQP9ju Fc+Zmrvy4UVabgZqrxkJS7u2CvY5fz5GGN4QrIDZgFW1XeED/5hA5VYwAd36QvueIVWG 8r0G05do+6gZ2+nt3s0hfDSKXrcUm8kGVYYHKX/iGua0UCe8OoLSjxD+W2wOGtG7HqGZ QXqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=92Ap8HPAB8/MDXHmk/KascZglACyrXnQYkxdMYEdECc=; b=s2xEo8XA08in8FElUuXiIPz2+1LnHGb2H6BpcOkYj9cpPdOB7aCW2YR5kANuX80e45 apWWmOv3nh7gL+Ss1SSvjX9H6Tg+ggoSl/CJbCmdI3Gd+NS32aXLiodmeTHKk8TF/EmB d13t//DkYAOkWAuS4VjLv8StW4BJJIBOY1F35ZK/aAe91j48E4N+8V856OqWvfNJOLsr PG2P/g2Sj5UWlZqMLjE+dav+BrBUhbcEEM7rcbnVl2EdGWb7QoAS414c0TRjGNQLcTeV Qiv2khKG8WIXft8orzJ4sT4Zg2MNMS+8/Y6IMn0cYTFUaaYurOIgXQ45/tfjr8gVAlFf k4FQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=c+zLR0wV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sc31-20020a1709078a1f00b00711c9d7c469si5036080ejc.1006.2022.06.08.22.29.20; Wed, 08 Jun 2022 22:29:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=c+zLR0wV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237000AbiFIFYR (ORCPT + 99 others); Thu, 9 Jun 2022 01:24:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238277AbiFIFYK (ORCPT ); Thu, 9 Jun 2022 01:24:10 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 418FDE005 for ; Wed, 8 Jun 2022 22:24:07 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-2fb7cb07885so193781547b3.23 for ; Wed, 08 Jun 2022 22:24:07 -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=92Ap8HPAB8/MDXHmk/KascZglACyrXnQYkxdMYEdECc=; b=c+zLR0wVXL9kWNiEJ57kdQibv8Zr0VL9IAwXHVwAVZyRLzSx4FHmS08R8X92ZplbmI JwWF11N1zwiHTbW9SmZ4vb/QH4ZKShWHaH/YgSkPyTt2dpuEuLZxGAC4HzpAJIlK8g1F vzBlyDuOohjGG5ZK/cNkxV6wGysCTSaUjckk9Wmp8VXiGLRSNOZYW1/AXTWBn6yZGFc4 2Rib8pTuoWepYbhadEw+NZZlEayCVyc+nwqJ/flEGluYAL7WbBTYW1f+86at6Cy47Q5n nyKHKvQLgkVDkXo+19wYo+iKLuU7doOb4y0GRrkQCrmqdPP/G9OITHbvlijbbos7QKwR xNSg== 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=92Ap8HPAB8/MDXHmk/KascZglACyrXnQYkxdMYEdECc=; b=DSINLy9J6yY/xHJrNxmlAjJGgBmMnADozQmH7jG+Rxjsiy5N66Uhgn18wfs6Tm+969 mEz7kXbNxkmC23lQwDmOnoH/akOyV5bjlYksk5GOpAu59mhMr8nq1x1V535+0YsxoVd5 SMLbwLSSTFMYnJt6aQBD98YzPEziqE00Y0ROAdkCmpy0Xar3man2YbyBUKyV/6NrhXGe xBkM8rzDGjC8WEul7rze4tVDV9ZJeG3h4DBHNruWpt0BFKFaj7C1UOWkKLVwKjgA1tr6 NwZtKhIfbkpPECBCTrlwhgloDlUfAdJPiBgMAxMKnJ2N+yyAdTNQbZ4j65IRtkHxH9+Z oS8w== X-Gm-Message-State: AOAM531AmNRoVf35WTeVix+I54K2RaicaYsZGFAWwrueqbI0HFyxuBS6 u7vAH362nPByCzpdwVE+A3qxGVCppznu X-Received: from irogers.svl.corp.google.com ([2620:15c:2cd:202:ae20:7578:fb21:976b]) (user=irogers job=sendgmr) by 2002:a25:6583:0:b0:660:3a01:84f7 with SMTP id z125-20020a256583000000b006603a0184f7mr35986524ybb.130.1654752246414; Wed, 08 Jun 2022 22:24:06 -0700 (PDT) Date: Wed, 8 Jun 2022 22:23:53 -0700 In-Reply-To: <20220609052355.1300162-1-irogers@google.com> Message-Id: <20220609052355.1300162-3-irogers@google.com> Mime-Version: 1.0 References: <20220609052355.1300162-1-irogers@google.com> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog Subject: [PATCH v2 2/4] 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- include/uapi/linux/perf_event.h | 32 ++++++++++++++++----------- tools/include/uapi/linux/perf_event.h | 32 ++++++++++++++++----------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h index d37629dbad72..3b84e0ad0723 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 != running) { * cyc = rdtsc(); - * time_offset = pc->time_offset; * time_mult = pc->time_mult; * time_shift = pc->time_shift; + * time_offset = pc->time_offset; + * if (pc->cap_user_time_short) { + * time_cycles = pc->time_cycles; + * time_mask = pc->time_mask; + * } * } * * index = pc->index; @@ -548,6 +552,9 @@ struct perf_event_mmap_page { * if (pc->cap_user_rdpmc && index) { * width = pc->pmc_width; * pmc = rdpmc(index - 1); + * pmc <<= 64 - width; + * pmc >>= 64 - width; + * count += pmc; * } * * barrier(); @@ -590,25 +597,24 @@ 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 = (cyc >> time_shift); - * rem = cyc & (((u64)1 << time_shift) - 1); - * delta = time_offset + quot * time_mult + - * ((rem * time_mult) >> time_shift); + * cyc = time_cycles + ((cyc - time_cycles) & time_mask); + * delta = 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: + * enabled and possible running (if index) to improve the scaling. Due + * to event multiplexing, running maybe zero and so care is needed to + * avoid division by zero. * * enabled += delta; - * if (index) + * if (idx) * running += delta; * - * quot = count / running; - * rem = count % running; - * count = quot * enabled + (rem * enabled) / running; + * if (running != 0) { + * quot = count / running; + * rem = count % running; + * count = 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/linux/perf_event.h index d37629dbad72..3b84e0ad0723 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 != running) { * cyc = rdtsc(); - * time_offset = pc->time_offset; * time_mult = pc->time_mult; * time_shift = pc->time_shift; + * time_offset = pc->time_offset; + * if (pc->cap_user_time_short) { + * time_cycles = pc->time_cycles; + * time_mask = pc->time_mask; + * } * } * * index = pc->index; @@ -548,6 +552,9 @@ struct perf_event_mmap_page { * if (pc->cap_user_rdpmc && index) { * width = pc->pmc_width; * pmc = rdpmc(index - 1); + * pmc <<= 64 - width; + * pmc >>= 64 - width; + * count += pmc; * } * * barrier(); @@ -590,25 +597,24 @@ 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 = (cyc >> time_shift); - * rem = cyc & (((u64)1 << time_shift) - 1); - * delta = time_offset + quot * time_mult + - * ((rem * time_mult) >> time_shift); + * cyc = time_cycles + ((cyc - time_cycles) & time_mask); + * delta = 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: + * enabled and possible running (if index) to improve the scaling. Due + * to event multiplexing, running maybe zero and so care is needed to + * avoid division by zero. * * enabled += delta; - * if (index) + * if (idx) * running += delta; * - * quot = count / running; - * rem = count % running; - * count = quot * enabled + (rem * enabled) / running; + * if (running != 0) { + * quot = count / running; + * rem = count % running; + * count = quot * enabled + (rem * enabled) / running; + * } */ __u16 time_shift; __u32 time_mult; -- 2.36.1.255.ge46751e96f-goog