1
2
3
4
5 package oshi.hardware;
6
7 import static org.hamcrest.MatcherAssert.assertThat;
8 import static org.hamcrest.Matchers.both;
9 import static org.hamcrest.Matchers.either;
10 import static org.hamcrest.Matchers.emptyOrNullString;
11 import static org.hamcrest.Matchers.equalTo;
12 import static org.hamcrest.Matchers.greaterThan;
13 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
14 import static org.hamcrest.Matchers.is;
15 import static org.hamcrest.Matchers.lessThanOrEqualTo;
16 import static org.hamcrest.Matchers.not;
17 import static org.hamcrest.Matchers.notNullValue;
18
19 import org.junit.jupiter.api.BeforeAll;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.TestInstance;
22 import org.junit.jupiter.api.TestInstance.Lifecycle;
23
24 import oshi.SystemInfo;
25 import oshi.hardware.CentralProcessor.ProcessorIdentifier;
26 import oshi.hardware.CentralProcessor.TickType;
27 import oshi.util.Util;
28
29
30
31
32 @TestInstance(Lifecycle.PER_CLASS)
33 class CentralProcessorTest {
34
35 private CentralProcessor p = null;
36
37 @BeforeAll
38 void setUp() {
39 SystemInfo si = new SystemInfo();
40 this.p = si.getHardware().getProcessor();
41 }
42
43 @Test
44 void testProcessorIdentifier() {
45 ProcessorIdentifier pi = p.getProcessorIdentifier();
46 assertThat("Processor Identifier's vendor shouldn't be Null", pi.getVendor(), is(notNullValue()));
47 assertThat("Processor Identifier's vendor frequency should either be -1, or a greater than 0",
48 pi.getVendorFreq(), is(either(equalTo(-1L)).or(greaterThan(0L))));
49 assertThat("Processor Identifier's name shouldn't be Null", pi.getName(), is(notNullValue()));
50 assertThat("Processor Identifier's identifier shouldn't be Null", pi.getIdentifier(), is(notNullValue()));
51 assertThat("Processor Identifier's ID shouldn't be Null", pi.getProcessorID(), is(notNullValue()));
52 assertThat("Processor Identifier's stepping shouldn't be Null", pi.getStepping(), is(notNullValue()));
53 assertThat("Processor Identifier's model shouldn't be null", pi.getModel(), is(notNullValue()));
54 assertThat("Processor Identifier's family shouldn't be null", pi.getFamily(), is(notNullValue()));
55 assertThat("Processor Identifier's toString shouldn't be null", pi.toString(), is(notNullValue()));
56 assertThat("Processor Identifier's micro-architecture shouldn't be blank", pi.getMicroarchitecture(),
57 is(not(emptyOrNullString())));
58 }
59
60 @Test
61 void testFrequencies() {
62 long max = p.getMaxFreq();
63 long[] curr = p.getCurrentFreq();
64 assertThat("Logical processor frequency array length should be the same as its logical processor count",
65 p.getLogicalProcessorCount(), is(curr.length));
66 if (max >= 0) {
67 for (long freq : curr) {
68 assertThat("Logical processor frequency should be at most its max frequency", freq,
69 is(lessThanOrEqualTo(max)));
70 }
71 }
72 }
73
74 @Test
75 void testTicks() {
76 long[] ticks = p.getSystemCpuLoadTicks();
77 long[][] procTicks = p.getProcessorCpuLoadTicks();
78 assertThat("System should have the same amount of cpu-load-tick counters as there are TickType values",
79 TickType.values().length, is(ticks.length));
80
81 Util.sleep(500);
82
83 assertThat("System's cpu load between ticks should be inclusively between 0 and 1",
84 p.getSystemCpuLoadBetweenTicks(ticks), is(both(greaterThanOrEqualTo(0d)).and(lessThanOrEqualTo(1d))));
85 assertThat("System's load averages length for 3 elements should equal 3", p.getSystemLoadAverage(3).length,
86 is(3));
87
88 assertThat("Cpu load between ticks size should equal the tick array size", procTicks.length,
89 is(p.getProcessorCpuLoadBetweenTicks(procTicks).length));
90 for (int cpu = 0; cpu < p.getLogicalProcessorCount(); cpu++) {
91 assertThat("Cpu number " + cpu + "'s load between ticks should be inclusively between 0 and 1",
92 p.getProcessorCpuLoadBetweenTicks(procTicks)[cpu],
93 is(both(greaterThanOrEqualTo(0d)).and(lessThanOrEqualTo(1d))));
94 assertThat(
95 "Cpu number " + cpu
96 + " should have the same amount of cpu-load-tick counters as there are TickType values",
97 TickType.values().length, is(p.getProcessorCpuLoadTicks()[cpu].length));
98 }
99 }
100
101 @Test
102 void testDelayTicks() {
103 long[][] procTicks = p.getProcessorCpuLoadTicks();
104 assertThat("System's cpu load should be inclusively between 0 and 1", p.getSystemCpuLoad(500),
105 is(both(greaterThanOrEqualTo(0d)).and(lessThanOrEqualTo(1d))));
106
107 double[] procCpuLoad = p.getProcessorCpuLoad(500);
108 assertThat("Cpu load array size should equal the tick array size", procTicks.length, is(procCpuLoad.length));
109 for (int cpu = 0; cpu < p.getLogicalProcessorCount(); cpu++) {
110 assertThat("Cpu number " + cpu + "'s load should be inclusively between 0 and 1", procCpuLoad[cpu],
111 is(both(greaterThanOrEqualTo(0d)).and(lessThanOrEqualTo(1d))));
112 }
113 }
114
115 @Test
116 void testCounts() {
117 assertThat("Logical processor count should be at least as high as its physical processor count",
118 p.getLogicalProcessorCount(), is(greaterThanOrEqualTo(p.getPhysicalProcessorCount())));
119 assertThat("Physical processor count should by higher than 0", p.getPhysicalProcessorCount(),
120 is(greaterThan(0)));
121 assertThat("Physical processor count should be higher than its physical package count",
122 p.getPhysicalProcessorCount(), is(greaterThanOrEqualTo(p.getPhysicalPackageCount())));
123 assertThat("Physical package count should be higher than 0", p.getPhysicalPackageCount(), is(greaterThan(0)));
124
125 assertThat("Logical processor list size should match count", p.getLogicalProcessors().size(),
126 is(p.getLogicalProcessorCount()));
127 assertThat("Physical processor list size should match count", p.getPhysicalProcessors().size(),
128 is(p.getPhysicalProcessorCount()));
129
130 assertThat("Context switch count should be 0 or higher", p.getContextSwitches(), is(greaterThanOrEqualTo(0L)));
131 assertThat("Interrupt count should be 0 or higher", p.getInterrupts(), is(greaterThanOrEqualTo(0L)));
132 for (int lp = 0; lp < p.getLogicalProcessorCount(); lp++) {
133 assertThat("Logical processor number is negative", p.getLogicalProcessors().get(lp).getProcessorNumber(),
134 is(greaterThanOrEqualTo(0)));
135 switch (SystemInfo.getCurrentPlatform()) {
136 case WINDOWS:
137 if (p.getLogicalProcessorCount() < 64) {
138 assertThat("Processor group should be 0 for Windows systems with less than 64 logical processors",
139 p.getLogicalProcessors().get(lp).getProcessorGroup(), is(0));
140 }
141 assertThat("NUMA node number is negative", p.getLogicalProcessors().get(lp).getNumaNode(),
142 is(greaterThanOrEqualTo(0)));
143 break;
144 case LINUX:
145 assertThat("Processor group should be 0 for Linux systems",
146 p.getLogicalProcessors().get(lp).getProcessorGroup(), is(0));
147 assertThat("NUMA node number is negative", p.getLogicalProcessors().get(lp).getNumaNode(),
148 is(greaterThanOrEqualTo(0)));
149 break;
150 case MACOS:
151 assertThat("Processor group should be 0 for macOS systems",
152 p.getLogicalProcessors().get(lp).getProcessorGroup(), is(0));
153 assertThat("NUMA Node should be 0 for macOS systems", p.getLogicalProcessors().get(lp).getNumaNode(),
154 is(0));
155 break;
156 case SOLARIS:
157 assertThat("Processor group should be 0 for Solaris systems",
158 p.getLogicalProcessors().get(lp).getProcessorGroup(), is(0));
159 assertThat("NUMA node number is negative", p.getLogicalProcessors().get(lp).getNumaNode(),
160 is(greaterThanOrEqualTo(0)));
161 break;
162 case FREEBSD:
163 case AIX:
164 assertThat("Processor group should be 0 for FreeBSD or AIX systems",
165 p.getLogicalProcessors().get(lp).getProcessorGroup(), is(0));
166 assertThat("NUMA Node should be 0 for FreeBSD or AIX systems",
167 p.getLogicalProcessors().get(lp).getNumaNode(), is(0));
168 break;
169 default:
170 break;
171 }
172 }
173 }
174 }