Skip to contentSkip to conversation

Prefer evsel over evsel->core.idx

PATCH Applied Ian Rogers 4 participants 9 messages Nov 14, 2024 – Dec 20, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover Prefer evsel over evsel->core.idx

James Clark's patches fixing evsel->core.idx [1] reminded me that we pass the int value around unnecessarily. Passing the evsel avoids issues if the evlist is reordered but paired with sanitizers we can also know when something is used when it shouldn't be. These patches do some initial work reducing the use of evsel->core.idx or reducing the API to pass evsels and not their interior index.

[1] https://lore.kernel.org/lkml/[email protected]/

Ian Rogers (3):
perf stream: Use evsel rather than evsel->idx perf values: Use evsel rather than evsel->idx perf annotate: Prefer passing evsel to evsel->core.idx

 tools/perf/builtin-diff.c         |   4 +-
 tools/perf/builtin-report.c       |   4 +-
 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 +++---
 tools/perf/util/stream.c          |   7 +-
 tools/perf/util/stream.h          |  10 +--
 tools/perf/util/values.c          | 106 +++++++++++++-----------------
 tools/perf/util/values.h          |   9 +--
 10 files changed, 90 insertions(+), 108 deletions(-)
Signature · 1 line
2.47.0.338.g60cca15819-goog
author reviewer
#1 lore raw

PATCH 1/3 perf stream: Use evsel rather than evsel->idx

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.

---
 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(-)
tools/perf/builtin-diff.c +2 -2
index 82fb7773e03e..f72c791dada4 100644
@@ -1019,12 +1019,12 @@ static int process_base_stream(struct data__file *data_base,
 			continue;
 
 		es_base = evsel_streams__entry(data_base->evlist_streams,
-					       evsel_base->core.idx);
+					       evsel_base);
 		if (!es_base)
 			return -1;
 
 		es_pair = evsel_streams__entry(data_pair->evlist_streams,
-					       evsel_pair->core.idx);
+					       evsel_pair);
 		if (!es_pair)
 			return -1;
 
tools/perf/util/stream.c +3 -4
index 545e44981a27..3de4a6130853 100644
@@ -52,7 +52,6 @@ static struct evlist_streams *evlist_streams__new(int nr_evsel,
 			goto err;
 
 		s->nr_streams_max = nr_streams_max;
-		s->evsel_idx = -1;
 	}
 
 	els->ev_streams = es;
@@ -139,7 +138,7 @@ static int evlist__init_callchain_streams(struct evlist *evlist,
 
 		hists__output_resort(hists, NULL);
 		init_hot_callchain(hists, &es[i]);
-		es[i].evsel_idx = pos->core.idx;
+		es[i].evsel = pos;
 		i++;
 	}
 
