|
3 | 3 | import me.cortex.voxy.common.Logger; |
4 | 4 | import me.cortex.voxy.common.util.Pair; |
5 | 5 | import me.cortex.voxy.common.util.ThreadUtils; |
| 6 | +import me.cortex.voxy.common.util.cpu.CpuLayout; |
6 | 7 |
|
7 | 8 | import java.lang.invoke.VarHandle; |
8 | 9 | import java.lang.management.ManagementFactory; |
@@ -32,11 +33,25 @@ public ServiceThreadPool(int threadCount) { |
32 | 33 | } |
33 | 34 |
|
34 | 35 | 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 | + |
35 | 40 | this.threadGroup = new ThreadGroup("Service job workers"); |
36 | 41 | this.workers = new Thread[threadCount]; |
37 | 42 | for (int i = 0; i < threadCount; i++) { |
38 | 43 | 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 | + }); |
40 | 55 | worker.setDaemon(false); |
41 | 56 | worker.setName("Service worker #" + i); |
42 | 57 | worker.setPriority(priority); |
@@ -133,9 +148,6 @@ void steal(ServiceSlice service, int count) { |
133 | 148 | } |
134 | 149 |
|
135 | 150 | private void worker(int threadId) { |
136 | | - ThreadUtils.SetSelfThreadPriorityWin32(ThreadUtils.WIN32_THREAD_PRIORITY_LOWEST); |
137 | | - //ThreadUtils.SetSelfThreadPriorityWin32(ThreadUtils.WIN32_THREAD_MODE_BACKGROUND_BEGIN); |
138 | | - |
139 | 151 | long[] seed = new long[]{1234342^(threadId*124987198651981L+215987981111L)}; |
140 | 152 | int[] revolvingSelector = new int[1]; |
141 | 153 | long[] logIO = new long[] {0, System.currentTimeMillis()}; |
|
0 commit comments