Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rdtsc for ARM 64 and 32 bit #129

Merged
merged 6 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion affinity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<goal>exec</goal>
</goals>
<configuration>
<executable>${project.basedir}/${native.source.dir}/Makefile</executable>
<executable>make</executable>
<workingDirectory>${project.basedir}/${native.source.dir}</workingDirectory>
</configuration>
</execution>
Expand Down
17 changes: 13 additions & 4 deletions affinity/src/main/c/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/make -f
#
# Makefile for C code
#
Expand All @@ -10,6 +9,16 @@ TARGET := $(TARGET_DIR)/libCEInternals.so

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

JNI_OS := win32
UNAME_S:= $(shell uname -s)
ifeq ($(UNAME_S), Linux)
JNI_OS := linux
LRT := -lrt
endif
ifeq ($(UNAME_S), Darwin)
JNI_OS := darwin
endif

JAVA_CLASSES = software.chronicle.enterprise.internals.impl.NativeAffinity net.openhft.ticker.impl.JNIClock

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

JAVA_HOME ?= /usr/java/default
JAVA_LIB := $(JAVA_HOME)/jre/lib
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
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"

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

# classpath for javah
ifdef CLASSPATH
Expand All @@ -36,7 +45,7 @@ endif
all: $(TARGET)

$(TARGET): $(JNI_SOURCES)
$(CXX) -O3 -Wall -shared -fPIC $(JVM_SHARED_LIBS) -ljvm -lrt $(INCLUDES) $(JNI_SOURCES) -o $(TARGET)
$(CXX) -O3 -Wall -shared -fPIC $(JVM_SHARED_LIBS) $(LRT) $(INCLUDES) $(JNI_SOURCES) -o $(TARGET)

clean:
rm $(TARGET)
24 changes: 24 additions & 0 deletions affinity/src/main/c/net_openhft_ticker_impl_JNIClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ unsigned long long rdtsc(){
__asm__ __volatile__("mfspr %%r3, 268": "=r" (rval));
return rval;
}
#elif defined(__aarch64__) // ARMv8-A (AArch64)
#include <cstdint>
inline uint64_t rdtsc() {
uint64_t virtual_timer_value;
asm volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer_value));
return virtual_timer_value;
}
#elif defined(__ARM_ARCH) && (__ARM_ARCH >= 7) // ARMv7-A (32-bit)
#include <sys/time.h>
inline uint64_t rdtsc() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
}
#elif defined(__APPLE__)
#include <mach/mach_time.h>
inline uint64_t rdtsc() {
return mach_absolute_time();
}
#elif defined(_MSC_VER)
#include <intrin.h>
inline uint64_t rdtsc() {
return __rdtsc();
}
#endif

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
#endif

#include <jni.h>
#include <sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#ifdef __linux__
#include <sched.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#endif
#include <stdexcept>
#include "software_chronicle_enterprise_internals_impl_NativeAffinity.h"

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

return ret;
#else
throw std::runtime_error("Not supported");
#endif
}

/*
Expand All @@ -63,6 +69,7 @@ JNIEXPORT jbyteArray JNICALL Java_software_chronicle_enterprise_internals_impl_N
JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_setAffinity0
(JNIEnv *env, jclass c, jbyteArray affinity)
{
#ifdef __linux__
cpu_set_t mask;
const size_t size = sizeof(mask);
CPU_ZERO(&mask);
Expand All @@ -71,6 +78,9 @@ JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
memcpy(&mask, bytes, size);

sched_setaffinity(0, size, &mask);
#else
throw std::runtime_error("Not supported");
#endif
}

/*
Expand All @@ -80,7 +90,12 @@ JNIEXPORT void JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
*/
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getProcessId0
(JNIEnv *env, jclass c) {
#ifndef __linux__
throw std::runtime_error("Not supported");
#else

return (jint) getpid();
#endif
}

/*
Expand All @@ -90,7 +105,12 @@ JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
*/
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getThreadId0
(JNIEnv *env, jclass c) {
#ifndef __linux__
throw std::runtime_error("Not supported");
#else

return (jint) (pid_t) syscall (SYS_gettid);
#endif
}

/*
Expand All @@ -100,6 +120,11 @@ JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeA
*/
JNIEXPORT jint JNICALL Java_software_chronicle_enterprise_internals_impl_NativeAffinity_getCpu0
(JNIEnv *env, jclass c) {
#ifndef __linux__
throw std::runtime_error("Not supported");
#else

return (jint) sched_getcpu();
#endif
}