pmu-events.c 26.7 KB
Newer Older
J
John Garry 已提交
1
// SPDX-License-Identifier: GPL-2.0
2
#include "math.h"
J
John Garry 已提交
3 4 5 6 7 8
#include "parse-events.h"
#include "pmu.h"
#include "tests.h"
#include <errno.h>
#include <stdio.h>
#include <linux/kernel.h>
J
John Garry 已提交
9
#include <linux/zalloc.h>
J
John Garry 已提交
10 11
#include "debug.h"
#include "../pmu-events/pmu-events.h"
12 13 14
#include "util/evlist.h"
#include "util/expr.h"
#include "util/parse-events.h"
15
#include "metricgroup.h"
J
John Garry 已提交
16 17

struct perf_pmu_test_event {
18
	/* used for matching against events from generated pmu-events.c */
J
John Garry 已提交
19
	struct pmu_event event;
J
John Garry 已提交
20

21
	/* used for matching against event aliases */
J
John Garry 已提交
22 23 24 25 26 27 28 29 30
	/* extra events for aliases */
	const char *alias_str;

	/*
	 * Note: For when PublicDescription does not exist in the JSON, we
	 * will have no long_desc in pmu_event.long_desc, but long_desc may
	 * be set in the alias.
	 */
	const char *alias_long_desc;
31 32 33 34 35 36 37 38

	/* PMU which we should match against */
	const char *matching_pmu;
};

struct perf_pmu_test_pmu {
	struct perf_pmu pmu;
	struct perf_pmu_test_event const *aliases[10];
J
John Garry 已提交
39
};
J
John Garry 已提交
40

41 42 43 44 45 46
static const struct perf_pmu_test_event bp_l1_btb_correct = {
	.event = {
		.name = "bp_l1_btb_correct",
		.event = "event=0x8a",
		.desc = "L1 BTB Correction",
		.topic = "branch",
J
John Garry 已提交
47
	},
48 49 50 51 52 53 54 55 56 57
	.alias_str = "event=0x8a",
	.alias_long_desc = "L1 BTB Correction",
};

static const struct perf_pmu_test_event bp_l2_btb_correct = {
	.event = {
		.name = "bp_l2_btb_correct",
		.event = "event=0x8b",
		.desc = "L2 BTB Correction",
		.topic = "branch",
J
John Garry 已提交
58
	},
59 60 61 62 63 64 65 66 67 68
	.alias_str = "event=0x8b",
	.alias_long_desc = "L2 BTB Correction",
};

static const struct perf_pmu_test_event segment_reg_loads_any = {
	.event = {
		.name = "segment_reg_loads.any",
		.event = "umask=0x80,period=200000,event=0x6",
		.desc = "Number of segment register loads",
		.topic = "other",
J
John Garry 已提交
69
	},
70
	.alias_str = "umask=0x80,period=0x30d40,event=0x6",
71 72 73 74 75 76 77 78 79
	.alias_long_desc = "Number of segment register loads",
};

static const struct perf_pmu_test_event dispatch_blocked_any = {
	.event = {
		.name = "dispatch_blocked.any",
		.event = "umask=0x20,period=200000,event=0x9",
		.desc = "Memory cluster signals to block micro-op dispatch for any reason",
		.topic = "other",
J
John Garry 已提交
80
	},
81
	.alias_str = "umask=0x20,period=0x30d40,event=0x9",
82 83 84 85 86 87 88 89 90
	.alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
};

static const struct perf_pmu_test_event eist_trans = {
	.event = {
		.name = "eist_trans",
		.event = "umask=0x0,period=200000,event=0x3a",
		.desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
		.topic = "other",
91
	},
92
	.alias_str = "umask=0,period=0x30d40,event=0x3a",
93 94 95 96 97 98 99 100 101 102
	.alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
};

static const struct perf_pmu_test_event l3_cache_rd = {
	.event = {
		.name = "l3_cache_rd",
		.event = "event=0x40",
		.desc = "L3 cache access, read",
		.long_desc = "Attributable Level 3 cache access, read",
		.topic = "cache",
J
John Garry 已提交
103
	},
104 105
	.alias_str = "event=0x40",
	.alias_long_desc = "Attributable Level 3 cache access, read",
J
John Garry 已提交
106 107
};

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static const struct perf_pmu_test_event *core_events[] = {
	&bp_l1_btb_correct,
	&bp_l2_btb_correct,
	&segment_reg_loads_any,
	&dispatch_blocked_any,
	&eist_trans,
	&l3_cache_rd,
	NULL
};

