metricgroup.c 14.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8
/*
 * Copyright (c) 2017, Intel Corporation.
 */

/* Manage metrics and groups of metrics from JSON files */

#include "metricgroup.h"
9
#include "debug.h"
10
#include "evlist.h"
11
#include "evsel.h"
12 13 14 15 16 17 18 19 20
#include "strbuf.h"
#include "pmu.h"
#include "expr.h"
#include "rblist.h"
#include <string.h>
#include <errno.h>
#include "pmu-events/pmu-events.h"
#include "strlist.h"
#include <assert.h>
21
#include <linux/ctype.h>
22
#include <linux/string.h>
23
#include <linux/zalloc.h>
24
#include <subcmd/parse-options.h>
25 26
#include <api/fs/fs.h>
#include "util.h"
27 28

struct metric_event *metricgroup__lookup(struct rblist *metric_events,
29
					 struct evsel *evsel,
30 31 32 33 34 35
					 bool create)
{
	struct rb_node *nd;
	struct metric_event me = {
		.evsel = evsel
	};
36 37 38 39

	if (!metric_events)
		return NULL;

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	nd = rblist__find(metric_events, &me);
	if (nd)
		return container_of(nd, struct metric_event, nd);
	if (create) {
		rblist__add_node(metric_events, &me);
		nd = rblist__find(metric_events, &me);
		if (nd)
			return container_of(nd, struct metric_event, nd);
	}
	return NULL;
}

static int metric_event_cmp(struct rb_node *rb_node, const void *entry)
{
	struct metric_event *a = container_of(rb_node,
					      struct metric_event,
					      nd);
	const struct metric_event *b = entry;

	if (a->evsel == b->evsel)
		return 0;
	if ((char *)a->evsel < (char *)b->evsel)
		return -1;
	return +1;
}

static struct rb_node *metric_event_new(struct rblist *rblist __maybe_unused,
					const void *entry)
{
	struct metric_event *me = malloc(sizeof(struct metric_event));

	if (!me)
		return NULL;
	memcpy(me, entry, sizeof(struct metric_event));
	me->evsel = ((struct metric_event *)entry)->evsel;
	INIT_LIST_HEAD(&me->head);
	return &me->nd;
}

static void metricgroup__rblist_init(struct rblist *metric_events)
{
	rblist__init(metric_events);
	metric_events->node_cmp = metric_event_cmp;
	metric_events->node_new = metric_event_new;
}

struct egroup {
	struct list_head nd;
88
	struct expr_parse_ctx pctx;
89 90
	const char *metric_name;
	const char *metric_expr;
91
	const char *metric_unit;
92
	int runtime;
93 94
};

95
static struct evsel *find_evsel_group(struct evlist *perf_evlist,
96
				      struct expr_parse_ctx *pctx,
97
				      struct evsel **metric_events,
98
				      unsigned long *evlist_used)
99
{
100 101
	struct evsel *ev;
	bool leader_found;
102 103 104 105
	const size_t idnum = hashmap__size(&pctx->ids);
	size_t i = 0;
	int j = 0;
	double *val_ptr;
106 107

	evlist__for_each_entry (perf_evlist, ev) {
108
		if (test_bit(j++, evlist_used))
109
			continue;
110
		if (hashmap__find(&pctx->ids, ev->name, (void **)&val_ptr)) {
111 112
			if (!metric_events[i])
				metric_events[i] = ev;
113 114 115
			i++;
			if (i == idnum)
				break;
116
		} else {
117 118 119 120
			/* Discard the whole match and start again */
			i = 0;
			memset(metric_events, 0,
				sizeof(struct evsel *) * idnum);
121 122
		}
	}
123

124
	if (i != idnum) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		/* Not whole match */
		return NULL;
	}

	metric_events[idnum] = NULL;

	for (i = 0; i < idnum; i++) {
		leader_found = false;
		evlist__for_each_entry(perf_evlist, ev) {
			if (!leader_found && (ev == metric_events[i]))
				leader_found = true;

			if (leader_found &&
			    !strcmp(ev->name, metric_events[i]->name)) {
				ev->metric_leader = metric_events[i];
			}
141
			j++;
142
		}
143
		ev = metric_events[i];
144
		set_bit(ev->idx, evlist_used);
145 146 147
	}

	return metric_events[0];
