xref: /linux/tools/perf/tests/hwmon_pmu.c (revision db26a8c9e3c285092ed3e14a33755582c04e0269)
18c329057SIan Rogers // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
28c329057SIan Rogers #include "debug.h"
3531ee0fdSIan Rogers #include "evlist.h"
48c329057SIan Rogers #include "hwmon_pmu.h"
5531ee0fdSIan Rogers #include "parse-events.h"
68c329057SIan Rogers #include "tests.h"
7531ee0fdSIan Rogers #include <fcntl.h>
8531ee0fdSIan Rogers #include <sys/stat.h>
98c329057SIan Rogers #include <linux/compiler.h>
108c329057SIan Rogers #include <linux/kernel.h>
118c329057SIan Rogers #include <linux/string.h>
128c329057SIan Rogers 
13531ee0fdSIan Rogers static const struct test_event {
14531ee0fdSIan Rogers 	const char *name;
15531ee0fdSIan Rogers 	const char *alias;
16531ee0fdSIan Rogers 	long config;
17531ee0fdSIan Rogers } test_events[] = {
18531ee0fdSIan Rogers 	{
19531ee0fdSIan Rogers 		"temp_test_hwmon_event1",
20531ee0fdSIan Rogers 		"temp1",
21531ee0fdSIan Rogers 		0xA0001,
22531ee0fdSIan Rogers 	},
23531ee0fdSIan Rogers 	{
24531ee0fdSIan Rogers 		"temp_test_hwmon_event2",
25531ee0fdSIan Rogers 		"temp2",
26531ee0fdSIan Rogers 		0xA0002,
27531ee0fdSIan Rogers 	},
28531ee0fdSIan Rogers };
29531ee0fdSIan Rogers 
30531ee0fdSIan Rogers /* Cleanup test PMU directory. */
31531ee0fdSIan Rogers static int test_pmu_put(const char *dir, struct perf_pmu *hwm)
32531ee0fdSIan Rogers {
33531ee0fdSIan Rogers 	char buf[PATH_MAX + 20];
34531ee0fdSIan Rogers 	int ret;
35531ee0fdSIan Rogers 
36531ee0fdSIan Rogers 	if (scnprintf(buf, sizeof(buf), "rm -fr %s", dir) < 0) {
37531ee0fdSIan Rogers 		pr_err("Failure to set up buffer for \"%s\"\n", dir);
38531ee0fdSIan Rogers 		return -EINVAL;
39531ee0fdSIan Rogers 	}
40531ee0fdSIan Rogers 	ret = system(buf);
41531ee0fdSIan Rogers 	if (ret)
42531ee0fdSIan Rogers 		pr_err("Failure to \"%s\"\n", buf);
43531ee0fdSIan Rogers 
44531ee0fdSIan Rogers 	perf_pmu__delete(hwm);
45531ee0fdSIan Rogers 	return ret;
46531ee0fdSIan Rogers }
47531ee0fdSIan Rogers 
48531ee0fdSIan Rogers /*
49531ee0fdSIan Rogers  * Prepare test PMU directory data, normally exported by kernel at
50531ee0fdSIan Rogers  * /sys/class/hwmon/hwmon<number>/. Give as input a buffer to hold the file
51531ee0fdSIan Rogers  * path, the result is PMU loaded using that directory.
52531ee0fdSIan Rogers  */
53531ee0fdSIan Rogers static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
54531ee0fdSIan Rogers {
55531ee0fdSIan Rogers 	const char *test_hwmon_name_nl = "A test hwmon PMU\n";
56531ee0fdSIan Rogers 	const char *test_hwmon_name = "A test hwmon PMU";
57531ee0fdSIan Rogers 	/* Simulated hwmon items. */
58531ee0fdSIan Rogers 	const struct test_item {
59531ee0fdSIan Rogers 		const char *name;
60531ee0fdSIan Rogers 		const char *value;
61531ee0fdSIan Rogers 	} test_items[] = {
62531ee0fdSIan Rogers 		{ "temp1_label", "test hwmon event1\n", },
63531ee0fdSIan Rogers 		{ "temp1_input", "40000\n", },
64531ee0fdSIan Rogers 		{ "temp2_label", "test hwmon event2\n", },
65531ee0fdSIan Rogers 		{ "temp2_input", "50000\n", },
66531ee0fdSIan Rogers 	};
67531ee0fdSIan Rogers 	int dirfd, file;
68531ee0fdSIan Rogers 	struct perf_pmu *hwm = NULL;
69531ee0fdSIan Rogers 	ssize_t len;
70531ee0fdSIan Rogers 
71531ee0fdSIan Rogers 	/* Create equivalent of sysfs mount point. */
72531ee0fdSIan Rogers 	scnprintf(dir, sz, "/tmp/perf-hwmon-pmu-test-XXXXXX");
73531ee0fdSIan Rogers 	if (!mkdtemp(dir)) {
74531ee0fdSIan Rogers 		pr_err("mkdtemp failed\n");
75531ee0fdSIan Rogers 		dir[0] = '\0';
76531ee0fdSIan Rogers 		return NULL;
77531ee0fdSIan Rogers 	}
78531ee0fdSIan Rogers 	dirfd = open(dir, O_DIRECTORY);
79531ee0fdSIan Rogers 	if (dirfd < 0) {
80531ee0fdSIan Rogers 		pr_err("Failed to open test directory \"%s\"\n", dir);
81531ee0fdSIan Rogers 		goto err_out;
82531ee0fdSIan Rogers 	}
83531ee0fdSIan Rogers 
84531ee0fdSIan Rogers 	/* Create the test hwmon directory and give it a name. */
85531ee0fdSIan Rogers 	if (mkdirat(dirfd, "hwmon1234", 0755) < 0) {
86531ee0fdSIan Rogers 		pr_err("Failed to mkdir hwmon directory\n");
87531ee0fdSIan Rogers 		goto err_out;
88531ee0fdSIan Rogers 	}
89531ee0fdSIan Rogers 	file = openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600);
90531ee0fdSIan Rogers 	if (!file) {
91531ee0fdSIan Rogers 		pr_err("Failed to open for writing file \"name\"\n");
92531ee0fdSIan Rogers 		goto err_out;
93531ee0fdSIan Rogers 	}
94531ee0fdSIan Rogers 	len = strlen(test_hwmon_name_nl);
95531ee0fdSIan Rogers 	if (write(file, test_hwmon_name_nl, len) < len) {
96531ee0fdSIan Rogers 		close(file);
97531ee0fdSIan Rogers 		pr_err("Failed to write to 'name' file\n");
98531ee0fdSIan Rogers 		goto err_out;
99531ee0fdSIan Rogers 	}
100531ee0fdSIan Rogers 	close(file);
101531ee0fdSIan Rogers 
102531ee0fdSIan Rogers 	/* Create test hwmon files. */
103531ee0fdSIan Rogers 	for (size_t i = 0; i < ARRAY_SIZE(test_items); i++) {
104531ee0fdSIan Rogers 		const struct test_item *item = &test_items[i];
105531ee0fdSIan Rogers 
106531ee0fdSIan Rogers 		file = openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600);
107531ee0fdSIan Rogers 		if (!file) {
108531ee0fdSIan Rogers 			pr_err("Failed to open for writing file \"%s\"\n", item->name);
109531ee0fdSIan Rogers 			goto err_out;
110531ee0fdSIan Rogers 		}
111531ee0fdSIan Rogers 
112531ee0fdSIan Rogers 		if (write(file, item->value, strlen(item->value)) < 0) {
113531ee0fdSIan Rogers 			pr_err("Failed to write to file \"%s\"\n", item->name);
114531ee0fdSIan Rogers 			close(file);
115531ee0fdSIan Rogers 			goto err_out;
116531ee0fdSIan Rogers 		}
117531ee0fdSIan Rogers 		close(file);
118531ee0fdSIan Rogers 	}
119531ee0fdSIan Rogers 
120531ee0fdSIan Rogers 	/* Make the PMU reading the files created above. */
121531ee0fdSIan Rogers 	hwm = perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name);
122531ee0fdSIan Rogers 	if (!hwm)
123531ee0fdSIan Rogers 		pr_err("Test hwmon creation failed\n");
124531ee0fdSIan Rogers 
125531ee0fdSIan Rogers err_out:
126531ee0fdSIan Rogers 	if (!hwm) {
127531ee0fdSIan Rogers 		test_pmu_put(dir, hwm);
128531ee0fdSIan Rogers 		if (dirfd >= 0)
129531ee0fdSIan Rogers 			close(dirfd);
130531ee0fdSIan Rogers 	}
131531ee0fdSIan Rogers 	return hwm;
132531ee0fdSIan Rogers }
133531ee0fdSIan Rogers 
134531ee0fdSIan Rogers static int do_test(size_t i, bool with_pmu, bool with_alias)
135531ee0fdSIan Rogers {
136531ee0fdSIan Rogers 	const char *test_event = with_alias ? test_events[i].alias : test_events[i].name;
137531ee0fdSIan Rogers 	struct evlist *evlist = evlist__new();
138531ee0fdSIan Rogers 	struct evsel *evsel;
139531ee0fdSIan Rogers 	struct parse_events_error err;
140531ee0fdSIan Rogers 	int ret;
141531ee0fdSIan Rogers 	char str[128];
142531ee0fdSIan Rogers 	bool found = false;
143531ee0fdSIan Rogers 
144531ee0fdSIan Rogers 	if (!evlist) {
145531ee0fdSIan Rogers 		pr_err("evlist allocation failed\n");
146531ee0fdSIan Rogers 		return TEST_FAIL;
147531ee0fdSIan Rogers 	}
148531ee0fdSIan Rogers 
149531ee0fdSIan Rogers 	if (with_pmu)
150531ee0fdSIan Rogers 		snprintf(str, sizeof(str), "/%s/", test_event);
151531ee0fdSIan Rogers 	else
152531ee0fdSIan Rogers 		strlcpy(str, test_event, sizeof(str));
153531ee0fdSIan Rogers 
154531ee0fdSIan Rogers 	pr_debug("Testing '%s'\n", str);
155531ee0fdSIan Rogers 	parse_events_error__init(&err);
156531ee0fdSIan Rogers 	ret = parse_events(evlist, str, &err);
157531ee0fdSIan Rogers 	if (ret) {
158531ee0fdSIan Rogers 		evlist__delete(evlist);
159531ee0fdSIan Rogers 
160531ee0fdSIan Rogers 		pr_debug("FAILED %s:%d failed to parse event '%s', err %d\n",
161531ee0fdSIan Rogers 			 __FILE__, __LINE__, str, ret);
162531ee0fdSIan Rogers 		parse_events_error__print(&err, str);
163531ee0fdSIan Rogers 		ret = TEST_FAIL;
164531ee0fdSIan Rogers 		goto out;
165531ee0fdSIan Rogers 	}
166531ee0fdSIan Rogers 
167531ee0fdSIan Rogers 	ret = TEST_OK;
168531ee0fdSIan Rogers 	if (with_pmu ? (evlist->core.nr_entries != 1) : (evlist->core.nr_entries < 1)) {
169531ee0fdSIan Rogers 		pr_debug("FAILED %s:%d Unexpected number of events for '%s' of %d\n",
170531ee0fdSIan Rogers 			 __FILE__, __LINE__, str, evlist->core.nr_entries);
171531ee0fdSIan Rogers 		ret = TEST_FAIL;
172531ee0fdSIan Rogers 		goto out;
173531ee0fdSIan Rogers 	}
174531ee0fdSIan Rogers 
175531ee0fdSIan Rogers 	evlist__for_each_entry(evlist, evsel) {
176531ee0fdSIan Rogers 		if (!perf_pmu__is_hwmon(evsel->pmu))
177531ee0fdSIan Rogers 			continue;
178531ee0fdSIan Rogers 
179531ee0fdSIan Rogers 		if (evsel->core.attr.config != (u64)test_events[i].config) {
180531ee0fdSIan Rogers 			pr_debug("FAILED %s:%d Unexpected config for '%s', %lld != %ld\n",
181531ee0fdSIan Rogers 				__FILE__, __LINE__, str,
182531ee0fdSIan Rogers 				evsel->core.attr.config,
183531ee0fdSIan Rogers 				test_events[i].config);
184531ee0fdSIan Rogers 			ret = TEST_FAIL;
185531ee0fdSIan Rogers 			goto out;
186531ee0fdSIan Rogers 		}
187531ee0fdSIan Rogers 		found = true;
188531ee0fdSIan Rogers 	}
189531ee0fdSIan Rogers 
190531ee0fdSIan Rogers 	if (!found) {
191531ee0fdSIan Rogers 		pr_debug("FAILED %s:%d Didn't find hwmon event '%s' in parsed evsels\n",
192531ee0fdSIan Rogers 			 __FILE__, __LINE__, str);
193531ee0fdSIan Rogers 		ret = TEST_FAIL;
194531ee0fdSIan Rogers 	}
195531ee0fdSIan Rogers 
196531ee0fdSIan Rogers out:
197*db26a8c9SIan Rogers 	parse_events_error__exit(&err);
198531ee0fdSIan Rogers 	evlist__delete(evlist);
199531ee0fdSIan Rogers 	return ret;
200531ee0fdSIan Rogers }
201531ee0fdSIan Rogers 
202531ee0fdSIan Rogers static int test__hwmon_pmu(bool with_pmu)
203531ee0fdSIan Rogers {
204531ee0fdSIan Rogers 	char dir[PATH_MAX];
205531ee0fdSIan Rogers 	struct perf_pmu *pmu = test_pmu_get(dir, sizeof(dir));
206531ee0fdSIan Rogers 	int ret = TEST_OK;
207531ee0fdSIan Rogers 
208531ee0fdSIan Rogers 	if (!pmu)
209531ee0fdSIan Rogers 		return TEST_FAIL;
210531ee0fdSIan Rogers 
211531ee0fdSIan Rogers 	for (size_t i = 0; i < ARRAY_SIZE(test_events); i++) {
212531ee0fdSIan Rogers 		ret = do_test(i, with_pmu, /*with_alias=*/false);
213531ee0fdSIan Rogers 
214531ee0fdSIan Rogers 		if (ret != TEST_OK)
215531ee0fdSIan Rogers 			break;
216531ee0fdSIan Rogers 
217531ee0fdSIan Rogers 		ret = do_test(i, with_pmu, /*with_alias=*/true);
218531ee0fdSIan Rogers 
219531ee0fdSIan Rogers 		if (ret != TEST_OK)
220531ee0fdSIan Rogers 			break;
221531ee0fdSIan Rogers 	}
222531ee0fdSIan Rogers 	test_pmu_put(dir, pmu);
223531ee0fdSIan Rogers 	return ret;
224531ee0fdSIan Rogers }
225531ee0fdSIan Rogers 
226531ee0fdSIan Rogers static int test__hwmon_pmu_without_pmu(struct test_suite *test __maybe_unused,
227531ee0fdSIan Rogers 				      int subtest __maybe_unused)
228531ee0fdSIan Rogers {
229531ee0fdSIan Rogers 	return test__hwmon_pmu(/*with_pmu=*/false);
230531ee0fdSIan Rogers }
231531ee0fdSIan Rogers 
232531ee0fdSIan Rogers static int test__hwmon_pmu_with_pmu(struct test_suite *test __maybe_unused,
233531ee0fdSIan Rogers 				   int subtest __maybe_unused)
234531ee0fdSIan Rogers {
235531ee0fdSIan Rogers 	return test__hwmon_pmu(/*with_pmu=*/false);
236531ee0fdSIan Rogers }
237531ee0fdSIan Rogers 
2388c329057SIan Rogers static int test__parse_hwmon_filename(struct test_suite *test __maybe_unused,
2398c329057SIan Rogers 				      int subtest __maybe_unused)
2408c329057SIan Rogers {
2418c329057SIan Rogers 	const struct hwmon_parse_test {
2428c329057SIan Rogers 		const char *filename;
2438c329057SIan Rogers 		enum hwmon_type type;
2448c329057SIan Rogers 		int number;
2458c329057SIan Rogers 		enum hwmon_item item;
2468c329057SIan Rogers 		bool alarm;
2478c329057SIan Rogers 		bool parse_ok;
2488c329057SIan Rogers 	} tests[] = {
2498c329057SIan Rogers 		{
2508c329057SIan Rogers 			.filename = "cpu0_accuracy",
2518c329057SIan Rogers 			.type = HWMON_TYPE_CPU,
2528c329057SIan Rogers 			.number = 0,
2538c329057SIan Rogers 			.item = HWMON_ITEM_ACCURACY,
2548c329057SIan Rogers 			.alarm = false,
2558c329057SIan Rogers 			.parse_ok = true,
2568c329057SIan Rogers 		},
2578c329057SIan Rogers 		{
2588c329057SIan Rogers 			.filename = "temp1_input",
2598c329057SIan Rogers 			.type = HWMON_TYPE_TEMP,
2608c329057SIan Rogers 			.number = 1,
2618c329057SIan Rogers 			.item = HWMON_ITEM_INPUT,
2628c329057SIan Rogers 			.alarm = false,
2638c329057SIan Rogers 			.parse_ok = true,
2648c329057SIan Rogers 		},
2658c329057SIan Rogers 		{
2668c329057SIan Rogers 			.filename = "fan2_vid",
2678c329057SIan Rogers 			.type = HWMON_TYPE_FAN,
2688c329057SIan Rogers 			.number = 2,
2698c329057SIan Rogers 			.item = HWMON_ITEM_VID,
2708c329057SIan Rogers 			.alarm = false,
2718c329057SIan Rogers 			.parse_ok = true,
2728c329057SIan Rogers 		},
2738c329057SIan Rogers 		{
2748c329057SIan Rogers 			.filename = "power3_crit_alarm",
2758c329057SIan Rogers 			.type = HWMON_TYPE_POWER,
2768c329057SIan Rogers 			.number = 3,
2778c329057SIan Rogers 			.item = HWMON_ITEM_CRIT,
2788c329057SIan Rogers 			.alarm = true,
2798c329057SIan Rogers 			.parse_ok = true,
2808c329057SIan Rogers 		},
2818c329057SIan Rogers 		{
2828c329057SIan Rogers 			.filename = "intrusion4_average_interval_min_alarm",
2838c329057SIan Rogers 			.type = HWMON_TYPE_INTRUSION,
2848c329057SIan Rogers 			.number = 4,
2858c329057SIan Rogers 			.item = HWMON_ITEM_AVERAGE_INTERVAL_MIN,
2868c329057SIan Rogers 			.alarm = true,
2878c329057SIan Rogers 			.parse_ok = true,
2888c329057SIan Rogers 		},
2898c329057SIan Rogers 		{
2908c329057SIan Rogers 			.filename = "badtype5_baditem",
2918c329057SIan Rogers 			.type = HWMON_TYPE_NONE,
2928c329057SIan Rogers 			.number = 5,
2938c329057SIan Rogers 			.item = HWMON_ITEM_NONE,
2948c329057SIan Rogers 			.alarm = false,
2958c329057SIan Rogers 			.parse_ok = false,
2968c329057SIan Rogers 		},
2978c329057SIan Rogers 		{
2988c329057SIan Rogers 			.filename = "humidity6_baditem",
2998c329057SIan Rogers 			.type = HWMON_TYPE_NONE,
3008c329057SIan Rogers 			.number = 6,
3018c329057SIan Rogers 			.item = HWMON_ITEM_NONE,
3028c329057SIan Rogers 			.alarm = false,
3038c329057SIan Rogers 			.parse_ok = false,
3048c329057SIan Rogers 		},
3058c329057SIan Rogers 	};
3068c329057SIan Rogers 
3078c329057SIan Rogers 	for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
3088c329057SIan Rogers 		enum hwmon_type type;
3098c329057SIan Rogers 		int number;
3108c329057SIan Rogers 		enum hwmon_item item;
3118c329057SIan Rogers 		bool alarm;
3128c329057SIan Rogers 
3138c329057SIan Rogers 		TEST_ASSERT_EQUAL("parse_hwmon_filename",
3148c329057SIan Rogers 				parse_hwmon_filename(
3158c329057SIan Rogers 					tests[i].filename,
3168c329057SIan Rogers 					&type,
3178c329057SIan Rogers 					&number,
3188c329057SIan Rogers 					&item,
3198c329057SIan Rogers 					&alarm),
3208c329057SIan Rogers 				tests[i].parse_ok
3218c329057SIan Rogers 			);
3228c329057SIan Rogers 		if (tests[i].parse_ok) {
3238c329057SIan Rogers 			TEST_ASSERT_EQUAL("parse_hwmon_filename type", type, tests[i].type);
3248c329057SIan Rogers 			TEST_ASSERT_EQUAL("parse_hwmon_filename number", number, tests[i].number);
3258c329057SIan Rogers 			TEST_ASSERT_EQUAL("parse_hwmon_filename item", item, tests[i].item);
3268c329057SIan Rogers 			TEST_ASSERT_EQUAL("parse_hwmon_filename alarm", alarm, tests[i].alarm);
3278c329057SIan Rogers 		}
3288c329057SIan Rogers 	}
3298c329057SIan Rogers 	return TEST_OK;
3308c329057SIan Rogers }
3318c329057SIan Rogers 
3328c329057SIan Rogers static struct test_case tests__hwmon_pmu[] = {
3338c329057SIan Rogers 	TEST_CASE("Basic parsing test", parse_hwmon_filename),
334531ee0fdSIan Rogers 	TEST_CASE("Parsing without PMU name", hwmon_pmu_without_pmu),
335531ee0fdSIan Rogers 	TEST_CASE("Parsing with PMU name", hwmon_pmu_with_pmu),
3368c329057SIan Rogers 	{	.name = NULL, }
3378c329057SIan Rogers };
3388c329057SIan Rogers 
3398c329057SIan Rogers struct test_suite suite__hwmon_pmu = {
3408c329057SIan Rogers 	.desc = "Hwmon PMU",
3418c329057SIan Rogers 	.test_cases = tests__hwmon_pmu,
3428c329057SIan Rogers };
343