perf session: Add accessor for session->header.env

The perf_env from the header in the session is frequently accessed,
add an accessor function rather than access directly. Cache the value
to avoid repeated calls. No behavioral change.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250724163302.596743-10-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
This commit is contained in:
Ian Rogers
2025-07-24 09:32:49 -07:00
committed by Namhyung Kim
parent 53b00ff358
commit c3e5b9ec96
25 changed files with 120 additions and 107 deletions

View File

@@ -257,7 +257,8 @@ static int process_sample_event(const struct perf_tool *tool,
static void output_headers(struct perf_session *session, struct convert_json *c)
{
struct stat st;
struct perf_header *header = &session->header;
const struct perf_header *header = &session->header;
const struct perf_env *env = perf_session__env(session);
int ret;
int fd = perf_data__fd(session->data);
int i;
@@ -280,32 +281,32 @@ static void output_headers(struct perf_session *session, struct convert_json *c)
output_json_key_format(out, true, 2, "data-size", "%" PRIu64, header->data_size);
output_json_key_format(out, true, 2, "feat-offset", "%" PRIu64, header->feat_offset);
output_json_key_string(out, true, 2, "hostname", header->env.hostname);
output_json_key_string(out, true, 2, "os-release", header->env.os_release);
output_json_key_string(out, true, 2, "arch", header->env.arch);
output_json_key_string(out, true, 2, "hostname", env->hostname);
output_json_key_string(out, true, 2, "os-release", env->os_release);
output_json_key_string(out, true, 2, "arch", env->arch);
if (header->env.cpu_desc)
output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
if (env->cpu_desc)
output_json_key_string(out, true, 2, "cpu-desc", env->cpu_desc);
output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
output_json_key_string(out, true, 2, "cpuid", env->cpuid);
output_json_key_format(out, true, 2, "nrcpus-online", "%u", env->nr_cpus_online);
output_json_key_format(out, true, 2, "nrcpus-avail", "%u", env->nr_cpus_avail);
if (header->env.clock.enabled) {
if (env->clock.enabled) {
output_json_key_format(out, true, 2, "clockid",
"%u", header->env.clock.clockid);
"%u", env->clock.clockid);
output_json_key_format(out, true, 2, "clock-time",
"%" PRIu64, header->env.clock.clockid_ns);
"%" PRIu64, env->clock.clockid_ns);
output_json_key_format(out, true, 2, "real-time",
"%" PRIu64, header->env.clock.tod_ns);
"%" PRIu64, env->clock.tod_ns);
}
output_json_key_string(out, true, 2, "perf-version", header->env.version);
output_json_key_string(out, true, 2, "perf-version", env->version);
output_json_key_format(out, true, 2, "cmdline", "[");
for (i = 0; i < header->env.nr_cmdline; i++) {
for (i = 0; i < env->nr_cmdline; i++) {
output_json_delimiters(out, i != 0, 3);
output_json_string(c->out, header->env.cmdline_argv[i]);
output_json_string(c->out, env->cmdline_argv[i]);
}
output_json_format(out, false, 2, "]");
}
@@ -376,8 +377,7 @@ int bt_convert__perf2json(const char *input_name, const char *output_name,
fprintf(stderr, "Error creating perf session!\n");
goto err_fclose;
}
if (symbol__init(&session->header.env) < 0) {
if (symbol__init(perf_session__env(session)) < 0) {
fprintf(stderr, "Symbol init error!\n");
goto err_session_delete;
}