Skip to content

Commit 89e6b43

Browse files
riptlripatel-fd
authored andcommitted
fd_tickcount support on Arm via cntvct_el0
1 parent 239d78f commit 89e6b43

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

config/extra/with-arm.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ include config/extra/with-rocksdb.mk
3737
endif
3838

3939
FD_ARCH_SUPPORTS_SANDBOX:=1
40+
41+
CPPFLAGS+=-DFD_HAS_ARM=1
42+
FD_HAS_ARM:=1

src/app/firedancer/commands/shred_version.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../../../disco/topo/fd_topo.h"
55
#include "../../../discof/ipecho/fd_ipecho_client.h"
66

7+
#include <stdlib.h>
78
#include <unistd.h>
89

910
void

src/util/fd_util_base.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@
181181
#define FD_HAS_AESNI 0
182182
#endif
183183

184+
/* FD_HAS_ARM: If the build target supports armv8-a specific features
185+
and can benefit from aarch64 specific optimizations, define
186+
FD_HAS_ARM. */
187+
188+
#ifndef FD_HAS_ARM
189+
#define FD_HAS_ARM 0
190+
#endif
191+
184192
/* FD_HAS_LZ4 indicates that the target supports LZ4 compression.
185193
Roughly, does "#include <lz4.h>" and the APIs therein work? */
186194

@@ -1245,6 +1253,8 @@ fd_hash_memcpy( ulong seed,
12451253
#ifndef FD_TICKCOUNT_STYLE
12461254
#if FD_HAS_X86 /* Use RDTSC */
12471255
#define FD_TICKCOUNT_STYLE 1
1256+
#elif FD_HAS_ARM /* Use CNTVCT_EL0 */
1257+
#define FD_TICKCOUNT_STYLE 2
12481258
#else /* Use portable fallback */
12491259
#define FD_TICKCOUNT_STYLE 0
12501260
#endif
@@ -1290,6 +1300,23 @@ fd_hash_memcpy( ulong seed,
12901300

12911301
#define fd_tickcount() ((long)__builtin_ia32_rdtsc())
12921302

1303+
#elif FD_TICKCOUNT_STYLE==2 /* armv8 (fast) */
1304+
1305+
/* fd_tickcount (ARM): https://developer.arm.com/documentation/ddi0601/2021-12/AArch64-Registers/CNTVCT-EL0--Counter-timer-Virtual-Count-register
1306+
Approx 24 MHz on Apple M1. */
1307+
1308+
static inline long
1309+
fd_tickcount( void ) {
1310+
/* consider using 'isb' */
1311+
ulong value;
1312+
__asm__ __volatile__ (
1313+
"isb\n"
1314+
"mrs %0, cntvct_el0\n"
1315+
"nop"
1316+
: "=r" (value) );
1317+
return (long)value;
1318+
}
1319+
12931320
#else
12941321
#error "Unknown FD_TICKCOUNT_STYLE"
12951322
#endif

0 commit comments

Comments
 (0)