1private int getNumberOfCPUCores() {
2 OSValidator osValidator = new OSValidator();
3 String command = "";
4 if(osValidator.isMac()){
5 command = "sysctl -n machdep.cpu.core_count";
6 }else if(osValidator.isUnix()){
7 command = "lscpu";
8 }else if(osValidator.isWindows()){
9 command = "cmd /C WMIC CPU Get /Format:List";
10 }
11 Process process = null;
12 int numberOfCores = 0;
13 int sockets = 0;
14 try {
15 if(osValidator.isMac()){
16 String[] cmd = { "/bin/sh", "-c", command};
17 process = Runtime.getRuntime().exec(cmd);
18 }else{
19 process = Runtime.getRuntime().exec(command);
20 }
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
24
25 BufferedReader reader = new BufferedReader(
26 new InputStreamReader(process.getInputStream()));
27 String line;
28
29 try {
30 while ((line = reader.readLine()) != null) {
31 if(osValidator.isMac()){
32 numberOfCores = line.length() > 0 ? Integer.parseInt(line) : 0;
33 }else if (osValidator.isUnix()) {
34 if (line.contains("Core(s) per socket:")) {
35 numberOfCores = Integer.parseInt(line.split("\\s+")[line.split("\\s+").length - 1]);
36 }
37 if(line.contains("Socket(s):")){
38 sockets = Integer.parseInt(line.split("\\s+")[line.split("\\s+").length - 1]);
39 }
40 } else if (osValidator.isWindows()) {
41 if (line.contains("NumberOfCores")) {
42 numberOfCores = Integer.parseInt(line.split("=")[1]);
43 }
44 }
45 }
46 } catch (IOException e) {
47 e.printStackTrace();
48 }
49 if(osValidator.isUnix()){
50 return numberOfCores * sockets;
51 }
52 return numberOfCores;
53}
54