@@ -166,12 +165,12 @@ struct evlist_streams *evlist__create_streams(struct evlist *evlist,
 }
 
 struct evsel_streams *evsel_streams__entry(struct evlist_streams *els,
-					   int evsel_idx)
+					   const struct evsel *evsel)
 {
 	struct evsel_streams *es = els->ev_streams;
 
 	for (int i = 0; i < els->nr_evsel; i++) {
-		if (es[i].evsel_idx == evsel_idx)
+		if (es[i].evsel == evsel)
 			return &es[i];
 	}
 
tools/perf/util/stream.h +5 -5
index bee768874fea..50f7e6e04982 100644
@@ -2,7 +2,9 @@
 #ifndef __PERF_STREAM_H
 #define __PERF_STREAM_H
 
-#include "callchain.h"
+struct callchain_node;
+struct evlist;
+struct evsel;
 
 struct stream {
 	struct callchain_node	*cnode;
@@ -11,9 +13,9 @@ struct stream {
 
 struct evsel_streams {
 	struct stream		*streams;
+	const struct evsel	*evsel;
 	int			nr_streams_max;
 	int			nr_streams;
-	int			evsel_idx;
 	u64			streams_hits;
 };
 
@@ -22,15 +24,13 @@ struct evlist_streams {
 	int			nr_evsel;
 };
 
-struct evlist;
-
 void evlist_streams__delete(struct evlist_streams *els);
 
 struct evlist_streams *evlist__create_streams(struct evlist *evlist,
 					      int nr_streams_max);
 
 struct evsel_streams *evsel_streams__entry(struct evlist_streams *els,
-					   int evsel_idx);
+					   const struct evsel *evsel);
 
 void evsel_streams__match(struct evsel_streams *es_base,
 			  struct evsel_streams *es_pair);
Signature · 1 line
2.47.0.338.g60cca15819-goog
author reviewer
#2 lore raw

PATCH 2/3 perf values: Use evsel rather than evsel->idx

In reply to Ian Rogers

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.

---
 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(-)
tools/perf/builtin-report.c +1 -3
index 048c91960ba9..e5478082845c 100644
@@ -348,11 +348,9 @@ static int process_read_event(const struct perf_tool *tool,
 	struct report *rep = container_of(tool, struct report, tool);
 
 	if (rep->show_threads) {
-		const char *name = evsel__name(evsel);
 		int err = perf_read_values_add_value(&rep->show_threads_values,
 					   event->read.pid, event->read.tid,
-					   evsel->core.idx,
-					   name,
+					   evsel,
 					   event->read.value);
 
 		if (err)
tools/perf/util/values.c +45 -61

227 lines not on this page · raw message

tools/perf/util/values.h +5 -4
index 791c1ad606c2..bbca33daca19 100644
@@ -5,14 +5,15 @@
 #include <stdio.h>
 #include <linux/types.h>
 
+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;
 };
 
@@ -21,7 +22,7 @@ void perf_read_values_destroy(struct perf_read_values *values);
 
 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);
 
 void perf_read_values_display(FILE *fp, struct perf_read_values *values,
 			      int raw);
Signature · 1 line
2.47.0.338.g60cca15819-goog
author reviewer
#3 lore raw

PATCH 3/3 perf annotate: Prefer passing evsel to evsel->core.idx

In reply to Ian Rogers

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.

---
 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(-)
tools/perf/builtin-top.c +2 -2
index 724a79386321..881e6cf26979 100644
@@ -267,9 +267,9 @@ static void perf_top__show_details(struct perf_top *top)
 
 	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 != 0)
 		printf("%d lines not displayed, maybe increase display entries [e]\n", more);
tools/perf/ui/browsers/annotate.c +1 -1
index d7e727345dab..135d6ce88fb3 100644
@@ -754,7 +754,7 @@ static int annotate_browser__run(struct annotate_browser *browser,
 				hbt->timer(hbt->arg);
 
 			if (delay_secs != 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);
 			}
tools/perf/util/annotate.c +15 -17
index b1d98da79be8..9bd3f472a525 100644
@@ -209,7 +209,7 @@ static int __symbol__account_cycles(struct cyc_hist *ch,
 }
 
 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 = ms->sym;
@@ -228,14 +228,14 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,
 	}
 
 	offset = addr - sym->start;
-	h = annotated_source__histogram(src, evidx);
+	h = annotated_source__histogram(src, evsel);
 	if (h == NULL) {
 		pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
 			 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
 		return -ENOMEM;
 	}
 