static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
	.event = {
		.name = "uncore_hisi_ddrc.flux_wcmd",
		.event = "event=0x2",
		.desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
		.topic = "uncore",
		.long_desc = "DDRC write commands",
		.pmu = "hisi_sccl,ddrc",
J
John Garry 已提交
126
	},
127 128
	.alias_str = "event=0x2",
	.alias_long_desc = "DDRC write commands",
129
	.matching_pmu = "hisi_sccl1_ddrc2",
130 131 132 133 134 135 136 137 138 139
};

static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
	.event = {
		.name = "unc_cbo_xsnp_response.miss_eviction",
		.event = "umask=0x81,event=0x22",
		.desc = "Unit: uncore_cbox A cross-core snoop resulted from L3 Eviction which misses in some processor core",
		.topic = "uncore",
		.long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
		.pmu = "uncore_cbox",
J
John Garry 已提交
140
	},
141 142
	.alias_str = "umask=0x81,event=0x22",
	.alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
143
	.matching_pmu = "uncore_cbox_0",
J
John Garry 已提交
144 145
};

146 147 148
static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
	.event = {
		.name = "uncore_hisi_l3c.rd_hit_cpipe",
149
		.event = "event=0x7",
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
		.desc = "Total read hits. Unit: hisi_sccl,l3c ",
		.topic = "uncore",
		.long_desc = "Total read hits",
		.pmu = "hisi_sccl,l3c",
	},
	.alias_str = "event=0x7",
	.alias_long_desc = "Total read hits",
	.matching_pmu = "hisi_sccl3_l3c7",
};

static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
	.event = {
		.name = "uncore_imc_free_running.cache_miss",
		.event = "event=0x12",
		.desc = "Total cache misses. Unit: uncore_imc_free_running ",
		.topic = "uncore",
		.long_desc = "Total cache misses",
		.pmu = "uncore_imc_free_running",
	},
	.alias_str = "event=0x12",
	.alias_long_desc = "Total cache misses",
	.matching_pmu = "uncore_imc_free_running_0",
};

static const struct perf_pmu_test_event uncore_imc_cache_hits = {
	.event = {
		.name = "uncore_imc.cache_hits",
		.event = "event=0x34",
		.desc = "Total cache hits. Unit: uncore_imc ",
		.topic = "uncore",
		.long_desc = "Total cache hits",
		.pmu = "uncore_imc",
	},
	.alias_str = "event=0x34",
	.alias_long_desc = "Total cache hits",
	.matching_pmu = "uncore_imc_0",
};

188 189 190
static const struct perf_pmu_test_event *uncore_events[] = {
	&uncore_hisi_ddrc_flux_wcmd,
	&unc_cbo_xsnp_response_miss_eviction,
191 192 193
	&uncore_hisi_l3c_rd_hit_cpipe,
	&uncore_imc_free_running_cache_miss,
	&uncore_imc_cache_hits,
194 195
	NULL
};
J
John Garry 已提交
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210
static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
	.event = {
		.name = "sys_ddr_pmu.write_cycles",
		.event = "event=0x2b",
		.desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
		.topic = "uncore",
		.pmu = "uncore_sys_ddr_pmu",
		.compat = "v8",
	},
	.alias_str = "event=0x2b",
	.alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
	.matching_pmu = "uncore_sys_ddr_pmu",
};

211 212 213 214 215 216 217 218 219 220 221 222 223 224
static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
	.event = {
		.name = "sys_ccn_pmu.read_cycles",
		.event = "config=0x2c",
		.desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
		.topic = "uncore",
		.pmu = "uncore_sys_ccn_pmu",
		.compat = "0x01",
	},
	.alias_str = "config=0x2c",
	.alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
	.matching_pmu = "uncore_sys_ccn_pmu",
};

