Linux Audio

Check our new training course

Linux kernel drivers training

May 6-19, 2025
Register
Loading...
v6.13.7
   1#include <stdlib.h>
   2#include <stdio.h>
   3#include <inttypes.h>
   4#include <linux/string.h>
   5#include <linux/time64.h>
   6#include <math.h>
   7#include <perf/cpumap.h>
   8#include "color.h"
   9#include "counts.h"
  10#include "debug.h"
  11#include "evlist.h"
  12#include "evsel.h"
  13#include "stat.h"
  14#include "top.h"
  15#include "thread_map.h"
  16#include "cpumap.h"
  17#include "string2.h"
  18#include <linux/ctype.h>
  19#include "cgroup.h"
  20#include <api/fs/fs.h>
  21#include "util.h"
  22#include "iostat.h"
  23#include "pmu.h"
  24#include "pmus.h"
  25#include "tool_pmu.h"
  26
  27#define CNTR_NOT_SUPPORTED	"<not supported>"
  28#define CNTR_NOT_COUNTED	"<not counted>"
  29
  30#define MGROUP_LEN   50
  31#define METRIC_LEN   38
  32#define EVNAME_LEN   32
  33#define COUNTS_LEN   18
  34#define INTERVAL_LEN 16
  35#define CGROUP_LEN   16
  36#define COMM_LEN     16
  37#define PID_LEN       7
  38#define CPUS_LEN      4
  39
  40static int aggr_header_lens[] = {
  41	[AGGR_CORE] 	= 18,
  42	[AGGR_CACHE]	= 22,
  43	[AGGR_CLUSTER]	= 20,
  44	[AGGR_DIE] 	= 12,
  45	[AGGR_SOCKET] 	= 6,
  46	[AGGR_NODE] 	= 6,
  47	[AGGR_NONE] 	= 6,
  48	[AGGR_THREAD] 	= 16,
  49	[AGGR_GLOBAL] 	= 0,
  50};
  51
  52static const char *aggr_header_csv[] = {
  53	[AGGR_CORE] 	= 	"core,cpus,",
  54	[AGGR_CACHE]	= 	"cache,cpus,",
  55	[AGGR_CLUSTER]	= 	"cluster,cpus,",
  56	[AGGR_DIE] 	= 	"die,cpus,",
  57	[AGGR_SOCKET] 	= 	"socket,cpus,",
  58	[AGGR_NONE] 	= 	"cpu,",
  59	[AGGR_THREAD] 	= 	"comm-pid,",
  60	[AGGR_NODE] 	= 	"node,",
  61	[AGGR_GLOBAL] 	=	""
  62};
  63
  64static const char *aggr_header_std[] = {
  65	[AGGR_CORE] 	= 	"core",
  66	[AGGR_CACHE] 	= 	"cache",
  67	[AGGR_CLUSTER]	= 	"cluster",
  68	[AGGR_DIE] 	= 	"die",
  69	[AGGR_SOCKET] 	= 	"socket",
  70	[AGGR_NONE] 	= 	"cpu",
  71	[AGGR_THREAD] 	= 	"comm-pid",
  72	[AGGR_NODE] 	= 	"node",
  73	[AGGR_GLOBAL] 	=	""
  74};
  75
  76const char *metric_threshold_classify__color(enum metric_threshold_classify thresh)
  77{
  78	const char * const colors[] = {
  79		"", /* unknown */
  80		PERF_COLOR_RED,     /* bad */
  81		PERF_COLOR_MAGENTA, /* nearly bad */
  82		PERF_COLOR_YELLOW,  /* less good */
  83		PERF_COLOR_GREEN,   /* good */
  84	};
  85	static_assert(ARRAY_SIZE(colors) - 1  == METRIC_THRESHOLD_GOOD, "missing enum value");
  86	return colors[thresh];
  87}
  88
  89static const char *metric_threshold_classify__str(enum metric_threshold_classify thresh)
  90{
  91	const char * const strs[] = {
  92		"unknown",
  93		"bad",
  94		"nearly bad",
  95		"less good",
  96		"good",
  97	};
  98	static_assert(ARRAY_SIZE(strs) - 1  == METRIC_THRESHOLD_GOOD, "missing enum value");
  99	return strs[thresh];
 100}
 101
 102static void print_running_std(struct perf_stat_config *config, u64 run, u64 ena)
 103{
 104	if (run != ena)
 105		fprintf(config->output, "  (%.2f%%)", 100.0 * run / ena);
 106}
 107
 108static void print_running_csv(struct perf_stat_config *config, u64 run, u64 ena)
 109{
 110	double enabled_percent = 100;
 111
 112	if (run != ena)
 113		enabled_percent = 100 * run / ena;
 114	fprintf(config->output, "%s%" PRIu64 "%s%.2f",
 115		config->csv_sep, run, config->csv_sep, enabled_percent);
 116}
 117struct outstate {
 118	FILE *fh;
 119	bool newline;
 120	bool first;
 121	const char *prefix;
 122	int  nfields;
 123	int  aggr_nr;
 124	struct aggr_cpu_id id;
 125	struct evsel *evsel;
 126	struct cgroup *cgrp;
 127};
 128
 129static const char *json_sep(struct outstate *os)
 130{
 131	const char *sep = os->first ? "" : ", ";
 132
 133	os->first = false;
 134	return sep;
 135}
 136
 137#define json_out(os, format, ...) fprintf((os)->fh, "%s" format, json_sep(os), ##__VA_ARGS__)
 138
 139static void print_running_json(struct outstate *os, u64 run, u64 ena)
 140{
 141	double enabled_percent = 100;
 142
 143	if (run != ena)
 144		enabled_percent = 100 * run / ena;
 145	json_out(os, "\"event-runtime\" : %" PRIu64 ", \"pcnt-running\" : %.2f",
 146		 run, enabled_percent);
 147}
 148
 149static void print_running(struct perf_stat_config *config, struct outstate *os,
 150			  u64 run, u64 ena, bool before_metric)
 151{
 152	if (config->json_output) {
 153		if (before_metric)
 154			print_running_json(os, run, ena);
 155	} else if (config->csv_output) {
 156		if (before_metric)
 157			print_running_csv(config, run, ena);
 158	} else {
 159		if (!before_metric)
 160			print_running_std(config, run, ena);
 161	}
 162}
 163
 164static void print_noise_pct_std(struct perf_stat_config *config,
 165				double pct)
 166{
 167	if (pct)
 168		fprintf(config->output, "  ( +-%6.2f%% )", pct);
 169}
 170
 171static void print_noise_pct_csv(struct perf_stat_config *config,
 172				double pct)
 173{
 174	fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
 175}
 176
 177static void print_noise_pct_json(struct outstate *os,
 178				 double pct)
 179{
 180	json_out(os, "\"variance\" : %.2f", pct);
 181}
 182
 183static void print_noise_pct(struct perf_stat_config *config, struct outstate *os,
 184			    double total, double avg, bool before_metric)
 185{
 186	double pct = rel_stddev_stats(total, avg);
 187
 188	if (config->json_output) {
 189		if (before_metric)
 190			print_noise_pct_json(os, pct);
 191	} else if (config->csv_output) {
 192		if (before_metric)
 193			print_noise_pct_csv(config, pct);
 194	} else {
 195		if (!before_metric)
 196			print_noise_pct_std(config, pct);
 197	}
 198}
 199
 200static void print_noise(struct perf_stat_config *config, struct outstate *os,
 201			struct evsel *evsel, double avg, bool before_metric)
 202{
 203	struct perf_stat_evsel *ps;
 204
 205	if (config->run_count == 1)
 206		return;
 207
 208	ps = evsel->stats;
 209	print_noise_pct(config, os, stddev_stats(&ps->res_stats), avg, before_metric);
 210}
 211
 212static void print_cgroup_std(struct perf_stat_config *config, const char *cgrp_name)
 213{
 214	fprintf(config->output, " %-*s", CGROUP_LEN, cgrp_name);
 215}
 216
 217static void print_cgroup_csv(struct perf_stat_config *config, const char *cgrp_name)
 218{
 219	fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
 220}
 221
 222static void print_cgroup_json(struct outstate *os, const char *cgrp_name)
 223{
 224	json_out(os, "\"cgroup\" : \"%s\"", cgrp_name);
 225}
 226
 227static void print_cgroup(struct perf_stat_config *config, struct outstate *os,
 228			 struct cgroup *cgrp)
 229{
 230	if (nr_cgroups || config->cgroup_list) {
 231		const char *cgrp_name = cgrp ? cgrp->name  : "";
 232
 233		if (config->json_output)
 234			print_cgroup_json(os, cgrp_name);
 235		else if (config->csv_output)
 236			print_cgroup_csv(config, cgrp_name);
 237		else
 238			print_cgroup_std(config, cgrp_name);
 239	}
 240}
 241
 242static void print_aggr_id_std(struct perf_stat_config *config,
 243			      struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
 244{
 245	FILE *output = config->output;
 246	int idx = config->aggr_mode;
 247	char buf[128];
 248
 249	switch (config->aggr_mode) {
 250	case AGGR_CORE:
 251		snprintf(buf, sizeof(buf), "S%d-D%d-C%d", id.socket, id.die, id.core);
 252		break;
 253	case AGGR_CACHE:
 254		snprintf(buf, sizeof(buf), "S%d-D%d-L%d-ID%d",
 255			 id.socket, id.die, id.cache_lvl, id.cache);
 256		break;
 257	case AGGR_CLUSTER:
 258		snprintf(buf, sizeof(buf), "S%d-D%d-CLS%d", id.socket, id.die, id.cluster);
 259		break;
 260	case AGGR_DIE:
 261		snprintf(buf, sizeof(buf), "S%d-D%d", id.socket, id.die);
 262		break;
 263	case AGGR_SOCKET:
 264		snprintf(buf, sizeof(buf), "S%d", id.socket);
 265		break;
 266	case AGGR_NODE:
 267		snprintf(buf, sizeof(buf), "N%d", id.node);
 268		break;
 269	case AGGR_NONE:
 270		if (evsel->percore && !config->percore_show_thread) {
 271			snprintf(buf, sizeof(buf), "S%d-D%d-C%d ",
 272				id.socket, id.die, id.core);
 273			fprintf(output, "%-*s ",
 274				aggr_header_lens[AGGR_CORE], buf);
 275		} else if (id.cpu.cpu > -1) {
 276			fprintf(output, "CPU%-*d ",
 277				aggr_header_lens[AGGR_NONE] - 3, id.cpu.cpu);
 278		}
 279		return;
 280	case AGGR_THREAD:
 281		fprintf(output, "%*s-%-*d ",
 282			COMM_LEN, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
 283			PID_LEN, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
 284		return;
 285	case AGGR_GLOBAL:
 286	case AGGR_UNSET:
 287	case AGGR_MAX:
 288	default:
 289		return;
 290	}
 291
 292	fprintf(output, "%-*s %*d ", aggr_header_lens[idx], buf, 4, aggr_nr);
 293}
 294
 295static void print_aggr_id_csv(struct perf_stat_config *config,
 296			      struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
 297{
 298	FILE *output = config->output;
 299	const char *sep = config->csv_sep;
 300
 301	switch (config->aggr_mode) {
 302	case AGGR_CORE:
 303		fprintf(output, "S%d-D%d-C%d%s%d%s",
 304			id.socket, id.die, id.core, sep, aggr_nr, sep);
 305		break;
 306	case AGGR_CACHE:
 307		fprintf(config->output, "S%d-D%d-L%d-ID%d%s%d%s",
 308			id.socket, id.die, id.cache_lvl, id.cache, sep, aggr_nr, sep);
 309		break;
 310	case AGGR_CLUSTER:
 311		fprintf(config->output, "S%d-D%d-CLS%d%s%d%s",
 312			id.socket, id.die, id.cluster, sep, aggr_nr, sep);
 313		break;
 314	case AGGR_DIE:
 315		fprintf(output, "S%d-D%d%s%d%s",
 316			id.socket, id.die, sep, aggr_nr, sep);
 
 
 
 
 
 
 317		break;
 318	case AGGR_SOCKET:
 319		fprintf(output, "S%d%s%d%s",
 320			id.socket, sep, aggr_nr, sep);
 321		break;
 
 
 
 
 
 322	case AGGR_NODE:
 323		fprintf(output, "N%d%s%d%s",
 324			id.node, sep, aggr_nr, sep);
 325		break;
 
 
 
 
 
 326	case AGGR_NONE:
 327		if (evsel->percore && !config->percore_show_thread) {
 328			fprintf(output, "S%d-D%d-C%d%s",
 329				id.socket, id.die, id.core, sep);
 330		} else if (id.cpu.cpu > -1) {
 331			fprintf(output, "CPU%d%s",
 332				id.cpu.cpu, sep);
 
 
 
 
 
 333		}
 334		break;
 335	case AGGR_THREAD:
 336		fprintf(output, "%s-%d%s",
 337			perf_thread_map__comm(evsel->core.threads, id.thread_idx),
 338			perf_thread_map__pid(evsel->core.threads, id.thread_idx),
 339			sep);
 
 
 340		break;
 341	case AGGR_GLOBAL:
 342	case AGGR_UNSET:
 343	case AGGR_MAX:
 344	default:
 345		break;
 346	}
 347}
 348
 349static void print_aggr_id_json(struct perf_stat_config *config, struct outstate *os,
 350			       struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
 351{
 352	switch (config->aggr_mode) {
 353	case AGGR_CORE:
 354		json_out(os, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d",
 355			id.socket, id.die, id.core, aggr_nr);
 356		break;
 357	case AGGR_CACHE:
 358		json_out(os, "\"cache\" : \"S%d-D%d-L%d-ID%d\", \"aggregate-number\" : %d",
 359			id.socket, id.die, id.cache_lvl, id.cache, aggr_nr);
 360		break;
 361	case AGGR_CLUSTER:
 362		json_out(os, "\"cluster\" : \"S%d-D%d-CLS%d\", \"aggregate-number\" : %d",
 363			id.socket, id.die, id.cluster, aggr_nr);
 364		break;
 365	case AGGR_DIE:
 366		json_out(os, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d",
 367			id.socket, id.die, aggr_nr);
 368		break;
 369	case AGGR_SOCKET:
 370		json_out(os, "\"socket\" : \"S%d\", \"aggregate-number\" : %d",
 371			id.socket, aggr_nr);
 372		break;
 373	case AGGR_NODE:
 374		json_out(os, "\"node\" : \"N%d\", \"aggregate-number\" : %d",
 375			id.node, aggr_nr);
 376		break;
 377	case AGGR_NONE:
 378		if (evsel->percore && !config->percore_show_thread) {
 379			json_out(os, "\"core\" : \"S%d-D%d-C%d\"",
 380				id.socket, id.die, id.core);
 381		} else if (id.cpu.cpu > -1) {
 382			json_out(os, "\"cpu\" : \"%d\"",
 383				id.cpu.cpu);
 384		}
 385		break;
 386	case AGGR_THREAD:
 387		json_out(os, "\"thread\" : \"%s-%d\"",
 388			perf_thread_map__comm(evsel->core.threads, id.thread_idx),
 389			perf_thread_map__pid(evsel->core.threads, id.thread_idx));
 390		break;
 391	case AGGR_GLOBAL:
 392	case AGGR_UNSET:
 393	case AGGR_MAX:
 394	default:
 395		break;
 396	}
 397}
 398
 399static void aggr_printout(struct perf_stat_config *config, struct outstate *os,
 400			  struct evsel *evsel, struct aggr_cpu_id id, int aggr_nr)
 401{
 402	if (config->json_output)
 403		print_aggr_id_json(config, os, evsel, id, aggr_nr);
 404	else if (config->csv_output)
 405		print_aggr_id_csv(config, evsel, id, aggr_nr);
 406	else
 407		print_aggr_id_std(config, evsel, id, aggr_nr);
 408}
 409
 410static void new_line_std(struct perf_stat_config *config __maybe_unused,
 411			 void *ctx)
 412{
 413	struct outstate *os = ctx;
 414
 415	os->newline = true;
 416}
 417
 418static inline void __new_line_std_csv(struct perf_stat_config *config,
 419				      struct outstate *os)
 420{
 421	fputc('\n', os->fh);
 422	if (os->prefix)
 423		fputs(os->prefix, os->fh);
 424	aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
 425}
 426
 427static inline void __new_line_std(struct outstate *os)
 428{
 429	fprintf(os->fh, "                                                 ");
 430}
 431
 432static void do_new_line_std(struct perf_stat_config *config,
 433			    struct outstate *os)
 434{
 435	__new_line_std_csv(config, os);
 
 
 436	if (config->aggr_mode == AGGR_NONE)
 437		fprintf(os->fh, "        ");
 438	__new_line_std(os);
 439}
 440
 441static void print_metric_std(struct perf_stat_config *config,
 442			     void *ctx, enum metric_threshold_classify thresh,
 443			     const char *fmt, const char *unit, double val)
 444{
 445	struct outstate *os = ctx;
 446	FILE *out = os->fh;
 447	int n;
 448	bool newline = os->newline;
 449	const char *color = metric_threshold_classify__color(thresh);
 450
 451	os->newline = false;
 452
 453	if (unit == NULL || fmt == NULL) {
 454		fprintf(out, "%-*s", METRIC_LEN, "");
 455		return;
 456	}
 457
 458	if (newline)
 459		do_new_line_std(config, os);
 460
 461	n = fprintf(out, " # ");
 462	if (color)
 463		n += color_fprintf(out, color, fmt, val);
 464	else
 465		n += fprintf(out, fmt, val);
 466	fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
 467}
 468
 469static void new_line_csv(struct perf_stat_config *config, void *ctx)
 470{
 471	struct outstate *os = ctx;
 472	int i;
 473
 474	__new_line_std_csv(config, os);
 
 
 
 475	for (i = 0; i < os->nfields; i++)
 476		fputs(config->csv_sep, os->fh);
 477}
 478
 479static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
 480			     void *ctx,
 481			     enum metric_threshold_classify thresh __maybe_unused,
 482			     const char *fmt, const char *unit, double val)
 483{
 484	struct outstate *os = ctx;
 485	FILE *out = os->fh;
 486	char buf[64], *vals, *ends;
 487
 488	if (unit == NULL || fmt == NULL) {
 489		fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
 490		return;
 491	}
 492	snprintf(buf, sizeof(buf), fmt, val);
 493	ends = vals = skip_spaces(buf);
 494	while (isdigit(*ends) || *ends == '.')
 495		ends++;
 496	*ends = 0;
 497	fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
 498}
 499
 500static void print_metric_json(struct perf_stat_config *config __maybe_unused,
 501			     void *ctx,
 502			     enum metric_threshold_classify thresh,
 503			     const char *fmt __maybe_unused,
 504			     const char *unit, double val)
 505{
 506	struct outstate *os = ctx;
 507	FILE *out = os->fh;
 508
 509	if (unit) {
 510		json_out(os, "\"metric-value\" : \"%f\", \"metric-unit\" : \"%s\"", val, unit);
 511		if (thresh != METRIC_THRESHOLD_UNKNOWN) {
 512			json_out(os, "\"metric-threshold\" : \"%s\"",
 513				metric_threshold_classify__str(thresh));
 514		}
 515	}
 516	if (!config->metric_only)
 517		fprintf(out, "}");
 518}
 519
 520static void new_line_json(struct perf_stat_config *config, void *ctx)
 521{
 522	struct outstate *os = ctx;
 523
 524	fputs("\n{", os->fh);
 525	os->first = true;
 526	if (os->prefix)
 527		json_out(os, "%s", os->prefix);
 528
 529	aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
 530}
 531
 532static void print_metricgroup_header_json(struct perf_stat_config *config,
 533					  void *ctx,
 534					  const char *metricgroup_name)
 535{
 536	if (!metricgroup_name)
 537		return;
 538
 539	json_out((struct outstate *) ctx, "\"metricgroup\" : \"%s\"}", metricgroup_name);
 540	new_line_json(config, ctx);
 541}
 542
 543static void print_metricgroup_header_csv(struct perf_stat_config *config,
 544					 void *ctx,
 545					 const char *metricgroup_name)
 546{
 547	struct outstate *os = ctx;
 548	int i;
 549
 550	if (!metricgroup_name) {
 551		/* Leave space for running and enabling */
 552		for (i = 0; i < os->nfields - 2; i++)
 553			fputs(config->csv_sep, os->fh);
 554		return;
 555	}
 556
 557	for (i = 0; i < os->nfields; i++)
 558		fputs(config->csv_sep, os->fh);
 559	fprintf(config->output, "%s", metricgroup_name);
 560	new_line_csv(config, ctx);
 561}
 562
 563static void print_metricgroup_header_std(struct perf_stat_config *config,
 564					 void *ctx,
 565					 const char *metricgroup_name)
 566{
 567	struct outstate *os = ctx;
 568	int n;
 569
 570	if (!metricgroup_name) {
 571		__new_line_std(os);
 572		return;
 573	}
 574
 575	n = fprintf(config->output, " %*s", EVNAME_LEN, metricgroup_name);
 576
 577	fprintf(config->output, "%*s", MGROUP_LEN - n - 1, "");
 578}
 579
 580/* Filter out some columns that don't work well in metrics only mode */
 581
 582static bool valid_only_metric(const char *unit)
 583{
 584	if (!unit)
 585		return false;
 586	if (strstr(unit, "/sec") ||
 587	    strstr(unit, "CPUs utilized"))
 588		return false;
 589	return true;
 590}
 591
 592static const char *fixunit(char *buf, struct evsel *evsel,
 593			   const char *unit)
 594{
 595	if (!strncmp(unit, "of all", 6)) {
 596		snprintf(buf, 1024, "%s %s", evsel__name(evsel),
 597			 unit);
 598		return buf;
 599	}
 600	return unit;
 601}
 602
 603static void print_metric_only(struct perf_stat_config *config,
 604			      void *ctx, enum metric_threshold_classify thresh,
 605			      const char *fmt, const char *unit, double val)
 606{
 607	struct outstate *os = ctx;
 608	FILE *out = os->fh;
 609	char buf[1024], str[1024];
 610	unsigned mlen = config->metric_only_len;
 611	const char *color = metric_threshold_classify__color(thresh);
 612
 613	if (!valid_only_metric(unit))
 614		return;
 615	unit = fixunit(buf, os->evsel, unit);
 616	if (mlen < strlen(unit))
 617		mlen = strlen(unit) + 1;
 618
 619	if (color)
 620		mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
 621
 622	color_snprintf(str, sizeof(str), color ?: "", fmt ?: "", val);
 623	fprintf(out, "%*s ", mlen, str);
 624	os->first = false;
 625}
 626
 627static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
 628				  void *ctx,
 629				  enum metric_threshold_classify thresh __maybe_unused,
 630				  const char *fmt,
 631				  const char *unit, double val)
 632{
 633	struct outstate *os = ctx;
 634	FILE *out = os->fh;
 635	char buf[64], *vals, *ends;
 636	char tbuf[1024];
 637
 638	if (!valid_only_metric(unit))
 639		return;
 640	unit = fixunit(tbuf, os->evsel, unit);
 641	snprintf(buf, sizeof(buf), fmt ?: "", val);
 642	ends = vals = skip_spaces(buf);
 643	while (isdigit(*ends) || *ends == '.')
 644		ends++;
 645	*ends = 0;
 646	fprintf(out, "%s%s", vals, config->csv_sep);
 647	os->first = false;
 648}
 649
 650static void print_metric_only_json(struct perf_stat_config *config __maybe_unused,
 651				  void *ctx,
 652				  enum metric_threshold_classify thresh __maybe_unused,
 653				  const char *fmt,
 654				  const char *unit, double val)
 655{
 656	struct outstate *os = ctx;
 657	char buf[64], *ends;
 658	char tbuf[1024];
 659	const char *vals;
 660
 661	if (!valid_only_metric(unit))
 662		return;
 663	unit = fixunit(tbuf, os->evsel, unit);
 664	if (!unit[0])
 665		return;
 666	snprintf(buf, sizeof(buf), fmt ?: "", val);
 667	vals = ends = skip_spaces(buf);
 668	while (isdigit(*ends) || *ends == '.')
 669		ends++;
 670	*ends = 0;
 671	if (!vals[0])
 672		vals = "none";
 673	json_out(os, "\"%s\" : \"%s\"", unit, vals);
 674}
 675
 676static void new_line_metric(struct perf_stat_config *config __maybe_unused,
 677			    void *ctx __maybe_unused)
 678{
 679}
 680
 681static void print_metric_header(struct perf_stat_config *config,
 682				void *ctx,
 683				enum metric_threshold_classify thresh __maybe_unused,
 684				const char *fmt __maybe_unused,
 685				const char *unit, double val __maybe_unused)
 686{
 687	struct outstate *os = ctx;
 688	char tbuf[1024];
 689
 690	/* In case of iostat, print metric header for first root port only */
 691	if (config->iostat_run &&
 692	    os->evsel->priv != os->evsel->evlist->selected->priv)
 693		return;
 694
 695	if (os->evsel->cgrp != os->cgrp)
 696		return;
 697
 698	if (!valid_only_metric(unit))
 699		return;
 700	unit = fixunit(tbuf, os->evsel, unit);
 701
 702	if (config->json_output)
 703		return;
 704	else if (config->csv_output)
 705		fprintf(os->fh, "%s%s", unit, config->csv_sep);
 706	else
 707		fprintf(os->fh, "%*s ", config->metric_only_len, unit);
 708}
 709
 710static void print_counter_value_std(struct perf_stat_config *config,
 711				    struct evsel *evsel, double avg, bool ok)
 712{
 713	FILE *output = config->output;
 714	double sc =  evsel->scale;
 715	const char *fmt;
 716	const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
 717
 718	if (config->big_num)
 719		fmt = floor(sc) != sc ? "%'*.2f " : "%'*.0f ";
 720	else
 721		fmt = floor(sc) != sc ? "%*.2f " : "%*.0f ";
 722
 723	if (ok)
 724		fprintf(output, fmt, COUNTS_LEN, avg);
 725	else
 726		fprintf(output, "%*s ", COUNTS_LEN, bad_count);
 727
 728	if (evsel->unit)
 729		fprintf(output, "%-*s ", config->unit_width, evsel->unit);
 730
 731	fprintf(output, "%-*s", EVNAME_LEN, evsel__name(evsel));
 
 
 
 
 
 
 732}
 733
 734static void print_counter_value_csv(struct perf_stat_config *config,
 735				    struct evsel *evsel, double avg, bool ok)
 736{
 737	FILE *output = config->output;
 738	double sc =  evsel->scale;
 739	const char *sep = config->csv_sep;
 740	const char *fmt = floor(sc) != sc ? "%.2f%s" : "%.0f%s";
 741	const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
 742
 743	if (ok)
 744		fprintf(output, fmt, avg, sep);
 745	else
 746		fprintf(output, "%s%s", bad_count, sep);
 747
 748	if (evsel->unit)
 749		fprintf(output, "%s%s", evsel->unit, sep);
 750
 751	fprintf(output, "%s", evsel__name(evsel));
 752}
 
 
 
 
 
 
 753
 754static void print_counter_value_json(struct outstate *os,
 755				     struct evsel *evsel, double avg, bool ok)
 756{
 757	const char *bad_count = evsel->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED;
 758
 759	if (ok)
 760		json_out(os, "\"counter-value\" : \"%f\"", avg);
 761	else
 762		json_out(os, "\"counter-value\" : \"%s\"", bad_count);
 763
 764	if (evsel->unit)
 765		json_out(os, "\"unit\" : \"%s\"", evsel->unit);
 766
 767	json_out(os, "\"event\" : \"%s\"", evsel__name(evsel));
 768}
 769
 770static void print_counter_value(struct perf_stat_config *config, struct outstate *os,
 771				struct evsel *evsel, double avg, bool ok)
 772{
 773	if (config->json_output)
 774		print_counter_value_json(os, evsel, avg, ok);
 775	else if (config->csv_output)
 776		print_counter_value_csv(config, evsel, avg, ok);
 777	else
 778		print_counter_value_std(config, evsel, avg, ok);
 779}
 780
 781static void abs_printout(struct perf_stat_config *config,
 782			 struct outstate *os,
 783			 struct aggr_cpu_id id, int aggr_nr,
 784			 struct evsel *evsel, double avg, bool ok)
 785{
 786	aggr_printout(config, os, evsel, id, aggr_nr);
 787	print_counter_value(config, os, evsel, avg, ok);
 788	print_cgroup(config, os, evsel->cgrp);
 789}
 790
 791static bool is_mixed_hw_group(struct evsel *counter)
 792{
 793	struct evlist *evlist = counter->evlist;
 794	u32 pmu_type = counter->core.attr.type;
 795	struct evsel *pos;
 796
 797	if (counter->core.nr_members < 2)
 798		return false;
 799
 800	evlist__for_each_entry(evlist, pos) {
 801		/* software events can be part of any hardware group */
 802		if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
 803			continue;
 804		if (pmu_type == PERF_TYPE_SOFTWARE) {
 805			pmu_type = pos->core.attr.type;
 806			continue;
 807		}
 808		if (pmu_type != pos->core.attr.type)
 809			return true;
 810	}
 811
 812	return false;
 813}
 814
 815static bool evlist__has_hybrid(struct evlist *evlist)
 816{
 817	struct evsel *evsel;
 818
 819	if (perf_pmus__num_core_pmus() == 1)
 820		return false;
 821
 822	evlist__for_each_entry(evlist, evsel) {
 823		if (evsel->core.is_pmu_core)
 824			return true;
 825	}
 826
 827	return false;
 828}
 829
 830static void printout(struct perf_stat_config *config, struct outstate *os,
 831		     double uval, u64 run, u64 ena, double noise, int aggr_idx)
 832{
 833	struct perf_stat_output_ctx out;
 834	print_metric_t pm;
 
 
 
 
 
 
 
 835	new_line_t nl;
 836	print_metricgroup_header_t pmh;
 837	bool ok = true;
 838	struct evsel *counter = os->evsel;
 839
 840	if (config->csv_output) {
 841		pm = config->metric_only ? print_metric_only_csv : print_metric_csv;
 842		nl = config->metric_only ? new_line_metric : new_line_csv;
 843		pmh = print_metricgroup_header_csv;
 844		os->nfields = 4 + (counter->cgrp ? 1 : 0);
 845	} else if (config->json_output) {
 846		pm = config->metric_only ? print_metric_only_json : print_metric_json;
 847		nl = config->metric_only ? new_line_metric : new_line_json;
 848		pmh = print_metricgroup_header_json;
 849	} else {
 850		pm = config->metric_only ? print_metric_only : print_metric_std;
 851		nl = config->metric_only ? new_line_metric : new_line_std;
 852		pmh = print_metricgroup_header_std;
 
 
 
 
 
 
 
 
 
 
 
 
 853	}
 854
 855	if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
 856		if (config->metric_only) {
 857			pm(config, os, METRIC_THRESHOLD_UNKNOWN, "", "", 0);
 858			return;
 859		}
 
 860
 861		ok = false;
 
 
 
 862
 863		if (counter->supported) {
 864			if (!evlist__has_hybrid(counter->evlist)) {
 865				config->print_free_counters_hint = 1;
 866				if (is_mixed_hw_group(counter))
 867					config->print_mixed_hw_group_error = 1;
 868			}
 869		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 870	}
 871
 
 
 
 872	out.print_metric = pm;
 873	out.new_line = nl;
 874	out.print_metricgroup_header = pmh;
 875	out.ctx = os;
 876	out.force_header = false;
 877
 878	if (!config->metric_only && !counter->default_metricgroup) {
 879		abs_printout(config, os, os->id, os->aggr_nr, counter, uval, ok);
 880
 881		print_noise(config, os, counter, noise, /*before_metric=*/true);
 882		print_running(config, os, run, ena, /*before_metric=*/true);
 883	}
 884
 885	if (ok) {
 886		if (!config->metric_only && counter->default_metricgroup) {
 887			void *from = NULL;
 888
 889			aggr_printout(config, os, os->evsel, os->id, os->aggr_nr);
 890			/* Print out all the metricgroup with the same metric event. */
 891			do {
 892				int num = 0;
 893
 894				/* Print out the new line for the next new metricgroup. */
 895				if (from) {
 896					if (config->json_output)
 897						new_line_json(config, (void *)os);
 898					else
 899						__new_line_std_csv(config, os);
 900				}
 901
 902				print_noise(config, os, counter, noise, /*before_metric=*/true);
 903				print_running(config, os, run, ena, /*before_metric=*/true);
 904				from = perf_stat__print_shadow_stats_metricgroup(config, counter, aggr_idx,
 905										 &num, from, &out,
 906										 &config->metric_events);
 907			} while (from != NULL);
 908		} else
 909			perf_stat__print_shadow_stats(config, counter, uval, aggr_idx,
 910						      &out, &config->metric_events);
 911	} else {
 912		pm(config, os, METRIC_THRESHOLD_UNKNOWN, /*format=*/NULL, /*unit=*/"", /*val=*/0);
 913	}
 
 
 
 
 
 
 
 
 914
 915	if (!config->metric_only) {
 916		print_noise(config, os, counter, noise, /*before_metric=*/false);
 917		print_running(config, os, run, ena, /*before_metric=*/false);
 
 
 
 
 
 
 
 
 
 
 
 918	}
 919}
 920
 921static void uniquify_event_name(struct evsel *counter)
 922{
 923	const char *name, *pmu_name;
 924	char *new_name, *config;
 925	int ret;
 926
 927	/* The evsel was already uniquified. */
 928	if (counter->uniquified_name)
 929		return;
 930
 931	/* Avoid checking to uniquify twice. */
 932	counter->uniquified_name = true;
 933
 934	/* The evsel has a "name=" config term or is from libpfm. */
 935	if (counter->use_config_name || counter->is_libpfm_event)
 936		return;
 937
 938	/* Legacy no PMU event, don't uniquify. */
 939	if  (!counter->pmu ||
 940	     (counter->pmu->type < PERF_TYPE_MAX && counter->pmu->type != PERF_TYPE_RAW))
 941		return;
 942
 943	/* A sysfs or json event replacing a legacy event, don't uniquify. */
 944	if (counter->pmu->is_core && counter->alternate_hw_config != PERF_COUNT_HW_MAX)
 945		return;
 946
 947	name = evsel__name(counter);
 948	pmu_name = counter->pmu->name;
 949	/* Already prefixed by the PMU name. */
 950	if (!strncmp(name, pmu_name, strlen(pmu_name)))
 951		return;
 952
 953	config = strchr(name, '/');
 954	if (config) {
 955		int len = config - name;
 956
 957		if (config[1] == '/') {
 958			/* case: event// */
 959			ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 2);
 960		} else {
 961			/* case: event/.../ */
 962			ret = asprintf(&new_name, "%s/%.*s,%s", pmu_name, len, name, config + 1);
 963		}
 964	} else {
 965		config = strchr(name, ':');
 966		if (config) {
 967			/* case: event:.. */
 968			int len = config - name;
 969
 970			ret = asprintf(&new_name, "%s/%.*s/%s", pmu_name, len, name, config + 1);
 971		} else {
 972			/* case: event */
 973			ret = asprintf(&new_name, "%s/%s/", pmu_name, name);
 974		}
 975	}
 976	if (ret > 0) {
 977		free(counter->name);
 978		counter->name = new_name;
 979	} else {
 980		/* ENOMEM from asprintf. */
 981		counter->uniquified_name = false;
 982	}
 983}
 984
 985static bool hybrid_uniquify(struct evsel *evsel, struct perf_stat_config *config)
 
 
 
 986{
 987	return evsel__is_hybrid(evsel) && !config->hybrid_merge;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 988}
 989
 990static void uniquify_counter(struct perf_stat_config *config, struct evsel *counter)
 
 
 
 991{
 992	if (config->aggr_mode == AGGR_NONE || hybrid_uniquify(counter, config))
 
 
 
 993		uniquify_event_name(counter);
 
 
 
 994}
 995
 996/**
 997 * should_skip_zero_count() - Check if the event should print 0 values.
 998 * @config: The perf stat configuration (including aggregation mode).
 999 * @counter: The evsel with its associated cpumap.
1000 * @id: The aggregation id that is being queried.
1001 *
1002 * Due to mismatch between the event cpumap or thread-map and the
1003 * aggregation mode, sometimes it'd iterate the counter with the map
1004 * which does not contain any values.
1005 *
1006 * For example, uncore events have dedicated CPUs to manage them,
1007 * result for other CPUs should be zero and skipped.
1008 *
1009 * Return: %true if the value should NOT be printed, %false if the value
1010 * needs to be printed like "<not counted>" or "<not supported>".
1011 */
1012static bool should_skip_zero_counter(struct perf_stat_config *config,
1013				     struct evsel *counter,
1014				     const struct aggr_cpu_id *id)
1015{
1016	struct perf_cpu cpu;
1017	int idx;
1018
1019	/*
1020	 * Skip unsupported default events when not verbose. (default events
1021	 * are all marked 'skippable').
1022	 */
1023	if (verbose == 0 && counter->skippable && !counter->supported)
1024		return true;
1025
1026	/*
1027	 * Skip value 0 when enabling --per-thread globally,
1028	 * otherwise it will have too many 0 output.
1029	 */
1030	if (config->aggr_mode == AGGR_THREAD && config->system_wide)
1031		return true;
1032
1033	/*
1034	 * Many tool events are only gathered on the first index, skip other
1035	 * zero values.
1036	 */
1037	if (evsel__is_tool(counter)) {
1038		struct aggr_cpu_id own_id =
1039			config->aggr_get_id(config, (struct perf_cpu){ .cpu = 0 });
1040
1041		return !aggr_cpu_id__equal(id, &own_id);
1042	}
1043
1044	/*
1045	 * Skip value 0 when it's an uncore event and the given aggr id
1046	 * does not belong to the PMU cpumask.
1047	 */
1048	if (!counter->pmu || !counter->pmu->is_uncore)
1049		return false;
1050
1051	perf_cpu_map__for_each_cpu(cpu, idx, counter->pmu->cpus) {
1052		struct aggr_cpu_id own_id = config->aggr_get_id(config, cpu);
1053
1054		if (aggr_cpu_id__equal(id, &own_id))
1055			return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1056	}
1057	return true;
1058}
1059
1060static void print_counter_aggrdata(struct perf_stat_config *config,
1061				   struct evsel *counter, int aggr_idx,
1062				   struct outstate *os)
 
1063{
 
1064	FILE *output = config->output;
1065	u64 ena, run, val;
 
1066	double uval;
1067	struct perf_stat_evsel *ps = counter->stats;
1068	struct perf_stat_aggr *aggr = &ps->aggr[aggr_idx];
1069	struct aggr_cpu_id id = config->aggr_map->map[aggr_idx];
1070	double avg = aggr->counts.val;
1071	bool metric_only = config->metric_only;
1072
1073	os->id = id;
1074	os->aggr_nr = aggr->nr;
1075	os->evsel = counter;
1076
1077	/* Skip already merged uncore/hybrid events */
1078	if (counter->merged_stat)
1079		return;
 
 
 
 
 
 
 
 
 
 
 
 
 
1080
1081	uniquify_counter(config, counter);
 
 
 
 
 
1082
1083	val = aggr->counts.val;
1084	ena = aggr->counts.ena;
1085	run = aggr->counts.run;
 
 
 
 
 
 
1086
1087	if (perf_stat__skip_metric_event(counter, &config->metric_events, ena, run))
1088		return;
1089
1090	if (val == 0 && should_skip_zero_counter(config, counter, &id))
1091		return;
 
 
 
 
 
 
 
1092
1093	if (!metric_only) {
1094		if (config->json_output) {
1095			os->first = true;
1096			fputc('{', output);
 
1097		}
1098		if (os->prefix) {
1099			if (config->json_output)
1100				json_out(os, "%s", os->prefix);
1101			else
1102				fprintf(output, "%s", os->prefix);
1103		} else if (config->summary && config->csv_output &&
1104			   !config->no_csv_summary && !config->interval)
1105			fprintf(output, "%s%s", "summary", config->csv_sep);
1106	}
 
1107
1108	uval = val * counter->scale;
1109
1110	printout(config, os, uval, run, ena, avg, aggr_idx);
1111
1112	if (!metric_only)
1113		fputc('\n', output);
1114}
1115
1116static void print_metric_begin(struct perf_stat_config *config,
1117			       struct evlist *evlist,
1118			       struct outstate *os, int aggr_idx)
 
 
1119{
1120	struct perf_stat_aggr *aggr;
1121	struct aggr_cpu_id id;
1122	struct evsel *evsel;
1123
1124	os->first = true;
1125	if (!config->metric_only)
1126		return;
 
 
 
 
 
 
 
 
 
1127
1128	if (config->json_output)
1129		fputc('{', config->output);
1130
1131	if (os->prefix) {
1132		if (config->json_output)
1133			json_out(os, "%s", os->prefix);
1134		else
1135			fprintf(config->output, "%s", os->prefix);
 
 
 
 
 
 
 
 
 
1136	}
1137	evsel = evlist__first(evlist);
1138	id = config->aggr_map->map[aggr_idx];
1139	aggr = &evsel->stats->aggr[aggr_idx];
1140	aggr_printout(config, os, evsel, id, aggr->nr);
1141
1142	print_cgroup(config, os, os->cgrp ? : evsel->cgrp);
 
 
 
 
 
1143}
1144
1145static void print_metric_end(struct perf_stat_config *config, struct outstate *os)
 
 
1146{
1147	FILE *output = config->output;
1148
1149	if (!config->metric_only)
 
 
 
 
 
 
1150		return;
 
1151
1152	if (config->json_output) {
1153		if (os->first)
1154			fputs("\"metric-value\" : \"none\"", output);
1155		fputc('}', output);
 
 
 
 
 
 
 
 
 
 
1156	}
1157	fputc('\n', output);
1158}
1159
1160static void print_aggr(struct perf_stat_config *config,
1161		       struct evlist *evlist,
1162		       struct outstate *os)
1163{
1164	struct evsel *counter;
1165	int aggr_idx;
1166
1167	if (!config->aggr_map || !config->aggr_get_id)
1168		return;
 
1169
1170	/*
1171	 * With metric_only everything is on a single line.
1172	 * Without each counter has its own line.
1173	 */
1174	cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1175		print_metric_begin(config, evlist, os, aggr_idx);
1176
1177		evlist__for_each_entry(evlist, counter) {
1178			print_counter_aggrdata(config, counter, aggr_idx, os);
1179		}
1180		print_metric_end(config, os);
1181	}
1182}
1183
1184static void print_aggr_cgroup(struct perf_stat_config *config,
1185			      struct evlist *evlist,
1186			      struct outstate *os)
 
 
 