-	hash_key = offset << 16 | evidx;
+	hash_key = offset << 16 | evsel->core.idx;
 	if (!hashmap__find(src->samples, hash_key, &entry)) {
 		entry = zalloc(sizeof(*entry));
 		if (entry == NULL)
@@ -252,7 +252,7 @@ static int __symbol__inc_addr_samples(struct map_symbol *ms,
 
 	pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
 		  ", evidx=%d] => 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 == NULL)
 		return 0;
 	src = symbol__hists(sym, evsel->evlist->core.nr_entries);
-	return src ? __symbol__inc_addr_samples(ms, src, evsel->core.idx, addr, sample) : 0;
+	return src ? __symbol__inc_addr_samples(ms, src, evsel, addr, sample) : 0;
 }
 
 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 = evsel__hists(evsel);
-	int evidx = evsel->core.idx;
-	struct sym_hist *sym_hist = annotation__histogram(notes, evidx);
+	struct sym_hist *sym_hist = annotation__histogram(notes, evsel);
 	unsigned int hits = 0;
 	u64 period = 0;
 
 	while (offset < end) {
 		struct sym_hist_entry *entry;
 
-		entry = annotated_source__hist_entry(notes->src, evidx, offset);
+		entry = annotated_source__hist_entry(notes->src, evsel, offset);
 		if (entry) {
 			hits   += entry->nr_samples;
 			period += entry->period;
@@ -1140,15 +1139,14 @@ static void print_summary(struct rb_root *root, const char *filename)
 
 static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
 {
-	int evidx = evsel->core.idx;
 	struct annotation *notes = symbol__annotation(sym);
-	struct sym_hist *h = annotation__histogram(notes, evidx);
+	struct sym_hist *h = annotation__histogram(notes, evsel);
 	u64 len = symbol__size(sym), offset;
 
 	for (offset = 0; offset < len; ++offset) {
 		struct sym_hist_entry *entry;
 
-		entry = annotated_source__hist_entry(notes->src, evidx, offset);
+		entry = annotated_source__hist_entry(notes->src, evsel, offset);
 		if (entry && entry->nr_samples != 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, struct evsel *evsel)
 	const char *d_filename;
 	const char *evsel_name = evsel__name(evsel);
 	struct annotation *notes = symbol__annotation(sym);
-	struct sym_hist *h = annotation__histogram(notes, evsel->core.idx);
+	struct sym_hist *h = annotation__histogram(notes, evsel);
 	struct annotation_line *pos, *queue = NULL;
 	struct annotation_options *opts = &annotate_opts;
 	u64 start = map__rip_2objdump(map, sym->start);
@@ -1364,18 +1362,18 @@ int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel)
 	return err;
 }
 
-void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
+void symbol__annotate_zero_histogram(struct symbol *sym, struct evsel *evsel)
 {
 	struct annotation *notes = symbol__annotation(sym);
-	struct sym_hist *h = annotation__histogram(notes, evidx);
+	struct sym_hist *h = annotation__histogram(notes, evsel);
 
 	memset(h, 0, sizeof(*notes->src->histograms) * notes->src->nr_histograms);
 }
 
-void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
+void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel)
 {
 	struct annotation *notes = symbol__annotation(sym);
-	struct sym_hist *h = annotation__histogram(notes, evidx);
+	struct sym_hist *h = annotation__histogram(notes, evsel);
 	struct annotation_line *al;
 
 	h->nr_samples = 0;
@@ -1385,7 +1383,7 @@ void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
 		if (al->offset == -1)
 			continue;
 
-		entry = annotated_source__hist_entry(notes->src, evidx, al->offset);
+		entry = annotated_source__hist_entry(notes->src, evsel, al->offset);
 		if (entry == NULL)
 			continue;
 
tools/perf/util/annotate.h +11 -9
index 8b9e05a1932f..2458e99abc18 100644
@@ -15,6 +15,7 @@
 #include "hashmap.h"
 #include "disasm.h"
 #include "branch.h"
+#include "evsel.h"
 
 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;
 
@@ -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_symbol *ms);
 
-static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src, int idx)
+static inline struct sym_hist *annotated_source__histogram(struct annotated_source *src,
+							   const struct evsel *evsel)
 {
-	return &src->histograms[idx];
+	return &src->histograms[evsel->core.idx];
 }
 
-static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
+static inline struct sym_hist *annotation__histogram(struct annotation *notes,
+						     const struct evsel *evsel)
 {
-	return annotated_source__histogram(notes->src, idx);
+	return annotated_source__histogram(notes->src, evsel);
 }
 
 static inline struct sym_hist_entry *
-annotated_source__hist_entry(struct annotated_source *src, int idx, u64 offset)
+annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset)
 {
 	struct sym_hist_entry *entry;
-	long key = offset << 16 | idx;
+	long key = offset << 16 | evsel->core.idx;
 
 	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);
 
 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 *evsel);
+void symbol__annotate_decay_histogram(struct symbol *sym, struct evsel *evsel);
 void annotated_source__purge(struct annotated_source *as);
 
 int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel);
Signature · 1 line
2.47.0.338.g60cca15819-goog
#4 lore raw

Re: [PATCH v1 3/3] perf annotate: Prefer passing evsel to evsel->core.idx

Quoted 4 lines · Ian Rogers

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.

I think this is where this comes into play, are you running 'make -C tools/perf tools-build' before submitting?

I'll have to stop now, but will fix this if you don´t have time for a v2.

- Arnaldo

ui/gtk/annotate.c: In function ‘perf_gtk__get_percent’:
ui/gtk/annotate.c:45:48: error: passing argument 2 of ‘annotation__histogram’ makes pointer from integer without a cast [-Wint-conversion]

   45 |         symhist = annotation__histogram(notes, evidx);
      |                                                ^~~~~
      |                                                |
      |                                                int
In file included from ui/gtk/annotate.c:5:
/home/acme/git/perf-tools-next/tools/perf/util/annotate.h:383:74: note: expected ‘const struct evsel *’ but argument is of type ‘int’
  383 |                                                      const struct evsel *evsel)
      |                                                      ~~~~~~~~~~~~~~~~~~~~^~~~~
ui/gtk/annotate.c:46:58: error: passing argument 2 of ‘annotated_source__hist_entry’ makes pointer from integer without a cast [-Wint-conversion]
   46 |         entry = annotated_source__hist_entry(notes->src, evidx, dl->al.offset);
      |                                                          ^~~~~
      |                                                          |
      |                                                          int
/home/acme/git/perf-tools-next/tools/perf/util/annotate.h:389:80: note: expected ‘const struct evsel *’ but argument is of type ‘int’
  389 | annotated_source__hist_entry(struct annotated_source *src, const struct evsel *evsel, u64 offset)
      |                                                            ~~~~~~~~~~~~~~~~~~~~^~~~~

