Skip to content

Commit 73cc564

Browse files
rogersimmonsrogersimmons
andauthored
Add rdtsc for ARM 64 and 32 bit (#129)
* Add rdtsc for ARM 64 and 32 bit * Remove link lib which isn't needed * Add macOS and Widows support for rdtsc() * Add OS detect to Makefile mac build * Add windows support Tweak macOS support * More macOS tweaks --------- Co-authored-by: rogersimmons <[email protected]>
1 parent f44ec43 commit 73cc564

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

affinity/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<goal>exec</goal>
130130
</goals>
131131
<configuration>
132-
<executable>${project.basedir}/${native.source.dir}/Makefile</executable>
132+
<executable>make</executable>
133133
<workingDirectory>${project.basedir}/${native.source.dir}</workingDirectory>
134134
</configuration>
135135
</execution>

affinity/src/main/c/Makefile

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/make -f
21
#
32
# Makefile for C code
43
#
@@ -10,6 +9,16 @@ TARGET := $(TARGET_DIR)/libCEInternals.so
109

1110
WORKING_DIR := $(TARGET_DIR)/../jni
1211

12+
JNI_OS := win32
13+
UNAME_S:= $(shell uname -s)
14+
ifeq ($(UNAME_S), Linux)
15+
JNI_OS := linux
16+
LRT := -lrt
17+
endif
18+
ifeq ($(UNAME_S), Darwin)
19+
JNI_OS := darwin
20+
endif
21+
1322
JAVA_CLASSES = software.chronicle.enterprise.internals.impl.NativeAffinity net.openhft.ticker.impl.JNIClock
1423

1524
JNI_STUBS := $(subst .,_,$(JAVA_CLASSES))
@@ -19,10 +28,10 @@ JAVA_BUILD_DIR := $(TARGET_DIR)
1928

2029
JAVA_HOME ?= /usr/java/default
2130
JAVA_LIB := $(JAVA_HOME)/jre/lib
22-
JVM_SHARED_LIBS := -L$(JAVA_LIB)/amd64/server -L$(JAVA_LIB)/i386/server -L$(JAVA_LIB)/amd64/jrockit/ -L$(JAVA_LIB)/i386/jrockit/ -L$(JAVA_LIB)/ppc64le/server -L$(JAVA_LIB)/ppc64le/jrockit/ -L$(JAVA_HOME)/lib/server
31+
JVM_SHARED_LIBS := -L"$(JAVA_LIB)/amd64/server" -L"$(JAVA_LIB)/i386/server" -L"$(JAVA_LIB)/amd64/jrockit" -L"$(JAVA_LIB)/i386/jrockit" -L"$(JAVA_LIB)/ppc64le/server" -L"$(JAVA_LIB)/ppc64le/jrockit" -L"$(JAVA_HOME)/lib/server"
2332

2433
CXX=g++
25-
INCLUDES := -I $(JAVA_HOME)/include -I $(JAVA_HOME)/include/linux -I $(WORKING_DIR)
34+
INCLUDES := -I"$(JAVA_HOME)/include" -I"$(JAVA_HOME)/include/$(JNI_OS)" -I"$(WORKING_DIR)"
2635

2736
# classpath for javah
2837
ifdef CLASSPATH
@@ -36,7 +45,7 @@ endif
3645
all: $(TARGET)
3746

3847
$(TARGET): $(JNI_SOURCES)
39-
$(CXX) -O3 -Wall -shared -fPIC $(JVM_SHARED_LIBS) -ljvm -lrt $(INCLUDES) $(JNI_SOURCES) -o $(TARGET)
48+
$(CXX) -O3 -Wall -shared -fPIC $(JVM_SHARED_LIBS) $(LRT) $(INCLUDES) $(JNI_SOURCES) -o $(TARGET)
4049

4150
clean:
4251
rm $(TARGET)

affinity/src/main/c/net_openhft_ticker_impl_JNIClock.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ unsigned long long rdtsc(){
5353
__asm__ __volatile__("mfspr %%r3, 268": "=r" (rval));
5454
return rval;
5555
}
56+
#elif defined(__aarch64__) // ARMv8-A (AArch64)
57+
#include <cstdint>
58+
inline uint64_t rdtsc() {
59+
uint64_t virtual_timer_value;
60+
asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
61+
return virtual_timer_value;
62+
}
63+
#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 7) // ARMv7-A (32-bit)
64+
#include <sys/time.h>
65+
inline uint64_t rdtsc() {
66+
struct timespec ts;
67+
clock_gettime(CLOCK_MONOTONIC, &ts);
68+
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
69+
}
70+
#elif defined(__APPLE__)
71+
#include <mach/mach_time.h>
72+
inline uint64_t rdtsc() {
73+
return mach_absolute_time();
74+
}
75+
#elif defined(_MSC_VER)
76+
#include <intrin.h>
77+
inline uint64_t rdtsc() {
78+
return __rdtsc();
79+
}
5680
#endif
5781

5882
/*

affinity/src/main/c/software_chronicle_enterprise_internals_impl_NativeAffinity.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#endif
2020

2121
#include <jni.h>
22-
#include <sched.h>
23-
#include <sys/syscall.h>
24-
#include <sys/types.h>
25-
#include <unistd.h>
26-
#include <string.h>
27-
22+
#ifdef __linux__
23+
#include <sched.h>
24+
#include <sys/syscall.h>
25+
#include <sys/types.h>
26+
#include <unistd.h>
27+
#include <string.h>
28+
#endif
29+
#include <stdexcept>
2830
#include "software_chronicle_enterprise_internals_impl_NativeAffinity.h"
2931

3032
/*
@@ -35,6 +37,7 @@
3537
JNIEXPORT jbyteArray JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getAffinity0
3638
(JNIEnv *env, jclass c)
3739
{
40+
#ifdef __linux__
3841
// The default size of the structure supports 1024 CPUs, should be enough
3942
// for now In the future we can use dynamic sets, which can support more
4043
// CPUs, given OS can handle them as well
@@ -53,6 +56,9 @@ JNIEXPORT jbyteArray JNICALL Java_software_chronicle_enterprise_internals_impl_N
5356
env->SetByteArrayRegion(ret, 0, size, bytes);
5457

5558
return ret;
59+
#else
60+
throw std::runtime_error("Not supported");
61+
#endif
5662
}
5763

5864
/*
@@ -63,6 +69,7 @@ JNIEXPORT jbyteArray JNICALL Java_software_chronicle_enterprise_internals_impl_N
6369
JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_setAffinity0
6470
(JNIEnv *env, jclass c, jbyteArray affinity)
6571
{
72+
#ifdef __linux__
6673
cpu_set_t mask;
6774
const size_t size = sizeof(mask);
6875
CPU_ZERO(&mask);
@@ -71,6 +78,9 @@ JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
7178
memcpy(&mask, bytes, size);
7279

7380
sched_setaffinity(0, size, &mask);
81+
#else
82+
throw std::runtime_error("Not supported");
83+
#endif
7484
}
7585

7686
/*
@@ -80,7 +90,12 @@ JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
8090
*/
8191
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getProcessId0
8292
(JNIEnv *env, jclass c) {
93+
#ifndef __linux__
94+
throw std::runtime_error("Not supported");
95+
#else
96+
8397
return (jint) getpid();
98+
#endif
8499
}
85100

86101
/*
@@ -90,7 +105,12 @@ JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
90105
*/
91106
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getThreadId0
92107
(JNIEnv *env, jclass c) {
108+
#ifndef __linux__
109+
throw std::runtime_error("Not supported");
110+
#else
111+
93112
return (jint) (pid_t) syscall (SYS_gettid);
113+
#endif
94114
}
95115

96116
/*
@@ -100,6 +120,11 @@ JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
100120
*/
101121
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getCpu0
102122
(JNIEnv *env, jclass c) {
123+
#ifndef __linux__
124+
throw std::runtime_error("Not supported");
125+
#else
126+
103127
return (jint) sched_getcpu();
128+
#endif
104129
}
105130

0 commit comments

Comments
 (0)