From nobody Fri Sep 12 04:13:52 2025 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 3D361C636CC for ; Mon, 13 Feb 2023 19:08:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229890AbjBMTIC (ORCPT ); Mon, 13 Feb 2023 14:08:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229560AbjBMTH7 (ORCPT ); Mon, 13 Feb 2023 14:07:59 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE21FBB9C for ; Mon, 13 Feb 2023 11:07:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315277; x=1707851277; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nthHD7ORD3zn2LmHh/lLY5cOKkIBSk15uJqRpUpGiOc=; b=U9RvRuLY9cvI44iibSx2NrKWux7oU5di7PofNUO+4kj0Yh0QNlaLvH6V q/nTnE7+Upz+4CB9ADJWNi/rOVK6xkTj3gRHyHkG5k4zLWbP2cpysPzLs mYY9cI6nxGGvFTcoacLNmFHfGzsIazk5GVg5VlCqhqfUi5RZjfuPB4zjN G5Y0RC4xucxVYyRIcTnK0eN4Akdmd7tGJK1CjWu/FqNyyE0iMQqOE1Gmc IbLHc2PSYZ8Tl0vrrlD+H+r3xBb0KolHc3O8hxW3E+tAFukKqViU2p4H4 mTZIXCwtSfFXit11r9yvYHdd7ovArQU7Te4W9R4X8dDC3V41lU36PgZG5 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108679" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108679" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:07:57 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901912" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901912" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:07:57 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 1/9] timekeeping: Expose the conversion information of monotonic raw Date: Mon, 13 Feb 2023 11:07:46 -0800 Message-Id: <20230213190754.1836051-2-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The conversion information of monotonic raw is not affected by NTP/PTP correction. The perf tool can utilize the information to correctly calculate the monotonic raw via a TSC in each PEBS record in the post-processing stage. The current conversion information is hidden in the internal struct tk_read_base. Add a new external struct ktime_conv to store and share the conversion information with other subsystems. Add a new interface ktime_get_fast_mono_raw_conv() to expose the conversion information of monotonic raw. The function probably be invoked in a NMI. Use NMI safe tk_fast_raw to retrieve the conversion information. Signed-off-by: Kan Liang --- include/linux/timekeeping.h | 18 ++++++++++++++++++ kernel/time/timekeeping.c | 24 ++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/linux/timekeeping.h b/include/linux/timekeeping.h index fe1e467ba046..94ba02e7eb13 100644 --- a/include/linux/timekeeping.h +++ b/include/linux/timekeeping.h @@ -253,6 +253,21 @@ struct system_time_snapshot { u8 cs_was_changed_seq; }; =20 +/** + * struct ktime_conv - Timestamp conversion information + * @mult: Multiplier for scaled math conversion + * @shift: Shift value for scaled math conversion + * @xtime_nsec: Shifted (fractional) nano seconds offset for readout + * @base: (nanoseconds) base time for readout + */ +struct ktime_conv { + u64 cycle_last; + u32 mult; + u32 shift; + u64 xtime_nsec; + u64 base; +}; + /** * struct system_device_crosststamp - system/device cross-timestamp * (synchronized capture) @@ -297,6 +312,9 @@ extern void ktime_get_snapshot(struct system_time_snaps= hot *systime_snapshot); /* NMI safe mono/boot/realtime timestamps */ extern void ktime_get_fast_timestamps(struct ktime_timestamps *snap); =20 +/* NMI safe mono raw conv information */ +extern void ktime_get_fast_mono_raw_conv(struct ktime_conv *conv); + /* * Persistent clock related interfaces */ diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 5579ead449f2..a202b7a0a249 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -505,6 +505,30 @@ u64 notrace ktime_get_raw_fast_ns(void) } EXPORT_SYMBOL_GPL(ktime_get_raw_fast_ns); =20 +/** + * ktime_get_fast_mono_raw_conv - NMI safe access to get the conversion + * information of clock monotonic raw + * + * The conversion information is not affected by NTP/PTP correction. + */ +void ktime_get_fast_mono_raw_conv(struct ktime_conv *conv) +{ + struct tk_fast *tkf =3D &tk_fast_raw; + struct tk_read_base *tkr; + unsigned int seq; + + do { + seq =3D raw_read_seqcount_latch(&tkf->seq); + tkr =3D tkf->base + (seq & 0x01); + conv->cycle_last =3D tkr->cycle_last; + conv->mult =3D tkr->mult; + conv->shift =3D tkr->shift; + conv->xtime_nsec =3D tkr->xtime_nsec; + conv->base =3D tkr->base; + } while (read_seqcount_latch_retry(&tkf->seq, seq)); +} +EXPORT_SYMBOL_GPL(ktime_get_fast_mono_raw_conv); + /** * ktime_get_boot_fast_ns - NMI safe and fast access to boot clock. * --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 8325CC636D4 for ; Mon, 13 Feb 2023 19:08:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229911AbjBMTIF (ORCPT ); Mon, 13 Feb 2023 14:08:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229668AbjBMTH7 (ORCPT ); Mon, 13 Feb 2023 14:07:59 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14940CA1A for ; Mon, 13 Feb 2023 11:07:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315279; x=1707851279; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9lgoINWxU1sPPhF4j2mJcGx7Hoa3TJdaZRev5+44tes=; b=g2KxmUMVoIAHC1Crf3KbXVJOTw+tVN+7qO9huGhP9xjmfV1EuHY2esDs pTb5e5iKbXCiYdu4t29T65YaqNAdPf1l/YqGS0jhDMWB2/kRwOwOtMxAe sCLOFLlqTaRHSvuM+u+isJPB9xKuqVMVWEI0HFATCuXFTGIY9A+TeqRTi FYQvy2GxzNhq4y92D0GJWpUK0QP2FGTwfiLmJLie0lk+nCT+2SNQAaSEM uqC71kQ2y/A2qxb1lz/2jFIOjKtXJFEqtoU8Op510wiDc/76AluopGltm eHltr98S0PkhS7ON50ZI1GDrSyZr7Lsv2KAmEXbXP/qHCoIa/jOpiecFK g==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108696" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108696" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:07:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901920" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901920" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:07:58 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 2/9] perf: Extend ABI to support post-processing monotonic raw conversion Date: Mon, 13 Feb 2023 11:07:47 -0800 Message-Id: <20230213190754.1836051-3-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The monotonic raw clock is not affected by NTP/PTP correction. The calculation of the monotonic raw clock can be done in the post-processing, which can reduce the kernel overhead. Add hw_time in the struct perf_event_attr to tell the kernel dump the raw HW time to user space. The perf tool will calculate the HW time in post-processing. Currently, only supports the monotonic raw conversion. Only dump the raw HW time with PERF_RECORD_SAMPLE, because the accurate HW time can only be provided in a sample by HW. For other type of records, the user requested clock should be returned as usual. Nothing is changed. Add perf_event_mmap_page::cap_user_time_mono_raw ABI to dump the conversion information. The cap_user_time_mono_raw also indicates whether the monotonic raw conversion information is available. If yes, the clock monotonic raw can be calculated as mono_raw =3D base + ((cyc - last) * mult + nsec) >> shift Signed-off-by: Kan Liang --- include/uapi/linux/perf_event.h | 21 ++++++++++++++++++--- kernel/events/core.c | 7 +++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_even= t.h index ccb7f5dad59b..9d56fe027f6c 100644 --- a/include/uapi/linux/perf_event.h +++ b/include/uapi/linux/perf_event.h @@ -455,7 +455,8 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THR= EAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + hw_time : 1, /* generate raw HW time for samples */ + __reserved_1 : 25; =20 union { __u32 wakeup_events; /* wakeup every n events */ @@ -615,7 +616,8 @@ struct perf_event_mmap_page { cap_user_time : 1, /* The time_{shift,mult,offset} fields are used */ cap_user_time_zero : 1, /* The time_zero field is used */ cap_user_time_short : 1, /* the time_{cycle,mask} fields are used */ - cap_____res : 58; + cap_user_time_mono_raw : 1, /* The time_mono_* fields are used */ + cap_____res : 57; }; }; =20 @@ -692,11 +694,24 @@ struct perf_event_mmap_page { __u64 time_cycles; __u64 time_mask; =20 + /* + * If cap_user_time_mono_raw, the monotonic raw clock can be calculated + * from the hardware clock (e.g. TSC) 'cyc'. + * + * mono_raw =3D base + ((cyc - last) * mult + nsec) >> shift + * + */ + __u64 time_mono_last; + __u32 time_mono_mult; + __u32 time_mono_shift; + __u64 time_mono_nsec; + __u64 time_mono_base; + /* * Hole for extension of the self monitor capabilities */ =20 - __u8 __reserved[116*8]; /* align to 1k. */ + __u8 __reserved[112*8]; /* align to 1k. */ =20 /* * Control data for the mmap() data buffer. diff --git a/kernel/events/core.c b/kernel/events/core.c index 380476a934e8..f062cce2dafc 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -12135,6 +12135,13 @@ static int perf_copy_attr(struct perf_event_attr _= _user *uattr, if (attr->sigtrap && !attr->remove_on_exec) return -EINVAL; =20 + if (attr->use_clockid) { + /* + * Only support post-processing for the monotonic raw clock + */ + if (attr->hw_time && (attr->clockid !=3D CLOCK_MONOTONIC_RAW)) + return -EINVAL; + } out: return ret; =20 --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 884C8C636CC for ; Mon, 13 Feb 2023 19:08:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230365AbjBMTIO (ORCPT ); Mon, 13 Feb 2023 14:08:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230038AbjBMTIG (ORCPT ); Mon, 13 Feb 2023 14:08:06 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B181D20D13 for ; Mon, 13 Feb 2023 11:08:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315284; x=1707851284; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=c/ssmWV2rsADLLuYj8FPgrlgffTVyCWr1wk53uQoQzs=; b=T7nwOSAwKCm6JusTwbhnPqd2EAQXqjU3LA+xw2/vs8ASzYfLxmNhPTQb lVsorpwn3Ac+bnX/yd/6WtD6W3ZC2Ot+05YRfswS1Y3iL+K0SQE/ojlSC 038hyGVhIL6uQenesZsCvAR9lLruB9ba3EsVBR9Lnx5DIurWkOxBWrXeZ iIUsHr2hMdTiv/bFvABItsYGlaBIfqbB6mJeg3FwncTMlzUjLuPFM6ISM g//ZvxULY43xCZ05HhV333q1R3dpXMaXzb4bKpJUScmVK+N7J6AZ8awT/ +0OSoIrfTGotUXD5ZMsucvDg/dk65CZJStv38M+euJgSIwHaik9N5suMI A==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108728" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108728" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901940" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901940" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:03 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang , Ravi Bangoria Subject: [RFC PATCH V2 3/9] perf/x86: Factor out x86_pmu_sample_preload() Date: Mon, 13 Feb 2023 11:07:48 -0800 Message-Id: <20230213190754.1836051-4-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang Some common sample data are preloaded on X86 platforms before the sample output. For example, the branch stack information. Factor out a generic x86_pmu_sample_preload(). It will also be used later to preload the common HW time, TSC. Signed-off-by: Kan Liang Cc: Ravi Bangoria --- arch/x86/events/amd/core.c | 3 +-- arch/x86/events/core.c | 5 +---- arch/x86/events/intel/core.c | 3 +-- arch/x86/events/intel/ds.c | 3 +-- arch/x86/events/perf_event.h | 8 ++++++++ 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c index 8c45b198b62f..af7b3977efa8 100644 --- a/arch/x86/events/amd/core.c +++ b/arch/x86/events/amd/core.c @@ -928,8 +928,7 @@ static int amd_pmu_v2_handle_irq(struct pt_regs *regs) if (!x86_perf_event_set_period(event)) continue; =20 - if (has_branch_stack(event)) - perf_sample_save_brstack(&data, event, &cpuc->lbr_stack); + x86_pmu_sample_preload(&data, event, cpuc); =20 if (perf_event_overflow(event, &data, regs)) x86_pmu_stop(event, 0); diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 85a63a41c471..b19ac54ebeea 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -1703,10 +1703,7 @@ int x86_pmu_handle_irq(struct pt_regs *regs) =20 perf_sample_data_init(&data, 0, event->hw.last_period); =20 - if (has_branch_stack(event)) { - data.br_stack =3D &cpuc->lbr_stack; - data.sample_flags |=3D PERF_SAMPLE_BRANCH_STACK; - } + x86_pmu_sample_preload(&data, event, cpuc); =20 if (perf_event_overflow(event, &data, regs)) x86_pmu_stop(event, 0); diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 14f0a746257d..d9be5701e60a 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3036,8 +3036,7 @@ static int handle_pmi_common(struct pt_regs *regs, u6= 4 status) =20 perf_sample_data_init(&data, 0, event->hw.last_period); =20 - if (has_branch_stack(event)) - perf_sample_save_brstack(&data, event, &cpuc->lbr_stack); + x86_pmu_sample_preload(&data, event, cpuc); =20 if (perf_event_overflow(event, &data, regs)) x86_pmu_stop(event, 0); diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index 7980e92dec64..2f59573ed463 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1742,8 +1742,7 @@ static void setup_pebs_fixed_sample_data(struct perf_= event *event, if (x86_pmu.intel_cap.pebs_format >=3D 3) setup_pebs_time(event, data, pebs->tsc); =20 - if (has_branch_stack(event)) - perf_sample_save_brstack(data, event, &cpuc->lbr_stack); + x86_pmu_sample_preload(data, event, cpuc); } =20 static void adaptive_pebs_save_regs(struct pt_regs *regs, diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index d6de4487348c..ae6ec58fde14 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1185,6 +1185,14 @@ int x86_pmu_handle_irq(struct pt_regs *regs); void x86_pmu_show_pmu_cap(int num_counters, int num_counters_fixed, u64 intel_ctrl); =20 +static inline void x86_pmu_sample_preload(struct perf_sample_data *data, + struct perf_event *event, + struct cpu_hw_events *cpuc) +{ + if (has_branch_stack(event)) + perf_sample_save_brstack(data, event, &cpuc->lbr_stack); +} + extern struct event_constraint emptyconstraint; =20 extern struct event_constraint unconstrained; --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 3E9EBC636D4 for ; Mon, 13 Feb 2023 19:08:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230265AbjBMTIQ (ORCPT ); Mon, 13 Feb 2023 14:08:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230199AbjBMTIK (ORCPT ); Mon, 13 Feb 2023 14:08:10 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D5D6113FF for ; Mon, 13 Feb 2023 11:08:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315286; x=1707851286; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fxFiGFPky9M3poyi0730g1j+bTkHg+8oo/QfUKfpmfE=; b=W61ZLQ1LWzYrf4I3Ba/hdnO/+e9BkSaup7utoAXJIJU52KCsx9gCAB9H dM3ZN01qyZlYBnphcOM4KVRtaYHxINvdbig+XAYZ1U4SfRx2CUfENSWGv 22b1oirDuuqVze+bOapS2lEWZ0vpFBcIcASP3pElXzIkMkC1ksbWmxa28 TxabMpRJ6qhkQyooyeGjESIMauXyHfcyMQsKjxWrWr7SMFDyULL62vEEz lGfKOnlfoV31/Hd3qFNJ8K4PVbuPOTswYtAgqaOw+42rKDpW2aBPrlsS7 Afn8uBZYi7XQBICX/ao/aRdXQpacfuiADIDlcF0YrW5oa5Zsllon723lK A==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108738" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108738" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901943" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901943" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:05 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang , Ravi Bangoria Subject: [RFC PATCH V2 4/9] perf/x86: Enable post-processing monotonic raw conversion Date: Mon, 13 Feb 2023 11:07:49 -0800 Message-Id: <20230213190754.1836051-5-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The raw HW time is from TSC on X86. Preload the HW time for each sample, once the hw_time is set with the monotonic raw clock by the new perf tool. Also, dump the conversion information into mmap_page. For the legacy perf tool which doesn't know the hw_time, nothing is changed. Move the x86_pmu_sample_preload() before setup_pebs_time() to utilize the TSC from a PEBS record. Signed-off-by: Kan Liang Cc: Ravi Bangoria --- arch/x86/events/core.c | 10 ++++++++++ arch/x86/events/intel/ds.c | 14 +++++++++++--- arch/x86/events/perf_event.h | 12 ++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index b19ac54ebeea..7c1dfb8c763d 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -2740,6 +2740,16 @@ void arch_perf_update_userpage(struct perf_event *ev= ent, if (!event->attr.use_clockid) { userpg->cap_user_time_zero =3D 1; userpg->time_zero =3D offset; + } else if (perf_event_hw_time(event)) { + struct ktime_conv mono; + + userpg->cap_user_time_mono_raw =3D 1; + ktime_get_fast_mono_raw_conv(&mono); + userpg->time_mono_last =3D mono.cycle_last; + userpg->time_mono_mult =3D mono.mult; + userpg->time_mono_shift =3D mono.shift; + userpg->time_mono_nsec =3D mono.xtime_nsec; + userpg->time_mono_base =3D mono.base; } =20 cyc2ns_read_end(); diff --git a/arch/x86/events/intel/ds.c b/arch/x86/events/intel/ds.c index 2f59573ed463..10d4b63c891f 100644 --- a/arch/x86/events/intel/ds.c +++ b/arch/x86/events/intel/ds.c @@ -1574,6 +1574,12 @@ static void setup_pebs_time(struct perf_event *event, struct perf_sample_data *data, u64 tsc) { + u64 time =3D tsc; + + /* Perf tool does the conversion. No conversion here. */ + if (perf_event_hw_time(event)) + goto done; + /* Converting to a user-defined clock is not supported yet. */ if (event->attr.use_clockid !=3D 0) return; @@ -1588,7 +1594,9 @@ static void setup_pebs_time(struct perf_event *event, if (!using_native_sched_clock() || !sched_clock_stable()) return; =20 - data->time =3D native_sched_clock_from_tsc(tsc) + __sched_clock_offset; + time =3D native_sched_clock_from_tsc(tsc) + __sched_clock_offset; +done: + data->time =3D time; data->sample_flags |=3D PERF_SAMPLE_TIME; } =20 @@ -1733,6 +1741,8 @@ static void setup_pebs_fixed_sample_data(struct perf_= event *event, } } =20 + x86_pmu_sample_preload(data, event, cpuc); + /* * v3 supplies an accurate time stamp, so we use that * for the time stamp. @@ -1741,8 +1751,6 @@ static void setup_pebs_fixed_sample_data(struct perf_= event *event, */ if (x86_pmu.intel_cap.pebs_format >=3D 3) setup_pebs_time(event, data, pebs->tsc); - - x86_pmu_sample_preload(data, event, cpuc); } =20 static void adaptive_pebs_save_regs(struct pt_regs *regs, diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index ae6ec58fde14..0486ee6a7605 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1185,12 +1185,24 @@ int x86_pmu_handle_irq(struct pt_regs *regs); void x86_pmu_show_pmu_cap(int num_counters, int num_counters_fixed, u64 intel_ctrl); =20 +static inline bool perf_event_hw_time(struct perf_event *event) +{ + return (event->attr.hw_time && + event->attr.use_clockid && + (event->attr.clockid =3D=3D CLOCK_MONOTONIC_RAW)); +} + static inline void x86_pmu_sample_preload(struct perf_sample_data *data, struct perf_event *event, struct cpu_hw_events *cpuc) { if (has_branch_stack(event)) perf_sample_save_brstack(data, event, &cpuc->lbr_stack); + + if (perf_event_hw_time(event)) { + data->time =3D rdtsc(); + data->sample_flags |=3D PERF_SAMPLE_TIME; + } } =20 extern struct event_constraint emptyconstraint; --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 4632DC636D4 for ; Mon, 13 Feb 2023 19:08:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229602AbjBMTIU (ORCPT ); Mon, 13 Feb 2023 14:08:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230074AbjBMTIL (ORCPT ); Mon, 13 Feb 2023 14:08:11 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E39B76E80 for ; Mon, 13 Feb 2023 11:08:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315286; x=1707851286; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=X9aN6C+r13kwLYq+5iI5APWDkP47jg3B6kqADSWKNhM=; b=aKK74wCEIiSvFaYQeCUtkXBYhpexCr3jy2ES9NUgOznOWSeZEM7dAIQK 6An1x/xqeBVAVNv6QDXvC8A/L80kIpdCyd1jWNJ2Iu/ixsK415AkdwK1q d48BoRfQ5lnBnXGsGw4yhO8SY58O0S86zQt2/H8isprCnOjbkn6HtEQ+H AVLGYvPcwkHE0B8mitvNKxbK0frWC7B+Z4E/ihTm9r12oOT5ifxvrwjdr TNjcAa5DrLnBVKry+612yl39bdMcx3cIwgjDv53HBhCXZ3WQNnNx1ln/v Wi2Gp54cKHOuq+pcrongR2C5OBeo56mi3fuDD2yF34O3SBLaY5s9vDjKN w==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108757" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108757" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901950" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901950" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:06 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 5/9] perf/x86/intel: Enable large PEBS for monotonic raw Date: Mon, 13 Feb 2023 11:07:50 -0800 Message-Id: <20230213190754.1836051-6-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The monotonic raw clock is not affected by NTP/PTP correction. The monotonic raw clock can be calculated from the TSC of each PEBS record by the same conversion information. Signed-off-by: Kan Liang --- arch/x86/events/intel/core.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index d9be5701e60a..eac389e1f44c 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3776,7 +3776,8 @@ static unsigned long intel_pmu_large_pebs_flags(struc= t perf_event *event) { unsigned long flags =3D x86_pmu.large_pebs_flags; =20 - if (event->attr.use_clockid) + if (event->attr.use_clockid && + !((x86_pmu.intel_cap.pebs_format >=3D 3) && perf_event_hw_time(event)= )) flags &=3D ~PERF_SAMPLE_TIME; if (!event->attr.exclude_kernel) flags &=3D ~PERF_SAMPLE_REGS_USER; --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 5B3B8C636D4 for ; Mon, 13 Feb 2023 19:08:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230369AbjBMTI0 (ORCPT ); Mon, 13 Feb 2023 14:08:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230292AbjBMTIL (ORCPT ); Mon, 13 Feb 2023 14:08:11 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EB6C21A19 for ; Mon, 13 Feb 2023 11:08:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315288; x=1707851288; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ew5LHI5pCmCiUBMVzv2I8reY5qJGqW8V/hL9T+aryPM=; b=BBHEk5HPSfq9c+Jm60Gw/Ydm8f+79enTtISNBSd06JVpinuHb76jKCHo UPRQyFK8SgT0dx/ftqXrun8CVXP3Y1NLv78GP6/G2LoLQNJpuKYDfQut8 bl4mC2XW09kR/sMVLrh/0zt951Gd5bh2S35MWTjZ6x4Mknp7Jp+5lO488 Eig/p0xUMwbZ9c4DsSAIDMKcg4gr4eHtLucB7jlO0ZNyCSvWdmjUKZY2k 7FzvN/GBjyhRwLDAKHLIMTyYO9lilwQMK3Rzhd5DT64fLDmZ0wPvuzkBD oKJTpEU+muyIEOsVb/NIV2WrWt8roRcox/ofsU64xFg/LrzR+vmUhOImz Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108766" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108766" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:07 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901955" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901955" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:07 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 6/9] tools headers UAPI: Sync linux/perf_event.h with the kernel sources Date: Mon, 13 Feb 2023 11:07:51 -0800 Message-Id: <20230213190754.1836051-7-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The kernel ABI has been extended to support tool based monotonic raw conversion. This thus partially addresses this perf build warning: Warning: Kernel ABI header at 'tools/include/uapi/linux/perf_event.h' differs from latest version at 'include/uapi/linux/perf_event.h' diff -u tools/include/uapi/linux/perf_event.h include/uapi/linux/perf_event= .h Signed-off-by: Kan Liang --- tools/include/uapi/linux/perf_event.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tools/include/uapi/linux/perf_event.h b/tools/include/uapi/lin= ux/perf_event.h index ccb7f5dad59b..9d56fe027f6c 100644 --- a/tools/include/uapi/linux/perf_event.h +++ b/tools/include/uapi/linux/perf_event.h @@ -455,7 +455,8 @@ struct perf_event_attr { inherit_thread : 1, /* children only inherit if cloned with CLONE_THR= EAD */ remove_on_exec : 1, /* event is removed from task on exec */ sigtrap : 1, /* send synchronous SIGTRAP on event */ - __reserved_1 : 26; + hw_time : 1, /* generate raw HW time for samples */ + __reserved_1 : 25; =20 union { __u32 wakeup_events; /* wakeup every n events */ @@ -615,7 +616,8 @@ struct perf_event_mmap_page { cap_user_time : 1, /* The time_{shift,mult,offset} fields are used */ cap_user_time_zero : 1, /* The time_zero field is used */ cap_user_time_short : 1, /* the time_{cycle,mask} fields are used */ - cap_____res : 58; + cap_user_time_mono_raw : 1, /* The time_mono_* fields are used */ + cap_____res : 57; }; }; =20 @@ -692,11 +694,24 @@ struct perf_event_mmap_page { __u64 time_cycles; __u64 time_mask; =20 + /* + * If cap_user_time_mono_raw, the monotonic raw clock can be calculated + * from the hardware clock (e.g. TSC) 'cyc'. + * + * mono_raw =3D base + ((cyc - last) * mult + nsec) >> shift + * + */ + __u64 time_mono_last; + __u32 time_mono_mult; + __u32 time_mono_shift; + __u64 time_mono_nsec; + __u64 time_mono_base; + /* * Hole for extension of the self monitor capabilities */ =20 - __u8 __reserved[116*8]; /* align to 1k. */ + __u8 __reserved[112*8]; /* align to 1k. */ =20 /* * Control data for the mmap() data buffer. --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 20737C636CC for ; Mon, 13 Feb 2023 19:08:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230398AbjBMTI2 (ORCPT ); Mon, 13 Feb 2023 14:08:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230378AbjBMTIO (ORCPT ); Mon, 13 Feb 2023 14:08:14 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42FE712F26 for ; Mon, 13 Feb 2023 11:08:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315289; x=1707851289; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MsZexMlSjxnKR+I1ycQ9RqJ26PCvPIyRW1Mj53H7MC4=; b=PIESye95aYFhcKpERBIKzGflt8OP70tCxvDAgmPOq7yseNcaxR5/AK1s qjtlvwRZiU5hBGSXaOwgANi/9Z6WqBXXrVZ67dxvlPsFD1oneMjOFPJrS gK5l5cOdvQi9wOGGLK1lbAPX20UBGXEKFaxo2z1Ny8S2RUnGrfro3ugAp TP7bC3mSOc4cLs7lcefGviVBAumyQYFocp1eXCmAdvKA8k36JorvZ0GDX 0pvPA1hS5nCTdeH+UdlKFqKriW5XLGD3qE/7gsnPEiUVQC9nQc4qemA35 ZlYRyFuFMFbDR4tUfLbcfRiLJOCrg6ItdpoAwi8sIj2rxscfuKFDsPIz1 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108781" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108781" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:08 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901959" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901959" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:08 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 7/9] perf session: Support the monotonic raw clock conversion information Date: Mon, 13 Feb 2023 11:07:52 -0800 Message-Id: <20230213190754.1836051-8-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The monotonic raw clock conversion information can be retrieved from the perf_event_mmap_page::cap_user_time_mono_raw ABI. Store the information in the struct perf_record_time_conv for later usage. Dump the information in TIME_CONV event as well. Signed-off-by: Kan Liang --- tools/lib/perf/include/perf/event.h | 8 +++++++- tools/perf/util/session.c | 8 ++++++++ tools/perf/util/tsc.c | 22 +++++++++++++++++++++- tools/perf/util/tsc.h | 6 ++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/tools/lib/perf/include/perf/event.h b/tools/lib/perf/include/p= erf/event.h index ad47d7b31046..20187a3d84c8 100644 --- a/tools/lib/perf/include/perf/event.h +++ b/tools/lib/perf/include/perf/event.h @@ -422,9 +422,15 @@ struct perf_record_time_conv { __u64 time_zero; __u64 time_cycles; __u64 time_mask; + __u64 time_mono_last; + __u32 time_mono_mult; + __u32 time_mono_shift; + __u64 time_mono_nsec; + __u64 time_mono_base; __u8 cap_user_time_zero; __u8 cap_user_time_short; - __u8 reserved[6]; /* For alignment */ + __u8 cap_user_time_mono_raw; + __u8 reserved[5]; /* For alignment */ }; =20 struct perf_record_header_feature { diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 7c021c6cedb9..189149a7012f 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -995,6 +995,14 @@ static void perf_event__time_conv_swap(union perf_even= t *event, event->time_conv.time_cycles =3D bswap_64(event->time_conv.time_cycles); event->time_conv.time_mask =3D bswap_64(event->time_conv.time_mask); } + + if (event_contains(event->time_conv, time_mono_last)) { + event->time_conv.time_mono_last =3D bswap_64(event->time_conv.time_mono_= last); + event->time_conv.time_mono_mult =3D bswap_32(event->time_conv.time_mono_= mult); + event->time_conv.time_mono_shift =3D bswap_32(event->time_conv.time_mono= _shift); + event->time_conv.time_mono_nsec =3D bswap_64(event->time_conv.time_mono_= nsec); + event->time_conv.time_mono_base =3D bswap_64(event->time_conv.time_mono_= base); + } } =20 typedef void (*perf_event__swap_op)(union perf_event *event, diff --git a/tools/perf/util/tsc.c b/tools/perf/util/tsc.c index f19791d46e99..0b59c0f815f9 100644 --- a/tools/perf/util/tsc.c +++ b/tools/perf/util/tsc.c @@ -54,8 +54,14 @@ int perf_read_tsc_conversion(const struct perf_event_mma= p_page *pc, tc->time_zero =3D pc->time_zero; tc->time_cycles =3D pc->time_cycles; tc->time_mask =3D pc->time_mask; + tc->time_mono_last =3D pc->time_mono_last; + tc->time_mono_mult =3D pc->time_mono_mult; + tc->time_mono_shift =3D pc->time_mono_shift; + tc->time_mono_nsec =3D pc->time_mono_nsec; + tc->time_mono_base =3D pc->time_mono_base; tc->cap_user_time_zero =3D pc->cap_user_time_zero; tc->cap_user_time_short =3D pc->cap_user_time_short; + tc->cap_user_time_mono_raw =3D pc->cap_user_time_mono_raw; rmb(); if (pc->lock =3D=3D seq && !(seq & 1)) break; @@ -65,7 +71,7 @@ int perf_read_tsc_conversion(const struct perf_event_mmap= _page *pc, } } =20 - if (!tc->cap_user_time_zero) + if (!tc->cap_user_time_zero && !tc->cap_user_time_mono_raw) return -EOPNOTSUPP; =20 return 0; @@ -102,8 +108,14 @@ int perf_event__synth_time_conv(const struct perf_even= t_mmap_page *pc, event.time_conv.time_zero =3D tc.time_zero; event.time_conv.time_cycles =3D tc.time_cycles; event.time_conv.time_mask =3D tc.time_mask; + event.time_conv.time_mono_last =3D tc.time_mono_last; + event.time_conv.time_mono_mult =3D tc.time_mono_mult; + event.time_conv.time_mono_shift =3D tc.time_mono_shift; + event.time_conv.time_mono_nsec =3D tc.time_mono_nsec; + event.time_conv.time_mono_base =3D tc.time_mono_base; event.time_conv.cap_user_time_zero =3D tc.cap_user_time_zero; event.time_conv.cap_user_time_short =3D tc.cap_user_time_short; + event.time_conv.cap_user_time_mono_raw =3D tc.cap_user_time_mono_raw; =20 return process(tool, &event, NULL, machine); } @@ -138,5 +150,13 @@ size_t perf_event__fprintf_time_conv(union perf_event = *event, FILE *fp) tc->cap_user_time_short); } =20 + ret +=3D fprintf(fp, "... Cap Time Monotonic Raw %" PRId32 "\n", + tc->cap_user_time_mono_raw); + ret +=3D fprintf(fp, "... Time Last %" PRI_lu64 "\n", tc->time_mono= _last); + ret +=3D fprintf(fp, "... Time Multiplier %" PRId32 "\n", tc->time_mono_m= ult); + ret +=3D fprintf(fp, "... Time Shift %" PRId32 "\n", tc->time_mono_s= hift); + ret +=3D fprintf(fp, "... Time Nsec %" PRI_lu64 "\n", tc->time_mono= _nsec); + ret +=3D fprintf(fp, "... Time Base %" PRI_lu64 "\n", tc->time_mono= _base); + return ret; } diff --git a/tools/perf/util/tsc.h b/tools/perf/util/tsc.h index 88fd1c4c1cb8..6bacc450a14d 100644 --- a/tools/perf/util/tsc.h +++ b/tools/perf/util/tsc.h @@ -12,9 +12,15 @@ struct perf_tsc_conversion { u64 time_zero; u64 time_cycles; u64 time_mask; + u64 time_mono_last; + u32 time_mono_mult; + u32 time_mono_shift; + u64 time_mono_nsec; + u64 time_mono_base; =20 bool cap_user_time_zero; bool cap_user_time_short; + bool cap_user_time_mono_raw; }; =20 struct perf_event_mmap_page; --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 462ECC6379F for ; Mon, 13 Feb 2023 19:08:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231171AbjBMTIb (ORCPT ); Mon, 13 Feb 2023 14:08:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230204AbjBMTIO (ORCPT ); Mon, 13 Feb 2023 14:08:14 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D571E1F4B1 for ; Mon, 13 Feb 2023 11:08:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315289; x=1707851289; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k5xM53xjvbGCGQ5zJv4ziyGbnCGQidPVN7fJrmFsYRQ=; b=Gv/AwFIHrWB4+sz2xTHKInbBzV/TdVGB96JkIrA8a4vhStKe37ij6obV hM38cWxF9XloULEZ6Dtu7jzQ2kxbWL7O0dJgRTjQ4EPephIcdWK6xiLTI qSDxPEBjPKYBqvQO2Y3/lebIjEycGwlA0uJnEe7ACkNngDmjjcTGcWhof ggyhsb1SQj8yW8JP8XtHq4f6rnWQChVdn6YWd8DLHhAXiizR3u2vDz4/X YCxaY+En220UTC34iFnVsF9aWfkjd2mRFJ7wWC2Np6/9rcFCJ04PiBVOW DvcBsaZlCZAv7FX9e8/U5osZ7xuB4X0c1ZMKQX0HlxhiY8epj86XGhQBh Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108787" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108787" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901963" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901963" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:09 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 8/9] perf evsel, tsc: Support the monotonic raw clock conversion Date: Mon, 13 Feb 2023 11:07:53 -0800 Message-Id: <20230213190754.1836051-9-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The cap_user_time_mono_raw indicates that the kernel relies on the perf tool to convert the HW time to the monotonic raw clock. Add tsc_to_monotonic_raw() to do the conversion. The conversion information is stored in the session, which cannot be read in evsel parsing. Add a pointor in the evlist to point to the conversion information. Signed-off-by: Kan Liang --- tools/perf/util/evlist.h | 1 + tools/perf/util/evsel.c | 17 +++++++++++++++-- tools/perf/util/evsel.h | 7 +++++++ tools/perf/util/session.c | 1 + tools/perf/util/tsc.c | 12 ++++++++++++ tools/perf/util/tsc.h | 2 ++ 6 files changed, 38 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index 01fa9d592c5a..d860dc94009c 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -82,6 +82,7 @@ struct evlist { int pos; /* index at evlist core object to check signals */ } ctl_fd; struct event_enable_timer *eet; + struct perf_record_time_conv *time_conv; }; =20 struct evsel_str_handler { diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 999dd1700502..5e27ac2b9f9b 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -50,6 +50,7 @@ #include "off_cpu.h" #include "../perf-sys.h" #include "util/parse-branch-options.h" +#include "tsc.h" #include #include #include @@ -2349,6 +2350,18 @@ u64 evsel__bitfield_swap_branch_flags(u64 value) return new_val; } =20 +static u64 perf_evsel_parse_time(struct evsel *evsel, u64 time) +{ + /* + * The HW time can only be generated by HW events. + */ + if ((evsel->core.attr.clockid =3D=3D CLOCK_MONOTONIC_RAW) && + evsel->evlist->time_conv && evsel__is_hw_event(evsel)) + return tsc_to_monotonic_raw(evsel->evlist->time_conv, time); + + return time; +} + int evsel__parse_sample(struct evsel *evsel, union perf_event *event, struct perf_sample *data) { @@ -2411,7 +2424,7 @@ int evsel__parse_sample(struct evsel *evsel, union pe= rf_event *event, } =20 if (type & PERF_SAMPLE_TIME) { - data->time =3D *array; + data->time =3D perf_evsel_parse_time(evsel, *array); array++; } =20 @@ -2734,7 +2747,7 @@ int evsel__parse_sample_timestamp(struct evsel *evsel= , union perf_event *event, array++; =20 if (type & PERF_SAMPLE_TIME) - *timestamp =3D *array; + *timestamp =3D perf_evsel_parse_time(evsel, *array); =20 return 0; } diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index d572be41b960..d1ef67852bda 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -269,6 +269,13 @@ static inline bool evsel__is_bpf(struct evsel *evsel) return evsel->bpf_counter_ops !=3D NULL; } =20 +static inline bool evsel__is_hw_event(struct evsel *evsel) +{ + return (evsel->core.attr.type =3D=3D PERF_TYPE_HARDWARE) || + (evsel->core.attr.type =3D=3D PERF_TYPE_HW_CACHE) || + (evsel->core.attr.type =3D=3D PERF_TYPE_RAW); +} + #define EVSEL__MAX_ALIASES 8 =20 extern const char *const evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__M= AX_ALIASES]; diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 189149a7012f..d80d0c4e46da 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -1725,6 +1725,7 @@ static s64 perf_session__process_user_event(struct pe= rf_session *session, return tool->stat_round(session, event); case PERF_RECORD_TIME_CONV: session->time_conv =3D event->time_conv; + session->evlist->time_conv =3D &session->time_conv; return tool->time_conv(session, event); case PERF_RECORD_HEADER_FEATURE: return tool->feature(session, event); diff --git a/tools/perf/util/tsc.c b/tools/perf/util/tsc.c index 0b59c0f815f9..5264f9d54be4 100644 --- a/tools/perf/util/tsc.c +++ b/tools/perf/util/tsc.c @@ -160,3 +160,15 @@ size_t perf_event__fprintf_time_conv(union perf_event = *event, FILE *fp) =20 return ret; } + +u64 tsc_to_monotonic_raw(struct perf_record_time_conv *tc, u64 cyc) +{ + u64 delta; + + if (!tc->cap_user_time_mono_raw) + return cyc; + + delta =3D (cyc - tc->time_mono_last) * tc->time_mono_mult + tc->time_mono= _nsec; + delta >>=3D tc->time_mono_shift; + return tc->time_mono_base + delta; +} diff --git a/tools/perf/util/tsc.h b/tools/perf/util/tsc.h index 6bacc450a14d..2611d3de94b1 100644 --- a/tools/perf/util/tsc.h +++ b/tools/perf/util/tsc.h @@ -35,4 +35,6 @@ double arch_get_tsc_freq(void); =20 size_t perf_event__fprintf_time_conv(union perf_event *event, FILE *fp); =20 +u64 tsc_to_monotonic_raw(struct perf_record_time_conv *tc, u64 cyc); + #endif // __PERF_TSC_H --=20 2.35.1 From nobody Fri Sep 12 04:13:52 2025 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 EC47CC636D4 for ; Mon, 13 Feb 2023 19:08:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230463AbjBMTIe (ORCPT ); Mon, 13 Feb 2023 14:08:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230061AbjBMTIP (ORCPT ); Mon, 13 Feb 2023 14:08:15 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C18C813DE7 for ; Mon, 13 Feb 2023 11:08:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676315290; x=1707851290; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9vCwYtZX3hPfCSqvh+1wJUJFKXzNXPAQY4vmXXsWFxE=; b=fA4BsrQsZKY3Fo5eKyqf1I5EgTKh84RKa8Hw7J4t2d/eko5Y5C0rQLm3 8wMnbrTYdMclBXp65YZ6E6BYkdyt4MC7TF+eC6qyupiaSFj2edpc65kFu 8xbDNg0pyEEl566abfdhZrdVXgKP2+EcOCtUW5qrne2Ep86R4ZMcn3Xb2 M6E5nkpYzTkMqy/Dy8+OV5511AlA+QxUAaiGMRxMw8zbsyI10YKPiA4dI 6e1j9wyZcg+Pz1Qb6c54rEF20OiiClDzMWJkxFBiVXH+THDF0hNugwQha 37dZdtfqXqppDGU+lGb0a88ey75jUK/WUMOb1LhaGAon1QGym3ngT2EkX g==; X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="333108803" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="333108803" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 11:08:10 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10620"; a="668901968" X-IronPort-AV: E=Sophos;i="5.97,294,1669104000"; d="scan'208";a="668901968" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by orsmga002.jf.intel.com with ESMTP; 13 Feb 2023 11:08:10 -0800 From: kan.liang@linux.intel.com To: tglx@linutronix.de, jstultz@google.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: sboyd@kernel.org, eranian@google.com, namhyung@kernel.org, ak@linux.intel.com, adrian.hunter@intel.com, Kan Liang Subject: [RFC PATCH V2 9/9] perf evsel: Enable post-processing monotonic raw conversion by default Date: Mon, 13 Feb 2023 11:07:54 -0800 Message-Id: <20230213190754.1836051-10-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230213190754.1836051-1-kan.liang@linux.intel.com> References: <20230213190754.1836051-1-kan.liang@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Kan Liang The HW time is more accurate than the time recorded in the NMI handler. Set the hw_time by default for the monotonic raw clock, and convert the HW time to the monotonic raw clock in perf tool. For the legacy kernel which doesn't support the attr, nothing is changed. The monotonic raw clock is still from the time recorded in the NMI handler. Print the hw_time in perf_event_attr__fprintf Signed-off-by: Kan Liang --- tools/perf/util/evsel.c | 11 ++++++++++- tools/perf/util/evsel.h | 1 + tools/perf/util/perf_event_attr_fprintf.c | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index 5e27ac2b9f9b..d182c12fd291 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -1349,6 +1349,8 @@ void evsel__config(struct evsel *evsel, struct record= _opts *opts, if (opts->use_clockid) { attr->use_clockid =3D 1; attr->clockid =3D opts->clockid; + if (opts->clockid =3D=3D CLOCK_MONOTONIC_RAW) + attr->hw_time =3D 1; } =20 if (evsel->precise_max) @@ -1853,6 +1855,8 @@ static int __evsel__prepare_open(struct evsel *evsel,= struct perf_cpu_map *cpus, =20 static void evsel__disable_missing_features(struct evsel *evsel) { + if (perf_missing_features.hw_time) + evsel->core.attr.hw_time =3D 0; if (perf_missing_features.read_lost) evsel->core.attr.read_format &=3D ~PERF_FORMAT_LOST; if (perf_missing_features.weight_struct) { @@ -1906,7 +1910,12 @@ bool evsel__detect_missing_features(struct evsel *ev= sel) * Must probe features in the order they were added to the * perf_event_attr interface. */ - if (!perf_missing_features.read_lost && + if (!perf_missing_features.hw_time && + evsel->core.attr.hw_time) { + perf_missing_features.hw_time =3D true; + pr_debug2("switching off hw_time support\n"); + return true; + } else if (!perf_missing_features.read_lost && (evsel->core.attr.read_format & PERF_FORMAT_LOST)) { perf_missing_features.read_lost =3D true; pr_debug2("switching off PERF_FORMAT_LOST support\n"); diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index d1ef67852bda..c1d6fd40ea39 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -189,6 +189,7 @@ struct perf_missing_features { bool code_page_size; bool weight_struct; bool read_lost; + bool hw_time; }; =20 extern struct perf_missing_features perf_missing_features; diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/pe= rf_event_attr_fprintf.c index 7e5e7b30510d..7b3669430b87 100644 --- a/tools/perf/util/perf_event_attr_fprintf.c +++ b/tools/perf/util/perf_event_attr_fprintf.c @@ -154,6 +154,7 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_even= t_attr *attr, PRINT_ATTRf(sample_max_stack, p_unsigned); PRINT_ATTRf(aux_sample_size, p_unsigned); PRINT_ATTRf(sig_data, p_unsigned); + PRINT_ATTRf(hw_time, p_unsigned); =20 return ret; } --=20 2.35.1