Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp114411ybb; Tue, 31 Mar 2020 18:31:37 -0700 (PDT) X-Google-Smtp-Source: ADFU+vuj5ToNwKBGYe+vVHjeskbrQx5O68VQxXZ4/Br0F0/GGvqtEkaaiOHi7acxvNt/x/wjeg2R X-Received: by 2002:a05:6820:122:: with SMTP id i2mr15233860ood.73.1585704697331; Tue, 31 Mar 2020 18:31:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585704697; cv=none; d=google.com; s=arc-20160816; b=pCi6CTlqmCY8pLHOmT+ZObiwXHg0wBos0IY97X29taCv3H8Dqmwel8m5sP+za2A8gI ovGLGXQ1jlzIn6Yrv8qhXNhurrq6stE6h3AHpR2UNuIVb4z6/x/gzF75FmshQT1ECw5h yi5Lv6Dkqz3+/cgE/AydVdX+eQY1M1Hxk9Rru9nEFkvThkvVdCS9cC/3lA3NuJtO/h6y 9VLg4DyVrCfG2BBoTPt0mfNCOG2j2M4zwXaV20xPsaFpjNBI+aKShR+GcGvBPCo3F2Pg 35DBWSAZr+QfQGdwKguWrB/jdOsAyjLNMJXfRxUUmKNYgduAxpHWbsh3k3C7fPa+sTD0 lUHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:to :from:date:dkim-signature; bh=Aw32lMi9SSDdJS5PfKnpiYWuSlV1WvfCQUy80vI+qqk=; b=H9L77rc0x7E1NbxC9sGZCNZV4u6UwiOZkYhzDyptM01OQkjX0K+T+Q2Hr8DrCx0lzp I2zvZ9wRUbQwfm+FwldXM57xQv4Y2kE3696ludyTI0n+DMUCoK6xeL9vJC1kTeTC3zN1 PAAwHtJXyUGrTnuNzgNGWZ17p/zwO+SKCfwNtamwre/bQAtcOUIVYcc2BQjELySIQR2p d1mVTHBVVdMBYKgPNOdORWJYhNBEZoSrnqAujkybV/2E7SsKyyx52beYrlqwCxt0fpMC CO5OyIy97/N874kZhGSKmMPUTX5A/cTaM0ZDTCHqcuY82egOslN+OQPXjYBf4MMC1ah+ uztQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HaLcJN72; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u199si264089oif.110.2020.03.31.18.31.24; Tue, 31 Mar 2020 18:31:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HaLcJN72; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731669AbgDABYc (ORCPT + 99 others); Tue, 31 Mar 2020 21:24:32 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:35941 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731509AbgDABYc (ORCPT ); Tue, 31 Mar 2020 21:24:32 -0400 Received: by mail-pf1-f196.google.com with SMTP id i13so11255751pfe.3 for ; Tue, 31 Mar 2020 18:24:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Aw32lMi9SSDdJS5PfKnpiYWuSlV1WvfCQUy80vI+qqk=; b=HaLcJN72XZVuHbNNMetMUznMmDpsKnTpYIJ9YHL/1gep0jJFppLHcKhmxVhroiS7ey IRgCd+xMQpwRt+jKzOlB6ycHB/8oF6QtmttD0+KEEId8fo/h4vW8OPRbh8WF4VgU9DAb ymETk39riy8JVKNSpp7zUMf8lAYTshHf+lC3gF7Y7LKS8ALP+9uiLCPlX5/2WNTISwqM 2WZXtCTkWbFgsKZeHXWp0AenuxHRI6ai3WmKZHnh4GtLCIdj4d5pJCm2e/oW63Pki1Sh HI7IXhcz7aVonrE8Dp72e2UtZ7bjBzlK7sBxf3a3VAJ47R+4hbwaGtJp17pl8ZMWdIN3 NHkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Aw32lMi9SSDdJS5PfKnpiYWuSlV1WvfCQUy80vI+qqk=; b=kweLmAdkIHl28hNN5kFne4RTUaJDj2kCfCgfXQu7cSZJvcoQtx0yV2ru4Yv8fjxP4f sgbLv12y8XEiIVU0dw0ejYpIg71KojHJi2UXLENZZYNPZh/aUiMKbnt6N4IzgwNqb61f MAQcUYUNFzLRwRraYqWyEbkYNYEDnGRPZbyTv1yBqz/DCTdbVljxv1FpEPCXTd0FGTs4 AdZM+2Xtv28Rx8s2ytnY00j0FvCKBQ+9ig1cYqJCU45LU9z3utHmYa0fDGYJfLjJarZ2 u6j5brqH51G868XVsyFVw11qD8CGMtkmFWszAWTn45wzcup180sSNEFnOabtIdCpVZ0q 4UNw== X-Gm-Message-State: ANhLgQ0uyu9JlkWgBLWXvZcfQ+aA+g5CB/gXWdgmwtue84nfy7DqQYlW KvYv7uZZSHpMHQx3QAhUXmo66A== X-Received: by 2002:a63:c451:: with SMTP id m17mr20319106pgg.223.1585704271012; Tue, 31 Mar 2020 18:24:31 -0700 (PDT) Received: from leoy-ThinkPad-X240s ([2400:8902::f03c:91ff:fe3f:32da]) by smtp.gmail.com with ESMTPSA id q27sm297876pfn.173.2020.03.31.18.24.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 31 Mar 2020 18:24:30 -0700 (PDT) Date: Wed, 1 Apr 2020 09:24:17 +0800 From: Leo Yan To: Will Deacon , Mark Rutland , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Catalin Marinas , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Mathieu Poirier , Mike Leach , Al Grant , James Clark Subject: Re: [PATCH] arm64: perf_event: Fix time_offset for arch timer Message-ID: <20200401012417.GA9892@leoy-ThinkPad-X240s> References: <20200320093545.28227-1-leo.yan@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200320093545.28227-1-leo.yan@linaro.org> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 20, 2020 at 05:35:45PM +0800, Leo Yan wrote: > Between the system powering on and kernel's sched clock registration, > the arch timer usually has been enabled at the early time and its > counter is incremented during the period of the booting up. Thus the > arch timer's counter is not completely accounted into the sched clock, > and has a delta between the arch timer's counter and sched clock. This > delta value should be stored into userpg->time_offset, which later can > be retrieved by Perf tool in the user space for sample timestamp > calculation. > > Now userpg->time_offset is assigned to the negative sched clock with > '-now', this value cannot reflect the delta between arch timer's counter > and sched clock, so Perf cannot use it to calculate the sample time. > > To fix this issue, this patch calculate the delta between the arch > timer's and sched clock and assign the delta to userpg->time_offset. > The detailed steps are firstly to convert counter to nanoseconds 'ns', > then the offset is calculated as 'now' minus 'ns'. > > |<------------------- 'ns' ---------------------->| > |<-------- 'now' -------->| > |<---- time_offset ---->| > |-----------------------|-------------------------| > ^ ^ ^ > Power on system sched clock registration Perf starts > > Signed-off-by: Leo Yan > --- > arch/arm64/kernel/perf_event.c | 19 ++++++++++++++++++- > 1 file changed, 18 insertions(+), 1 deletion(-) Gentle ping ... Hi Mike R., Peter, If possible, could you give a look for this patch? Thank you, Leo > > diff --git a/arch/arm64/kernel/perf_event.c b/arch/arm64/kernel/perf_event.c > index e40b65645c86..226d25d77072 100644 > --- a/arch/arm64/kernel/perf_event.c > +++ b/arch/arm64/kernel/perf_event.c > @@ -1143,6 +1143,7 @@ void arch_perf_update_userpage(struct perf_event *event, > { > u32 freq; > u32 shift; > + u64 count, ns, quot, rem; > > /* > * Internal timekeeping for enabled/running/stopped times > @@ -1164,5 +1165,21 @@ void arch_perf_update_userpage(struct perf_event *event, > userpg->time_mult >>= 1; > } > userpg->time_shift = (u16)shift; > - userpg->time_offset = -now; > + > + /* > + * Since arch timer is enabled ealier than sched clock registration, > + * compuate the delta (in nanosecond unit) between the arch timer > + * counter and sched clock, assign the delta to time_offset and > + * perf tool can use it for timestamp calculation. > + * > + * The formula for conversion arch timer cycle to ns is: > + * quot = (cyc >> time_shift); > + * rem = cyc & ((1 << time_shift) - 1); > + * ns = quot * time_mult + ((rem * time_mult) >> time_shift); > + */ > + count = arch_timer_read_counter(); > + quot = count >> shift; > + rem = count & ((1 << shift) - 1); > + ns = quot * userpg->time_mult + ((rem * userpg->time_mult) >> shift); > + userpg->time_offset = now - ns; > } > -- > 2.17.1 >