148 149 150
}

static int metricgroup__setup_events(struct list_head *groups,
151
				     struct evlist *perf_evlist,
152 153 154 155 156 157 158
				     struct rblist *metric_events_list)
{
	struct metric_event *me;
	struct metric_expr *expr;
	int i = 0;
	int ret = 0;
	struct egroup *eg;
159
	struct evsel *evsel;
160
	unsigned long *evlist_used;
161

162 163 164
	evlist_used = bitmap_alloc(perf_evlist->core.nr_entries);
	if (!evlist_used)
		return -ENOMEM;
165 166

	list_for_each_entry (eg, groups, nd) {
167
		struct evsel **metric_events;
168

169 170
		metric_events = calloc(sizeof(void *),
				hashmap__size(&eg->pctx.ids) + 1);
171 172 173 174
		if (!metric_events) {
			ret = -ENOMEM;
			break;
		}
175 176
		evsel = find_evsel_group(perf_evlist, &eg->pctx, metric_events,
					evlist_used);
177 178 179
		if (!evsel) {
			pr_debug("Cannot resolve %s: %s\n",
					eg->metric_name, eg->metric_expr);
180
			free(metric_events);
181 182
			continue;
		}
183
		for (i = 0; metric_events[i]; i++)
184 185 186 187
			metric_events[i]->collect_stat = true;
		me = metricgroup__lookup(metric_events_list, evsel, true);
		if (!me) {
			ret = -ENOMEM;
188
			free(metric_events);
189 190 191 192 193
			break;
		}
		expr = malloc(sizeof(struct metric_expr));
		if (!expr) {
			ret = -ENOMEM;
194
			free(metric_events);
195 196 197 198
			break;
		}
		expr->metric_expr = eg->metric_expr;
		expr->metric_name = eg->metric_name;
199
		expr->metric_unit = eg->metric_unit;
200
		expr->metric_events = metric_events;
201
		expr->runtime = eg->runtime;
202 203
		list_add(&expr->nd, &me->head);
	}
204

205
	bitmap_free(evlist_used);
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
	return ret;
}

static bool match_metric(const char *n, const char *list)
{
	int len;
	char *m;

	if (!list)
		return false;
	if (!strcmp(list, "all"))
		return true;
	if (!n)
		return !strcasecmp(list, "No_group");
	len = strlen(list);
	m = strcasestr(n, list);
	if (!m)
		return false;
	if ((m == n || m[-1] == ';' || m[-1] == ' ') &&
	    (m[len] == 0 || m[len] == ';'))
		return true;
	return false;
}

231 232 233 234 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
struct mep {
	struct rb_node nd;
	const char *name;
	struct strlist *metrics;
};

static int mep_cmp(struct rb_node *rb_node, const void *entry)
{
	struct mep *a = container_of(rb_node, struct mep, nd);
	struct mep *b = (struct mep *)entry;

	return strcmp(a->name, b->name);
}

static struct rb_node *mep_new(struct rblist *rl __maybe_unused,
					const void *entry)
{
	struct mep *me = malloc(sizeof(struct mep));

	if (!me)
		return NULL;
	memcpy(me, entry, sizeof(struct mep));
	me->name = strdup(me->name);
	if (!me->name)
		goto out_me;
	me->metrics = strlist__new(NULL, NULL);
	if (!me->metrics)
		goto out_name;
	return &me->nd;
out_name:
261
	zfree(&me->name);
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
out_me:
	free(me);
	return NULL;
}

