perf inject: Combine build_ids and build_id_all into enum
authorIan Rogers <irogers@google.com>
Sat, 17 Aug 2024 06:44:36 +0000 (23:44 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 19 Aug 2024 17:53:55 +0000 (14:53 -0300)
It is clearer to have a single enum that determines how build ids are
injected, it also allows for future extension.

Set the header build ID feature whether lazy or all are generated,
previously only the lazy case would set it.

Allow parsing of known build IDs for either the lazy or all cases.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Anne Macedo <retpolanne@posteo.net>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Casey Chen <cachen@purestorage.com>
Cc: Chaitanya S Prakash <chaitanyas.prakash@arm.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dominique Martinet <asmadeus@codewreck.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jann Horn <jannh@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sun Haiyong <sunhaiyong@loongson.cn>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Yunseong Kim <yskelg@gmail.com>
Cc: Ze Gao <zegao2021@gmail.com>
Link: https://lore.kernel.org/r/20240817064442.2152089-8-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-inject.c

index 8440ddf..865d16c 100644 (file)
@@ -103,11 +103,16 @@ struct guest_session {
        struct guest_event              ev;
 };
 
+enum build_id_rewrite_style {
+       BID_RWS__NONE = 0,
+       BID_RWS__INJECT_HEADER_LAZY,
+       BID_RWS__INJECT_HEADER_ALL,
+};
+
 struct perf_inject {
        struct perf_tool        tool;
        struct perf_session     *session;
-       bool                    build_ids;
-       bool                    build_id_all;
+       enum build_id_rewrite_style build_id_style;
        bool                    sched_stat;
        bool                    have_auxtrace;
        bool                    strip;
@@ -2015,8 +2020,8 @@ static int __cmd_inject(struct perf_inject *inject)
 
        signal(SIGINT, sig_handler);
 
-       if (inject->build_ids || inject->sched_stat ||
-           inject->itrace_synth_opts.set || inject->build_id_all) {
+       if (inject->build_id_style != BID_RWS__NONE || inject->sched_stat ||
+           inject->itrace_synth_opts.set) {
                inject->tool.mmap         = perf_event__repipe_mmap;
                inject->tool.mmap2        = perf_event__repipe_mmap2;
                inject->tool.fork         = perf_event__repipe_fork;
@@ -2027,10 +2032,10 @@ static int __cmd_inject(struct perf_inject *inject)
 
        output_data_offset = perf_session__data_offset(session->evlist);
 
-       if (inject->build_id_all) {
+       if (inject->build_id_style == BID_RWS__INJECT_HEADER_ALL) {
                inject->tool.mmap         = perf_event__repipe_buildid_mmap;
                inject->tool.mmap2        = perf_event__repipe_buildid_mmap2;
-       } else if (inject->build_ids) {
+       } else if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY) {
                inject->tool.sample = perf_event__inject_buildid;
        } else if (inject->sched_stat) {
                struct evsel *evsel;
@@ -2148,9 +2153,9 @@ static int __cmd_inject(struct perf_inject *inject)
                        .inject = inject,
                };
 
-               if (inject->build_ids)
-                       perf_header__set_feat(&session->header,
-                                             HEADER_BUILD_ID);
+               if (inject->build_id_style == BID_RWS__INJECT_HEADER_LAZY ||
+                   inject->build_id_style == BID_RWS__INJECT_HEADER_ALL)
+                       perf_header__set_feat(&session->header, HEADER_BUILD_ID);
                /*
                 * Keep all buildids when there is unprocessed AUX data because
                 * it is not known which ones the AUX trace hits.
@@ -2211,11 +2216,13 @@ int cmd_inject(int argc, const char **argv)
        int ret;
        bool repipe = true;
        const char *known_build_ids = NULL;
+       bool build_ids;
+       bool build_id_all;
 
        struct option options[] = {
-               OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
+               OPT_BOOLEAN('b', "build-ids", &build_ids,
                            "Inject build-ids into the output stream"),
-               OPT_BOOLEAN(0, "buildid-all", &inject.build_id_all,
+               OPT_BOOLEAN(0, "buildid-all", &build_id_all,
                            "Inject build-ids of all DSOs into the output stream"),
                OPT_STRING(0, "known-build-ids", &known_build_ids,
                           "buildid path [,buildid path...]",
@@ -2313,6 +2320,10 @@ int cmd_inject(int argc, const char **argv)
                        return -1;
                }
        }
+       if (build_ids)
+               inject.build_id_style = BID_RWS__INJECT_HEADER_LAZY;
+       if (build_id_all)
+               inject.build_id_style = BID_RWS__INJECT_HEADER_ALL;
 
        data.path = inject.input_name;
        if (!strcmp(inject.input_name, "-") || inject.output.is_pipe) {
@@ -2326,7 +2337,7 @@ int cmd_inject(int argc, const char **argv)
                        repipe = false;
        }
        ordered_events = inject.jit_mode || inject.sched_stat ||
-               (inject.build_ids && !inject.build_id_all);
+               (inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY);
        perf_tool__init(&inject.tool, ordered_events);
        inject.tool.sample              = perf_event__repipe_sample;
        inject.tool.read                = perf_event__repipe_sample;
@@ -2398,7 +2409,7 @@ int cmd_inject(int argc, const char **argv)
                        goto out_delete;
        }
 
-       if (inject.build_ids && !inject.build_id_all) {
+       if (inject.build_id_style == BID_RWS__INJECT_HEADER_LAZY) {
                /*
                 * to make sure the mmap records are ordered correctly
                 * and so that the correct especially due to jitted code
@@ -2406,14 +2417,14 @@ int cmd_inject(int argc, const char **argv)
                 * inject the jit mmaps at the same time for now.
                 */
                inject.tool.ordering_requires_timestamps = true;
-               if (known_build_ids != NULL) {
-                       inject.known_build_ids =
-                               perf_inject__parse_known_build_ids(known_build_ids);
+       }
+       if (inject.build_id_style != BID_RWS__NONE && known_build_ids != NULL) {
+               inject.known_build_ids =
+                       perf_inject__parse_known_build_ids(known_build_ids);
 
-                       if (inject.known_build_ids == NULL) {
-                               pr_err("Couldn't parse known build ids.\n");
-                               goto out_delete;
-                       }
+               if (inject.known_build_ids == NULL) {
+                       pr_err("Couldn't parse known build ids.\n");
+                       goto out_delete;
                }
        }