From patchwork Tue Dec 18 22:06:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaldo Carvalho de Melo X-Patchwork-Id: 154207 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp4235506ljp; Tue, 18 Dec 2018 14:10:22 -0800 (PST) X-Google-Smtp-Source: AFSGD/UKL1trDY4iAttZiryFLFl/XofLsxUzgOFWNs6UfCfO51u+I1oCaOC9rXMi0IsPw57BbryR X-Received: by 2002:a17:902:a83:: with SMTP id 3mr16851475plp.276.1545171022663; Tue, 18 Dec 2018 14:10:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545171022; cv=none; d=google.com; s=arc-20160816; b=nJf5dtem0TvmCl6+2j36oQt/MfUxLG90Fe5cj3/GexuX+ZNrCf+pNEQPceSARo7AwN cIwH3MI21VpYOz7pFsdLlEn3VAHp/M5vNXJk0BhYyhXUdVMn+BFMkrJlTFe2pYJSozVp MDwP3Xfdupf/6ZrL2y+7C2Jp0lGu9yu7fjoVCJBiHTyC2SX2elqsAE1qVY3Ml2EWysxE 3bshXisYBIaFz+dpp8kcrUVrDHbb834NqW+JSu48awgwtWhZb5rToTiaVYU6zMFhWsRk RHq2s1Y3rftFdkQ07SmgXF/8rQIElZ5XkY+IjFgc80+EP9iZB5MSyszYtzDNXqRNv1Ry 89YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=5Ragp8JJ9pawjjjq3z+Si3FgK0OcE6KWIseyXde29Qs=; b=LOVcvC3GEIyg0FXU+cReVMR28VzARektJpCRWSRM+trRiCWi92WhL7/Kv3kOixgpl5 Nr9D21WRErhPHh60HYNoolZK/AbmLdR7pNueKO78sjQu/GkwlGQ0jOS7U9lpfj67Exxo kZCYJFP0B9XsYmyOb6uhW4tQRxHNEG0Qg94vfhsp1AdwIW3qz7kFQCuMGFjyHoH916QZ peRUK/oFU5OnWgy4/C/ZObvU/Wju/+unA9UQyZWNNTWDRsFz1jsdEBpakzfcPmIZfJkX 03jLUhP64tUWPzgWmsdlXGPdnsw2bps+cO+OUwTfenbEZGiy44IHN02auVozt5nKKfk9 ipuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=fFKnYZ3J; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n4si14356654pgm.487.2018.12.18.14.10.22; Tue, 18 Dec 2018 14:10:22 -0800 (PST) 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=@kernel.org header.s=default header.b=fFKnYZ3J; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728051AbeLRWKV (ORCPT + 31 others); Tue, 18 Dec 2018 17:10:21 -0500 Received: from mail.kernel.org ([198.145.29.99]:38784 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726962AbeLRWKU (ORCPT ); Tue, 18 Dec 2018 17:10:20 -0500 Received: from quaco.ghostprotocols.net (unknown [189.40.101.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7E462218A2; Tue, 18 Dec 2018 22:10:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1545171019; bh=dsy2YhwR2nlv+p5/mn/YuJE3yOKcsU29OQ88s+1DFDU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fFKnYZ3JN1bjXAUhFB788+4ZJO3vDbmCk519n9yd/4PobrnbVRldeDe0EOPKSTyJx MQMHSfpnnr0j5HMxSAtHFSTq0tHJPRKjBTVOFnMT5ZNrZxC5AlqzxtH30xWV0zPJt8 p885XvKcnP4Mlu5s8qlYzkO4wDojUkgJo3EXl5WA= From: Arnaldo Carvalho de Melo To: Ingo Molnar Cc: Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Leo Yan , Alexander Shishkin , Jiri Olsa , Mike Leach , Namhyung Kim , Robert Walker , coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, Arnaldo Carvalho de Melo Subject: [PATCH 27/63] perf cs-etm: Avoid stale branch samples when flush packet Date: Tue, 18 Dec 2018 19:06:57 -0300 Message-Id: <20181218220733.15839-28-acme@kernel.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181218220733.15839-1-acme@kernel.org> References: <20181218220733.15839-1-acme@kernel.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Leo Yan At the end of trace buffer handling, function cs_etm__flush() is invoked to flush any remaining branch stack entries. As a side effect, it also generates branch sample, because the 'etmq->packet' doesn't contains any new coming packet but point to one stale packet after packets swapping, so it wrongly makes synthesize branch samples with stale packet info. We could review below detailed flow which causes issue: Packet1: start_addr=0xffff000008b1fbf0 end_addr=0xffff000008b1fbfc Packet2: start_addr=0xffff000008b1fb5c end_addr=0xffff000008b1fb6c step 1: cs_etm__sample(): sample: ip=(0xffff000008b1fbfc-4) addr=0xffff000008b1fb5c step 2: flush packet in cs_etm__run_decoder(): cs_etm__run_decoder() `-> err = cs_etm__flush(etmq, false); sample: ip=(0xffff000008b1fb6c-4) addr=0xffff000008b1fbf0 Packet1 and packet2 are two continuous packets, when packet2 is the new coming packet, cs_etm__sample() generates branch sample for these two packets and use [packet1::end_addr - 4 => packet2::start_addr] as branch jump flow, thus we can see the first generated branch sample in step 1. At the end of cs_etm__sample() it swaps packets so 'etm->prev_packet'= packet2 and 'etm->packet'=packet1, so far it's okay for branch sample. If packet2 is the last one packet in trace buffer, even there have no any new coming packet, cs_etm__run_decoder() invokes cs_etm__flush() to flush branch stack entries as expected, but it also generates branch samples by taking 'etm->packet' as a new coming packet, thus the branch jump flow is as [packet2::end_addr - 4 => packet1::start_addr]; this is the second sample which is generated in step 2. So actually the second sample is a stale sample and we should not generate it. This patch introduces a new function cs_etm__end_block(), at the end of trace block this function is invoked to only flush branch stack entries and thus can avoid to generate branch sample for stale packet. Signed-off-by: Leo Yan Reviewed-by: Mathieu Poirier Cc: Alexander Shishkin Cc: Jiri Olsa Cc: Mike Leach Cc: Namhyung Kim Cc: Robert Walker Cc: coresight@lists.linaro.org Cc: linux-arm-kernel@lists.infradead.org Link: http://lkml.kernel.org/r/1544513908-16805-3-git-send-email-leo.yan@linaro.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/cs-etm.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) -- 2.19.2 diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 789707bc4e9e..ffc4fe5c0b7e 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1055,6 +1055,39 @@ static int cs_etm__flush(struct cs_etm_queue *etmq) return err; } +static int cs_etm__end_block(struct cs_etm_queue *etmq) +{ + int err; + + /* + * It has no new packet coming and 'etmq->packet' contains the stale + * packet which was set at the previous time with packets swapping; + * so skip to generate branch sample to avoid stale packet. + * + * For this case only flush branch stack and generate a last branch + * event for the branches left in the circular buffer at the end of + * the trace. + */ + if (etmq->etm->synth_opts.last_branch && + etmq->prev_packet->sample_type == CS_ETM_RANGE) { + /* + * Use the address of the end of the last reported execution + * range. + */ + u64 addr = cs_etm__last_executed_instr(etmq->prev_packet); + + err = cs_etm__synth_instruction_sample( + etmq, addr, + etmq->period_instructions); + if (err) + return err; + + etmq->period_instructions = 0; + } + + return 0; +} + static int cs_etm__run_decoder(struct cs_etm_queue *etmq) { struct cs_etm_auxtrace *etm = etmq->etm; @@ -1137,7 +1170,7 @@ static int cs_etm__run_decoder(struct cs_etm_queue *etmq) if (err == 0) /* Flush any remaining branch stack entries */ - err = cs_etm__flush(etmq); + err = cs_etm__end_block(etmq); } return err;