From nobody Sun Nov 24 01:58:28 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 8A1825B216 for ; Sat, 9 Nov 2024 00:34:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731112457; cv=none; b=tavbLjDYnE+YPIqSIqM1KOHgZXndBkj7/BDjeBmrwGFuFaWxv3HNYBWNLBlT9EK2yFouVhmIJrBU1p+nQ3VqiR9vJE1R18b2j9Bu3uaAoncWCv96dNt3lgiWZsIGDYzfmVdzz8tgukr7JGXrjklAkOfDjEuVDvzUKQvaQI3DKjA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731112457; c=relaxed/simple; bh=AGv74AJZdeu7zWXnn9qpaFjvkwR5rG7fCt7CgLsbOyw=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=gv82fnrGBzPW3631QTtsx1S/+rfntEkhU5YoKQ6HdtTI2Nuwl/Vn/9GJHuHdEGLiYsAvwv3HST3vAHnywRRp4sbWWAnGoS6tzmMfrBAUFP3iKARABSRr/+IRf4zrWU/8ffoPGzhejEbOQ+Tmsby030gmJjgQjzGZoGobI410y0s= 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=Lthqh13k; arc=none smtp.client-ip=209.85.219.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="Lthqh13k" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e30daaf5928so5290359276.1 for ; Fri, 08 Nov 2024 16:34:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1731112455; x=1731717255; 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=xGtMOz6yOSk+lm+i7Vt4SUAHF0VdrlVxtCtmCN8/Bao=; b=Lthqh13kFggUHJl5gei+bGLCPn6i15jBp/uDKoTQdNYjXrzgS3DI/hm+KJxrerDKI8 POZnbF6zEHAGIIOLJbZ8ZvkQPTMmFNb61CEjJyQ2CHM0C6iWPoVzJKb02dqHZ32EnQUD qe4iqJQ00rRE4CEOgfict5Rl6IkifysDJteYilXW3zAEhVDaE5m9FCd7oU06NqsQcMau 5tV7HP11K/ln2PiEjbn2cZE+L5sXZjpG9+iOuzb4ZE0qPojOxoKTD9ZFL2JLJtkz4PEB PWV/4YLMRvzojHTdSYXk3dmbahXitDS5Sd/vFPhs5m+Cx8Q9rNEuK3ozvWSDnqGitZOy CdeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731112455; x=1731717255; 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=xGtMOz6yOSk+lm+i7Vt4SUAHF0VdrlVxtCtmCN8/Bao=; b=E3n+A6UH/DbXBCHt1SlLj3Zl+zG/XQGTj6aW5G+1Me5T3SJBU07TOAWxYpm2D4L7F5 OR8SLoKRcjEFCfqESJ3t6a94e9Ukoz9UjCaK5ZCMHITSRLVAOzhQN/37pBCdzpKmpfdb MnDZE/KmWFfmCAGY+VCAhEdyM2BVkq2wJiqpnk3O6On0Wjd5sCgBWYzSx/azMyRlVwwx XaZTzif4BZin3iuuN9NSiee8EqqjdYLkGyWsiRaHY/kDC0Iwhv5l245rFUGtr1A19JSV C52hgRL4wnCWBxT6o9QG1+Yl1uTCK6UerpOLHUVC4rFvLpzX5nDmbgU/844UgHEeNQRU 4qSQ== X-Forwarded-Encrypted: i=1; AJvYcCXtyKyrb6oxhUhcNphyvXx5Z607Gm/9wmxNv71V5TLpy3NMy/E1u6n6BC5Coyg5cc1wVfwHAVpuTqolzrc=@vger.kernel.org X-Gm-Message-State: AOJu0Yyvc28Qdn2dpLIeZEIZnW6OF+629Cj1W48pPd7aYMgcY68TBbzU 4hTMpJ7tdx2MRZmPYobhN1Yf2Wnss0Y4H/H3PC4GZrrOTC+9ce/gzj+bC9JepO8XWP+ZDrS9XED 07/FoyQ== X-Google-Smtp-Source: AGHT+IFgId3CJ0thNULc8VJ0pydC7o+YSJzwxnR9byUvy2JEXYKIQ3KhmfGvhui7YaD89EyDNOqcBRbcDoO/ X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:6fd2:1e36:6d8c:7c49]) (user=irogers job=sendgmr) by 2002:a25:15c2:0:b0:e28:f231:1aa8 with SMTP id 3f1490d57ef6-e337f846765mr5659276.2.1731112454789; Fri, 08 Nov 2024 16:34:14 -0800 (PST) Date: Fri, 8 Nov 2024 16:33:57 -0800 In-Reply-To: <20241109003358.461437-1-irogers@google.com> Message-Id: <20241109003358.461437-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241109003358.461437-1-irogers@google.com> X-Mailer: git-send-email 2.47.0.277.g8800431eea-goog Subject: [PATCH v8 6/7] perf test: Add hwmon "PMU" test 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" Based on a mix of the sysfs PMU test (for creating the reference files) and the tool PMU test, test that parsing given hwmon events with there aliases creates the expected config values. Signed-off-by: Ian Rogers --- tools/perf/tests/hwmon_pmu.c | 230 +++++++++++++++++++++++++++++++++++ tools/perf/util/pmus.c | 7 ++ tools/perf/util/pmus.h | 3 + 3 files changed, 240 insertions(+) diff --git a/tools/perf/tests/hwmon_pmu.c b/tools/perf/tests/hwmon_pmu.c index f5b58486d8d3..3a95cc63fc48 100644 --- a/tools/perf/tests/hwmon_pmu.c +++ b/tools/perf/tests/hwmon_pmu.c @@ -1,10 +1,238 @@ // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) #include "debug.h" +#include "evlist.h" #include "hwmon_pmu.h" +#include "parse-events.h" #include "tests.h" +#include +#include #include #include =20 +static const struct test_event { + const char *name; + const char *alias; + long config; +} test_events[] =3D { + { + "temp_test_hwmon_event1", + "temp1", + 0xA0001, + }, + { + "temp_test_hwmon_event2", + "temp2", + 0xA0002, + }, +}; + +/* Cleanup test PMU directory. */ +static int test_pmu_put(const char *dir, struct perf_pmu *hwm) +{ + char buf[PATH_MAX + 20]; + int ret; + + if (scnprintf(buf, sizeof(buf), "rm -fr %s", dir) < 0) { + pr_err("Failure to set up buffer for \"%s\"\n", dir); + return -EINVAL; + } + ret =3D system(buf); + if (ret) + pr_err("Failure to \"%s\"\n", buf); + + perf_pmu__delete(hwm); + return ret; +} + +/* + * Prepare test PMU directory data, normally exported by kernel at + * /sys/class/hwmon/hwmon/. Give as input a buffer to hold the file + * path, the result is PMU loaded using that directory. + */ +static struct perf_pmu *test_pmu_get(char *dir, size_t sz) +{ + const char *test_hwmon_name_nl =3D "A test hwmon PMU\n"; + const char *test_hwmon_name =3D "A test hwmon PMU"; + /* Simulated hwmon items. */ + const struct test_item { + const char *name; + const char *value; + } test_items[] =3D { + { "temp1_label", "test hwmon event1\n", }, + { "temp1_input", "40000\n", }, + { "temp2_label", "test hwmon event2\n", }, + { "temp2_input", "50000\n", }, + }; + int dirfd, file; + struct perf_pmu *hwm =3D NULL; + ssize_t len; + + /* Create equivalent of sysfs mount point. */ + scnprintf(dir, sz, "/tmp/perf-hwmon-pmu-test-XXXXXX"); + if (!mkdtemp(dir)) { + pr_err("mkdtemp failed\n"); + dir[0] =3D '\0'; + return NULL; + } + dirfd =3D open(dir, O_DIRECTORY); + if (dirfd < 0) { + pr_err("Failed to open test directory \"%s\"\n", dir); + goto err_out; + } + + /* Create the test hwmon directory and give it a name. */ + if (mkdirat(dirfd, "hwmon1234", 0755) < 0) { + pr_err("Failed to mkdir hwmon directory\n"); + goto err_out; + } + file =3D openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600); + if (!file) { + pr_err("Failed to open for writing file \"name\"\n"); + goto err_out; + } + len =3D strlen(test_hwmon_name_nl); + if (write(file, test_hwmon_name_nl, len) < len) { + close(file); + pr_err("Failed to write to 'name' file\n"); + goto err_out; + } + close(file); + + /* Create test hwmon files. */ + for (size_t i =3D 0; i < ARRAY_SIZE(test_items); i++) { + const struct test_item *item =3D &test_items[i]; + + file =3D openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600); + if (!file) { + pr_err("Failed to open for writing file \"%s\"\n", item->name); + goto err_out; + } + + if (write(file, item->value, strlen(item->value)) < 0) { + pr_err("Failed to write to file \"%s\"\n", item->name); + close(file); + goto err_out; + } + close(file); + } + + /* Make the PMU reading the files created above. */ + hwm =3D perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name= ); + if (!hwm) + pr_err("Test hwmon creation failed\n"); + +err_out: + if (!hwm) { + test_pmu_put(dir, hwm); + if (dirfd >=3D 0) + close(dirfd); + } + return hwm; +} + +static int do_test(size_t i, bool with_pmu, bool with_alias) +{ + const char *test_event =3D with_alias ? test_events[i].alias : test_event= s[i].name; + struct evlist *evlist =3D evlist__new(); + struct evsel *evsel; + struct parse_events_error err; + int ret; + char str[128]; + bool found =3D false; + + if (!evlist) { + pr_err("evlist allocation failed\n"); + return TEST_FAIL; + } + + if (with_pmu) + snprintf(str, sizeof(str), "/%s/", test_event); + else + strlcpy(str, test_event, sizeof(str)); + + pr_debug("Testing '%s'\n", str); + parse_events_error__init(&err); + ret =3D parse_events(evlist, str, &err); + if (ret) { + evlist__delete(evlist); + + pr_debug("FAILED %s:%d failed to parse event '%s', err %d\n", + __FILE__, __LINE__, str, ret); + parse_events_error__print(&err, str); + ret =3D TEST_FAIL; + goto out; + } + + ret =3D TEST_OK; + if (with_pmu ? (evlist->core.nr_entries !=3D 1) : (evlist->core.nr_entrie= s < 1)) { + pr_debug("FAILED %s:%d Unexpected number of events for '%s' of %d\n", + __FILE__, __LINE__, str, evlist->core.nr_entries); + ret =3D TEST_FAIL; + goto out; + } + + evlist__for_each_entry(evlist, evsel) { + if (!perf_pmu__is_hwmon(evsel->pmu)) + continue; + + if (evsel->core.attr.config !=3D (u64)test_events[i].config) { + pr_debug("FAILED %s:%d Unexpected config for '%s', %lld !=3D %ld\n", + __FILE__, __LINE__, str, + evsel->core.attr.config, + test_events[i].config); + ret =3D TEST_FAIL; + goto out; + } + found =3D true; + } + + if (!found) { + pr_debug("FAILED %s:%d Didn't find hwmon event '%s' in parsed evsels\n", + __FILE__, __LINE__, str); + ret =3D TEST_FAIL; + } + +out: + evlist__delete(evlist); + return ret; +} + +static int test__hwmon_pmu(bool with_pmu) +{ + char dir[PATH_MAX]; + struct perf_pmu *pmu =3D test_pmu_get(dir, sizeof(dir)); + int ret =3D TEST_OK; + + if (!pmu) + return TEST_FAIL; + + for (size_t i =3D 0; i < ARRAY_SIZE(test_events); i++) { + ret =3D do_test(i, with_pmu, /*with_alias=3D*/false); + + if (ret !=3D TEST_OK) + break; + + ret =3D do_test(i, with_pmu, /*with_alias=3D*/true); + + if (ret !=3D TEST_OK) + break; + } + test_pmu_put(dir, pmu); + return ret; +} + +static int test__hwmon_pmu_without_pmu(struct test_suite *test __maybe_unu= sed, + int subtest __maybe_unused) +{ + return test__hwmon_pmu(/*with_pmu=3D*/false); +} + +static int test__hwmon_pmu_with_pmu(struct test_suite *test __maybe_unused, + int subtest __maybe_unused) +{ + return test__hwmon_pmu(/*with_pmu=3D*/false); +} + static int test__parse_hwmon_filename(struct test_suite *test __maybe_unus= ed, int subtest __maybe_unused) { @@ -101,6 +329,8 @@ static int test__parse_hwmon_filename(struct test_suite= *test __maybe_unused, =20 static struct test_case tests__hwmon_pmu[] =3D { TEST_CASE("Basic parsing test", parse_hwmon_filename), + TEST_CASE("Parsing without PMU name", hwmon_pmu_without_pmu), + TEST_CASE("Parsing with PMU name", hwmon_pmu_with_pmu), { .name =3D NULL, } }; =20 diff --git a/tools/perf/util/pmus.c b/tools/perf/util/pmus.c index 5c3e88adb9e6..451c6e00ad70 100644 --- a/tools/perf/util/pmus.c +++ b/tools/perf/util/pmus.c @@ -733,6 +733,13 @@ struct perf_pmu *perf_pmus__add_test_pmu(int test_sysf= s_dirfd, const char *name) return perf_pmu__lookup(&other_pmus, test_sysfs_dirfd, name, /*eager_load= =3D*/true); } =20 +struct perf_pmu *perf_pmus__add_test_hwmon_pmu(int hwmon_dir, + const char *sysfs_name, + const char *name) +{ + return hwmon_pmu__new(&other_pmus, hwmon_dir, sysfs_name, name); +} + struct perf_pmu *perf_pmus__fake_pmu(void) { static struct perf_pmu fake =3D { diff --git a/tools/perf/util/pmus.h b/tools/perf/util/pmus.h index e1742b56eec7..a0cb0eb2ff97 100644 --- a/tools/perf/util/pmus.h +++ b/tools/perf/util/pmus.h @@ -30,6 +30,9 @@ bool perf_pmus__supports_extended_type(void); char *perf_pmus__default_pmu_name(void); =20 struct perf_pmu *perf_pmus__add_test_pmu(int test_sysfs_dirfd, const char = *name); +struct perf_pmu *perf_pmus__add_test_hwmon_pmu(int hwmon_dir, + const char *sysfs_name, + const char *name); struct perf_pmu *perf_pmus__fake_pmu(void); =20 #endif /* __PMUS_H */ --=20 2.47.0.277.g8800431eea-goog