static struct mep *mep_lookup(struct rblist *groups, const char *name)
{
	struct rb_node *nd;
	struct mep me = {
		.name = name
	};
	nd = rblist__find(groups, &me);
	if (nd)
		return container_of(nd, struct mep, nd);
	rblist__add_node(groups, &me);
	nd = rblist__find(groups, &me);
	if (nd)
		return container_of(nd, struct mep, nd);
	return NULL;
}

static void mep_delete(struct rblist *rl __maybe_unused,
		       struct rb_node *nd)
{
	struct mep *me = container_of(nd, struct mep, nd);

	strlist__delete(me->metrics);
289
	zfree(&me->name);
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	free(me);
}

static void metricgroup__print_strlist(struct strlist *metrics, bool raw)
{
	struct str_node *sn;
	int n = 0;

	strlist__for_each_entry (sn, metrics) {
		if (raw)
			printf("%s%s", n > 0 ? " " : "", sn->s);
		else
			printf("  %s\n", sn->s);
		n++;
	}
	if (raw)
		putchar('\n');
}

void metricgroup__print(bool metrics, bool metricgroups, char *filter,
310
			bool raw, bool details)
311
{
312
	struct pmu_events_map *map = perf_pmu__find_map(NULL);
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	struct pmu_event *pe;
	int i;
	struct rblist groups;
	struct rb_node *node, *next;
	struct strlist *metriclist = NULL;

	if (!map)
		return;

	if (!metricgroups) {
		metriclist = strlist__new(NULL, NULL);
		if (!metriclist)
			return;
	}

	rblist__init(&groups);
	groups.node_new = mep_new;
	groups.node_cmp = mep_cmp;
	groups.node_delete = mep_delete;
	for (i = 0; ; i++) {
		const char *g;
		pe = &map->table[i];

		if (!pe->name && !pe->metric_group && !pe->metric_name)
			break;
		if (!pe->metric_expr)
			continue;
		g = pe->metric_group;
		if (!g && pe->metric_name) {
			if (pe->name)
				continue;
			g = "No_group";
		}
		if (g) {
			char *omg;
			char *mg = strdup(g);

			if (!mg)
				return;
			omg = mg;
			while ((g = strsep(&mg, ";")) != NULL) {
				struct mep *me;
				char *s;

357
				g = skip_spaces(g);
358 359 360 361 362 363 364
				if (*g == 0)
					g = "No_group";
				if (filter && !strstr(g, filter))
					continue;
				if (raw)
					s = (char *)pe->metric_name;
				else {
365 366
					if (asprintf(&s, "%s\n%*s%s]",
						     pe->metric_name, 8, "[", pe->desc) < 0)
367
						return;
368 369 370 371 372 373

					if (details) {
						if (asprintf(&s, "%s\n%*s%s]",
							     s, 8, "[", pe->metric_expr) < 0)
							return;
					}
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
				}

				if (!s)
					continue;

				if (!metricgroups) {
					strlist__add(metriclist, s);
				} else {
					me = mep_lookup(&groups, g);
					if (!me)
						continue;
					strlist__add(me->metrics, s);
				}
			}
			free(omg);
		}
	}

	if (metricgroups && !raw)
		printf("\nMetric Groups:\n\n");
	else if (metrics && !raw)
		printf("\nMetrics:\n\n");

397
	for (node = rb_first_cached(&groups.entries); node; node = next) {
398 399 400
		struct mep *me = container_of(node, struct mep, nd);

		if (metricgroups)
401
			printf("%s%s%s", me->name, metrics && !raw ? ":" : "", raw ? " " : "\n");
402 403 404 405 406 407 408 409 410 411
		if (metrics)
			metricgroup__print_strlist(me->metrics, raw);
		next = rb_next(node);
		rblist__remove_node(&groups, node);
	}
	if (!metricgroups)
		metricgroup__print_strlist(metriclist, raw);
	strlist__delete(metriclist);
}