225 226
static const struct perf_pmu_test_event *sys_events[] = {
	&sys_ddr_pmu_write_cycles,
227
	&sys_ccn_pmu_read_cycles,
228 229 230
	NULL
};

J
John Garry 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244
static bool is_same(const char *reference, const char *test)
{
	if (!reference && !test)
		return true;

	if (reference && !test)
		return false;

	if (!reference && test)
		return false;

	return !strcmp(reference, test);
}

245
static const struct pmu_events_map *__test_pmu_get_events_map(void)
J
John Garry 已提交
246
{
247
	const struct pmu_events_map *map;
J
John Garry 已提交
248 249 250 251 252 253 254 255 256 257 258

	for (map = &pmu_events_map[0]; map->cpuid; map++) {
		if (!strcmp(map->cpuid, "testcpu"))
			return map;
	}

	pr_err("could not find test events map\n");

	return NULL;
}

259
static const struct pmu_event *__test_pmu_get_sys_events_table(void)
260
{
261
	const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
262 263 264 265 266 267 268 269 270

	for ( ; tables->name; tables++) {
		if (!strcmp("pme_test_soc_sys", tables->name))
			return tables->table;
	}

	return NULL;
}

271
static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
272
{
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	if (!is_same(e1->name, e2->name)) {
		pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
			  e1->name, e1->name, e2->name);
		return -1;
	}

	if (!is_same(e1->compat, e2->compat)) {
		pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
			  e1->name, e1->compat, e2->compat);
		return -1;
	}

	if (!is_same(e1->event, e2->event)) {
		pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
			  e1->name, e1->event, e2->event);
		return -1;
	}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	if (!is_same(e1->desc, e2->desc)) {
		pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
			  e1->name, e1->desc, e2->desc);
		return -1;
	}

	if (!is_same(e1->topic, e2->topic)) {
		pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
			  e1->name, e1->topic, e2->topic);
		return -1;
	}

	if (!is_same(e1->long_desc, e2->long_desc)) {
		pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
			  e1->name, e1->long_desc, e2->long_desc);
		return -1;
	}

309 310 311 312 313 314
	if (!is_same(e1->pmu, e2->pmu)) {
		pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
			  e1->name, e1->pmu, e2->pmu);
		return -1;
	}

315 316 317 318 319 320 321 322 323 324 325 326
	if (!is_same(e1->unit, e2->unit)) {
		pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
			  e1->name, e1->unit, e2->unit);
		return -1;
	}

	if (!is_same(e1->perpkg, e2->perpkg)) {
		pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
			  e1->name, e1->perpkg, e2->perpkg);
		return -1;
	}

327 328 329 330 331 332
	if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
		pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
			  e1->name, e1->aggr_mode, e2->aggr_mode);
		return -1;
	}

333 334 335 336 337 338 339 340 341 342 343 344
	if (!is_same(e1->metric_expr, e2->metric_expr)) {
		pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
			  e1->name, e1->metric_expr, e2->metric_expr);
		return -1;
	}

	if (!is_same(e1->metric_name, e2->metric_name)) {
		pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
			  e1->name,	e1->metric_name, e2->metric_name);
		return -1;
	}

345 346 347
	if (!is_same(e1->metric_group, e2->metric_group)) {
		pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
			  e1->name, e1->metric_group, e2->metric_group);
348 349 350
		return -1;
	}

351 352 353
	if (!is_same(e1->deprecated, e2->deprecated)) {
		pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
			  e1->name, e1->deprecated, e2->deprecated);
354 355 356
		return -1;
	}

357 358 359
	if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
		pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
			  e1->name, e1->metric_constraint, e2->metric_constraint);
360 361 362 363 364 365
		return -1;
	}

	return 0;
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
				struct perf_pmu_test_event const *test_event,
				char const *pmu_name)
{
	struct pmu_event const *event = &test_event->event;

	/* An alias was found, ensure everything is in order */
	if (!is_same(alias->name, event->name)) {
		pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
			  pmu_name, alias->name, event->name);
		return -1;
	}

	if (!is_same(alias->desc, event->desc)) {
		pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
			  pmu_name, alias->desc, event->desc);
		return -1;
	}

	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
		pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
			  pmu_name, alias->long_desc,
			  test_event->alias_long_desc);
		return -1;
	}

	if (!is_same(alias->topic, event->topic)) {
		pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
			  pmu_name, alias->topic, event->topic);
		return -1;
	}

	if (!is_same(alias->str, test_event->alias_str)) {
		pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
			  pmu_name, alias->str, test_event->alias_str);
		return -1;
	}

	if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
		pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
			  pmu_name, alias->str, test_event->alias_long_desc);
		return -1;
	}


	if (!is_same(alias->pmu_name, test_event->event.pmu)) {
		pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
			  pmu_name, alias->pmu_name, test_event->event.pmu);
		return -1;
	}

	return 0;
}

