1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package oshi.software.os.mac;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import oshi.hardware.HardwareAbstractionLayer;
23 import oshi.hardware.Memory;
24 import oshi.hardware.PowerSource;
25 import oshi.hardware.Processor;
26 import oshi.software.os.OSFileStore;
27 import oshi.software.os.mac.local.CentralProcessor;
28 import oshi.software.os.mac.local.GlobalMemory;
29 import oshi.software.os.mac.local.MacFileSystem;
30 import oshi.software.os.mac.local.MacPowerSource;
31 import oshi.software.os.mac.local.SystemB;
32
33 import com.sun.jna.LastErrorException;
34 import com.sun.jna.Native;
35 import com.sun.jna.ptr.IntByReference;
36
37
38
39
40
41 public class MacHardwareAbstractionLayer implements HardwareAbstractionLayer {
42
43 private Processor[] _processors;
44
45 private Memory _memory;
46
47 private PowerSource[] _powerSources;
48
49
50
51
52
53
54 @Override
55 public Processor[] getProcessors() {
56 if (this._processors == null) {
57 int nbCPU = 1;
58 List<Processor> processors = new ArrayList<>();
59 int[] mib = { SystemB.CTL_HW, SystemB.HW_LOGICALCPU };
60 com.sun.jna.Memory pNbCPU = new com.sun.jna.Memory(SystemB.INT_SIZE);
61 if (0 != SystemB.INSTANCE.sysctl(mib, mib.length, pNbCPU,
62 new IntByReference(SystemB.INT_SIZE), null, 0))
63 throw new LastErrorException("Error code: "
64 + Native.getLastError());
65 nbCPU = pNbCPU.getInt(0);
66 for (int i = 0; i < nbCPU; i++)
67 processors.add(new CentralProcessor(i));
68
69 this._processors = processors.toArray(new Processor[0]);
70 }
71 return this._processors;
72 }
73
74
75
76
77
78
79 @Override
80 public Memory getMemory() {
81 if (this._memory == null) {
82 this._memory = new GlobalMemory();
83 }
84 return this._memory;
85 }
86
87 @Override
88 public PowerSource[] getPowerSources() {
89 if (this._powerSources == null) {
90 this._powerSources = MacPowerSource.getPowerSources();
91 }
92 return this._powerSources;
93 }
94
95 @Override
96 public OSFileStore[] getFileStores() {
97 return MacFileSystem.getFileStores();
98 }
99
100 }