linux/tools/perf/util/hwmon_pmu.h
Thomas Richter 888751e4d0 perf test: Fix Hwmon PMU test endianess issue
perf test 11 hwmon fails on s390 with this error

 # ./perf test -Fv 11
 --- start ---
 ---- end ----
 11.1: Basic parsing test             : Ok
 --- start ---
 Testing 'temp_test_hwmon_event1'
 Using CPUID IBM,3931,704,A01,3.7,002f
 temp_test_hwmon_event1 -> hwmon_a_test_hwmon_pmu/temp_test_hwmon_event1/
 FAILED tests/hwmon_pmu.c:189 Unexpected config for
    'temp_test_hwmon_event1', 292470092988416 != 655361
 ---- end ----
 11.2: Parsing without PMU name       : FAILED!
 --- start ---
 Testing 'hwmon_a_test_hwmon_pmu/temp_test_hwmon_event1/'
 FAILED tests/hwmon_pmu.c:189 Unexpected config for
    'hwmon_a_test_hwmon_pmu/temp_test_hwmon_event1/',
    292470092988416 != 655361
 ---- end ----
 11.3: Parsing with PMU name          : FAILED!
 #

The root cause is in member test_event::config which is initialized
to 0xA0001 or 655361. During event parsing a long list event parsing
functions are called and end up with this gdb call stack:

 #0  hwmon_pmu__config_term (hwm=0x168dfd0, attr=0x3ffffff5ee8,
	term=0x168db60, err=0x3ffffff81c8) at util/hwmon_pmu.c:623
 #1  hwmon_pmu__config_terms (pmu=0x168dfd0, attr=0x3ffffff5ee8,
	terms=0x3ffffff5ea8, err=0x3ffffff81c8) at util/hwmon_pmu.c:662
 #2  0x00000000012f870c in perf_pmu__config_terms (pmu=0x168dfd0,
	attr=0x3ffffff5ee8, terms=0x3ffffff5ea8, zero=false,
	apply_hardcoded=false, err=0x3ffffff81c8) at util/pmu.c:1519
 #3  0x00000000012f88a4 in perf_pmu__config (pmu=0x168dfd0, attr=0x3ffffff5ee8,
	head_terms=0x3ffffff5ea8, apply_hardcoded=false, err=0x3ffffff81c8)
	at util/pmu.c:1545
 #4  0x00000000012680c4 in parse_events_add_pmu (parse_state=0x3ffffff7fb8,
	list=0x168dc00, pmu=0x168dfd0, const_parsed_terms=0x3ffffff6090,
	auto_merge_stats=true, alternate_hw_config=10)
	at util/parse-events.c:1508
 #5  0x00000000012684c6 in parse_events_multi_pmu_add (parse_state=0x3ffffff7fb8,
	event_name=0x168ec10 "temp_test_hwmon_event1", hw_config=10,
	const_parsed_terms=0x0, listp=0x3ffffff6230, loc_=0x3ffffff70e0)
	at util/parse-events.c:1592
 #6  0x00000000012f0e4e in parse_events_parse (_parse_state=0x3ffffff7fb8,
	scanner=0x16878c0) at util/parse-events.y:293
 #7  0x00000000012695a0 in parse_events__scanner (str=0x3ffffff81d8
	"temp_test_hwmon_event1", input=0x0, parse_state=0x3ffffff7fb8)
	at util/parse-events.c:1867
 #8  0x000000000126a1e8 in __parse_events (evlist=0x168b580,
	str=0x3ffffff81d8 "temp_test_hwmon_event1", pmu_filter=0x0,
	err=0x3ffffff81c8, fake_pmu=false, warn_if_reordered=true,
	fake_tp=false) at util/parse-events.c:2136
 #9  0x00000000011e36aa in parse_events (evlist=0x168b580,
	str=0x3ffffff81d8 "temp_test_hwmon_event1", err=0x3ffffff81c8)
	at /root/linux/tools/perf/util/parse-events.h:41
 #10 0x00000000011e3e64 in do_test (i=0, with_pmu=false, with_alias=false)
	at tests/hwmon_pmu.c:164
 #11 0x00000000011e422c in test__hwmon_pmu (with_pmu=false)
	at tests/hwmon_pmu.c:219
 #12 0x00000000011e431c in test__hwmon_pmu_without_pmu (test=0x1610368
	<suite.hwmon_pmu>, subtest=1) at tests/hwmon_pmu.c:23