420
/* Verify generated events from pmu-events.c are as expected */
421
static int test_pmu_event_table(void)
J
John Garry 已提交
422
{
423
	const struct pmu_event *sys_event_tables = __test_pmu_get_sys_events_table();
424
	const struct pmu_events_map *map = __test_pmu_get_events_map();
425
	const struct pmu_event *table;
J
John Garry 已提交
426 427
	int map_events = 0, expected_events;

428
	/* ignore 3x sentinels */
429
	expected_events = ARRAY_SIZE(core_events) +
430 431
			  ARRAY_SIZE(uncore_events) +
			  ARRAY_SIZE(sys_events) - 3;
J
John Garry 已提交
432

433
	if (!map || !sys_event_tables)
J
John Garry 已提交
434 435 436
		return -1;

	for (table = map->table; table->name; table++) {
437
		struct perf_pmu_test_event const **test_event_table;
J
John Garry 已提交
438 439 440
		bool found = false;

		if (table->pmu)
441
			test_event_table = &uncore_events[0];
J
John Garry 已提交
442
		else
443
			test_event_table = &core_events[0];
J
John Garry 已提交
444

445 446 447
		for (; *test_event_table; test_event_table++) {
			struct perf_pmu_test_event const *test_event = *test_event_table;
			struct pmu_event const *event = &test_event->event;
J
John Garry 已提交
448

449
			if (strcmp(table->name, event->name))
J
John Garry 已提交
450 451 452 453
				continue;
			found = true;
			map_events++;

454
			if (compare_pmu_events(table, event))
J
John Garry 已提交
455 456 457 458 459 460 461 462 463 464 465 466
				return -1;

			pr_debug("testing event table %s: pass\n", table->name);
		}

		if (!found) {
			pr_err("testing event table: could not find event %s\n",
			       table->name);
			return -1;
		}
	}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	for (table = sys_event_tables; table->name; table++) {
		struct perf_pmu_test_event const **test_event_table;
		bool found = false;

		test_event_table = &sys_events[0];

		for (; *test_event_table; test_event_table++) {
			struct perf_pmu_test_event const *test_event = *test_event_table;
			struct pmu_event const *event = &test_event->event;

			if (strcmp(table->name, event->name))
				continue;
			found = true;
			map_events++;

			if (compare_pmu_events(table, event))
				return -1;

			pr_debug("testing sys event table %s: pass\n", table->name);
		}
		if (!found) {
			pr_debug("testing event table: could not find event %s\n",
				   table->name);
			return -1;
		}
	}

J
John Garry 已提交
494 495 496 497 498 499 500 501
	if (map_events != expected_events) {
		pr_err("testing event table: found %d, but expected %d\n",
		       map_events, expected_events);
		return -1;
	}

	return 0;
}
J
John Garry 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514

static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
{
	struct perf_pmu_alias *alias;

	list_for_each_entry(alias, aliases, list)
		if (!strcmp(test_event, alias->name))
			return alias;

	return NULL;
}

