pmu-events.c 18.9 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;
J
John Garry 已提交
31
};
J
John Garry 已提交
32

33 34 35 36 37 38
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 已提交
39
	},
40 41 42 43 44 45 46 47 48 49
	.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 已提交
50
	},
51 52 53 54 55 56 57 58 59 60
	.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 已提交
61
	},
62 63 64 65 66 67 68 69 70 71
	.alias_str = "umask=0x80,(null)=0x30d40,event=0x6",
	.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 已提交
72
	},
73 74 75 76 77 78 79 80 81 82
	.alias_str = "umask=0x20,(null)=0x30d40,event=0x9",
	.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",
83
	},
84 85 86 87 88 89 90 91 92 93 94
	.alias_str = "umask=0,(null)=0x30d40,event=0x3a",
	.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 已提交
95
	},
96 97
	.alias_str = "event=0x40",
	.alias_long_desc = "Attributable Level 3 cache access, read",
J
John Garry 已提交
98 99
};

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
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 已提交
118
	},
119 120 121 122 123 124 125 126 127 128 129 130
	.alias_str = "event=0x2",
	.alias_long_desc = "DDRC write commands",
};

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 已提交
131
	},
132 133
	.alias_str = "umask=0x81,event=0x22",
	.alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
J
John Garry 已提交
134 135
};

136 137 138 139 140
static const struct perf_pmu_test_event *uncore_events[] = {
	&uncore_hisi_ddrc_flux_wcmd,
	&unc_cbo_xsnp_response_miss_eviction,
	NULL
};
J
John Garry 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

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);
}

static struct pmu_events_map *__test_pmu_get_events_map(void)
{
	struct pmu_events_map *map;

	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;
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
static int compare_pmu_events(struct pmu_event *e1, const struct pmu_event *e2)
{
	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;
	}

	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;
	}

	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;
	}

	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);
		return -1;
	}

	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;
	}

	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;
	}

	return 0;
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
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;
}

289
/* Verify generated events from pmu-events.c are as expected */
290
static int test_pmu_event_table(void)
J
John Garry 已提交
291 292 293 294 295 296
{
	struct pmu_events_map *map = __test_pmu_get_events_map();
	struct pmu_event *table;
	int map_events = 0, expected_events;

	/* ignore 2x sentinels */
297 298
	expected_events = ARRAY_SIZE(core_events) +
			  ARRAY_SIZE(uncore_events) - 2;
J
John Garry 已提交
299 300 301 302 303

	if (!map)
		return -1;

	for (table = map->table; table->name; table++) {
304
		struct perf_pmu_test_event const **test_event_table;
J
John Garry 已提交
305 306 307
		bool found = false;

		if (table->pmu)
308
			test_event_table = &uncore_events[0];
J
John Garry 已提交
309
		else
310
			test_event_table = &core_events[0];
J
John Garry 已提交
311

312 313 314
		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 已提交
315

316
			if (strcmp(table->name, event->name))
J
John Garry 已提交
317 318 319 320
				continue;
			found = true;
			map_events++;

321
			if (compare_pmu_events(table, event))
J
John Garry 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
				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;
		}
	}

	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 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354

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 */
355
static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
J
John Garry 已提交
356
{
357
	struct perf_pmu_test_event const **test_event_table;
J
John Garry 已提交
358 359 360 361
	struct perf_pmu *pmu;
	LIST_HEAD(aliases);
	int res = 0;
	struct pmu_events_map *map = __test_pmu_get_events_map();
362
	struct perf_pmu_alias *a, *tmp;
J
John Garry 已提交
363 364 365 366

	if (!map)
		return -1;

367
	test_event_table = &core_events[0];
J
John Garry 已提交
368 369 370 371 372 373 374 375 376

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

	pmu->name = pmu_name;

	pmu_add_cpu_aliases_map(&aliases, pmu, map);

377 378 379 380
	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 已提交
381 382

		if (!alias) {
383
			pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
384
				  pmu_name, event->name);
J
John Garry 已提交
385 386 387 388
			res = -1;
			break;
		}

389
		if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
J
John Garry 已提交
390 391 392 393 394
			res = -1;
			break;
		}

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

399 400 401 402
	list_for_each_entry_safe(a, tmp, &aliases, list) {
		list_del(&a->list);
		perf_pmu_free_alias(a);
	}
J
John Garry 已提交
403 404 405 406
	free(pmu);
	return res;
}

407
/* Test that aliases generated are as expected */
408
static int test_aliases(void)
J
John Garry 已提交
409
{
J
John Garry 已提交
410 411 412 413 414
	struct perf_pmu *pmu = NULL;

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

415 416 417
		if (!is_pmu_core(pmu->name))
			continue;

J
John Garry 已提交
418
		if (list_empty(&pmu->format)) {
419
			pr_debug2("skipping testing core PMU %s\n", pmu->name);
J
John Garry 已提交
420 421 422
			continue;
		}

423 424
		if (__test_core_pmu_event_aliases(pmu->name, &count)) {
			pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
J
John Garry 已提交
425 426 427
			return -1;
		}

428 429
		if (count == 0) {
			pr_debug("testing core PMU %s aliases: no events to match\n",
J
John Garry 已提交
430
				  pmu->name);
431 432 433 434
			return -1;
		}

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

J
John Garry 已提交
437 438
	return 0;
}
439 440 441 442 443 444 445 446 447 448 449 450

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;
}

451 452
static int check_parse_id(const char *id, struct parse_events_error *error,
			  struct perf_pmu *fake_pmu)
