perf tools: Factor out evsel__id_hdr_size()
authorAdrian Hunter <adrian.hunter@intel.com>
Mon, 11 Jul 2022 09:31:48 +0000 (12:31 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 Jul 2022 14:07:48 +0000 (11:07 -0300)
Factor out evsel__id_hdr_size() so it can be reused.

This is needed by perf inject. When injecting events from a guest perf.data
file, there is a possibility that the sample ID numbers conflict. To
re-write an ID sample, the old one needs to be removed first, which means
determining how big it is with evsel__id_hdr_size() and then subtracting
that from the event size.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: kvm@vger.kernel.org
Link: https://lore.kernel.org/r/20220711093218.10967-6-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/evlist.c
tools/perf/util/evsel.c
tools/perf/util/evsel.h

index 48af7d3..03fbe15 100644 (file)
@@ -1244,34 +1244,8 @@ bool evlist__valid_read_format(struct evlist *evlist)
 u16 evlist__id_hdr_size(struct evlist *evlist)
 {
        struct evsel *first = evlist__first(evlist);
-       struct perf_sample *data;
-       u64 sample_type;
-       u16 size = 0;
 
-       if (!first->core.attr.sample_id_all)
-               goto out;
-
-       sample_type = first->core.attr.sample_type;
-
-       if (sample_type & PERF_SAMPLE_TID)
-               size += sizeof(data->tid) * 2;
-
-       if (sample_type & PERF_SAMPLE_TIME)
-               size += sizeof(data->time);
-
-       if (sample_type & PERF_SAMPLE_ID)
-               size += sizeof(data->id);
-
-       if (sample_type & PERF_SAMPLE_STREAM_ID)
-               size += sizeof(data->stream_id);
-
-       if (sample_type & PERF_SAMPLE_CPU)
-               size += sizeof(data->cpu) * 2;
-
-       if (sample_type & PERF_SAMPLE_IDENTIFIER)
-               size += sizeof(data->id);
-out:
-       return size;
+       return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
 }
 
 bool evlist__valid_sample_id_all(struct evlist *evlist)
index a67cc3f..9a30ccb 100644 (file)
@@ -2724,6 +2724,32 @@ int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
        return 0;
 }
 
+u16 evsel__id_hdr_size(struct evsel *evsel)
+{
+       u64 sample_type = evsel->core.attr.sample_type;
+       u16 size = 0;
+
+       if (sample_type & PERF_SAMPLE_TID)
+               size += sizeof(u64);
+
+       if (sample_type & PERF_SAMPLE_TIME)
+               size += sizeof(u64);
+
+       if (sample_type & PERF_SAMPLE_ID)
+               size += sizeof(u64);
+
+       if (sample_type & PERF_SAMPLE_STREAM_ID)
+               size += sizeof(u64);
+
+       if (sample_type & PERF_SAMPLE_CPU)
+               size += sizeof(u64);
+
+       if (sample_type & PERF_SAMPLE_IDENTIFIER)
+               size += sizeof(u64);
+
+       return size;
+}
+
 struct tep_format_field *evsel__field(struct evsel *evsel, const char *name)
 {
        return tep_find_field(evsel->tp_format, name);
index 92bed8e..699448f 100644 (file)
@@ -381,6 +381,8 @@ int evsel__parse_sample(struct evsel *evsel, union perf_event *event,
 int evsel__parse_sample_timestamp(struct evsel *evsel, union perf_event *event,
                                  u64 *timestamp);
 
+u16 evsel__id_hdr_size(struct evsel *evsel);
+
 static inline struct evsel *evsel__next(struct evsel *evsel)
 {
        return list_entry(evsel->core.node.next, struct evsel, core.node);