1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package oshi.software.os.linux;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import oshi.hardware.HardwareAbstractionLayer;
24 import oshi.hardware.Memory;
25 import oshi.hardware.PowerSource;
26 import oshi.hardware.Processor;
27 import oshi.software.os.OSFileStore;
28 import oshi.software.os.linux.proc.CentralProcessor;
29 import oshi.software.os.linux.proc.GlobalMemory;
30 import oshi.software.os.linux.proc.LinuxFileSystem;
31 import oshi.software.os.linux.proc.LinuxPowerSource;
32 import oshi.util.FileUtil;
33
34
35
36
37
38 public class LinuxHardwareAbstractionLayer implements HardwareAbstractionLayer {
39
40 private static final String SEPARATOR = "\\s+:\\s";
41
42 private Processor[] _processors = null;
43
44 private Memory _memory = null;
45
46 @Override
47 public Memory getMemory() {
48 if (this._memory == null) {
49 this._memory = new GlobalMemory();
50 }
51 return this._memory;
52 }
53
54 @Override
55 public Processor[] getProcessors() {
56
57 if (this._processors == null) {
58 List<Processor> processors = new ArrayList<>();
59 List<String> cpuInfo = null;
60 try {
61 cpuInfo = FileUtil.readFile("/proc/cpuinfo");
62 } catch (IOException e) {
63 System.err.println("Problem with: /proc/cpuinfo");
64 System.err.println(e.getMessage());
65 return null;
66 }
67 CentralProcessor cpu = null;
68 int numCPU = 0;
69 for (String toBeAnalyzed : cpuInfo) {
70 if (toBeAnalyzed.equals("")) {
71 if (cpu != null) {
72 processors.add(cpu);
73 }
74 cpu = null;
75 continue;
76 }
77 if (cpu == null) {
78 cpu = new CentralProcessor(numCPU++);
79 }
80 if (toBeAnalyzed.startsWith("model name\t")) {
81 cpu.setName(toBeAnalyzed.split(SEPARATOR)[1]);
82 continue;
83 }
84 if (toBeAnalyzed.startsWith("flags\t")) {
85 String[] flags = toBeAnalyzed.split(SEPARATOR)[1]
86 .split(" ");
87 boolean found = false;
88 for (String flag : flags) {
89 if (flag.equalsIgnoreCase("LM")) {
90 found = true;
91 break;
92 }
93 }
94 cpu.setCpu64(found);
95 continue;
96 }
97 if (toBeAnalyzed.startsWith("cpu family\t")) {
98 cpu.setFamily(toBeAnalyzed.split(SEPARATOR)[1]);
99 continue;
100 }
101 if (toBeAnalyzed.startsWith("model\t")) {
102 cpu.setModel(toBeAnalyzed.split(SEPARATOR)[1]);
103 continue;
104 }
105 if (toBeAnalyzed.startsWith("stepping\t")) {
106 cpu.setStepping(toBeAnalyzed.split(SEPARATOR)[1]);
107 continue;
108 }
109 if (toBeAnalyzed.startsWith("vendor_id")) {
110 cpu.setVendor(toBeAnalyzed.split(SEPARATOR)[1]);
111 continue;
112 }
113 }
114 if (cpu != null) {
115 processors.add(cpu);
116 }
117 this._processors = processors.toArray(new Processor[0]);
118 }
119
120 return this._processors;
121 }
122
123 @Override
124 public PowerSource[] getPowerSources() {
125 return LinuxPowerSource.getPowerSources();
126 }
127
128 @Override
129 public OSFileStore[] getFileStores() {
130 return LinuxFileSystem.getFileStores();
131 }
132
133 }