Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp19223798rwd; Wed, 28 Jun 2023 06:45:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5XEaRkhfaBnx/7ocpD5Mh6JZLu6vupMMeWTKxFXC34bbBbMWPO9oGOZlHiKvmuiCbyZHqS X-Received: by 2002:a50:fc16:0:b0:51d:b450:5e35 with SMTP id i22-20020a50fc16000000b0051db4505e35mr2176739edr.15.1687959905058; Wed, 28 Jun 2023 06:45:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687959905; cv=none; d=google.com; s=arc-20160816; b=BVAh+nPj59Tpb5Rko126kxp0nHtJIowro6vvBiDzO/+ESoDqzrnWdhqdhcpKZ6aeMj 63l7a2SpNtZoNZvb3p+goh9LZKgqs8xgYgjiMK3f58jCZcFpYj3lo8SVUNNJophcf+9d wzz1Zt/DH2aBznlVKBl3nTYQrdOc4FjnAfG7uBkuuNg+0M58zk2w+9014Tzy2NNZSPnU RUXSez+1nVxVgT+irFtsNayvvMZnDvS3csdu+PeydtihRl/e86Xjw3LpxFyMK2wrjSnY 2TPVjKHVxkwx29se+i5b5TGi2t8QA2vVD9sMa34c7+KNwt3/I0kf33//X2tSV+fU9uNO leVg== 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=HZHZOHg7jf2ckXQ/f5nX6ay78yoAjlgg8JvCDgP99T0=; fh=N2oIZrbUyE37b/w+7LH8ZPGi3WOLgXAJxYfUMucaoNA=; b=sYwhNRE6ISI9WPs9EJYtd42bbKx24FHaXKJHc++CI05JtcSHpgRv71aEw3yddwLGdW lY8bWuyunn9iq8s9/A/FpJCt1Zk3V3y1vo81CD3yhz1WMSMy3Tc5dmTyepqlukCN3GaX MFkGvVizLzFDd9sHdKwLs734Ck0AoGqGgjzAL6goexAVQaA23jxLS8wEJ2ncqYCLZsqs /fKORxUboVGh7hKF3Byub17pkA0y02y3pTGKhhIUTzY6Mi2jFP43NCYlhgjbewzPBHzh 39tS203uwjIuBDGUlou44x0Er9p69Lu/PTma3BEx4vGPbkfhd0ZlSEyerJ5XIfY7Q7HL cIjQ== 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 u18-20020aa7db92000000b0051bfc119d36si5014471edt.85.2023.06.28.06.44.39; Wed, 28 Jun 2023 06:45:05 -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 S232047AbjF1N3H (ORCPT + 99 others); Wed, 28 Jun 2023 09:29:07 -0400 Received: from foss.arm.com ([217.140.110.172]:55028 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232060AbjF1N1u (ORCPT ); Wed, 28 Jun 2023 09:27:50 -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 DA1A0C14; Wed, 28 Jun 2023 06:28:33 -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 BFAFE3F663; Wed, 28 Jun 2023 06:27:47 -0700 (PDT) Message-ID: Date: Wed, 28 Jun 2023 14:27:46 +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 1/1] perf: cs-etm: Fixes in instruction sample synthesis Content-Language: en-US To: Tanmay Jagdale Cc: "linux-arm-kernel@lists.infradead.org" , "linux-perf-users@vger.kernel.org" , "linux-kernel@vger.kernel.org" , Sunil Kovvuri Goutham , George Cherian , Linu Cherian , Anil Kumar Reddy H , "john.g.garry@oracle.com" , "will@kernel.org" , "mike.leach@linaro.org" , "suzuki.poulose@arm.com" , "peterz@infradead.org" , "mingo@redhat.com" , "acme@kernel.org" , "mark.rutland@arm.com" , "irogers@google.com" , "adrian.hunter@intel.com" , "coresight@lists.linaro.org" References: <20230623182204.25199-1-tanmay@marvell.com> <20230623182204.25199-2-tanmay@marvell.com> From: James Clark In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 28/06/2023 09:01, Tanmay Jagdale wrote: > Hi James, > >> On 23/06/2023 19:22, Tanmay Jagdale wrote: >>> The existing method of synthesizing instruction samples has the >>> following issues: >>> 1. Non-branch instructions have mnemonics of branch instructions. >>> 2. Branch target address is missing. >>> >>> Set the sample flags only when we reach the last instruction in >>> the tidq (which would be a branch instruction) to solve issue 1). >>> >>> To fix issue 2), start synthesizing the instructions from the >>> previous packet (tidq->prev_packet) instead of current packet >>> (tidq->packet). This way, it is easy to figure out the target >>> address of the branch instruction in tidq->prev_packet which >>> is the current packet's (tidq->packet) first executed instruction. >>> >>> After the switch to processing the previous packet first, we no >>> longer need to swap the packets during cs_etm__flush(). >> >> Hi Tanmay, >> >> I think the fix for setting the right flags and instruction type makes >> sense, but is it possible to do it without the change to swapping in >> cs_etm__flush() or some of the other changes to >> cs_etm__synth_instruction_sample()? > Thanks for the review. I took this approach of swapping because > it would be easy to figure out the target address of the branch. > If we don't swap the packet synthesis, then we must decode the > actual instruction to get the branch target address. > IMHO, this would be a complex change. > > Since the swapping approach is meant to solve issue 2) I will > split the patch while posting the next version. > >> >> I'm seeing some differences in the output related to the PID that's >> assigned to a sample and some of the addresses that aren't explained by >> the commit message. Also there is no corresponding change to >> cs_etm__synth_branch_sample(), which is also using prev_packet etc so >> I'm wondering if that's correct now without the swap? That function gets >> used with the default itrace options or itrace=b > IMHO the existing way to handle itrace=b or itrace is correct and > does not need any change since we are interested in only the last and > first instruction from tidq->prev_packet and tidq->packet respectively, > to generate the branching information. > >> >> For example if I run 'perf script --itrace=i100ns' and diff the output >> before and after your change I see a difference even though branch and >> instruction info isn't printed, so I wouldn't expect to see any changes. >> This is on a systemwide recording of a system under load. > Yes, the "tr start" mnemonic isn't printed in the flags column, but > rest of columns are the same. Will fix this in the next version. > > If you have observed any other differences, can you please share them ? > > Also, can you please share the test case commands so that I can run them > before submitting the next version ? It's just if you diff any existing recording it seems that there are differences. In this case below I see a difference in the ordering of the samples generated. In a more complicated case with a VM running I also see a difference in which PID is assigned to some samples. It's probably not related to the VM but just that there was more going on on the machine. It's not necessarily wrong, but we don't currently have any tests that verify the complete correctness of the decoding, so unless the commit message explains why there should be a difference we shouldn't make any changes. stress -c 4 & sudo perf record -e cs_etm// -a -m,16M -- sleep 3 sudo perf-before-change script --itrace=i1000i > before sudo perf-after-change script --itrace=i1000i > after diff before after 29d28 < stress 8198 [000] 1096.381602: 1000 instructions: aaaaceb70f2c rand@plt+0xc (/usr/bin/stress) 30a30 > stress 8198 [000] 1096.381602: 1000 instructions: aaaaceb70f2c rand@plt+0xc (/usr/bin/stress) 193d192 < stress 8200 [001] 1096.381602: 1000 instructions: aaaaceb70f28 rand@plt+0x8 (/usr/bin/stress) 194a194 > stress 8200 [001] 1096.381602: 1000 instructions: aaaaceb70f28 rand@plt+0x8 (/usr/bin/stress) > > Thanks and regards, > Tanmay >> >> Thanks >> James >> >>> >>> Signed-off-by: Tanmay Jagdale >>> --- >>> tools/perf/util/cs-etm.c | 32 +++++++++++++++++++++++++------- >>> 1 file changed, 25 insertions(+), 7 deletions(-) >>> >>> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c >>> index 91299cc56bf7..446e00d98fd5 100644 >>> --- a/tools/perf/util/cs-etm.c >>> +++ b/tools/perf/util/cs-etm.c >>> @@ -1418,10 +1418,26 @@ static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq, >>> sample.stream_id = etmq->etm->instructions_id; >>> sample.period = period; >>> sample.cpu = tidq->packet->cpu; >>> - sample.flags = tidq->prev_packet->flags; >>> sample.cpumode = event->sample.header.misc; >>> >>> - cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->packet, &sample); >>> + cs_etm__copy_insn(etmq, tidq->trace_chan_id, tidq->prev_packet, &sample); >>> + >>> + /* Populate branch target information only when we encounter >>> + * branch instruction, which is at the end of tidq->prev_packet. >>> + */ >>> + if (addr == (tidq->prev_packet->end_addr - 4)) { >>> + /* Update the perf_sample flags using the prev_packet >>> + * since that is the queue we are synthesizing. >>> + */ >>> + sample.flags = tidq->prev_packet->flags; >>> + >>> + /* The last instruction of the previous queue would be a >>> + * branch operation. Get the target of that branch by looking >>> + * into the first executed instruction of the current packet >>> + * queue. >>> + */ >>> + sample.addr = cs_etm__first_executed_instr(tidq->packet); >>> + } >>> >>> if (etm->synth_opts.last_branch) >>> sample.branch_stack = tidq->last_branch; >>> @@ -1641,7 +1657,7 @@ static int cs_etm__sample(struct cs_etm_queue *etmq, >>> /* Get instructions remainder from previous packet */ >>> instrs_prev = tidq->period_instructions; >>> >>> - tidq->period_instructions += tidq->packet->instr_count; >>> + tidq->period_instructions += tidq->prev_packet->instr_count; >>> >>> /* >>> * Record a branch when the last instruction in >>> @@ -1721,8 +1737,11 @@ static int cs_etm__sample(struct cs_etm_queue *etmq, >>> * been executed, but PC has not advanced to next >>> * instruction) >>> */ >>> + /* Get address from prev_packet since we are synthesizing >>> + * that in cs_etm__synth_instruction_sample() >>> + */ >>> addr = cs_etm__instr_addr(etmq, trace_chan_id, >>> - tidq->packet, offset - 1); >>> + tidq->prev_packet, offset - 1); >>> ret = cs_etm__synth_instruction_sample( >>> etmq, tidq, addr, >>> etm->instructions_sample_period); >>> @@ -1786,7 +1805,7 @@ static int cs_etm__flush(struct cs_etm_queue *etmq, >>> >>> /* Handle start tracing packet */ >>> if (tidq->prev_packet->sample_type == CS_ETM_EMPTY) >>> - goto swap_packet; >>> + goto reset_last_br; >>> >>> if (etmq->etm->synth_opts.last_branch && >>> etmq->etm->synth_opts.instructions && >>> @@ -1822,8 +1841,7 @@ static int cs_etm__flush(struct cs_etm_queue *etmq, >>> return err; >>> } >>> >>> -swap_packet: >>> - cs_etm__packet_swap(etm, tidq); >>> +reset_last_br: >>> >>> /* Reset last branches after flush the trace */ >>> if (etm->synth_opts.last_branch)