Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2#include "math.h"
   3#include "parse-events.h"
   4#include "pmu.h"
 
   5#include "tests.h"
   6#include <errno.h>
   7#include <stdio.h>
   8#include <linux/kernel.h>
   9#include <linux/zalloc.h>
  10#include "debug.h"
  11#include "../pmu-events/pmu-events.h"
  12#include <perf/evlist.h>
  13#include "util/evlist.h"
  14#include "util/expr.h"
  15#include "util/hashmap.h"
  16#include "util/parse-events.h"
  17#include "metricgroup.h"
  18#include "stat.h"
  19
  20struct perf_pmu_test_event {
  21	/* used for matching against events from generated pmu-events.c */
  22	struct pmu_event event;
  23
  24	/* used for matching against event aliases */
  25	/* extra events for aliases */
  26	const char *alias_str;
  27
  28	/*
  29	 * Note: For when PublicDescription does not exist in the JSON, we
  30	 * will have no long_desc in pmu_event.long_desc, but long_desc may
  31	 * be set in the alias.
  32	 */
  33	const char *alias_long_desc;
  34
  35	/* PMU which we should match against */
  36	const char *matching_pmu;
  37};
  38
  39struct perf_pmu_test_pmu {
  40	struct perf_pmu pmu;
  41	struct perf_pmu_test_event const *aliases[10];
  42};
  43
  44static const struct perf_pmu_test_event bp_l1_btb_correct = {
  45	.event = {
 
  46		.name = "bp_l1_btb_correct",
  47		.event = "event=0x8a",
  48		.desc = "L1 BTB Correction",
  49		.topic = "branch",
  50	},
  51	.alias_str = "event=0x8a",
  52	.alias_long_desc = "L1 BTB Correction",
  53};
  54
  55static const struct perf_pmu_test_event bp_l2_btb_correct = {
  56	.event = {
 
  57		.name = "bp_l2_btb_correct",
  58		.event = "event=0x8b",
  59		.desc = "L2 BTB Correction",
  60		.topic = "branch",
  61	},
  62	.alias_str = "event=0x8b",
  63	.alias_long_desc = "L2 BTB Correction",
  64};
  65
  66static const struct perf_pmu_test_event segment_reg_loads_any = {
  67	.event = {
 
  68		.name = "segment_reg_loads.any",
  69		.event = "event=0x6,period=200000,umask=0x80",
  70		.desc = "Number of segment register loads",
  71		.topic = "other",
  72	},
  73	.alias_str = "event=0x6,period=0x30d40,umask=0x80",
  74	.alias_long_desc = "Number of segment register loads",
  75};
  76
  77static const struct perf_pmu_test_event dispatch_blocked_any = {
  78	.event = {
 
  79		.name = "dispatch_blocked.any",
  80		.event = "event=0x9,period=200000,umask=0x20",
  81		.desc = "Memory cluster signals to block micro-op dispatch for any reason",
  82		.topic = "other",
  83	},
  84	.alias_str = "event=0x9,period=0x30d40,umask=0x20",
  85	.alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
  86};
  87
  88static const struct perf_pmu_test_event eist_trans = {
  89	.event = {
 
  90		.name = "eist_trans",
  91		.event = "event=0x3a,period=200000,umask=0x0",
  92		.desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
  93		.topic = "other",
  94	},
  95	.alias_str = "event=0x3a,period=0x30d40,umask=0",
  96	.alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
  97};
  98
  99static const struct perf_pmu_test_event l3_cache_rd = {
 100	.event = {
 
 101		.name = "l3_cache_rd",
 102		.event = "event=0x40",
 103		.desc = "L3 cache access, read",
 104		.long_desc = "Attributable Level 3 cache access, read",
 105		.topic = "cache",
 106	},
 107	.alias_str = "event=0x40",
 108	.alias_long_desc = "Attributable Level 3 cache access, read",
 109};
 110
 111static const struct perf_pmu_test_event *core_events[] = {
 112	&bp_l1_btb_correct,
 113	&bp_l2_btb_correct,
 114	&segment_reg_loads_any,
 115	&dispatch_blocked_any,
 116	&eist_trans,
 117	&l3_cache_rd,
 118	NULL
 119};
 120
 121static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
 122	.event = {
 123		.name = "uncore_hisi_ddrc.flux_wcmd",
 124		.event = "event=0x2",
 125		.desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
 126		.topic = "uncore",
 127		.long_desc = "DDRC write commands",
 128		.pmu = "hisi_sccl,ddrc",
 129	},
 130	.alias_str = "event=0x2",
 131	.alias_long_desc = "DDRC write commands",
 132	.matching_pmu = "hisi_sccl1_ddrc2",
 133};
 134
 135static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
 136	.event = {
 137		.name = "unc_cbo_xsnp_response.miss_eviction",
 138		.event = "event=0x22,umask=0x81",
 139		.desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core. Unit: uncore_cbox ",
 140		.topic = "uncore",
 141		.long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
 142		.pmu = "uncore_cbox",
 143	},
 144	.alias_str = "event=0x22,umask=0x81",
 145	.alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
 146	.matching_pmu = "uncore_cbox_0",
 147};
 148
 149static const struct perf_pmu_test_event uncore_hyphen = {
 150	.event = {
 151		.name = "event-hyphen",
 152		.event = "event=0xe0,umask=0x00",
 153		.desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox ",
 154		.topic = "uncore",
 155		.long_desc = "UNC_CBO_HYPHEN",
 156		.pmu = "uncore_cbox",
 157	},
 158	.alias_str = "event=0xe0,umask=0",
 159	.alias_long_desc = "UNC_CBO_HYPHEN",
 160	.matching_pmu = "uncore_cbox_0",
 161};
 162
 163static const struct perf_pmu_test_event uncore_two_hyph = {
 164	.event = {
 165		.name = "event-two-hyph",
 166		.event = "event=0xc0,umask=0x00",
 167		.desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ",
 168		.topic = "uncore",
 169		.long_desc = "UNC_CBO_TWO_HYPH",
 170		.pmu = "uncore_cbox",
 171	},
 172	.alias_str = "event=0xc0,umask=0",
 173	.alias_long_desc = "UNC_CBO_TWO_HYPH",
 174	.matching_pmu = "uncore_cbox_0",
 175};
 176
 177static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
 178	.event = {
 179		.name = "uncore_hisi_l3c.rd_hit_cpipe",
 180		.event = "event=0x7",
 181		.desc = "Total read hits. Unit: hisi_sccl,l3c ",
 182		.topic = "uncore",
 183		.long_desc = "Total read hits",
 184		.pmu = "hisi_sccl,l3c",
 185	},
 186	.alias_str = "event=0x7",
 187	.alias_long_desc = "Total read hits",
 188	.matching_pmu = "hisi_sccl3_l3c7",
 189};
 190
 191static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
 192	.event = {
 193		.name = "uncore_imc_free_running.cache_miss",
 194		.event = "event=0x12",
 195		.desc = "Total cache misses. Unit: uncore_imc_free_running ",
 196		.topic = "uncore",
 197		.long_desc = "Total cache misses",
 198		.pmu = "uncore_imc_free_running",
 199	},
 200	.alias_str = "event=0x12",
 201	.alias_long_desc = "Total cache misses",
 202	.matching_pmu = "uncore_imc_free_running_0",
 203};
 204
 205static const struct perf_pmu_test_event uncore_imc_cache_hits = {
 206	.event = {
 207		.name = "uncore_imc.cache_hits",
 208		.event = "event=0x34",
 209		.desc = "Total cache hits. Unit: uncore_imc ",
 210		.topic = "uncore",
 211		.long_desc = "Total cache hits",
 212		.pmu = "uncore_imc",
 213	},
 214	.alias_str = "event=0x34",
 215	.alias_long_desc = "Total cache hits",
 216	.matching_pmu = "uncore_imc_0",
 217};
 218
 219static const struct perf_pmu_test_event *uncore_events[] = {
 220	&uncore_hisi_ddrc_flux_wcmd,
 221	&unc_cbo_xsnp_response_miss_eviction,
 222	&uncore_hyphen,
 223	&uncore_two_hyph,
 224	&uncore_hisi_l3c_rd_hit_cpipe,
 225	&uncore_imc_free_running_cache_miss,
 226	&uncore_imc_cache_hits,
 227	NULL
 228};
 229
 230static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
 231	.event = {
 232		.name = "sys_ddr_pmu.write_cycles",
 233		.event = "event=0x2b",
 234		.desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
 235		.topic = "uncore",
 236		.pmu = "uncore_sys_ddr_pmu",
 237		.compat = "v8",
 238	},
 239	.alias_str = "event=0x2b",
 240	.alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
 241	.matching_pmu = "uncore_sys_ddr_pmu",
 242};
 243
 244static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
 245	.event = {
 246		.name = "sys_ccn_pmu.read_cycles",
 247		.event = "config=0x2c",
 248		.desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
 249		.topic = "uncore",
 250		.pmu = "uncore_sys_ccn_pmu",
 251		.compat = "0x01",
 252	},
 253	.alias_str = "config=0x2c",
 254	.alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
 255	.matching_pmu = "uncore_sys_ccn_pmu",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 256};
 257
 258static const struct perf_pmu_test_event *sys_events[] = {
 259	&sys_ddr_pmu_write_cycles,
 260	&sys_ccn_pmu_read_cycles,
 
 261	NULL
 262};
 263
 264static bool is_same(const char *reference, const char *test)
 265{
 266	if (!reference && !test)
 267		return true;
 268
 269	if (reference && !test)
 270		return false;
 271
 272	if (!reference && test)
 273		return false;
 274
 275	return !strcmp(reference, test);
 276}
 277
 278static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
 279{
 280	if (!is_same(e1->name, e2->name)) {
 281		pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
 282			  e1->name, e1->name, e2->name);
 283		return -1;
 284	}
 285
 286	if (!is_same(e1->compat, e2->compat)) {
 287		pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
 288			  e1->name, e1->compat, e2->compat);
 289		return -1;
 290	}
 291
 292	if (!is_same(e1->event, e2->event)) {
 293		pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
 294			  e1->name, e1->event, e2->event);
 295		return -1;
 296	}
 297
 298	if (!is_same(e1->desc, e2->desc)) {
 299		pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
 300			  e1->name, e1->desc, e2->desc);
 301		return -1;
 302	}
 303
 304	if (!is_same(e1->topic, e2->topic)) {
 305		pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
 306			  e1->name, e1->topic, e2->topic);
 307		return -1;
 308	}
 309
 310	if (!is_same(e1->long_desc, e2->long_desc)) {
 311		pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
 312			  e1->name, e1->long_desc, e2->long_desc);
 313		return -1;
 314	}
 315
 316	if (!is_same(e1->pmu, e2->pmu)) {
 317		pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
 318			  e1->name, e1->pmu, e2->pmu);
 319		return -1;
 320	}
 321
 322	if (!is_same(e1->unit, e2->unit)) {
 323		pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
 324			  e1->name, e1->unit, e2->unit);
 325		return -1;
 326	}
 327
 328	if (!is_same(e1->perpkg, e2->perpkg)) {
 329		pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
 330			  e1->name, e1->perpkg, e2->perpkg);
 331		return -1;
 332	}
 333
 334	if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
 335		pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
 336			  e1->name, e1->aggr_mode, e2->aggr_mode);
 337		return -1;
 338	}
 339
 340	if (!is_same(e1->metric_expr, e2->metric_expr)) {
 341		pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
 342			  e1->name, e1->metric_expr, e2->metric_expr);
 343		return -1;
 344	}
 345
 346	if (!is_same(e1->metric_name, e2->metric_name)) {
 347		pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
 348			  e1->name,	e1->metric_name, e2->metric_name);
 349		return -1;
 350	}
 351
 352	if (!is_same(e1->metric_group, e2->metric_group)) {
 353		pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
 354			  e1->name, e1->metric_group, e2->metric_group);
 355		return -1;
 356	}
 357
 358	if (!is_same(e1->deprecated, e2->deprecated)) {
 359		pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
 360			  e1->name, e1->deprecated, e2->deprecated);
 361		return -1;
 362	}
 363
 364	if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
 365		pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
 366			  e1->name, e1->metric_constraint, e2->metric_constraint);
 367		return -1;
 368	}
 369
 370	return 0;
 371}
 372
 373static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
 374				struct perf_pmu_test_event const *test_event,
 375				char const *pmu_name)
 376{
 377	struct pmu_event const *event = &test_event->event;
 378
 379	/* An alias was found, ensure everything is in order */
 380	if (!is_same(alias->name, event->name)) {
 381		pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
 382			  pmu_name, alias->name, event->name);
 383		return -1;
 384	}
 385
 386	if (!is_same(alias->desc, event->desc)) {
 387		pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
 388			  pmu_name, alias->desc, event->desc);
 389		return -1;
 390	}
 391
 392	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
 393		pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
 394			  pmu_name, alias->long_desc,
 395			  test_event->alias_long_desc);
 396		return -1;
 397	}
 398
 399	if (!is_same(alias->topic, event->topic)) {
 400		pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
 401			  pmu_name, alias->topic, event->topic);
 402		return -1;
 403	}
 404
 405	if (!is_same(alias->str, test_event->alias_str)) {
 406		pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
 407			  pmu_name, alias->str, test_event->alias_str);
 408		return -1;
 409	}
 410
 411	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
 412		pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
 413			  pmu_name, alias->str, test_event->alias_long_desc);
 414		return -1;
 415	}
 416
 417
 418	if (!is_same(alias->pmu_name, test_event->event.pmu)) {
 419		pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
 420			  pmu_name, alias->pmu_name, test_event->event.pmu);
 421		return -1;
 422	}
 423
 424	return 0;
 425}
 426
 427static int test__pmu_event_table_core_callback(const struct pmu_event *pe,
 428					       const struct pmu_events_table *table __maybe_unused,
 429					       void *data)
 430{
 431	int *map_events = data;
 432	struct perf_pmu_test_event const **test_event_table;
 433	bool found = false;
 434
 435	if (!pe->name)
 436		return 0;
 437
 438	if (pe->pmu)
 439		test_event_table = &uncore_events[0];
 440	else
 441		test_event_table = &core_events[0];
 442
 443	for (; *test_event_table; test_event_table++) {
 444		struct perf_pmu_test_event const *test_event = *test_event_table;
 445		struct pmu_event const *event = &test_event->event;
 446
 447		if (strcmp(pe->name, event->name))
 448			continue;
 449		found = true;
 450		(*map_events)++;
 451
 452		if (compare_pmu_events(pe, event))
 453			return -1;
 454
 455		pr_debug("testing event table %s: pass\n", pe->name);
 456	}
 457	if (!found) {
 458		pr_err("testing event table: could not find event %s\n", pe->name);
 459		return -1;
 460	}
 461	return 0;
 462}
 463
 464static int test__pmu_event_table_sys_callback(const struct pmu_event *pe,
 465					      const struct pmu_events_table *table __maybe_unused,
 466					      void *data)
 467{
 468	int *map_events = data;
 469	struct perf_pmu_test_event const **test_event_table;
 470	bool found = false;
 471
 472	test_event_table = &sys_events[0];
 473
 474	for (; *test_event_table; test_event_table++) {
 475		struct perf_pmu_test_event const *test_event = *test_event_table;
 476		struct pmu_event const *event = &test_event->event;
 477
 478		if (strcmp(pe->name, event->name))
 479			continue;
 480		found = true;
 481		(*map_events)++;
 482
 483		if (compare_pmu_events(pe, event))
 484			return TEST_FAIL;
 485
 486		pr_debug("testing sys event table %s: pass\n", pe->name);
 487	}
 488	if (!found) {
 489		pr_debug("testing sys event table: could not find event %s\n", pe->name);
 490		return TEST_FAIL;
 491	}
 492	return TEST_OK;
 493}
 494
 495/* Verify generated events from pmu-events.c are as expected */
 496static int test__pmu_event_table(struct test_suite *test __maybe_unused,
 497				 int subtest __maybe_unused)
 498{
 499	const struct pmu_events_table *sys_event_table = find_sys_events_table("pme_test_soc_sys");
 
 500	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
 501	int map_events = 0, expected_events, err;
 502
 503	/* ignore 3x sentinels */
 504	expected_events = ARRAY_SIZE(core_events) +
 505			  ARRAY_SIZE(uncore_events) +
 506			  ARRAY_SIZE(sys_events) - 3;
 507
 508	if (!table || !sys_event_table)
 509		return -1;
 510
 511	err = pmu_events_table_for_each_event(table, test__pmu_event_table_core_callback,
 
 512					      &map_events);
 513	if (err)
 514		return err;
 515
 516	err = pmu_events_table_for_each_event(sys_event_table, test__pmu_event_table_sys_callback,
 
 517					      &map_events);
 518	if (err)
 519		return err;
 520
 521	if (map_events != expected_events) {
 522		pr_err("testing event table: found %d, but expected %d\n",
 523		       map_events, expected_events);
 524		return TEST_FAIL;
 525	}
 526
 527	return 0;
 528}
 529
 530static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
 531{
 532	struct perf_pmu_alias *alias;
 
 533
 534	list_for_each_entry(alias, aliases, list)
 535		if (!strcmp(test_event, alias->name))
 536			return alias;
 537
 538	return NULL;
 
 
 
 
 
 539}
 540
 541/* Verify aliases are as expected */
 542static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
 543{
 544	struct perf_pmu_test_event const **test_event_table;
 545	struct perf_pmu *pmu;
 546	LIST_HEAD(aliases);
 547	int res = 0;
 548	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
 549	struct perf_pmu_alias *a, *tmp;
 550
 551	if (!table)
 552		return -1;
 553
 554	test_event_table = &core_events[0];
 555
 556	pmu = zalloc(sizeof(*pmu));
 557	if (!pmu)
 558		return -1;
 559
 560	pmu->name = pmu_name;
 561
 562	pmu_add_cpu_aliases_table(&aliases, pmu, table);
 563
 
 
 
 
 
 
 
 
 
 
 
 
 
 564	for (; *test_event_table; test_event_table++) {
 565		struct perf_pmu_test_event const *test_event = *test_event_table;
 566		struct pmu_event const *event = &test_event->event;
 567		struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
 568
 569		if (!alias) {
 570			pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
 571				  pmu_name, event->name);
 572			res = -1;
 573			break;
 574		}
 575
 576		if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
 577			res = -1;
 578			break;
 579		}
 580
 581		(*count)++;
 582		pr_debug2("testing aliases core PMU %s: matched event %s\n",
 583			  pmu_name, alias->name);
 584	}
 
 585
 586	list_for_each_entry_safe(a, tmp, &aliases, list) {
 587		list_del(&a->list);
 588		perf_pmu_free_alias(a);
 589	}
 590	free(pmu);
 591	return res;
 592}
 593
 594static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
 595{
 596	int alias_count = 0, to_match_count = 0, matched_count = 0;
 597	struct perf_pmu_test_event const **table;
 598	struct perf_pmu *pmu = &test_pmu->pmu;
 599	const char *pmu_name = pmu->name;
 600	struct perf_pmu_alias *a, *tmp, *alias;
 601	const struct pmu_events_table *events_table;
 602	LIST_HEAD(aliases);
 603	int res = 0;
 604
 605	events_table = find_core_events_table("testarch", "testcpu");
 606	if (!events_table)
 607		return -1;
 608	pmu_add_cpu_aliases_table(&aliases, pmu, events_table);
 609	pmu_add_sys_aliases(&aliases, pmu);
 
 
 
 610
 611	/* Count how many aliases we generated */
 612	list_for_each_entry(alias, &aliases, list)
 613		alias_count++;
 614
 615	/* Count how many aliases we expect from the known table */
 616	for (table = &test_pmu->aliases[0]; *table; table++)
 617		to_match_count++;
 618
 619	if (alias_count != to_match_count) {
 620		pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
 621			 pmu_name, to_match_count, alias_count);
 622		res = -1;
 623		goto out;
 624	}
 625
 626	list_for_each_entry(alias, &aliases, list) {
 627		bool matched = false;
 628
 629		for (table = &test_pmu->aliases[0]; *table; table++) {
 630			struct perf_pmu_test_event const *test_event = *table;
 631			struct pmu_event const *event = &test_event->event;
 632
 633			if (!strcmp(event->name, alias->name)) {
 634				if (compare_alias_to_test_event(alias,
 635							test_event,
 636							pmu_name)) {
 637					continue;
 638				}
 639				matched = true;
 640				matched_count++;
 641			}
 642		}
 643
 644		if (matched == false) {
 
 
 
 645			pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
 646				 pmu_name, alias->name);
 647			res = -1;
 648			goto out;
 649		}
 650	}
 651
 652	if (alias_count != matched_count) {
 653		pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
 654			 pmu_name, matched_count, alias_count);
 655		res = -1;
 656	}
 657
 658out:
 659	list_for_each_entry_safe(a, tmp, &aliases, list) {
 660		list_del(&a->list);
 661		perf_pmu_free_alias(a);
 662	}
 663	return res;
 664}
 665
 666static struct perf_pmu_test_pmu test_pmus[] = {
 667	{
 668		.pmu = {
 669			.name = (char *)"hisi_sccl1_ddrc2",
 670			.is_uncore = 1,
 671		},
 672		.aliases = {
 673			&uncore_hisi_ddrc_flux_wcmd,
 674		},
 675	},
 676	{
 677		.pmu = {
 678			.name = (char *)"uncore_cbox_0",
 679			.is_uncore = 1,
 680		},
 681		.aliases = {
 682			&unc_cbo_xsnp_response_miss_eviction,
 683			&uncore_hyphen,
 684			&uncore_two_hyph,
 685		},
 686	},
 687	{
 688		.pmu = {
 689			.name = (char *)"hisi_sccl3_l3c7",
 690			.is_uncore = 1,
 691		},
 692		.aliases = {
 693			&uncore_hisi_l3c_rd_hit_cpipe,
 694		},
 695	},
 696	{
 697		.pmu = {
 698			.name = (char *)"uncore_imc_free_running_0",
 699			.is_uncore = 1,
 700		},
 701		.aliases = {
 702			&uncore_imc_free_running_cache_miss,
 703		},
 704	},
 705	{
 706		.pmu = {
 707			.name = (char *)"uncore_imc_0",
 708			.is_uncore = 1,
 709		},
 710		.aliases = {
 711			&uncore_imc_cache_hits,
 712		},
 713	},
 714	{
 715		.pmu = {
 716			.name = (char *)"uncore_sys_ddr_pmu0",
 717			.is_uncore = 1,
 718			.id = (char *)"v8",
 719		},
 720		.aliases = {
 721			&sys_ddr_pmu_write_cycles,
 722		},
 723	},
 724	{
 725		.pmu = {
 726			.name = (char *)"uncore_sys_ccn_pmu4",
 727			.is_uncore = 1,
 728			.id = (char *)"0x01",
 729		},
 730		.aliases = {
 731			&sys_ccn_pmu_read_cycles,
 732		},
 733	},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 734};
 735
 736/* Test that aliases generated are as expected */
 737static int test__aliases(struct test_suite *test __maybe_unused,
 738			int subtest __maybe_unused)
 739{
 740	struct perf_pmu *pmu = NULL;
 741	unsigned long i;
 742
 743	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
 744		int count = 0;
 745
 746		if (!is_pmu_core(pmu->name))
 747			continue;
 748
 749		if (list_empty(&pmu->format)) {
 750			pr_debug2("skipping testing core PMU %s\n", pmu->name);
 751			continue;
 752		}
 753
 754		if (__test_core_pmu_event_aliases(pmu->name, &count)) {
 755			pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
 756			return -1;
 757		}
 758
 759		if (count == 0) {
 760			pr_debug("testing core PMU %s aliases: no events to match\n",
 761				  pmu->name);
 762			return -1;
 763		}
 764
 765		pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
 766	}
 767
 768	for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
 769		int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
 
 
 
 
 770
 
 771		if (res)
 772			return res;
 773	}
 774
 775	return 0;
 776}
 777
 778static bool is_number(const char *str)
 779{
 780	char *end_ptr;
 781	double v;
 782
 783	errno = 0;
 784	v = strtod(str, &end_ptr);
 785	(void)v; // We're not interested in this value, only if it is valid
 786	return errno == 0 && end_ptr != str;
 787}
 788
 789static int check_parse_id(const char *id, struct parse_events_error *error,
 790			  struct perf_pmu *fake_pmu)
 791{
 792	struct evlist *evlist;
 793	int ret;
 794	char *dup, *cur;
 795
 796	/* Numbers are always valid. */
 797	if (is_number(id))
 798		return 0;
 799
 800	evlist = evlist__new();
 801	if (!evlist)
 802		return -ENOMEM;
 803
 804	dup = strdup(id);
 805	if (!dup)
 806		return -ENOMEM;
 807
 808	for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
 809		*cur = '/';
 810
 811	if (fake_pmu) {
 812		/*
 813		 * Every call to __parse_events will try to initialize the PMU
 814		 * state from sysfs and then clean it up at the end. Reset the
 815		 * PMU events to the test state so that we don't pick up
 816		 * erroneous prefixes and suffixes.
 817		 */
 818		perf_pmu__test_parse_init();
 819	}
 820	ret = __parse_events(evlist, dup, error, fake_pmu);
 821	free(dup);
 822
 823	evlist__delete(evlist);
 824	return ret;
 825}
 826
 827static int check_parse_fake(const char *id)
 828{
 829	struct parse_events_error error;
 830	int ret;
 831
 832	parse_events_error__init(&error);
 833	ret = check_parse_id(id, &error, &perf_pmu__fake);
 834	parse_events_error__exit(&error);
 835	return ret;
 836}
 837
 838struct metric {
 839	struct list_head list;
 840	struct metric_ref metric_ref;
 841};
 842
 843static int test__parsing_callback(const struct pmu_event *pe, const struct pmu_events_table *table,
 
 844				  void *data)
 845{
 846	int *failures = data;
 847	int k;
 848	struct evlist *evlist;
 849	struct perf_cpu_map *cpus;
 850	struct runtime_stat st;
 851	struct evsel *evsel;
 852	struct rblist metric_events = {
 853		.nr_entries = 0,
 854	};
 855	int err = 0;
 856
 857	if (!pe->metric_expr)
 858		return 0;
 859
 860	pr_debug("Found metric '%s'\n", pe->metric_name);
 861	(*failures)++;
 862
 863	/*
 864	 * We need to prepare evlist for stat mode running on CPU 0
 865	 * because that's where all the stats are going to be created.
 866	 */
 867	evlist = evlist__new();
 868	if (!evlist)
 869		return -ENOMEM;
 870
 871	cpus = perf_cpu_map__new("0");
 872	if (!cpus) {
 873		evlist__delete(evlist);
 874		return -ENOMEM;
 875	}
 876
 877	perf_evlist__set_maps(&evlist->core, cpus, NULL);
 878	runtime_stat__init(&st);
 879
 880	err = metricgroup__parse_groups_test(evlist, table, pe->metric_name,
 881					     false, false,
 882					     &metric_events);
 883	if (err) {
 884		if (!strcmp(pe->metric_name, "M1") || !strcmp(pe->metric_name, "M2") ||
 885		    !strcmp(pe->metric_name, "M3")) {
 886			(*failures)--;
 887			pr_debug("Expected broken metric %s skipping\n", pe->metric_name);
 888			err = 0;
 889		}
 890		goto out_err;
 891	}
 892
 893	err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
 894	if (err)
 895		goto out_err;
 896	/*
 897	 * Add all ids with a made up value. The value may trigger divide by
 898	 * zero when subtracted and so try to make them unique.
 899	 */
 900	k = 1;
 901	perf_stat__reset_shadow_stats();
 902	evlist__for_each_entry(evlist, evsel) {
 903		perf_stat__update_shadow_stats(evsel, k, 0, &st);
 904		if (!strcmp(evsel->name, "duration_time"))
 905			update_stats(&walltime_nsecs_stats, k);
 906		k++;
 907	}
 908	evlist__for_each_entry(evlist, evsel) {
 909		struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
 910
 911		if (me != NULL) {
 912			struct metric_expr *mexp;
 913
 914			list_for_each_entry (mexp, &me->head, nd) {
 915				if (strcmp(mexp->metric_name, pe->metric_name))
 916					continue;
 917				pr_debug("Result %f\n", test_generic_metric(mexp, 0, &st));
 918				err = 0;
 919				(*failures)--;
 920				goto out_err;
 921			}
 922		}
 923	}
 924	pr_debug("Didn't find parsed metric %s", pe->metric_name);
 925	err = 1;
 926out_err:
 927	if (err)
 928		pr_debug("Broken metric %s\n", pe->metric_name);
 929
 930	/* ... cleanup. */
 931	metricgroup__rblist_exit(&metric_events);
 932	runtime_stat__exit(&st);
 933	evlist__free_stats(evlist);
 934	perf_cpu_map__put(cpus);
 935	evlist__delete(evlist);
 936	return err;
 937}
 938
 939static int test__parsing(struct test_suite *test __maybe_unused,
 940			 int subtest __maybe_unused)
 941{
 942	int failures = 0;
 943
 944	pmu_for_each_core_event(test__parsing_callback, &failures);
 945	pmu_for_each_sys_event(test__parsing_callback, &failures);
 946
 947	return failures == 0 ? TEST_OK : TEST_FAIL;
 948}
 949
 950struct test_metric {
 951	const char *str;
 952};
 953
 954static struct test_metric metrics[] = {
 955	{ "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
 956	{ "imx8_ddr0@read\\-cycles@ * 4 * 4", },
 957	{ "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
 958	{ "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
 959	{ "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
 960};
 961
 962static int metric_parse_fake(const char *metric_name, const char *str)
 963{
 964	struct expr_parse_ctx *ctx;
 965	struct hashmap_entry *cur;
 966	double result;
 967	int ret = -1;
 968	size_t bkt;
 969	int i;
 970
 971	pr_debug("parsing '%s': '%s'\n", metric_name, str);
 972
 973	ctx = expr__ctx_new();
 974	if (!ctx) {
 975		pr_debug("expr__ctx_new failed");
 976		return TEST_FAIL;
 977	}
 
 978	if (expr__find_ids(str, NULL, ctx) < 0) {
 979		pr_err("expr__find_ids failed\n");
 980		return -1;
 981	}
 982
 983	/*
 984	 * Add all ids with a made up value. The value may
 985	 * trigger divide by zero when subtracted and so try to
 986	 * make them unique.
 987	 */
 988	i = 1;
 989	hashmap__for_each_entry(ctx->ids, cur, bkt)
 990		expr__add_id_val(ctx, strdup(cur->pkey), i++);
 991
 992	hashmap__for_each_entry(ctx->ids, cur, bkt) {
 993		if (check_parse_fake(cur->pkey)) {
 994			pr_err("check_parse_fake failed\n");
 995			goto out;
 996		}
 997	}
 998
 999	ret = 0;
1000	if (expr__parse(&result, ctx, str)) {
1001		/*
1002		 * Parsing failed, make numbers go from large to small which can
1003		 * resolve divide by zero issues.
1004		 */
1005		i = 1024;
1006		hashmap__for_each_entry(ctx->ids, cur, bkt)
1007			expr__add_id_val(ctx, strdup(cur->pkey), i--);
1008		if (expr__parse(&result, ctx, str)) {
1009			pr_err("expr__parse failed for %s\n", metric_name);
1010			/* The following have hard to avoid divide by zero. */
1011			if (!strcmp(metric_name, "tma_clears_resteers") ||
1012			    !strcmp(metric_name, "tma_mispredicts_resteers"))
1013				ret = 0;
1014			else
1015				ret = -1;
1016		}
1017	}
1018
1019out:
1020	expr__ctx_free(ctx);
1021	return ret;
1022}
1023
1024static int test__parsing_fake_callback(const struct pmu_event *pe,
1025				       const struct pmu_events_table *table __maybe_unused,
1026				       void *data __maybe_unused)
1027{
1028	if (!pe->metric_expr)
1029		return 0;
1030
1031	return metric_parse_fake(pe->metric_name, pe->metric_expr);
1032}
1033
1034/*
1035 * Parse all the metrics for current architecture,
1036 * or all defined cpus via the 'fake_pmu'
1037 * in parse_events.
1038 */
1039static int test__parsing_fake(struct test_suite *test __maybe_unused,
1040			      int subtest __maybe_unused)
1041{
1042	int err = 0;
1043
1044	for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1045		err = metric_parse_fake("", metrics[i].str);
1046		if (err)
1047			return err;
1048	}
1049
1050	err = pmu_for_each_core_event(test__parsing_fake_callback, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1051	if (err)
1052		return err;
1053
1054	return pmu_for_each_sys_event(test__parsing_fake_callback, NULL);
1055}
1056
1057static struct test_case pmu_events_tests[] = {
1058	TEST_CASE("PMU event table sanity", pmu_event_table),
1059	TEST_CASE("PMU event map aliases", aliases),
1060	TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1061			 "some metrics failed"),
1062	TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
 
1063	{ .name = NULL, }
1064};
1065
1066struct test_suite suite__pmu_events = {
1067	.desc = "PMU events",
1068	.test_cases = pmu_events_tests,
1069};
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2#include "math.h"
   3#include "parse-events.h"
   4#include "pmu.h"
   5#include "pmus.h"
   6#include "tests.h"
   7#include <errno.h>
   8#include <stdio.h>
   9#include <linux/kernel.h>
  10#include <linux/zalloc.h>
  11#include "debug.h"
  12#include "../pmu-events/pmu-events.h"
  13#include <perf/evlist.h>
  14#include "util/evlist.h"
  15#include "util/expr.h"
  16#include "util/hashmap.h"
  17#include "util/parse-events.h"
  18#include "metricgroup.h"
  19#include "stat.h"
  20
  21struct perf_pmu_test_event {
  22	/* used for matching against events from generated pmu-events.c */
  23	struct pmu_event event;
  24
  25	/* used for matching against event aliases */
  26	/* extra events for aliases */
  27	const char *alias_str;
  28
  29	/*
  30	 * Note: For when PublicDescription does not exist in the JSON, we
  31	 * will have no long_desc in pmu_event.long_desc, but long_desc may
  32	 * be set in the alias.
  33	 */
  34	const char *alias_long_desc;
  35
  36	/* PMU which we should match against */
  37	const char *matching_pmu;
  38};
  39
  40struct perf_pmu_test_pmu {
  41	struct perf_pmu pmu;
  42	struct perf_pmu_test_event const *aliases[10];
  43};
  44
  45static const struct perf_pmu_test_event bp_l1_btb_correct = {
  46	.event = {
  47		.pmu = "default_core",
  48		.name = "bp_l1_btb_correct",
  49		.event = "event=0x8a",
  50		.desc = "L1 BTB Correction",
  51		.topic = "branch",
  52	},
  53	.alias_str = "event=0x8a",
  54	.alias_long_desc = "L1 BTB Correction",
  55};
  56
  57static const struct perf_pmu_test_event bp_l2_btb_correct = {
  58	.event = {
  59		.pmu = "default_core",
  60		.name = "bp_l2_btb_correct",
  61		.event = "event=0x8b",
  62		.desc = "L2 BTB Correction",
  63		.topic = "branch",
  64	},
  65	.alias_str = "event=0x8b",
  66	.alias_long_desc = "L2 BTB Correction",
  67};
  68
  69static const struct perf_pmu_test_event segment_reg_loads_any = {
  70	.event = {
  71		.pmu = "default_core",
  72		.name = "segment_reg_loads.any",
  73		.event = "event=6,period=200000,umask=0x80",
  74		.desc = "Number of segment register loads",
  75		.topic = "other",
  76	},
  77	.alias_str = "event=0x6,period=0x30d40,umask=0x80",
  78	.alias_long_desc = "Number of segment register loads",
  79};
  80
  81static const struct perf_pmu_test_event dispatch_blocked_any = {
  82	.event = {
  83		.pmu = "default_core",
  84		.name = "dispatch_blocked.any",
  85		.event = "event=9,period=200000,umask=0x20",
  86		.desc = "Memory cluster signals to block micro-op dispatch for any reason",
  87		.topic = "other",
  88	},
  89	.alias_str = "event=0x9,period=0x30d40,umask=0x20",
  90	.alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
  91};
  92
  93static const struct perf_pmu_test_event eist_trans = {
  94	.event = {
  95		.pmu = "default_core",
  96		.name = "eist_trans",
  97		.event = "event=0x3a,period=200000",
  98		.desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
  99		.topic = "other",
 100	},
 101	.alias_str = "event=0x3a,period=0x30d40",
 102	.alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
 103};
 104
 105static const struct perf_pmu_test_event l3_cache_rd = {
 106	.event = {
 107		.pmu = "default_core",
 108		.name = "l3_cache_rd",
 109		.event = "event=0x40",
 110		.desc = "L3 cache access, read",
 111		.long_desc = "Attributable Level 3 cache access, read",
 112		.topic = "cache",
 113	},
 114	.alias_str = "event=0x40",
 115	.alias_long_desc = "Attributable Level 3 cache access, read",
 116};
 117
 118static const struct perf_pmu_test_event *core_events[] = {
 119	&bp_l1_btb_correct,
 120	&bp_l2_btb_correct,
 121	&segment_reg_loads_any,
 122	&dispatch_blocked_any,
 123	&eist_trans,
 124	&l3_cache_rd,
 125	NULL
 126};
 127
 128static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
 129	.event = {
 130		.name = "uncore_hisi_ddrc.flux_wcmd",
 131		.event = "event=2",
 132		.desc = "DDRC write commands",
 133		.topic = "uncore",
 134		.long_desc = "DDRC write commands",
 135		.pmu = "hisi_sccl,ddrc",
 136	},
 137	.alias_str = "event=0x2",
 138	.alias_long_desc = "DDRC write commands",
 139	.matching_pmu = "hisi_sccl1_ddrc2",
 140};
 141
 142static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
 143	.event = {
 144		.name = "unc_cbo_xsnp_response.miss_eviction",
 145		.event = "event=0x22,umask=0x81",
 146		.desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
 147		.topic = "uncore",
 148		.long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
 149		.pmu = "uncore_cbox",
 150	},
 151	.alias_str = "event=0x22,umask=0x81",
 152	.alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
 153	.matching_pmu = "uncore_cbox_0",
 154};
 155
 156static const struct perf_pmu_test_event uncore_hyphen = {
 157	.event = {
 158		.name = "event-hyphen",
 159		.event = "event=0xe0",
 160		.desc = "UNC_CBO_HYPHEN",
 161		.topic = "uncore",
 162		.long_desc = "UNC_CBO_HYPHEN",
 163		.pmu = "uncore_cbox",
 164	},
 165	.alias_str = "event=0xe0",
 166	.alias_long_desc = "UNC_CBO_HYPHEN",
 167	.matching_pmu = "uncore_cbox_0",
 168};
 169
 170static const struct perf_pmu_test_event uncore_two_hyph = {
 171	.event = {
 172		.name = "event-two-hyph",
 173		.event = "event=0xc0",
 174		.desc = "UNC_CBO_TWO_HYPH",
 175		.topic = "uncore",
 176		.long_desc = "UNC_CBO_TWO_HYPH",
 177		.pmu = "uncore_cbox",
 178	},
 179	.alias_str = "event=0xc0",
 180	.alias_long_desc = "UNC_CBO_TWO_HYPH",
 181	.matching_pmu = "uncore_cbox_0",
 182};
 183
 184static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
 185	.event = {
 186		.name = "uncore_hisi_l3c.rd_hit_cpipe",
 187		.event = "event=7",
 188		.desc = "Total read hits",
 189		.topic = "uncore",
 190		.long_desc = "Total read hits",
 191		.pmu = "hisi_sccl,l3c",
 192	},
 193	.alias_str = "event=0x7",
 194	.alias_long_desc = "Total read hits",
 195	.matching_pmu = "hisi_sccl3_l3c7",
 196};
 197
 198static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
 199	.event = {
 200		.name = "uncore_imc_free_running.cache_miss",
 201		.event = "event=0x12",
 202		.desc = "Total cache misses",
 203		.topic = "uncore",
 204		.long_desc = "Total cache misses",
 205		.pmu = "uncore_imc_free_running",
 206	},
 207	.alias_str = "event=0x12",
 208	.alias_long_desc = "Total cache misses",
 209	.matching_pmu = "uncore_imc_free_running_0",
 210};
 211
 212static const struct perf_pmu_test_event uncore_imc_cache_hits = {
 213	.event = {
 214		.name = "uncore_imc.cache_hits",
 215		.event = "event=0x34",
 216		.desc = "Total cache hits",
 217		.topic = "uncore",
 218		.long_desc = "Total cache hits",
 219		.pmu = "uncore_imc",
 220	},
 221	.alias_str = "event=0x34",
 222	.alias_long_desc = "Total cache hits",
 223	.matching_pmu = "uncore_imc_0",
 224};
 225
 226static const struct perf_pmu_test_event *uncore_events[] = {
 227	&uncore_hisi_ddrc_flux_wcmd,
 228	&unc_cbo_xsnp_response_miss_eviction,
 229	&uncore_hyphen,
 230	&uncore_two_hyph,
 231	&uncore_hisi_l3c_rd_hit_cpipe,
 232	&uncore_imc_free_running_cache_miss,
 233	&uncore_imc_cache_hits,
 234	NULL
 235};
 236
 237static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
 238	.event = {
 239		.name = "sys_ddr_pmu.write_cycles",
 240		.event = "event=0x2b",
 241		.desc = "ddr write-cycles event",
 242		.topic = "uncore",
 243		.pmu = "uncore_sys_ddr_pmu",
 244		.compat = "v8",
 245	},
 246	.alias_str = "event=0x2b",
 247	.alias_long_desc = "ddr write-cycles event",
 248	.matching_pmu = "uncore_sys_ddr_pmu0",
 249};
 250
 251static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
 252	.event = {
 253		.name = "sys_ccn_pmu.read_cycles",
 254		.event = "config=0x2c",
 255		.desc = "ccn read-cycles event",
 256		.topic = "uncore",
 257		.pmu = "uncore_sys_ccn_pmu",
 258		.compat = "0x01",
 259	},
 260	.alias_str = "config=0x2c",
 261	.alias_long_desc = "ccn read-cycles event",
 262	.matching_pmu = "uncore_sys_ccn_pmu4",
 263};
 264
 265static const struct perf_pmu_test_event sys_cmn_pmu_hnf_cache_miss = {
 266	.event = {
 267		.name = "sys_cmn_pmu.hnf_cache_miss",
 268		.event = "eventid=1,type=5",
 269		.desc = "Counts total cache misses in first lookup result (high priority)",
 270		.topic = "uncore",
 271		.pmu = "uncore_sys_cmn_pmu",
 272		.compat = "(434|436|43c|43a).*",
 273	},
 274	.alias_str = "eventid=0x1,type=0x5",
 275	.alias_long_desc = "Counts total cache misses in first lookup result (high priority)",
 276	.matching_pmu = "uncore_sys_cmn_pmu0",
 277};
 278
 279static const struct perf_pmu_test_event *sys_events[] = {
 280	&sys_ddr_pmu_write_cycles,
 281	&sys_ccn_pmu_read_cycles,
 282	&sys_cmn_pmu_hnf_cache_miss,
 283	NULL
 284};
 285
 286static bool is_same(const char *reference, const char *test)
 287{
 288	if (!reference && !test)
 289		return true;
 290
 291	if (reference && !test)
 292		return false;
 293
 294	if (!reference && test)
 295		return false;
 296
 297	return !strcmp(reference, test);
 298}
 299
 300static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
 301{
 302	if (!is_same(e1->name, e2->name)) {
 303		pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
 304			  e1->name, e1->name, e2->name);
 305		return -1;
 306	}
 307
 308	if (!is_same(e1->compat, e2->compat)) {
 309		pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
 310			  e1->name, e1->compat, e2->compat);
 311		return -1;
 312	}
 313
 314	if (!is_same(e1->event, e2->event)) {
 315		pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
 316			  e1->name, e1->event, e2->event);
 317		return -1;
 318	}
 319
 320	if (!is_same(e1->desc, e2->desc)) {
 321		pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
 322			  e1->name, e1->desc, e2->desc);
 323		return -1;
 324	}
 325
 326	if (!is_same(e1->topic, e2->topic)) {
 327		pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
 328			  e1->name, e1->topic, e2->topic);
 329		return -1;
 330	}
 331
 332	if (!is_same(e1->long_desc, e2->long_desc)) {
 333		pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
 334			  e1->name, e1->long_desc, e2->long_desc);
 335		return -1;
 336	}
 337
 338	if (!is_same(e1->pmu, e2->pmu)) {
 339		pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
 340			  e1->name, e1->pmu, e2->pmu);
 341		return -1;
 342	}
 343
 344	if (!is_same(e1->unit, e2->unit)) {
 345		pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
 346			  e1->name, e1->unit, e2->unit);
 347		return -1;
 348	}
 349
 350	if (e1->perpkg != e2->perpkg) {
 351		pr_debug2("testing event e1 %s: mismatched perpkg, %d vs %d\n",
 352			  e1->name, e1->perpkg, e2->perpkg);
 353		return -1;
 354	}
 355
 356	if (e1->deprecated != e2->deprecated) {
 357		pr_debug2("testing event e1 %s: mismatched deprecated, %d vs %d\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 358			  e1->name, e1->deprecated, e2->deprecated);
 359		return -1;
 360	}
 361
 
 
 
 
 
 
 362	return 0;
 363}
 364
 365static int compare_alias_to_test_event(struct pmu_event_info *alias,
 366				struct perf_pmu_test_event const *test_event,
 367				char const *pmu_name)
 368{
 369	struct pmu_event const *event = &test_event->event;
 370
 371	/* An alias was found, ensure everything is in order */
 372	if (!is_same(alias->name, event->name)) {
 373		pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
 374			  pmu_name, alias->name, event->name);
 375		return -1;
 376	}
 377
 378	if (!is_same(alias->desc, event->desc)) {
 379		pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
 380			  pmu_name, alias->desc, event->desc);
 381		return -1;
 382	}
 383
 384	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
 385		pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
 386			  pmu_name, alias->long_desc,
 387			  test_event->alias_long_desc);
 388		return -1;
 389	}
 390
 391	if (!is_same(alias->topic, event->topic)) {
 392		pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
 393			  pmu_name, alias->topic, event->topic);
 394		return -1;
 395	}
 396
 397	if (!is_same(alias->str, test_event->alias_str)) {
 398		pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
 399			  pmu_name, alias->str, test_event->alias_str);
 400		return -1;
 401	}
 402
 403	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
 404		pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
 405			  pmu_name, alias->str, test_event->alias_long_desc);
 406		return -1;
 407	}
 408
 409	if (!is_same(alias->pmu_name, test_event->event.pmu) &&
 410	    !is_same(alias->pmu_name, "default_core")) {
 411		pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
 412			  pmu_name, alias->pmu_name, test_event->event.pmu);
 413		return -1;
 414	}
 415
 416	return 0;
 417}
 418
 419static int test__pmu_event_table_core_callback(const struct pmu_event *pe,
 420					       const struct pmu_events_table *table __maybe_unused,
 421					       void *data)
 422{
 423	int *map_events = data;
 424	struct perf_pmu_test_event const **test_event_table;
 425	bool found = false;
 426
 427	if (strcmp(pe->pmu, "default_core"))
 
 
 
 428		test_event_table = &uncore_events[0];
 429	else
 430		test_event_table = &core_events[0];
 431
 432	for (; *test_event_table; test_event_table++) {
 433		struct perf_pmu_test_event const *test_event = *test_event_table;
 434		struct pmu_event const *event = &test_event->event;
 435
 436		if (strcmp(pe->name, event->name))
 437			continue;
 438		found = true;
 439		(*map_events)++;
 440
 441		if (compare_pmu_events(pe, event))
 442			return -1;
 443
 444		pr_debug("testing event table %s: pass\n", pe->name);
 445	}
 446	if (!found) {
 447		pr_err("testing event table: could not find event %s\n", pe->name);
 448		return -1;
 449	}
 450	return 0;
 451}
 452
 453static int test__pmu_event_table_sys_callback(const struct pmu_event *pe,
 454					      const struct pmu_events_table *table __maybe_unused,
 455					      void *data)
 456{
 457	int *map_events = data;
 458	struct perf_pmu_test_event const **test_event_table;
 459	bool found = false;
 460
 461	test_event_table = &sys_events[0];
 462
 463	for (; *test_event_table; test_event_table++) {
 464		struct perf_pmu_test_event const *test_event = *test_event_table;
 465		struct pmu_event const *event = &test_event->event;
 466
 467		if (strcmp(pe->name, event->name))
 468			continue;
 469		found = true;
 470		(*map_events)++;
 471
 472		if (compare_pmu_events(pe, event))
 473			return TEST_FAIL;
 474
 475		pr_debug("testing sys event table %s: pass\n", pe->name);
 476	}
 477	if (!found) {
 478		pr_debug("testing sys event table: could not find event %s\n", pe->name);
 479		return TEST_FAIL;
 480	}
 481	return TEST_OK;
 482}
 483
 484/* Verify generated events from pmu-events.c are as expected */
 485static int test__pmu_event_table(struct test_suite *test __maybe_unused,
 486				 int subtest __maybe_unused)
 487{
 488	const struct pmu_events_table *sys_event_table =
 489		find_sys_events_table("pmu_events__test_soc_sys");
 490	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
 491	int map_events = 0, expected_events, err;
 492
 493	/* ignore 3x sentinels */
 494	expected_events = ARRAY_SIZE(core_events) +
 495			  ARRAY_SIZE(uncore_events) +
 496			  ARRAY_SIZE(sys_events) - 3;
 497
 498	if (!table || !sys_event_table)
 499		return -1;
 500
 501	err = pmu_events_table__for_each_event(table, /*pmu=*/ NULL,
 502					      test__pmu_event_table_core_callback,
 503					      &map_events);
 504	if (err)
 505		return err;
 506
 507	err = pmu_events_table__for_each_event(sys_event_table, /*pmu=*/ NULL,
 508					      test__pmu_event_table_sys_callback,
 509					      &map_events);
 510	if (err)
 511		return err;
 512
 513	if (map_events != expected_events) {
 514		pr_err("testing event table: found %d, but expected %d\n",
 515		       map_events, expected_events);
 516		return TEST_FAIL;
 517	}
 518
 519	return 0;
 520}
 521
 522struct test_core_pmu_event_aliases_cb_args {
 523	struct perf_pmu_test_event const *test_event;
 524	int *count;
 525};
 526
 527static int test_core_pmu_event_aliases_cb(void *state, struct pmu_event_info *alias)
 528{
 529	struct test_core_pmu_event_aliases_cb_args *args = state;
 530
 531	if (compare_alias_to_test_event(alias, args->test_event, alias->pmu->name))
 532		return -1;
 533	(*args->count)++;
 534	pr_debug2("testing aliases core PMU %s: matched event %s\n",
 535		alias->pmu_name, alias->name);
 536	return 0;
 537}
 538
 539/* Verify aliases are as expected */
 540static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)
 541{
 542	struct perf_pmu_test_event const **test_event_table;
 543	struct perf_pmu *pmu;
 
 544	int res = 0;
 545	const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
 
 546
 547	if (!table)
 548		return -1;
 549
 550	test_event_table = &core_events[0];
 551
 552	pmu = zalloc(sizeof(*pmu));
 553	if (!pmu)
 554		return -1;
 555
 556	INIT_LIST_HEAD(&pmu->format);
 557	INIT_LIST_HEAD(&pmu->aliases);
 558	INIT_LIST_HEAD(&pmu->caps);
 559	INIT_LIST_HEAD(&pmu->list);
 560	pmu->name = strdup(pmu_name);
 561	pmu->is_core = true;
 562
 563	pmu->events_table = table;
 564	pmu_add_cpu_aliases_table(pmu, table);
 565	pmu->cpu_aliases_added = true;
 566	pmu->sysfs_aliases_loaded = true;
 567
 568	res = pmu_events_table__find_event(table, pmu, "bp_l1_btb_correct", NULL, NULL);
 569	if (res != 0) {
 570		pr_debug("Missing test event in test architecture");
 571		return res;
 572	}
 573	for (; *test_event_table; test_event_table++) {
 574		struct perf_pmu_test_event test_event = **test_event_table;
 575		struct pmu_event const *event = &test_event.event;
 576		struct test_core_pmu_event_aliases_cb_args args = {
 577			.test_event = &test_event,
 578			.count = count,
 579		};
 580		int err;
 581
 582		test_event.event.pmu = pmu_name;
 583		err = perf_pmu__find_event(pmu, event->name, &args,
 584					   test_core_pmu_event_aliases_cb);
 585		if (err)
 586			res = err;
 
 
 
 
 
 
 587	}
 588	perf_pmu__delete(pmu);
 589
 
 
 
 
 
 590	return res;
 591}
 592
 593static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
 594{
 595	int alias_count = 0, to_match_count = 0, matched_count = 0;
 596	struct perf_pmu_test_event const **table;
 597	struct perf_pmu *pmu = &test_pmu->pmu;
 598	const char *pmu_name = pmu->name;
 
 599	const struct pmu_events_table *events_table;
 
 600	int res = 0;
 601
 602	events_table = find_core_events_table("testarch", "testcpu");
 603	if (!events_table)
 604		return -1;
 605	pmu->events_table = events_table;
 606	pmu_add_cpu_aliases_table(pmu, events_table);
 607	pmu->cpu_aliases_added = true;
 608	pmu->sysfs_aliases_loaded = true;
 609	pmu_add_sys_aliases(pmu);
 610
 611	/* Count how many aliases we generated */
 612	alias_count = perf_pmu__num_events(pmu);
 
 613
 614	/* Count how many aliases we expect from the known table */
 615	for (table = &test_pmu->aliases[0]; *table; table++)
 616		to_match_count++;
 617
 618	if (alias_count != to_match_count) {
 619		pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
 620			 pmu_name, to_match_count, alias_count);
 621		return -1;
 
 622	}
 623
 624	for (table = &test_pmu->aliases[0]; *table; table++) {
 625		struct perf_pmu_test_event test_event = **table;
 626		struct pmu_event const *event = &test_event.event;
 627		int err;
 628		struct test_core_pmu_event_aliases_cb_args args = {
 629			.test_event = &test_event,
 630			.count = &matched_count,
 631		};
 632
 633		if (strcmp(pmu_name, test_event.matching_pmu)) {
 634			pr_debug("testing aliases uncore PMU %s: mismatched matching_pmu, %s vs %s\n",
 635					pmu_name, test_event.matching_pmu, pmu_name);
 636			return -1;
 
 
 
 637		}
 638
 639		err = perf_pmu__find_event(pmu, event->name, &args,
 640					   test_core_pmu_event_aliases_cb);
 641		if (err) {
 642			res = err;
 643			pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
 644				 pmu_name, event->name);
 645			return -1;
 
 646		}
 647	}
 648
 649	if (alias_count != matched_count) {
 650		pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
 651			 pmu_name, matched_count, alias_count);
 652		res = -1;
 653	}
 
 
 
 
 
 
 654	return res;
 655}
 656
 657static struct perf_pmu_test_pmu test_pmus[] = {
 658	{
 659		.pmu = {
 660			.name = "hisi_sccl1_ddrc2",
 661			.is_uncore = 1,
 662		},
 663		.aliases = {
 664			&uncore_hisi_ddrc_flux_wcmd,
 665		},
 666	},
 667	{
 668		.pmu = {
 669			.name = "uncore_cbox_0",
 670			.is_uncore = 1,
 671		},
 672		.aliases = {
 673			&unc_cbo_xsnp_response_miss_eviction,
 674			&uncore_hyphen,
 675			&uncore_two_hyph,
 676		},
 677	},
 678	{
 679		.pmu = {
 680			.name = "hisi_sccl3_l3c7",
 681			.is_uncore = 1,
 682		},
 683		.aliases = {
 684			&uncore_hisi_l3c_rd_hit_cpipe,
 685		},
 686	},
 687	{
 688		.pmu = {
 689			.name = "uncore_imc_free_running_0",
 690			.is_uncore = 1,
 691		},
 692		.aliases = {
 693			&uncore_imc_free_running_cache_miss,
 694		},
 695	},
 696	{
 697		.pmu = {
 698			.name = "uncore_imc_0",
 699			.is_uncore = 1,
 700		},
 701		.aliases = {
 702			&uncore_imc_cache_hits,
 703		},
 704	},
 705	{
 706		.pmu = {
 707			.name = "uncore_sys_ddr_pmu0",
 708			.is_uncore = 1,
 709			.id = "v8",
 710		},
 711		.aliases = {
 712			&sys_ddr_pmu_write_cycles,
 713		},
 714	},
 715	{
 716		.pmu = {
 717			.name = "uncore_sys_ccn_pmu4",
 718			.is_uncore = 1,
 719			.id = "0x01",
 720		},
 721		.aliases = {
 722			&sys_ccn_pmu_read_cycles,
 723		},
 724	},
 725	{
 726		.pmu = {
 727			.name = (char *)"uncore_sys_cmn_pmu0",
 728			.is_uncore = 1,
 729			.id = (char *)"43401",
 730		},
 731		.aliases = {
 732			&sys_cmn_pmu_hnf_cache_miss,
 733		},
 734	},
 735	{
 736		.pmu = {
 737			.name = (char *)"uncore_sys_cmn_pmu0",
 738			.is_uncore = 1,
 739			.id = (char *)"43602",
 740		},
 741		.aliases = {
 742			&sys_cmn_pmu_hnf_cache_miss,
 743		},
 744	},
 745	{
 746		.pmu = {
 747			.name = (char *)"uncore_sys_cmn_pmu0",
 748			.is_uncore = 1,
 749			.id = (char *)"43c03",
 750		},
 751		.aliases = {
 752			&sys_cmn_pmu_hnf_cache_miss,
 753		},
 754	},
 755	{
 756		.pmu = {
 757			.name = (char *)"uncore_sys_cmn_pmu0",
 758			.is_uncore = 1,
 759			.id = (char *)"43a01",
 760		},
 761		.aliases = {
 762			&sys_cmn_pmu_hnf_cache_miss,
 763		},
 764	}
 765};
 766
 767/* Test that aliases generated are as expected */
 768static int test__aliases(struct test_suite *test __maybe_unused,
 769			int subtest __maybe_unused)
 770{
 771	struct perf_pmu *pmu = NULL;
 772	unsigned long i;
 773
 774	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
 775		int count = 0;
 776
 
 
 
 777		if (list_empty(&pmu->format)) {
 778			pr_debug2("skipping testing core PMU %s\n", pmu->name);
 779			continue;
 780		}
 781
 782		if (__test_core_pmu_event_aliases(pmu->name, &count)) {
 783			pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
 784			return -1;
 785		}
 786
 787		if (count == 0) {
 788			pr_debug("testing core PMU %s aliases: no events to match\n",
 789				  pmu->name);
 790			return -1;
 791		}
 792
 793		pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
 794	}
 795
 796	for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
 797		int res;
 798
 799		INIT_LIST_HEAD(&test_pmus[i].pmu.format);
 800		INIT_LIST_HEAD(&test_pmus[i].pmu.aliases);
 801		INIT_LIST_HEAD(&test_pmus[i].pmu.caps);
 802
 803		res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
 804		if (res)
 805			return res;
 806	}
 807
 808	return 0;
 809}
 810
 811static bool is_number(const char *str)
 812{
 813	char *end_ptr;
 814	double v;
 815
 816	errno = 0;
 817	v = strtod(str, &end_ptr);
 818	(void)v; // We're not interested in this value, only if it is valid
 819	return errno == 0 && end_ptr != str;
 820}
 821
 822static int check_parse_id(const char *id, struct parse_events_error *error)
 
 823{
 824	struct evlist *evlist;
 825	int ret;
 826	char *dup, *cur;
 827
 828	/* Numbers are always valid. */
 829	if (is_number(id))
 830		return 0;
 831
 832	evlist = evlist__new();
 833	if (!evlist)
 834		return -ENOMEM;
 835
 836	dup = strdup(id);
 837	if (!dup)
 838		return -ENOMEM;
 839
 840	for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
 841		*cur = '/';
 842
 843	ret = __parse_events(evlist, dup, /*pmu_filter=*/NULL, error, /*fake_pmu=*/true,
 844			     /*warn_if_reordered=*/true, /*fake_tp=*/false);
 
 
 
 
 
 
 
 
 845	free(dup);
 846
 847	evlist__delete(evlist);
 848	return ret;
 849}
 850
 851static int check_parse_fake(const char *id)
 852{
 853	struct parse_events_error error;
 854	int ret;
 855
 856	parse_events_error__init(&error);
 857	ret = check_parse_id(id, &error);
 858	parse_events_error__exit(&error);
 859	return ret;
 860}
 861
 862struct metric {
 863	struct list_head list;
 864	struct metric_ref metric_ref;
 865};
 866
 867static int test__parsing_callback(const struct pmu_metric *pm,
 868				  const struct pmu_metrics_table *table,
 869				  void *data)
 870{
 871	int *failures = data;
 872	int k;
 873	struct evlist *evlist;
 874	struct perf_cpu_map *cpus;
 
 875	struct evsel *evsel;
 876	struct rblist metric_events = {
 877		.nr_entries = 0,
 878	};
 879	int err = 0;
 880
 881	if (!pm->metric_expr)
 882		return 0;
 883
 884	pr_debug("Found metric '%s'\n", pm->metric_name);
 885	(*failures)++;
 886
 887	/*
 888	 * We need to prepare evlist for stat mode running on CPU 0
 889	 * because that's where all the stats are going to be created.
 890	 */
 891	evlist = evlist__new();
 892	if (!evlist)
 893		return -ENOMEM;
 894
 895	cpus = perf_cpu_map__new("0");
 896	if (!cpus) {
 897		evlist__delete(evlist);
 898		return -ENOMEM;
 899	}
 900
 901	perf_evlist__set_maps(&evlist->core, cpus, NULL);
 
 902
 903	err = metricgroup__parse_groups_test(evlist, table, pm->metric_name, &metric_events);
 
 
 904	if (err) {
 905		if (!strcmp(pm->metric_name, "M1") || !strcmp(pm->metric_name, "M2") ||
 906		    !strcmp(pm->metric_name, "M3")) {
 907			(*failures)--;
 908			pr_debug("Expected broken metric %s skipping\n", pm->metric_name);
 909			err = 0;
 910		}
 911		goto out_err;
 912	}
 913
 914	err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
 915	if (err)
 916		goto out_err;
 917	/*
 918	 * Add all ids with a made up value. The value may trigger divide by
 919	 * zero when subtracted and so try to make them unique.
 920	 */
 921	k = 1;
 922	evlist__alloc_aggr_stats(evlist, 1);
 923	evlist__for_each_entry(evlist, evsel) {
 924		evsel->stats->aggr->counts.val = k;
 925		if (evsel__name_is(evsel, "duration_time"))
 926			update_stats(&walltime_nsecs_stats, k);
 927		k++;
 928	}
 929	evlist__for_each_entry(evlist, evsel) {
 930		struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
 931
 932		if (me != NULL) {
 933			struct metric_expr *mexp;
 934
 935			list_for_each_entry (mexp, &me->head, nd) {
 936				if (strcmp(mexp->metric_name, pm->metric_name))
 937					continue;
 938				pr_debug("Result %f\n", test_generic_metric(mexp, 0));
 939				err = 0;
 940				(*failures)--;
 941				goto out_err;
 942			}
 943		}
 944	}
 945	pr_debug("Didn't find parsed metric %s", pm->metric_name);
 946	err = 1;
 947out_err:
 948	if (err)
 949		pr_debug("Broken metric %s\n", pm->metric_name);
 950
 951	/* ... cleanup. */
 952	metricgroup__rblist_exit(&metric_events);
 
 953	evlist__free_stats(evlist);
 954	perf_cpu_map__put(cpus);
 955	evlist__delete(evlist);
 956	return err;
 957}
 958
 959static int test__parsing(struct test_suite *test __maybe_unused,
 960			 int subtest __maybe_unused)
 961{
 962	int failures = 0;
 963
 964	pmu_for_each_core_metric(test__parsing_callback, &failures);
 965	pmu_for_each_sys_metric(test__parsing_callback, &failures);
 966
 967	return failures == 0 ? TEST_OK : TEST_FAIL;
 968}
 969
 970struct test_metric {
 971	const char *str;
 972};
 973
 974static struct test_metric metrics[] = {
 975	{ "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
 976	{ "imx8_ddr0@read\\-cycles@ * 4 * 4", },
 977	{ "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
 978	{ "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
 979	{ "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
 980};
 981
 982static int metric_parse_fake(const char *metric_name, const char *str)
 983{
 984	struct expr_parse_ctx *ctx;
 985	struct hashmap_entry *cur;
 986	double result;
 987	int ret = -1;
 988	size_t bkt;
 989	int i;
 990
 991	pr_debug("parsing '%s': '%s'\n", metric_name, str);
 992
 993	ctx = expr__ctx_new();
 994	if (!ctx) {
 995		pr_debug("expr__ctx_new failed");
 996		return TEST_FAIL;
 997	}
 998	ctx->sctx.is_test = true;
 999	if (expr__find_ids(str, NULL, ctx) < 0) {
1000		pr_err("expr__find_ids failed\n");
1001		return -1;
1002	}
1003
1004	/*
1005	 * Add all ids with a made up value. The value may
1006	 * trigger divide by zero when subtracted and so try to
1007	 * make them unique.
1008	 */
1009	i = 1;
1010	hashmap__for_each_entry(ctx->ids, cur, bkt)
1011		expr__add_id_val(ctx, strdup(cur->pkey), i++);
1012
1013	hashmap__for_each_entry(ctx->ids, cur, bkt) {
1014		if (check_parse_fake(cur->pkey)) {
1015			pr_err("check_parse_fake failed\n");
1016			goto out;
1017		}
1018	}
1019
1020	ret = 0;
1021	if (expr__parse(&result, ctx, str)) {
1022		/*
1023		 * Parsing failed, make numbers go from large to small which can
1024		 * resolve divide by zero issues.
1025		 */
1026		i = 1024;
1027		hashmap__for_each_entry(ctx->ids, cur, bkt)
1028			expr__add_id_val(ctx, strdup(cur->pkey), i--);
1029		if (expr__parse(&result, ctx, str)) {
1030			pr_err("expr__parse failed for %s\n", metric_name);
1031			/* The following have hard to avoid divide by zero. */
1032			if (!strcmp(metric_name, "tma_clears_resteers") ||
1033			    !strcmp(metric_name, "tma_mispredicts_resteers"))
1034				ret = 0;
1035			else
1036				ret = -1;
1037		}
1038	}
1039
1040out:
1041	expr__ctx_free(ctx);
1042	return ret;
1043}
1044
1045static int test__parsing_fake_callback(const struct pmu_metric *pm,
1046				       const struct pmu_metrics_table *table __maybe_unused,
1047				       void *data __maybe_unused)
1048{
1049	return metric_parse_fake(pm->metric_name, pm->metric_expr);
 
 
 
1050}
1051
1052/*
1053 * Parse all the metrics for current architecture, or all defined cpus via the
1054 * 'fake_pmu' in parse_events.
 
1055 */
1056static int test__parsing_fake(struct test_suite *test __maybe_unused,
1057			      int subtest __maybe_unused)
1058{
1059	int err = 0;
1060
1061	for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1062		err = metric_parse_fake("", metrics[i].str);
1063		if (err)
1064			return err;
1065	}
1066
1067	err = pmu_for_each_core_metric(test__parsing_fake_callback, NULL);
1068	if (err)
1069		return err;
1070
1071	return pmu_for_each_sys_metric(test__parsing_fake_callback, NULL);
1072}
1073
1074static int test__parsing_threshold_callback(const struct pmu_metric *pm,
1075					const struct pmu_metrics_table *table __maybe_unused,
1076					void *data __maybe_unused)
1077{
1078	if (!pm->metric_threshold)
1079		return 0;
1080	return metric_parse_fake(pm->metric_name, pm->metric_threshold);
1081}
1082
1083static int test__parsing_threshold(struct test_suite *test __maybe_unused,
1084			      int subtest __maybe_unused)
1085{
1086	int err = 0;
1087
1088	err = pmu_for_each_core_metric(test__parsing_threshold_callback, NULL);
1089	if (err)
1090		return err;
1091
1092	return pmu_for_each_sys_metric(test__parsing_threshold_callback, NULL);
1093}
1094
1095static struct test_case pmu_events_tests[] = {
1096	TEST_CASE("PMU event table sanity", pmu_event_table),
1097	TEST_CASE("PMU event map aliases", aliases),
1098	TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1099			 "some metrics failed"),
1100	TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1101	TEST_CASE("Parsing of metric thresholds with fake PMUs", parsing_threshold),
1102	{ .name = NULL, }
1103};
1104
1105struct test_suite suite__pmu_events = {
1106	.desc = "PMU JSON event tests",
1107	.test_cases = pmu_events_tests,
1108};