/* Verify aliases are as expected */
515
static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
J
John Garry 已提交
516
{
517
	struct perf_pmu_test_event const **test_event_table;
J
John Garry 已提交
518 519 520
	struct perf_pmu *pmu;
	LIST_HEAD(aliases);
	int res = 0;
521
	const struct pmu_events_map *map = __test_pmu_get_events_map();
522
	struct perf_pmu_alias *a, *tmp;
J
John Garry 已提交
523 524 525 526

	if (!map)
		return -1;

527
	test_event_table = &core_events[0];
J
John Garry 已提交
528 529 530 531 532 533 534 535 536

	pmu = zalloc(sizeof(*pmu));
	if (!pmu)
		return -1;

	pmu->name = pmu_name;

	pmu_add_cpu_aliases_map(&aliases, pmu, map);

537 538 539 540
	for (; *test_event_table; test_event_table++) {
		struct perf_pmu_test_event const *test_event = *test_event_table;
		struct pmu_event const *event = &test_event->event;
		struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
J
John Garry 已提交
541 542

		if (!alias) {
543
			pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
544
				  pmu_name, event->name);
J
John Garry 已提交
545 546 547 548
			res = -1;
			break;
		}

549
		if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
J
John Garry 已提交
550 551 552 553 554
			res = -1;
			break;
		}

		(*count)++;
555
		pr_debug2("testing aliases core PMU %s: matched event %s\n",
J
John Garry 已提交
556 557 558
			  pmu_name, alias->name);
	}

559 560 561 562
	list_for_each_entry_safe(a, tmp, &aliases, list) {
		list_del(&a->list);
		perf_pmu_free_alias(a);
	}
J
John Garry 已提交
563 564 565 566
	free(pmu);
	return res;
}

567 568 569 570 571 572 573
static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
{
	int alias_count = 0, to_match_count = 0, matched_count = 0;
	struct perf_pmu_test_event const **table;
	struct perf_pmu *pmu = &test_pmu->pmu;
	const char *pmu_name = pmu->name;
	struct perf_pmu_alias *a, *tmp, *alias;
574
	const struct pmu_events_map *map;
575 576 577 578 579 580 581
	LIST_HEAD(aliases);
	int res = 0;

	map = __test_pmu_get_events_map();
	if (!map)
		return -1;
	pmu_add_cpu_aliases_map(&aliases, pmu, map);
582
	pmu_add_sys_aliases(&aliases, pmu);
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

	/* Count how many aliases we generated */
	list_for_each_entry(alias, &aliases, list)
		alias_count++;

	/* Count how many aliases we expect from the known table */
	for (table = &test_pmu->aliases[0]; *table; table++)
		to_match_count++;

	if (alias_count != to_match_count) {
		pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
			 pmu_name, to_match_count, alias_count);
		res = -1;
		goto out;
	}

	list_for_each_entry(alias, &aliases, list) {
		bool matched = false;

		for (table = &test_pmu->aliases[0]; *table; table++) {
			struct perf_pmu_test_event const *test_event = *table;
			struct pmu_event const *event = &test_event->event;

			if (!strcmp(event->name, alias->name)) {
				if (compare_alias_to_test_event(alias,
							test_event,
							pmu_name)) {
					continue;
				}
				matched = true;
				matched_count++;
			}
		}

		if (matched == false) {
			pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
				 pmu_name, alias->name);
			res = -1;
			goto out;
		}
	}

	if (alias_count != matched_count) {
		pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
			 pmu_name, matched_count, alias_count);
		res = -1;
	}

out:
	list_for_each_entry_safe(a, tmp, &aliases, list) {
		list_del(&a->list);
		perf_pmu_free_alias(a);
	}
	return res;
}

static struct perf_pmu_test_pmu test_pmus[] = {
	{
		.pmu = {
			.name = (char *)"hisi_sccl1_ddrc2",
			.is_uncore = 1,
		},
		.aliases = {
			&uncore_hisi_ddrc_flux_wcmd,
		},
	},
	{
		.pmu = {
			.name = (char *)"uncore_cbox_0",
			.is_uncore = 1,
		},
		.aliases = {
			&unc_cbo_xsnp_response_miss_eviction,
		},
	},
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
	{
		.pmu = {
			.name = (char *)"hisi_sccl3_l3c7",
			.is_uncore = 1,
		},
		.aliases = {
			&uncore_hisi_l3c_rd_hit_cpipe,
		},
	},
	{
		.pmu = {
			.name = (char *)"uncore_imc_free_running_0",
			.is_uncore = 1,
		},
		.aliases = {
			&uncore_imc_free_running_cache_miss,
		},
	},
	{
		.pmu = {
			.name = (char *)"uncore_imc_0",
			.is_uncore = 1,
		},
		.aliases = {
			&uncore_imc_cache_hits,
		},
	},
685 686 687 688 689 690 691 692 693 694
	{
		.pmu = {
			.name = (char *)"uncore_sys_ddr_pmu0",
			.is_uncore = 1,
			.id = (char *)"v8",
		},
		.aliases = {
			&sys_ddr_pmu_write_cycles,
		},
	},
695 696 697 698 699 700 701 702 703 704
	{
		.pmu = {
			.name = (char *)"uncore_sys_ccn_pmu4",
			.is_uncore = 1,
			.id = (char *)"0x01",
		},
		.aliases = {
			&sys_ccn_pmu_read_cycles,
		},
	},
705 706
};

