Skip to content

Commit 128f8ed

Browse files
committed
Bind threads to cores
1 parent 4a5961d commit 128f8ed

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/main/java/me/cortex/voxy/common/thread/ServiceThreadPool.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import me.cortex.voxy.common.Logger;
44
import me.cortex.voxy.common.util.Pair;
55
import me.cortex.voxy.common.util.ThreadUtils;
6+
import me.cortex.voxy.common.util.cpu.CpuLayout;
67

78
import java.lang.invoke.VarHandle;
89
import java.lang.management.ManagementFactory;
@@ -32,11 +33,25 @@ public ServiceThreadPool(int threadCount) {
3233
}
3334

3435
public ServiceThreadPool(int threadCount, int priority) {
36+
if (CpuLayout.CORES.length-2 < threadCount) {
37+
Logger.warn("The thread count over core count -2, performance degradation possible");
38+
}
39+
3540
this.threadGroup = new ThreadGroup("Service job workers");
3641
this.workers = new Thread[threadCount];
3742
for (int i = 0; i < threadCount; i++) {
3843
int threadId = i;
39-
var worker = new Thread(this.threadGroup, ()->this.worker(threadId));
44+
var worker = new Thread(this.threadGroup, ()->{
45+
if (CpuLayout.CORES.length>3) {
46+
//Set worker affinity if possible
47+
CpuLayout.setThreadAffinity(CpuLayout.CORES[2 + (threadId % (CpuLayout.CORES.length - 2))]);
48+
}
49+
50+
ThreadUtils.SetSelfThreadPriorityWin32(ThreadUtils.WIN32_THREAD_PRIORITY_LOWEST);
51+
//ThreadUtils.SetSelfThreadPriorityWin32(ThreadUtils.WIN32_THREAD_MODE_BACKGROUND_BEGIN);
52+
53+
this.worker(threadId);
54+
});
4055
worker.setDaemon(false);
4156
worker.setName("Service worker #" + i);
4257
worker.setPriority(priority);
@@ -133,9 +148,6 @@ void steal(ServiceSlice service, int count) {
133148
}
134149

135150
private void worker(int threadId) {
136-
ThreadUtils.SetSelfThreadPriorityWin32(ThreadUtils.WIN32_THREAD_PRIORITY_LOWEST);
137-
//ThreadUtils.SetSelfThreadPriorityWin32(ThreadUtils.WIN32_THREAD_MODE_BACKGROUND_BEGIN);
138-
139151
long[] seed = new long[]{1234342^(threadId*124987198651981L+215987981111L)};
140152
int[] revolvingSelector = new int[1];
141153
long[] logIO = new long[] {0, System.currentTimeMillis()};

src/main/java/me/cortex/voxy/common/util/cpu/CpuLayout.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ private static Core[] generateCoreLayoutWindows() {
6464
}
6565
res[i++] = new Core((!allSameClass)&&eclz==0, new Affinity(msk, core.groupMask[0].group));
6666
}
67+
sort(res);
6768
return res;
6869
}
6970

@@ -94,10 +95,25 @@ private static Core[] generateCoreLayoutLinux() {
9495
}
9596
cores[i++] = new Core(core.getEfficiency()==0&&!allSameEfficiency, aff);
9697
}
97-
98+
sort(cores);
9899
return cores;
99100
}
100101

102+
103+
private static void sort(Core[] cores) {
104+
Arrays.sort(cores, (a,b)->{
105+
if (a.isEfficiency == b.isEfficiency) {
106+
int c = Short.compareUnsigned(a.affinity.group, b.affinity.group);
107+
if (c==0) {
108+
return Long.compareUnsigned(a.affinity.msk, b.affinity.msk);
109+
}
110+
return c;
111+
} else {
112+
return a.isEfficiency?1:-1;
113+
}
114+
});
115+
}
116+
101117
public record Affinity(long msk, short group) {}
102118
public record Core(boolean isEfficiency, Affinity affinity) {
103119

@@ -115,6 +131,7 @@ public record Core(boolean isEfficiency, Affinity affinity) {
115131
}
116132

117133
public static void main(String[] args) throws InterruptedException {
134+
System.err.println(Arrays.toString(CORES));
118135
setThreadAffinity(CORES[0], CORES[1]);
119136
for (int i = 0; i < 20; i++) {
120137
int finalI = i;

0 commit comments

Comments
 (0)