Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2502075rwb; Mon, 15 Aug 2022 06:37:32 -0700 (PDT) X-Google-Smtp-Source: AA6agR74HqnS+BSYGpnRT4vHSDJkfgjOYcC5rmxFbmfCY75dj52Pft9LYup9agH3Nr7/ZzTivVmY X-Received: by 2002:a05:6402:40c3:b0:442:d798:48ad with SMTP id z3-20020a05640240c300b00442d79848admr14713275edb.154.1660570652442; Mon, 15 Aug 2022 06:37:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660570652; cv=none; d=google.com; s=arc-20160816; b=rkPCf0ZdbZoTkRfo/NaRFOrH+wbD7Tt6TRlvlTTxhZsvc0jl6qCVYgg//pM/z/gpAe gwrZiA3blLzNWN2mnpZ/CabRASfMPXLRYMr3BAujR19uL3g+J/FRC+cRY3KYVcGypklt jM6UWqPaqri8V3AuTbo+1Mo5Qoq2uymNBx8U6qpWdbiUXyJzp7WdC+CThLlfedeeDTd2 /uWl9MVyNy0EQSX0G18ByHytxUXRnrjZNuCFVJf8dFJXmse/xTiSAiw2Nt6RqDXQLRG9 LLvrOcs9goTtRGOsQd07fPmvur4YmTH7hH9CShys5OHEbOI7qrwW7pNNbDaZak/KYw2k p+4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:from :dkim-signature; bh=Pn3L6cXxvX9m7wl4hmev8LZGUCinVPWeTkQjWKkyw4Y=; b=kNS0RuPfeFycEWHA8tMV7RL8shfVr/+WaQa1YHimFCYcbeVh4sfyNbh0rh11uiNqoe qAzRaNXrLI+YK51RayUnzjqL5pyIqfDfVDJMHQHFRo8OkoNDpMbSb/QPE5MQ4CAvw+JL kmw8s3gaGCapSBjV7me9eoCCB4u2iOBzQ555RJHFtBIHLBwE9iZBkljhsKyrUXWukTxP jYLgxnlbwt+oAugPUwKuVie47cIxuEoWhJoBqWRFsUC5Qs2oW+yGF20kl+A9x1xkQglH iHQvj9z2m26RLX/UlowKxWFr9yznvywFxmCp462P7/o0JUDUU2fI6dNEAAgwS9kRE8Hq G7Ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@maquefel.me header.s=mail header.b="Ic1PN/9R"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d24-20020a17090694d800b0072b83e87339si6718257ejy.208.2022.08.15.06.37.07; Mon, 15 Aug 2022 06:37:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@maquefel.me header.s=mail header.b="Ic1PN/9R"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242444AbiHONfK (ORCPT + 99 others); Mon, 15 Aug 2022 09:35:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242071AbiHONfJ (ORCPT ); Mon, 15 Aug 2022 09:35:09 -0400 X-Greylist: delayed 347 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 15 Aug 2022 06:35:07 PDT Received: from forward100o.mail.yandex.net (forward100o.mail.yandex.net [37.140.190.180]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAB8320F68; Mon, 15 Aug 2022 06:35:07 -0700 (PDT) Received: from forward503p.mail.yandex.net (forward503p.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b7:122]) by forward100o.mail.yandex.net (Yandex) with ESMTP id 61BB052ABA4F; Mon, 15 Aug 2022 16:24:02 +0300 (MSK) Received: from iva1-dcde80888020.qloud-c.yandex.net (iva1-dcde80888020.qloud-c.yandex.net [IPv6:2a02:6b8:c0c:7695:0:640:dcde:8088]) by forward503p.mail.yandex.net (Yandex) with ESMTP id 49E5511018E6; Mon, 15 Aug 2022 16:22:59 +0300 (MSK) Received: by iva1-dcde80888020.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id BL0XdiM9S6-Mvi4Dxrs; Mon, 15 Aug 2022 16:22:57 +0300 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (Client certificate not present) X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maquefel.me; s=mail; t=1660569778; bh=Pn3L6cXxvX9m7wl4hmev8LZGUCinVPWeTkQjWKkyw4Y=; h=Message-Id:Date:In-Reply-To:Cc:Subject:References:To:From; b=Ic1PN/9REPOl1gnzimegeayVri/yEbacitqkG72vxi/iYEZbgZh/GKBJ8KVikBmVu rNSIAhRerd+A+IZogTabK1I2VTuV5slOnQY3jdESYH19lPfT66eVvgeqk9upyoadN/ DKvVG/cM3V+9jPWQkpFx0fPPWYx3jNO7Vb6CYsQ4= Authentication-Results: iva1-dcde80888020.qloud-c.yandex.net; dkim=pass header.i=@maquefel.me From: Nikita Shubin Cc: linux@yadro.com, Anup Patel , Arnaldo Carvalho de Melo , Nikita Shubin , Kautuk Consul , Peter Zijlstra , Ingo Molnar , 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 v6 1/3] perf tools riscv: Add support for get_cpuid_str function Date: Mon, 15 Aug 2022 16:22:38 +0300 Message-Id: <20220815132251.25702-2-nikita.shubin@maquefel.me> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220815132251.25702-1-nikita.shubin@maquefel.me> References: <20220815132251.25702-1-nikita.shubin@maquefel.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_RPBL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Tested-by: Kautuk Consul --- 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 += perf_regs.o +perf-y += header.o perf-$(CONFIG_DWARF) += dwarf-regs.o perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o diff --git a/tools/perf/arch/riscv/util/header.c b/tools/perf/arch/riscv/util/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 = strrchr(line, ' '); + if (!line2) + return NULL; + + line2++; + nl = strrchr(line, '\n'); + if (!nl) + return NULL; + + return strndup(line2, nl - line2); +} + +static char *_get_cpuid(void) +{ + char *line = NULL; + char *mvendorid = NULL; + char *marchid = NULL; + char *mimpid = NULL; + char *cpuid = NULL; + int read; + unsigned long line_sz; + FILE *cpuinfo; + + cpuinfo = fopen(CPUINFO, "r"); + if (cpuinfo == NULL) + return cpuid; + + while ((read = getline(&line, &line_sz, cpuinfo)) != -1) { + if (!strncmp(line, CPUINFO_MVEN, strlen(CPUINFO_MVEN))) { + mvendorid = _get_field(line); + if (!mvendorid) + goto free; + } else if (!strncmp(line, CPUINFO_MARCH, strlen(CPUINFO_MARCH))) { + marchid = _get_field(line); + if (!marchid) + goto free; + } else if (!strncmp(line, CPUINFO_MIMP, strlen(CPUINFO_MIMP))) { + mimpid = _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 = NULL; + +free: + fclose(cpuinfo); + free(mvendorid); + free(marchid); + free(mimpid); + + return cpuid; +} + +int get_cpuid(char *buffer, size_t sz) +{ + char *cpuid = _get_cpuid(); + int ret = 0; + + if (sz < strlen(cpuid)) { + ret = -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(); +} -- 2.35.1