]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
perf machine: Explicitly pass in host perf_env
authorIan Rogers <irogers@google.com>
Thu, 24 Jul 2025 16:32:56 +0000 (09:32 -0700)
committerNamhyung Kim <namhyung@kernel.org>
Fri, 25 Jul 2025 17:37:57 +0000 (10:37 -0700)
When creating a machine for the host explicitly pass in a scoped
perf_env. This removes a use of the global perf_env.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250724163302.596743-17-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
12 files changed:
tools/perf/builtin-buildid-list.c
tools/perf/builtin-kallsyms.c
tools/perf/builtin-trace.c
tools/perf/tests/code-reading.c
tools/perf/tests/dlfilter-test.c
tools/perf/tests/dwarf-unwind.c
tools/perf/tests/mmap-thread-lookup.c
tools/perf/tests/symbols.c
tools/perf/util/debug.c
tools/perf/util/machine.c
tools/perf/util/machine.h
tools/perf/util/probe-event.c

index 151cd84b6dfebd1f934bc095c70b56017693ed37..a91bbb34ac9463607de2e4c978a2c53d0112d68b 100644 (file)
@@ -45,11 +45,14 @@ static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
 
 static void buildid__show_kernel_maps(void)
 {
+       struct perf_env host_env;
        struct machine *machine;
 
-       machine = machine__new_host();
+       perf_env__init(&host_env);
+       machine = machine__new_host(&host_env);
        machine__for_each_kernel_map(machine, buildid__map_cb, NULL);
        machine__delete(machine);
+       perf_env__exit(&host_env);
 }
 
 static int sysfs__fprintf_build_id(FILE *fp)
index a3c2ffdc1af837b09fa2ae5ae1ec5e17fc69007b..3c4339982b16e36d3bbf75520b79195b58a32f24 100644 (file)
 #include <subcmd/parse-options.h>
 #include "debug.h"
 #include "dso.h"
+#include "env.h"
 #include "machine.h"
 #include "map.h"
 #include "symbol.h"
 
 static int __cmd_kallsyms(int argc, const char **argv)
 {
-       int i;
-       struct machine *machine = machine__new_kallsyms();
+       int i, err;
+       struct perf_env host_env;
+       struct machine *machine = NULL;
 
+
+       perf_env__init(&host_env);
+       err = perf_env__set_cmdline(&host_env, argc, argv);
+       if (err)
+               goto out;
+
+       machine = machine__new_kallsyms(&host_env);
        if (machine == NULL) {
                pr_err("Couldn't read /proc/kallsyms\n");
-               return -1;
+               err = -1;
+               goto out;
        }
 
        for (i = 0; i < argc; ++i) {
@@ -42,9 +52,10 @@ static int __cmd_kallsyms(int argc, const char **argv)
                        map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end),
                        symbol->start, symbol->end);
        }
-
+out:
        machine__delete(machine);
-       return 0;
+       perf_env__exit(&host_env);
+       return err;
 }
 
 int cmd_kallsyms(int argc, const char **argv)
index 0261f4eefe6d7ba2e62d5093989a95502ec5a399..5b53571de4000b1345eb99f84cc3cc9cb49e55d1 100644 (file)
@@ -140,6 +140,7 @@ struct syscall_fmt {
 };
 
 struct trace {
+       struct perf_env         host_env;
        struct perf_tool        tool;
        struct {
                /** Sorted sycall numbers used by the trace. */
@@ -1977,17 +1978,24 @@ static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long l
        return machine__resolve_kernel_addr(vmachine, addrp, modp);
 }
 
-static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
+static int trace__symbols_init(struct trace *trace, int argc, const char **argv,
+                              struct evlist *evlist)
 {
        int err = symbol__init(NULL);
 
        if (err)
                return err;
 
-       trace->host = machine__new_host();
-       if (trace->host == NULL)
-               return -ENOMEM;
+       perf_env__init(&trace->host_env);
+       err = perf_env__set_cmdline(&trace->host_env, argc, argv);
+       if (err)
+               goto out;
 
+       trace->host = machine__new_host(&trace->host_env);
+       if (trace->host == NULL) {
+               err = -ENOMEM;
+               goto out;
+       }
        thread__set_priv_destructor(thread_trace__delete);
 
        err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr);
@@ -1998,9 +2006,10 @@ static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
                                            evlist->core.threads, trace__tool_process,
                                            true, false, 1);
 out:
-       if (err)
+       if (err) {
+               perf_env__exit(&trace->host_env);
                symbol__exit();
-
+       }
        return err;
 }
 
@@ -2009,6 +2018,7 @@ static void trace__symbols__exit(struct trace *trace)
        machine__exit(trace->host);
        trace->host = NULL;
 
+       perf_env__exit(&trace->host_env);
        symbol__exit();
 }
 
@@ -4428,7 +4438,7 @@ create_maps:
                goto out_delete_evlist;
        }
 
