From nobody Sun Apr 26 12:20:01 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DD96CCA479 for ; Tue, 28 Jun 2022 11:47:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344470AbiF1Lrx (ORCPT ); Tue, 28 Jun 2022 07:47:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345348AbiF1LrZ (ORCPT ); Tue, 28 Jun 2022 07:47:25 -0400 Received: from forward500o.mail.yandex.net (forward500o.mail.yandex.net [37.140.190.195]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E638132055; Tue, 28 Jun 2022 04:46:50 -0700 (PDT) Received: from vla1-d53b4f396d89.qloud-c.yandex.net (vla1-d53b4f396d89.qloud-c.yandex.net [IPv6:2a02:6b8:c0d:b8c:0:640:d53b:4f39]) by forward500o.mail.yandex.net (Yandex) with ESMTP id 26B4F944B2A; Tue, 28 Jun 2022 14:46:49 +0300 (MSK) Received: from vla3-178a3f795968.qloud-c.yandex.net (vla3-178a3f795968.qloud-c.yandex.net [2a02:6b8:c15:2584:0:640:178a:3f79]) by vla1-d53b4f396d89.qloud-c.yandex.net (mxback/Yandex) with ESMTP id shX8vltbCi-klf4VlmY; Tue, 28 Jun 2022 14:46:49 +0300 X-Yandex-Fwd: 2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maquefel.me; s=mail; t=1656416809; bh=2/6RMvAXK2iD9AZRDQ9gg5xO/5zbiLVsv0H//9xZIDA=; h=In-Reply-To:References:Date:Subject:Cc:To:From:Message-Id; b=sl5Mzu5Ig9R8ynXv8QtUBqWYcIPYbeVE2X741qhvMD3WXpDu+9UKMWl9YvxdaVPHT qlROEU4obY84Txlo5Nyw7OsnwgkkaS6S3VJ2tqC8RGkrNgU2857MgIgWm1HYgpQVHN svjG3dQdYlqUF+J7SJbXdRNk60hH7vWT4nAwylpk= Authentication-Results: vla1-d53b4f396d89.qloud-c.yandex.net; dkim=pass header.i=@maquefel.me Received: by vla3-178a3f795968.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id s5dKAuvn1y-kjMCPfi8; Tue, 28 Jun 2022 14:46:46 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) From: Nikita Shubin Cc: linux@yadro.com, Genevieve Chan , =?UTF-8?q?Jo=C3=A3o=20M=C3=A1rio=20Domingos?= , Nikita Shubin , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v5] perf tools riscv: Add support for get_cpuid_str function Date: Tue, 28 Jun 2022 14:45:56 +0300 Message-Id: <20220628114625.166665-3-nikita.shubin@maquefel.me> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220628114625.166665-1-nikita.shubin@maquefel.me> References: <20220628114625.166665-1-nikita.shubin@maquefel.me> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Nikita Shubin The get_cpuid_str function returns the string that contains values of MVENDORID, MARCHID and MIMPID in hex format separated by coma. The values themselves are taken from first cpu entry in "/proc/cpuid" that contains "mvendorid", "marchid" and "mimpid". Signed-off-by: Nikita Shubin --- v4->v5: - fixed cpuid leakage - no "if" before free - don't set cpuid to NULL, just goto free --- tools/perf/arch/riscv/util/Build | 1 + tools/perf/arch/riscv/util/header.c | 104 ++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 tools/perf/arch/riscv/util/header.c diff --git a/tools/perf/arch/riscv/util/Build b/tools/perf/arch/riscv/util/= Build index 7d3050134ae0..603dbb5ae4dc 100644 --- a/tools/perf/arch/riscv/util/Build +++ b/tools/perf/arch/riscv/util/Build @@ -1,4 +1,5 @@ perf-y +=3D perf_regs.o +perf-y +=3D header.o =20 perf-$(CONFIG_DWARF) +=3D dwarf-regs.o perf-$(CONFIG_LIBDW_DWARF_UNWIND) +=3D unwind-libdw.o diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/ut= il/header.c new file mode 100644 index 000000000000..4a41856938a8 --- /dev/null +++ b/tools/perf/arch/riscv/util/header.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Implementation of get_cpuid(). + * + * Author: Nikita Shubin + */ + +#include +#include +#include +#include +#include "../../util/debug.h" +#include "../../util/header.h" + +#define CPUINFO_MVEN "mvendorid" +#define CPUINFO_MARCH "marchid" +#define CPUINFO_MIMP "mimpid" +#define CPUINFO "/proc/cpuinfo" + +static char *_get_field(const char *line) +{ + char *line2, *nl; + + line2 =3D strrchr(line, ' '); + if (!line2) + return NULL; + + line2++; + nl =3D strrchr(line, '\n'); + if (!nl) + return NULL; + + return strndup(line2, nl - line2); +} + +static char *_get_cpuid(void) +{ + char *line =3D NULL; + char *mvendorid =3D NULL; + char *marchid =3D NULL; + char *mimpid =3D NULL; + char *cpuid =3D NULL; + int read; + unsigned long line_sz; + FILE *cpuinfo; + + cpuinfo =3D fopen(CPUINFO, "r"); + if (cpuinfo =3D=3D NULL) + return cpuid; + + while ((read =3D getline(&line, &line_sz, cpuinfo)) !=3D -1) { + if (!strncmp(line, CPUINFO_MVEN, strlen(CPUINFO_MVEN))) { + mvendorid =3D _get_field(line); + if (!mvendorid) + goto free; + } else if (!strncmp(line, CPUINFO_MARCH, strlen(CPUINFO_MARCH))) { + marchid =3D _get_field(line); + if (!marchid) + goto free; + } else if (!strncmp(line, CPUINFO_MIMP, strlen(CPUINFO_MIMP))) { + mimpid =3D _get_field(line); + if (!mimpid) + goto free; + + break; + } + } + + if (!mvendorid || !marchid || !mimpid) + goto free; + + if (asprintf(&cpuid, "%s-%s-%s", mvendorid, marchid, mimpid) < 0) + cpuid =3D NULL; + +free: + fclose(cpuinfo); + free(mvendorid); + free(marchid); + free(mimpid); + + return cpuid; +} + +int get_cpuid(char *buffer, size_t sz) +{ + char *cpuid =3D _get_cpuid(); + int ret =3D 0; + + if (sz < strlen(cpuid)) { + ret =3D -EINVAL; + goto free; + } + + scnprintf(buffer, sz, "%s", cpuid); +free: + free(cpuid); + return ret; +} + +char * +get_cpuid_str(struct perf_pmu *pmu __maybe_unused) +{ + return _get_cpuid(); +} --=20 2.30.2