1187{
1188	struct evsel *counter, *evsel;
1189	int aggr_idx;
 
 
1190
1191	if (!config->aggr_map || !config->aggr_get_id)
1192		return;
1193
1194	evlist__for_each_entry(evlist, evsel) {
1195		if (os->cgrp == evsel->cgrp)
1196			continue;
1197
1198		os->cgrp = evsel->cgrp;
1199
1200		cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1201			print_metric_begin(config, evlist, os, aggr_idx);
 
 
 
 
1202
1203			evlist__for_each_entry(evlist, counter) {
1204				if (counter->cgrp != os->cgrp)
1205					continue;
 
 
1206
1207				print_counter_aggrdata(config, counter, aggr_idx, os);
1208			}
1209			print_metric_end(config, os);
1210		}
1211	}
1212}
1213
 
 
 
 
1214static void print_counter(struct perf_stat_config *config,
1215			  struct evsel *counter, struct outstate *os)
1216{
1217	int aggr_idx;
 
 
 
1218
1219	/* AGGR_THREAD doesn't have config->aggr_get_id */
1220	if (!config->aggr_map)
1221		return;
 
 
 
 
 
 
 
 
 
 
 
 
1222
1223	cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1224		print_counter_aggrdata(config, counter, aggr_idx, os);
1225	}
1226}
1227
1228static void print_no_aggr_metric(struct perf_stat_config *config,
1229				 struct evlist *evlist,
1230				 struct outstate *os)
1231{
1232	int all_idx;
1233	struct perf_cpu cpu;
 
 
 
1234
1235	perf_cpu_map__for_each_cpu(cpu, all_idx, evlist->core.user_requested_cpus) {
1236		struct evsel *counter;
1237		bool first = true;
1238
 
 
1239		evlist__for_each_entry(evlist, counter) {
1240			u64 ena, run, val;
1241			double uval;
1242			struct perf_stat_evsel *ps = counter->stats;
1243			int aggr_idx = 0;
1244
1245			if (!perf_cpu_map__has(evsel__cpus(counter), cpu))
1246				continue;
1247
1248			cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1249				if (config->aggr_map->map[aggr_idx].cpu.cpu == cpu.cpu)
1250					break;
1251			}
1252
1253			os->evsel = counter;
1254			os->id = aggr_cpu_id__cpu(cpu, /*data=*/NULL);
1255			if (first) {
1256				print_metric_begin(config, evlist, os, aggr_idx);
1257				first = false;
1258			}
1259			val = ps->aggr[aggr_idx].counts.val;
1260			ena = ps->aggr[aggr_idx].counts.ena;
1261			run = ps->aggr[aggr_idx].counts.run;
1262
1263			uval = val * counter->scale;
1264			printout(config, os, uval, run, ena, 1.0, aggr_idx);
 
1265		}
1266		if (!first)
1267			print_metric_end(config, os);
1268	}
1269}
1270
1271static void print_metric_headers_std(struct perf_stat_config *config,
1272				     bool no_indent)
1273{
1274	fputc(' ', config->output);
1275
1276	if (!no_indent) {
1277		int len = aggr_header_lens[config->aggr_mode];
1278
1279		if (nr_cgroups || config->cgroup_list)
1280			len += CGROUP_LEN + 1;
1281
1282		fprintf(config->output, "%*s", len, "");
1283	}
1284}
1285
1286static void print_metric_headers_csv(struct perf_stat_config *config,
1287				     bool no_indent __maybe_unused)
1288{
1289	const char *p;
1290
1291	if (config->interval)
1292		fprintf(config->output, "time%s", config->csv_sep);
1293	if (config->iostat_run)
1294		return;
1295
1296	p = aggr_header_csv[config->aggr_mode];
1297	while (*p) {
1298		if (*p == ',')
1299			fputs(config->csv_sep, config->output);
1300		else
1301			fputc(*p, config->output);
1302		p++;
1303	}
1304}
1305
1306static void print_metric_headers_json(struct perf_stat_config *config __maybe_unused,
1307				      bool no_indent __maybe_unused)
1308{
1309}
 
 
 
 
1310
1311static void print_metric_headers(struct perf_stat_config *config,
1312				 struct evlist *evlist, bool no_indent)
 
