From nobody Sun Feb 8 05:27:05 2026 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37BED1849EB for ; Wed, 3 Jul 2024 20:03:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720037007; cv=none; b=fTg9w3bHRhDSdJBs+qZHC5qgYZK4Jj0LVMgYj0WS53Gn8kkgIHop431HqK8YpRPQQ4eBYXFy4T9d9UDbfEWZoGt9AGzQEAMZDuAwzysukOchpuHZe+/iLTwvJQpP5m7aLlFrCU8nakcslETBRloPKx2rI9//f2NvQTs709oOL3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720037007; c=relaxed/simple; bh=PnDdlfcERxVSvWUX3ql7Jr5lCD7PahiVaogQ8rp5bfU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HgUOIUtJVFxAtnJJ5ahOB2ppexzFM2Rha5hV8QVFYoWrY9W7QDpyGAiMPtbIZognkqj5gxTdjuEkP/+Tp0B9TQXGipN7zGiVN5xiozKHwYcjz+rScRnYtVzKx44CQPVDZUyE2t6CcXr8dCCxI7TI/4xoed4SW0a1gpZo1gTMXgE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=nUynTbGr; arc=none smtp.client-ip=198.175.65.9 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nUynTbGr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1720037006; x=1751573006; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PnDdlfcERxVSvWUX3ql7Jr5lCD7PahiVaogQ8rp5bfU=; b=nUynTbGrmRF4sX/K10d1cySejLVB3CFi9uk/WZbc046/ws+XCljPlt+C bbHLDneOyw/+mPbwTTomRXym5rxaXsaaGAgB5T+g/i/Yq0RhXR2bLwdsQ 7mcaz+YmzarFiGJgzUqQBfeJO2CWS0QtiZo1X+1GIEI5b7NeFQUS7mhWP Ge5XjCW/WDhrsp0agfGu3lDF1p2aPe89OARUJPJUaF/D1EGcQLdgkdEIo HBqDhOfRXsH2C86u8SkDRQZP6pWnTrh/io9QGSB8XLXjn9wyT2IaMuR7J TiYrewsdRwbl4uGXU3vfJqTH9zzT/wgHiqCMCNq07Kc7wkLAaM/oUfd/G A==; X-CSE-ConnectionGUID: zmqscmi2To2kdXCutvmZsw== X-CSE-MsgGUID: 7RWldaUWTQedE9puPRHF7A== X-IronPort-AV: E=McAfee;i="6700,10204,11122"; a="39807576" X-IronPort-AV: E=Sophos;i="6.09,182,1716274800"; d="scan'208";a="39807576" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Jul 2024 13:03:25 -0700 X-CSE-ConnectionGUID: Iq3UV6fWQQmLF1DfDZ1oIw== X-CSE-MsgGUID: 1scwYKs/S0+K7HkEZGYl+g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,182,1716274800"; d="scan'208";a="46442561" Received: from kanliang-dev.jf.intel.com ([10.165.154.102]) by fmviesa010.fm.intel.com with ESMTP; 03 Jul 2024 13:03:24 -0700 From: kan.liang@linux.intel.com To: acme@kernel.org, namhyung@kernel.org, irogers@google.com, peterz@infradead.org, mingo@kernel.org, linux-kernel@vger.kernel.org Cc: adrian.hunter@intel.com, ak@linux.intel.com, eranian@google.com, Kan Liang Subject: [PATCH 3/9] perf evlist: Save branch counters information Date: Wed, 3 Jul 2024 13:03:50 -0700 Message-Id: <20240703200356.852727-4-kan.liang@linux.intel.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20240703200356.852727-1-kan.liang@linux.intel.com> References: <20240703200356.852727-1-kan.liang@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Kan Liang The branch counters logging (A.K.A LBR event logging) introduces a per-counter indication of precise event occurrences in LBRs. The kernel only dumps the number of occurrences into a record. The perf tool has to map the number to the corresponding event. Add evlist__update_br_cntr() to go through the evlist to pick the events that are configured to be logged. Assign a logical idx to track them, and add the total number of the events in the leader event. The total number will be used to allocate the space to save the branch counters for a block. The logical idx will be used to locate the corresponding event quickly in the following patches. It only needs to iterate the evlist once. The evsel__has_branch_counters() is also optimized. Signed-off-by: Kan Liang --- tools/perf/util/evlist.c | 15 +++++++++++++++ tools/perf/util/evlist.h | 2 ++ tools/perf/util/evsel.c | 13 +++++++------ tools/perf/util/evsel.h | 8 ++++++++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c index 3a719edafc7a..6f5311d01a14 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -78,6 +78,7 @@ void evlist__init(struct evlist *evlist, struct perf_cpu_= map *cpus, evlist->ctl_fd.fd =3D -1; evlist->ctl_fd.ack =3D -1; evlist->ctl_fd.pos =3D -1; + evlist->nr_br_cntr =3D -1; } =20 struct evlist *evlist__new(void) @@ -1261,6 +1262,20 @@ u64 evlist__combined_branch_type(struct evlist *evli= st) return branch_type; } =20 +void evlist__update_br_cntr(struct evlist *evlist) +{ + struct evsel *evsel; + int i =3D 0; + + evlist__for_each_entry(evlist, evsel) { + if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) { + evsel->br_cntr_idx =3D i++; + evsel__leader(evsel)->br_cntr_nr++; + } + } + evlist->nr_br_cntr =3D i; +} + bool evlist__valid_read_format(struct evlist *evlist) { struct evsel *first =3D evlist__first(evlist), *pos =3D first; diff --git a/tools/perf/util/evlist.h b/tools/perf/util/evlist.h index cb91dc9117a2..88206dd554c7 100644 --- a/tools/perf/util/evlist.h +++ b/tools/perf/util/evlist.h @@ -56,6 +56,7 @@ struct evlist { bool enabled; int id_pos; int is_pos; + int nr_br_cntr; u64 combined_sample_type; enum bkw_mmap_state bkw_mmap_state; struct { @@ -217,6 +218,7 @@ int evlist__apply_filters(struct evlist *evlist, struct= evsel **err_evsel); u64 __evlist__combined_sample_type(struct evlist *evlist); u64 evlist__combined_sample_type(struct evlist *evlist); u64 evlist__combined_branch_type(struct evlist *evlist); +void evlist__update_br_cntr(struct evlist *evlist); bool evlist__sample_id_all(struct evlist *evlist); u16 evlist__id_hdr_size(struct evlist *evlist); =20 diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c index a5dd031c9080..89c3baae926e 100644 --- a/tools/perf/util/evsel.c +++ b/tools/perf/util/evsel.c @@ -2562,17 +2562,18 @@ u64 evsel__bitfield_swap_branch_flags(u64 value) =20 static inline bool evsel__has_branch_counters(const struct evsel *evsel) { - struct evsel *cur, *leader =3D evsel__leader(evsel); + struct evsel *leader =3D evsel__leader(evsel); =20 /* The branch counters feature only supports group */ if (!leader || !evsel->evlist) return false; =20 - evlist__for_each_entry(evsel->evlist, cur) { - if ((leader =3D=3D evsel__leader(cur)) && - (cur->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS)) - return true; - } + if (evsel->evlist->nr_br_cntr < 0) + evlist__update_br_cntr(evsel->evlist); + + if (leader->br_cntr_nr > 0) + return true; + return false; } =20 diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h index 80b5f6dd868e..a733d3407b35 100644 --- a/tools/perf/util/evsel.h +++ b/tools/perf/util/evsel.h @@ -147,6 +147,14 @@ struct evsel { */ __u64 synth_sample_type; =20 + /* + * Store the branch counter related information. + * br_cntr_idx: The idx of the branch counter event in the evlist + * br_cntr_nr: The number of the branch counter event in the group + * (Only available for the leader event) + */ + int br_cntr_idx; + int br_cntr_nr; /* * bpf_counter_ops serves two use cases: * 1. perf-stat -b counting events used byBPF programs --=20 2.38.1