TEST tests/shell/test_task_analyzer.sh.shellcheck_log
make[6]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:105: ui/gtk/annotate.o] Error 1
TEST tests/shell/test_uprobe_from_different_cu.sh.shellcheck_log
CC util/session.o
make[5]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:158: ui/gtk] Error 2 make[4]: *** [Makefile.perf:802: gtk-in.o] Error 2
make[4]: *** Waiting for unfinished jobs....
CC util/tool.o

reviewer Reviewed-by on the v1 series
#5 lore raw
In reply to Ian Rogers
Quoted 14 lines · Ian Rogers

James Clark's patches fixing evsel->core.idx [1] reminded me that we pass the int value around unnecessarily. Passing the evsel avoids issues if the evlist is reordered but paired with sanitizers we can also know when something is used when it shouldn't be. These patches do some initial work reducing the use of evsel->core.idx or reducing the API to pass evsels and not their interior index.

[1] https://lore.kernel.org/lkml/[email protected]/

Ian Rogers (3):
perf stream: Use evsel rather than evsel->idx perf values: Use evsel rather than evsel->idx perf annotate: Prefer passing evsel to evsel->core.idx

Quoted 12 lines
  tools/perf/builtin-diff.c         |   4 +-
  tools/perf/builtin-report.c       |   4 +-
  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 +++---
  tools/perf/util/stream.c          |   7 +-
  tools/perf/util/stream.h          |  10 +--
  tools/perf/util/values.c          | 106 +++++++++++++-----------------
  tools/perf/util/values.h          |   9 +--
  10 files changed, 90 insertions(+), 108 deletions(-)
maintainer Reviewed-by on the v1 series
#6 lore raw
In reply to Ian Rogers
Quoted 7 lines · Ian Rogers

James Clark's patches fixing evsel->core.idx [1] reminded me that we pass the int value around unnecessarily. Passing the evsel avoids issues if the evlist is reordered but paired with sanitizers we can also know when something is used when it shouldn't be. These patches do some initial work reducing the use of evsel->core.idx or reducing the API to pass evsels and not their interior index.

Quoted 6 lines

[1] https://lore.kernel.org/lkml/[email protected]/

Ian Rogers (3):
perf stream: Use evsel rather than evsel->idx perf values: Use evsel rather than evsel->idx perf annotate: Prefer passing evsel to evsel->core.idx

Thanks,
Namhyung

Quoted 16 lines
 tools/perf/builtin-diff.c         |   4 +-
 tools/perf/builtin-report.c       |   4 +-
 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 +++---
 tools/perf/util/stream.c          |   7 +-
 tools/perf/util/stream.h          |  10 +--
 tools/perf/util/values.c          | 106 +++++++++++++-----------------
 tools/perf/util/values.h          |   9 +--
 10 files changed, 90 insertions(+), 108 deletions(-)
Signature · 1 line
2.47.0.338.g60cca15819-goog
author reviewer Reviewed-by Namhyung Kim Reviewed-by James Clark
#7 lore raw
Quoted 9 lines · Namhyung Kim
Quoted 7 lines · Ian Rogers

James Clark's patches fixing evsel->core.idx [1] reminded me that we pass the int value around unnecessarily. Passing the evsel avoids issues if the evlist is reordered but paired with sanitizers we can also know when something is used when it shouldn't be. These patches do some initial work reducing the use of evsel->core.idx or reducing the API to pass evsels and not their interior index.

Quoted 6 lines
Quoted 6 lines

[1] https://lore.kernel.org/lkml/[email protected]/

Ian Rogers (3):
perf stream: Use evsel rather than evsel->idx perf values: Use evsel rather than evsel->idx perf annotate: Prefer passing evsel to evsel->core.idx

Ping.

Thanks,
Ian

maintainer Applied
#8 lore raw
Quoted 17 lines · Ian Rogers
Quoted 9 lines · Namhyung Kim
Quoted 7 lines · Ian Rogers

James Clark's patches fixing evsel->core.idx [1] reminded me that we pass the int value around unnecessarily. Passing the evsel avoids issues if the evlist is reordered but paired with sanitizers we can also know when something is used when it shouldn't be. These patches do some initial work reducing the use of evsel->core.idx or reducing the API to pass evsels and not their interior index.

Quoted 6 lines
Quoted 6 lines

[1] https://lore.kernel.org/lkml/[email protected]/

Ian Rogers (3):
perf stream: Use evsel rather than evsel->idx perf values: Use evsel rather than evsel->idx perf annotate: Prefer passing evsel to evsel->core.idx

Quoted 4 lines

Ping.

Thanks, applied to perf-tools-next,

- Arnaldo