xref: /linux/tools/testing/selftests/powerpc/pmu/ebb/instruction_count_test.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * Copyright 2014, Michael Ellerman, IBM Corp.
3  * Licensed under GPLv2.
4  */
5 
6 #define _GNU_SOURCE
7 
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <string.h>
11 #include <sys/prctl.h>
12 
13 #include "ebb.h"
14 
15 
16 /*
17  * Run a calibrated instruction loop and count instructions executed using
18  * EBBs. Make sure the counts look right.
19  */
20 
21 extern void thirty_two_instruction_loop(uint64_t loops);
22 
23 static bool counters_frozen = true;
24 
25 static int do_count_loop(struct event *event, uint64_t instructions,
26 			 uint64_t overhead, bool report)
27 {
28 	int64_t difference, expected;
29 	double percentage;
30 
31 	clear_ebb_stats();
32 
33 	counters_frozen = false;
34 	mb();
35 	mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
36 
37 	thirty_two_instruction_loop(instructions >> 5);
38 
39 	counters_frozen = true;
40 	mb();
41 	mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) | MMCR0_FC);
42 
43 	count_pmc(4, sample_period);
44 
45 	event->result.value = ebb_state.stats.pmc_count[4-1];
46 	expected = instructions + overhead;
47 	difference = event->result.value - expected;
48 	percentage = (double)difference / event->result.value * 100;
49 
50 	if (report) {
51 		printf("Looped for %lu instructions, overhead %lu\n", instructions, overhead);
52 		printf("Expected %lu\n", expected);
53 		printf("Actual   %llu\n", event->result.value);
54 		printf("Error    %ld, %f%%\n", difference, percentage);
55 		printf("Took %d EBBs\n", ebb_state.stats.ebb_count);
56 	}
57 
58 	if (difference < 0)
59 		difference = -difference;
60 
61 	/* Tolerate a difference of up to 0.0001 % */
62 	difference *= 10000 * 100;
63 	if (difference / event->result.value)
64 		return -1;
65 
66 	return 0;
67 }
68 
69 /* Count how many instructions it takes to do a null loop */
70 static uint64_t determine_overhead(struct event *event)
71 {
72 	uint64_t current, overhead;
73 	int i;
74 
75 	do_count_loop(event, 0, 0, false);
76 	overhead = event->result.value;
77 
78 	for (i = 0; i < 100; i++) {
79 		do_count_loop(event, 0, 0, false);
80 		current = event->result.value;
81 		if (current < overhead) {
82 			printf("Replacing overhead %lu with %lu\n", overhead, current);
83 			overhead = current;
84 		}
85 	}
86 
87 	return overhead;
88 }
89 
90 static void pmc4_ebb_callee(void)
91 {
92 	uint64_t val;
93 
94 	val = mfspr(SPRN_BESCR);
95 	if (!(val & BESCR_PMEO)) {
96 		ebb_state.stats.spurious++;
97 		goto out;
98 	}
99 
100 	ebb_state.stats.ebb_count++;
101 	count_pmc(4, sample_period);
102 out:
103 	if (counters_frozen)
104 		reset_ebb_with_clear_mask(MMCR0_PMAO);
105 	else
106 		reset_ebb();
107 }
108 
109 int instruction_count(void)
110 {
111 	struct event event;
112 	uint64_t overhead;
113 
114 	event_init_named(&event, 0x400FA, "PM_RUN_INST_CMPL");
115 	event_leader_ebb_init(&event);
116 	event.attr.exclude_kernel = 1;
117 	event.attr.exclude_hv = 1;
118 	event.attr.exclude_idle = 1;
119 
120 	FAIL_IF(event_open(&event));
121 	FAIL_IF(ebb_event_enable(&event));
122 
123 	sample_period = COUNTER_OVERFLOW;
124 
125 	setup_ebb_handler(pmc4_ebb_callee);
126 	mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
127 	ebb_global_enable();
128 
129 	overhead = determine_overhead(&event);
130 	printf("Overhead of null loop: %lu instructions\n", overhead);
131 
132 	/* Run for 1M instructions */
133 	FAIL_IF(do_count_loop(&event, 0x100000, overhead, true));
134 
135 	/* Run for 10M instructions */
136 	FAIL_IF(do_count_loop(&event, 0xa00000, overhead, true));
137 
138 	/* Run for 100M instructions */
139 	FAIL_IF(do_count_loop(&event, 0x6400000, overhead, true));
140 
141 	/* Run for 1G instructions */
142 	FAIL_IF(do_count_loop(&event, 0x40000000, overhead, true));
143 
144 	/* Run for 16G instructions */
145 	FAIL_IF(do_count_loop(&event, 0x400000000, overhead, true));
146 
147 	/* Run for 64G instructions */
148 	FAIL_IF(do_count_loop(&event, 0x1000000000, overhead, true));
149 
150 	/* Run for 128G instructions */
151 	FAIL_IF(do_count_loop(&event, 0x2000000000, overhead, true));
152 
153 	ebb_global_disable();
154 	event_close(&event);
155 
156 	printf("Finished OK\n");
157 
158 	return 0;
159 }
160 
161 int main(void)
162 {
163 	return test_harness(instruction_count, "instruction_count");
164 }
165