Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c04f5e5d authored by Ingo Molnar's avatar Ingo Molnar
Browse files

perf_counter tools: Clean up builtin-stat.c's do_perfstat()



[ Impact: cleanup ]

Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com>
Cc: Marcelo Tosatti <mtosatti@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: John Kacur <jkacur@redhat.com>
LKML-Reference: <new-submission>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent c323d95f
Loading
Loading
Loading
Loading
+67 −49
Original line number Diff line number Diff line
@@ -109,56 +109,33 @@ static void create_perfstat_counter(int counter)
	}
}

static int do_perfstat(int argc, const char **argv)
{
	unsigned long long t0, t1;
	int counter;
	ssize_t res;
	int status;
	int pid;

	if (!system_wide)
		nr_cpus = 1;

	for (counter = 0; counter < nr_counters; counter++)
		create_perfstat_counter(counter);

/*
	 * Enable counters and exec the command:
 * Does the counter have nsecs as a unit?
 */
	t0 = rdclock();
	prctl(PR_TASK_PERF_COUNTERS_ENABLE);
static inline int nsec_counter(int counter)
{
	if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK))
		return 1;
	if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK))
		return 1;

	if ((pid = fork()) < 0)
		perror("failed to fork");
	if (!pid) {
		if (execvp(argv[0], (char **)argv)) {
			perror(argv[0]);
			exit(-1);
		}
	return 0;
}
	while (wait(&status) >= 0)
		;
	prctl(PR_TASK_PERF_COUNTERS_DISABLE);
	t1 = rdclock();

	fflush(stdout);

	fprintf(stderr, "\n");
	fprintf(stderr, " Performance counter stats for \'%s\':\n",
		argv[0]);
	fprintf(stderr, "\n");

	for (counter = 0; counter < nr_counters; counter++) {
		int cpu, nv;
/*
 * Print out the results of a single counter:
 */
static void print_counter(int counter)
{
	__u64 count[3], single_count[3];
	ssize_t res;
	int cpu, nv;
	int scaled;

	count[0] = count[1] = count[2] = 0;
	nv = scale ? 3 : 1;
	for (cpu = 0; cpu < nr_cpus; cpu ++) {
			res = read(fd[cpu][counter],
				   single_count, nv * sizeof(__u64));
		res = read(fd[cpu][counter], single_count, nv * sizeof(__u64));
		assert(res == nv * sizeof(__u64));

		count[0] += single_count[0];
@@ -173,7 +150,7 @@ static int do_perfstat(int argc, const char **argv)
		if (count[2] == 0) {
			fprintf(stderr, " %14s  %-20s\n",
				"<not counted>", event_name(counter));
				continue;
			return;
		}
		if (count[2] < count[1]) {
			scaled = 1;
@@ -182,9 +159,7 @@ static int do_perfstat(int argc, const char **argv)
		}
	}

		if (event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_CPU_CLOCK) ||
		    event_id[counter] == EID(PERF_TYPE_SOFTWARE, PERF_COUNT_TASK_CLOCK)) {

	if (nsec_counter(counter)) {
		double msecs = (double)count[0] / 1000000;

		fprintf(stderr, " %14.6f  %-20s (msecs)",
@@ -198,6 +173,50 @@ static int do_perfstat(int argc, const char **argv)
			(double) count[2] / count[1] * 100);
	fprintf(stderr, "\n");
}

static int do_perfstat(int argc, const char **argv)
{
	unsigned long long t0, t1;
	int counter;
	int status;
	int pid;

	if (!system_wide)
		nr_cpus = 1;

	for (counter = 0; counter < nr_counters; counter++)
		create_perfstat_counter(counter);

	/*
	 * Enable counters and exec the command:
	 */
	t0 = rdclock();
	prctl(PR_TASK_PERF_COUNTERS_ENABLE);

	if ((pid = fork()) < 0)
		perror("failed to fork");
	if (!pid) {
		if (execvp(argv[0], (char **)argv)) {
			perror(argv[0]);
			exit(-1);
		}
	}
	while (wait(&status) >= 0)
		;
	prctl(PR_TASK_PERF_COUNTERS_DISABLE);
	t1 = rdclock();

	fflush(stdout);

	fprintf(stderr, "\n");
	fprintf(stderr, " Performance counter stats for \'%s\':\n",
		argv[0]);
	fprintf(stderr, "\n");

	for (counter = 0; counter < nr_counters; counter++)
		print_counter(counter);


	fprintf(stderr, "\n");
	fprintf(stderr, " Wall-clock time elapsed: %12.6f msecs\n",
			(double)(t1-t0)/1e6);
@@ -212,7 +231,6 @@ static void skip_signal(int signo)

static const char * const stat_usage[] = {
	"perf stat [<options>] <command>",
	"perf stat [<options>] -- <command> [<options>]",
	NULL
};