Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp48230imu; Thu, 20 Dec 2018 16:40:04 -0800 (PST) X-Google-Smtp-Source: AFSGD/XG/N/7on2UZOliL3s5wzzx5iqbCdAvxdp+B1nfsv06kWLNtO6Px/Ao1v2sANSEVl1mK637 X-Received: by 2002:a62:848d:: with SMTP id k135mr355513pfd.47.1545352804174; Thu, 20 Dec 2018 16:40:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545352804; cv=none; d=google.com; s=arc-20160816; b=c0V4k77wtOPVOr66nxELjn7eYHOJbR1C6X/FpC9vx9tlApnxijSPBeWQXODiGfhWX1 DkcpoKhtloIRqixOlARLWreJlr5WyQtLTJcQlYmE49K8Zrf2aM+sgBMAWjA/GnkRcaCW JtGyTE4yc0NH/kUiUkrKqutUL5UIgK2ioFpTXu48sLryL5dZ08xOpKvmQpencmUhPKMc bwZGr8I4mF3MWks1nCDSqgYa5jvoG4HyWdYjIxtkLd+U1y+/eASY+kCI79avBFGEu9d2 pQIturvRQRLWHU9ad7mFpD/DyZYwV8WmQOQkReAFU5AMnz7zPedEv6PUNFWkvnmWsi/U jRzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date; bh=HLrU4HnuPTws2TP+H11PGkD1Rzt8Eb3sV1JKvgB9m8M=; b=o/tydsg3NFuyrjTccW4QegJM4Yh0NE9snd6CS+IC04ae+1m35LvKfRWivs0BDFdbUs yj6ARrn4CI9b/OUf+jjvd9q5t0WFT1B1RzaQQxcedEdWZxE2GCqLPaexCKWtGr0grVna wAGcROhRDBVvpjtHIwnS67NZl4UesXeADkkslVVHBzAkjGQUVjEnzKBWSu/sUGK1GSCW sxp/Y+/KAhR6Qudt083D5gqYf1cvjYIY7gKG0FDA04B5PhCfEFJOAYXLFFVqvc2X6699 Gks1mDOIV9tMLPMF9qkx9adP1dqllmxHyyNkkMt595mQWdsppqL+vOA7y7lb6KkQ8GsF Enrw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i35si19748106plg.396.2018.12.20.16.39.48; Thu, 20 Dec 2018 16:40:04 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732873AbeLTSOH (ORCPT + 99 others); Thu, 20 Dec 2018 13:14:07 -0500 Received: from terminus.zytor.com ([198.137.202.136]:43247 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732677AbeLTSOH (ORCPT ); Thu, 20 Dec 2018 13:14:07 -0500 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id wBKICsCp3682343 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 20 Dec 2018 10:12:54 -0800 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id wBKICs7L3682340; Thu, 20 Dec 2018 10:12:54 -0800 Date: Thu, 20 Dec 2018 10:12:54 -0800 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Leo Yan Message-ID: Cc: mike.leach@linaro.org, hpa@zytor.com, robert.walker@arm.com, acme@redhat.com, namhyung@kernel.org, jolsa@redhat.com, tglx@linutronix.de, leo.yan@linaro.org, mingo@kernel.org, alexander.shishkin@linux.intel.com, mathieu.poirier@linaro.org, linux-kernel@vger.kernel.org Reply-To: linux-kernel@vger.kernel.org, alexander.shishkin@linux.intel.com, mathieu.poirier@linaro.org, mingo@kernel.org, leo.yan@linaro.org, tglx@linutronix.de, namhyung@kernel.org, acme@redhat.com, jolsa@redhat.com, robert.walker@arm.com, hpa@zytor.com, mike.leach@linaro.org In-Reply-To: <1544513908-16805-3-git-send-email-leo.yan@linaro.org> References: <1544513908-16805-3-git-send-email-leo.yan@linaro.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf cs-etm: Avoid stale branch samples when flush packet Git-Commit-ID: 24fff5eb2b937951af5dce6cdb7a1e679ac8e751 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 24fff5eb2b937951af5dce6cdb7a1e679ac8e751 Gitweb: https://git.kernel.org/tip/24fff5eb2b937951af5dce6cdb7a1e679ac8e751 Author: Leo Yan AuthorDate: Tue, 11 Dec 2018 15:38:22 +0800 Committer: Arnaldo Carvalho de Melo CommitDate: Tue, 18 Dec 2018 12:23:59 -0300 perf cs-etm: Avoid stale branch samples when flush packet 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(-) 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 @@ swap_packet: 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;