Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2527227imm; Mon, 28 May 2018 09:46:52 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo8iZ82JJixJu0XuOuwWfw4OyYa8ALqKXijjfG/d9fgOMIwl2z/Xwg46AfTc/wEzR9ERDCR X-Received: by 2002:a65:654a:: with SMTP id a10-v6mr11062013pgw.107.1527526012171; Mon, 28 May 2018 09:46:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527526012; cv=none; d=google.com; s=arc-20160816; b=WHRp4ap+Bwqf+hIeA8oB2xHaXHZXeMttUrlt965S4tDfhQIU0QNxYQHterBB+JU7Fy OGnPVOxKkj9hkSZNhSOi9IU2/nZSkvSVCMtADmypLyuGY2ScX/SaJeRmCRJm5OSKSbsK Zi03XdmMqhpRCEZAFWQFZzKdqKBedO8AiOAwdoHSb1SaOgpzhNnyAuC6q2ksCy3KS6dq MNUqkag7eSZKm+r0Vlwc0pSHamFdjJ96HU5CjAZkpZ178fZxn/okDCMcBLX3AYctCT2b 3M1tOtO6ixm+tPAtGVRwufKOHNik9RHTz2udCHSCyJJpkREUoRB5ll963utvZKOnB+nD 0vtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=17wKwDLSePQFJwfe8HfTvpHuYB1Eat/+jbcvNh5MJK0=; b=w2n/ydrKgkSNSpOwh0umonvhEr2K9t8f9pqDNsiJCQI24bNlE8eV3Kx4sjAtmM8YSQ +tc7K2VlLeC9lMKGIypAVll/gOSz5xbJkMEckBIVKXhmqxB75vjEmFLUViv3YsyINbOI v9OsnpIA4JKKtuPlOwKN9HlYcgdzRc29VBJkoRP4lpjhltsygGoj2LU0LZoHu13zEN1U rfZy7k1ZUy0OZrE4D8APNQ47IROFLg30d8d43kf2k9VEymVOR3bzSPAlaWLT+XgkrmZM 0Y8Knpe7giWblIoBirXKnuEsjnS9QH4AyZceeJZv8eZzvXiedfL6w1jPpLZUngbS7Arw JhWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RLj7srP2; 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 e16-v6si30439560pli.476.2018.05.28.09.46.37; Mon, 28 May 2018 09:46:52 -0700 (PDT) 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=RLj7srP2; 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 S940352AbeE1QpU (ORCPT + 99 others); Mon, 28 May 2018 12:45:20 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:50623 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932195AbeE1QpL (ORCPT ); Mon, 28 May 2018 12:45:11 -0400 Received: by mail-wm0-f68.google.com with SMTP id t11-v6so33632328wmt.0 for ; Mon, 28 May 2018 09:45:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=17wKwDLSePQFJwfe8HfTvpHuYB1Eat/+jbcvNh5MJK0=; b=RLj7srP2N96R7KA//q/K8/5CyODdYCuNhgNeN4Di2tlEeup9yI7ffAEqw2fF0p9tbI OCQz+y3rGBL4dJi+4gZC1SO0VnpmKZgBxx/oOep6MKK9FftKlszaN8V2+XdEdd0I8Co5 Bct7qaynfGkIPun2hWnZ2LunCHx5Ez/YA3HOA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=17wKwDLSePQFJwfe8HfTvpHuYB1Eat/+jbcvNh5MJK0=; b=qWo1FV8HDc0hB2DW+14ZfYu20kcDixerrlyZ/vO07M7d5PKwk4xw16AGDI2waC6c1Y jvzhzwxI1Vv/vvqJuxx1U0VX7Tj4dwRPD2UcR4nm7zV3Dw5vmDja2W0j8Uq2Ptn3FKsA m4ImUTrCMuT9iqpZ9cSPLjcufNQISmwWNdO1tJ61W1W8LuSd7QGz61ZmqM6P0PQXtnea wS7Jb2lmDDeeujvDfYrAIGbOQHt3l4QpwZUrhmOHXYsVI9Hsaeuo6rLNBOOm2yx4seI+ K93ntJOXLLWnDKPKHeNn/gJbhrjIkbwzDNMNOZH5C7fE1lA5JzUGGvnYWsL/Hm9M1uVN Bu5g== X-Gm-Message-State: ALKqPwdkDeiv3UR6xE+HjmNfwtwQmRDcfTtJa58+QxJ7AqT8yN9A8UQn eA68SZ4565B5htvsSVuqQKuU202eheVl0mM8gvB8CQ== X-Received: by 2002:a50:b003:: with SMTP id i3-v6mr15787537edd.293.1527525909373; Mon, 28 May 2018 09:45:09 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a50:a4a1:0:0:0:0:0 with HTTP; Mon, 28 May 2018 09:45:08 -0700 (PDT) In-Reply-To: References: <1527289854-10755-1-git-send-email-mathieu.poirier@linaro.org> From: Mathieu Poirier Date: Mon, 28 May 2018 10:45:08 -0600 Message-ID: Subject: Re: [PATCH] perf tools: Fix indexing for decoder packet queue To: Leo Yan Cc: Arnaldo Carvalho de Melo , Robert Walker , Peter Zijlstra , Ingo Molnar , Alexander Shishkin , Jiri Olsa , Namhyung Kim , LAK , linux-kernel Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27 May 2018 at 21:13, Leo Yan wrote: > On Fri, May 25, 2018 at 05:10:54PM -0600, Mathieu Poirier wrote: >> The tail of a queue is supposed to be pointing to the next available slot >> in a queue. In this implementation the tail is incremented before it is >> used and as such points to the last used element, something that has the >> immense advantage of centralizing tail management at a single location >> and eliminating a lot of redundant code. >> >> But this needs to be taken into consideration on the dequeueing side where >> the head also needs to be incremented before it is used, or the first >> available element of the queue will be skipped. >> >> Signed-off-by: Mathieu Poirier >> --- >> tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 12 ++++++++++-- >> 1 file changed, 10 insertions(+), 2 deletions(-) >> >> diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c >> index c8b98fa22997..4d5fc374e730 100644 >> --- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c >> +++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c >> @@ -96,11 +96,19 @@ int cs_etm_decoder__get_packet(struct cs_etm_decoder *decoder, >> /* Nothing to do, might as well just return */ >> if (decoder->packet_count == 0) >> return 0; >> + /* >> + * The queueing process in function cs_etm_decoder__buffer_packet() >> + * increments the tail *before* using it. This is somewhat counter >> + * intuitive but it has the advantage of centralizing tail management >> + * at a single location. Because of that we need to follow the same >> + * heuristic with the head, i.e we increment it before using its >> + * value. Otherwise the first element of the packet queue is not >> + * used. >> + */ >> + decoder->head = (decoder->head + 1) & (MAX_BUFFER - 1); >> >> *packet = decoder->packet_buffer[decoder->head]; >> >> - decoder->head = (decoder->head + 1) & (MAX_BUFFER - 1); >> - > > I tested this patch and confirmed it can work well with python > decoding script: > > Tested-by: Leo Yan > > Actually, I have another idea for this fixing, seems to me > the unchanged code has right logic for decoder->head, and I think this > issue is more related with incorrect initialization index. So we can > change the initialization index for decoder->head as below. How about > you think for this? > > diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c > b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c > index c8b98fa..b133260 100644 > --- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c > +++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c > @@ -249,7 +249,7 @@ static void cs_etm_decoder__clear_buffer(struct > cs_etm_decoder *decoder) > { > int i; > > - decoder->head = 0; > + decoder->head = 1; I flirted with that idea but thought the problem is really with the tail and as such would have done: decoder->tail = -1; But since both head and tail are declared as u32 it would have required changing the types to int, necessitating modifications everywhere other parts of the code deals with them. As such I just decided to swap the order of events in cs_etm_decoder__get_packet(). I'm not strongly opinionated on this and can send another patch if you're keen. Thanks for the feedback, Mathieu > decoder->tail = 0; > decoder->packet_count = 0; > for (i = 0; i < MAX_BUFFER; i++) { > > Thanks, > Leo Yan > >> decoder->packet_count--; >> >> return 1; >> -- >> 2.7.4 >>