Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp624573pxb; Thu, 21 Oct 2021 06:28:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaQXxnTm2QleQpdm92ppFqN8UmeuKrVtg6UOQICGa6Tzw/H41bgjAtqfNbyMx8Pa0ggsG6 X-Received: by 2002:aa7:811a:0:b0:44c:b9ef:f618 with SMTP id b26-20020aa7811a000000b0044cb9eff618mr5520070pfi.9.1634822921688; Thu, 21 Oct 2021 06:28:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634822921; cv=none; d=google.com; s=arc-20160816; b=FluomAhytlODlGN5nRW6kyOfLXgtD+IOpdPcNst06uNHtX9T6lmcE7OMLl/I9PDF6V fqoSpIzDbCG9fFTqSexkdC++mvDHJBJEDtrQoo8gBReqB1ZKDRJ2rHaeliP22n4+wHwY MVd4AyS/h7XcVUpQhx934Qetrskw1Z96L4z1mQkaW9yOx2hRPZ1w4GeNVQ9Z1Gzf+3k9 M5dKG4riJzH+4uX0+8ALvvBJjQPJXjQgxPowiICvnb3ECB5+s3dVitFIyW2ThSr170b9 HNJKdM0R27iRp7F0gnFjBn58rQ+L9hD8dRJ6MlTBF0CPsocQ+wHHBfCfmifqkqs0owH6 +ETA== 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=NkppuMobEIHKB0sez/ioBJ9Xfdh1AlpgLhgQ5nmiOuA=; b=XpepwMk9gdyZVuVVGDuclk08oh5KzjcFi5+UTqNLNNmWoyK/s8JXq7n1330GPAp2DE /DlrH2X+6Ir3GDObIV0qrl5vqkqQ2PAO74MeDAoAg04//K0DLW4x5jfxMR20pA3TS3H9 jfQIxvR9aiAVAq5sj4zoxxt8zlAgES4Db5s+0NR7RNnlHMqp39AMrWZU+yxtlVNuAcu0 zbn6rimNfYbFrDPVlHwOfeftbYZzOcs54bk+XveZH8j4nvAbtyZqeZZweTHOyZJ8aKlm cklrDztZOcAQgg87tzFyVdZPprO60LrJif7m372hMnuUWhTNUsJv8NnmJMmpaXH4igmf cUag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fjBWDuLW; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u14si8710005pfk.23.2021.10.21.06.28.28; Thu, 21 Oct 2021 06:28:41 -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=@redhat.com header.s=mimecast20190719 header.b=fjBWDuLW; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231431AbhJUN1o (ORCPT + 99 others); Thu, 21 Oct 2021 09:27:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:25820 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230483AbhJUN1g (ORCPT ); Thu, 21 Oct 2021 09:27:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634822720; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NkppuMobEIHKB0sez/ioBJ9Xfdh1AlpgLhgQ5nmiOuA=; b=fjBWDuLWC/5WL3b0u08x/x1mkBxouYFVk4QMMu+NZEd05KE2Vth04c2lwmyQaRmo6T8t4F NYfb7Z38NzY1UFptdEDvRAaFL5Qm0XXzsdGUW1amAJV3Za7IaUpJpR9TbtH7Yso4XCBbvi Qs/TTmlJMZmZWatOqI4MbMpAuWKEiOA= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-149-BxbsF3UDPEeMCM7Lmq1_mw-1; Thu, 21 Oct 2021 09:25:18 -0400 X-MC-Unique: BxbsF3UDPEeMCM7Lmq1_mw-1 Received: by mail-ed1-f70.google.com with SMTP id v2-20020a50f082000000b003db24e28d59so336472edl.5 for ; Thu, 21 Oct 2021 06:25:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=NkppuMobEIHKB0sez/ioBJ9Xfdh1AlpgLhgQ5nmiOuA=; b=RehC8cgY8aqC1AR5QjPdZsjQAlzQLikv8T2S/8t+JS7puDQxeGJ/Ha8JboeHnBZdho R5WnRgcHd4/+TRYojJqBg9rSCn7ePg7ewwkTE5J4UaAbRoiMRS5uVIExQE5XRkzFCsRX J2fOXVTwRyzPMg704zRdNrRebjh7ysN4ECgFtKef/FM9r5GMBI9vgX6zeBAO5VtIpZwB chippblRuig2Q+jpOekqFi/kJYw4c0qsdYvBUhK1dXpiuDcw0qlWeO95giGgJvGK1bpe FiBVv5ks/4clYdFn3pQ7n6KuEMEsuHZgrWOs6LTMf720bt0yJg3cY81fqS3BYWvLBBrT HAEQ== X-Gm-Message-State: AOAM530weweiGGWKwMjwiNRQ5G030XmuQV1v3MQbPWfXYCfDxI1XZeo6 52VH8stHkjauBT2iF+29MSnqjcU2nJt1OJBnxzvIh8C9Rz6Iler4CFrIbZ5M8E4G0gEJlnluZKL xFhvrccuriZPMMMeznNPtK7rs X-Received: by 2002:a50:c355:: with SMTP id q21mr2064273edb.161.1634822717619; Thu, 21 Oct 2021 06:25:17 -0700 (PDT) X-Received: by 2002:a50:c355:: with SMTP id q21mr2064244edb.161.1634822717435; Thu, 21 Oct 2021 06:25:17 -0700 (PDT) Received: from krava ([83.240.63.48]) by smtp.gmail.com with ESMTPSA id y6sm2588772ejf.58.2021.10.21.06.25.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 06:25:17 -0700 (PDT) Date: Thu, 21 Oct 2021 15:25:15 +0200 From: Jiri Olsa To: Madhavan Srinivasan Cc: acme@kernel.org, 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: <20211016125059.691856-1-maddy@linux.ibm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > --- > 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) { thanks for all the comments, make it easy to review > + /* > + * 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); one last thing that confuses me.. the sample is already swapped at this point, right? with perf_event__all64_swap function should you swap it back first and then do the proper bitfield swap? like we do in PERF_SAMPLE_RAW to get proper u32 values thanks, jirka > + } > + > 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 >