707
/* Test that aliases generated are as expected */
708
static int test_aliases(void)
J
John Garry 已提交
709
{
J
John Garry 已提交
710
	struct perf_pmu *pmu = NULL;
711
	unsigned long i;
J
John Garry 已提交
712 713 714 715

	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
		int count = 0;

716 717 718
		if (!is_pmu_core(pmu->name))
			continue;

J
John Garry 已提交
719
		if (list_empty(&pmu->format)) {
720
			pr_debug2("skipping testing core PMU %s\n", pmu->name);
J
John Garry 已提交
721 722 723
			continue;
		}

724 725
		if (__test_core_pmu_event_aliases(pmu->name, &count)) {
			pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
J
John Garry 已提交
726 727 728
			return -1;
		}

729 730
		if (count == 0) {
			pr_debug("testing core PMU %s aliases: no events to match\n",
J
John Garry 已提交
731
				  pmu->name);
732 733 734 735
			return -1;
		}

		pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
J
John Garry 已提交
736 737
	}

738 739 740 741 742 743 744
	for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
		int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);

		if (res)
			return res;
	}

J
John Garry 已提交
745 746
	return 0;
}
747 748 749 750 751 752 753 754 755 756 757 758

static bool is_number(const char *str)
{
	char *end_ptr;
	double v;

	errno = 0;
	v = strtod(str, &end_ptr);
	(void)v; // We're not interested in this value, only if it is valid
	return errno == 0 && end_ptr != str;
}

759 760
static int check_parse_id(const char *id, struct parse_events_error *error,
			  struct perf_pmu *fake_pmu)
761 762 763
{
	struct evlist *evlist;
	int ret;
764
	char *dup, *cur;
765 766 767 768 769 770

	/* Numbers are always valid. */
	if (is_number(id))
		return 0;

	evlist = evlist__new();
771 772
	if (!evlist)
		return -ENOMEM;
773 774 775 776 777 778 779 780 781 782 783

	dup = strdup(id);
	if (!dup)
		return -ENOMEM;

	for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
		*cur = '/';

	ret = __parse_events(evlist, dup, error, fake_pmu);
	free(dup);

784 785 786 787
	evlist__delete(evlist);
	return ret;
}

788
static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
789
{
790 791
	struct parse_events_error error;
	int ret;
792

793 794
	parse_events_error__init(&error);
	ret = check_parse_id(id, &error, NULL);
795 796 797 798 799 800 801 802 803 804
	if (ret && same_cpu) {
		pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
			pe->metric_name, id, pe->metric_expr);
		pr_warning("Error string '%s' help '%s'\n", error.str,
			error.help);
	} else if (ret) {
		pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
			  id, pe->metric_name, pe->metric_expr);
		ret = 0;
	}
805
	parse_events_error__exit(&error);
806 807 808
	return ret;
}

809 810
static int check_parse_fake(const char *id)
{
811 812
	struct parse_events_error error;
	int ret;
813

814 815 816
	parse_events_error__init(&error);
	ret = check_parse_id(id, &error, &perf_pmu__fake);
	parse_events_error__exit(&error);
817 818 819
	return ret;
}

820 821 822 823 824 825 826 827 828 829
static void expr_failure(const char *msg,
			 const struct pmu_events_map *map,
			 const struct pmu_event *pe)
{
	pr_debug("%s for map %s %s %s\n",
		msg, map->cpuid, map->version, map->type);
	pr_debug("On metric %s\n", pe->metric_name);
	pr_debug("On expression %s\n", pe->metric_expr);
}

