ARM PMU tests
v12 cover ARM PMU tests
Changes from v11:
* Use report_info() to report PMU HW related info (implementer, id code, ...)
* Print PMU PMCR info in the same line
Note:
1) Current KVM code has bugs in handling PMCCFILTR write. A fix (see below) is required for this unit testing code to work correctly under KVM mode.
https://lists.cs.columbia.edu/pipermail/kvmarm/2016-November/022134.html.
Thanks,
-Wei
Christopher Covington (3):
arm: Add PMU test
arm: pmu: Check cycle count increases
arm: pmu: Add CPI checking
arm/Makefile.common | 3 +- arm/pmu.c | 350 ++++++++++++++++++++++++++++++++++++++++++++++++++++ arm/unittests.cfg | 19 +++ 3 files changed, 371 insertions(+), 1 deletion(-) create mode 100644 arm/pmu.c
Signature · 1 line
1.8.3.1
PATCH v12 1/3 arm: Add PMU test
From: Christopher Covington <[email protected]>
Beginning with a simple sanity check of the control register, add a unit test for the ARM Performance Monitors Unit (PMU).
--- arm/Makefile.common | 3 ++- arm/pmu.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++ arm/unittests.cfg | 5 ++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 arm/pmu.c
arm/Makefile.common +2 -1
@@ -12,7 +12,8 @@ endif tests-common = \ $(TEST_DIR)/selftest.flat \ $(TEST_DIR)/spinlock-test.flat \ - $(TEST_DIR)/pci-test.flat + $(TEST_DIR)/pci-test.flat \ + $(TEST_DIR)/pmu.flat all: test_cases
arm/pmu.c new file +73 -0
73 lines not on this page · raw message
arm/unittests.cfg +5 -0
@@ -58,3 +58,8 @@ groups = selftest [pci-test] file = pci-test.flat groups = pci + +# Test PMU support +[pmu] +file = pmu.flat +groups = pmu
Signature · 1 line
1.8.3.1
PATCH v12 2/3 arm: pmu: Check cycle count increases
In reply to Wei HuangFrom: Christopher Covington <[email protected]>
Ensure that reads of the PMCCNTR_EL0 are monotonically increasing, even for the smallest delta of two subsequent reads.
--- arm/pmu.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+)
arm/pmu.c +156 -0
@@ -15,6 +15,9 @@#include "libcflat.h" #include "asm/barrier.h" +#define PMU_PMCR_E (1 << 0) +#define PMU_PMCR_C (1 << 2) +#define PMU_PMCR_LC (1 << 6)#define PMU_PMCR_N_SHIFT 11#define PMU_PMCR_N_MASK 0x1f#define PMU_PMCR_ID_SHIFT 16
@@ -22,6 +25,14 @@#define PMU_PMCR_IMP_SHIFT 24#define PMU_PMCR_IMP_MASK 0xff +#define ID_DFR0_PERFMON_SHIFT 24 +#define ID_DFR0_PERFMON_MASK 0xf + +#define PMU_CYCLE_IDX 31 + +#define NR_SAMPLES 10 + +static unsigned int pmu_version;#if defined(__arm__)static inline uint32_t pmcr_read (void ) {
@@ -30,6 +41,69 @@ static inline uint32_t pmcr_read(void) asmvolatile ("mrc p15, 0, %0, c9, c12, 0" :"=r" (ret));return ret; } + +static inline void pmcr_write (uint32_t value) +{ + asmvolatile ("mcr p15, 0, %0, c9, c12, 0" : :"r" (value)); +isb (); +} + +static inline void pmselr_write (uint32_t value) +{ + asmvolatile ("mcr p15, 0, %0, c9, c12, 5" : :"r" (value)); +isb (); +} + +static inline void pmxevtyper_write (uint32_t value) +{ + asmvolatile ("mcr p15, 0, %0, c9, c13, 1" : :"r" (value)); +} + +static inline uint64_t pmccntr_read (void ) +{ +uint32_t lo, hi =0 ; + +if (pmu_version ==0x3 ) + asmvolatile ("mrrc p15, 0, %0, %1, c9" :"=r" (lo),"=r" (hi)); +else + asmvolatile ("mrc p15, 0, %0, c9, c13, 0" :"=r" (lo)); + +return ((uint64_t )hi <<32 ) | lo; +} + +static inline void pmccntr_write (uint64_t value) +{ +uint32_t lo, hi; + + lo = value &0xffffffff ; + hi = (value >>32 ) &0xffffffff ; + +if (pmu_version ==0x3 ) + asmvolatile ("mcrr p15, 0, %0, %1, c9" : :"r" (lo),"r" (hi)); +else + asmvolatile ("mcr p15, 0, %0, c9, c13, 0" : :"r" (lo)); +} + +static inline void pmcntenset_write (uint32_t value) +{ + asmvolatile ("mcr p15, 0, %0, c9, c12, 1" : :"r" (value)); +} + +/* PMCCFILTR is an obsolete name for PMXEVTYPER31 in ARMv7 */ +static inline void pmccfiltr_write (uint32_t value) +{ +pmselr_write (PMU_CYCLE_IDX ); +pmxevtyper_write (value); +isb (); +} + +static inline uint32_t id_dfr0_read (void ) +{ +uint32_t val; + + asmvolatile ("mrc p15, 0, %0, c0, c1, 2" :"=r" (val)); +return val; +}#elif defined(__aarch64__)static inline uint32_t pmcr_read (void ) {
@@ -38,6 +112,44 @@ static inline uint32_t pmcr_read(void) asmvolatile ("mrs %0, pmcr_el0" :"=r" (ret));return ret; } + +static inline void pmcr_write (uint32_t value) +{ + asmvolatile ("msr pmcr_el0, %0" : :"r" (value)); +isb (); +} + +static inline uint64_t pmccntr_read (void ) +{ +uint64_t cycles; + + asmvolatile ("mrs %0, pmccntr_el0" :"=r" (cycles)); +return cycles; +} + +static inline void pmccntr_write (uint64_t value) +{ + asmvolatile ("msr pmccntr_el0, %0" : :"r" (value)); +} + +static inline void pmcntenset_write (uint32_t value) +{ + asmvolatile ("msr pmcntenset_el0, %0" : :"r" (value)); +} + +static inline void pmccfiltr_write (uint32_t value) +{ + asmvolatile ("msr pmccfiltr_el0, %0" : :"r" (value)); +isb (); +} + +static inline uint32_t id_dfr0_read (void ) +{ +uint32_t id; + + asmvolatile ("mrs %0, id_dfr0_el1" :"=r" (id)); +return id; +}#endif /*
@@ -63,11 +175,55 @@ static bool check_pmcr(void)return ((pmcr >>PMU_PMCR_IMP_SHIFT ) &PMU_PMCR_IMP_MASK ) !=0 ; } +/* +* Ensure that the cycle counter progresses between back-to-back reads. +*/ +static bool check_cycles_increase (void ) +{ +bool success = true; + +/* init before event access, this test only cares about cycle count */ +pmcntenset_write (1 <<PMU_CYCLE_IDX ); +pmccfiltr_write (0 );/* count cycles in EL0, EL1, but not EL2 */ +pmccntr_write (0 ); + +pmcr_write (pmcr_read () |PMU_PMCR_LC |PMU_PMCR_C |PMU_PMCR_E ); + +for (int i =0 ; i <NR_SAMPLES ; i++) { +uint64_t a, b; + + a =pmccntr_read (); + b =pmccntr_read (); + +if (a >= b) { +printf ("Read %" PRId64" then %" PRId64".\n" , a, b); + success = false; +break ; + } + } + +pmcr_write (pmcr_read () & ~PMU_PMCR_E ); + +return success; +} + +void pmu_init (void ) +{ +uint32_t dfr0; + +/* probe pmu version */ + dfr0 =id_dfr0_read (); + pmu_version = (dfr0 >>ID_DFR0_PERFMON_SHIFT ) &ID_DFR0_PERFMON_MASK ; +report_info ("PMU version: %d" , pmu_version); +} +int main (void ) {report_prefix_push ("pmu" ); +pmu_init ();report ("Control register" ,check_pmcr ()); +report ("Monotonically increasing cycle count" ,check_cycles_increase ());return report_summary (); }
Signature · 1 line
1.8.3.1
PATCH v12 3/3 arm: pmu: Add CPI checking
In reply to Wei HuangFrom: Christopher Covington <[email protected]>
Calculate the numbers of cycles per instruction (CPI) implied by ARM PMU cycle counter values. The code includes a strict checking facility intended for the -icount option in TCG mode in the configuration file.
--- arm/pmu.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++- arm/unittests.cfg | 14 +++++++ 2 files changed, 136 insertions(+), 1 deletion(-)
arm/pmu.c +122 -1
@@ -104,6 +104,27 @@ static inline uint32_t id_dfr0_read(void) asmvolatile ("mrc p15, 0, %0, c0, c1, 2" :"=r" (val)); return val; } + +/* +* Extra instructions inserted by the compiler would be difficult to compensate +* for, so hand assemble everything between, and including, the PMCR accesses +* to start and stop counting. isb instructions were inserted to make sure +* pmccntr read after this function returns the exact instructions executed in +* the controlled block. Total instrs = isb + mcr + 2*loop = 2 + 2*loop. +*/ +static inline void precise_instrs_loop (int loop,uint32_t pmcr) +{ + asmvolatile ( +" mcr p15, 0, %[pmcr], c9, c12, 0\n" +" isb\n" +"1: subs %[loop], %[loop], #1\n" +" bgt 1b\n" +" mcr p15, 0, %[z], c9, c12, 0\n" +" isb\n" + : [loop]"+r" (loop) + : [pmcr]"r" (pmcr), [z]"r" (0 ) + :"cc" ); +}#elif defined(__aarch64__)static inline uint32_t pmcr_read (void ) {
@@ -150,6 +171,27 @@ static inline uint32_t id_dfr0_read(void) asmvolatile ("mrs %0, id_dfr0_el1" :"=r" (id)); return id; } + +/* +* Extra instructions inserted by the compiler would be difficult to compensate +* for, so hand assemble everything between, and including, the PMCR accesses +* to start and stop counting. isb instructions are inserted to make sure +* pmccntr read after this function returns the exact instructions executed +* in the controlled block. Total instrs = isb + msr + 2*loop = 2 + 2*loop. +*/ +static inline void precise_instrs_loop (int loop,uint32_t pmcr) +{ + asmvolatile ( +" msr pmcr_el0, %[pmcr]\n" +" isb\n" +"1: subs %[loop], %[loop], #1\n" +" b.gt 1b\n" +" msr pmcr_el0, xzr\n" +" isb\n" + : [loop]"+r" (loop) + : [pmcr]"r" (pmcr) + :"cc" ); +}#endif /*
@@ -207,6 +249,79 @@ static bool check_cycles_increase(void)return success; } +/* +* Execute a known number of guest instructions. Only even instruction counts +* greater than or equal to 4 are supported by the in-line assembly code. The +* control register (PMCR_EL0) is initialized with the provided value (allowing +* for example for the cycle counter or event counters to be reset). At the end +* of the exact instruction loop, zero is written to PMCR_EL0 to disable +* counting, allowing the cycle counter or event counters to be read at the +* leisure of the calling code. +*/ +static void measure_instrs (int num,uint32_t pmcr) +{ +int loop = (num -2 ) /2 ; + +assert (num >=4 && ((num -2 ) %2 ==0 )); +precise_instrs_loop (loop, pmcr); +} + +/* +* Measure cycle counts for various known instruction counts. Ensure that the +* cycle counter progresses (similar to check_cycles_increase() but with more +* instructions and using reset and stop controls). If supplied a positive, +* nonzero CPI parameter, also strictly check that every measurement matches +* it. Strict CPI checking is used to test -icount mode. +*/ +static bool check_cpi (int cpi) +{ +uint32_t pmcr =pmcr_read () |PMU_PMCR_LC |PMU_PMCR_C |PMU_PMCR_E ; + +/* init before event access, this test only cares about cycle count */ +pmcntenset_write (1 <<PMU_CYCLE_IDX ); +pmccfiltr_write (0 );/* count cycles in EL0, EL1, but not EL2 */ + +if (cpi >0 ) +printf ("Checking for CPI=%d.\n" , cpi); +printf ("instrs : cycles0 cycles1 ...\n" ); + +for (unsigned int i =4 ; i <300 ; i +=32 ) { +uint64_t avg, sum =0 ; + +printf ("%d :" , i); +for (int j =0 ; j <NR_SAMPLES ; j++) { +uint64_t cycles; + +pmccntr_write (0 ); +measure_instrs (i, pmcr); + cycles =pmccntr_read (); +printf (" %" PRId64"" , cycles); + +if (!cycles) { +printf ("\ncycles not incrementing!\n" ); +return false; + }else if (cpi >0 && cycles != i * cpi) { +printf ("\nunexpected cycle count received!\n" ); +return false; + }else if ((cycles >>32 ) !=0 ) { +/* The cycles taken by the loop above should +* fit in 32 bits easily. We check the upper +* 32 bits of the cycle counter to make sure +* there is no supprise. */ +printf ("\ncycle count bigger than 32bit!\n" ); +return false; + } + + sum += cycles; + } + avg = sum /NR_SAMPLES ; +printf (" sum=%" PRId64" avg=%" PRId64" avg_ipc=%" PRId64" " +"avg_cpi=%" PRId64"\n" , sum, avg, i / avg, avg / i); + } + +return true; +} +void pmu_init (void ) {uint32_t dfr0;
@@ -217,13 +332,19 @@ void pmu_init(void)report_info ("PMU version: %d" , pmu_version); } -int main (void ) +int main (int argc,char *argv[]) { +int cpi =0 ; + +if (argc >1 ) + cpi =atol (argv[1 ]); +report_prefix_push ("pmu" );pmu_init ();report ("Control register" ,check_pmcr ());report ("Monotonically increasing cycle count" ,check_cycles_increase ()); +report ("Cycle/instruction ratio" ,check_cpi (cpi));return report_summary (); }
arm/unittests.cfg +14 -0
@@ -63,3 +63,17 @@ groups = pci [pmu] file = pmu.flat groups = pmu + +# Test PMU support (TCG) with -icount IPC=1 +[pmu-tcg-icount-1] +file = pmu.flat +extra_params = -icount 0 -append '1' +groups = pmu +accel = tcg + +# Test PMU support (TCG) with -icount IPC=256 +[pmu-tcg-icount-256] +file = pmu.flat +extra_params = -icount 8 -append '256' +groups = pmu +accel = tcg
Signature · 1 line
1.8.3.1