From patchwork Thu Mar 27 11:11:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tanmay Jagdale X-Patchwork-Id: 14031064 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 767D8C3600B for ; Thu, 27 Mar 2025 11:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding: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=bivTPowlGQmTy7yuDT95E9A5Mfe9xptdQTOsUs2Gez4=; b=NSKX3AjfJ3jXeB+rxo5yYCBl2p ZlGqyUMUUagBgKI7hznI/bBuq6XATX+UEmxNmHSBQn+fPXYUCS/SgqUWR8S2HeSBw8AjAXiKp0hg2 /gY5YoeGrnNfCqAIyMtRZJDzB4VyDykSLXLKcAV+WB7RtDaAuxCRhLurgufpW7IPhFc0QkKocXWBS x6NfFqOr1Hld3qAr7No8FeONo03YFKer1Ni0gaKTIku2PGr3nmrHv/ItwxYlf6m91ZXRZiES5mX+9 8yYp8kpRxBHyfQOwsgvmYifuvKmjAOD2RE+QNZFyBsNTmNzlKevDzfQQz2zbLizEfo98HeM78BBFN 1zV0Rptg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1txlDM-0000000AnyF-1fp8; Thu, 27 Mar 2025 11:16:00 +0000 Received: from mx0a-0016f401.pphosted.com ([67.231.148.174] helo=mx0b-0016f401.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1txlA3-0000000AnYX-1o26 for linux-arm-kernel@lists.infradead.org; Thu, 27 Mar 2025 11:12:36 +0000 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 52R75Ueg012794; Thu, 27 Mar 2025 04:12:27 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pfpt0220; bh=b ivTPowlGQmTy7yuDT95E9A5Mfe9xptdQTOsUs2Gez4=; b=GCfVK1Fm9nYf1CVra aByeWaYFmg/DqXI+obKiwzGOpnU/R45mdYD/kjAVKdLpLPOjDIi5qOci89QBvVeo r7YF1YXxwig59jOGlf4HHbQCuHX6k1IORVKEscjdftPFaR88dSxIh1zxZ/c4QoIV UD3aFY3SVyNQV0o7ReWkh/CNDuTAE3rNemuUHWSlo11dBgyJR3M1oBLxcGb2F7ot H6tu8u5aT0ckZUr6PZyVTm/zw8G7jmIZIg9xY2gyH1dLQWqqJH3+FzjmyKiO0Bc7 66CYncNhNZx5aonAjK0t+4aU7I2X+zztRkRlkIMK7QAI0PyxQZlO/kJr5vk8b2eH iVX7Q== Received: from dc5-exch05.marvell.com ([199.233.59.128]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 45n1yv8gde-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 27 Mar 2025 04:12:26 -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, 27 Mar 2025 04:12:26 -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, 27 Mar 2025 04:12:26 -0700 Received: from optiplex.marvell.com (unknown [10.28.34.253]) by maili.marvell.com (Postfix) with ESMTP id 8C3993F7060; Thu, 27 Mar 2025 04:12:21 -0700 (PDT) From: Tanmay Jagdale To: , , , , , , , CC: , , , , , , , Tanmay Jagdale , James Clark Subject: [PATCH V3 1/2] perf: cs-etm: Fixes in instruction sample synthesis Date: Thu, 27 Mar 2025 16:41:48 +0530 Message-ID: <20250327111149.461012-2-tanmay@marvell.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250327111149.461012-1-tanmay@marvell.com> References: <20250327111149.461012-1-tanmay@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: huwJN9WSawLQ9rR4h18eHSgrnx9flAsg X-Authority-Analysis: v=2.4 cv=dKCmmPZb c=1 sm=1 tr=0 ts=67e5329a cx=c_pps a=rEv8fa4AjpPjGxpoe8rlIQ==:117 a=rEv8fa4AjpPjGxpoe8rlIQ==:17 a=Vs1iUdzkB0EA:10 a=M5GUcnROAAAA:8 a=7CQSdrXTAAAA:8 a=NoWHmBFwJINbK3vHNx0A:9 a=OBjm3rFKGHvpk9ecZwUJ:22 a=a-qgeE7W1pNrGK8U0ZQC:22 X-Proofpoint-GUID: huwJN9WSawLQ9rR4h18eHSgrnx9flAsg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1095,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-03-27_01,2025-03-26_02,2024-11-22_01 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250327_041235_490437_EC80D79C X-CRM114-Status: GOOD ( 19.12 ) 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 to synthesize instruction samples has the following issues: 1. Branch instruction mnemonics were being added to non-branch instructions too. 2. Branch target address was missing To fix the issues, start synthesizing the instructions from the previous packet (tidq->prev_packet) instead of current packet (tidq->packet). This way it's 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. Since we have now switched to processing the previous packet first, we need not swap the packets during cs_etm__flush(). Signed-off-by: Tanmay Jagdale Reviewed-by: James Clark --- 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 0bf9e5c27b59..ebed5b98860e 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1576,10 +1576,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; @@ -1771,7 +1787,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 @@ -1851,8 +1867,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); @@ -1916,7 +1935,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 && @@ -1952,8 +1971,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 Mar 27 11:11:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tanmay Jagdale X-Patchwork-Id: 14031065 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 8162EC3600B for ; Thu, 27 Mar 2025 11:17:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type: Content-Transfer-Encoding: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=pbpkHIovNPqcQ5fu1aW16jtnIw8TYXiKiBmahzxX8zY=; b=nsPvUnBN8NVIFsxchw/SibFoTT W6yHUM4TS8loqWgM/dJaYRHzDzfFajoWreDA6hf5e1Rw6N24+QEFpHWkDmSzfnhAj93sH//H+AQ3i lGk/+R2LoJ61YQ8Yh/awDx987j5b4vYiDQAoSkxCkjMPD/BExTA87CRwDnfxmEip1HwIpm1FbbE/P kBCy1aTEb40Uut0vyoUWzNXnahfdYMJ1cSJsqsgwE+U1ZJuM1YyOHj7S5npO3djW0XCX61iUUjP3o Js54iNKNjGLZa0Xj88ZVWQcET+QM2eWcK8evCFAzVn1S71vXgWnxvB5ZnF+7KPgL1GDhVAjiJ2tlH ct6EtfqA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1txlF4-0000000AoEM-0XU0; Thu, 27 Mar 2025 11:17:46 +0000 Received: from mx0a-0016f401.pphosted.com ([67.231.148.174] helo=mx0b-0016f401.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1txlAK-0000000Anam-0pER for linux-arm-kernel@lists.infradead.org; Thu, 27 Mar 2025 11:12:53 +0000 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 52R75sHT013524; Thu, 27 Mar 2025 04:12:43 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=pfpt0220; bh=p bpkHIovNPqcQ5fu1aW16jtnIw8TYXiKiBmahzxX8zY=; b=U7AmohYtbyOlUMH+k acDChOZxhPQMVIIUuq7Y+igMiiCEFo+9ymjINVEDavLzcoocD9hxXYltGwGouSVB mXnaIWy6LRQMMXNh13PIyUweVYQjcoC+LG8XQovMZQb9K6ti4fFn+bX46q+y8JUJ VLezvZvR4g/CgZeTSpUSDbJWfwl3QkyqrW9YtTNGJ3wK+Xl/qqsq16Zvb8rtmAPw PquCOBnXhUFh+qHspdS0jpVlYMWHcF/n8QJdaGv9sEQ/4dQ0OysHuPkGa6woNWqe uVaox0T1Rz3vbLcTmeddh/jmk2NyLi4hoZ0b1u4u/psNpmFH8aI1N7fz8d/Y7a+O OtK7w== Received: from dc6wp-exch02.marvell.com ([4.21.29.225]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 45n1yv8gds-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 27 Mar 2025 04:12:43 -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, 27 Mar 2025 04:12:42 -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, 27 Mar 2025 04:12:41 -0700 Received: from optiplex.marvell.com (unknown [10.28.34.253]) by maili.marvell.com (Postfix) with ESMTP id 36F623F7060; Thu, 27 Mar 2025 04:12:36 -0700 (PDT) From: Tanmay Jagdale To: , , , , , , , CC: , , , , , , , Tanmay Jagdale , James Clark Subject: [PATCH V3 2/2] perf: cs-etm: Store previous timestamp in packet queue Date: Thu, 27 Mar 2025 16:41:49 +0530 Message-ID: <20250327111149.461012-3-tanmay@marvell.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250327111149.461012-1-tanmay@marvell.com> References: <20250327111149.461012-1-tanmay@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: 8izJ4PYGXSk9WIQHdeAh21W6kF_57qXd X-Authority-Analysis: v=2.4 cv=dKCmmPZb c=1 sm=1 tr=0 ts=67e532ab cx=c_pps a=gIfcoYsirJbf48DBMSPrZA==:117 a=gIfcoYsirJbf48DBMSPrZA==:17 a=Vs1iUdzkB0EA:10 a=M5GUcnROAAAA:8 a=7CQSdrXTAAAA:8 a=7j_uQjMFRymVN5L776oA:9 a=OBjm3rFKGHvpk9ecZwUJ:22 a=a-qgeE7W1pNrGK8U0ZQC:22 X-Proofpoint-GUID: 8izJ4PYGXSk9WIQHdeAh21W6kF_57qXd X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1095,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-03-27_01,2025-03-26_02,2024-11-22_01 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250327_041252_240922_F3115E79 X-CRM114-Status: GOOD ( 18.67 ) 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 Reviewed-by: James Clark --- 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 ebed5b98860e..b52b58328ca0 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1542,13 +1542,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; } @@ -1567,7 +1569,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); @@ -1643,7 +1645,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); @@ -1903,6 +1905,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 a8caeea720aa..359d6233dd9b 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]; };