Skip to content

Commit e481066

Browse files
captain5050namhyung
authored andcommitted
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent aa91baa commit e481066

File tree

12 files changed

+81
-35
lines changed

12 files changed

+81
-35
lines changed

tools/perf/builtin-buildid-list.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
4545

4646
static void buildid__show_kernel_maps(void)
4747
{
48+
struct perf_env host_env;
4849
struct machine *machine;
4950

50-
machine = machine__new_host();
51+
perf_env__init(&host_env);
52+
machine = machine__new_host(&host_env);
5153
machine__for_each_kernel_map(machine, buildid__map_cb, NULL);
5254
machine__delete(machine);
55+
perf_env__exit(&host_env);
5356
}
5457

5558
static int sysfs__fprintf_build_id(FILE *fp)

tools/perf/builtin-kallsyms.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,28 @@
1212
#include <subcmd/parse-options.h>
1313
#include "debug.h"
1414
#include "dso.h"
15+
#include "env.h"
1516
#include "machine.h"
1617
#include "map.h"
1718
#include "symbol.h"
1819

1920
static int __cmd_kallsyms(int argc, const char **argv)
2021
{
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;
2325

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);
2433
if (machine == NULL) {
2534
pr_err("Couldn't read /proc/kallsyms\n");
26-
return -1;
35+
err = -1;
36+
goto out;
2737
}
2838

2939
for (i = 0; i < argc; ++i) {
@@ -42,9 +52,10 @@ static int __cmd_kallsyms(int argc, const char **argv)
4252
map__unmap_ip(map, symbol->start), map__unmap_ip(map, symbol->end),
4353
symbol->start, symbol->end);
4454
}
45-
55+
out:
4656
machine__delete(machine);
47-
return 0;
57+
perf_env__exit(&host_env);
58+
return err;
4859
}
4960

5061
int cmd_kallsyms(int argc, const char **argv)

tools/perf/builtin-trace.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ struct syscall_fmt {
140140
};
141141

142142
struct trace {
143+
struct perf_env host_env;
143144
struct perf_tool tool;
144145
struct {
145146
/** Sorted sycall numbers used by the trace. */
@@ -1977,17 +1978,24 @@ static char *trace__machine__resolve_kernel_addr(void *vmachine, unsigned long l
19771978
return machine__resolve_kernel_addr(vmachine, addrp, modp);
19781979
}
19791980

1980-
static int trace__symbols_init(struct trace *trace, struct evlist *evlist)
1981+
static int trace__symbols_init(struct trace *trace, int argc, const char **argv,
1982+
struct evlist *evlist)
19811983
{
19821984
int err = symbol__init(NULL);
19831985

19841986
if (err)
19851987
return err;
19861988

1987-
trace->host = machine__new_host();
1988-
if (trace->host == NULL)
1989-
return -ENOMEM;
1989+
perf_env__init(&trace->host_env);
1990+
err = perf_env__set_cmdline(&trace->host_env, argc, argv);
1991+
if (err)
1992+
goto out;
19901993

1994+
trace->host = machine__new_host(&trace->host_env);
1995+
if (trace->host == NULL) {
1996+
err = -ENOMEM;
1997+
goto out;
1998+
}
19911999
thread__set_priv_destructor(thread_trace__delete);
19922000

19932001
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)
19982006
evlist->core.threads, trace__tool_process,
19992007
true, false, 1);
20002008
out:
2001-
if (err)
2009+
if (err) {
2010+
perf_env__exit(&trace->host_env);
20022011
symbol__exit();
2003-
2012+
}
20042013
return err;
20052014
}
20062015

@@ -2009,6 +2018,7 @@ static void trace__symbols__exit(struct trace *trace)
20092018
machine__exit(trace->host);
20102019
trace->host = NULL;
20112020

2021+
perf_env__exit(&trace->host_env);
20122022
symbol__exit();
20132023
}
20142024

@@ -4428,7 +4438,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
44284438
goto out_delete_evlist;
44294439
}
44304440

