Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp405318pxj; Thu, 13 May 2021 07:38:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxQVhm45BFq2KZCBSVVUwE4Stq012SPLQXhKwoN2FNTH3yrmGll82n/znayLg+q0p4fFiGA X-Received: by 2002:a17:906:b6c5:: with SMTP id ec5mr44323148ejb.290.1620916727227; Thu, 13 May 2021 07:38:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620916727; cv=none; d=google.com; s=arc-20160816; b=sW5Ed8NXI1EIQ3jgAlkwqGnP3rkdMHQCNDBR8D0xzD/c9D+LqlqG/PQmQvYMPfVwH9 hSfxykVr4HE6c8GM4YEwTRRT+DKAkwHdmvDXN0MIaOmn/j1ooCsOH6SEOzDcaE0ZKHz5 TyIXvK7d/OKBKqNTENA0m9Is7t8IOCbkszaMS9C+qZoCCC7HE1fYayOrLvw15QHoJ5K9 XpoHVzAgDMbp7wznbmpC687AOk8oudjcSCMO2Xo5WFOOfAZD7V2eNTDTDPu/Tmsntru5 sdeZlXoGEbIvniIucNaDAuArMWl5ZywtsMwbiqm1EWtlF9ueWr6gTvCzr69U62hSO4rT cS1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ALSJqrMUK8xxLVLpxOT7YlLZfFutOwbOwFTQpIJAj8M=; b=N4D0n2dreu5vVa4MP0Uze7spPBoE4p0KSBAIV0AfyYcTGnLSig5uYDrdccBFoOqwHO 3acU0d0YYouLszMwlzvpC/OqLawQkphIvZpbXLWF9VHFTIM8EyT1bmi4r11GjZfylSoT 9hmQ29ucJv8EoIhA9VLUS9B6FRf3uHZHYpbACZQjEM/y3LiCYjAzYCi0akuI+PCU5Ach 8WX4bu8T0GCWwdvOAgsq/mJj63jaUTFPj2pA/G5VuZgEGNrtxgEoH0gYJ6TfeDXwVKD8 mwi5ZQBmAJtqJYg4aCC6WiAn6JGj6bqXEJ1cvEMpuqJXJSxU1/DWUzb55KAZostll1vb wGcA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z4si3164446eji.172.2021.05.13.07.38.20; Thu, 13 May 2021 07:38:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234596AbhEMOhP (ORCPT + 99 others); Thu, 13 May 2021 10:37:15 -0400 Received: from foss.arm.com ([217.140.110.172]:36766 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234553AbhEMOgx (ORCPT ); Thu, 13 May 2021 10:36:53 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4A8361764; Thu, 13 May 2021 07:35:42 -0700 (PDT) Received: from e121896.arm.com (unknown [10.57.81.122]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 766E03F73B; Thu, 13 May 2021 07:35:39 -0700 (PDT) From: James Clark To: coresight@lists.linaro.org, leo.yan@linaro.org Cc: al.grant@arm.com, branislav.rankov@arm.com, denik@chromium.org, suzuki.poulose@arm.com, anshuman.khandual@arm.com, James Clark , Mike Leach , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , John Garry , Will Deacon , Mathieu Poirier , linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/3] perf cs-etm: Prevent and warn on underflows during timestamp calculation. Date: Thu, 13 May 2021 17:35:20 +0300 Message-Id: <20210513143520.22694-4-james.clark@arm.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20210513143520.22694-1-james.clark@arm.com> References: <20210513143520.22694-1-james.clark@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a zero timestamp is encountered, warn once. This is to make hardware or configuration issues visible. Also suggest that the issue can be worked around with the --itrace=Z option. When an underflow with a non-zero timestamp occurrs, warn every time. This is an unexpected scenario, and with increasing timestamps, it's unlikely that it would occur more than once, therefore it should be ok to warn every time. Only try to calculate the timestamp by subtracting the instruction count if neither of the above cases are true. This makes attempting to decode files with zero timestamps in non-timeless mode more consistent. Currently it can half work if the timestamp wraps around and becomes non-zero, although the behavior is undefined and unpredictable. Signed-off-by: James Clark --- .../perf/util/cs-etm-decoder/cs-etm-decoder.c | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c index b01d363b9301..3e1a05bc82cc 100644 --- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c @@ -6,6 +6,7 @@ * Author: Mathieu Poirier */ +#include #include #include #include @@ -17,6 +18,7 @@ #include "cs-etm.h" #include "cs-etm-decoder.h" +#include "debug.h" #include "intlist.h" /* use raw logging */ @@ -294,7 +296,8 @@ cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq, static ocsd_datapath_resp_t cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq, const ocsd_generic_trace_elem *elem, - const uint8_t trace_chan_id) + const uint8_t trace_chan_id, + const ocsd_trc_index_t indx) { struct cs_etm_packet_queue *packet_queue; @@ -313,14 +316,33 @@ cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq, return OCSD_RESP_CONT; } - /* - * This is the first timestamp we've seen since the beginning of traces - * or a discontinuity. Since timestamps packets are generated *after* - * range packets have been generated, we need to estimate the time at - * which instructions started by subtracting the number of instructions - * executed to the timestamp. - */ - packet_queue->cs_timestamp = elem->timestamp - packet_queue->instr_count; + + if (!elem->timestamp) { + /* + * Zero timestamps can be seen due to misconfiguration or hardware bugs. + * Warn once, and don't try to subtract instr_count as it would result in an + * underflow. + */ + packet_queue->cs_timestamp = 0; + WARN_ONCE(true, "Zero Coresight timestamp found at Idx:%" OCSD_TRC_IDX_STR + ". Decoding may be improved with --itrace=Z...\n", indx); + } else if (packet_queue->instr_count > elem->timestamp) { + /* + * Sanity check that the elem->timestamp - packet_queue->instr_count would not + * result in an underflow. Warn and clamp at 0 if it would. + */ + packet_queue->cs_timestamp = 0; + pr_err("Timestamp calculation underflow at Idx:%" OCSD_TRC_IDX_STR "\n", indx); + } else { + /* + * This is the first timestamp we've seen since the beginning of traces + * or a discontinuity. Since timestamps packets are generated *after* + * range packets have been generated, we need to estimate the time at + * which instructions started by subtracting the number of instructions + * executed to the timestamp. + */ + packet_queue->cs_timestamp = elem->timestamp - packet_queue->instr_count; + } packet_queue->next_cs_timestamp = elem->timestamp; packet_queue->instr_count = 0; @@ -542,7 +564,7 @@ cs_etm_decoder__set_tid(struct cs_etm_queue *etmq, static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( const void *context, - const ocsd_trc_index_t indx __maybe_unused, + const ocsd_trc_index_t indx, const u8 trace_chan_id __maybe_unused, const ocsd_generic_trace_elem *elem) { @@ -579,7 +601,8 @@ static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer( break; case OCSD_GEN_TRC_ELEM_TIMESTAMP: resp = cs_etm_decoder__do_hard_timestamp(etmq, elem, - trace_chan_id); + trace_chan_id, + indx); break; case OCSD_GEN_TRC_ELEM_PE_CONTEXT: resp = cs_etm_decoder__set_tid(etmq, packet_queue, -- 2.28.0