where the attr::config is set to value 292470092988416 or 0x10a0000000000
in line 625 of file ./util/hwmon_pmu.c:

   attr->config = key.type_and_num;

However member key::type_and_num is defined as union and bit field:

   union hwmon_pmu_event_key {
        long type_and_num;
        struct {
                int num :16;
                enum hwmon_type type :8;
        };
   };

s390 is big endian and Intel is little endian architecture.
The events for the hwmon dummy pmu have num = 1 or num = 2 and
type is set to HWMON_TYPE_TEMP (which is 10).
On s390 this assignes member key::type_and_num the value of
0x10a0000000000 (which is 292470092988416) as shown in above
trace output.

Fix this and export the structure/union hwmon_pmu_event_key
so the test shares the same implementation as the event parsing
functions for union and bit fields. This should avoid
endianess issues on all platforms.

Output after:
 # ./perf test -F 11
 11.1: Basic parsing test         : Ok
 11.2: Parsing without PMU name   : Ok
 11.3: Parsing with PMU name      : Ok
 #

Fixes: 531ee0fd4836 ("perf test: Add hwmon "PMU" test")
Signed-off-by: Thomas Richter <tmricht@linux.ibm.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20250131112400.568975-1-tmricht@linux.ibm.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
2025-02-04 17:22:40 -08:00

168 lines
4.4 KiB
C