-       err = trace__symbols_init(trace, evlist);
+       err = trace__symbols_init(trace, argc, argv, evlist);
        if (err < 0) {
                fprintf(trace->output, "Problems initializing symbol libraries!\n");
                goto out_delete_evlist;
index 0ec7004f90fe9ddf9e79d044d11935940b01ef00..9c2091310191f286cd5de2bbafb05546b071d6c2 100644 (file)
@@ -655,9 +655,8 @@ static int do_test_code_reading(bool try_kcore)
 
        pid = getpid();
 
-       machine = machine__new_host();
        perf_env__init(&host_env);
-       machine->env = &host_env;
+       machine = machine__new_host(&host_env);
 
        ret = machine__create_kernel_maps(machine);
        if (ret < 0) {
index 6427e3382711f818c981ce1707c80dad2ac17489..80a1c941138d31d98c84e5c2169acc80fd762365 100644 (file)
@@ -353,9 +353,8 @@ static int test__dlfilter_test(struct test_data *td)
                return test_result("Failed to find program symbols", TEST_FAIL);
 
        pr_debug("Creating new host machine structure\n");
-       td->machine = machine__new_host();
        perf_env__init(&host_env);
-       td->machine->env = &host_env;
+       td->machine = machine__new_host(&host_env);
 
        td->fd = creat(td->perf_data_file_name, 0644);
        if (td->fd < 0)
index 525c46b7971adb5e969bf5ceb32c09ae3b19d8e3..9ed78d00fb872d74dced9698574247958035c764 100644 (file)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include "tests.h"
 #include "debug.h"
+#include "env.h"
 #include "machine.h"
 #include "event.h"
 #include "../util/unwind.h"
@@ -180,6 +181,7 @@ NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_1(struct thread *th
 noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
                                int subtest __maybe_unused)
 {
+       struct perf_env host_env;
        struct machine *machine;
        struct thread *thread;
        int err = -1;
@@ -188,15 +190,16 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
        callchain_param.record_mode = CALLCHAIN_DWARF;
        dwarf_callchain_users = true;
 
-       machine = machine__new_live(/*kernel_maps=*/true, pid);
+       perf_env__init(&host_env);
+       machine = machine__new_live(&host_env, /*kernel_maps=*/true, pid);
        if (!machine) {
                pr_err("Could not get machine\n");
-               return -1;
+               goto out;
        }
 
        if (machine__create_kernel_maps(machine)) {
                pr_err("Failed to create kernel maps\n");
-               return -1;
+               goto out;
        }
 
        if (verbose > 1)
@@ -213,6 +216,7 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
 
  out:
        machine__delete(machine);
+       perf_env__exit(&host_env);
        return err;
 }
 
index 446a3615d7202484f93d51242d422efdb0736236..0c5619c6e6e999767a30cebf3f5fe5f39cb9a661 100644 (file)
@@ -8,6 +8,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include "debug.h"
+#include "env.h"
 #include "event.h"
 #include "tests.h"
 #include "machine.h"
@@ -155,6 +156,7 @@ static int synth_process(struct machine *machine)
 
 static int mmap_events(synth_cb synth)
 {
+       struct perf_env host_env;
        struct machine *machine;
        int err, i;
 
@@ -167,7 +169,8 @@ static int mmap_events(synth_cb synth)
         */
        TEST_ASSERT_VAL("failed to create threads", !threads_create());
 
-       machine = machine__new_host();
+       perf_env__init(&host_env);
+       machine = machine__new_host(&host_env);
 
        dump_trace = verbose > 1 ? 1 : 0;
 
@@ -209,6 +212,7 @@ static int mmap_events(synth_cb synth)
        }
 
        machine__delete(machine);
+       perf_env__exit(&host_env);
        return err;
 }
 
index b07fdf831868283bbefd9c08501ae61022e87bf5..f4ffe5804f401f5d239c1170e4088594eee8a727 100644 (file)
@@ -5,6 +5,7 @@
 #include <limits.h>
 #include "debug.h"
 #include "dso.h"
+#include "env.h"
 #include "machine.h"
 #include "thread.h"
 #include "symbol.h"
 #include "tests.h"
 
 struct test_info {
+       struct perf_env host_env;
        struct machine *machine;
        struct thread *thread;
 };
 
 static int init_test_info(struct test_info *ti)
 {
-       ti->machine = machine__new_host();
+       perf_env__init(&ti->host_env);
+       ti->machine = machine__new_host(&ti->host_env);
        if (!ti->machine) {
                pr_debug("machine__new_host() failed!\n");
+               perf_env__exit(&ti->host_env);
                return TEST_FAIL;
        }
 
@@ -29,6 +33,7 @@ static int init_test_info(struct test_info *ti)
        ti->thread = machine__findnew_thread(ti->machine, 100, 100);
        if (!ti->thread) {
                pr_debug("machine__findnew_thread() failed!\n");
+               perf_env__exit(&ti->host_env);
                return TEST_FAIL;
        }
 
@@ -39,6 +44,7 @@ static void exit_test_info(struct test_info *ti)
 {
        thread__put(ti->thread);
        machine__delete(ti->machine);
+       perf_env__exit(&ti->host_env);
 }
 
 struct dso_map {
index 2878a7363ac888f3b09f424e082c2498614c73ed..1dfa4d0eec4d7d5ac4be1c09cdb855efb2650e29 100644 (file)
@@ -17,6 +17,7 @@
 #include "addr_location.h"
 #include "color.h"
 #include "debug.h"
+#include "env.h"
 #include "event.h"
 #include "machine.h"
 #include "map.h"
@@ -309,8 +310,12 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
 {
        /* TODO: async safety. printf, malloc, etc. aren't safe inside a signal handler. */
        pid_t pid = getpid();
-       struct machine *machine = machine__new_live(/*kernel_maps=*/false, pid);
+       struct machine *machine;
        struct thread *thread = NULL;
+       struct perf_env host_env;
+
+       perf_env__init(&host_env);
+       machine = machine__new_live(&host_env, /*kernel_maps=*/false, pid);
 
        if (machine)
                thread = machine__find_thread(machine, pid, pid);
@@ -323,6 +328,7 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
                 */
                backtrace_symbols_fd(stackdump, stackdump_size, fileno(file));
                machine__delete(machine);
+               perf_env__exit(&host_env);
                return;
        }
 #endif
@@ -349,6 +355,7 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
        }
        thread__put(thread);
        machine__delete(machine);
+       perf_env__exit(&host_env);
 }
 
 /* Obtain a backtrace and print it to stdout. */
index 2ef8c1cfae1e9e81be829000d1d845583c7041c1..b5dd42588c916d9195ef0301314cbb08b0bc8dd6 100644 (file)
@@ -129,7 +129,7 @@ out:
        return 0;
 }
 
