Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf machine: Explicitly pass in host perf_env

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>

authored by

Ian Rogers and committed by
Namhyung Kim
e4810663 aa91baa0

+81 -35
+4 -1
tools/perf/builtin-buildid-list.c
··· 45 45 46 46 static void buildid__show_kernel_maps(void) 47 47 { 48 + struct perf_env host_env; 48 49 struct machine *machine; 49 50 50 - machine = machine__new_host(); 51 + perf_env__init(&host_env); 52 + machine = machine__new_host(&host_env); 51 53 machine__for_each_kernel_map(machine, buildid__map_cb, NULL); 52 54 machine__delete(machine); 55 + perf_env__exit(&host_env); 53 56 } 54 57 55 58 static int sysfs__fprintf_build_id(FILE *fp)
+16 -5
tools/perf/builtin-kallsyms.c
··· 12 12 #include <subcmd/parse-options.h> 13 13 #include "debug.h" 14 14 #include "dso.h" 15 + #include "env.h" 15 16 #include "machine.h" 16 17 #include "map.h" 17 18 #include "symbol.h" 18 19 19 20 static int __cmd_kallsyms(int argc, const char **argv) 20 21 { 21 - int i; 22 - struct machine *machine = machine__new_kallsyms(); 22 + int i, err; 23 + struct perf_env host_env; 24 + struct machine *machine = NULL; 23 25 26 + 27 + perf_env__init(&host_env); 28 + err = perf_env__set_cmdline(&host_env, argc, argv); 29 + if (err) 30 + goto out; 31 + 32 + machine = machine__new_kallsyms(&host_env); 24 33 if (machine == NULL) { 25 34 pr_err("Couldn't read /proc/kallsyms\n"); 26 - return -1; 35 + err = -1; 36 + goto out; 27 37 } 28 38 29 39 for (i = 0; i < argc; ++i) { ··· 52 42 map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end), 53 43 symbol->start, symbol->end); 54 44 } 55 - 45 + out: 56 46 machine__delete(machine); 57 - return 0; 47 + perf_env__exit(&host_env); 48 + return err; 58 49 } 59 50 60 51 int cmd_kallsyms(int argc, const char **argv)
+17 -7
tools/perf/builtin-trace.c
··· 140 140 }; 141 141 142 142 struct trace { 143 + struct perf_env host_env; 143 144 struct perf_tool tool; 144 145 struct { 145 146 /** Sorted sycall numbers used by the trace. */ ··· 1978 1977 return machine__resolve_kernel_addr(vmachine, addrp, modp); 1979 1978 } 1980 1979 1981 - static int trace__symbols_init(struct trace *trace, struct evlist *evlist) 1980 + static int trace__symbols_init(struct trace *trace, int argc, const char **argv, 1981 + struct evlist *evlist) 1982 1982 { 1983 1983 int err = symbol__init(NULL); 1984 1984 1985 1985 if (err) 1986 1986 return err; 1987 1987 1988 - trace->host = machine__new_host(); 1989 - if (trace->host == NULL) 1990 - return -ENOMEM; 1988 + perf_env__init(&trace->host_env); 1989 + err = perf_env__set_cmdline(&trace->host_env, argc, argv); 1990 + if (err) 1991 + goto out; 1991 1992 1993 + trace->host = machine__new_host(&trace->host_env); 1994 + if (trace->host == NULL) { 1995 + err = -ENOMEM; 1996 + goto out; 1997 + } 1992 1998 thread__set_priv_destructor(thread_trace__delete); 1993 1999 1994 2000 err = trace_event__register_resolver(trace->host, trace__machine__resolve_kernel_addr); ··· 2006 1998 evlist->core.threads, trace__tool_process, 2007 1999 true, false, 1); 2008 2000 out: 2009 - if (err) 2001 + if (err) { 2002 + perf_env__exit(&trace->host_env); 2010 2003 symbol__exit(); 2011 - 2004 + } 2012 2005 return err; 2013 2006 } 2014 2007 ··· 2018 2009 machine__exit(trace->host); 2019 2010 trace->host = NULL; 2020 2011 2012 + perf_env__exit(&trace->host_env); 2021 2013 symbol__exit(); 2022 2014 } 2023 2015 ··· 4438 4428 goto out_delete_evlist; 4439 4429 } 4440 4430 4441 - err = trace__symbols_init(trace, evlist); 4431 + err = trace__symbols_init(trace, argc, argv, evlist); 4442 4432 if (err < 0) { 4443 4433 fprintf(trace->output, "Problems initializing symbol libraries!\n"); 4444 4434 goto out_delete_evlist;
+1 -2
tools/perf/tests/code-reading.c
··· 655 655 656 656 pid = getpid(); 657 657 658 - machine = machine__new_host(); 659 658 perf_env__init(&host_env); 660 - machine->env = &host_env; 659 + machine = machine__new_host(&host_env); 661 660 662 661 ret = machine__create_kernel_maps(machine); 663 662 if (ret < 0) {
+1 -2
tools/perf/tests/dlfilter-test.c
··· 353 353 return test_result("Failed to find program symbols", TEST_FAIL); 354 354 355 355 pr_debug("Creating new host machine structure\n"); 356 - td->machine = machine__new_host(); 357 356 perf_env__init(&host_env); 358 - td->machine->env = &host_env; 357 + td->machine = machine__new_host(&host_env); 359 358 360 359 td->fd = creat(td->perf_data_file_name, 0644); 361 360 if (td->fd < 0)
+7 -3
tools/perf/tests/dwarf-unwind.c
··· 7 7 #include <unistd.h> 8 8 #include "tests.h" 9 9 #include "debug.h" 10 + #include "env.h" 10 11 #include "machine.h" 11 12 #include "event.h" 12 13 #include "../util/unwind.h" ··· 181 180 noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused, 182 181 int subtest __maybe_unused) 183 182 { 183 + struct perf_env host_env; 184 184 struct machine *machine; 185 185 struct thread *thread; 186 186 int err = -1; ··· 190 188 callchain_param.record_mode = CALLCHAIN_DWARF; 191 189 dwarf_callchain_users = true; 192 190 193 - machine = machine__new_live(/*kernel_maps=*/true, pid); 191 + perf_env__init(&host_env); 192 + machine = machine__new_live(&host_env, /*kernel_maps=*/true, pid); 194 193 if (!machine) { 195 194 pr_err("Could not get machine\n"); 196 - return -1; 195 + goto out; 197 196 } 198 197 199 198 if (machine__create_kernel_maps(machine)) { 200 199 pr_err("Failed to create kernel maps\n"); 201 - return -1; 200 + goto out; 202 201 } 203 202 204 203 if (verbose > 1) ··· 216 213 217 214 out: 218 215 machine__delete(machine); 216 + perf_env__exit(&host_env); 219 217 return err; 220 218 } 221 219
+5 -1
tools/perf/tests/mmap-thread-lookup.c
··· 8 8 #include <stdlib.h> 9 9 #include <stdio.h> 10 10 #include "debug.h" 11 + #include "env.h" 11 12 #include "event.h" 12 13 #include "tests.h" 13 14 #include "machine.h" ··· 156 155 157 156 static int mmap_events(synth_cb synth) 158 157 { 158 + struct perf_env host_env; 159 159 struct machine *machine; 160 160 int err, i; 161 161 ··· 169 167 */ 170 168 TEST_ASSERT_VAL("failed to create threads", !threads_create()); 171 169 172 - machine = machine__new_host(); 170 + perf_env__init(&host_env); 171 + machine = machine__new_host(&host_env); 173 172 174 173 dump_trace = verbose > 1 ? 1 : 0; 175 174 ··· 212 209 } 213 210 214 211 machine__delete(machine); 212 + perf_env__exit(&host_env); 215 213 return err; 216 214 } 217 215
+7 -1
tools/perf/tests/symbols.c
··· 5 5 #include <limits.h> 6 6 #include "debug.h" 7 7 #include "dso.h" 8 + #include "env.h" 8 9 #include "machine.h" 9 10 #include "thread.h" 10 11 #include "symbol.h" ··· 14 13 #include "tests.h" 15 14 16 15 struct test_info { 16 + struct perf_env host_env; 17 17 struct machine *machine; 18 18 struct thread *thread; 19 19 }; 20 20 21 21 static int init_test_info(struct test_info *ti) 22 22 { 23 - ti->machine = machine__new_host(); 23 + perf_env__init(&ti->host_env); 24 + ti->machine = machine__new_host(&ti->host_env); 24 25 if (!ti->machine) { 25 26 pr_debug("machine__new_host() failed!\n"); 27 + perf_env__exit(&ti->host_env); 26 28 return TEST_FAIL; 27 29 } 28 30 ··· 33 29 ti->thread = machine__findnew_thread(ti->machine, 100, 100); 34 30 if (!ti->thread) { 35 31 pr_debug("machine__findnew_thread() failed!\n"); 32 + perf_env__exit(&ti->host_env); 36 33 return TEST_FAIL; 37 34 } 38 35 ··· 44 39 { 45 40 thread__put(ti->thread); 46 41 machine__delete(ti->machine); 42 + perf_env__exit(&ti->host_env); 47 43 } 48 44 49 45 struct dso_map {
+8 -1
tools/perf/util/debug.c
··· 17 17 #include "addr_location.h" 18 18 #include "color.h" 19 19 #include "debug.h" 20 + #include "env.h" 20 21 #include "event.h" 21 22 #include "machine.h" 22 23 #include "map.h" ··· 310 309 { 311 310 /* TODO: async safety. printf, malloc, etc. aren't safe inside a signal handler. */ 312 311 pid_t pid = getpid(); 313 - struct machine *machine = machine__new_live(/*kernel_maps=*/false, pid); 312 + struct machine *machine; 314 313 struct thread *thread = NULL; 314 + struct perf_env host_env; 315 + 316 + perf_env__init(&host_env); 317 + machine = machine__new_live(&host_env, /*kernel_maps=*/false, pid); 315 318 316 319 if (machine) 317 320 thread = machine__find_thread(machine, pid, pid); ··· 328 323 */ 329 324 backtrace_symbols_fd(stackdump, stackdump_size, fileno(file)); 330 325 machine__delete(machine); 326 + perf_env__exit(&host_env); 331 327 return; 332 328 } 333 329 #endif ··· 355 349 } 356 350 thread__put(thread); 357 351 machine__delete(machine); 352 + perf_env__exit(&host_env); 358 353 } 359 354 360 355 /* Obtain a backtrace and print it to stdout. */
+8 -8
tools/perf/util/machine.c
··· 129 129 return 0; 130 130 } 131 131 132 - static struct machine *__machine__new_host(bool kernel_maps) 132 + static struct machine *__machine__new_host(struct perf_env *host_env, bool kernel_maps) 133 133 { 134 134 struct machine *machine = malloc(sizeof(*machine)); 135 135 ··· 142 142 free(machine); 143 143 return NULL; 144 144 } 145 - machine->env = &perf_env; 145 + machine->env = host_env; 146 146 return machine; 147 147 } 148 148 149 - struct machine *machine__new_host(void) 149 + struct machine *machine__new_host(struct perf_env *host_env) 150 150 { 151 - return __machine__new_host(/*kernel_maps=*/true); 151 + return __machine__new_host(host_env, /*kernel_maps=*/true); 152 152 } 153 153 154 154 static int mmap_handler(const struct perf_tool *tool __maybe_unused, ··· 168 168 mmap_handler, machine, true); 169 169 } 170 170 171 - struct machine *machine__new_live(bool kernel_maps, pid_t pid) 171 + struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid) 172 172 { 173 - struct machine *machine = __machine__new_host(kernel_maps); 173 + struct machine *machine = __machine__new_host(host_env, kernel_maps); 174 174 175 175 if (!machine) 176 176 return NULL; ··· 182 182 return machine; 183 183 } 184 184 185 - struct machine *machine__new_kallsyms(void) 185 + struct machine *machine__new_kallsyms(struct perf_env *host_env) 186 186 { 187 - struct machine *machine = machine__new_host(); 187 + struct machine *machine = machine__new_host(host_env); 188 188 /* 189 189 * FIXME: 190 190 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
+3 -3
tools/perf/util/machine.h
··· 169 169 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size); 170 170 void machines__set_comm_exec(struct machines *machines, bool comm_exec); 171 171 172 - struct machine *machine__new_host(void); 173 - struct machine *machine__new_kallsyms(void); 174 - struct machine *machine__new_live(bool kernel_maps, pid_t pid); 172 + struct machine *machine__new_host(struct perf_env *host_env); 173 + struct machine *machine__new_kallsyms(struct perf_env *host_env); 174 + struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid); 175 175 int machine__init(struct machine *machine, const char *root_dir, pid_t pid); 176 176 void machine__exit(struct machine *machine); 177 177 void machine__delete_threads(struct machine *machine);
+4 -1
tools/perf/util/probe-event.c
··· 75 75 } 76 76 77 77 static struct machine *host_machine; 78 + static struct perf_env host_env; 78 79 79 80 /* Initialize symbol maps and path of vmlinux/modules */ 80 81 int init_probe_symbol_maps(bool user_only) 81 82 { 82 83 int ret; 83 84 85 + perf_env__init(&host_env); 84 86 symbol_conf.allow_aliases = true; 85 87 ret = symbol__init(NULL); 86 88 if (ret < 0) { ··· 96 94 if (symbol_conf.vmlinux_name) 97 95 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 98 96 99 - host_machine = machine__new_host(); 97 + host_machine = machine__new_host(&host_env); 100 98 if (!host_machine) { 101 99 pr_debug("machine__new_host() failed.\n"); 102 100 symbol__exit(); ··· 113 111 machine__delete(host_machine); 114 112 host_machine = NULL; 115 113 symbol__exit(); 114 + perf_env__exit(&host_env); 116 115 } 117 116 118 117 static struct ref_reloc_sym *kernel_get_ref_reloc_sym(struct map **pmap)