Skip to content

Commit

Permalink
Option to use PMU for benchmarking on ARM (#1147)
Browse files Browse the repository at this point in the history
* Add CMake option and compiler flag for using ARM PMU in speed

* Enabling use of ARM PMU for benchmarking when compiler flag is enabled (#1141)

* Enabling use of ARM PMU for benchmarking when compiler flag is enabled

* Adding documentation in code on how to enable PMU

Co-authored-by: Ted Eaton <[email protected]>
  • Loading branch information
dstebila and tedeaton authored Dec 8, 2021
1 parent 8e22871 commit c97706c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ else()
set(OQS_DEBUG_BUILD OFF)
endif()

option(OQS_SPEED_USE_ARM_PMU "Use ARM Performance Monitor Unit during benchmarking" OFF)

if(WIN32)
set(CMAKE_GENERATOR_CC cl)
endif()
Expand Down
2 changes: 2 additions & 0 deletions src/oqsconfig.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#cmakedefine OQS_USE_ARM_SHA3_INSTRUCTIONS 1
#cmakedefine OQS_USE_ARM_NEON_INSTRUCTIONS 1

#cmakedefine OQS_SPEED_USE_ARM_PMU 1

#cmakedefine OQS_ENABLE_TEST_CONSTANT_TIME 1

#cmakedefine OQS_ENABLE_SHA3_xkcp_low_avx2 1
Expand Down
29 changes: 29 additions & 0 deletions tests/ds_benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ static uint64_t _bench_rdtsc(void) {
__asm__ volatile("mrc p15, 0, %0, c9, c13, 0\t\n"
: "=r"(value));
return value;
#elif defined(SPEED_USE_ARM_PMU)
/* Use the Performance Monitoring Unit */
uint64_t value;
/* Read the PMU register */
__asm__ volatile("mrs %0, PMCCNTR_EL0" : "=r" (value));
return value;
#elif defined(__s390x__)
#define USING_TIME_RATHER_THAN_CYCLES
uint64_t tod;
Expand Down Expand Up @@ -174,6 +180,20 @@ static void _bench_init_perfcounters(int32_t do_reset, int32_t enable_divider) {
/* Clear overflows */
__asm__ volatile("mcr p15, 0, %0, c9, c12, 3\t\n" ::"r"(0x8000000f));
}
#elif defined(SPEED_USE_ARM_PMU)

/* Enabling access to ARMv8's Performance Monitoring Unit
* cannot be done from user mode. A kernel module to
* enable access must be loaded. This generally will
* require superuser permissions. A module that has
* been found to work on some platforms can be found at
* https://github.com/mupq/pqax#enable-access-to-performance-counters
*/

static void _bench_init_perfcounters(void) {
__asm__ volatile("MSR PMCR_EL0, %0" ::"r"(1));
__asm__ volatile("MSR PMCNTENSET_EL0, %0" ::"r"(0x80000000));
}
#endif

#define DEFINE_TIMER_VARIABLES \
Expand All @@ -194,6 +214,15 @@ static void _bench_init_perfcounters(int32_t do_reset, int32_t enable_divider) {
_bench_time_cumulative = 0; \
_bench_time_mean = 0.0; \
_bench_time_M2 = 0.0;
#elif defined(SPEED_USE_ARM_PMU)
#define INITIALIZE_TIMER \
_bench_init_perfcounters(); \
_bench_iterations = 0; \
_bench_cycles_mean = 0.0; \
_bench_cycles_M2 = 0.0; \
_bench_time_cumulative = 0; \
_bench_time_mean = 0.0; \
_bench_time_M2 = 0.0;
#else
#define INITIALIZE_TIMER \
_bench_iterations = 0; \
Expand Down
3 changes: 3 additions & 0 deletions tests/speed_kem.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if defined(USE_RASPBERRY_PI)
#define _RASPBERRY_PI
#endif
#if defined(OQS_SPEED_USE_ARM_PMU)
#define SPEED_USE_ARM_PMU
#endif
#include "ds_benchmark.h"
#include "system_info.c"

Expand Down
3 changes: 3 additions & 0 deletions tests/speed_sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if defined(USE_RASPBERRY_PI)
#define _RASPBERRY_PI
#endif
#if defined(OQS_SPEED_USE_ARM_PMU)
#define SPEED_USE_ARM_PMU
#endif
#include "ds_benchmark.h"
#include "system_info.c"

Expand Down
3 changes: 3 additions & 0 deletions tests/test_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#if defined(USE_RASPBERRY_PI)
#define _RASPBERRY_PI
#endif
#if defined(OQS_SPEED_USE_ARM_PMU)
#define SPEED_USE_ARM_PMU
#endif
#include "ds_benchmark.h"
#include "system_info.c"

Expand Down

0 comments on commit c97706c

Please sign in to comment.