/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#ifndef __HWMON_PMU_H
#define __HWMON_PMU_H
#include "pmu.h"
#include <stdbool.h>
struct list_head;
struct perf_thread_map;
/**
* enum hwmon_type:
*
* As described in Documentation/hwmon/sysfs-interface.rst hwmon events are
* defined over multiple files of the form <type><num>_<item>. This enum
* captures potential <type> values.
*
* This enum is exposed for testing.
*/
enum hwmon_type {
HWMON_TYPE_NONE,
HWMON_TYPE_CPU,
HWMON_TYPE_CURR,
HWMON_TYPE_ENERGY,
HWMON_TYPE_FAN,
HWMON_TYPE_HUMIDITY,
HWMON_TYPE_IN,
HWMON_TYPE_INTRUSION,
HWMON_TYPE_POWER,
HWMON_TYPE_PWM,
HWMON_TYPE_TEMP,
HWMON_TYPE_MAX
};
/**
* enum hwmon_item:
*
* Similar to enum hwmon_type but describes the item part of a a sysfs filename.
*
* This enum is exposed for testing.
*/
enum hwmon_item {
HWMON_ITEM_NONE,
HWMON_ITEM_ACCURACY,
HWMON_ITEM_ALARM,
HWMON_ITEM_AUTO_CHANNELS_TEMP,
HWMON_ITEM_AVERAGE,
HWMON_ITEM_AVERAGE_HIGHEST,
HWMON_ITEM_AVERAGE_INTERVAL,
HWMON_ITEM_AVERAGE_INTERVAL_MAX,
HWMON_ITEM_AVERAGE_INTERVAL_MIN,
HWMON_ITEM_AVERAGE_LOWEST,
HWMON_ITEM_AVERAGE_MAX,
HWMON_ITEM_AVERAGE_MIN,
HWMON_ITEM_BEEP,
HWMON_ITEM_CAP,
HWMON_ITEM_CAP_HYST,
HWMON_ITEM_CAP_MAX,
HWMON_ITEM_CAP_MIN,
HWMON_ITEM_CRIT,
HWMON_ITEM_CRIT_HYST,
HWMON_ITEM_DIV,
HWMON_ITEM_EMERGENCY,
HWMON_ITEM_EMERGENCY_HIST,
HWMON_ITEM_ENABLE,
HWMON_ITEM_FAULT,
HWMON_ITEM_FREQ,
HWMON_ITEM_HIGHEST,
HWMON_ITEM_INPUT,
HWMON_ITEM_LABEL,
HWMON_ITEM_LCRIT,
HWMON_ITEM_LCRIT_HYST,
HWMON_ITEM_LOWEST,
HWMON_ITEM_MAX,
HWMON_ITEM_MAX_HYST,
HWMON_ITEM_MIN,
HWMON_ITEM_MIN_HYST,
HWMON_ITEM_MOD,
HWMON_ITEM_OFFSET,
HWMON_ITEM_PULSES,
HWMON_ITEM_RATED_MAX,
HWMON_ITEM_RATED_MIN,
HWMON_ITEM_RESET_HISTORY,
HWMON_ITEM_TARGET,
HWMON_ITEM_TYPE,
HWMON_ITEM_VID,
HWMON_ITEM__MAX,
};
/**
* union hwmon_pmu_event_key: Key for hwmon_pmu->events as such each key
* represents an event.
* union is exposed for testing to ensure problems are avoided on big
* endian machines.
*
* Related hwmon files start <type><number> that this key represents.
*/
union hwmon_pmu_event_key {
long type_and_num;
struct {
int num :16;
enum hwmon_type type :8;
};
};
bool perf_pmu__is_hwmon(const struct perf_pmu *pmu);
bool evsel__is_hwmon(const struct evsel *evsel);
/**
* parse_hwmon_filename() - Parse filename into constituent parts.
*
* @filename: To be parsed, of the form <type><number>_<item>.
* @type: The type defined from the parsed file name.
* @number: The number of the type, for example there may be more than 1 fan.
* @item: A hwmon <type><number> may have multiple associated items.
* @alarm: Is the filename for an alarm value?
*
* An example of a hwmon filename is "temp1_input". The type is temp for a
* temperature value. The number is 1. The item within the file is an input
* value - the temperature itself. This file doesn't contain an alarm value.
*
* Exposed for testing.
*/
bool parse_hwmon_filename(const char *filename,
enum hwmon_type *type,
int *number,
enum hwmon_item *item,
bool *alarm);
/**
* hwmon_pmu__new() - Allocate and construct a hwmon PMU.
*
* @pmus: The list of PMUs to be added to.
* @hwmon_dir: An O_DIRECTORY file descriptor for a hwmon directory.
* @sysfs_name: Name of the hwmon sysfs directory like hwmon0.
* @name: The contents of the "name" file in the hwmon directory.
*
* Exposed for testing. Regular construction should happen via
* perf_pmus__read_hwmon_pmus.
*/
struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir,
const char *sysfs_name, const char *name);
void hwmon_pmu__exit(struct perf_pmu *pmu);
int hwmon_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb);
size_t hwmon_pmu__num_events(struct perf_pmu *pmu);
bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name);
int hwmon_pmu__config_terms(const struct perf_pmu *pmu,
struct perf_event_attr *attr,
struct parse_events_terms *terms,
struct parse_events_error *err);
int hwmon_pmu__check_alias(struct parse_events_terms *terms, struct perf_pmu_info *info,
struct parse_events_error *err);
int perf_pmus__read_hwmon_pmus(struct list_head *pmus);
int evsel__hwmon_pmu_open(struct evsel *evsel,
struct perf_thread_map *threads,
int start_cpu_map_idx, int end_cpu_map_idx);
int evsel__hwmon_pmu_read(struct evsel *evsel, int cpu_map_idx, int thread);
#endif /* __HWMON_PMU_H */