412
static void metricgroup__add_metric_weak_group(struct strbuf *events,
413
					       struct expr_parse_ctx *ctx)
414
{
415
	struct hashmap_entry *cur;
416 417
	size_t bkt;
	bool no_group = true, has_duration = false;
418

419 420
	hashmap__for_each_entry((&ctx->ids), cur, bkt) {
		pr_debug("found event %s\n", (const char *)cur->key);
421 422 423 424 425
		/*
		 * Duration time maps to a software event and can make
		 * groups not count. Always use it outside a
		 * group.
		 */
426
		if (!strcmp(cur->key, "duration_time")) {
427
			has_duration = true;
428 429 430
			continue;
		}
		strbuf_addf(events, "%s%s",
431
			no_group ? "{" : ",",
432
			(const char *)cur->key);
433 434
		no_group = false;
	}
435
	if (!no_group) {
436
		strbuf_addf(events, "}:W");
437 438 439 440
		if (has_duration)
			strbuf_addf(events, ",duration_time");
	} else if (has_duration)
		strbuf_addf(events, "duration_time");
441 442
}

443
static void metricgroup__add_metric_non_group(struct strbuf *events,
444
					      struct expr_parse_ctx *ctx)
445
{
446 447
	struct hashmap_entry *cur;
	size_t bkt;
448

449 450
	hashmap__for_each_entry((&ctx->ids), cur, bkt)
		strbuf_addf(events, ",%s", (const char *)cur->key);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
}

static void metricgroup___watchdog_constraint_hint(const char *name, bool foot)
{
	static bool violate_nmi_constraint;

	if (!foot) {
		pr_warning("Splitting metric group %s into standalone metrics.\n", name);
		violate_nmi_constraint = true;
		return;
	}

	if (!violate_nmi_constraint)
		return;

	pr_warning("Try disabling the NMI watchdog to comply NO_NMI_WATCHDOG metric constraint:\n"
		   "    echo 0 > /proc/sys/kernel/nmi_watchdog\n"
		   "    perf stat ...\n"
		   "    echo 1 > /proc/sys/kernel/nmi_watchdog\n");
}

static bool metricgroup__has_constraint(struct pmu_event *pe)
{
	if (!pe->metric_constraint)
		return false;

	if (!strcmp(pe->metric_constraint, "NO_NMI_WATCHDOG") &&
	    sysctl__nmi_watchdog_enabled()) {
		metricgroup___watchdog_constraint_hint(pe->metric_name, false);
		return true;
	}

	return false;
}

486 487 488 489 490
int __weak arch_get_runtimeparam(void)
{
	return 1;
}

491
static int __metricgroup__add_metric(struct strbuf *events,
492
		struct list_head *group_list, struct pmu_event *pe, int runtime)
493 494 495 496 497 498 499
{
	struct egroup *eg;

	eg = malloc(sizeof(*eg));
	if (!eg)
		return -ENOMEM;

500
	expr__ctx_init(&eg->pctx);
501 502 503
	eg->metric_name = pe->metric_name;
	eg->metric_expr = pe->metric_expr;
	eg->metric_unit = pe->unit;
504
	eg->runtime = runtime;
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

	if (expr__find_other(pe->metric_expr, NULL, &eg->pctx, runtime) < 0) {
		expr__ctx_clear(&eg->pctx);
		free(eg);
		return -EINVAL;
	}

	if (events->len > 0)
		strbuf_addf(events, ",");

	if (metricgroup__has_constraint(pe))
		metricgroup__add_metric_non_group(events, &eg->pctx);
	else
		metricgroup__add_metric_weak_group(events, &eg->pctx);

520 521 522 523 524
	list_add_tail(&eg->nd, group_list);

	return 0;
}

525 526 527
static int metricgroup__add_metric(const char *metric, struct strbuf *events,
				   struct list_head *group_list)
{
528
	struct pmu_events_map *map = perf_pmu__find_map(NULL);
529
	struct pmu_event *pe;
530
	int i, ret = -EINVAL;
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