1313{
 
1314	struct evsel *counter;
1315	struct outstate os = {
1316		.fh = config->output
1317	};
1318	struct perf_stat_output_ctx out = {
1319		.ctx = &os,
1320		.print_metric = print_metric_header,
1321		.new_line = new_line_metric,
1322		.force_header = true,
1323	};
1324
1325	if (config->json_output)
1326		print_metric_headers_json(config, no_indent);
1327	else if (config->csv_output)
1328		print_metric_headers_csv(config, no_indent);
1329	else
1330		print_metric_headers_std(config, no_indent);
1331
1332	if (config->iostat_run)
1333		iostat_print_header_prefix(config);
1334
1335	if (config->cgroup_list)
1336		os.cgrp = evlist__first(evlist)->cgrp;
 
 
 
 
 
 
1337
1338	/* Print metrics headers only */
1339	evlist__for_each_entry(evlist, counter) {
1340		if (!config->iostat_run &&
1341		    config->aggr_mode != AGGR_NONE && counter->metric_leader != counter)
1342			continue;
1343
1344		os.evsel = counter;
1345
 
 
 
 
1346		perf_stat__print_shadow_stats(config, counter, 0,
1347					      0,
1348					      &out,
1349					      &config->metric_events);
 
1350	}
1351
1352	if (!config->json_output)
1353		fputc('\n', config->output);
1354}
1355
1356static void prepare_interval(struct perf_stat_config *config,
1357			     char *prefix, size_t len, struct timespec *ts)
1358{
1359	if (config->iostat_run)
1360		return;
1361
1362	if (config->json_output)
1363		scnprintf(prefix, len, "\"interval\" : %lu.%09lu",
1364			  (unsigned long) ts->tv_sec, ts->tv_nsec);
1365	else if (config->csv_output)
1366		scnprintf(prefix, len, "%lu.%09lu%s",
1367			  (unsigned long) ts->tv_sec, ts->tv_nsec, config->csv_sep);
1368	else
1369		scnprintf(prefix, len, "%6lu.%09lu ",
1370			  (unsigned long) ts->tv_sec, ts->tv_nsec);
1371}
1372
1373static void print_header_interval_std(struct perf_stat_config *config,
1374				      struct target *_target __maybe_unused,
1375				      struct evlist *evlist,
1376				      int argc __maybe_unused,
1377				      const char **argv __maybe_unused)
1378{
 
 
1379	FILE *output = config->output;
 
1380
1381	switch (config->aggr_mode) {
1382	case AGGR_NODE:
1383	case AGGR_SOCKET:
1384	case AGGR_DIE:
1385	case AGGR_CLUSTER:
1386	case AGGR_CACHE:
1387	case AGGR_CORE:
1388		fprintf(output, "#%*s %-*s cpus",
1389			INTERVAL_LEN - 1, "time",
1390			aggr_header_lens[config->aggr_mode],
1391			aggr_header_std[config->aggr_mode]);
1392		break;
1393	case AGGR_NONE:
1394		fprintf(output, "#%*s %-*s",
1395			INTERVAL_LEN - 1, "time",
1396			aggr_header_lens[config->aggr_mode],
1397			aggr_header_std[config->aggr_mode]);
1398		break;
1399	case AGGR_THREAD:
1400		fprintf(output, "#%*s %*s-%-*s",
1401			INTERVAL_LEN - 1, "time",
1402			COMM_LEN, "comm", PID_LEN, "pid");
1403		break;
1404	case AGGR_GLOBAL:
1405	default:
1406		if (!config->iostat_run)
1407			fprintf(output, "#%*s",
1408				INTERVAL_LEN - 1, "time");
1409	case AGGR_UNSET:
1410	case AGGR_MAX:
1411		break;
1412	}
1413
1414	if (config->metric_only)
1415		print_metric_headers(config, evlist, true);
1416	else
1417		fprintf(output, " %*s %*s events\n",
1418			COUNTS_LEN, "counts", config->unit_width, "unit");
1419}
1420
1421static void print_header_std(struct perf_stat_config *config,
1422			     struct target *_target, struct evlist *evlist,
1423			     int argc, const char **argv)
1424{
1425	FILE *output = config->output;
1426	int i;
1427
1428	fprintf(output, "\n");
1429	fprintf(output, " Performance counter stats for ");
1430	if (_target->bpf_str)
1431		fprintf(output, "\'BPF program(s) %s", _target->bpf_str);
1432	else if (_target->system_wide)
1433		fprintf(output, "\'system wide");
1434	else if (_target->cpu_list)
1435		fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1436	else if (!target__has_task(_target)) {
1437		fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1438		for (i = 1; argv && (i < argc); i++)
1439			fprintf(output, " %s", argv[i]);
1440	} else if (_target->pid)
1441		fprintf(output, "process id \'%s", _target->pid);
1442	else
1443		fprintf(output, "thread id \'%s", _target->tid);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1444
1445	fprintf(output, "\'");
1446	if (config->run_count > 1)
1447		fprintf(output, " (%d runs)", config->run_count);
1448	fprintf(output, ":\n\n");
1449
1450	if (config->metric_only)
1451		print_metric_headers(config, evlist, false);
1452}
1453
1454static void print_header_csv(struct perf_stat_config *config,
1455			     struct target *_target __maybe_unused,
1456			     struct evlist *evlist,
1457			     int argc __maybe_unused,
1458			     const char **argv __maybe_unused)
1459{
1460	if (config->metric_only)
1461		print_metric_headers(config, evlist, true);
1462}
1463static void print_header_json(struct perf_stat_config *config,
1464			      struct target *_target __maybe_unused,
1465			      struct evlist *evlist,
1466			      int argc __maybe_unused,
1467			      const char **argv __maybe_unused)
1468{
1469	if (config->metric_only)
1470		print_metric_headers(config, evlist, true);
1471}
1472
1473static void print_header(struct perf_stat_config *config,
1474			 struct target *_target,
1475			 struct evlist *evlist,
1476			 int argc, const char **argv)
1477{
1478	static int num_print_iv;
 
1479
1480	fflush(stdout);
1481
1482	if (config->interval_clear)
1483		puts(CONSOLE_CLEAR);
1484
1485	if (num_print_iv == 0 || config->interval_clear) {
1486		if (config->json_output)
1487			print_header_json(config, _target, evlist, argc, argv);
1488		else if (config->csv_output)
1489			print_header_csv(config, _target, evlist, argc, argv);
1490		else if (config->interval)
1491			print_header_interval_std(config, _target, evlist, argc, argv);
 
 
 
1492		else
1493			print_header_std(config, _target, evlist, argc, argv);
1494	}
1495
1496	if (num_print_iv++ == 25)
1497		num_print_iv = 0;
 
 
 
1498}
1499
1500static int get_precision(double num)
1501{
1502	if (num > 1)
1503		return 0;
1504
1505	return lround(ceil(-log10(num)));
1506}
1507
1508static void print_table(struct perf_stat_config *config,
1509			FILE *output, int precision, double avg)
1510{
1511	char tmp[64];
1512	int idx, indent = 0;
1513
1514	scnprintf(tmp, 64, " %17.*f", precision, avg);
1515	while (tmp[indent] == ' ')
1516		indent++;
1517
1518	fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1519
1520	for (idx = 0; idx < config->run_count; idx++) {
1521		double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1522		int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1523
1524		fprintf(output, " %17.*f (%+.*f) ",
1525			precision, run, precision, run - avg);
1526
1527		for (h = 0; h < n; h++)
1528			fprintf(output, "#");
1529
1530		fprintf(output, "\n");
1531	}
1532
1533	fprintf(output, "\n%*s# Final result:\n", indent, "");
1534}
1535
1536static double timeval2double(struct timeval *t)
1537{
1538	return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1539}
1540
1541static void print_footer(struct perf_stat_config *config)
1542{
1543	double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1544	FILE *output = config->output;
1545
1546	if (config->interval || config->csv_output || config->json_output)
1547		return;
1548
1549	if (!config->null_run)
1550		fprintf(output, "\n");
1551
1552	if (config->run_count == 1) {
1553		fprintf(output, " %17.9f seconds time elapsed", avg);
1554
1555		if (config->ru_display) {
1556			double ru_utime = timeval2double(&config->ru_data.ru_utime);
1557			double ru_stime = timeval2double(&config->ru_data.ru_stime);
1558
1559			fprintf(output, "\n\n");
1560			fprintf(output, " %17.9f seconds user\n", ru_utime);
1561			fprintf(output, " %17.9f seconds sys\n", ru_stime);
1562		}
1563	} else {
1564		double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1565		/*
1566		 * Display at most 2 more significant
1567		 * digits than the stddev inaccuracy.
1568		 */
1569		int precision = get_precision(sd) + 2;
1570
1571		if (config->walltime_run_table)
1572			print_table(config, output, precision, avg);
1573
1574		fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1575			precision, avg, precision, sd);
1576
1577		print_noise_pct(config, NULL, sd, avg, /*before_metric=*/false);
1578	}
1579	fprintf(output, "\n\n");
1580
1581	if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1582		fprintf(output,
1583"Some events weren't counted. Try disabling the NMI watchdog:\n"
1584"	echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1585"	perf stat ...\n"
1586"	echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1587
1588	if (config->print_mixed_hw_group_error)
1589		fprintf(output,
1590			"The events in group usually have to be from "
1591			"the same PMU. Try reorganizing the group.\n");
1592}
1593
1594static void print_percore(struct perf_stat_config *config,
1595			  struct evsel *counter, struct outstate *os)
1596{
1597	bool metric_only = config->metric_only;
1598	FILE *output = config->output;
1599	struct cpu_aggr_map *core_map;
1600	int aggr_idx, core_map_len = 0;
1601
1602	if (!config->aggr_map || !config->aggr_get_id)
1603		return;
1604
1605	if (config->percore_show_thread)
1606		return print_counter(config, counter, os);
1607
1608	/*
1609	 * core_map will hold the aggr_cpu_id for the cores that have been
1610	 * printed so that each core is printed just once.
1611	 */
1612	core_map = cpu_aggr_map__empty_new(config->aggr_map->nr);
1613	if (core_map == NULL) {
1614		fprintf(output, "Cannot allocate per-core aggr map for display\n");
1615		return;
1616	}
1617
1618	cpu_aggr_map__for_each_idx(aggr_idx, config->aggr_map) {
1619		struct perf_cpu curr_cpu = config->aggr_map->map[aggr_idx].cpu;
1620		struct aggr_cpu_id core_id = aggr_cpu_id__core(curr_cpu, NULL);
1621		bool found = false;
1622
1623		for (int i = 0; i < core_map_len; i++) {
1624			if (aggr_cpu_id__equal(&core_map->map[i], &core_id)) {
1625				found = true;
1626				break;
1627			}
1628		}
1629		if (found)
1630			continue;
1631
1632		print_counter_aggrdata(config, counter, aggr_idx, os);
1633
1634		core_map->map[core_map_len++] = core_id;
1635	}
1636	free(core_map);
1637
1638	if (metric_only)
1639		fputc('\n', output);
1640}
1641
1642static void print_cgroup_counter(struct perf_stat_config *config, struct evlist *evlist,
1643				 struct outstate *os)
1644{
1645	struct evsel *counter;
 
 
 
1646
1647	evlist__for_each_entry(evlist, counter) {
1648		if (os->cgrp != counter->cgrp) {
1649			if (os->cgrp != NULL)
1650				print_metric_end(config, os);
1651
1652			os->cgrp = counter->cgrp;
1653			print_metric_begin(config, evlist, os, /*aggr_idx=*/0);
1654		}
1655
1656		print_counter(config, counter, os);
 
 
 
 
 
 
1657	}
1658	if (os->cgrp)
1659		print_metric_end(config, os);
1660}
1661
1662static void disable_uniquify(struct evlist *evlist)
1663{
1664	struct evsel *counter;
1665	struct perf_pmu *last_pmu = NULL;
1666	bool first = true;
1667
1668	evlist__for_each_entry(evlist, counter) {
1669		/* If PMUs vary then uniquify can be useful. */
1670		if (!first && counter->pmu != last_pmu)
1671			return;
1672		first = false;
1673		if (counter->pmu) {
1674			/* Allow uniquify for uncore PMUs. */
1675			if (!counter->pmu->is_core)
1676				return;
1677			/* Keep hybrid event names uniquified for clarity. */
1678			if (perf_pmus__num_core_pmus() > 1)
1679				return;
1680		}
1681	}
1682	evlist__for_each_entry_continue(evlist, counter) {
1683		counter->uniquified_name = true;
1684	}
1685}
1686
1687void evlist__print_counters(struct evlist *evlist, struct perf_stat_config *config,
1688			    struct target *_target, struct timespec *ts,
 
 
 
1689			    int argc, const char **argv)
1690{
1691	bool metric_only = config->metric_only;
1692	int interval = config->interval;
1693	struct evsel *counter;
1694	char buf[64];
1695	struct outstate os = {
1696		.fh = config->output,
1697		.first = true,
1698	};
1699
1700	disable_uniquify(evlist);
 
 
 
1701
1702	if (config->iostat_run)
1703		evlist->selected = evlist__first(evlist);
1704
1705	if (interval) {
1706		os.prefix = buf;
1707		prepare_interval(config, buf, sizeof(buf), ts);
 
 
 
1708	}
1709
1710	print_header(config, _target, evlist, argc, argv);
1711
1712	switch (config->aggr_mode) {
1713	case AGGR_CORE:
1714	case AGGR_CACHE:
1715	case AGGR_CLUSTER:
1716	case AGGR_DIE:
1717	case AGGR_SOCKET:
1718	case AGGR_NODE:
1719		if (config->cgroup_list)
1720			print_aggr_cgroup(config, evlist, &os);
1721		else
1722			print_aggr(config, evlist, &os);
1723		break;
1724	case AGGR_THREAD:
 
 
 
 
1725	case AGGR_GLOBAL:
1726		if (config->iostat_run) {
1727			iostat_print_counters(evlist, config, ts, buf,
1728					      (iostat_print_counter_t)print_counter, &os);
1729		} else if (config->cgroup_list) {
1730			print_cgroup_counter(config, evlist, &os);
1731		} else {
1732			print_metric_begin(config, evlist, &os, /*aggr_idx=*/0);
1733			evlist__for_each_entry(evlist, counter) {
1734				print_counter(config, counter, &os);
1735			}
1736			print_metric_end(config, &os);
1737		}
 
 
1738		break;
1739	case AGGR_NONE:
1740		if (metric_only)
1741			print_no_aggr_metric(config, evlist, &os);
1742		else {
1743			evlist__for_each_entry(evlist, counter) {
1744				if (counter->percore)
1745					print_percore(config, counter, &os);
1746				else
1747					print_counter(config, counter, &os);
1748			}
1749		}
1750		break;
1751	case AGGR_MAX:
1752	case AGGR_UNSET:
1753	default:
1754		break;
1755	}
1756
1757	print_footer(config);
 
1758
1759	fflush(config->output);
1760}
v5.9
   1#include <stdlib.h>
   2#include <stdio.h>
   3#include <inttypes.h>
   4#include <linux/string.h>
   5#include <linux/time64.h>
   6#include <math.h>
 
   7#include "color.h"
   8#include "counts.h"
 
   9#include "evlist.h"
  10#include "evsel.h"
  11#include "stat.h"
  12#include "top.h"
  13#include "thread_map.h"
  14#include "cpumap.h"
  15#include "string2.h"
  16#include <linux/ctype.h>
  17#include "cgroup.h"
  18#include <api/fs/fs.h>
  19#include "util.h"
 
 
 
 
  20
  21#define CNTR_NOT_SUPPORTED	"<not supported>"
  22#define CNTR_NOT_COUNTED	"<not counted>"
  23
  24static void print_running(struct perf_stat_config *config,
  25			  u64 run, u64 ena)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  26{
  27	if (config->csv_output) {
  28		fprintf(config->output, "%s%" PRIu64 "%s%.2f",
  29					config->csv_sep,
  30					run,
  31					config->csv_sep,
  32					ena ? 100.0 * run / ena : 100.0);
  33	} else if (run != ena) {
 
 
 
 
 
 
 
  34		fprintf(config->output, "  (%.2f%%)", 100.0 * run / ena);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  35	}
  36}
  37
  38static void print_noise_pct(struct perf_stat_config *config,
  39			    double total, double avg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  40{
  41	double pct = rel_stddev_stats(total, avg);
  42
  43	if (config->csv_output)
  44		fprintf(config->output, "%s%.2f%%", config->csv_sep, pct);
  45	else if (pct)
  46		fprintf(config->output, "  ( +-%6.2f%% )", pct);
 
 
 
 
 
 
  47}
  48
  49static void print_noise(struct perf_stat_config *config,
  50			struct evsel *evsel, double avg)
  51{
  52	struct perf_stat_evsel *ps;
  53
  54	if (config->run_count == 1)
  55		return;
  56
  57	ps = evsel->stats;
  58	print_noise_pct(config, stddev_stats(&ps->res_stats[0]), avg);
  59}
  60
  61static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
  62{
  63	if (nr_cgroups) {
  64		const char *cgrp_name = evsel->cgrp ? evsel->cgrp->name  : "";
  65		fprintf(config->output, "%s%s", config->csv_sep, cgrp_name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  66	}
  67}
  68
 
 
 
 
 
 
  69
  70static void aggr_printout(struct perf_stat_config *config,
  71			  struct evsel *evsel, int id, int nr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  72{
 
 
 
  73	switch (config->aggr_mode) {
  74	case AGGR_CORE:
  75		fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
  76			cpu_map__id_to_socket(id),
  77			cpu_map__id_to_die(id),
  78			config->csv_output ? 0 : -8,
  79			cpu_map__id_to_cpu(id),
  80			config->csv_sep,
  81			config->csv_output ? 0 : 4,
  82			nr,
  83			config->csv_sep);
 
  84		break;
  85	case AGGR_DIE:
  86		fprintf(config->output, "S%d-D%*d%s%*d%s",
  87			cpu_map__id_to_socket(id << 16),
  88			config->csv_output ? 0 : -8,
  89			cpu_map__id_to_die(id << 16),
  90			config->csv_sep,
  91			config->csv_output ? 0 : 4,
  92			nr,
  93			config->csv_sep);
  94		break;
  95	case AGGR_SOCKET:
  96		fprintf(config->output, "S%*d%s%*d%s",
  97			config->csv_output ? 0 : -5,
  98			id,
  99			config->csv_sep,
 100			config->csv_output ? 0 : 4,
 101			nr,
 102			config->csv_sep);
 103			break;
 104	case AGGR_NODE:
 105		fprintf(config->output, "N%*d%s%*d%s",
 106			config->csv_output ? 0 : -5,
 107			id,
 108			config->csv_sep,
 109			config->csv_output ? 0 : 4,
 110			nr,
 111			config->csv_sep);
 112			break;
 113	case AGGR_NONE:
 114		if (evsel->percore && !config->percore_show_thread) {
 115			fprintf(config->output, "S%d-D%d-C%*d%s",
 116				cpu_map__id_to_socket(id),
 117				cpu_map__id_to_die(id),
 118				config->csv_output ? 0 : -3,
 119				cpu_map__id_to_cpu(id), config->csv_sep);
 120		} else if (id > -1) {
 121			fprintf(config->output, "CPU%*d%s",
 122				config->csv_output ? 0 : -7,
 123				evsel__cpus(evsel)->map[id],
 124				config->csv_sep);
 125		}
 126		break;
 127	case AGGR_THREAD:
 128		fprintf(config->output, "%*s-%*d%s",
 129			config->csv_output ? 0 : 16,
 130			perf_thread_map__comm(evsel->core.threads, id),
 131			config->csv_output ? 0 : -8,
 132			perf_thread_map__pid(evsel->core.threads, id),
 133			config->csv_sep);
 134		break;
 135	case AGGR_GLOBAL:
 136	case AGGR_UNSET:
 
 137	default:
 138		break;
 139	}
 140}
 141
 142struct outstate {
 143	FILE *fh;
 144	bool newline;
 145	const char *prefix;
 146	int  nfields;
 147	int  id, nr;
 148	struct evsel *evsel;
 149};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 150
 151#define METRIC_LEN  35
 
 
 
 
 
 
 
 
 
 152
 153static void new_line_std(struct perf_stat_config *config __maybe_unused,
 154			 void *ctx)
 155{
 156	struct outstate *os = ctx;
 157
 158	os->newline = true;
 159}
 160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 161static void do_new_line_std(struct perf_stat_config *config,
 162			    struct outstate *os)
 163{
 164	fputc('\n', os->fh);
 165	fputs(os->prefix, os->fh);
 166	aggr_printout(config, os->evsel, os->id, os->nr);
 167	if (config->aggr_mode == AGGR_NONE)
 168		fprintf(os->fh, "        ");
 169	fprintf(os->fh, "                                                 ");
 170}
 171
 172static void print_metric_std(struct perf_stat_config *config,
 173			     void *ctx, const char *color, const char *fmt,
 174			     const char *unit, double val)
 175{
 176	struct outstate *os = ctx;
 177	FILE *out = os->fh;
 178	int n;
 179	bool newline = os->newline;
 
 180
 181	os->newline = false;
 182
 183	if (unit == NULL || fmt == NULL) {
 184		fprintf(out, "%-*s", METRIC_LEN, "");
 185		return;
 186	}
 187
 188	if (newline)
 189		do_new_line_std(config, os);
 190
 191	n = fprintf(out, " # ");
 192	if (color)
 193		n += color_fprintf(out, color, fmt, val);
 194	else
 195		n += fprintf(out, fmt, val);
 196	fprintf(out, " %-*s", METRIC_LEN - n - 1, unit);
 197}
 198
 199static void new_line_csv(struct perf_stat_config *config, void *ctx)
 200{
 201	struct outstate *os = ctx;
 202	int i;
 203
 204	fputc('\n', os->fh);
 205	if (os->prefix)
 206		fprintf(os->fh, "%s%s", os->prefix, config->csv_sep);
 207	aggr_printout(config, os->evsel, os->id, os->nr);
 208	for (i = 0; i < os->nfields; i++)
 209		fputs(config->csv_sep, os->fh);
 210}
 211
 212static void print_metric_csv(struct perf_stat_config *config __maybe_unused,
 213			     void *ctx,
 214			     const char *color __maybe_unused,
 215			     const char *fmt, const char *unit, double val)
 216{
 217	struct outstate *os = ctx;
 218	FILE *out = os->fh;
 219	char buf[64], *vals, *ends;
 220
 221	if (unit == NULL || fmt == NULL) {
 222		fprintf(out, "%s%s", config->csv_sep, config->csv_sep);
 223		return;
 224	}
 225	snprintf(buf, sizeof(buf), fmt, val);
 226	ends = vals = skip_spaces(buf);
 227	while (isdigit(*ends) || *ends == '.')
 228		ends++;
 229	*ends = 0;
 230	fprintf(out, "%s%s%s%s", config->csv_sep, vals, config->csv_sep, skip_spaces(unit));
 231}
 232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 233/* Filter out some columns that don't work well in metrics only mode */
 234
 235static bool valid_only_metric(const char *unit)
 236{
 237	if (!unit)
 238		return false;
 239	if (strstr(unit, "/sec") ||
 240	    strstr(unit, "CPUs utilized"))
 241		return false;
 242	return true;
 243}
 244
 245static const char *fixunit(char *buf, struct evsel *evsel,
 246			   const char *unit)
 247{
 248	if (!strncmp(unit, "of all", 6)) {
 249		snprintf(buf, 1024, "%s %s", evsel__name(evsel),
 250			 unit);
 251		return buf;
 252	}
 253	return unit;
 254}
 255
 256static void print_metric_only(struct perf_stat_config *config,
 257			      void *ctx, const char *color, const char *fmt,
 258			      const char *unit, double val)
 259{
 260	struct outstate *os = ctx;
 261	FILE *out = os->fh;
 262	char buf[1024], str[1024];
 263	unsigned mlen = config->metric_only_len;
 
 264
 265	if (!valid_only_metric(unit))
 266		return;
 267	unit = fixunit(buf, os->evsel, unit);
 268	if (mlen < strlen(unit))
 269		mlen = strlen(unit) + 1;
 270
 271	if (color)
 272		mlen += strlen(color) + sizeof(PERF_COLOR_RESET) - 1;
 273
 274	color_snprintf(str, sizeof(str), color ?: "", fmt, val);
 275	fprintf(out, "%*s ", mlen, str);
 
 276}
 277
 278static void print_metric_only_csv(struct perf_stat_config *config __maybe_unused,
 279				  void *ctx, const char *color __maybe_unused,
 
 280				  const char *fmt,
 281				  const char *unit, double val)
 282{
 283	struct outstate *os = ctx;
 284	FILE *out = os->fh;
 285	char buf[64], *vals, *ends;
 286	char tbuf[1024];
 287
 288	if (!valid_only_metric(unit))
 289		return;
 290	unit = fixunit(tbuf, os->evsel, unit);
 291	snprintf(buf, sizeof buf, fmt, val);
 292	ends = vals = skip_spaces(buf);
 293	while (isdigit(*ends) || *ends == '.')
 294		ends++;
 295	*ends = 0;
 296	fprintf(out, "%s%s", vals, config->csv_sep);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 297}
 298
 299static void new_line_metric(struct perf_stat_config *config __maybe_unused,
 300			    void *ctx __maybe_unused)
 301{
 302}
 303
 304static void print_metric_header(struct perf_stat_config *config,
 305				void *ctx, const char *color __maybe_unused,
 
 306				const char *fmt __maybe_unused,
 307				const char *unit, double val __maybe_unused)
 308{
 309	struct outstate *os = ctx;
 310	char tbuf[1024];
 311
 
 
 
 
 
 
 
 
 312	if (!valid_only_metric(unit))
 313		return;
 314	unit = fixunit(tbuf, os->evsel, unit);
 315	if (config->csv_output)
 
 
 
 316		fprintf(os->fh, "%s%s", unit, config->csv_sep);
 317	else
 318		fprintf(os->fh, "%*s ", config->metric_only_len, unit);
 319}
 320
 321static int first_shadow_cpu(struct perf_stat_config *config,
 322			    struct evsel *evsel, int id)
 323{
 324	struct evlist *evlist = evsel->evlist;
 325	int i;
 
 
 326
 327	if (!config->aggr_get_id)
 328		return 0;
 
 
 329
 330	if (config->aggr_mode == AGGR_NONE)
 331		return id;
 
 
 332
 333	if (config->aggr_mode == AGGR_GLOBAL)
 334		return 0;
 335
 336	for (i = 0; i < evsel__nr_cpus(evsel); i++) {
 337		int cpu2 = evsel__cpus(evsel)->map[i];
 338
 339		if (config->aggr_get_id(config, evlist->core.cpus, cpu2) == id)
 340			return cpu2;
 341	}
 342	return 0;
 343}
 344
 345static void abs_printout(struct perf_stat_config *config,
 346			 int id, int nr, struct evsel *evsel, double avg)
 347{
 348	FILE *output = config->output;
 349	double sc =  evsel->scale;
 350	const char *fmt;
 
 
 
 
 
 
 
 
 
 
 351
 352	if (config->csv_output) {
 353		fmt = floor(sc) != sc ?  "%.2f%s" : "%.0f%s";
 354	} else {
 355		if (config->big_num)
 356			fmt = floor(sc) != sc ? "%'18.2f%s" : "%'18.0f%s";
 357		else
 358			fmt = floor(sc) != sc ? "%18.2f%s" : "%18.0f%s";
 359	}
 360
 361	aggr_printout(config, evsel, id, nr);
 
 
 
 362
 363	fprintf(output, fmt, avg, config->csv_sep);
 
 
 
 364
 365	if (evsel->unit)
 366		fprintf(output, "%-*s%s",
 367			config->csv_output ? 0 : config->unit_width,
 368			evsel->unit, config->csv_sep);
 
 369
 370	fprintf(output, "%-*s", config->csv_output ? 0 : 25, evsel__name(evsel));
 
 
 
 
 
 
 
 
 
 371
 372	print_cgroup(config, evsel);
 
 
 
 
 
 
 
 373}
 374
 375static bool is_mixed_hw_group(struct evsel *counter)
 376{
 377	struct evlist *evlist = counter->evlist;
 378	u32 pmu_type = counter->core.attr.type;
 379	struct evsel *pos;
 380
 381	if (counter->core.nr_members < 2)
 382		return false;
 383
 384	evlist__for_each_entry(evlist, pos) {
 385		/* software events can be part of any hardware group */
 386		if (pos->core.attr.type == PERF_TYPE_SOFTWARE)
 387			continue;
 388		if (pmu_type == PERF_TYPE_SOFTWARE) {
 389			pmu_type = pos->core.attr.type;
 390			continue;
 391		}
 392		if (pmu_type != pos->core.attr.type)
 393			return true;
 394	}
 395
 396	return false;
 397}
 398
 399static void printout(struct perf_stat_config *config, int id, int nr,
 400		     struct evsel *counter, double uval,
 401		     char *prefix, u64 run, u64 ena, double noise,
 402		     struct runtime_stat *st)
 
 
 
 
 
 
 
 
 
 
 
 
 
 403{
 404	struct perf_stat_output_ctx out;
 405	struct outstate os = {
 406		.fh = config->output,
 407		.prefix = prefix ? prefix : "",
 408		.id = id,
 409		.nr = nr,
 410		.evsel = counter,
 411	};
 412	print_metric_t pm = print_metric_std;
 413	new_line_t nl;
 
 
 
 414
 415	if (config->metric_only) {
 416		nl = new_line_metric;
 417		if (config->csv_output)
 418			pm = print_metric_only_csv;
 419		else
 420			pm = print_metric_only;
 421	} else
 422		nl = new_line_std;
 423
 424	if (config->csv_output && !config->metric_only) {
 425		static int aggr_fields[] = {
 426			[AGGR_GLOBAL] = 0,
 427			[AGGR_THREAD] = 1,
 428			[AGGR_NONE] = 1,
 429			[AGGR_SOCKET] = 2,
 430			[AGGR_DIE] = 2,
 431			[AGGR_CORE] = 2,
 432		};
 433
 434		pm = print_metric_csv;
 435		nl = new_line_csv;
 436		os.nfields = 3;
 437		os.nfields += aggr_fields[config->aggr_mode];
 438		if (counter->cgrp)
 439			os.nfields++;
 440	}
 
 441	if (run == 0 || ena == 0 || counter->counts->scaled == -1) {
 442		if (config->metric_only) {
 443			pm(config, &os, NULL, "", "", 0);
 444			return;
 445		}
 446		aggr_printout(config, counter, id, nr);
 447
 448		fprintf(config->output, "%*s%s",
 449			config->csv_output ? 0 : 18,
 450			counter->supported ? CNTR_NOT_COUNTED : CNTR_NOT_SUPPORTED,
 451			config->csv_sep);
 452
 453		if (counter->supported) {
 454			config->print_free_counters_hint = 1;
 455			if (is_mixed_hw_group(counter))
 456				config->print_mixed_hw_group_error = 1;
 
 
 457		}
 458
 459		fprintf(config->output, "%-*s%s",
 460			config->csv_output ? 0 : config->unit_width,
 461			counter->unit, config->csv_sep);
 462
 463		fprintf(config->output, "%*s",
 464			config->csv_output ? 0 : -25, evsel__name(counter));
 465
 466		print_cgroup(config, counter);
 467
 468		if (!config->csv_output)
 469			pm(config, &os, NULL, NULL, "", 0);
 470		print_noise(config, counter, noise);
 471		print_running(config, run, ena);
 472		if (config->csv_output)
 473			pm(config, &os, NULL, NULL, "", 0);
 474		return;
 475	}
 476
 477	if (!config->metric_only)
 478		abs_printout(config, id, nr, counter, uval);
 479
 480	out.print_metric = pm;
 481	out.new_line = nl;
 482	out.ctx = &os;
 
 483	out.force_header = false;
 484
 485	if (config->csv_output && !config->metric_only) {
 486		print_noise(config, counter, noise);
 487		print_running(config, run, ena);
 
 
 488	}
 489
 490	perf_stat__print_shadow_stats(config, counter, uval,
 491				first_shadow_cpu(config, counter, id),
 492				&out, &config->metric_events, st);
 493	if (!config->csv_output && !config->metric_only) {
 494		print_noise(config, counter, noise);
 495		print_running(config, run, ena);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 496	}
 497}
 498
 499static void aggr_update_shadow(struct perf_stat_config *config,
 500			       struct evlist *evlist)
 501{
 502	int cpu, s2, id, s;
 503	u64 val;
 504	struct evsel *counter;
 505
 506	for (s = 0; s < config->aggr_map->nr; s++) {
 507		id = config->aggr_map->map[s];
 508		evlist__for_each_entry(evlist, counter) {
 509			val = 0;
 510			for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
 511				s2 = config->aggr_get_id(config, evlist->core.cpus, cpu);
 512				if (s2 != id)
 513					continue;
 514				val += perf_counts(counter->counts, cpu, 0)->val;
 515			}
 516			perf_stat__update_shadow_stats(counter, val,
 517					first_shadow_cpu(config, counter, id),
 518					&rt_stat);
 519		}
 520	}
 521}
 522
 523static void uniquify_event_name(struct evsel *counter)
 524{
 525	char *new_name;
 526	char *config;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 527
 528	if (counter->uniquified_name ||
 529	    !counter->pmu_name || !strncmp(counter->name, counter->pmu_name,
 530					   strlen(counter->pmu_name)))
 
 531		return;
 532
 533	config = strchr(counter->name, '/');
 534	if (config) {
 535		if (asprintf(&new_name,
 536			     "%s%s", counter->pmu_name, config) > 0) {
 537			free(counter->name);
 538			counter->name = new_name;
 
 
 
 
 539		}
 540	} else {
 541		if (asprintf(&new_name,
 542			     "%s [%s]", counter->name, counter->pmu_name) > 0) {
 543			free(counter->name);
 544			counter->name = new_name;
 
 
 
 
 
 545		}
 546	}
 547
 548	counter->uniquified_name = true;
 
 
 
 
 
 549}
 550
 551static void collect_all_aliases(struct perf_stat_config *config, struct evsel *counter,
 552			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
 553				       bool first),
 554			    void *data)
 555{
 556	struct evlist *evlist = counter->evlist;
 557	struct evsel *alias;
 558
 559	alias = list_prepare_entry(counter, &(evlist->core.entries), core.node);
 560	list_for_each_entry_continue (alias, &evlist->core.entries, core.node) {
 561		if (strcmp(evsel__name(alias), evsel__name(counter)) ||
 562		    alias->scale != counter->scale ||
 563		    alias->cgrp != counter->cgrp ||
 564		    strcmp(alias->unit, counter->unit) ||
 565		    evsel__is_clock(alias) != evsel__is_clock(counter) ||
 566		    !strcmp(alias->pmu_name, counter->pmu_name))
 567			break;
 568		alias->merged_stat = true;
 569		cb(config, alias, data, false);
 570	}
 571}
 572
 573static bool collect_data(struct perf_stat_config *config, struct evsel *counter,
 574			    void (*cb)(struct perf_stat_config *config, struct evsel *counter, void *data,
 575				       bool first),
 576			    void *data)
 577{
 578	if (counter->merged_stat)
 579		return false;
 580	cb(config, counter, data, true);
 581	if (config->no_merge)
 582		uniquify_event_name(counter);
 583	else if (counter->auto_merge_stats)
 584		collect_all_aliases(config, counter, cb, data);
 585	return true;
 586}
 587
 588struct aggr_data {
 589	u64 ena, run, val;
 590	int id;
 591	int nr;
 592	int cpu;
 593};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 594
 595static void aggr_cb(struct perf_stat_config *config,
 596		    struct evsel *counter, void *data, bool first)
 597{
 598	struct aggr_data *ad = data;
 599	int cpu, s2;
 
 600
 601	for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
 602		struct perf_counts_values *counts;
 603
 604		s2 = config->aggr_get_id(config, evsel__cpus(counter), cpu);
 605		if (s2 != ad->id)
 606			continue;
 607		if (first)
 608			ad->nr++;
 609		counts = perf_counts(counter->counts, cpu, 0);
 610		/*
 611		 * When any result is bad, make them all to give
 612		 * consistent output in interval mode.
 613		 */
 614		if (counts->ena == 0 || counts->run == 0 ||
 615		    counter->counts->scaled == -1) {
 616			ad->ena = 0;
 617			ad->run = 0;
 618			break;
 619		}
 620		ad->val += counts->val;
 621		ad->ena += counts->ena;
 622		ad->run += counts->run;
 623	}
 
 624}
 625
 626static void print_counter_aggrdata(struct perf_stat_config *config,
 627				   struct evsel *counter, int s,
 628				   char *prefix, bool metric_only,
 629				   bool *first, int cpu)
 630{
 631	struct aggr_data ad;
 632	FILE *output = config->output;
 633	u64 ena, run, val;
 634	int id, nr;
 635	double uval;
 
 
 
 
 
 
 
 
 
 636
 637	ad.id = id = config->aggr_map->map[s];
 638	ad.val = ad.ena = ad.run = 0;
 639	ad.nr = 0;
 640	if (!collect_data(config, counter, aggr_cb, &ad))
 641		return;
 642
 643	nr = ad.nr;
 644	ena = ad.ena;
 645	run = ad.run;
 646	val = ad.val;
 647	if (*first && metric_only) {
 648		*first = false;
 649		aggr_printout(config, counter, id, nr);
 650	}
 651	if (prefix && !metric_only)
 652		fprintf(output, "%s", prefix);
 653
 654	uval = val * counter->scale;
 655	printout(config, cpu != -1 ? cpu : id, nr, counter, uval, prefix,
 656		 run, ena, 1.0, &rt_stat);
 657	if (!metric_only)
 658		fputc('\n', output);
 659}
 660
 661static void print_aggr(struct perf_stat_config *config,
 662		       struct evlist *evlist,
 663		       char *prefix)
 664{
 665	bool metric_only = config->metric_only;
 666	FILE *output = config->output;
 667	struct evsel *counter;
 668	int s;
 669	bool first;
 670
 671	if (!config->aggr_map || !config->aggr_get_id)
 672		return;
 673
 674	aggr_update_shadow(config, evlist);
 675
 676	/*
 677	 * With metric_only everything is on a single line.
 678	 * Without each counter has its own line.
 679	 */
 680	for (s = 0; s < config->aggr_map->nr; s++) {
 681		if (prefix && metric_only)
 682			fprintf(output, "%s", prefix);
 683
 684		first = true;
 685		evlist__for_each_entry(evlist, counter) {
 686			print_counter_aggrdata(config, counter, s,
 687					       prefix, metric_only,
 688					       &first, -1);
 689		}
 690		if (metric_only)
 691			fputc('\n', output);
 
 
 
 
 
 
 692	}
 693}
 694
 695static int cmp_val(const void *a, const void *b)
 696{
 697	return ((struct perf_aggr_thread_value *)b)->val -
 698		((struct perf_aggr_thread_value *)a)->val;
 
 
 699}
 700
 701static struct perf_aggr_thread_value *sort_aggr_thread(
 702					struct evsel *counter,
 703					int nthreads, int ncpus,
 704					int *ret,
 705					struct target *_target)
 706{
 707	int cpu, thread, i = 0;
 708	double uval;
 709	struct perf_aggr_thread_value *buf;
 710
 711	buf = calloc(nthreads, sizeof(struct perf_aggr_thread_value));
 712	if (!buf)
 713		return NULL;
 714
 715	for (thread = 0; thread < nthreads; thread++) {
 716		u64 ena = 0, run = 0, val = 0;
 717
 718		for (cpu = 0; cpu < ncpus; cpu++) {
 719			val += perf_counts(counter->counts, cpu, thread)->val;
 720			ena += perf_counts(counter->counts, cpu, thread)->ena;
 721			run += perf_counts(counter->counts, cpu, thread)->run;
 722		}
 723
 724		uval = val * counter->scale;
 
 725
 726		/*
 727		 * Skip value 0 when enabling --per-thread globally,
 728		 * otherwise too many 0 output.
 729		 */
 730		if (uval == 0.0 && target__has_per_thread(_target))
 731			continue;
 732
 733		buf[i].counter = counter;
 734		buf[i].id = thread;
 735		buf[i].uval = uval;
 736		buf[i].val = val;
 737		buf[i].run = run;
 738		buf[i].ena = ena;
 739		i++;
 740	}
 
 
 
 
 741
 742	qsort(buf, i, sizeof(struct perf_aggr_thread_value), cmp_val);
 743
 744	if (ret)
 745		*ret = i;
 746
 747	return buf;
 748}
 749
 750static void print_aggr_thread(struct perf_stat_config *config,
 751			      struct target *_target,
 752			      struct evsel *counter, char *prefix)
 753{
 754	FILE *output = config->output;
 755	int nthreads = perf_thread_map__nr(counter->core.threads);
 756	int ncpus = perf_cpu_map__nr(counter->core.cpus);
 757	int thread, sorted_threads, id;
 758	struct perf_aggr_thread_value *buf;
 759
 760	buf = sort_aggr_thread(counter, nthreads, ncpus, &sorted_threads, _target);
 761	if (!buf) {
 762		perror("cannot sort aggr thread");
 763		return;
 764	}
 765
 766	for (thread = 0; thread < sorted_threads; thread++) {
 767		if (prefix)
 768			fprintf(output, "%s", prefix);
 769
 770		id = buf[thread].id;
 771		if (config->stats)
 772			printout(config, id, 0, buf[thread].counter, buf[thread].uval,
 773				 prefix, buf[thread].run, buf[thread].ena, 1.0,
 774				 &config->stats[id]);
 775		else
 776			printout(config, id, 0, buf[thread].counter, buf[thread].uval,
 777				 prefix, buf[thread].run, buf[thread].ena, 1.0,
 778				 &rt_stat);
 779		fputc('\n', output);
 780	}
 
 
 781
 782	free(buf);
 783}
 
 
 
 
 784
 785struct caggr_data {
 786	double avg, avg_enabled, avg_running;
 787};
 788
 789static void counter_aggr_cb(struct perf_stat_config *config __maybe_unused,
 790			    struct evsel *counter, void *data,
 791			    bool first __maybe_unused)
 792{
 793	struct caggr_data *cd = data;
 794	struct perf_stat_evsel *ps = counter->stats;
 795
 796	cd->avg += avg_stats(&ps->res_stats[0]);
 797	cd->avg_enabled += avg_stats(&ps->res_stats[1]);
 798	cd->avg_running += avg_stats(&ps->res_stats[2]);
 
 
 799}
 800
 801/*
 802 * Print out the results of a single counter:
 803 * aggregated counts in system-wide mode
 804 */
 805static void print_counter_aggr(struct perf_stat_config *config,
 806			       struct evsel *counter, char *prefix)
 807{
 808	bool metric_only = config->metric_only;
 809	FILE *output = config->output;
 810	double uval;
 811	struct caggr_data cd = { .avg = 0.0 };
 812
 813	if (!collect_data(config, counter, counter_aggr_cb, &cd))
 814		return;
 815
 816	if (prefix && !metric_only)
 817		fprintf(output, "%s", prefix);
 
 
 
 818
 819	uval = cd.avg * counter->scale;
 820	printout(config, -1, 0, counter, uval, prefix, cd.avg_running, cd.avg_enabled,
 821		 cd.avg, &rt_stat);
 822	if (!metric_only)
 823		fprintf(output, "\n");
 824}
 825
 826static void counter_cb(struct perf_stat_config *config __maybe_unused,
 827		       struct evsel *counter, void *data,
 828		       bool first __maybe_unused)
 829{
 830	struct aggr_data *ad = data;
 831
 832	ad->val += perf_counts(counter->counts, ad->cpu, 0)->val;
 833	ad->ena += perf_counts(counter->counts, ad->cpu, 0)->ena;
 834	ad->run += perf_counts(counter->counts, ad->cpu, 0)->run;
 
 
 835}
 836
 837/*
 838 * Print out the results of a single counter:
 839 * does not use aggregated count in system-wide
 840 */
 841static void print_counter(struct perf_stat_config *config,
 842			  struct evsel *counter, char *prefix)
 843{
 844	FILE *output = config->output;
 845	u64 ena, run, val;
 846	double uval;
 847	int cpu;
 848
 849	for (cpu = 0; cpu < evsel__nr_cpus(counter); cpu++) {
 850		struct aggr_data ad = { .cpu = cpu };
 851
 852		if (!collect_data(config, counter, counter_cb, &ad))
 853			return;
 854		val = ad.val;
 855		ena = ad.ena;
 856		run = ad.run;
 857
 858		if (prefix)
 859			fprintf(output, "%s", prefix);
 860
 861		uval = val * counter->scale;
 862		printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
 863			 &rt_stat);
 864
 865		fputc('\n', output);
 
 866	}
 867}
 868
 869static void print_no_aggr_metric(struct perf_stat_config *config,
 870				 struct evlist *evlist,
 871				 char *prefix)
 872{
 873	int cpu;
 874	int nrcpus = 0;
 875	struct evsel *counter;
 876	u64 ena, run, val;
 877	double uval;
 878
 879	nrcpus = evlist->core.cpus->nr;
 880	for (cpu = 0; cpu < nrcpus; cpu++) {
 881		bool first = true;
 882
 883		if (prefix)
 884			fputs(prefix, config->output);
 885		evlist__for_each_entry(evlist, counter) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 886			if (first) {
 887				aggr_printout(config, counter, cpu, 0);
 888				first = false;
 889			}
 890			val = perf_counts(counter->counts, cpu, 0)->val;
 891			ena = perf_counts(counter->counts, cpu, 0)->ena;
 892			run = perf_counts(counter->counts, cpu, 0)->run;
 893
 894			uval = val * counter->scale;
 895			printout(config, cpu, 0, counter, uval, prefix, run, ena, 1.0,
 896				 &rt_stat);
 897		}
 898		fputc('\n', config->output);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 899	}
 900}
 901
 902static int aggr_header_lens[] = {
 903	[AGGR_CORE] = 24,
 904	[AGGR_DIE] = 18,
 905	[AGGR_SOCKET] = 12,
 906	[AGGR_NONE] = 6,
 907	[AGGR_THREAD] = 24,
 908	[AGGR_GLOBAL] = 0,
 909};
 
 
 
 
 
 
 
 
 
 
 
 910
 911static const char *aggr_header_csv[] = {
 912	[AGGR_CORE] 	= 	"core,cpus,",
 913	[AGGR_DIE] 	= 	"die,cpus",
 914	[AGGR_SOCKET] 	= 	"socket,cpus",
 915	[AGGR_NONE] 	= 	"cpu,",
 916	[AGGR_THREAD] 	= 	"comm-pid,",
 917	[AGGR_GLOBAL] 	=	""
 918};
 919
 920static void print_metric_headers(struct perf_stat_config *config,
 921				 struct evlist *evlist,
 922				 const char *prefix, bool no_indent)
 923{
 924	struct perf_stat_output_ctx out;
 925	struct evsel *counter;
 926	struct outstate os = {
 927		.fh = config->output
 928	};
 
 
 
 
 
 
 929
 930	if (prefix)
 931		fprintf(config->output, "%s", prefix);
 
 
 
 
 
 
 
 932
 933	if (!config->csv_output && !no_indent)
 934		fprintf(config->output, "%*s",
 935			aggr_header_lens[config->aggr_mode], "");
 936	if (config->csv_output) {
 937		if (config->interval)
 938			fputs("time,", config->output);
 939		fputs(aggr_header_csv[config->aggr_mode], config->output);
 940	}
 941
 942	/* Print metrics headers only */
 943	evlist__for_each_entry(evlist, counter) {
 
 
 
 
 944		os.evsel = counter;
 945		out.ctx = &os;
 946		out.print_metric = print_metric_header;
 947		out.new_line = new_line_metric;
 948		out.force_header = true;
 949		os.evsel = counter;
 950		perf_stat__print_shadow_stats(config, counter, 0,
 951					      0,
 952					      &out,
 953					      &config->metric_events,
 954					      &rt_stat);
 955	}
 956	fputc('\n', config->output);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 957}
 958
 959static void print_interval(struct perf_stat_config *config,
 960			   struct evlist *evlist,
 961			   char *prefix, struct timespec *ts)
 
 
 962{
 963	bool metric_only = config->metric_only;
 964	unsigned int unit_width = config->unit_width;
 965	FILE *output = config->output;
 966	static int num_print_interval;
 967
 968	if (config->interval_clear)
 969		puts(CONSOLE_CLEAR);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 970
 971	sprintf(prefix, "%6lu.%09lu%s", ts->tv_sec, ts->tv_nsec, config->csv_sep);
 
 
 
 
 
 972
 973	if ((num_print_interval == 0 && !config->csv_output) || config->interval_clear) {
 974		switch (config->aggr_mode) {
 975		case AGGR_NODE:
 976			fprintf(output, "#           time node   cpus");
 977			if (!metric_only)
 978				fprintf(output, "             counts %*s events\n", unit_width, "unit");
 979			break;
 980		case AGGR_SOCKET:
 981			fprintf(output, "#           time socket cpus");
 982			if (!metric_only)
 983				fprintf(output, "             counts %*s events\n", unit_width, "unit");
 984			break;
 985		case AGGR_DIE:
 986			fprintf(output, "#           time die          cpus");
 987			if (!metric_only)
 988				fprintf(output, "             counts %*s events\n", unit_width, "unit");
 989			break;
 990		case AGGR_CORE:
 991			fprintf(output, "#           time core            cpus");
 992			if (!metric_only)
 993				fprintf(output, "             counts %*s events\n", unit_width, "unit");
 994			break;
 995		case AGGR_NONE:
 996			fprintf(output, "#           time CPU    ");
 997			if (!metric_only)
 998				fprintf(output, "                counts %*s events\n", unit_width, "unit");
 999			break;
1000		case AGGR_THREAD:
1001			fprintf(output, "#           time             comm-pid");
1002			if (!metric_only)
1003				fprintf(output, "                  counts %*s events\n", unit_width, "unit");
1004			break;
1005		case AGGR_GLOBAL:
1006		default:
1007			fprintf(output, "#           time");
1008			if (!metric_only)
1009				fprintf(output, "             counts %*s events\n", unit_width, "unit");
1010		case AGGR_UNSET:
1011			break;
1012		}
1013	}
1014
1015	if ((num_print_interval == 0 || config->interval_clear) && metric_only)
1016		print_metric_headers(config, evlist, " ", true);
1017	if (++num_print_interval == 25)
1018		num_print_interval = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1019}
1020
1021static void print_header(struct perf_stat_config *config,
1022			 struct target *_target,
 
1023			 int argc, const char **argv)
1024{
1025	FILE *output = config->output;
1026	int i;
1027
1028	fflush(stdout);
1029
1030	if (!config->csv_output) {
1031		fprintf(output, "\n");
1032		fprintf(output, " Performance counter stats for ");
1033		if (_target->system_wide)
1034			fprintf(output, "\'system wide");
1035		else if (_target->cpu_list)
1036			fprintf(output, "\'CPU(s) %s", _target->cpu_list);
1037		else if (!target__has_task(_target)) {
1038			fprintf(output, "\'%s", argv ? argv[0] : "pipe");
1039			for (i = 1; argv && (i < argc); i++)
1040				fprintf(output, " %s", argv[i]);
1041		} else if (_target->pid)
1042			fprintf(output, "process id \'%s", _target->pid);
1043		else
1044			fprintf(output, "thread id \'%s", _target->tid);
 
1045
1046		fprintf(output, "\'");
1047		if (config->run_count > 1)
1048			fprintf(output, " (%d runs)", config->run_count);
1049		fprintf(output, ":\n\n");
1050	}
1051}
1052
1053static int get_precision(double num)
1054{
1055	if (num > 1)
1056		return 0;
1057
1058	return lround(ceil(-log10(num)));
1059}
1060
1061static void print_table(struct perf_stat_config *config,
1062			FILE *output, int precision, double avg)
1063{
1064	char tmp[64];
1065	int idx, indent = 0;
1066
1067	scnprintf(tmp, 64, " %17.*f", precision, avg);
1068	while (tmp[indent] == ' ')
1069		indent++;
1070
1071	fprintf(output, "%*s# Table of individual measurements:\n", indent, "");
1072
1073	for (idx = 0; idx < config->run_count; idx++) {
1074		double run = (double) config->walltime_run[idx] / NSEC_PER_SEC;
1075		int h, n = 1 + abs((int) (100.0 * (run - avg)/run) / 5);
1076
1077		fprintf(output, " %17.*f (%+.*f) ",
1078			precision, run, precision, run - avg);
1079
1080		for (h = 0; h < n; h++)
1081			fprintf(output, "#");
1082
1083		fprintf(output, "\n");
1084	}
1085
1086	fprintf(output, "\n%*s# Final result:\n", indent, "");
1087}
1088
1089static double timeval2double(struct timeval *t)
1090{
1091	return t->tv_sec + (double) t->tv_usec/USEC_PER_SEC;
1092}
1093
1094static void print_footer(struct perf_stat_config *config)
1095{
1096	double avg = avg_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1097	FILE *output = config->output;
1098
 
 
 
1099	if (!config->null_run)
1100		fprintf(output, "\n");
1101
1102	if (config->run_count == 1) {
1103		fprintf(output, " %17.9f seconds time elapsed", avg);
1104
1105		if (config->ru_display) {
1106			double ru_utime = timeval2double(&config->ru_data.ru_utime);
1107			double ru_stime = timeval2double(&config->ru_data.ru_stime);
1108
1109			fprintf(output, "\n\n");
1110			fprintf(output, " %17.9f seconds user\n", ru_utime);
1111			fprintf(output, " %17.9f seconds sys\n", ru_stime);
1112		}
1113	} else {
1114		double sd = stddev_stats(config->walltime_nsecs_stats) / NSEC_PER_SEC;
1115		/*
1116		 * Display at most 2 more significant
1117		 * digits than the stddev inaccuracy.
1118		 */
1119		int precision = get_precision(sd) + 2;
1120
1121		if (config->walltime_run_table)
1122			print_table(config, output, precision, avg);
1123
1124		fprintf(output, " %17.*f +- %.*f seconds time elapsed",
1125			precision, avg, precision, sd);
1126
1127		print_noise_pct(config, sd, avg);
1128	}
1129	fprintf(output, "\n\n");
1130
1131	if (config->print_free_counters_hint && sysctl__nmi_watchdog_enabled())
1132		fprintf(output,
1133"Some events weren't counted. Try disabling the NMI watchdog:\n"
1134"	echo 0 > /proc/sys/kernel/nmi_watchdog\n"
1135"	perf stat ...\n"
1136"	echo 1 > /proc/sys/kernel/nmi_watchdog\n");
1137
1138	if (config->print_mixed_hw_group_error)
1139		fprintf(output,
1140			"The events in group usually have to be from "
1141			"the same PMU. Try reorganizing the group.\n");
1142}
1143
1144static void print_percore_thread(struct perf_stat_config *config,
1145				 struct evsel *counter, char *prefix)
1146{
1147	int s, s2, id;
1148	bool first = true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1149
1150	for (int i = 0; i < evsel__nr_cpus(counter); i++) {
1151		s2 = config->aggr_get_id(config, evsel__cpus(counter), i);
1152		for (s = 0; s < config->aggr_map->nr; s++) {
1153			id = config->aggr_map->map[s];
1154			if (s2 == id)
 
 
 
1155				break;
 
1156		}
 
 
1157
1158		print_counter_aggrdata(config, counter, s,
1159				       prefix, false,
1160				       &first, i);
1161	}
 
 
 
 
1162}
1163
1164static void print_percore(struct perf_stat_config *config,
1165			  struct evsel *counter, char *prefix)
1166{
1167	bool metric_only = config->metric_only;
1168	FILE *output = config->output;
1169	int s;
1170	bool first = true;
1171
1172	if (!config->aggr_map || !config->aggr_get_id)
1173		return;
 
 
1174
1175	if (config->percore_show_thread)
1176		return print_percore_thread(config, counter, prefix);
 
1177
1178	for (s = 0; s < config->aggr_map->nr; s++) {
1179		if (prefix && metric_only)
1180			fprintf(output, "%s", prefix);
1181
1182		print_counter_aggrdata(config, counter, s,
1183				       prefix, metric_only,
1184				       &first, -1);
1185	}
 
 
 
1186
1187	if (metric_only)
1188		fputc('\n', output);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1189}
1190
1191void
1192perf_evlist__print_counters(struct evlist *evlist,
1193			    struct perf_stat_config *config,
1194			    struct target *_target,
1195			    struct timespec *ts,
1196			    int argc, const char **argv)
1197{
1198	bool metric_only = config->metric_only;
1199	int interval = config->interval;
1200	struct evsel *counter;
1201	char buf[64], *prefix = NULL;
 
 
 
 
1202
1203	if (interval)
1204		print_interval(config, evlist, prefix = buf, ts);
1205	else
1206		print_header(config, _target, argc, argv);
1207
1208	if (metric_only) {
1209		static int num_print_iv;
1210
1211		if (num_print_iv == 0 && !interval)
1212			print_metric_headers(config, evlist, prefix, false);
1213		if (num_print_iv++ == 25)
1214			num_print_iv = 0;
1215		if (config->aggr_mode == AGGR_GLOBAL && prefix)
1216			fprintf(config->output, "%s", prefix);
1217	}
1218
 
 
1219	switch (config->aggr_mode) {
1220	case AGGR_CORE:
 
 
1221	case AGGR_DIE:
1222	case AGGR_SOCKET:
1223	case AGGR_NODE:
1224		print_aggr(config, evlist, prefix);
 
 
 
1225		break;
1226	case AGGR_THREAD:
1227		evlist__for_each_entry(evlist, counter) {
1228			print_aggr_thread(config, _target, counter, prefix);
1229		}
1230		break;
1231	case AGGR_GLOBAL:
1232		evlist__for_each_entry(evlist, counter) {
1233			print_counter_aggr(config, counter, prefix);
 
 
 
 
 
 
 
 
 
1234		}
1235		if (metric_only)
1236			fputc('\n', config->output);
1237		break;
1238	case AGGR_NONE:
1239		if (metric_only)
1240			print_no_aggr_metric(config, evlist, prefix);
1241		else {
1242			evlist__for_each_entry(evlist, counter) {
1243				if (counter->percore)
1244					print_percore(config, counter, prefix);
1245				else
1246					print_counter(config, counter, prefix);
1247			}
1248		}
1249		break;
 
1250	case AGGR_UNSET:
1251	default:
1252		break;
1253	}
1254
1255	if (!interval && !config->csv_output)
1256		print_footer(config);
1257
1258	fflush(config->output);
1259}