~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/powerpc/pmu/ebb/instruction_count_test.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * Copyright 2014, Michael Ellerman, IBM Corp.
  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("Delta    %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         SKIP_IF(!ebb_is_supported());
115 
116         event_init_named(&event, 0x400FA, "PM_RUN_INST_CMPL");
117         event_leader_ebb_init(&event);
118         event.attr.exclude_kernel = 1;
119         event.attr.exclude_hv = 1;
120         event.attr.exclude_idle = 1;
121 
122         FAIL_IF(event_open(&event));
123         FAIL_IF(ebb_event_enable(&event));
124 
125         sample_period = COUNTER_OVERFLOW;
126 
127         setup_ebb_handler(pmc4_ebb_callee);
128         mtspr(SPRN_MMCR0, mfspr(SPRN_MMCR0) & ~MMCR0_FC);
129         ebb_global_enable();
130 
131         overhead = determine_overhead(&event);
132         printf("Overhead of null loop: %lu instructions\n", overhead);
133 
134         /* Run for 1M instructions */
135         FAIL_IF(do_count_loop(&event, 0x100000, overhead, true));
136 
137         /* Run for 10M instructions */
138         FAIL_IF(do_count_loop(&event, 0xa00000, overhead, true));
139 
140         /* Run for 100M instructions */
141         FAIL_IF(do_count_loop(&event, 0x6400000, overhead, true));
142 
143         /* Run for 1G instructions */
144         FAIL_IF(do_count_loop(&event, 0x40000000, overhead, true));
145 
146         /* Run for 16G instructions */
147         FAIL_IF(do_count_loop(&event, 0x400000000, overhead, true));
148 
149         /* Run for 64G instructions */
150         FAIL_IF(do_count_loop(&event, 0x1000000000, overhead, true));
151 
152         /* Run for 128G instructions */
153         FAIL_IF(do_count_loop(&event, 0x2000000000, overhead, true));
154 
155         ebb_global_disable();
156         event_close(&event);
157 
158         printf("Finished OK\n");
159 
160         return 0;
161 }
162 
163 int main(void)
164 {
165         test_harness_set_timeout(300);
166         return test_harness(instruction_count, "instruction_count");
167 }
168 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php