Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3227909ybl; Sun, 2 Feb 2020 18:15:13 -0800 (PST) X-Google-Smtp-Source: APXvYqye6cBN1UDFQOv6Lb6+3YX7bgIpgIV0GobVF4hH4XmpgO0fhKJl86yCB9CI0DPXvo40tQ1Y X-Received: by 2002:a05:6830:2110:: with SMTP id i16mr15843193otc.337.1580696113610; Sun, 02 Feb 2020 18:15:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580696113; cv=none; d=google.com; s=arc-20160816; b=mRz68TW6g2Tce4XY65hroKYxzuJxR4ayOr+OBTZ3Z/WEXuqvRATP/5uJliFK6a5So0 Hw269kY9pNB7iOoPHaxdg5KQ++h+jd8Vqe8lDdUFROJC0+Ds+Br+abU5bHWAtaX6kqo8 wjn9Y6ORXRHcwCsiEAlKJR5yQBU4IROc9kpaIcycFHqVSDpoGjFIjoX7Ey6WilfXSxW1 FpjUK2nEa8YOa047Wn6Hy6J4RNanEiVaXMlkyHorMtnd8BHloq7jO4HuH96KLqECuuK8 SRjQIeJxUiZT8HDKdkkbcji5Lb3te4GAIvKytAnaRYeLwYtKex6U1oksw1pnoYZB4wpL oxWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=mqHe4kpzcWWGiBvCMvqthV/qb6mJrwUYZdST62vWU3I=; b=yz2dJHKYEtuIgnSEQMD9NKNrCLwJwdUnUj/9SfxeEgYTWuOPk1uGEcmILDqoN2EioK GA3BunztXZuFSKvQroihXElsYmeBRPyjtsLrwo5T2icrYY+yrViHIDukN9BUov9BQqT+ 42T15cIluBOuFjDqEgm/46M2o/lh0X+TNSecNB8p3jUkaIPsecAZcY8kjGF9NprnPGqW eMh6mnKHl9vrgworXnvnKKtCfhEdRLb4a3n3mcJWY7RceQtjBAaiYU6f7ROLEJWq/UpX wEPdiOaLIQUBuhN+VlUZJ3aE0INcYc7FTzMitWsVcXtZfugmFxreKiU3bWULWV6DW9GU ymQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iNaUZPNF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p13si8643314otq.210.2020.02.02.18.15.00; Sun, 02 Feb 2020 18:15:13 -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; dkim=pass header.i=@linaro.org header.s=google header.b=iNaUZPNF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727256AbgBCBw6 (ORCPT + 99 others); Sun, 2 Feb 2020 20:52:58 -0500 Received: from mail-pf1-f182.google.com ([209.85.210.182]:46778 "EHLO mail-pf1-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727180AbgBCBw6 (ORCPT ); Sun, 2 Feb 2020 20:52:58 -0500 Received: by mail-pf1-f182.google.com with SMTP id k29so6682826pfp.13 for ; Sun, 02 Feb 2020 17:52:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=mqHe4kpzcWWGiBvCMvqthV/qb6mJrwUYZdST62vWU3I=; b=iNaUZPNFFVRxhU2mL7ouJ8gYcRrT170JXFwHyqq/VgpyFf1Y8ku3/HHef8ZJQ1O52a etBl8WUro89ePTq0dJs8pcE2tFR/aWxj+AiM7CZj3Asje+2qeZQK+uKDa+3VCqbNatgA LgkkPmT3xOf+eZ+NteTL0uAEUVP8g0GFzDe7Ybz3jpaPmKb3KDsd40BXRK+UIBh1NQDE T5YMMBg395kFIEJ0S0K97K0OTflND0ZKIn3gYCQkJuESqqT/YSzfQPuyzriQJxBLo18/ Pa8v6IrN/T5VCovb8BjRo83GxZSMtszPWwlKMYsjm3CtBcY/pz6hWu9ThuoS05X0nyKL W1Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=mqHe4kpzcWWGiBvCMvqthV/qb6mJrwUYZdST62vWU3I=; b=GdHYgqhprKYtlQvefcEeeplauuNxvX6xWbaX1Th54t0eBVpJQWAfhsMy4KBuzfxJro oOGTAgZbOgsGDUXtW1T1i06BF77UQMwvD0QFm9kiJVpOL0T4epL+TZR2FYz8+HdfZiCc 75ZWom/WnZh8spd6n6CbJlONqjj8AoZ9xCiXNl0YcCNh8haGrMFvEw4ky9f8qP0NUxvZ gnVk8Dwdt0OZiZRuhi9Y4QJck7NVhqhaxguTakapTd1SQ48Xl6NkUoRYNRG+bOOA333e Z0M7QjhSzPGNVwSiSGdI6+FXr+Kg0tdLo5b19sUtdvwNL2PULLx4OGTikt47DBKzE2NP eyGA== X-Gm-Message-State: APjAAAUXtv+k91TSLwWYW6NEZgN8+SfLI2eVb1oyXKmd/qiOK9Qp4f9m amkO9PfpTqmUF82W5DdqpbZDag== X-Received: by 2002:a63:2309:: with SMTP id j9mr12530563pgj.54.1580694776169; Sun, 02 Feb 2020 17:52:56 -0800 (PST) Received: from localhost.localdomain (li1441-214.members.linode.com. [45.118.134.214]) by smtp.gmail.com with ESMTPSA id y38sm17348308pgk.33.2020.02.02.17.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 Feb 2020 17:52:55 -0800 (PST) From: Leo Yan To: Arnaldo Carvalho de Melo , Mathieu Poirier , Suzuki K Poulose , Peter Zijlstra , Ingo Molnar , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Mike Leach , Robert Walker , Coresight ML Cc: Leo Yan Subject: [PATCH v3 3/5] perf cs-etm: Correct synthesizing instruction samples Date: Mon, 3 Feb 2020 09:52:01 +0800 Message-Id: <20200203015203.27882-4-leo.yan@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200203015203.27882-1-leo.yan@linaro.org> References: <20200203015203.27882-1-leo.yan@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When 'etm->instructions_sample_period' is less than 'tidq->period_instructions', the function cs_etm__sample() cannot handle this case properly with its logic. Let's see below flow as an example: - If we set itrace option '--itrace=i4', then function cs_etm__sample() has variables with initialized values: tidq->period_instructions = 0 etm->instructions_sample_period = 4 - When the first packet is coming: packet->instr_count = 10; the number of instructions executed in this packet is 10, thus update period_instructions as below: tidq->period_instructions = 0 + 10 = 10 instrs_over = 10 - 4 = 6 offset = 10 - 6 - 1 = 3 tidq->period_instructions = instrs_over = 6 - When the second packet is coming: packet->instr_count = 10; in the second pass, assume 10 instructions in the trace sample again: tidq->period_instructions = 6 + 10 = 16 instrs_over = 16 - 4 = 12 offset = 10 - 12 - 1 = -3 -> the negative value tidq->period_instructions = instrs_over = 12 So after handle these two packets, there have below issues: The first issue is that cs_etm__instr_addr() returns the address within the current trace sample of the instruction related to offset, so the offset is supposed to be always unsigned value. But in fact, function cs_etm__sample() might calculate a negative offset value (in handling the second packet, the offset is -3) and pass to cs_etm__instr_addr() with u64 type with a big positive integer. The second issue is it only synthesizes 2 samples for sample period = 4. In theory, every packet has 10 instructions so the two packets have total 20 instructions, 20 instructions should generate 5 samples (4 x 5 = 20). This is because cs_etm__sample() only calls once cs_etm__synth_instruction_sample() to generate instruction sample per range packet. This patch fixes the logic in function cs_etm__sample(); the basic idea is to divide into three parts for handling coming packet: - The first part is for synthesizing the first instruction sample, it combines the instructions from the tail of previous packet and the instructions from the head of the new packet; - The second part is to simply generate samples with sample period aligned; - The third part is the tail of new packet, the rest instructions will be left for the sequential sample handling. Suggested-by: Mike Leach Signed-off-by: Leo Yan --- tools/perf/util/cs-etm.c | 105 ++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 13 deletions(-) diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c index 3e28462609e7..c5a05f728eac 100644 --- a/tools/perf/util/cs-etm.c +++ b/tools/perf/util/cs-etm.c @@ -1360,23 +1360,102 @@ static int cs_etm__sample(struct cs_etm_queue *etmq, * TODO: allow period to be defined in cycles and clock time */ - /* Get number of instructions executed after the sample point */ - u64 instrs_over = tidq->period_instructions - - etm->instructions_sample_period; + /* + * Below diagram demonstrates the instruction samples + * generation flows: + * + * Instrs Instrs Instrs Instrs + * Sample(n) Sample(n+1) Sample(n+2) Sample(n+3) + * | | | | + * V V V V + * -------------------------------------------------- + * ^ ^ + * | | + * Period Period + * instructions(Pi) instructions(Pi') + * + * | | + * \---------------- -----------------/ + * V + * instrs_executed + * + * Period instructions (Pi) contains the the number of + * instructions executed after the sample point(n). When a new + * instruction packet is coming and generate for the next sample + * (n+1), it combines with two parts instructions, one is the + * tail of the old packet and another is the head of the new + * coming packet. So 'head' variable is used to cauclate the + * instruction numbers in the new packet for sample(n+1). + * + * Sample(n+2) and sample(n+3) consume the instructions with + * sample period, so directly generate samples based on the + * sampe period. + * + * After sample(n+3), the rest instructions will be used by + * later packet; so use 'instrs_over' to track the rest + * instruction number and it is assigned to + * 'tidq->period_instructions' for next round calculation. + */ + u64 head, offset = 0; + u64 addr; /* - * Calculate the address of the sampled instruction (-1 as - * sample is reported as though instruction has just been - * executed, but PC has not advanced to next instruction) + * 'instrs_over' is the number of instructions executed after + * sample points, initialise it to 'instrs_executed' and will + * decrease it for consumed instructions in every synthesized + * instruction sample. */ - u64 offset = (instrs_executed - instrs_over - 1); - u64 addr = cs_etm__instr_addr(etmq, trace_chan_id, - tidq->packet, offset); + u64 instrs_over = instrs_executed; - ret = cs_etm__synth_instruction_sample( - etmq, tidq, addr, etm->instructions_sample_period); - if (ret) - return ret; + /* + * 'head' is the instructions number of the head in the new + * packet, it combines with the tail of previous packet to + * generate a sample. So 'head' uses the sample period to + * decrease the instruction number introduced by the previous + * packet. + */ + head = etm->instructions_sample_period - + (tidq->period_instructions - instrs_executed); + + if (head) { + offset = head; + + /* + * Calculate the address of the sampled instruction (-1 + * as sample is reported as though instruction has just + * been executed, but PC has not advanced to next + * instruction) + */ + addr = cs_etm__instr_addr(etmq, trace_chan_id, + tidq->packet, offset - 1); + ret = cs_etm__synth_instruction_sample( + etmq, tidq, addr, + etm->instructions_sample_period); + if (ret) + return ret; + + instrs_over -= head; + } + + while (instrs_over >= etm->instructions_sample_period) { + offset += etm->instructions_sample_period; + + /* + * Calculate the address of the sampled instruction (-1 + * as sample is reported as though instruction has just + * been executed, but PC has not advanced to next + * instruction) + */ + addr = cs_etm__instr_addr(etmq, trace_chan_id, + tidq->packet, offset - 1); + ret = cs_etm__synth_instruction_sample( + etmq, tidq, addr, + etm->instructions_sample_period); + if (ret) + return ret; + + instrs_over -= etm->instructions_sample_period; + } /* Carry remaining instructions into next sample period */ tidq->period_instructions = instrs_over; -- 2.17.1