Use core affinity on windows to improve performance
This commit is contained in:
committed by
Laurent Clouet
parent
32540e7712
commit
be3a30facc
@@ -221,6 +221,7 @@ public class Job {
|
|||||||
Map<String, String> new_env = new HashMap<String, String>();
|
Map<String, String> new_env = new HashMap<String, String>();
|
||||||
|
|
||||||
new_env.put("BLENDER_USER_CONFIG", config.workingDirectory.getAbsolutePath().replace("\\", "\\\\"));
|
new_env.put("BLENDER_USER_CONFIG", config.workingDirectory.getAbsolutePath().replace("\\", "\\\\"));
|
||||||
|
new_env.put("CORES", Integer.toString(config.getNbCores()));
|
||||||
|
|
||||||
for (String arg : command1) {
|
for (String arg : command1) {
|
||||||
switch (arg) {
|
switch (arg) {
|
||||||
|
|||||||
@@ -133,6 +133,10 @@ public class Windows extends OS {
|
|||||||
Process p = builder.start();
|
Process p = builder.start();
|
||||||
WinProcess wproc = new WinProcess(p);
|
WinProcess wproc = new WinProcess(p);
|
||||||
wproc.setPriority(WinProcess.PRIORITY_BELOW_NORMAL);
|
wproc.setPriority(WinProcess.PRIORITY_BELOW_NORMAL);
|
||||||
|
if (env != null) {
|
||||||
|
String cores = env.get("CORES");
|
||||||
|
wproc.setAffinity(Integer.parseInt(cores));
|
||||||
|
}
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import com.sun.jna.Library;
|
|||||||
import com.sun.jna.Pointer;
|
import com.sun.jna.Pointer;
|
||||||
import com.sun.jna.Structure;
|
import com.sun.jna.Structure;
|
||||||
import com.sun.jna.platform.win32.BaseTSD;
|
import com.sun.jna.platform.win32.BaseTSD;
|
||||||
|
import com.sun.jna.platform.win32.BaseTSD.DWORD_PTR;
|
||||||
import com.sun.jna.platform.win32.WinDef;
|
import com.sun.jna.platform.win32.WinDef;
|
||||||
import com.sun.jna.platform.win32.WinDef.DWORD;
|
import com.sun.jna.platform.win32.WinDef.DWORD;
|
||||||
import com.sun.jna.platform.win32.WinNT;
|
import com.sun.jna.platform.win32.WinNT;
|
||||||
@@ -211,4 +212,13 @@ public interface Kernel32Lib extends Library {
|
|||||||
*/
|
*/
|
||||||
public int SetErrorMode(DWORD uMode);
|
public int SetErrorMode(DWORD uMode);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See : https://msdn.microsoft.com/en-us/library/windows/desktop/ms686247%28v=vs.85%29.aspx
|
||||||
|
*
|
||||||
|
* @param hThread
|
||||||
|
* @param dwThreadAffinityMask
|
||||||
|
* @return If the function succeeds, the return value is the thread's previous affinity mask.
|
||||||
|
*/
|
||||||
|
public boolean SetProcessAffinityMask(HANDLE hProcess, DWORD_PTR dwProcessAffinityMask);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import java.util.List;
|
|||||||
|
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
||||||
import com.sun.jna.Pointer;
|
import com.sun.jna.Pointer;
|
||||||
|
import com.sun.jna.platform.win32.BaseTSD.DWORD_PTR;
|
||||||
import com.sun.jna.platform.win32.Kernel32;
|
import com.sun.jna.platform.win32.Kernel32;
|
||||||
import com.sun.jna.platform.win32.Kernel32Util;
|
import com.sun.jna.platform.win32.Kernel32Util;
|
||||||
import com.sun.jna.platform.win32.WinDef.DWORD;
|
import com.sun.jna.platform.win32.WinDef.DWORD;
|
||||||
@@ -121,6 +122,20 @@ public class WinProcess {
|
|||||||
return this.kernel32lib.SetPriorityClass(this.handle, priority);
|
return this.kernel32lib.SetPriorityClass(this.handle, priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean setAffinity(int numberCores) {
|
||||||
|
// affects the process to a specific core/cpu, it will reduce the number of switch between core
|
||||||
|
// that way the machine will look "less busy" since default behavior for windows is to put 4 cores
|
||||||
|
// at 25% instead of 1 core at 100% but from the user point of view, it introduce lag.
|
||||||
|
if (numberCores > 0) {
|
||||||
|
long coreAffinity = 0;
|
||||||
|
for (int i = 0; i < numberCores; i++) {
|
||||||
|
coreAffinity |= 1L << i;
|
||||||
|
}
|
||||||
|
return this.kernel32lib.SetProcessAffinityMask(this.handle, new DWORD_PTR(coreAffinity));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private void terminate() {
|
private void terminate() {
|
||||||
Kernel32.INSTANCE.TerminateProcess(this.handle, 0);
|
Kernel32.INSTANCE.TerminateProcess(this.handle, 0);
|
||||||
Kernel32.INSTANCE.CloseHandle(this.handle); // we are sure that the parent Process object is dead
|
Kernel32.INSTANCE.CloseHandle(this.handle); // we are sure that the parent Process object is dead
|
||||||
|
|||||||
Reference in New Issue
Block a user