	if (!map)
		return 0;

	for (i = 0; ; i++) {
		pe = &map->table[i];

		if (!pe->name && !pe->metric_group && !pe->metric_name)
			break;
		if (!pe->metric_expr)
			continue;
		if (match_metric(pe->metric_group, metric) ||
		    match_metric(pe->metric_name, metric)) {

			pr_debug("metric expr %s for %s\n", pe->metric_expr, pe->metric_name);

547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
			if (!strstr(pe->metric_expr, "?")) {
				ret = __metricgroup__add_metric(events, group_list, pe, 1);
			} else {
				int j, count;

				count = arch_get_runtimeparam();

				/* This loop is added to create multiple
				 * events depend on count value and add
				 * those events to group_list.
				 */

				for (j = 0; j < count; j++)
					ret = __metricgroup__add_metric(events, group_list, pe, j);
			}
562
			if (ret == -ENOMEM)
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
				break;
		}
	}
	return ret;
}

static int metricgroup__add_metric_list(const char *list, struct strbuf *events,
				        struct list_head *group_list)
{
	char *llist, *nlist, *p;
	int ret = -EINVAL;

	nlist = strdup(list);
	if (!nlist)
		return -ENOMEM;
	llist = nlist;
579 580 581 582

	strbuf_init(events, 100);
	strbuf_addf(events, "%s", "");

583 584 585 586 587 588 589 590 591
	while ((p = strsep(&llist, ",")) != NULL) {
		ret = metricgroup__add_metric(p, events, group_list);
		if (ret == -EINVAL) {
			fprintf(stderr, "Cannot find metric or group `%s'\n",
					p);
			break;
		}
	}
	free(nlist);
592 593 594 595

	if (!ret)
		metricgroup___watchdog_constraint_hint(NULL, true);

596 597 598 599 600 601 602 603
	return ret;
}

static void metricgroup__free_egroups(struct list_head *group_list)
{
	struct egroup *eg, *egtmp;

	list_for_each_entry_safe (eg, egtmp, group_list, nd) {
604
		expr__ctx_clear(&eg->pctx);
605
		list_del_init(&eg->nd);
606 607 608 609 610 611 612 613 614
		free(eg);
	}
}

int metricgroup__parse_groups(const struct option *opt,
			   const char *str,
			   struct rblist *metric_events)
{
	struct parse_events_error parse_error;
615
	struct evlist *perf_evlist = *(struct evlist **)opt->value;
616 617 618 619 620 621 622 623 624 625
	struct strbuf extra_events;
	LIST_HEAD(group_list);
	int ret;

	if (metric_events->nr_entries == 0)
		metricgroup__rblist_init(metric_events);
	ret = metricgroup__add_metric_list(str, &extra_events, &group_list);
	if (ret)
		return ret;
	pr_debug("adding %s\n", extra_events.buf);
626
	bzero(&parse_error, sizeof(parse_error));
627 628
	ret = parse_events(perf_evlist, extra_events.buf, &parse_error);
	if (ret) {
629
		parse_events_print_error(&parse_error, extra_events.buf);
630 631 632 633 634 635 636 637 638
		goto out;
	}
	strbuf_release(&extra_events);
	ret = metricgroup__setup_events(&group_list, perf_evlist,
					metric_events);
out:
	metricgroup__free_egroups(&group_list);
	return ret;
}
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

bool metricgroup__has_metric(const char *metric)
{
	struct pmu_events_map *map = perf_pmu__find_map(NULL);
	struct pmu_event *pe;
	int i;

	if (!map)
		return false;

	for (i = 0; ; i++) {
		pe = &map->table[i];

		if (!pe->name && !pe->metric_group && !pe->metric_name)
			break;
		if (!pe->metric_expr)
			continue;
		if (match_metric(pe->metric_name, metric))
			return true;
	}
	return false;
}