Skip to content

Commit aaa2357

Browse files
captain5050namhyung
authored andcommitted
perf top: Make perf_env locally scoped
The use of the global host perf_env variable is potentially inconsistent within the code. Switch perf top to using a locally scoped variable that is generally accessed through the session. Signed-off-by: Ian Rogers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Namhyung Kim <[email protected]>
1 parent 740f7ba commit aaa2357

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

tools/perf/builtin-top.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ static int __cmd_top(struct perf_top *top)
13011301
perf_set_multithreaded();
13021302

13031303
if (perf_hpp_list.socket) {
1304-
ret = perf_env__read_cpu_topology_map(&perf_env);
1304+
ret = perf_env__read_cpu_topology_map(perf_session__env(top->session));
13051305
if (ret < 0) {
13061306
char errbuf[BUFSIZ];
13071307
const char *err = str_error_r(-ret, errbuf, sizeof(errbuf));
@@ -1624,6 +1624,7 @@ int cmd_top(int argc, const char **argv)
16241624
NULL
16251625
};
16261626
int status = hists__init();
1627+
struct perf_env host_env;
16271628

16281629
if (status < 0)
16291630
return status;
@@ -1637,14 +1638,19 @@ int cmd_top(int argc, const char **argv)
16371638
if (top.evlist == NULL)
16381639
return -ENOMEM;
16391640

1641+
perf_env__init(&host_env);
16401642
status = perf_config(perf_top_config, &top);
16411643
if (status)
1642-
return status;
1644+
goto out_delete_evlist;
16431645
/*
16441646
* Since the per arch annotation init routine may need the cpuid, read
16451647
* it here, since we are not getting this from the perf.data header.
16461648
*/
1647-
status = perf_env__read_cpuid(&perf_env);
1649+
status = perf_env__set_cmdline(&host_env, argc, argv);
1650+
if (status)
1651+
goto out_delete_evlist;
1652+
1653+
status = perf_env__read_cpuid(&host_env);
16481654
if (status) {
16491655
/*
16501656
* Some arches do not provide a get_cpuid(), so just use pr_debug, otherwise
@@ -1661,18 +1667,24 @@ int cmd_top(int argc, const char **argv)
16611667

16621668
if (disassembler_style) {
16631669
annotate_opts.disassembler_style = strdup(disassembler_style);
1664-
if (!annotate_opts.disassembler_style)
1665-
return -ENOMEM;
1670+
if (!annotate_opts.disassembler_style) {
1671+
status = -ENOMEM;
1672+
goto out_delete_evlist;
1673+
}
16661674
}
16671675
if (objdump_path) {
16681676
annotate_opts.objdump_path = strdup(objdump_path);
1669-
if (!annotate_opts.objdump_path)
1670-
return -ENOMEM;
1677+
if (!annotate_opts.objdump_path) {
1678+
status = -ENOMEM;
1679+
goto out_delete_evlist;
1680+
}
16711681
}
16721682
if (addr2line_path) {
16731683
symbol_conf.addr2line_path = strdup(addr2line_path);
1674-
if (!symbol_conf.addr2line_path)
1675-
return -ENOMEM;
1684+
if (!symbol_conf.addr2line_path) {
1685+
status = -ENOMEM;
1686+
goto out_delete_evlist;
1687+
}
16761688
}
16771689

16781690
status = symbol__validate_sym_arguments();
@@ -1735,7 +1747,7 @@ int cmd_top(int argc, const char **argv)
17351747
symbol_conf.show_branchflag_count = true;
17361748

17371749
if (opts->branch_stack) {
1738-
status = perf_env__read_core_pmu_caps(&perf_env);
1750+
status = perf_env__read_core_pmu_caps(&host_env);
17391751
if (status) {
17401752
pr_err("PMU capability data is not available\n");
17411753
goto out_delete_evlist;
@@ -1829,14 +1841,16 @@ int cmd_top(int argc, const char **argv)
18291841
perf_top__update_print_entries(&top);
18301842
signal(SIGWINCH, winch_sig);
18311843
}
1832-
top.session->env = &perf_env;
18331844

1834-
top.session = perf_session__new(NULL, NULL);
1845+
top.session = __perf_session__new(/*data=*/NULL, /*tool=*/NULL,
1846+
/*trace_event_repipe=*/false,
1847+
&host_env);
18351848
if (IS_ERR(top.session)) {
18361849
status = PTR_ERR(top.session);
18371850
top.session = NULL;
18381851
goto out_delete_evlist;
18391852
}
1853+
top.evlist->session = top.session;
18401854

18411855
if (!evlist__needs_bpf_sb_event(top.evlist))
18421856
top.record_opts.no_bpf_event = true;
@@ -1851,7 +1865,7 @@ int cmd_top(int argc, const char **argv)
18511865
goto out_delete_evlist;
18521866
}
18531867

1854-
if (evlist__add_bpf_sb_event(top.sb_evlist, &perf_env)) {
1868+
if (evlist__add_bpf_sb_event(top.sb_evlist, &host_env)) {
18551869
pr_err("Couldn't ask for PERF_RECORD_BPF_EVENT side band events.\n.");
18561870
status = -EINVAL;
18571871
goto out_delete_evlist;
@@ -1873,6 +1887,7 @@ int cmd_top(int argc, const char **argv)
18731887
evlist__delete(top.evlist);
18741888
perf_session__delete(top.session);
18751889
annotation_options__exit();
1890+
perf_env__exit(&host_env);
18761891

18771892
return status;
18781893
}

0 commit comments

Comments
 (0)