Commit 33b2e2c2 authored by Namhyung Kim's avatar Namhyung Kim Committed by Arnaldo Carvalho de Melo
Browse files

perf stat: Split aggr_printout() function



The aggr_printout() function is to print aggr_id and count (nr).
Split it for each output mode to simplify the code.

Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Link: https://lore.kernel.org/r/20221114230227.1255976-6-namhyung@kernel.org


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 41cb8752
Loading
Loading
Loading
Loading
+121 −99
Original line number Original line Diff line number Diff line
@@ -135,123 +135,134 @@ static void print_cgroup(struct perf_stat_config *config, struct evsel *evsel)
	}
	}
}
}



static void print_aggr_id_std(struct perf_stat_config *config,
static void aggr_printout(struct perf_stat_config *config,
			      struct evsel *evsel, struct aggr_cpu_id id, int nr)
			      struct evsel *evsel, struct aggr_cpu_id id, int nr)
{
{
	FILE *output = config->output;


	switch (config->aggr_mode) {
	case AGGR_CORE:
		fprintf(output, "S%d-D%d-C%*d %*d ",
			id.socket, id.die, -8, id.core, 4, nr);
		break;
	case AGGR_DIE:
		fprintf(output, "S%d-D%*d %*d ",
			id.socket, -8, id.die, 4, nr);
		break;
	case AGGR_SOCKET:
		fprintf(output, "S%*d %*d ",
			-5, id.socket, 4, nr);
		break;
	case AGGR_NODE:
		fprintf(output, "N%*d %*d ",
			-5, id.node, 4, nr);
		break;
	case AGGR_NONE:
		if (evsel->percore && !config->percore_show_thread) {
			fprintf(output, "S%d-D%d-C%*d ",
				id.socket, id.die, -3, id.core);
		} else if (id.cpu.cpu > -1) {
			fprintf(output, "CPU%*d ",
				-7, id.cpu.cpu);
		}
		break;
	case AGGR_THREAD:
		fprintf(output, "%*s-%*d ",
			16, perf_thread_map__comm(evsel->core.threads, id.thread_idx),
			-8, perf_thread_map__pid(evsel->core.threads, id.thread_idx));
		break;
	case AGGR_GLOBAL:
	case AGGR_UNSET:
	case AGGR_MAX:
	default:
		break;
	}
}


	if (config->json_output && !config->interval)
static void print_aggr_id_csv(struct perf_stat_config *config,
		fprintf(config->output, "{");
			      struct evsel *evsel, struct aggr_cpu_id id, int nr)
{
	FILE *output = config->output;
	const char *sep = config->csv_sep;


