From nobody Sat Nov 23 01:33:22 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 DE8A61B21AD for ; Thu, 14 Nov 2024 23:07:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731625643; cv=none; b=KURIrZqjTEDptLb3+181vzM9Xf4rU5DKztyE6d2EPX4CTzGwNuvqHslTVa8EtC33oLlL9/TIlrl2duDXpLjsAkwE5QFALN9lpuIKXd7cIQ96NgBauXRJL4UfDR6nQd8W3TUmiUUtSX+9v/U5GJztDGJchIYJT8cUWgWeWeZN1+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731625643; c=relaxed/simple; bh=+cGZw+tvHLTewZNjqPb2GZKmgFpnAggUx3gDJN+OFw4=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=mpOrVzC8vVLh2YV/MnbqFLxAZW7oLyxfzqiuBUqzuxHt7PLd9nqBn5BF5nIljrrki4KZq6CLwko/CifVRgovv3eIjsqrt5p1eYkdYtiKf+ub5uoF1q0cTeyQo+1NN0QifpHemFzkNqPuc9y4VrFutlWXB0snwgTTNgIp1gixTOQ= 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=m3tVzvBK; arc=none smtp.client-ip=209.85.128.202 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="m3tVzvBK" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e35199eb2bso24634787b3.3 for ; Thu, 14 Nov 2024 15:07:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731625641; x=1732230441; 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=awvak6BK0I2zeOsGhl415ZTiOmR3ErcblxICLfuyEXo=; b=m3tVzvBKr12WpJ1OGYTCHpYQCtvOJ2h0IPSglL1IXiplk/n1F++GkOivCQvARLiRs6 I6ahqm039LEDnFKymw7MLoubZa9SyzzhFbZuiTUx6o19TKr9sMpd9R8+S9bnmkktWqbS NRJrBIoFhuOdhFMV3cl9BFciOHGEz4cE6+UEvssmT3y4YEyT1/7R6bhSp2l3RhYvQecB 7PsIlUKbfz1kb3bV4MZcMibRFaXG3uT3gTNVe4+2QIMwnRRTkHuiZ9U/3DRvcRchFRZa XCC50sSDwD8/BgmyrO8g+QNNFyYFUEYU/UVWbSIO01uL4zK0e6GpHeCyeSyJ+pITuiwP KPVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731625641; x=1732230441; 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=awvak6BK0I2zeOsGhl415ZTiOmR3ErcblxICLfuyEXo=; b=SyDUkD/evewCW4loMzXaEBa0vhLPmaURe2IWKMof8HDsGT9AMMpcEfZpj3BFES/+DI HN4TNsbhsRvpivxGU9wXCBIrG3SLPgKkLYRo5QdPDzCkaE5vLs0YL/zmrztsY7J12ON6 5r8WiQj5gKTGJlU/E2eitBssaPtwfNrMlUEOKNe4eYU1vt8xyPrhUAoZt4KU+AA1Ho6J LxCC86dru7Wmj0Fxwv3bPTJ5FKNCE0aUZx2L2Gm0KW6ogyFdbePdIob2FGoIced+lGP7 dlsDLQQH8wGXBtLdmCuXaEWYFX6XgStT9B5xKH9/YrMdFLqggGmUlROeUHl2f7t8ZLqK GZwA== X-Forwarded-Encrypted: i=1; AJvYcCXVeZPg4yXarc5RFTq/AnNY0sPrteO4qLLR6vRDYzqiKVqvIs7ZSmAzisa4UGMSh4RDz6mRYokAeAjjYJI=@vger.kernel.org X-Gm-Message-State: AOJu0YzU7vPXKLL3ozqZ/FCjhsNaClgykw9wnMvq+K/8CS7kp8yOo+dM ke9nigpw01/FwilVY+4ufmyhP/b++4gyKt6Zp1gUcGCBzSz/oitvqkwcZ5xMfdRn5dsaqd+dO7m j1j122g== X-Google-Smtp-Source: AGHT+IE01ZeBfC+uVoy8uSyATfWpgc2iMl0fBqMumClpOcR0gGEexzjcXqB7R5akVW/056emwX3JEbH44xkV X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:7595:be86:b969:ac41]) (user=irogers job=sendgmr) by 2002:a0d:c687:0:b0:6e3:ce76:103f with SMTP id 00721157ae682-6ee55cb79a4mr291297b3.6.1731625640890; Thu, 14 Nov 2024 15:07:20 -0800 (PST) Date: Thu, 14 Nov 2024 15:07:11 -0800 In-Reply-To: <20241114230713.330701-1-irogers@google.com> Message-Id: <20241114230713.330701-2-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 1/3] perf stream: 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. Signed-off-by: Ian Rogers Reviewed-by: James Clark --- tools/perf/builtin-diff.c | 4 ++-- tools/perf/util/stream.c | 7 +++---- tools/perf/util/stream.h | 10 +++++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/tools/perf/builtin-diff.c b/tools/perf/builtin-diff.c index 82fb7773e03e..f72c791dada4 100644 --- a/tools/perf/builtin-diff.c +++ b/tools/perf/builtin-diff.c @@ -1019,12 +1019,12 @@ static int process_base_stream(struct data__file *d= ata_base, continue; =20 es_base =3D evsel_streams__entry(data_base->evlist_streams, - evsel_base->core.idx); + evsel_base); if (!es_base) return -1; =20 es_pair =3D evsel_streams__entry(data_pair->evlist_streams, - evsel_pair->core.idx); + evsel_pair); if (!es_pair) return -1; =20 diff --git a/tools/perf/util/stream.c b/tools/perf/util/stream.c index 545e44981a27..3de4a6130853 100644 --- a/tools/perf/util/stream.c +++ b/tools/perf/util/stream.c @@ -52,7 +52,6 @@ static struct evlist_streams *evlist_streams__new(int nr_= evsel, goto err; =20 s->nr_streams_max =3D nr_streams_max; - s->evsel_idx =3D -1; } =20 els->ev_streams =3D es; @@ -139,7 +138,7 @@ static int evlist__init_callchain_streams(struct evlist= *evlist, =20 hists__output_resort(hists, NULL); init_hot_callchain(hists, &es[i]); - es[i].evsel_idx =3D pos->core.idx; + es[i].evsel =3D pos; i++; } =20 @@ -166,12 +165,12 @@ struct evlist_streams *evlist__create_streams(struct = evlist *evlist, } =20 struct evsel_streams *evsel_streams__entry(struct evlist_streams *els, - int evsel_idx) + const struct evsel *evsel) { struct evsel_streams *es =3D els->ev_streams; =20 for (int i =3D 0; i < els->nr_evsel; i++) { - if (es[i].evsel_idx =3D=3D evsel_idx) + if (es[i].evsel =3D=3D evsel) return &es[i]; } =20 diff --git a/tools/perf/util/stream.h b/tools/perf/util/stream.h index bee768874fea..50f7e6e04982 100644 --- a/tools/perf/util/stream.h +++ b/tools/perf/util/stream.h @@ -2,7 +2,9 @@ #ifndef __PERF_STREAM_H #define __PERF_STREAM_H =20 -#include "callchain.h" +struct callchain_node; +struct evlist; +struct evsel; =20 struct stream { struct callchain_node *cnode; @@ -11,9 +13,9 @@ struct stream { =20 struct evsel_streams { struct stream *streams; + const struct evsel *evsel; int nr_streams_max; int nr_streams; - int evsel_idx; u64 streams_hits; }; =20 @@ -22,15 +24,13 @@ struct evlist_streams { int nr_evsel; }; =20 -struct evlist; - void evlist_streams__delete(struct evlist_streams *els); =20 struct evlist_streams *evlist__create_streams(struct evlist *evlist, int nr_streams_max); =20 struct evsel_streams *evsel_streams__entry(struct evlist_streams *els, - int evsel_idx); + const struct evsel *evsel); =20 void evsel_streams__match(struct evsel_streams *es_base, struct evsel_streams *es_pair); --=20 2.47.0.338.g60cca15819-goog From nobody Sat Nov 23 01:33:22 2024 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 Reviewed-by: James Clark --- 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 From nobody Sat Nov 23 01:33:22 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 389FF1B3952 for ; Thu, 14 Nov 2024 23:07:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731625654; cv=none; b=i4CYABZQei1y5vTiNi6bx8GLPO3qod9n/UOKsOZ9MUyQhniBTBtGFyDeTiCnTCL5ygRJizNFrPEHxfZFIFVvMLInLPbO+TiId2Ej5gNj6iuueBaFllTgZSd9gEUmcjSELbMU+7AbpTsS7XzZCM78ym7ybWPjjkjizPX9wGxbqpM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731625654; c=relaxed/simple; bh=3JoeJuw8QxsbHL6WLuQjYhTPCcdRnI6vX0QdH3V4OBQ=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=dk2TVNYMqSOfWInECfz9STpBkcWyEgoCpyS8mnWZCXKs0giBo9ijV2twW9XRmAw7fnBkl78aqjtXqV+C1kZtV8shnvxejtFPX1ju8QSbncxtzbk6bKYZTiElb7vHUKPb15TKUAMNyFOMhj/Vlcdzcz1gUynbCrlYYBOkT+OfGEg= 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=O9noGree; arc=none smtp.client-ip=209.85.128.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="O9noGree" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ee3fc1090cso18117947b3.0 for ; Thu, 14 Nov 2024 15:07:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731625650; x=1732230450; 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=MVuzS7YJqN+s4J7S8KkTY7fYBB6a47H7Bb0wygpO12c=; b=O9noGreeQrUw+HN9GJDABfpsJxOgJrHoPxP/ckJLU1jxj6hOC63zPjqM3q/7Ap+MiX chC5aWlnSuDANblQtiy0RH3JHcPOtmaLsEUPgH/PKkMB4uSCIe6LHGvzwFxAkkiUpK6f xfFSt8otyXW1lGb3Ku+9X4nEDsCB34ZFJdgI4UV1wVV0HBmCDWmV2fMRN9sEXDs/+5GE Z7hEJLWcKfejZE5sdoDisQ/7qb8RMq70l2v1jHjiAVis8S/1cDgNABIGMh2QotQZ+C5o Wi3V/RVOw8XCTpq2MIu5KbXyKgAeeOfgC3K+t7PAiPF0bqpj1h5YNJ0l+Ibk5JeozoHv fAcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731625650; x=1732230450; 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=MVuzS7YJqN+s4J7S8KkTY7fYBB6a47H7Bb0wygpO12c=; b=b6hj611C3wQMf7iJgzMlZchGNiP/yTQ55QWb0pAbCX9DO5TKyeXWa7OR95A3AsAQ9p Mt2/qbtGgsu34tmOotvqEnF+MfWnx0eCMiMaFIinNGlGjKyLFq7zNjr/PFKvKpQ1Iwpe eGAIMEtTYOc43UGz/6mHvxlpHZ0RktohJmmHUk5aYS5ReiRMi2eF6kVRjqAOetco+Osx hMNr+M3CaJ2cobv2HCgKL/DKyE4PP5vSShao2VNyM/gNqJa1kQIOVRewIBqpsFfiQDaI AnafbEap6ppVKaQBpHr8F5iqlm+iiAofZbAkRhjBHYEFvZBPHWe2zcFTjeJa6iBGe4za FI4A== X-Forwarded-Encrypted: i=1; AJvYcCWIdlk45SsH3imulkfHV42MTx03IlFpLNnp+qJ0Nwet5KAUNl6eT11O+rVFzm1sek8R1uHm1LJeXqgDxKw=@vger.kernel.org X-Gm-Message-State: AOJu0YxbFQ/DPZ04ioE+5mv7c5CGYW8rsXCEZSsIswdwqK/rAQQYmqzP uNYwXSsm+Myn8XLYetyN6YWvNsY7gaLd9NgX0yUzemMBmfDyxgk4DZn5H3IkzcYw9Qce5f2HMP4 RjbzPKg== X-Google-Smtp-Source: AGHT+IFb9RpEJSri/jcDuTntZilE5runTdMWKIlRz1CGZ9It41OwwGqQKjZdre9qJ3utbEntStgk+ArT4Jpr X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:7595:be86:b969:ac41]) (user=irogers job=sendgmr) by 2002:a05:690c:8203:b0:6e2:ac0a:8982 with SMTP id 00721157ae682-6ee3c331f57mr386767b3.0.1731625650368; Thu, 14 Nov 2024 15:07:30 -0800 (PST) Date: Thu, 14 Nov 2024 15:07:13 -0800 In-Reply-To: <20241114230713.330701-1-irogers@google.com> Message-Id: <20241114230713.330701-4-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 3/3] perf annotate: Prefer passing evsel to evsel->core.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. Try to reduce it being part of APIs by explicitly passing the evsel in annotate code. Internally the code just reads evsel->core.idx so behavior is unchanged. Signed-off-by: Ian Rogers Reviewed-by: James Clark --- tools/perf/builtin-top.c | 4 ++-- tools/perf/ui/browsers/annotate.c | 2 +- tools/perf/util/annotate.c | 32 +++++++++++++++---------------- tools/perf/util/annotate.h | 20 ++++++++++--------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index 724a79386321..881e6cf26979 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -267,9 +267,9 @@ static void perf_top__show_details(struct perf_top *top) =20 if (top->evlist->enabled) { if (top->zero) - symbol__annotate_zero_histogram(symbol, top->sym_evsel->core.idx); + symbol__annotate_zero_histogram(symbol, top->sym_evsel); else - symbol__annotate_decay_histogram(symbol, top->sym_evsel->core.idx); + symbol__annotate_decay_histogram(symbol, top->sym_evsel); } if (more !=3D 0) printf("%d lines not displayed, maybe increase display entries [e]\n", m= ore); diff --git a/tools/perf/ui/browsers/annotate.c b/tools/perf/ui/browsers/ann= otate.c index d7e727345dab..135d6ce88fb3 100644 --- a/tools/perf/ui/browsers/annotate.c +++ b/tools/perf/ui/browsers/annotate.c @@ -754,7 +754,7 @@ static int annotate_browser__run(struct annotate_browse= r *browser, hbt->timer(hbt->arg); =20 if (delay_secs !=3D 0) { - symbol__annotate_decay_histogram(sym, evsel->core.idx); + symbol__annotate_decay_histogram(sym, evsel); hists__scnprintf_title(hists, title, sizeof(title)); annotate_browser__show(&browser->b, title, help); } diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index b1d98da79be8..9bd3f472a525 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -209,7 +209,7 @@ static int __symbol__account_cycles(struct cyc_hist *ch, } =20 static int __symbol__inc_addr_samples(struct map_symbol *ms, - struct annotated_source *src, int evidx, u64 addr, + struct annotated_source *src, struct evsel *evsel, u64 addr, struct perf_sample *sample) { struct symbol *sym =3D ms->sym; @@ -228,14 +228,14 @@ static int __symbol__inc_addr_samples(struct map_symb= ol *ms, } =20 offset =3D addr - sym->start; - h =3D annotated_source__histogram(src, evidx); + h =3D annotated_source__histogram(src, evsel); if (h =3D=3D NULL) { pr_debug("%s(%d): ENOMEM! sym->name=3D%s, start=3D%#" PRIx64 ", addr=3D%= #" PRIx64 ", end=3D%#" PRIx64 ", func: %d\n", __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type = =3D=3D STT_FUNC); return -ENOMEM; } =20 - hash_key =3D offset << 16 | evidx; + hash_key =3D offset << 16 | evsel->core.idx; if (!hashmap__find(src->samples, hash_key, &entry)) { entry =3D zalloc(sizeof(*entry)); if (entry =3D=3D NULL) @@ -252,7 +252,7 @@ static int __symbol__inc_addr_samples(struct map_symbol= *ms, =20 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 ", evidx=3D%d] =3D> nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", - sym->start, sym->name, addr, addr - sym->start, evidx, + sym->start, sym->name, addr, addr - sym->start, evsel->core.idx, entry->nr_samples, entry->period); return 0; } @@ -323,7 +323,7 @@ static int symbol__inc_addr_samples(struct map_symbol *= ms, if (sym =3D=3D NULL) return 0; src =3D symbol__hists(sym, evsel->evlist->core.nr_entries); - return src ? __symbol__inc_addr_samples(ms, src, evsel->core.idx, addr, s= ample) : 0; + return src ? __symbol__inc_addr_samples(ms, src, evsel, addr, sample) : 0; } =20 static int symbol__account_br_cntr(struct annotated_branch *branch, @@ -861,15 +861,14 @@ static void calc_percent(struct annotation *notes, s64 offset, s64 end) { struct hists *hists =3D evsel__hists(evsel); - int evidx =3D evsel->core.idx; - struct sym_hist *sym_hist =3D annotation__histogram(notes, evidx); + struct sym_hist *sym_hist =3D annotation__histogram(notes, evsel); unsigned int hits =3D 0; u64 period =3D 0; =20 while (offset < end) { struct sym_hist_entry *entry; =20 - entry =3D annotated_source__hist_entry(notes->src, evidx, offset); + entry =3D annotated_source__hist_entry(notes->src, evsel, offset); if (entry) { hits +=3D entry->nr_samples; period +=3D entry->period; @@ -1140,15 +1139,14 @@ static void print_summary(struct rb_root *root, con= st char *filename) =20 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel) { - int evidx =3D evsel->core.idx; struct annotation *notes =3D symbol__annotation(sym); - struct sym_hist *h =3D annotation__histogram(notes, evidx); + struct sym_hist *h =3D annotation__histogram(notes, evsel); u64 len =3D symbol__size(sym), offset; =20 for (offset =3D 0; offset < len; ++offset) { struct sym_hist_entry *entry; =20 - entry =3D annotated_source__hist_entry(notes->src, evidx, offset); + entry =3D annotated_source__hist_entry(notes->src, evsel, offset); if (entry && entry->nr_samples !=3D 0) printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, sym->start + offset, entry->nr_samples); @@ -1178,7 +1176,7 @@ int symbol__annotate_printf(struct map_symbol *ms, st= ruct evsel *evsel) const char *d_filename; const char *evsel_name =3D evsel__name(evsel); struct annotation *notes =3D symbol__annotation(sym); - struct sym_hist *h =3D annotation__histogram(notes, evsel->core.idx); + struct sym_hist *h =3D annotation__histogram(notes, evsel); struct annotation_line *pos, *queue =3D NULL; struct annotation_options *opts =3D &annotate_opts; u64 start =3D map__rip_2objdump(map, sym->start); @@ -1364,18 +1362,18 @@ int map_symbol__annotation_dump(struct map_symbol *= ms, struct evsel *evsel) return err; } =20 -void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) +void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evs= el) { struct annotation *notes =3D symbol__annotation(sym); - struct sym_hist *h =3D annotation__histogram(notes, evidx); + struct sym_hist *h =3D annotation__histogram(notes, evsel); =20 memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms); } =20 -void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) +void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *ev= sel) { struct annotation *notes =3D symbol__annotation(sym); - struct sym_hist *h =3D annotation__histogram(notes, evidx); + struct sym_hist *h =3D annotation__histogram(notes, evsel); struct annotation_line *al; =20 h->nr_samples =3D 0; @@ -1385,7 +1383,7 @@ void symbol__annotate_decay_histogram(struct symbol *= sym, int evidx) if (al->offset =3D=3D -1) continue; =20 - entry =3D annotated_source__hist_entry(notes->src, evidx, al->offset); + entry =3D annotated_source__hist_entry(notes->src, evsel, al->offset); if (entry =3D=3D NULL) continue; =20 diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index 8b9e05a1932f..2458e99abc18 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -15,6 +15,7 @@ #include "hashmap.h" #include "disasm.h" #include "branch.h" +#include "evsel.h" =20 struct hist_browser_timer; struct hist_entry; @@ -23,7 +24,6 @@ struct map_symbol; struct addr_map_symbol; struct option; struct perf_sample; -struct evsel; struct symbol; struct annotated_data_type; =20 @@ -367,21 +367,23 @@ static inline u8 annotation__br_cntr_width(void) void annotation__update_column_widths(struct annotation *notes); void annotation__toggle_full_addr(struct annotation *notes, struct map_sym= bol *ms); =20 -static inline struct sym_hist *annotated_source__histogram(struct annotate= d_source *src, int idx) +static inline struct sym_hist *annotated_source__histogram(struct annotate= d_source *src, + const struct evsel *evsel) { - return &src->histograms[idx]; + return &src->histograms[evsel->core.idx]; } =20 -static inline struct sym_hist *annotation__histogram(struct annotation *no= tes, int idx) +static inline struct sym_hist *annotation__histogram(struct annotation *no= tes, + const struct evsel *evsel) { - return annotated_source__histogram(notes->src, idx); + return annotated_source__histogram(notes->src, evsel); } =20 static inline struct sym_hist_entry * -annotated_source__hist_entry(struct annotated_source *src, int idx, u64 of= fset) +annotated_source__hist_entry(struct annotated_source *src, const struct ev= sel *evsel, u64 offset) { struct sym_hist_entry *entry; - long key =3D offset << 16 | idx; + long key =3D offset << 16 | evsel->core.idx; =20 if (!hashmap__find(src->samples, key, &entry)) return NULL; @@ -442,8 +444,8 @@ enum symbol_disassemble_errno { int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *= buf, size_t buflen); =20 int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel); -void symbol__annotate_zero_histogram(struct symbol *sym, int evidx); -void symbol__annotate_decay_histogram(struct symbol *sym, int evidx); +void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evs= el); +void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *ev= sel); void annotated_source__purge(struct annotated_source *as); =20 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel= ); --=20 2.47.0.338.g60cca15819-goog