830 831 832 833 834 835 836
struct metric {
	struct list_head list;
	struct metric_ref metric_ref;
};

static int resolve_metric_simple(struct expr_parse_ctx *pctx,
				 struct list_head *compound_list,
837
				 const struct pmu_events_map *map,
838 839 840 841 842 843 844 845 846 847
				 const char *metric_name)
{
	struct hashmap_entry *cur, *cur_tmp;
	struct metric *metric, *tmp;
	size_t bkt;
	bool all;
	int rc;

	do {
		all = true;
848
		hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
849
			struct metric_ref *ref;
850
			const struct pmu_event *pe;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

			pe = metricgroup__find_metric(cur->key, map);
			if (!pe)
				continue;

			if (!strcmp(metric_name, (char *)cur->key)) {
				pr_warning("Recursion detected for metric %s\n", metric_name);
				rc = -1;
				goto out_err;
			}

			all = false;

			/* The metric key itself needs to go out.. */
			expr__del_id(pctx, cur->key);

			metric = malloc(sizeof(*metric));
			if (!metric) {
				rc = -ENOMEM;
				goto out_err;
			}

			ref = &metric->metric_ref;
			ref->metric_name = pe->metric_name;
			ref->metric_expr = pe->metric_expr;
			list_add_tail(&metric->list, compound_list);

878
			rc = expr__find_ids(pe->metric_expr, NULL, pctx);
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
			if (rc)
				goto out_err;
			break; /* The hashmap has been modified, so restart */
		}
	} while (!all);

	return 0;

out_err:
	list_for_each_entry_safe(metric, tmp, compound_list, list)
		free(metric);

	return rc;

}

895 896
static int test_parsing(void)
{
897 898
	const struct pmu_events_map *cpus_map = pmu_events_map__find();
	const struct pmu_events_map *map;
899
	const struct pmu_event *pe;
900 901
	int i, j, k;
	int ret = 0;
902
	struct expr_parse_ctx *ctx;
903 904
	double result;

905 906 907 908 909
	ctx = expr__ctx_new();
	if (!ctx) {
		pr_debug("expr__ctx_new failed");
		return TEST_FAIL;
	}
910 911 912 913 914 915 916
	i = 0;
	for (;;) {
		map = &pmu_events_map[i++];
		if (!map->table)
			break;
		j = 0;
		for (;;) {
917
			struct metric *metric, *tmp;
918
			struct hashmap_entry *cur;
919
			LIST_HEAD(compound_list);
920 921
			size_t bkt;

922 923 924 925 926
			pe = &map->table[j++];
			if (!pe->name && !pe->metric_group && !pe->metric_name)
				break;
			if (!pe->metric_expr)
				continue;
927
			expr__ctx_clear(ctx);
928
			if (expr__find_ids(pe->metric_expr, NULL, ctx) < 0) {
929
				expr_failure("Parse find ids failed", map, pe);
930 931 932 933
				ret++;
				continue;
			}

934
			if (resolve_metric_simple(ctx, &compound_list, map,
935 936 937 938 939 940
						  pe->metric_name)) {
				expr_failure("Could not resolve metrics", map, pe);
				ret++;
				goto exit; /* Don't tolerate errors due to severity */
			}

941 942 943 944 945
			/*
			 * Add all ids with a made up value. The value may
			 * trigger divide by zero when subtracted and so try to
			 * make them unique.
			 */
946
			k = 1;
947 948
			hashmap__for_each_entry(ctx->ids, cur, bkt)
				expr__add_id_val(ctx, strdup(cur->key), k++);
949

950
			hashmap__for_each_entry(ctx->ids, cur, bkt) {
951
				if (check_parse_cpu(cur->key, map == cpus_map,
952
						   pe))
953 954 955
					ret++;
			}

956
			list_for_each_entry_safe(metric, tmp, &compound_list, list) {
957
				expr__add_ref(ctx, &metric->metric_ref);
958 959 960
				free(metric);
			}

961
			if (expr__parse(&result, ctx, pe->metric_expr)) {
962 963 964 965 966
				expr_failure("Parse failed", map, pe);
				ret++;
			}
		}
	}
967
	expr__ctx_free(ctx);
968
	/* TODO: fail when not ok */
969
exit:
970 971 972
	return ret == 0 ? TEST_OK : TEST_SKIP;
}