-static struct machine *__machine__new_host(bool kernel_maps)
+static struct machine *__machine__new_host(struct perf_env *host_env, bool kernel_maps)
 {
        struct machine *machine = malloc(sizeof(*machine));
 
@@ -142,13 +142,13 @@ static struct machine *__machine__new_host(bool kernel_maps)
                free(machine);
                return NULL;
        }
-       machine->env = &perf_env;
+       machine->env = host_env;
        return machine;
 }
 
-struct machine *machine__new_host(void)
+struct machine *machine__new_host(struct perf_env *host_env)
 {
-       return __machine__new_host(/*kernel_maps=*/true);
+       return __machine__new_host(host_env, /*kernel_maps=*/true);
 }
 
 static int mmap_handler(const struct perf_tool *tool __maybe_unused,
@@ -168,9 +168,9 @@ static int machine__init_live(struct machine *machine, pid_t pid)
                                                  mmap_handler, machine, true);
 }
 
-struct machine *machine__new_live(bool kernel_maps, pid_t pid)
+struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid)
 {
-       struct machine *machine = __machine__new_host(kernel_maps);
+       struct machine *machine = __machine__new_host(host_env, kernel_maps);
 
        if (!machine)
                return NULL;
@@ -182,9 +182,9 @@ struct machine *machine__new_live(bool kernel_maps, pid_t pid)
        return machine;
 }
 
-struct machine *machine__new_kallsyms(void)
+struct machine *machine__new_kallsyms(struct perf_env *host_env)
 {
-       struct machine *machine = machine__new_host();
+       struct machine *machine = machine__new_host(host_env);
        /*
         * FIXME:
         * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
index 180b369c366c2711ff8968351429cd6867ec3452..22a42c5825fabdb7c5eb85d3247d56a176b30b2e 100644 (file)
@@ -169,9 +169,9 @@ struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid);
 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
 void machines__set_comm_exec(struct machines *machines, bool comm_exec);
 
-struct machine *machine__new_host(void);
-struct machine *machine__new_kallsyms(void);
-struct machine *machine__new_live(bool kernel_maps, pid_t pid);
+struct machine *machine__new_host(struct perf_env *host_env);
+struct machine *machine__new_kallsyms(struct perf_env *host_env);
+struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid);
 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
 void machine__exit(struct machine *machine);
 void machine__delete_threads(struct machine *machine);
index 57ad150f8c4369c062e83bd5cb9d32a498dd8f39..6ab2eb551b6cdf1717bf39a3efb188611cab1c27 100644 (file)
@@ -75,12 +75,14 @@ int e_snprintf(char *str, size_t size, const char *format, ...)
 }
 
 static struct machine *host_machine;
+static struct perf_env host_env;
 
 /* Initialize symbol maps and path of vmlinux/modules */
 int init_probe_symbol_maps(bool user_only)
 {
        int ret;
 
+       perf_env__init(&host_env);
        symbol_conf.allow_aliases = true;
        ret = symbol__init(NULL);
        if (ret < 0) {
@@ -94,7 +96,7 @@ int init_probe_symbol_maps(bool user_only)
        if (symbol_conf.vmlinux_name)
                pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
 
-       host_machine = machine__new_host();
+       host_machine = machine__new_host(&host_env);
        if (!host_machine) {
                pr_debug("machine__new_host() failed.\n");
                symbol__exit();
@@ -111,6 +113,7 @@ void exit_probe_symbol_maps(void)
        machine__delete(host_machine);
        host_machine = NULL;
        symbol__exit();
+       perf_env__exit(&host_env);
 }
 
 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)