	switch (config->aggr_mode) {
	switch (config->aggr_mode) {
	case AGGR_CORE:
	case AGGR_CORE:
		if (config->json_output) {
		fprintf(output, "S%d-D%d-C%d%s%d%s",
			fprintf(config->output,
			id.socket, id.die, id.core, sep, nr, sep);
				"\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
				id.socket,
				id.die,
				id.core,
				nr);
		} else {
			fprintf(config->output, "S%d-D%d-C%*d%s%*d%s",
				id.socket,
				id.die,
				config->csv_output ? 0 : -8,
				id.core,
				config->csv_sep,
				config->csv_output ? 0 : 4,
				nr,
				config->csv_sep);
		}
		break;
		break;
	case AGGR_DIE:
	case AGGR_DIE:
		if (config->json_output) {
		fprintf(output, "S%d-D%d%s%d%s",
			fprintf(config->output,
			id.socket, id.die, sep, nr, sep);
				"\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
				id.socket,
				id.die,
				nr);
		} else {
			fprintf(config->output, "S%d-D%*d%s%*d%s",
				id.socket,
				config->csv_output ? 0 : -8,
				id.die,
				config->csv_sep,
				config->csv_output ? 0 : 4,
				nr,
				config->csv_sep);
		}
		break;
		break;
	case AGGR_SOCKET:
	case AGGR_SOCKET:
		if (config->json_output) {
		fprintf(output, "S%d%s%d%s",
			fprintf(config->output,
			id.socket, sep, nr, sep);
				"\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
				id.socket,
				nr);
		} else {
			fprintf(config->output, "S%*d%s%*d%s",
				config->csv_output ? 0 : -5,
				id.socket,
				config->csv_sep,
				config->csv_output ? 0 : 4,
				nr,
				config->csv_sep);
		}
		break;
		break;
	case AGGR_NODE:
	case AGGR_NODE:
		if (config->json_output) {
		fprintf(output, "N%d%s%d%s",
			fprintf(config->output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
			id.node, sep, nr, sep);
				id.node,
				nr);
		} else {
			fprintf(config->output, "N%*d%s%*d%s",
				config->csv_output ? 0 : -5,
				id.node,
				config->csv_sep,
				config->csv_output ? 0 : 4,
				nr,
				config->csv_sep);
		}
		break;
		break;
	case AGGR_NONE:
	case AGGR_NONE:
		if (config->json_output) {
		if (evsel->percore && !config->percore_show_thread) {
		if (evsel->percore && !config->percore_show_thread) {
				fprintf(config->output, "\"core\" : \"S%d-D%d-C%d\"",
			fprintf(output, "S%d-D%d-C%d%s",
					id.socket,
				id.socket, id.die, id.core, sep);
					id.die,
					id.core);
		} else if (id.cpu.cpu > -1) {
		} else if (id.cpu.cpu > -1) {
				fprintf(config->output, "\"cpu\" : \"%d\", ",
			fprintf(output, "CPU%d%s",
					id.cpu.cpu);
				id.cpu.cpu, sep);
		}
		}
		} else {
		break;
	case AGGR_THREAD:
		fprintf(output, "%s-%d%s",
			perf_thread_map__comm(evsel->core.threads, id.thread_idx),
			perf_thread_map__pid(evsel->core.threads, id.thread_idx),
			sep);
		break;
	case AGGR_GLOBAL:
	case AGGR_UNSET:
	case AGGR_MAX:
	default:
		break;
	}
}

static void print_aggr_id_json(struct perf_stat_config *config,
			       struct evsel *evsel, struct aggr_cpu_id id, int nr)
{
	FILE *output = config->output;

	if (!config->interval)
		fputc('{', output);

	switch (config->aggr_mode) {
	case AGGR_CORE:
		fprintf(output, "\"core\" : \"S%d-D%d-C%d\", \"aggregate-number\" : %d, ",
			id.socket, id.die, id.core, nr);
		break;
	case AGGR_DIE:
		fprintf(output, "\"die\" : \"S%d-D%d\", \"aggregate-number\" : %d, ",
			id.socket, id.die, nr);
		break;
	case AGGR_SOCKET:
		fprintf(output, "\"socket\" : \"S%d\", \"aggregate-number\" : %d, ",
			id.socket, nr);
		break;
	case AGGR_NODE:
		fprintf(output, "\"node\" : \"N%d\", \"aggregate-number\" : %d, ",
			id.node, nr);
		break;
	case AGGR_NONE:
		if (evsel->percore && !config->percore_show_thread) {
		if (evsel->percore && !config->percore_show_thread) {
				fprintf(config->output, "S%d-D%d-C%*d%s",
			fprintf(output, "\"core\" : \"S%d-D%d-C%d\"",
					id.socket,
				id.socket, id.die, id.core);
					id.die,
					config->csv_output ? 0 : -3,
					id.core, config->csv_sep);
		} else if (id.cpu.cpu > -1) {
		} else if (id.cpu.cpu > -1) {
				fprintf(config->output, "CPU%*d%s",
			fprintf(output, "\"cpu\" : \"%d\", ",
					config->csv_output ? 0 : -7,
				id.cpu.cpu);
					id.cpu.cpu, config->csv_sep);
			}
		}
		}
		break;
		break;
	case AGGR_THREAD:
	case AGGR_THREAD:
		if (config->json_output) {
		fprintf(output, "\"thread\" : \"%s-%d\", ",
			fprintf(config->output, "\"thread\" : \"%s-%d\", ",
			perf_thread_map__comm(evsel->core.threads, id.thread_idx),
			perf_thread_map__comm(evsel->core.threads, id.thread_idx),
			perf_thread_map__pid(evsel->core.threads, id.thread_idx));
			perf_thread_map__pid(evsel->core.threads, id.thread_idx));
		} else {
			fprintf(config->output, "%*s-%*d%s",
				config->csv_output ? 0 : 16,
				perf_thread_map__comm(evsel->core.threads, id.thread_idx),
				config->csv_output ? 0 : -8,
				perf_thread_map__pid(evsel->core.threads, id.thread_idx),
				config->csv_sep);
		}
		break;
		break;
	case AGGR_GLOBAL:
	case AGGR_GLOBAL:
	case AGGR_UNSET:
	case AGGR_UNSET:
@@ -261,6 +272,17 @@ static void aggr_printout(struct perf_stat_config *config,
	}
	}
}
}


static void aggr_printout(struct perf_stat_config *config,
			  struct evsel *evsel, struct aggr_cpu_id id, int nr)
{
	if (config->json_output)
		print_aggr_id_json(config, evsel, id, nr);
	else if (config->csv_output)
		print_aggr_id_csv(config, evsel, id, nr);
	else
		print_aggr_id_std(config, evsel, id, nr);
}

struct outstate {
struct outstate {
	FILE *fh;
	FILE *fh;
	bool newline;
	bool newline;