From a1cde7086010ce181a815f2d9c3f110e486c4ed5 Mon Sep 17 00:00:00 2001
From: "Phong X. Nguyen"
Date: Tue, 5 Dec 2023 20:24:20 +0000
Subject: [PATCH 1/2] Add aarch64 support
---
Makefile.base | 2 +-
include/mdbm_log.h | 8 ++++++--
src/lib/atomic.h | 10 +++++++++-
src/lib/log.c | 24 ++++++++++++------------
src/lib/mdbm.c | 10 ++++++++++
src/lib/mdbm_lock.cc | 8 ++++----
6 files changed, 42 insertions(+), 20 deletions(-)
diff --git a/Makefile.base b/Makefile.base
index 7bb6f1d..61a0601 100644
--- a/Makefile.base
+++ b/Makefile.base
@@ -161,7 +161,7 @@ GCC_VER_INT=$(shell printf "%d%04d%04d" $(GCC_VER_SPC))
# compare to minimum version 4.8.0
GCC_GTE_48=$(shell expr $(GCC_VER_INT) \>= 400080000)
-PEDANTIC = -Wall -pedantic -Wno-variadic-macros -Wno-long-long -Wno-overlength-strings
+PEDANTIC = -Wall -pedantic -Wno-variadic-macros -Wno-long-long -Wno-overlength-strings -Wno-error=deprecated-declarations -Wno-error=stringop-truncation -Wno-error=format-truncation -Wno-error=stringop-overflow
# For clang, we need to ignore c99 extensions warnings it seems ...
ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1)
diff --git a/include/mdbm_log.h b/include/mdbm_log.h
index 58d8c83..db3b1bc 100644
--- a/include/mdbm_log.h
+++ b/include/mdbm_log.h
@@ -14,6 +14,10 @@
extern "C" {
#endif
+typedef struct va_list_s {
+ va_list ap;
+} va_list_t;
+
/*============================================================*/
/* Core logging functions. */
@@ -23,14 +27,14 @@ extern "C" {
int mdbm_log_at (const char* file, int line, int level, const char* format, ...)
__attribute__ ((format (printf,4,5)))
__attribute__ ((visibility ("default")));
-int mdbm_log_vlog_at (const char* file, int line, int level, const char* format, va_list args)
+int mdbm_log_vlog_at (const char* file, int line, int level, const char* format, va_list_t* args)
__attribute__ ((format (printf,4,0)))
__attribute__ ((visibility ("default")));
int mdbm_logerror_at (const char* file, int line, int level, int error, const char* format, ...)
__attribute__ ((format (printf,5,6)))
__attribute__ ((visibility ("default")));
-int mdbm_log_vlogerror_at (const char* file, int line, int level, int error, const char* format, va_list args)
+int mdbm_log_vlogerror_at (const char* file, int line, int level, int error, const char* format, va_list_t* args)
__attribute__ ((format (printf,5,0)))
__attribute__ ((visibility ("default")));
diff --git a/src/lib/atomic.h b/src/lib/atomic.h
index 48500d5..744adf8 100644
--- a/src/lib/atomic.h
+++ b/src/lib/atomic.h
@@ -78,16 +78,23 @@ static inline void atomic_barrier() {
static inline void atomic_read_barrier() {
#ifdef __x86_64__
__asm__ __volatile__ ("lfence" : : : "memory");
+#elif defined(__aarch64__)
+ __asm__ volatile ("dmb ishld" : : : "memory");
#else
__asm__ __volatile__ ("lock addl $0,0(%%esp)" : : : "memory");
#endif
}
static inline void atomic_pause() {
- __asm__ __volatile__ ("pause");
+#ifdef __x86_64__
+ __asm__ __volatile__ ("pause");
+#elif defined(__aarch64__)
+ __asm__ __volatile__ ("isb");
+#endif
}
+#ifndef SYS_gettid
/* returns (linux-specific) thread-id (for single-thread processes it's just PID) */
static inline uint32_t gettid() {
/* AUTO_TSC("gettid()"); */
@@ -107,6 +114,7 @@ static inline uint32_t gettid() {
return tid;
#endif
}
+#endif
#ifdef __cplusplus
}
diff --git a/src/lib/log.c b/src/lib/log.c
index 80591e5..a1fa4bd 100644
--- a/src/lib/log.c
+++ b/src/lib/log.c
@@ -105,20 +105,20 @@ size_t mdbm_strlcpy (char* dst, const char* src, size_t dstlen) {
int mdbm_log_at (const char* file, int line, int level, const char* format, ...) {
int ret;
- va_list args;
- va_start(args,format);
- ret = mdbm_log_vlog_at(file, line, level, format, args);
- va_end(args);
+ va_list_t args;
+ va_start(args.ap,format);
+ ret = mdbm_log_vlog_at(file, line, level, format, &args);
+ va_end(args.ap);
return ret;
}
#define MESSAGE_MAX 4096
-int mdbm_log_vlogerror_at (const char* file, int line, int level, int error, const char* format, va_list args) {
+int mdbm_log_vlogerror_at (const char* file, int line, int level, int error, const char* format, va_list_t* args) {
char buf[MESSAGE_MAX];
int len;
- len = vsnprintf(buf,sizeof(buf)-2,format,args);
+ len = vsnprintf(buf,sizeof(buf)-2,format,args->ap);
strcpy(buf+len,": ");
len += 2;
if (!error) {
@@ -133,14 +133,14 @@ int mdbm_log_vlogerror_at (const char* file, int line, int level, int error, con
int mdbm_logerror_at (const char* file, int line, int level, int error, const char* format, ...) {
int ret;
- va_list args;
- va_start(args,format);
- ret = mdbm_log_vlogerror_at(file, line, level,error,format,args);
- va_end(args);
+ va_list_t args;
+ va_start(args.ap,format);
+ ret = mdbm_log_vlogerror_at(file, line, level,error,format,&args);
+ va_end(args.ap);
return ret;
}
-int mdbm_log_vlog_at (const char* file, int line, int level, const char* format, va_list args) {
+int mdbm_log_vlog_at (const char* file, int line, int level, const char* format, va_list_t* args) {
int pid;
struct timeval tv;
char buf[MESSAGE_MAX];
@@ -171,7 +171,7 @@ int mdbm_log_vlog_at (const char* file, int line, int level, const char* format,
}
if (args) {
- vsnprintf(buf+offset,sizeof(buf)-offset-2,format,args);
+ vsnprintf(buf+offset,sizeof(buf)-offset-2,format,args->ap);
} else {
mdbm_strlcpy(buf+offset,format,sizeof(buf)-offset-2);
}
diff --git a/src/lib/mdbm.c b/src/lib/mdbm.c
index 05ac281..662da7e 100644
--- a/src/lib/mdbm.c
+++ b/src/lib/mdbm.c
@@ -143,6 +143,7 @@ volatile static uint64_t hi_tsc; /* Keeps track of high water mark TSC value
volatile static uint64_t next_gtod_tsc; /* timestamp, in units of TSC, when to re-read gtod */
volatile static uint64_t tsc_per_usec; /* TSC clock cycles per microsecond */
+#ifdef __x86_64__
/* Intel (and later model AMD) Fetch Time-StampCounter
* WANRING: This value may be affected by speedstep and may vary randomly across cores. */
static inline uint64_t rdtsc(void)
@@ -153,6 +154,15 @@ static inline uint64_t rdtsc(void)
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return (((uint64_t)hi) << 32) | lo;
}
+#elif defined(__aarch64__)
+/* ARM64 */
+__inline__ uint64_t rdtsc(void)
+{
+ uint64_t val;
+ __asm__ __volatile__ ("mrs %0, cntvct_el0" : "=r" (val));
+ return val;
+}
+#endif
uint64_t
tsc_get_usec(void)
diff --git a/src/lib/mdbm_lock.cc b/src/lib/mdbm_lock.cc
index ca6bb9f..9200669 100644
--- a/src/lib/mdbm_lock.cc
+++ b/src/lib/mdbm_lock.cc
@@ -198,12 +198,12 @@ lock_error(MDBM* db, const char* what, ...)
const char* fname = db ? db->db_filename : NULL;
int flen = fname ? strlen(fname) : 0;
int len = strlen(what) + flen + 4;
- va_list args;
+ va_list_t args;
char *fmtbuf = (char*)malloc(len);
snprintf(fmtbuf,len,"%s: %s",fname,what);
- va_start(args,what);
- mdbm_log_vlogerror(LOG_ERR,0,fmtbuf,args);
- va_end(args);
+ va_start(args.ap,what);
+ mdbm_log_vlogerror(LOG_ERR,0,fmtbuf,&args);
+ va_end(args.ap);
if (locks) {
locks->printState();
}
From f87952706965959c0f7fc7dcd077326244a943f0 Mon Sep 17 00:00:00 2001
From: "Phong X. Nguyen"
Date: Mon, 4 Aug 2025 22:47:12 +0000
Subject: [PATCH 2/2] update rdtsc in util.hh
---
src/lib/util.hh | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/src/lib/util.hh b/src/lib/util.hh
index 1c765d6..cd123fa 100644
--- a/src/lib/util.hh
+++ b/src/lib/util.hh
@@ -26,6 +26,8 @@ extern "C" {
// Intel (and later model AMD) Fetch Time-StampCounter
// WARNING: This value may be affected by speedstep and may vary randomly across cores.
__inline__ uint64_t rdtsc(void) {
+#ifdef __x86_64__
+
uint32_t lo, hi;
__asm__ __volatile__ ( // serialize
"xorl %%eax,%%eax \n cpuid"
@@ -33,6 +35,11 @@ extern "C" {
/* We cannot use "=A", since this would use %rax on x86_64 and return only the lower 32bits of the TSC */
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return (uint64_t)hi << 32 | lo;
+#elif defined(__aarch64__)
+ uint64_t val;
+ __asm __volatile ("mrs %0, cntvct_el0" : "=r" (val));
+ return val;
+#endif
}
}
@@ -91,7 +98,7 @@ class AutoTSC {
public:
uint64_t start;
const char *label;
- AccumTSCRecord *rec;
+ AccumTSCRecord *rec;
AutoTSC(const char* lbl, AccumTSCRecord* rec) : rec(rec) {
label = lbl;
start = rdtsc();
@@ -121,8 +128,8 @@ public:
#define prefetch(x) __builtin_prefetch(x)
-//#define CHECKPOINTV(msg, ...) fprintf(stderr, "%d %s:%d::%s() "msg"\n", getpid(), __FILE__, __LINE__, __PRETTY_FUNCTION__,__VA_ARGS__);
-#define CHECKPOINTV(msg, ...)
+//#define CHECKPOINTV(msg, ...) fprintf(stderr, "%d %s:%d::%s() "msg"\n", getpid(), __FILE__, __LINE__, __PRETTY_FUNCTION__,__VA_ARGS__);
+#define CHECKPOINTV(msg, ...)
//#define TRACE_LOCKS
#define PRINT_LOCK_TRACE(msg) { \