From nobody Mon Feb 9 05:22:38 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E320A1B3942 for ; Thu, 14 Nov 2024 23:07:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731625648; cv=none; b=W6nyOjskCGv3czLZ0kpsiFsBRraGUMZ6S/6M0H4KWD/JuPsGZsU/U+jmJbO1j19FPtDdkDC17FscLhSbpqKlVHVVTDy6ADmb1g9T/ky+ojlGHCrTKlKkW3pDTJyEjW1jyVx9RZCTXkT1HnVIqt+fXGrMaIhcfsRbrZ83rAnirLk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731625648; c=relaxed/simple; bh=LLEFK7NZuHwP503adj1d6CSpVzaLmth1dYdPOz7NtLs=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=HKpTUQKgC/I9GTAyrPnAZWvu4QzWsNtP8IjRFOSglwbKGRdf2x/0tTYFybmgi/AR6H+FnxhkgKoA1pk5gN8z5yiX9XlcoD7y1zMjGMVj8keAL/xhjNH+9lTsyNuEbmNR8PJMcguWNp1klC4XoQ9pwliGXPCO93Z4kCb8IlRwVBk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GhT/UAQp; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GhT/UAQp" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e330f65bcd9so1424840276.1 for ; Thu, 14 Nov 2024 15:07:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731625646; x=1732230446; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=XoeFMjnBDbHZAr6lnCHUx3YoNMU6DqE2Glkf9cd6Lno=; b=GhT/UAQpuua3wX602UPd51/gnbyxTIJSm9bY8GX7p5dN+M8mgnBYkIVwARj3JafFxU v3OG34N2M0xJZwBzMwd8hVOqZqL8gLoEr5eR0lWTgBb242eX2HULBvGa4GVz1IYu+dYb sbABwZswo37xBn9grjMJSaINcxeR0ixR1CmJ25HXC76GFMn5Zf+JoEmyuJulLchY8Q0v G5DxnxFc+QLZuK+85uV/pzPRINkjVdYHQrtQVHwNyn5ejsNrM+ab/ENYZMBcwNYLftlf hOeqq0xK41T7zPS//n8FeIp434FoUkSmrij/VlpiuP09M5bZb+FFL92dXUQrTOykUH2J jqng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731625646; x=1732230446; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XoeFMjnBDbHZAr6lnCHUx3YoNMU6DqE2Glkf9cd6Lno=; b=Vvmamx5AXUUSFrnzz/cZ/yXTuyXrV11PGFoZVvMk9orDZWMLN7Lh5/KMs7WXJRhPNa tQZJbGN+3Og2hpYyWRcQ/F712UmAdEeqYqMYWl+hWsuimzvj+ktIzhVGejofuHboVVpM RzCyQYkWsZTahFvkk/ASdtp4qQdX6ayAWckC47J+KUt9iyOp5XrgGPpxVfDgF3DI1PB8 MDKxVFFRnooCxOLrZqtSz58XYAdcA5RQTYt2uJzYKPN3ZYXv1ZpYUCVUZXuqSOzFMj3M 5a1v8DObp+l3kngwI3GAw67g4RIWQml9oyAMOCuzZy69sDMFmYi8IBHOV3H2hQ/rouHv tCEQ== X-Forwarded-Encrypted: i=1; AJvYcCVGEtSysprjVn6QxyxZ7rxX5A7tE96qYrjyZfpCUHHjYAjqo6PCSVCWOhLYhhQA0Ddut6ViLbof40NDae8=@vger.kernel.org X-Gm-Message-State: AOJu0YwVCle7e8hi7hQuFxUZmAaRdB1G15KUyQOH9ZxBNKThervxbFgA ckAmBXyz4lUa+KiKLMtArMnSoaHaokNaFA40jS76l0TBQRQLfVOYMbyMkTQgb/ADeyx0LGc1x9t nvbywTw== X-Google-Smtp-Source: AGHT+IH9D+lYyuoj3q2tuIRgrssticmiMq310mlYBNoxXkrhCnEnZmlHd1QE9PfvOXZH5YA0DLmsyCbtKHhF X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:7595:be86:b969:ac41]) (user=irogers job=sendgmr) by 2002:a05:6902:18c1:b0:e30:b89f:e3d with SMTP id 3f1490d57ef6-e380e030963mr158951276.1.1731625645926; Thu, 14 Nov 2024 15:07:25 -0800 (PST) Date: Thu, 14 Nov 2024 15:07:12 -0800 In-Reply-To: <20241114230713.330701-1-irogers@google.com> Message-Id: <20241114230713.330701-3-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241114230713.330701-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog Subject: [PATCH v1 2/3] perf values: Use evsel rather than evsel->idx From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Andi Kleen , "=?UTF-8?q?Ahelenia=20Ziemia=C5=84ska?=" , Chen Ni , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" An evsel idx may not be stable due to sorting, evlist removal, etc. Avoid use of the idx where the evsel itself can be used to avoid these problems. This removed 1 values array and duplicated evsel name strings. Signed-off-by: Ian Rogers --- tools/perf/builtin-report.c | 4 +- tools/perf/util/values.c | 106 +++++++++++++++--------------------- tools/perf/util/values.h | 9 +-- 3 files changed, 51 insertions(+), 68 deletions(-) diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c index 048c91960ba9..e5478082845c 100644 --- a/tools/perf/builtin-report.c +++ b/tools/perf/builtin-report.c @@ -348,11 +348,9 @@ static int process_read_event(const struct perf_tool *= tool, struct report *rep =3D container_of(tool, struct report, tool); =20 if (rep->show_threads) { - const char *name =3D evsel__name(evsel); int err =3D perf_read_values_add_value(&rep->show_threads_values, event->read.pid, event->read.tid, - evsel->core.idx, - name, + evsel, event->read.value); =20 if (err) diff --git a/tools/perf/util/values.c b/tools/perf/util/values.c index b9823f414f10..ec72d29f3d58 100644 --- a/tools/perf/util/values.c +++ b/tools/perf/util/values.c @@ -8,6 +8,7 @@ =20 #include "values.h" #include "debug.h" +#include "evsel.h" =20 int perf_read_values_init(struct perf_read_values *values) { @@ -22,21 +23,17 @@ int perf_read_values_init(struct perf_read_values *valu= es) values->threads =3D 0; =20 values->counters_max =3D 16; - values->counterrawid =3D malloc(values->counters_max - * sizeof(*values->counterrawid)); - values->countername =3D malloc(values->counters_max - * sizeof(*values->countername)); - if (!values->counterrawid || !values->countername) { - pr_debug("failed to allocate read_values counters arrays"); + values->counters =3D malloc(values->counters_max * sizeof(*values->counte= rs)); + if (!values->counters) { + pr_debug("failed to allocate read_values counters array"); goto out_free_counter; } - values->counters =3D 0; + values->num_counters =3D 0; =20 return 0; =20 out_free_counter: - zfree(&values->counterrawid); - zfree(&values->countername); + zfree(&values->counters); out_free_pid: zfree(&values->pid); zfree(&values->tid); @@ -56,10 +53,7 @@ void perf_read_values_destroy(struct perf_read_values *v= alues) zfree(&values->value); zfree(&values->pid); zfree(&values->tid); - zfree(&values->counterrawid); - for (i =3D 0; i < values->counters; i++) - zfree(&values->countername[i]); - zfree(&values->countername); + zfree(&values->counters); } =20 static int perf_read_values__enlarge_threads(struct perf_read_values *valu= es) @@ -116,81 +110,71 @@ static int perf_read_values__findnew_thread(struct pe= rf_read_values *values, =20 static int perf_read_values__enlarge_counters(struct perf_read_values *val= ues) { - char **countername; - int i, counters_max =3D values->counters_max * 2; - u64 *counterrawid =3D realloc(values->counterrawid, counters_max * sizeof= (*values->counterrawid)); + int counters_max =3D values->counters_max * 2; + struct evsel **new_counters =3D realloc(values->counters, + counters_max * sizeof(*values->counters)); =20 - if (!counterrawid) { - pr_debug("failed to enlarge read_values rawid array"); + if (!new_counters) { + pr_debug("failed to enlarge read_values counters array"); goto out_enomem; } =20 - countername =3D realloc(values->countername, counters_max * sizeof(*value= s->countername)); - if (!countername) { - pr_debug("failed to enlarge read_values rawid array"); - goto out_free_rawid; - } - - for (i =3D 0; i < values->threads; i++) { + for (int i =3D 0; i < values->threads; i++) { u64 *value =3D realloc(values->value[i], counters_max * sizeof(**values-= >value)); - int j; =20 if (!value) { pr_debug("failed to enlarge read_values ->values array"); - goto out_free_name; + goto out_free_counters; } =20 - for (j =3D values->counters_max; j < counters_max; j++) + for (int j =3D values->counters_max; j < counters_max; j++) value[j] =3D 0; =20 values->value[i] =3D value; } =20 values->counters_max =3D counters_max; - values->counterrawid =3D counterrawid; - values->countername =3D countername; + values->counters =3D new_counters; =20 return 0; -out_free_name: - free(countername); -out_free_rawid: - free(counterrawid); +out_free_counters: + free(new_counters); out_enomem: return -ENOMEM; } =20 static int perf_read_values__findnew_counter(struct perf_read_values *valu= es, - u64 rawid, const char *name) + struct evsel *evsel) { int i; =20 - for (i =3D 0; i < values->counters; i++) - if (values->counterrawid[i] =3D=3D rawid) + for (i =3D 0; i < values->num_counters; i++) + if (values->counters[i] =3D=3D evsel) return i; =20 - if (values->counters =3D=3D values->counters_max) { - i =3D perf_read_values__enlarge_counters(values); - if (i) - return i; + if (values->num_counters =3D=3D values->counters_max) { + int err =3D perf_read_values__enlarge_counters(values); + + if (err) + return err; } =20 - i =3D values->counters++; - values->counterrawid[i] =3D rawid; - values->countername[i] =3D strdup(name); + i =3D values->num_counters++; + values->counters[i] =3D evsel; =20 return i; } =20 int perf_read_values_add_value(struct perf_read_values *values, u32 pid, u32 tid, - u64 rawid, const char *name, u64 value) + struct evsel *evsel, u64 value) { int tindex, cindex; =20 tindex =3D perf_read_values__findnew_thread(values, pid, tid); if (tindex < 0) return tindex; - cindex =3D perf_read_values__findnew_counter(values, rawid, name); + cindex =3D perf_read_values__findnew_counter(values, evsel); if (cindex < 0) return cindex; =20 @@ -205,15 +189,15 @@ static void perf_read_values__display_pretty(FILE *fp, int pidwidth, tidwidth; int *counterwidth; =20 - counterwidth =3D malloc(values->counters * sizeof(*counterwidth)); + counterwidth =3D malloc(values->num_counters * sizeof(*counterwidth)); if (!counterwidth) { fprintf(fp, "INTERNAL ERROR: Failed to allocate counterwidth array\n"); return; } tidwidth =3D 3; pidwidth =3D 3; - for (j =3D 0; j < values->counters; j++) - counterwidth[j] =3D strlen(values->countername[j]); + for (j =3D 0; j < values->num_counters; j++) + counterwidth[j] =3D strlen(evsel__name(values->counters[j])); for (i =3D 0; i < values->threads; i++) { int width; =20 @@ -223,7 +207,7 @@ static void perf_read_values__display_pretty(FILE *fp, width =3D snprintf(NULL, 0, "%d", values->tid[i]); if (width > tidwidth) tidwidth =3D width; - for (j =3D 0; j < values->counters; j++) { + for (j =3D 0; j < values->num_counters; j++) { width =3D snprintf(NULL, 0, "%" PRIu64, values->value[i][j]); if (width > counterwidth[j]) counterwidth[j] =3D width; @@ -231,14 +215,14 @@ static void perf_read_values__display_pretty(FILE *fp, } =20 fprintf(fp, "# %*s %*s", pidwidth, "PID", tidwidth, "TID"); - for (j =3D 0; j < values->counters; j++) - fprintf(fp, " %*s", counterwidth[j], values->countername[j]); + for (j =3D 0; j < values->num_counters; j++) + fprintf(fp, " %*s", counterwidth[j], evsel__name(values->counters[j])); fprintf(fp, "\n"); =20 for (i =3D 0; i < values->threads; i++) { fprintf(fp, " %*d %*d", pidwidth, values->pid[i], tidwidth, values->tid[i]); - for (j =3D 0; j < values->counters; j++) + for (j =3D 0; j < values->num_counters; j++) fprintf(fp, " %*" PRIu64, counterwidth[j], values->value[i][j]); fprintf(fp, "\n"); @@ -266,16 +250,16 @@ static void perf_read_values__display_raw(FILE *fp, if (width > tidwidth) tidwidth =3D width; } - for (j =3D 0; j < values->counters; j++) { - width =3D strlen(values->countername[j]); + for (j =3D 0; j < values->num_counters; j++) { + width =3D strlen(evsel__name(values->counters[j])); if (width > namewidth) namewidth =3D width; - width =3D snprintf(NULL, 0, "%" PRIx64, values->counterrawid[j]); + width =3D snprintf(NULL, 0, "%x", values->counters[j]->core.idx); if (width > rawwidth) rawwidth =3D width; } for (i =3D 0; i < values->threads; i++) { - for (j =3D 0; j < values->counters; j++) { + for (j =3D 0; j < values->num_counters; j++) { width =3D snprintf(NULL, 0, "%" PRIu64, values->value[i][j]); if (width > countwidth) countwidth =3D width; @@ -287,12 +271,12 @@ static void perf_read_values__display_raw(FILE *fp, namewidth, "Name", rawwidth, "Raw", countwidth, "Count"); for (i =3D 0; i < values->threads; i++) - for (j =3D 0; j < values->counters; j++) - fprintf(fp, " %*d %*d %*s %*" PRIx64 " %*" PRIu64, + for (j =3D 0; j < values->num_counters; j++) + fprintf(fp, " %*d %*d %*s %*x %*" PRIu64, pidwidth, values->pid[i], tidwidth, values->tid[i], - namewidth, values->countername[j], - rawwidth, values->counterrawid[j], + namewidth, evsel__name(values->counters[j]), + rawwidth, values->counters[j]->core.idx, countwidth, values->value[i][j]); } =20 diff --git a/tools/perf/util/values.h b/tools/perf/util/values.h index 791c1ad606c2..bbca33daca19 100644 --- a/tools/perf/util/values.h +++ b/tools/perf/util/values.h @@ -5,14 +5,15 @@ #include #include =20 +struct evsel; + struct perf_read_values { int threads; int threads_max; u32 *pid, *tid; - int counters; + int num_counters; int counters_max; - u64 *counterrawid; - char **countername; + struct evsel **counters; u64 **value; }; =20 @@ -21,7 +22,7 @@ void perf_read_values_destroy(struct perf_read_values *va= lues); =20 int perf_read_values_add_value(struct perf_read_values *values, u32 pid, u32 tid, - u64 rawid, const char *name, u64 value); + struct evsel *evsel, u64 value); =20 void perf_read_values_display(FILE *fp, struct perf_read_values *values, int raw); --=20 2.47.0.338.g60cca15819-goog