From nobody Sun Feb 8 21:28:00 2026 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E80F1369997 for ; Tue, 3 Feb 2026 18:27:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770143244; cv=none; b=mKSUP4d3YYG5JrHiLZZVWN++MNUeGT3WS/qOiSnsI2BKwfEEa4nr2w+0Y5JSl7c03u0jNMvuHtb98tgnKFbzoEEO63tOFMS61duDQSmgPLpehW9f9NvGJz3wEEJdy5PG+HPu2nAKvXtrD7cbty/dUIcEN4cU8PVyqTlHCaCOPLs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770143244; c=relaxed/simple; bh=sXKSOtLcPaTeWkO/mEs3KygOYQnKMmsW10C7VrPm5yA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OQzz2jy8P1uKa7Vgb4/H/Z76nv2gj0flb5zvmZBUugGdoj69P0NDiBcACPKzATsHRx/+sD+T9r7k8WYTScgECF06C/hFkz4o5856bkkYQt9j/lB53DbEYYB42x1qjScYMwuJrYyvhx8Hx81pYWSt7SERY9btq8HUfJ9M6DD5wec= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ED/6hEJu; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ED/6hEJu" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-2b7eddde359so3667564eec.0 for ; Tue, 03 Feb 2026 10:27:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770143242; x=1770748042; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yIM5+Of9W90WeHpl1KAdtOvtvkuL79tdVDHtpDNPDbc=; b=ED/6hEJuEHnwAhQWVkfVKOBB++5rnKW28S3EC1D/2R8wG8ri177jK6sHT2JVP/lgEO Ca//cpXler1yWifU1ofZlqq5No7FoOT85oxGJC42e8wkssn0cwGquGup1+jeljld3prV 3de+FoMdL1rqhRwSacJFwxhU2opZfbJBFdZAd6RhekmyH0NAHqnr4e/Vc6T/qlUxpec4 1sjzkpfsKMjLi1LOL9hfT6itf67aYA13GA4sCsf6eRcWnzMhJBuGqhBhxqyA4Ke41cyl R8i8p6XNhbwx4NQ42F+7UpEEJs9VtKN808SoLbrnUcAts+PGa0zwycGi996JOIayekhw /iYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770143242; x=1770748042; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yIM5+Of9W90WeHpl1KAdtOvtvkuL79tdVDHtpDNPDbc=; b=ZJAGqpafyghOdosc30/pIAhLOezok/VNVH3LqJqEhy2cP1veWZKEFW8wibPukkq36G yZ2lzoaamXwigthVrFGPuJyc2jaw6JZyn1buNLxW9TFh/rdJebB8dbb6lHbuSX1r0Hip RFsuNsvAnCNZfUpWNx2rLwRMw1SsPeCB1iOSmOF+74Nr13l8F1jCaM1LXFxF4nqMmlG3 KkbQd+UgTF6IfbtrBAy44J4ig0/7As9jnLHObpAH1x0vyda31n1jYTxVACTHUZNKTn01 WHyB5iBCC+9zrbKLqDFDu8jy/pdz2fYJryqF/nOj/Z13PT1V2JvZeMKvwVQwWtY1lIUH xa0w== X-Forwarded-Encrypted: i=1; AJvYcCUCtuvhl1Y27vgQxMn7PrCGYzmXnvGOgAa95oY8uEOL1MTx0SgEY1+WnEm/mvaviZWSmL5R6K4ygUl8xoM=@vger.kernel.org X-Gm-Message-State: AOJu0YxYMOhcgDZ/yPOSLpeyNhdOEQ5iI2qUVb9XV6D3a86YGsuz/icX KnyOJtlOtFxx6vgzdYW9s1XZlo6Apz9x1EmSS/kIl+/3h9QFfG8Y5wWDPgmmdcPE63L6atxEYOr WACSLQXQgSg== X-Received: from dyaw27-n1.prod.google.com ([2002:a05:693c:415b:10b0:2b0:5041:8a8b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:7bc2:b0:2b7:18eb:a0b9 with SMTP id 5a478bee46e88-2b8329b3935mr163770eec.43.1770143242111; Tue, 03 Feb 2026 10:27:22 -0800 (PST) Date: Tue, 3 Feb 2026 10:26:39 -0800 In-Reply-To: <20260203182640.3911987-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260203182640.3911987-1-irogers@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260203182640.3911987-5-irogers@google.com> Subject: [PATCH v3 4/5] perf header: Add e_machine/e_flags to the header From: Ian Rogers To: acme@kernel.org Cc: aditya.b1@linux.ibm.com, adrian.hunter@intel.com, ajones@ventanamicro.com, ak@linux.intel.com, alex@ghiti.fr, alexander.shishkin@linux.intel.com, anup@brainfault.org, aou@eecs.berkeley.edu, ashelat@redhat.com, atrajeev@linux.ibm.com, blakejones@google.com, ctshao@google.com, dapeng1.mi@linux.intel.com, dvyukov@google.com, howardchu95@gmail.com, irogers@google.com, james.clark@linaro.org, john.g.garry@oracle.com, jolsa@kernel.org, leo.yan@linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, mingo@redhat.com, namhyung@kernel.org, palmer@dabbelt.com, peterz@infradead.org, pjw@kernel.org, shimin.guo@skydio.com, swapnil.sapkal@amd.com, thomas.falcon@intel.com, will@kernel.org, ysk@kzalloc.com, zhouquan@iscas.ac.cn Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add 64-bits of feature data to record the ELF machine and flags. This allows readers to initialize based on the data. For example, `perf kvm stat` wants to initialize based on the kind of data to be read, but at initialization time there are no threads to base this data upon and using the host means cross platform support won't work. The values in the perf_env also act as a cache for these within the session. Signed-off-by: Ian Rogers --- tools/perf/util/env.h | 3 +++ tools/perf/util/header.c | 33 +++++++++++++++++++++++++++++++++ tools/perf/util/header.h | 1 + tools/perf/util/session.c | 33 +++++++++++++++++++++++++++------ 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index 76ba1a36e9ff..a4501cbca375 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -74,6 +74,9 @@ struct perf_env { char *os_release; char *version; char *arch; + /* e_machine expanded from 16 to 32-bits for alignment. */ + u32 e_machine; + u32 e_flags; int nr_cpus_online; int nr_cpus_avail; char *cpu_desc; diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index 31c3bab1b10a..9142a8ba4019 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -379,6 +379,21 @@ static int write_arch(struct feat_fd *ff, return do_write_string(ff, uts.machine); } =20 +static int write_e_machine(struct feat_fd *ff, + struct evlist *evlist __maybe_unused) +{ + /* e_machine expanded from 16 to 32-bits for alignment. */ + uint32_t e_flags; + uint32_t e_machine =3D perf_session__e_machine(evlist->session, &e_flags); + int ret; + + ret =3D do_write(ff, &e_machine, sizeof(e_machine)); + if (ret) + return ret; + + return do_write(ff, &e_flags, sizeof(e_flags)); +} + static int write_version(struct feat_fd *ff, struct evlist *evlist __maybe_unused) { @@ -1785,6 +1800,12 @@ static void print_arch(struct feat_fd *ff, FILE *fp) fprintf(fp, "# arch : %s\n", ff->ph->env.arch); } =20 +static void print_e_machine(struct feat_fd *ff, FILE *fp) +{ + fprintf(fp, "# e_machine : %u\n", ff->ph->env.e_machine); + fprintf(fp, "# e_flags : %u\n", ff->ph->env.e_flags); +} + static void print_cpudesc(struct feat_fd *ff, FILE *fp) { fprintf(fp, "# cpudesc : %s\n", ff->ph->env.cpu_desc); @@ -2612,6 +2633,17 @@ FEAT_PROCESS_STR_FUN(arch, arch); FEAT_PROCESS_STR_FUN(cpudesc, cpu_desc); FEAT_PROCESS_STR_FUN(cpuid, cpuid); =20 +static int process_e_machine(struct feat_fd *ff, void *data __maybe_unused) +{ + int ret; + + ret =3D do_read_u32(ff, &ff->ph->env.e_machine); + if (ret) + return ret; + + return do_read_u32(ff, &ff->ph->env.e_flags); +} + #ifdef HAVE_LIBTRACEEVENT static int process_tracing_data(struct feat_fd *ff, void *data) { @@ -3730,6 +3762,7 @@ const struct perf_header_feature_ops feat_ops[HEADER_= LAST_FEATURE] =3D { FEAT_OPN(HYBRID_TOPOLOGY, hybrid_topology, true), FEAT_OPR(PMU_CAPS, pmu_caps, false), FEAT_OPR(CPU_DOMAIN_INFO, cpu_domain_info, true), + FEAT_OPR(E_MACHINE, e_machine, false), }; =20 struct header_print_data { diff --git a/tools/perf/util/header.h b/tools/perf/util/header.h index 36cc74e2d14d..cc40ac796f52 100644 --- a/tools/perf/util/header.h +++ b/tools/perf/util/header.h @@ -54,6 +54,7 @@ enum { HEADER_HYBRID_TOPOLOGY, HEADER_PMU_CAPS, HEADER_CPU_DOMAIN_INFO, + HEADER_E_MACHINE, HEADER_LAST_FEATURE, HEADER_FEAT_BITS =3D 256, }; diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 72e8bb67d740..53f51c3f9603 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -2967,7 +2967,6 @@ struct perf_env *perf_session__env(struct perf_sessio= n *session) struct perf_session__e_machine_cb_args { uint32_t e_flags; uint16_t e_machine; - bool need_e_flags; }; =20 static int perf_session__e_machine_cb(struct thread *thread, void *_args) @@ -2975,8 +2974,7 @@ static int perf_session__e_machine_cb(struct thread *= thread, void *_args) struct perf_session__e_machine_cb_args *args =3D _args; struct machine *machine =3D maps__machine(thread__maps(thread)); =20 - args->e_machine =3D thread__e_machine(thread, machine, - args->need_e_flags ? &args->e_flags : NULL); + args->e_machine =3D thread__e_machine(thread, machine, &args->e_flags); return args->e_machine !=3D EM_NONE ? 1 : 0; } =20 @@ -2988,8 +2986,8 @@ uint16_t perf_session__e_machine(struct perf_session = *session, uint32_t *e_flags { struct perf_session__e_machine_cb_args args =3D { .e_machine =3D EM_NONE, - .need_e_flags =3D e_flags !=3D NULL, }; + struct perf_env *env; =20 if (!session) { /* Default to assuming a host machine. */ @@ -2999,12 +2997,35 @@ uint16_t perf_session__e_machine(struct perf_sessio= n *session, uint32_t *e_flags return EM_HOST; } =20 + env =3D perf_session__env(session); + if (env && env->e_machine !=3D EM_NONE) { + if (e_flags) + *e_flags =3D env->e_flags; + + return env->e_machine; + } + machines__for_each_thread(&session->machines, perf_session__e_machine_cb, &args); =20 + if (args.e_machine !=3D EM_NONE) { + if (env) { + env->e_machine =3D args.e_machine; + env->e_flags =3D args.e_flags; + } + if (e_flags) + *e_flags =3D args.e_flags; + + return args.e_machine; + } + + /* + * Couldn't determine from the perf_env or current set of + * threads. Default to the host. + */ if (e_flags) - *e_flags =3D args.e_flags; + *e_flags =3D EF_HOST; =20 - return args.e_machine =3D=3D EM_NONE ? EM_HOST : args.e_machine; + return EM_HOST; } --=20 2.53.0.rc2.204.g2597b5adb4-goog