Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1004225pxb; Wed, 27 Oct 2021 17:11:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxIm69fll6liTzmm4+EvnWwcN8BSiksnDwFsMT+39d7OnMxOKdiqGVV1YJZcDLmAL3mFYr7 X-Received: by 2002:a05:6402:5188:: with SMTP id q8mr1452177edd.332.1635379915783; Wed, 27 Oct 2021 17:11:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635379915; cv=none; d=google.com; s=arc-20160816; b=KI6uTynYjwEBuo7t5Ko3l0Gxbj/FgOVy3vll+5igL2WjYggJi//v7tvIxpjy0py6gt 4cSojoXrsOrJqtdqhLXoTM5HhHT0tb0ld1nknR+4Csnudl3tFTTNxj+jnZBdGVU72KFI L1kFlIr/BQLuA+poF2SBjJGoJ7L9h0Eq2OsAW7qRSbEum8z8XGF1V00mOvjOtwvrJo8Z wha0SiZVTyFrccf3GyfXt/jhXGl+EXRuahpVHP3xIFco+GL6LT1IdH+rGCzBdWQlzlid mEgRjRjzTtyJlaq6VJGADP4gim4M1EypJQNnw1/GacenWF5s6hivE0/Jq55z+/gLFFA7 UOVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=2JEuHyES0dDCaHwNdIYoFQpNJVugUZjipTKWIFNqiec=; b=MDNjR5Ni8WokVCu2K8NHjux7K+3TCcwFIWolptckPR6i5KOluOfdUV3EU+z+uA7wyj X2kzkmL5tSUItEU4VXjdfn/7Z12ocGd/c1pTiK9TnMrGso+Mxbhjhnicx0XKH3uvQkJ3 X48FCLwR6zTxKhSCU5Z0N7wn2m3c82jOd+rGkap5khg3RUmcAr6REBZThrc7zes52WXR r/DnkZrUu+0MiUPjEkul7yl4dt92gHHfAhKdRIyTQfK0aFk2rCmMzlFTKDCyCe9EZ1pL k7pOx7yV7zLA/WoXeJM9aNFbw7xXxepRbf4r/CuUBP0zNAqh5OK96E2NZxm/R49bl8n4 kkhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="RJvCsgn/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id j23si2064233eje.21.2021.10.27.17.11.31; Wed, 27 Oct 2021 17:11:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="RJvCsgn/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229508AbhJ1AMO (ORCPT + 99 others); Wed, 27 Oct 2021 20:12:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:51816 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbhJ1AMM (ORCPT ); Wed, 27 Oct 2021 20:12:12 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7D36560EC0; Thu, 28 Oct 2021 00:09:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635379785; bh=osHe5qQZL4+EsqRTmg0smsm97CQxSIioocJlwg5ueBk=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=RJvCsgn/fH5TMlG+wpAzKS7+9l7kL2689hFO2Z4HlgiW8S3ULVSpzJH3j2TGhU3qk C/G/sMA0YqcxVhYCpDnHyUdzc+Zuyq8BBrjyUmqHI5ig5YhWd6QhjxO+tdV+Mz2AFS UjfbGD+H2rJe412cZ+sBG+JUZUhGNw26UwcmGXz8YtJ7M7uRMGlHs5+7jLxH0C2lD6 H6HEcJRFHm0CKCRDEXCWYSBn6KKfAHoINkom4guaBwkJz1l2FJKKOcZPXzJvDaUk2X MEHJCSLv/3hCEpD82BwRXGwIb5wikqZ+6GIh8SHw5SNiFeb3DjKEDzctimVb8TXXxu vnowSwHW3e9TQ== Received: by quaco.ghostprotocols.net (Postfix, from userid 1000) id 8F373410A1; Wed, 27 Oct 2021 21:09:42 -0300 (-03) Date: Wed, 27 Oct 2021 21:09:42 -0300 From: Arnaldo Carvalho de Melo To: Jiri Olsa Cc: Madhavan Srinivasan , michael@ellerman.id.au, eranian@google.com, mark.rutland@arm.com, namhyung@kernel.org, kjain@linux.ibm.com, atrajeev@linux.vnet.ibm.com, linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 1/2] tools/perf: Add bitfield_swap to handle branch_stack endian issue Message-ID: References: <20211016125059.691856-1-maddy@linux.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Url: http://acmel.wordpress.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Mon, Oct 25, 2021 at 02:07:23PM +0200, Jiri Olsa escreveu: > On Sat, Oct 16, 2021 at 06:20:58PM +0530, Madhavan Srinivasan wrote: > > branch_stack struct has bit field definition which > > produces different bit ordering for big/little endian. > > Because of this, when branch_stack sample is collected > > in a BE system and viewed/reported in a LE system, bit > > fields of the branch stack are not presented properly. > > To address this issue, a evsel__bitfield_swap_branch_stack() > > is defined and introduced in evsel__parse_sample. > > > > Signed-off-by: Madhavan Srinivasan > > for both patches > > Acked-by: Jiri Olsa Thanks, applied. - Arnaldo > thanks, > jirka > > > --- > > Changelog v1: > > - Renamed function and macro > > - Added comments in code > > > > tools/perf/util/evsel.c | 74 +++++++++++++++++++++++++++++++++++++++-- > > tools/perf/util/evsel.h | 13 ++++++++ > > 2 files changed, 85 insertions(+), 2 deletions(-) > > > > diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c > > index dbfeceb2546c..746e642d4d32 100644 > > --- a/tools/perf/util/evsel.c > > +++ b/tools/perf/util/evsel.c > > @@ -2221,6 +2221,51 @@ void __weak arch_perf_parse_sample_weight(struct perf_sample *data, > > data->weight = *array; > > } > > > > +u64 evsel__bitfield_swap_branch_flags(u64 value) > > +{ > > + u64 new_val = 0; > > + > > + /* > > + * branch_flags > > + * union { > > + * u64 values; > > + * struct { > > + * mispred:1 //target mispredicted > > + * predicted:1 //target predicted > > + * in_tx:1 //in transaction > > + * abort:1 //transaction abort > > + * cycles:16 //cycle count to last branch > > + * type:4 //branch type > > + * reserved:40 > > + * } > > + * } > > + * > > + * Avoid bswap64() the entire branch_flag.value, > > + * as it has variable bit-field sizes. Instead the > > + * macro takes the bit-field position/size, > > + * swaps it based on the host endianness. > > + */ > > + if (bigendian()) { > > + new_val = bitfield_swap(value, 0, 1); > > + new_val |= bitfield_swap(value, 1, 1); > > + new_val |= bitfield_swap(value, 2, 1); > > + new_val |= bitfield_swap(value, 3, 1); > > + new_val |= bitfield_swap(value, 4, 16); > > + new_val |= bitfield_swap(value, 20, 4); > > + new_val |= bitfield_swap(value, 24, 40); > > + } else { > > + new_val = bitfield_swap(value, 63, 1); > > + new_val |= bitfield_swap(value, 62, 1); > > + new_val |= bitfield_swap(value, 61, 1); > > + new_val |= bitfield_swap(value, 60, 1); > > + new_val |= bitfield_swap(value, 44, 16); > > + new_val |= bitfield_swap(value, 40, 4); > > + new_val |= bitfield_swap(value, 0, 40); > > + } > > + > > + return new_val; > > +} > > + > > int evsel__parse_sample(struct evsel *evsel, union perf_event *event, > > struct perf_sample *data) > > { > > @@ -2408,6 +2453,8 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event, > > if (type & PERF_SAMPLE_BRANCH_STACK) { > > const u64 max_branch_nr = UINT64_MAX / > > sizeof(struct branch_entry); > > + struct branch_entry *e; > > + unsigned int i; > > > > OVERFLOW_CHECK_u64(array); > > data->branch_stack = (struct branch_stack *)array++; > > @@ -2416,10 +2463,33 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event, > > return -EFAULT; > > > > sz = data->branch_stack->nr * sizeof(struct branch_entry); > > - if (evsel__has_branch_hw_idx(evsel)) > > + if (evsel__has_branch_hw_idx(evsel)) { > > sz += sizeof(u64); > > - else > > + e = &data->branch_stack->entries[0]; > > + } else { > > data->no_hw_idx = true; > > + /* > > + * if the PERF_SAMPLE_BRANCH_HW_INDEX is not applied, > > + * only nr and entries[] will be output by kernel. > > + */ > > + e = (struct branch_entry *)&data->branch_stack->hw_idx; > > + } > > + > > + if (swapped) { > > + /* > > + * struct branch_flag does not have endian > > + * specific bit field definition. And bswap > > + * will not resolve the issue, since these > > + * are bit fields. > > + * > > + * evsel__bitfield_swap_branch_flags() uses a > > + * bitfield_swap macro to swap the bit position > > + * based on the host endians. > > + */ > > + for (i = 0; i < data->branch_stack->nr; i++, e++) > > + e->flags.value = evsel__bitfield_swap_branch_flags(e->flags.value); > > + } > > + > > OVERFLOW_CHECK(array, sz, max_size); > > array = (void *)array + sz; > > } > > diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h > > index 1f7edfa8568a..2e82cdbe2c08 100644 > > --- a/tools/perf/util/evsel.h > > +++ b/tools/perf/util/evsel.h > > @@ -482,4 +482,17 @@ struct evsel *evsel__leader(struct evsel *evsel); > > bool evsel__has_leader(struct evsel *evsel, struct evsel *leader); > > bool evsel__is_leader(struct evsel *evsel); > > void evsel__set_leader(struct evsel *evsel, struct evsel *leader); > > + > > +/* > > + * Macro to swap the bit-field postition and size. > > + * Used when, > > + * - dont need to swap the entire u64 && > > + * - when u64 has variable bit-field sizes && > > + * - when presented in a host endian which is different > > + * than the source endian of the perf.data file > > + */ > > +#define bitfield_swap(src, pos, size) \ > > + ((((src) >> (pos)) & ((1ull << (size)) - 1)) << (63 - ((pos) + (size) - 1))) > > + > > +u64 evsel__bitfield_swap_branch_flags(u64 value); > > #endif /* __PERF_EVSEL_H */ > > -- > > 2.31.1 > > -- - Arnaldo