perf evlist: Change env variable to session

The session holds a perf_env pointer env. In UI code container_of is
used to turn the env to a session, but this assumes the session
header's env is in use. Rather than a dubious container_of, hold the
session in the evlist and derive the env from the session with
evsel__env, perf_session__env, etc.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250724163302.596743-11-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers 2025-07-24 09:32:50 -07:00 committed by Namhyung Kim
parent c3e5b9ec96
commit 57ddb9cbb5
17 changed files with 35 additions and 22 deletions

View file

@ -1274,6 +1274,8 @@ static int process_attr(const struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct evlist **pevlist)
{
struct perf_session *session;
struct perf_env *env;
u64 sample_type;
int err;
@ -1286,7 +1288,9 @@ static int process_attr(const struct perf_tool *tool __maybe_unused,
* on events sample_type.
*/
sample_type = evlist__combined_sample_type(*pevlist);
callchain_param_setup(sample_type, perf_env__arch((*pevlist)->env));
session = (*pevlist)->session;
env = perf_session__env(session);
callchain_param_setup(sample_type, perf_env__arch(env));
return 0;
}

View file

@ -2534,7 +2534,7 @@ static int process_attr(const struct perf_tool *tool, union perf_event *event,
* on events sample_type.
*/
sample_type = evlist__combined_sample_type(evlist);
callchain_param_setup(sample_type, perf_env__arch((*pevlist)->env));
callchain_param_setup(sample_type, perf_env__arch(perf_session__env(scr->session)));
/* Enable fields for callchain entries */
if (symbol_conf.use_callchain &&

View file

@ -1654,7 +1654,6 @@ int cmd_top(int argc, const char **argv)
"Couldn't read the cpuid for this machine: %s\n",
str_error_r(errno, errbuf, sizeof(errbuf)));
}
top.evlist->env = &perf_env;
argc = parse_options(argc, argv, options, top_usage, 0);
if (argc)
@ -1830,6 +1829,7 @@ int cmd_top(int argc, const char **argv)
perf_top__update_print_entries(&top);
signal(SIGWINCH, winch_sig);
}
top.session->env = &perf_env;
top.session = perf_session__new(NULL, NULL);
if (IS_ERR(top.session)) {

View file

@ -43,6 +43,7 @@ static int session_write_header(char *path)
session->evlist = evlist__new_default();
TEST_ASSERT_VAL("can't get evlist", session->evlist);
session->evlist->session = session;
perf_header__set_feat(&session->header, HEADER_CPU_TOPOLOGY);
perf_header__set_feat(&session->header, HEADER_NRCPUS);

View file

@ -71,8 +71,8 @@ int ui_browser__help_window(struct ui_browser *browser, const char *text);
bool ui_browser__dialog_yesno(struct ui_browser *browser, const char *text);
int ui_browser__input_window(const char *title, const char *text, char *input,
const char *exit_msg, int delay_sec);
struct perf_env;
int tui__header_window(struct perf_env *env);
struct perf_session;
int tui__header_window(struct perf_session *session);
void ui_browser__argv_seek(struct ui_browser *browser, off_t offset, int whence);
unsigned int ui_browser__argv_refresh(struct ui_browser *browser);

View file

@ -93,16 +93,14 @@ static int ui__list_menu(int argc, char * const argv[])
return list_menu__run(&menu);
}
int tui__header_window(struct perf_env *env)
int tui__header_window(struct perf_session *session)
{
int i, argc = 0;
char **argv;
struct perf_session *session;
char *ptr, *pos;
size_t size;
FILE *fp = open_memstream(&ptr, &size);
session = container_of(env, struct perf_session, header.env);
perf_header__fprintf_info(session, fp, true);
fclose(fp);

View file

@ -3233,7 +3233,7 @@ do_hotkey: // key came straight from options ui__popup_menu()
case 'i':
/* env->arch is NULL for live-mode (i.e. perf top) */
if (env->arch)
tui__header_window(env);
tui__header_window(evsel__session(evsel));
continue;
case 'F':
symbol_conf.filter_relative ^= 1;

View file

@ -354,7 +354,7 @@ static void parse_cpuid(struct perf_env *env)
*/
bool evlist__has_amd_ibs(struct evlist *evlist)
{
struct perf_env *env = evlist->env;
struct perf_env *env = perf_session__env(evlist->session);
int ret, nr_pmu_mappings = perf_env__nr_pmu_mappings(env);
const char *pmu_mapping = perf_env__pmu_mappings(env);
char name[sizeof("ibs_fetch")];

View file

@ -856,7 +856,7 @@ static bool arm_spe__synth_ds(struct arm_spe_queue *speq,
const char *cpuid;
pr_warning_once("Old SPE metadata, re-record to improve decode accuracy\n");
cpuid = perf_env__cpuid(spe->session->evlist->env);
cpuid = perf_env__cpuid(perf_session__env(spe->session));
midr = strtol(cpuid, NULL, 16);
} else {
/* CPU ID is -1 for per-thread mode */

View file

@ -71,7 +71,7 @@ struct evlist {
struct mmap *overwrite_mmap;
struct evsel *selected;
struct events_stats stats;
struct perf_env *env;
struct perf_session *session;
void (*trace_event_sample_raw)(struct evlist *evlist,
union perf_event *event,
struct perf_sample *sample);

View file

@ -48,6 +48,7 @@
#include "record.h"
#include "debug.h"
#include "trace-event.h"
#include "session.h"
#include "stat.h"
#include "string2.h"
#include "memswap.h"
@ -3872,11 +3873,16 @@ int evsel__open_strerror(struct evsel *evsel, struct target *target,
err, str_error_r(err, sbuf, sizeof(sbuf)), evsel__name(evsel));
}
struct perf_session *evsel__session(struct evsel *evsel)
{
return evsel && evsel->evlist ? evsel->evlist->session : NULL;
}
struct perf_env *evsel__env(struct evsel *evsel)
{
if (evsel && evsel->evlist && evsel->evlist->env)
return evsel->evlist->env;
return &perf_env;
struct perf_session *session = evsel__session(evsel);
return session ? perf_session__env(session) : &perf_env;
}
static int store_evsel_ids(struct evsel *evsel, struct evlist *evlist)

View file

@ -542,6 +542,7 @@ static inline bool evsel__is_dummy_event(struct evsel *evsel)
(evsel->core.attr.config == PERF_COUNT_SW_DUMMY);
}
struct perf_session *evsel__session(struct evsel *evsel);
struct perf_env *evsel__env(struct evsel *evsel);
int evsel__store_ids(struct evsel *evsel, struct evlist *evlist);

View file

@ -4225,7 +4225,7 @@ int perf_session__read_header(struct perf_session *session)
if (session->evlist == NULL)
return -ENOMEM;
session->evlist->env = &header->env;
session->evlist->session = session;
session->machines.host.env = &header->env;
/*

View file

@ -1142,7 +1142,7 @@ int s390_cpumsf_process_auxtrace_info(union perf_event *event,
sf->machine = &session->machines.host; /* No kvm support */
sf->auxtrace_type = auxtrace_info->type;
sf->pmu_type = PERF_TYPE_RAW;
sf->machine_type = s390_cpumsf_get_type(session->evlist->env->cpuid);
sf->machine_type = s390_cpumsf_get_type(perf_session__env(session)->cpuid);
sf->auxtrace.process_event = s390_cpumsf_process_event;
sf->auxtrace.process_auxtrace_event = s390_cpumsf_process_auxtrace_event;

View file

@ -6,15 +6,16 @@
#include "env.h"
#include "header.h"
#include "sample-raw.h"
#include "session.h"
/*
* Check platform the perf data file was created on and perform platform
* specific interpretation.
*/
void evlist__init_trace_event_sample_raw(struct evlist *evlist)
void evlist__init_trace_event_sample_raw(struct evlist *evlist, struct perf_env *env)
{
const char *arch_pf = perf_env__arch(evlist->env);
const char *cpuid = perf_env__cpuid(evlist->env);
const char *arch_pf = perf_env__arch(env);
const char *cpuid = perf_env__cpuid(env);
if (arch_pf && !strcmp("s390", arch_pf))
evlist->trace_event_sample_raw = evlist__s390_sample_raw;

View file

@ -11,5 +11,5 @@ void evlist__s390_sample_raw(struct evlist *evlist, union perf_event *event,
bool evlist__has_amd_ibs(struct evlist *evlist);
void evlist__amd_sample_raw(struct evlist *evlist, union perf_event *event,
struct perf_sample *sample);
void evlist__init_trace_event_sample_raw(struct evlist *evlist);
void evlist__init_trace_event_sample_raw(struct evlist *evlist, struct perf_env *env);
#endif /* __PERF_EVLIST_H */

View file

@ -177,7 +177,7 @@ struct perf_session *__perf_session__new(struct perf_data *data,
perf_session__set_comm_exec(session);
}
evlist__init_trace_event_sample_raw(session->evlist);
evlist__init_trace_event_sample_raw(session->evlist, &session->header.env);
/* Open the directory data. */
if (data->is_dir) {
@ -193,6 +193,8 @@ struct perf_session *__perf_session__new(struct perf_data *data,
} else {
session->machines.host.env = &perf_env;
}
if (session->evlist)
session->evlist->session = session;
session->machines.host.single_address_space =
perf_env__single_address_space(session->machines.host.env);