4431-
err = trace__symbols_init(trace, evlist);
4441+
err = trace__symbols_init(trace, argc, argv, evlist);
44324442
if (err < 0) {
44334443
fprintf(trace->output, "Problems initializing symbol libraries!\n");
44344444
goto out_delete_evlist;

tools/perf/tests/code-reading.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,8 @@ static int do_test_code_reading(bool try_kcore)
655655

656656
pid = getpid();
657657

658-
machine = machine__new_host();
659658
perf_env__init(&host_env);
660-
machine->env = &host_env;
659+
machine = machine__new_host(&host_env);
661660

662661
ret = machine__create_kernel_maps(machine);
663662
if (ret < 0) {

tools/perf/tests/dlfilter-test.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ static int test__dlfilter_test(struct test_data *td)
353353
return test_result("Failed to find program symbols", TEST_FAIL);
354354

355355
pr_debug("Creating new host machine structure\n");
356-
td->machine = machine__new_host();
357356
perf_env__init(&host_env);
358-
td->machine->env = &host_env;
357+
td->machine = machine__new_host(&host_env);
359358

360359
td->fd = creat(td->perf_data_file_name, 0644);
361360
if (td->fd < 0)

tools/perf/tests/dwarf-unwind.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <unistd.h>
88
#include "tests.h"
99
#include "debug.h"
10+
#include "env.h"
1011
#include "machine.h"
1112
#include "event.h"
1213
#include "../util/unwind.h"
@@ -180,6 +181,7 @@ NO_TAIL_CALL_ATTRIBUTE noinline int test_dwarf_unwind__krava_1(struct thread *th
180181
noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
181182
int subtest __maybe_unused)
182183
{
184+
struct perf_env host_env;
183185
struct machine *machine;
184186
struct thread *thread;
185187
int err = -1;
@@ -188,15 +190,16 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
188190
callchain_param.record_mode = CALLCHAIN_DWARF;
189191
dwarf_callchain_users = true;
190192

191-
machine = machine__new_live(/*kernel_maps=*/true, pid);
193+
perf_env__init(&host_env);
194+
machine = machine__new_live(&host_env, /*kernel_maps=*/true, pid);
192195
if (!machine) {
193196
pr_err("Could not get machine\n");
194-
return -1;
197+
goto out;
195198
}
196199

197200
if (machine__create_kernel_maps(machine)) {
198201
pr_err("Failed to create kernel maps\n");
199-
return -1;
202+
goto out;
200203
}
201204

202205
if (verbose > 1)
@@ -213,6 +216,7 @@ noinline int test__dwarf_unwind(struct test_suite *test __maybe_unused,
213216

214217
out:
215218
machine__delete(machine);
219+
perf_env__exit(&host_env);
216220
return err;
217221
}
218222

tools/perf/tests/mmap-thread-lookup.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <stdlib.h>
99
#include <stdio.h>
1010
#include "debug.h"
11+
#include "env.h"
1112
#include "event.h"
1213
#include "tests.h"
1314
#include "machine.h"
@@ -155,6 +156,7 @@ static int synth_process(struct machine *machine)
155156

156157
static int mmap_events(synth_cb synth)
157158
{
159+
struct perf_env host_env;
158160
struct machine *machine;
159161
int err, i;
160162

@@ -167,7 +169,8 @@ static int mmap_events(synth_cb synth)
167169
*/
168170
TEST_ASSERT_VAL("failed to create threads", !threads_create());
169171

170-
machine = machine__new_host();
172+
perf_env__init(&host_env);
173+
machine = machine__new_host(&host_env);
171174

172175
dump_trace = verbose > 1 ? 1 : 0;
173176

@@ -209,6 +212,7 @@ static int mmap_events(synth_cb synth)
209212
}
210213

211214
machine__delete(machine);
215+
perf_env__exit(&host_env);
212216
return err;
213217
}
214218

tools/perf/tests/symbols.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <limits.h>
66
#include "debug.h"
77
#include "dso.h"
8+
#include "env.h"
89
#include "machine.h"
910
#include "thread.h"
1011
#include "symbol.h"
@@ -13,22 +14,26 @@
1314
#include "tests.h"
1415

1516
struct test_info {
17+
struct perf_env host_env;
1618
struct machine *machine;
1719
struct thread *thread;
1820
};
1921

2022
static int init_test_info(struct test_info *ti)
2123
{
22-
ti->machine = machine__new_host();
24+
perf_env__init(&ti->host_env);
25+
ti->machine = machine__new_host(&ti->host_env);
2326
if (!ti->machine) {
2427
pr_debug("machine__new_host() failed!\n");
28+
perf_env__exit(&ti->host_env);
2529
return TEST_FAIL;
2630
}
2731

2832
/* Create a dummy thread */
2933
ti->thread = machine__findnew_thread(ti->machine, 100, 100);
3034
if (!ti->thread) {
3135
pr_debug("machine__findnew_thread() failed!\n");
36+
perf_env__exit(&ti->host_env);
3237
return TEST_FAIL;
3338
}
3439

@@ -39,6 +44,7 @@ static void exit_test_info(struct test_info *ti)
3944
{
4045
thread__put(ti->thread);
4146
machine__delete(ti->machine);
47+
perf_env__exit(&ti->host_env);
4248
}
4349

4450
struct dso_map {

tools/perf/util/debug.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "addr_location.h"
1818
#include "color.h"
1919
#include "debug.h"
20+
#include "env.h"
2021
#include "event.h"
2122
#include "machine.h"
2223
#include "map.h"
@@ -309,8 +310,12 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
309310
{
310311
/* TODO: async safety. printf, malloc, etc. aren't safe inside a signal handler. */
311312
pid_t pid = getpid();
312-
struct machine *machine = machine__new_live(/*kernel_maps=*/false, pid);
313+
struct machine *machine;
313314
struct thread *thread = NULL;
315+
struct perf_env host_env;
316+
317+
perf_env__init(&host_env);
318+
machine = machine__new_live(&host_env, /*kernel_maps=*/false, pid);
314319

315320
if (machine)
316321
thread = machine__find_thread(machine, pid, pid);
@@ -323,6 +328,7 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
323328
*/
324329
backtrace_symbols_fd(stackdump, stackdump_size, fileno(file));
325330
machine__delete(machine);
331+
perf_env__exit(&host_env);
326332
return;
327333
}
328334
#endif
@@ -349,6 +355,7 @@ void __dump_stack(FILE *file, void **stackdump, size_t stackdump_size)
349355
}
350356
thread__put(thread);
351357
machine__delete(machine);
358+
perf_env__exit(&host_env);
352359
}
353360

354361
/* Obtain a backtrace and print it to stdout. */

tools/perf/util/machine.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
129129
return 0;
130130
}
131131

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)
133133
{
134134
struct machine *machine = malloc(sizeof(*machine));
135135

@@ -142,13 +142,13 @@ static struct machine *__machine__new_host(bool kernel_maps)
142142
free(machine);
143143
return NULL;
144144
}
145-
machine->env = &perf_env;
145+
machine->env = host_env;
146146
return machine;
147147
}
148148

149-
struct machine *machine__new_host(void)
149+
struct machine *machine__new_host(struct perf_env *host_env)
150150
{
151-
return __machine__new_host(/*kernel_maps=*/true);
151+
return __machine__new_host(host_env, /*kernel_maps=*/true);
152152
}
153153

154154
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)
168168
mmap_handler, machine, true);
169169
}
170170

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)
172172
{
173-
struct machine *machine = __machine__new_host(kernel_maps);
173+
struct machine *machine = __machine__new_host(host_env, kernel_maps);
174174

175175
if (!machine)
176176
return NULL;
@@ -182,9 +182,9 @@ struct machine *machine__new_live(bool kernel_maps, pid_t pid)
182182
return machine;
183183
}
184184

185-
struct machine *machine__new_kallsyms(void)
185+
struct machine *machine__new_kallsyms(struct perf_env *host_env)
186186
{
187-
struct machine *machine = machine__new_host();
187+
struct machine *machine = machine__new_host(host_env);
188188
/*
189189
* FIXME:
190190
* 1) We should switch to machine__load_kallsyms(), i.e. not explicitly

0 commit comments

Comments
 (0)