From patchwork Thu Apr 4 18:07:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tanmay Jagdale X-Patchwork-Id: 13618120 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7D227CD1292 for ; Thu, 4 Apr 2024 18:09:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=69JtwBXaqTSTWNgWxjHrr3W1DgfLGUf37xZFcadh1b0=; b=1kIVT8Fh7ijNkH WOrB2KRFWr8f0eY6MYdMKtypX71entmTPR5x3m0ux6T0XPXYtISxiyVkoR76kwwVVzTvSpnIshbHM 537/cs76uSySn9/2H76WOXQJATPauejPdV4pyIWZcfbskyp1hUt88P3+fjU/RdVEx2bGY7x7cL9Fo D+OGMwlhCE19Uvqyo9A7ziq5hy5NVf3PJhFzyHR8Epuw4m1SJEIBW1lXnitFB5Qzjp75WV6fCrWtg T8brhgbE9D+ooXSZRqnfFm5JJ3HgZ64Z+6eWXrUCVYY8mAUFaxVfc4Y1E+0criKWULybbs25rpvGv KknHsxb9djjILFYGEWgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rsRWK-00000003lDv-1EGM; Thu, 04 Apr 2024 18:09:04 +0000 Received: from mx0a-0016f401.pphosted.com ([67.231.148.174] helo=mx0b-0016f401.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rsRWC-00000003l9k-2CXU for linux-arm-kernel@lists.infradead.org; Thu, 04 Apr 2024 18:09:01 +0000 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 434BUfLl025075; Thu, 4 Apr 2024 11:08:36 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= pfpt0220; bh=TOabRFtQG8zLH9kv0ddXFT6E7a8XzuYFGYp40QV1b3E=; b=H0q 2WnWPB1tevJBBy4zjWgBujooBpMVStc4BYKzQe8MBd6NE72NqgsuM1XCHoFhDmDo DwgfSpjwRKXnTucls4GJIkJvT2k5WqzOKy1WB7a0Q2MTqxsYnFBleGi+dco8GDSO uxs3aLW85RiZzxQlVqR7vUjrpVQzEj/+EpRtsQXZcvtS+wuJfbqgLhkJa1TBuGEZ ZtztImiAAgsgRb/caYfr9ufXIjhsRSOYWj7ELUGmbPvq2sestNPhQEym0olGOv1H jmYwy0nGwwoIodnTNhmjPx8JK8Hk7zXxSntgAT4KXS7KsFjYK7vWmNAC1ksTp+AN Wp54613exdT1Jq2sOQA== Received: from dc5-exch05.marvell.com ([199.233.59.128]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3x9em4kkpr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 04 Apr 2024 11:08:35 -0700 (PDT) Received: from DC5-EXCH05.marvell.com (10.69.176.209) by DC5-EXCH05.marvell.com (10.69.176.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Thu, 4 Apr 2024 11:08:35 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH05.marvell.com (10.69.176.209) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Thu, 4 Apr 2024 11:08:34 -0700 Received: from POWEREDGE.marvell.com (POWEREDGE.marvell.com [10.28.8.3]) by maili.marvell.com (Postfix) with ESMTP id 6A0C93F7070; Thu, 4 Apr 2024 11:08:29 -0700 (PDT) From: Tanmay Jagdale To: , , , , , , , , , , , CC: , , , , , , , Tanmay Jagdale Subject: [PATCH V2 1/2] perf: cs-etm: Fixes in instruction sample synthesis Date: Thu, 4 Apr 2024 23:37:30 +0530 Message-ID: <20240404180731.7006-2-tanmay@marvell.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240404180731.7006-1-tanmay@marvell.com> References: <20240404180731.7006-1-tanmay@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 0jO92t8Yx8LSQamazqhXnznhbrOg62Xt X-Proofpoint-GUID: 0jO92t8Yx8LSQamazqhXnznhbrOg62Xt X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-04_14,2024-04-04_01,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240404_110856_605358_C6D6DFA3 X-CRM114-Status: GOOD ( 20.48 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The existing method of synthesizing instruction samples has the following issues: 1. Branch target address is missing. 2. Non-branch instructions have mnemonics of branch instructions. To fix issue 1), 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() Fix for issue 2) is to set the sample flags only when we reach the last instruction in the tidq (which would be a branch instruction). 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 d65d7485886c..55db1932f785 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1493,10 +1493,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; @@ -1717,7 +1733,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 @@ -1797,8 +1813,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); @@ -1862,7 +1881,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 && @@ -1898,8 +1917,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) From patchwork Thu Apr 4 18:07:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tanmay Jagdale X-Patchwork-Id: 13618121 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 53E9CCD1284 for ; Thu, 4 Apr 2024 18:09:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=d/abykPOSzOMwsP0lGvPGGdaw8Csno/BrvrZJxMKaVo=; b=2g02rlseHN8fUP qO0ht4ShA/V8dR+dEV3UuXV0QTB+Y8joud7ETDEU2++clXxSVOxBUzxC48Bi9vNAAbxih0DVSFH4M ba15L52iqEQGidBVqUGK20dC2JCNOIYj9Ml3tot8w32B+uizrtFPFu8yIXvPX7XL8gSjMAv9I8oig +AvJeThivIIy/u2ISQYyY3JmoZS/AhMG/YOqSzJfNVfy61WLmN7+xv2ebQK0nLPaxVGF8x0Ns9D2R JYUyKOQ71sdXreVaQSOz+PKupoEVVFqR9S6YZDt9Fp8UwTUWUo5XaQ2o6uarxt8WQfMdHYetl7D2T RnXHoM9jxhU/Zi36Mxkg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rsRWe-00000003lNB-3rHq; Thu, 04 Apr 2024 18:09:24 +0000 Received: from mx0b-0016f401.pphosted.com ([67.231.156.173]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rsRWW-00000003lJZ-38T0 for linux-arm-kernel@lists.infradead.org; Thu, 04 Apr 2024 18:09:20 +0000 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 434GsQgH030426; Thu, 4 Apr 2024 11:08:53 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h= from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= pfpt0220; bh=bs9rzbl/ZJsjnoCm4UWhViM7erxeHXkxZ5PmGyvl0I8=; b=aXj O9lqDdd0wiA3CPbcfwE1zgxKibz9wZCAF45eIvd6vtwgycbwtv7lyv0QGFfE4DJr vw6m0jYaUkAsg2lrBlnV6ycIPOy+/MeOs6X1R+oTIUBgSn7P0rhDlMsNlod9S3Lb GU1rtGlIZ6RPInEFZV06FZGsyN1kMGQJiomt5g1zll68zJ72lRV2SDHCyrv4o9b9 kHJfrhiIWvvGjtsZ4bArg1J5EFSvit2gZBDosRmXMLDknWv2oq0UCqvRfpQKcJH9 JpHbTbLU/SrQpC252FmyTSnOBjog5E1lUIN4DE6+IyYAdHquRnMlQ8LCBDZj+mw8 sKf3BAOpyK1EO2G153g== Received: from dc6wp-exch02.marvell.com ([4.21.29.225]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3x9em6kka2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 04 Apr 2024 11:08:52 -0700 (PDT) Received: from DC6WP-EXCH02.marvell.com (10.76.176.209) by DC6WP-EXCH02.marvell.com (10.76.176.209) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.4; Thu, 4 Apr 2024 11:08:52 -0700 Received: from maili.marvell.com (10.69.176.80) by DC6WP-EXCH02.marvell.com (10.76.176.209) with Microsoft SMTP Server id 15.2.1544.4 via Frontend Transport; Thu, 4 Apr 2024 11:08:51 -0700 Received: from POWEREDGE.marvell.com (POWEREDGE.marvell.com [10.28.8.3]) by maili.marvell.com (Postfix) with ESMTP id 32F4D3F7076; Thu, 4 Apr 2024 11:08:45 -0700 (PDT) From: Tanmay Jagdale To: , , , , , , , , , , , CC: , , , , , , , Tanmay Jagdale Subject: [PATCH V2 2/2] perf: cs-etm: Store previous timestamp in packet queue Date: Thu, 4 Apr 2024 23:37:31 +0530 Message-ID: <20240404180731.7006-3-tanmay@marvell.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240404180731.7006-1-tanmay@marvell.com> References: <20240404180731.7006-1-tanmay@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: nkJzkpKjPUXW5Ol3SCC4ytqvRjEW0W77 X-Proofpoint-GUID: nkJzkpKjPUXW5Ol3SCC4ytqvRjEW0W77 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-04_14,2024-04-04_01,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240404_110917_029178_7051FBC5 X-CRM114-Status: GOOD ( 18.97 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Since logic in cs_etm__sample is changed to synthesizing the previous packet (tidq->prev_packet) instead of current packet (tidq->packet), the first time this function is called, tidq->prev_packet is NULL and we return without processing anything. This is as expected but, in the first call, we would have a valid timestamp (stored in tidq->packet_queue.cs_timestamp) which belongs to tidq->packet. This would be lost due to no processing. Losing this timestamp results in all the synthesized packets being associated with the next timestamp and not their corresponding one. To fix this, introduce a new variable (prev_cs_timestamp) to store the queue's timestamp in cs_etm__sample(). When we start synthesizing the prev_packet, use this cached value instead of the current timestamp (cs_timestamp). Signed-off-by: Tanmay Jagdale --- tools/perf/util/cs-etm.c | 17 +++++++++++++---- tools/perf/util/cs-etm.h | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 55db1932f785..d5072c16fcd8 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1459,13 +1459,15 @@ u64 cs_etm__convert_sample_time(struct cs_etm_queue *etmq, u64 cs_timestamp) } static inline u64 cs_etm__resolve_sample_time(struct cs_etm_queue *etmq, - struct cs_etm_traceid_queue *tidq) + struct cs_etm_traceid_queue *tidq, + bool instruction_sample) { struct cs_etm_auxtrace *etm = etmq->etm; struct cs_etm_packet_queue *packet_queue = &tidq->packet_queue; if (!etm->timeless_decoding && etm->has_virtual_ts) - return packet_queue->cs_timestamp; + return instruction_sample ? packet_queue->prev_cs_timestamp : + packet_queue->cs_timestamp; else return etm->latest_kernel_timestamp; } @@ -1484,7 +1486,7 @@ static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq, event->sample.header.size = sizeof(struct perf_event_header); /* Set time field based on etm auxtrace config. */ - sample.time = cs_etm__resolve_sample_time(etmq, tidq); + sample.time = cs_etm__resolve_sample_time(etmq, tidq, true); sample.ip = addr; sample.pid = thread__pid(tidq->thread); @@ -1560,7 +1562,7 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq, event->sample.header.size = sizeof(struct perf_event_header); /* Set time field based on etm auxtrace config. */ - sample.time = cs_etm__resolve_sample_time(etmq, tidq); + sample.time = cs_etm__resolve_sample_time(etmq, tidq, false); sample.ip = ip; sample.pid = thread__pid(tidq->prev_packet_thread); @@ -1849,6 +1851,13 @@ static int cs_etm__sample(struct cs_etm_queue *etmq, } } + /* + * Since we synthesize the prev_packet, store the current timestamp + * here in prev_cs_timestamp so that when we *actually* synthesize + * the prev_packet, we use this timestamp and not the future one. + */ + tidq->packet_queue.prev_cs_timestamp = tidq->packet_queue.cs_timestamp; + cs_etm__packet_swap(etm, tidq); return 0; diff --git a/tools/perf/util/cs-etm.h b/tools/perf/util/cs-etm.h index 4696267a32f0..333eeb2c06a0 100644 --- a/tools/perf/util/cs-etm.h +++ b/tools/perf/util/cs-etm.h @@ -205,6 +205,7 @@ struct cs_etm_packet_queue { u32 instr_count; u64 cs_timestamp; /* Timestamp from trace data, converted to ns if possible */ u64 next_cs_timestamp; + u64 prev_cs_timestamp; struct cs_etm_packet packet_buffer[CS_ETM_PACKET_MAX_BUFFER]; };