453 454 455 456 457 458 459 460 461
{
	struct evlist *evlist;
	int ret;

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

	evlist = evlist__new();
462 463
	if (!evlist)
		return -ENOMEM;
464
	ret = __parse_events(evlist, id, error, fake_pmu);
465 466 467 468 469 470 471 472
	evlist__delete(evlist);
	return ret;
}

static int check_parse_cpu(const char *id, bool same_cpu, struct pmu_event *pe)
{
	struct parse_events_error error = { .idx = 0, };

473
	int ret = check_parse_id(id, &error, NULL);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	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;
	}
	free(error.str);
	free(error.help);
	free(error.first_str);
	free(error.first_help);
	return ret;
}

491 492 493 494 495 496 497 498 499 500 501 502
static int check_parse_fake(const char *id)
{
	struct parse_events_error error = { .idx = 0, };
	int ret = check_parse_id(id, &error, &perf_pmu__fake);

	free(error.str);
	free(error.help);
	free(error.first_str);
	free(error.first_help);
	return ret;
}

503 504 505 506 507 508 509 510 511 512
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);
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
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,
				 struct pmu_events_map *map,
				 const char *metric_name)
{
	struct hashmap_entry *cur, *cur_tmp;
	struct metric *metric, *tmp;
	size_t bkt;
	bool all;
	int rc;

	do {
		all = true;
		hashmap__for_each_entry_safe((&pctx->ids), cur, cur_tmp, bkt) {
			struct metric_ref *ref;
			struct pmu_event *pe;

			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);

			rc = expr__find_other(pe->metric_expr, NULL, pctx, 0);
			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;

}

578 579
static int test_parsing(void)
{
580
	struct pmu_events_map *cpus_map = pmu_events_map__find();
581 582 583 584 585 586 587 588 589 590 591 592 593 594
	struct pmu_events_map *map;
	struct pmu_event *pe;
	int i, j, k;
	int ret = 0;
	struct expr_parse_ctx ctx;
	double result;

	i = 0;
	for (;;) {
		map = &pmu_events_map[i++];
		if (!map->table)
			break;
		j = 0;
		for (;;) {
595
			struct metric *metric, *tmp;
596
			struct hashmap_entry *cur;
597
			LIST_HEAD(compound_list);
598 599
			size_t bkt;

600 601 602 603 604
			pe = &map->table[j++];
			if (!pe->name && !pe->metric_group && !pe->metric_name)
				break;
			if (!pe->metric_expr)
				continue;
605 606 607
			expr__ctx_init(&ctx);
			if (expr__find_other(pe->metric_expr, NULL, &ctx, 0)
				  < 0) {
608 609 610 611 612
				expr_failure("Parse other failed", map, pe);
				ret++;
				continue;
			}

613 614 615 616 617 618 619
			if (resolve_metric_simple(&ctx, &compound_list, map,
						  pe->metric_name)) {
				expr_failure("Could not resolve metrics", map, pe);
				ret++;
				goto exit; /* Don't tolerate errors due to severity */
			}

620 621 622 623 624
			/*
			 * Add all ids with a made up value. The value may
			 * trigger divide by zero when subtracted and so try to
			 * make them unique.
			 */
625 626
			k = 1;
			hashmap__for_each_entry((&ctx.ids), cur, bkt)
627
				expr__add_id_val(&ctx, strdup(cur->key), k++);
628

629
			hashmap__for_each_entry((&ctx.ids), cur, bkt) {
630
				if (check_parse_cpu(cur->key, map == cpus_map,
631
						   pe))
632 633 634
					ret++;
			}

635 636 637 638 639
			list_for_each_entry_safe(metric, tmp, &compound_list, list) {
				expr__add_ref(&ctx, &metric->metric_ref);
				free(metric);
			}

640 641 642 643
			if (expr__parse(&result, &ctx, pe->metric_expr, 0)) {
				expr_failure("Parse failed", map, pe);
				ret++;
			}
644
			expr__ctx_clear(&ctx);
645 646 647
		}
	}
	/* TODO: fail when not ok */
648
exit:
649 650 651
	return ret == 0 ? TEST_OK : TEST_SKIP;
}

652 653 654 655 656 657 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 685 686 687
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)
{
	struct expr_parse_ctx ctx;
	struct hashmap_entry *cur;
	double result;
	int ret = -1;
	size_t bkt;
	int i;

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

	expr__ctx_init(&ctx);
	if (expr__find_other(str, NULL, &ctx, 0) < 0) {
		pr_err("expr__find_other failed\n");
		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;
	hashmap__for_each_entry((&ctx.ids), cur, bkt)
688
		expr__add_id_val(&ctx, strdup(cur->key), i++);
689 690 691 692 693 694 695 696

	hashmap__for_each_entry((&ctx.ids), cur, bkt) {
		if (check_parse_fake(cur->key)) {
			pr_err("check_parse_fake failed\n");
			goto out;
		}
	}

K
Kajol Jain 已提交
697
	if (expr__parse(&result, &ctx, str, 0))
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
		pr_err("expr__parse failed\n");
	else
		ret = 0;

out:
	expr__ctx_clear(&ctx);
	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)
{
	struct pmu_events_map *map;
	struct pmu_event *pe;
	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;
}

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
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",
	},
762 763 764 765
	{
		.func = test_parsing_fake,
		.desc = "Parsing of PMU event table metrics with fake PMUs",
	},
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
};

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();
}