From nobody Sun Nov 24 01:43:30 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 B5EC345026 for ; Sat, 9 Nov 2024 00:38:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731112696; cv=none; b=L8plzlOHGSRFySfhnvaqoGPd3woYI8tPlfRImbm41DQvEvStxj9vNxyDFqL8uqfA6qxv8O1FBVTABgAEBeQmec3Jv2SsFYpAHeVyEZGGPv3VoGSryA+FcIGTjoeqR9vJ6IhOdXt83ricHRLpxa8WBqv6QCdjXPtbiFD4mZPZ4tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731112696; c=relaxed/simple; bh=DF29HfLYk4APYNM6PN4eXxEzIMhUOA5QnWG8bu4TeS8=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Dt7eW5RQbfWldg+HTil+Ic1a64l1IzmSvVJeLzVVleye853t07nTc6S8K8i8H7FEn6AkYNTTgC4RPtD+UX2qOKg68a0ExyMU78r7tovs6WBjMJ1r9XGWyoeOMMqY6vQ473tdB1ODN7CYUxNmEyppRGoSYhmjU6LdIFkWuk7cpRU= 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=fKNKFPB8; arc=none smtp.client-ip=209.85.219.201 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="fKNKFPB8" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e30c7a38bd7so2931957276.0 for ; Fri, 08 Nov 2024 16:38:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731112693; x=1731717493; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=SKmPEjnoDqQNPKBI5I5F9OwiMicG8/7OcYSqdoUQCi4=; b=fKNKFPB8EDkKmg2iewVHGKzkkNdWzGZH6hU8fnTn8slyvgXkUmaRs7/QVHP/1jU62t KPLXTShv5UoBT6IQw2Qk991QxvdMB3w6FSdk+u1t+LrR6eBt4FoNJCUbexttrVEbYPYS e5b8CZLH6flKDgHQYe4+yzpbf/mhyX9uBcjq2Y9sFyCUdeOSC1QI2fauvQjMqINDN/+5 WMtGF2P9T1/32EVIthhVK8f0kL8LeHkosP0nYOtEyMvC8Gx//lf7Az8ozy+nD5BA4KEC MljbnsI9DL/gPWASH298G86sXpszCCoM2YKk407wkGF5bn5tHbDAWSGtx6uyLZ+8lt6Z hqCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731112693; x=1731717493; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=SKmPEjnoDqQNPKBI5I5F9OwiMicG8/7OcYSqdoUQCi4=; b=ofuJescJm7RDXNcWP66ZCBlz1+jfGoOvf0zNWmB4jb0BVST0aehepYMz+QSs9hDJmq 1zyLcqxl/TCQeSDssvvHbg3Gy4N5d1tmprcT7XidC0T1MLuqc3ize6FhT2gt0zTu3Ryw eLx3Oua0MbLEdZejSRrYN6JV6HgNbrT2ooJ2D0hGiVfyX5ikXT8dJdnvX40OT/evuqI+ weLAvVPhM9pAZcIbOrSrbB0DhEcrPS37bUF8ckAPOpQAZ1AF6ra4xSnDSwuw5hzXbbEP YBVezHFSL+txeAdrzrkQSm0U96Eln1KJk9yBPSYX57hHrtajZQvRWN5rCTVjd3IhX3n1 DzGw== X-Forwarded-Encrypted: i=1; AJvYcCXiQJuca0a1uLQAb7ueoJD4IotyjMdHA1+sB/8ETMM1GUfk6BtvIIZBWGr4YMMjolfHcPTpoMWmTcANQE4=@vger.kernel.org X-Gm-Message-State: AOJu0Ywet2Ze3O1pKEycNC924ocSnZVlGmjXMamyPry5o3/jCIrE0aLC x/b7M6R/67GKoVxr/r4VBMJo/RCtcly/ApABM0AdexF8ehiDztYBaWw9DWr9vlLkS0zWwSj7TRq MHSCrcg== X-Google-Smtp-Source: AGHT+IGe6YYvSbH6XvUIdzf+Ra5PiGVW6RreBKcUNdPP8Dc0zQ0s5ZXhqQ6LJjzjtbfcSUav8fTAmyhjdaWF X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:6fd2:1e36:6d8c:7c49]) (user=irogers job=sendgmr) by 2002:a5b:ad0:0:b0:e2e:2c2e:277b with SMTP id 3f1490d57ef6-e337e18eb4fmr24771276.3.1731112692958; Fri, 08 Nov 2024 16:38:12 -0800 (PST) Date: Fri, 8 Nov 2024 16:37:56 -0800 In-Reply-To: <20241109003759.473460-1-irogers@google.com> Message-Id: <20241109003759.473460-5-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241109003759.473460-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Subject: [PATCH v9 4/7] perf hwmon_pmu: Add a tool PMU exposing events from hwmon in sysfs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Ravi Bangoria , Weilin Wang , Yoshihiro Furudera , James Clark , Athira Jajeev , Howard Chu , Oliver Upton , Changbin Du , Ze Gao , Junhao He , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a tool PMU for hwmon events but don't enable. The hwmon sysfs ABI is defined in Documentation/hwmon/sysfs-interface.rst. Create a PMU that reads the hwmon input and can be used in `perf stat` and metrics much as an uncore PMU can. For example, when enabled by a later patch, the following shows reading the CPU temperature and 2 fan speeds alongside the uncore frequency: ``` $ perf stat -e temp_cpu,fan1,hwmon_thinkpad/fan2/,tool/num_cpus_online/ -M = UNCORE_FREQ -I 1000 1.001153138 52.00 'C temp_cpu 1.001153138 2,588 rpm fan1 1.001153138 2,482 rpm hwmon_thinkpad/fan2/ 1.001153138 8 tool/num_cpus_online/ 1.001153138 1,077,101,397 UNC_CLOCK.SOCKET #= 1.08 UNCORE_FREQ 1.001153138 1,012,773,595 duration_time ... ``` The PMUs are named from /sys/class/hwmon/hwmon/name and have an alias of hwmon. Hwmon data is presented in multiple _ files. The is used to identify the event as is the followed by the contents of the _label file if it exists. The _input file gives the data read by perf. When enabled by a later patch, in `perf list` the other hwmon files are used to give a richer description, for example: ``` hwmon: temp1 [Temperature in unit acpitz named temp1. Unit: hwmon_acpitz] in0 [Voltage in unit bat0 named in0. Unit: hwmon_bat0] temp_core_0 OR temp2 [Temperature in unit coretemp named Core 0. crit=3D100'C,max=3D100'C= crit_alarm=3D0'C. Unit: hwmon_coretemp] temp_core_1 OR temp3 [Temperature in unit coretemp named Core 1. crit=3D100'C,max=3D100'C= crit_alarm=3D0'C. Unit: hwmon_coretemp] ... temp_package_id_0 OR temp1 [Temperature in unit coretemp named Package id 0. crit=3D100'C,max= =3D100'C crit_alarm=3D0'C. Unit: hwmon_coretemp] temp1 [Temperature in unit iwlwifi_1 named temp1. Unit: hwmon_iwlwifi_1] temp_composite OR temp1 [Temperature in unit nvme named Composite. alarm=3D0'C,crit=3D86.85'= C,max=3D75.85'C, min=3D-273.15'C. Unit: hwmon_nvme] temp_sensor_1 OR temp2 [Temperature in unit nvme named Sensor 1. max=3D65261.8'C,min=3D-273= .15'C. Unit: hwmon_nvme] temp_sensor_2 OR temp3 [Temperature in unit nvme named Sensor 2. max=3D65261.8'C,min=3D-273= .15'C. Unit: hwmon_nvme] fan1 [Fan in unit thinkpad named fan1. Unit: hwmon_thinkpad] fan2 [Fan in unit thinkpad named fan2. Unit: hwmon_thinkpad] ... temp_cpu OR temp1 [Temperature in unit thinkpad named CPU. Unit: hwmon_thinkpad] temp_gpu OR temp2 [Temperature in unit thinkpad named GPU. Unit: hwmon_thinkpad] curr1 [Current in unit ucsi_source_psy_usbc000_0 named curr1. max=3D1.5A. = Unit: hwmon_ucsi_source_psy_usbc000_0] in0 [Voltage in unit ucsi_source_psy_usbc000_0 named in0. max=3D5V,min= =3D5V. Unit: hwmon_ucsi_source_psy_usbc000_0] ``` As there may be multiple hwmon devices a range of PMU types are reserved for their use and to identify the PMU as belonging to the hwmon types. Signed-off-by: Ian Rogers --- tools/perf/util/hwmon_pmu.c | 682 ++++++++++++++++++++++++++++++++++++ tools/perf/util/hwmon_pmu.h | 40 +++ tools/perf/util/pmu.h | 2 + 3 files changed, 724 insertions(+) diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c index 8791e55097dc..89acc19347c8 100644 --- a/tools/perf/util/hwmon_pmu.c +++ b/tools/perf/util/hwmon_pmu.c @@ -1,11 +1,25 @@ // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) +#include "counts.h" #include "debug.h" +#include "evsel.h" +#include "hashmap.h" #include "hwmon_pmu.h" +#include "pmu.h" +#include +#include +#include +#include #include +#include +#include +#include #include #include #include +#include +#include #include +#include =20 /** Strings that correspond to enum hwmon_type. */ static const char * const hwmon_type_strs[HWMON_TYPE_MAX] =3D { @@ -72,6 +86,79 @@ static const char * const hwmon_item_strs[HWMON_ITEM__MA= X] =3D { }; #define LONGEST_HWMON_ITEM_STR "average_interval_max" =20 +static const char *const hwmon_units[HWMON_TYPE_MAX] =3D { + NULL, + "V", /* cpu */ + "A", /* curr */ + "J", /* energy */ + "rpm", /* fan */ + "%", /* humidity */ + "V", /* in */ + "", /* intrusion */ + "W", /* power */ + "Hz", /* pwm */ + "'C", /* temp */ +}; + +struct hwmon_pmu { + struct perf_pmu pmu; + struct hashmap events; + int hwmon_dir_fd; +}; + +/** + * union hwmon_pmu_event_key: Key for hwmon_pmu->events as such each key + * represents an event. + * + * Related hwmon files start that this key represents. + */ +union hwmon_pmu_event_key { + long type_and_num; + struct { + int num :16; + enum hwmon_type type :8; + }; +}; + +/** + * struct hwmon_pmu_event_value: Value in hwmon_pmu->events. + * + * Hwmon files are of the form _ and may have a suffix + * _alarm. + */ +struct hwmon_pmu_event_value { + /** @items: which item files are present. */ + DECLARE_BITMAP(items, HWMON_ITEM__MAX); + /** @alarm_items: which item files are present. */ + DECLARE_BITMAP(alarm_items, HWMON_ITEM__MAX); + /** @label: contents of _label if present. */ + char *label; + /** @name: name computed from label of the form _