Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp4600930rwd; Tue, 30 May 2023 07:33:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6pX34EJCeRs2l0FItddXjY2+R4OJsfSy8wqLwO7oBMr613kNQl+0nk+6152bMlHA5LdW8z X-Received: by 2002:a05:6a20:244f:b0:10c:37ed:3e94 with SMTP id t15-20020a056a20244f00b0010c37ed3e94mr12761719pzc.28.1685457196859; Tue, 30 May 2023 07:33:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685457196; cv=none; d=google.com; s=arc-20160816; b=ID5LvOpxjbytGERuSSwH0TgcsWytEUQAOu9616pp0sRXijaMoHD2LW81DQXtBTEBJE EEP4kMKkOVuG7ngzes0A8dJfsHSiqZWHScHto6XfFFaxS/o6xG7I8YcPiekPNtBXZ8sI AYyRJLJ6QBaCWMRDPWPyPfM4D7i2F5Y575ZbSFY5Ha69Ias7Gf7EKDzQKdIJMXkCX1Nk oqcAItkbluhuRBY4ggLxsZmrZ1Sspwl0009C7cVZ+4Ng5aqFsetLNTeN8DNV0jguwbgQ UYQzcqs5kCNpfUqAiLR/wSPV+qSo5HPBoxguXKnY3zafocFHEEhK30eCr2nP6gERfcxF oI+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=Ipj/z2YYdHwiEfvh2fKChr3Y2O51FY87AS49n/QVtEU=; b=vSVvQIHwZrKEFZ3tgRgo9u2ThQZ7cRdYpVOwx9b9tr8ui4GnYlx0ZyxXNwvu8iqOyh rPp639jPFKvb+q4kOu48oEiwxSRYMZUlWVyAMAre+nvMYY3BdlDcg3o/q2Z2acT+/y7C TIFM4n4GwtiIQEQW5YEsQdBr8Wo0iXs0mTdJddZ0O1nsL/Js6McVc0LSCpdRM+PtJYAO /QF8VizHkJQUZ3ARf0wYll76d1lIlu4CCA4jgzQSejnDlDULywX2/S3bOppsvnHnViuy z/MD4+/GYXogF2HtHUZ0/Pb3NfbXeyjLgt1aWhjYS3ZZmHPj/dHzDIhdTeh7q/e04He7 f5ew== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d2-20020a631d42000000b00533ffeaca3dsi10673283pgm.40.2023.05.30.07.33.00; Tue, 30 May 2023 07:33:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231147AbjE3O32 (ORCPT + 99 others); Tue, 30 May 2023 10:29:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230365AbjE3O3X (ORCPT ); Tue, 30 May 2023 10:29:23 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id CC78FE49; Tue, 30 May 2023 07:28:59 -0700 (PDT) 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 6E3A816F2; Tue, 30 May 2023 07:28:58 -0700 (PDT) Received: from [192.168.1.3] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B86233F663; Tue, 30 May 2023 07:28:10 -0700 (PDT) Message-ID: <630ab636-107d-4b12-5454-2ee91ad43543@arm.com> Date: Tue, 30 May 2023 15:28:09 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH 2/4] perf cs-etm: Use previous thread for branch sample source IP Content-Language: en-US To: Leo Yan Cc: coresight@lists.linaro.org, denik@chromium.org, Suzuki K Poulose , Mike Leach , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , John Garry , Will Deacon , linux-arm-kernel@lists.infradead.org, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org References: <20230524131958.2139331-1-james.clark@arm.com> <20230524131958.2139331-3-james.clark@arm.com> <20230527090635.GB886420@leoy-yangtze.lan> From: James Clark In-Reply-To: <20230527090635.GB886420@leoy-yangtze.lan> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27/05/2023 10:06, Leo Yan wrote: > On Wed, May 24, 2023 at 02:19:56PM +0100, James Clark wrote: >> Branch samples currently use the IP of the previous packet as the from >> IP, and the IP of the current packet as the to IP. But it incorrectly >> uses the current thread. In some cases like a jump into a different >> exception level this will attribute to the incorrect process. > > It's about the timing that branch has taken or not taken :) > > If we think the branch sample as 'branch has taken', then current code > is doning right thing, otherwise, we need this fix. > If you diff the outputs side by side you can see it mainly has an effect where there is a discontinuity. At this point we set either the from or the to IPs to 0. For example here is a before and after perf script output. Without the change it looks like stress was running before it actually was. The schedule function that was attributed to ls on the first line hasn't finished running yet. But it's attributed to stress on the second line even though the destination IP is 0 meaning we don't even know where it went. Before: ls 8350 [006] ... __schedule+0x394 => schedule+0x5c stress 8357 [006] ... schedule+0x84 => 0 [unknown] stress 8357 [006] ... 0 [unknown] => __unix_dgram_recvmsg+0x130 After: ls 8350 [006] ... __schedule+0x394 => schedule+0x5c ls 8357 [006] ... schedule+0x84 => 0 [unknown] stress 8357 [006] ... 0 [unknown] => __unix_dgram_recvmsg+0x130 I didn't see any decode differences that weren't around these discontinuity points, so it seems like a low risk change. >> Fix it by tracking the previous thread in the same way the previous >> packet is tracked. >> >> Signed-off-by: James Clark >> --- >> tools/perf/util/cs-etm.c | 9 +++++++-- >> 1 file changed, 7 insertions(+), 2 deletions(-) >> >> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c >> index ebffc9052561..a997fe79d458 100644 >> --- a/tools/perf/util/cs-etm.c >> +++ b/tools/perf/util/cs-etm.c >> @@ -86,6 +86,7 @@ struct cs_etm_traceid_queue { >> size_t last_branch_pos; >> union perf_event *event_buf; >> struct thread *thread; >> + struct thread *prev_thread; >> struct branch_stack *last_branch; >> struct branch_stack *last_branch_rb; >> struct cs_etm_packet *prev_packet; >> @@ -480,6 +481,7 @@ static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq, >> tidq->trace_chan_id = trace_chan_id; >> tidq->thread = machine__findnew_thread(&etm->session->machines.host, -1, >> queue->tid); >> + tidq->prev_thread = machine__idle_thread(&etm->session->machines.host); >> >> tidq->packet = zalloc(sizeof(struct cs_etm_packet)); >> if (!tidq->packet) >> @@ -616,6 +618,8 @@ static void cs_etm__packet_swap(struct cs_etm_auxtrace *etm, >> tmp = tidq->packet; >> tidq->packet = tidq->prev_packet; >> tidq->prev_packet = tmp; >> + thread__put(tidq->prev_thread); >> + tidq->prev_thread = thread__get(tidq->thread); > > Maybe cs_etm__packet_swap() is not the best place to update > "tidq->prev_thread", since swapping packet doesn't mean it's necessarily > thread switching; can we move this change into the cs_etm__set_thread()? > Yeah that might make more sense. I can move it there if we decide to keep this change. > Thanks, > Leo > >> } >> } >> >> @@ -791,6 +795,7 @@ static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq) >> /* Free this traceid_queue from the array */ >> tidq = etmq->traceid_queues[idx]; >> thread__zput(tidq->thread); >> + thread__zput(tidq->prev_thread); >> zfree(&tidq->event_buf); >> zfree(&tidq->last_branch); >> zfree(&tidq->last_branch_rb); >> @@ -1450,8 +1455,8 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, >> sample.time = cs_etm__resolve_sample_time(etmq, tidq); >> >> sample.ip = ip; >> - sample.pid = tidq->thread->pid_; >> - sample.tid = tidq->thread->tid; >> + sample.pid = tidq->prev_thread->pid_; >> + sample.tid = tidq->prev_thread->tid; >> sample.addr = cs_etm__first_executed_instr(tidq->packet); >> sample.id = etmq->etm->branches_id; >> sample.stream_id = etmq->etm->branches_id; >> -- >> 2.34.1 >>