Skip to content

Commit fda7d10

Browse files
committed
Add NativeAccessor#setAffinity
1 parent 3dbc2e8 commit fda7d10

7 files changed

Lines changed: 42 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ set(JAVA_AWT_LIBRARY "")
66
set(JAVA_AWT_INCLUDE_PATH "")
77
find_package(JNI)
88

9+
find_package(Threads REQUIRED)
10+
if(THREADS_HAVE_PTHREAD_ARG)
11+
target_compile_options(nativeutil PUBLIC "-pthread")
12+
endif()
13+
if(CMAKE_THREAD_LIBS_INIT)
14+
target_link_libraries(nativeutil "${CMAKE_THREAD_LIBS_INIT}")
15+
endif()
16+
917
include_directories(.)
1018
if (JNI_FOUND)
1119
include_directories("${JNI_INCLUDE_DIRS}")

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.blueberrymc</groupId>
88
<artifactId>native-util</artifactId>
9-
<version>2.1.2</version>
9+
<version>2.2.0-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>native-util</name>

src/main/c/native_util.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include "common_tools.cpp"
33
#include <cstring>
44
#include <string>
5+
#include <jni.h>
6+
#include <pthread.h>
7+
#include <sched.h>
8+
#include <stdio.h>
59

610
#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
711

@@ -812,4 +816,19 @@ JNIEXPORT jlong JNICALL Java_net_blueberrymc_nativeutil_NativeAccessor_memset
812816
return addr_to_java(memset(addr_from_java(address), value, size));
813817
}
814818

819+
JNIEXPORT void JNICALL Java_net_blueberrymc_nativeutil_NativeAccessor_setAffinity
820+
(JNIEnv *, jclass, jint threadId, jint cpuId) {
821+
#if !__APPLE__
822+
cpu_set_t cpuset;
823+
CPU_ZERO(&cpuset);
824+
CPU_SET(cpuId, &cpuset);
825+
826+
pthread_t thread = (pthread_t) threadId;
827+
int result = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
828+
if (result != 0) {
829+
perror("pthread_setaffinity_np failed");
830+
}
831+
#endif
832+
}
833+
815834
}

src/main/c/native_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,9 @@ JNIEXPORT void JNICALL Java_net_blueberrymc_nativeutil_NativeAccessor_free
553553
JNIEXPORT jlong JNICALL Java_net_blueberrymc_nativeutil_NativeAccessor_memset
554554
(JNIEnv *, jclass, jlong, jint, jint);
555555

556+
JNIEXPORT void JNICALL Java_net_blueberrymc_nativeutil_NativeAccessor_setAffinity
557+
(JNIEnv *, jclass, jint, jint);
558+
556559
#ifdef __cplusplus
557560
}
558561
#endif

src/main/java/net/blueberrymc/nativeutil/NativeAccessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,6 @@ public class NativeAccessor {
182182

183183
@NativeType("void *")
184184
public static native long memset(@NativeType("void *") long address, @NativeType("int") int value, @NativeType("size_t") int size);
185+
186+
public static native void setAffinity(int threadId, int cpuId);
185187
}

src/main/java/net/blueberrymc/nativeutil/NativeUtil.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,4 +1126,13 @@ public static void free(long address) {
11261126
public static long memset(long address, int value, int size) {
11271127
return NativeAccessor.memset(address, value, size);
11281128
}
1129+
1130+
/**
1131+
* Sets the CPU affinity of the specified thread. This method only works on Linux.
1132+
* @param threadId the thread id
1133+
* @param cpuId the cpu id
1134+
*/
1135+
public static void setAffinity(int threadId, int cpuId) {
1136+
NativeAccessor.setAffinity(threadId, cpuId);
1137+
}
11291138
}

src/main/resources/libnativeutil.dylib

100644100755
128 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)