973 974 975 976 977 978 979 980 981 982 983 984 985 986
struct test_metric {
	const char *str;
};

static struct test_metric metrics[] = {
	{ "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
	{ "imx8_ddr0@read\\-cycles@ * 4 * 4", },
	{ "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
	{ "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
	{ "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
};

static int metric_parse_fake(const char *str)
{
987
	struct expr_parse_ctx *ctx;
988 989 990 991 992 993 994 995
	struct hashmap_entry *cur;
	double result;
	int ret = -1;
	size_t bkt;
	int i;

	pr_debug("parsing '%s'\n", str);

996 997 998 999 1000
	ctx = expr__ctx_new();
	if (!ctx) {
		pr_debug("expr__ctx_new failed");
		return TEST_FAIL;
	}
1001
	if (expr__find_ids(str, NULL, ctx) < 0) {
1002
		pr_err("expr__find_ids failed\n");
1003 1004 1005 1006 1007 1008 1009 1010 1011
		return -1;
	}

	/*
	 * Add all ids with a made up value. The value may
	 * trigger divide by zero when subtracted and so try to
	 * make them unique.
	 */
	i = 1;
1012 1013
	hashmap__for_each_entry(ctx->ids, cur, bkt)
		expr__add_id_val(ctx, strdup(cur->key), i++);
1014

1015
	hashmap__for_each_entry(ctx->ids, cur, bkt) {
1016 1017 1018 1019 1020 1021
		if (check_parse_fake(cur->key)) {
			pr_err("check_parse_fake failed\n");
			goto out;
		}
	}

1022
	if (expr__parse(&result, ctx, str))
1023 1024 1025 1026 1027
		pr_err("expr__parse failed\n");
	else
		ret = 0;

out:
1028
	expr__ctx_free(ctx);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
	return ret;
}

/*
 * Parse all the metrics for current architecture,
 * or all defined cpus via the 'fake_pmu'
 * in parse_events.
 */
static int test_parsing_fake(void)
{
1039
	const struct pmu_events_map *map;
1040
	const struct pmu_event *pe;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
	unsigned int i, j;
	int err = 0;

	for (i = 0; i < ARRAY_SIZE(metrics); i++) {
		err = metric_parse_fake(metrics[i].str);
		if (err)
			return err;
	}

	i = 0;
	for (;;) {
		map = &pmu_events_map[i++];
		if (!map->table)
			break;
		j = 0;
		for (;;) {
			pe = &map->table[j++];
			if (!pe->name && !pe->metric_group && !pe->metric_name)
				break;
			if (!pe->metric_expr)
				continue;
			err = metric_parse_fake(pe->metric_expr);
			if (err)
				return err;
		}
	}

	return 0;
}

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
static const struct {
	int (*func)(void);
	const char *desc;
} pmu_events_testcase_table[] = {
	{
		.func = test_pmu_event_table,
		.desc = "PMU event table sanity",
	},
	{
		.func = test_aliases,
		.desc = "PMU event map aliases",
	},
	{
		.func = test_parsing,
		.desc = "Parsing of PMU event table metrics",
	},
1087 1088 1089 1090
	{
		.func = test_parsing_fake,
		.desc = "Parsing of PMU event table metrics with fake PMUs",
	},
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
};

const char *test__pmu_events_subtest_get_desc(int subtest)
{
	if (subtest < 0 ||
	    subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
		return NULL;
	return pmu_events_testcase_table[subtest].desc;
}

const char *test__pmu_events_subtest_skip_reason(int subtest)
{
	if (subtest < 0 ||
	    subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
		return NULL;
	if (pmu_events_testcase_table[subtest].func != test_parsing)
		return NULL;
	return "some metrics failed";
}

int test__pmu_events_subtest_get_nr(void)
{
	return (int)ARRAY_SIZE(pmu_events_testcase_table);
}

int test__pmu_events(struct test *test __maybe_unused, int subtest)
{
	if (subtest < 0 ||
	    subtest >= (int)ARRAY_SIZE(pmu_events_testcase_table))
		return TEST_FAIL;
	return pmu_events_testcase_table[subtest].func();
}