This is an automated email from the ASF dual-hosted git repository. jbarrett pushed a commit to branch wip/oshi-multios-stats-module in repository https://gitbox.apache.org/repos/asf/geode.git
commit 64e2a8c1f310dc16c5c31158863c1d726dec65a3 Author: Jacob Barrett <[email protected]> AuthorDate: Mon Nov 15 22:55:16 2021 -0800 Updated benchmark and test. --- .../OshiStatisticsBenchmark.java | 53 +++---- .../oshi/OshiStatisticsProviderImpl.java | 17 ++- .../oshi/OshiStatisticsProviderImplTest.java | 160 +++++++++++++++++++++ .../statistics/oshi/OshiStatisticsTest.java | 92 ------------ 4 files changed, 202 insertions(+), 120 deletions(-) diff --git a/geode-core/src/jmh/java/org/apache/geode/internal/statistics/platform/OshiStatisticsBenchmark.java b/geode-core/src/jmh/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsBenchmark.java similarity index 87% rename from geode-core/src/jmh/java/org/apache/geode/internal/statistics/platform/OshiStatisticsBenchmark.java rename to geode-core/src/jmh/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsBenchmark.java index ddf9fcf..a2f1746 100644 --- a/geode-core/src/jmh/java/org/apache/geode/internal/statistics/platform/OshiStatisticsBenchmark.java +++ b/geode-core/src/jmh/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsBenchmark.java @@ -13,51 +13,47 @@ * the License. */ -package org.apache.geode.internal.statistics.platform; +package org.apache.geode.internal.statistics.oshi; import java.util.concurrent.TimeUnit; import java.util.function.DoubleSupplier; import java.util.function.IntSupplier; import java.util.function.LongSupplier; +import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.Mode; import org.openjdk.jmh.annotations.OutputTimeUnit; import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; -import oshi.SystemInfo; import org.apache.geode.StatisticDescriptor; +import org.apache.geode.Statistics; import org.apache.geode.StatisticsType; import org.apache.geode.internal.statistics.SuppliableStatistics; +import org.apache.geode.internal.statistics.platform.OsStatisticsFactory; @State(Scope.Benchmark) @BenchmarkMode(Mode.SampleTime) @OutputTimeUnit(TimeUnit.MILLISECONDS) public class OshiStatisticsBenchmark { - private final int pid = new SystemInfo().getOperatingSystem().getProcessId(); - private final SuppliableStatistics noopStatistics = new NoopStatistics(); - - // @Setup - // public void setup() { - // OshiStatistics.init(); - // } - // - // @Benchmark - // public void noop() { - // - // } - // - // @Benchmark - // public void refreshProcess() { - // OshiStatistics.refreshProcess(pid, noopStatistics); - // } - // - // @Benchmark - // public void refreshSystem() { - // OshiStatistics.refreshSystem(noopStatistics); - // } + private final OshiStatisticsProviderImpl oshiStatisticsProvider = + new OshiStatisticsProviderImpl(); + + @Setup + public void setup() throws OshiStatisticsProviderException { + oshiStatisticsProvider.init(new NoopStatisticsProvider(), 0); + } + +// @Benchmark +// public void noop() {} + + @Benchmark + public void sampleProcess() { + oshiStatisticsProvider.sampleProcess(); + } private static class NoopStatistics implements SuppliableStatistics { @Override @@ -313,4 +309,13 @@ public class OshiStatisticsBenchmark { return null; } } + + private static class NoopStatisticsProvider implements OsStatisticsFactory { + @Override + public Statistics createOsStatistics(final StatisticsType type, final String textId, + final long numericId, + final int osStatFlags) { + return new NoopStatistics(); + } + } } diff --git a/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImpl.java b/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImpl.java index 8054881..71201fc 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImpl.java +++ b/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImpl.java @@ -26,7 +26,7 @@ import org.apache.geode.logging.internal.log4j.api.LogService; public class OshiStatisticsProviderImpl implements OshiStatisticsProvider { private static final Logger log = LogService.getLogger(); - final SystemInfo systemInfo = new SystemInfo(); + final SystemInfo systemInfo; private int processId; private CentralProcessor processor; @@ -43,6 +43,14 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider { private Statistics[] processorStats; private Statistics[] networkInterfaceStats; + public OshiStatisticsProviderImpl() { + this(new SystemInfo()); + } + + OshiStatisticsProviderImpl(final SystemInfo systemInfo) { + this.systemInfo = systemInfo; + } + @Override public void init(final @NotNull OsStatisticsFactory osStatisticsFactory, final long id) throws OshiStatisticsProviderException { @@ -77,10 +85,11 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider { networkInterfaceStats = new Statistics[networkIFs.size()]; for (int i = 0, size = networkIFs.size(); i < size; i++) { final NetworkIF networkIF = networkIFs.get(i); - log.info("Creating network interfaces stats for {}", networkIF.getDisplayName()); + final String displayName = networkIF.getDisplayName(); + log.info("Creating network interfaces stats for {}", displayName); networkInterfaceStats[i] = osStatisticsFactory.createOsStatistics(NetworkInterfaceStats.getType(), - networkIF.getDisplayName(), id, 0); + displayName, id, 0); } } @@ -95,7 +104,7 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider { @Override public void destroy() {} - private void sampleProcess() { + void sampleProcess() { final OSProcess process = operatingSystem.getProcess(processId); final double processCpuLoadBetweenTicks = process.getProcessCpuLoadBetweenTicks(this.process); diff --git a/geode-core/src/test/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImplTest.java b/geode-core/src/test/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImplTest.java new file mode 100644 index 0000000..c5244f3 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsProviderImplTest.java @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.geode.internal.statistics.oshi; + +import static java.util.Arrays.asList; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.Test; +import oshi.SystemInfo; +import oshi.hardware.CentralProcessor; +import oshi.hardware.HardwareAbstractionLayer; +import oshi.hardware.NetworkIF; +import oshi.software.os.OSProcess; +import oshi.software.os.OperatingSystem; + +import org.apache.geode.Statistics; +import org.apache.geode.internal.statistics.platform.OsStatisticsFactory; + +public class OshiStatisticsProviderImplTest { + + private static final String SYSTEM_IDENTITY = "mock-operating-system"; + private static final String PROCESS_IDENTITY = "mock-process"; + private static final String LOGICAL_PROCESSOR_0_IDENTITY = "mock-processor-0"; + private static final String LOGICAL_PROCESSOR_1_IDENTITY = "mock-processor-1"; + private static final String NETWORK_IF_0_DISPLAY_NAME = "mock-if0"; + private static final String NETWORK_IF_1_DISPLAY_NAME = "mock-if1"; + + private final OshiStatisticsProviderImpl oshiStatisticsProvider; + private final OSProcess osProcess; + + public OshiStatisticsProviderImplTest() { + SystemInfo systemInfo = mock(SystemInfo.class); + oshiStatisticsProvider = new OshiStatisticsProviderImpl(systemInfo); + + final OperatingSystem operatingSystem = mock(OperatingSystem.class); + when(systemInfo.getOperatingSystem()).thenReturn(operatingSystem); + + final int processId = 42; + when(operatingSystem.getProcessId()).thenReturn(processId); + + final HardwareAbstractionLayer hardwareAbstractionLayer = mock(HardwareAbstractionLayer.class); + when(systemInfo.getHardware()).thenReturn(hardwareAbstractionLayer); + + final CentralProcessor centralProcessor = mock(CentralProcessor.class); + when(hardwareAbstractionLayer.getProcessor()).thenReturn(centralProcessor); + + osProcess = mock(OSProcess.class); + when(operatingSystem.getProcess(eq(processId))).thenReturn(osProcess); + + when(osProcess.toString()).thenReturn(PROCESS_IDENTITY); + + when(operatingSystem.toString()).thenReturn(SYSTEM_IDENTITY); + + final CentralProcessor.LogicalProcessor logicalProcessor0 = + mock(CentralProcessor.LogicalProcessor.class); + when(logicalProcessor0.toString()).thenReturn(LOGICAL_PROCESSOR_0_IDENTITY); + final CentralProcessor.LogicalProcessor logicalProcessor1 = + mock(CentralProcessor.LogicalProcessor.class); + when(logicalProcessor1.toString()).thenReturn(LOGICAL_PROCESSOR_1_IDENTITY); + when(centralProcessor.getLogicalProcessors()) + .thenReturn(asList(logicalProcessor0, logicalProcessor1)); + + final NetworkIF networkIf0 = mock(NetworkIF.class); + when(networkIf0.getDisplayName()).thenReturn(NETWORK_IF_0_DISPLAY_NAME); + final NetworkIF networkIf1 = mock(NetworkIF.class); + when(networkIf1.getDisplayName()).thenReturn(NETWORK_IF_1_DISPLAY_NAME); + when(hardwareAbstractionLayer.getNetworkIFs()).thenReturn(asList(networkIf0, networkIf1)); + + } + + @Test + public void initCreatesOsStatistics() throws OshiStatisticsProviderException { + final OsStatisticsFactory osStatisticsFactory = mock(OsStatisticsFactory.class); + final long id = 13; + oshiStatisticsProvider.init(osStatisticsFactory, id); + + verify(osStatisticsFactory).createOsStatistics(eq(ProcessStats.getType()), eq(PROCESS_IDENTITY), + eq(id), + eq(0)); + verify(osStatisticsFactory).createOsStatistics(eq(OperatingSystemStats.getType()), + eq(SYSTEM_IDENTITY), + eq(id), eq(0)); + verify(osStatisticsFactory) + .createOsStatistics(eq(ProcessorStats.getType()), eq(LOGICAL_PROCESSOR_0_IDENTITY), eq(id), + eq(0)); + verify(osStatisticsFactory) + .createOsStatistics(eq(ProcessorStats.getType()), eq(LOGICAL_PROCESSOR_1_IDENTITY), eq(id), + eq(0)); + verify(osStatisticsFactory) + .createOsStatistics(eq(NetworkInterfaceStats.getType()), eq(NETWORK_IF_0_DISPLAY_NAME), + eq(id), eq(0)); + verify(osStatisticsFactory) + .createOsStatistics(eq(NetworkInterfaceStats.getType()), eq(NETWORK_IF_1_DISPLAY_NAME), + eq(id), eq(0)); + + verifyNoMoreInteractions(osStatisticsFactory); + } + + @Test + public void sampleProcessUpdatesStats() throws OshiStatisticsProviderException { + final OsStatisticsFactory osStatisticsFactory = mock(OsStatisticsFactory.class); + final Statistics statistics = mock(Statistics.class); + when(osStatisticsFactory.createOsStatistics(eq(ProcessStats.getType()), eq(PROCESS_IDENTITY), + anyLong(), + anyInt())).thenReturn(statistics); + + when(osProcess.getProcessCpuLoadBetweenTicks(any())).thenReturn(0.123D); + when(osProcess.getVirtualSize()).thenReturn(456L); + when(osProcess.getResidentSetSize()).thenReturn(789L); + when(osProcess.getThreadCount()).thenReturn(321); + when(osProcess.getKernelTime()).thenReturn(654L); + when(osProcess.getUserTime()).thenReturn(987L); + when(osProcess.getBytesRead()).thenReturn(1234L); + when(osProcess.getBytesWritten()).thenReturn(5678L); + when(osProcess.getOpenFiles()).thenReturn(9L); + when(osProcess.getProcessCpuLoadCumulative()).thenReturn(123.456D); + when(osProcess.getMinorFaults()).thenReturn(2L); + when(osProcess.getMajorFaults()).thenReturn(3L); + when(osProcess.getContextSwitches()).thenReturn(42L); + + oshiStatisticsProvider.init(osStatisticsFactory, 0); + oshiStatisticsProvider.sampleProcess(); + + verify(statistics).setDouble(eq(ProcessStats.cpuLoad), eq(0.123D)); + verify(statistics).setLong(eq(ProcessStats.virtualSize), eq(456L)); + verify(statistics).setLong(eq(ProcessStats.residentSetSize), eq(789L)); + verify(statistics).setLong(eq(ProcessStats.threadCount), eq(321L)); + verify(statistics).setLong(eq(ProcessStats.kernelTime), eq(654L)); + verify(statistics).setLong(eq(ProcessStats.userTime), eq(987L)); + verify(statistics).setLong(eq(ProcessStats.bytesRead), eq(1234L)); + verify(statistics).setLong(eq(ProcessStats.bytesWritten), eq(5678L)); + verify(statistics).setLong(eq(ProcessStats.openFiles), eq(9L)); + verify(statistics).setDouble(eq(ProcessStats.cpuLoadCumulative), eq(123.456D)); + verify(statistics).setLong(eq(ProcessStats.minorFaults), eq(2L)); + verify(statistics).setLong(eq(ProcessStats.majorFaults), eq(3L)); + verify(statistics).setLong(eq(ProcessStats.contextSwitches), eq(42L)); + + verifyNoMoreInteractions(statistics); + } +} diff --git a/geode-core/src/test/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsTest.java b/geode-core/src/test/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsTest.java deleted file mode 100644 index e87ae72..0000000 --- a/geode-core/src/test/java/org/apache/geode/internal/statistics/oshi/OshiStatisticsTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ - -package org.apache.geode.internal.statistics.oshi; - - - -public class OshiStatisticsTest { - - // private final OSProcess process = mock(OSProcess.class); - // private final OperatingSystem operatingSystem = mock(OperatingSystem.class); - // private final HardwareAbstractionLayer hardwareAbstractionLayer = - // mock(HardwareAbstractionLayer.class); - // private final OshiStatistics - // oshiStatistics = new OshiStatistics(operatingSystem, hardwareAbstractionLayer); - // - // - // private final SuppliableStatistics stats = mock(SuppliableStatistics.class); - // - // public OshiStatisticsTest() { - // when(operatingSystem.getProcess(eq(1))).thenReturn(process); - // } - // - // @Test - // public void testInit() { - // assertThat(OshiStatistics.init()).isEqualTo(0); - // } - // - // @Test - // public void updateProcessStats() { - // when(process.getVirtualSize()).thenReturn(42L); - // when(process.getResidentSetSize()).thenReturn(420L); - // when(process.getThreadCount()).thenReturn(4200); - // when(process.getKernelTime()).thenReturn(42000L); - // when(process.getUserTime()).thenReturn(420000L); - // - // oshiStatistics.updateProcessStats(1, stats); - // - // verify(stats).setLong(eq(ProcessStats.virtualSize), eq(42L)); - // verify(stats).setLong(eq(ProcessStats.residentSetSize), eq(420L)); - // verify(stats).setLong(eq(ProcessStats.threadCount), eq(4200L)); - // verify(stats).setLong(eq(ProcessStats.kernelTime), eq(42000L)); - // verify(stats).setLong(eq(ProcessStats.userTime), eq(420000L)); - // } - // - // @Test - // public void updateSystemStats() { - // when(operatingSystem.getProcessCount()).thenReturn(1); - // when(operatingSystem.getThreadCount()).thenReturn(2); - // final CentralProcessor centralProcessor = mock(CentralProcessor.class); - // when(centralProcessor.getContextSwitches()).thenReturn(3L); - // when(centralProcessor.getInterrupts()).thenReturn(4L); - // when(centralProcessor.getPhysicalProcessorCount()).thenReturn(5); - // when(centralProcessor.getLogicalProcessorCount()).thenReturn(6); - // when(centralProcessor.getSystemLoadAverage(eq(3))).thenReturn(new double[]{1.0, 2.0, 3.0}); - // when(centralProcessor.getSystemCpuLoadTicks()).thenReturn(new long[]{1, 2, 3, 4, 5, 6, 7, 8}); - // when(hardwareAbstractionLayer.getProcessor()).thenReturn(centralProcessor); - // - // oshiStatistics.updateSystemStats(stats); - // - // verify(stats).setLong(eq(OperatingSystemStats.processCount), eq(1L)); - // verify(stats).setLong(eq(OperatingSystemStats.threadCount), eq(2L)); - // verify(stats).setLong(eq(OperatingSystemStats.contextSwitches), eq(3L)); - // verify(stats).setLong(eq(OperatingSystemStats.interrupts), eq(4L)); - // verify(stats).setLong(eq(OperatingSystemStats.physicalProcessorCount), eq(5L)); - // verify(stats).setLong(eq(OperatingSystemStats.logicalProcessorCount), eq(6L)); - // verify(stats).setDouble(eq(OperatingSystemStats.systemLoadAverage1), eq(1.0)); - // verify(stats).setDouble(eq(OperatingSystemStats.systemLoadAverage5), eq(2.0)); - // verify(stats).setDouble(eq(OperatingSystemStats.systemLoadAverage15), eq(3.0)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksUSER), eq(1L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksNICE), eq(2L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksSYSTEM), eq(3L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksIDLE), eq(4L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksIOWAIT), eq(5L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksIRQ), eq(6L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksSOFTIRQ), eq(7L)); - // verify(stats).setLong(eq(OperatingSystemStats.systemCpuLoadTicksSTEAL), eq(8L)); - // } - -}
