You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by jb...@apache.org on 2021/11/19 19:44:24 UTC

[geode] 04/16: adds network intraces.

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 c46ec0d862c984508c72eee6f9e3c16c51f175ea
Author: Jacob Barrett <jb...@pivotal.io>
AuthorDate: Sat Jun 12 09:32:04 2021 -0700

    adds network intraces.
---
 .../statistics/oshi/NetworkInterfaceStats.java     | 98 ++++++++++++++++++++++
 .../oshi/OshiStatisticsProviderImpl.java           | 48 +++++++++--
 2 files changed, 141 insertions(+), 5 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/NetworkInterfaceStats.java b/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/NetworkInterfaceStats.java
new file mode 100644
index 0000000..95d4528
--- /dev/null
+++ b/geode-core/src/main/java/org/apache/geode/internal/statistics/oshi/NetworkInterfaceStats.java
@@ -0,0 +1,98 @@
+/*
+ * 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 org.jetbrains.annotations.NotNull;
+
+import org.apache.geode.StatisticDescriptor;
+import org.apache.geode.StatisticsType;
+import org.apache.geode.StatisticsTypeFactory;
+import org.apache.geode.annotations.Immutable;
+import org.apache.geode.internal.statistics.StatisticsTypeFactoryImpl;
+
+public class NetworkInterfaceStats {
+  static final int mtu;
+  static final int bytesReceived;
+  static final int bytesSent;
+  static final int packetsReceived;
+  static final int packetsSent;
+  static final int inErrors;
+  static final int outErrors;
+  static final int inDrops;
+  static final int collisions;
+  static final int speed;
+
+  @Immutable
+  private static final StatisticsType statisticsType;
+
+  static {
+    final StatisticsTypeFactory f = StatisticsTypeFactoryImpl.singleton();
+
+    statisticsType = f.createType("NetworkInterfaceStats", "Stats for a network interface.",
+        new StatisticDescriptor[]{
+            f.createLongGauge("mtu",
+                "The interface Maximum Transmission Unit (MTU).",
+                "bytes"),
+            f.createLongCounter("bytesReceived",
+                "The bytes received.",
+                "bytes"),
+            f.createLongCounter("bytesSent",
+                "The bytes sent.",
+                "bytes"),
+            f.createLongCounter("packetsReceived",
+                "The packets received",
+                "packets"),
+            f.createLongCounter("packetsSent",
+                "The packets sent.",
+                "packets"),
+            f.createLongCounter("inErrors",
+                "Input errors",
+                "packets"),
+            f.createLongCounter("outErrors",
+                "Output errors",
+                "packets"),
+            f.createLongCounter("inDrops",
+                "Incoming/Received dropped packets.",
+                "packets"),
+            f.createLongCounter("collisions",
+                "Packet collisions.",
+                "packets"),
+            f.createLongGauge("speed",
+                "The speed of the network interface in bits per second.",
+                "bits/s"),
+        });
+
+    mtu = statisticsType.nameToId("mtu");
+    bytesReceived = statisticsType.nameToId("bytesReceived");
+    bytesSent = statisticsType.nameToId("bytesSent");
+    packetsReceived = statisticsType.nameToId("packetsReceived");
+    packetsSent = statisticsType.nameToId("packetsSent");
+    inErrors = statisticsType.nameToId("inErrors");
+    outErrors = statisticsType.nameToId("outErrors");
+    inDrops = statisticsType.nameToId("inDrops");
+    collisions = statisticsType.nameToId("collisions");
+    speed = statisticsType.nameToId("speed");
+  }
+
+  private NetworkInterfaceStats() {
+    // no instances allowed
+  }
+
+  public static @NotNull StatisticsType getType() {
+    return statisticsType;
+  }
+
+}
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 dcaf5b4..ce02b38 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
@@ -2,6 +2,7 @@ package org.apache.geode.internal.statistics.oshi;
 
 import java.util.List;
 
+import org.apache.logging.log4j.Logger;
 import org.jetbrains.annotations.NotNull;
 import oshi.SystemInfo;
 import oshi.hardware.CentralProcessor;
@@ -9,14 +10,17 @@ import oshi.hardware.CentralProcessor.LogicalProcessor;
 import oshi.hardware.CentralProcessor.TickType;
 import oshi.hardware.GlobalMemory;
 import oshi.hardware.HardwareAbstractionLayer;
+import oshi.hardware.NetworkIF;
 import oshi.hardware.VirtualMemory;
 import oshi.software.os.OSProcess;
 import oshi.software.os.OperatingSystem;
 
 import org.apache.geode.Statistics;
 import org.apache.geode.internal.statistics.platform.OsStatisticsFactory;
+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();
   
@@ -24,13 +28,16 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider {
   private CentralProcessor processor;
   private OperatingSystem operatingSystem;
   private HardwareAbstractionLayer hardware;
+  private List<NetworkIF> networkIFs;
+
   private long[] systemCpuLoadTicks;
   private long[][] processorCpuLoadTicks;
+  private OSProcess process;
 
   private Statistics processStats;
   private Statistics systemStats;
   private Statistics[] processorStats;
-  private OSProcess process;
+  private Statistics[] networkInterfaceStats;
 
   @Override
   public void init(final @NotNull OsStatisticsFactory osStatisticsFactory,
@@ -55,12 +62,21 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider {
     final List<LogicalProcessor> logicalProcessors = processor.getLogicalProcessors();
     processorCpuLoadTicks = new long[logicalProcessors.size()][TickType.values().length];
     processorStats = new Statistics[logicalProcessors.size()];
-    for (int i = 0, logicalProcessorsSize = logicalProcessors.size(); i < logicalProcessorsSize; i++) {
+    for (int i = 0, size = logicalProcessors.size(); i < size; i++) {
       final LogicalProcessor logicalProcessor = logicalProcessors.get(i);
       final String processorIdentity = logicalProcessor.toString();
       processorStats[i] = osStatisticsFactory.createOsStatistics(ProcessorStats.getType(),
           processorIdentity, id, 0);
     }
+
+    networkIFs = hardware.getNetworkIFs();
+    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());
+      networkInterfaceStats[i] = osStatisticsFactory.createOsStatistics(NetworkInterfaceStats.getType(),
+          networkIF.getDisplayName(), id, 0);
+    }
   }
 
   @Override
@@ -68,13 +84,14 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider {
     sampleProcess();
     sampleSystem();
     sampleProcessors();
+    sampleNetworkInterfaces();
   }
 
   @Override
   public void destroy() {
   }
 
-  public void sampleProcess() {
+  private void sampleProcess() {
     final OSProcess process = operatingSystem.getProcess(processId);
 
     final double processCpuLoadBetweenTicks = process.getProcessCpuLoadBetweenTicks(this.process);
@@ -95,7 +112,7 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider {
     processStats.setLong(ProcessStats.contextSwitches, process.getContextSwitches());
   }
 
-  public void sampleSystem() {
+  private void sampleSystem() {
     systemStats.setLong(OperatingSystemStats.processCount, operatingSystem.getProcessCount());
     systemStats.setLong(OperatingSystemStats.threadCount, operatingSystem.getThreadCount());
 
@@ -175,5 +192,26 @@ public class OshiStatisticsProviderImpl implements OshiStatisticsProvider {
           processorCpuLoadTick[TickType.STEAL.getIndex()]);
     }
   }
-  
+
+  private void sampleNetworkInterfaces() {
+    for (int i = 0, size = networkIFs.size(); i < size; i++) {
+      final NetworkIF networkIF = networkIFs.get(i);
+      if (!networkIF.updateAttributes()) {
+        continue;
+      }
+
+      final Statistics networkInterfaceStat = networkInterfaceStats[i];
+      networkInterfaceStat.setLong(NetworkInterfaceStats.mtu, networkIF.getMTU());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.bytesReceived, networkIF.getBytesRecv());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.bytesSent, networkIF.getBytesSent());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.packetsReceived, networkIF.getPacketsRecv());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.packetsSent, networkIF.getPacketsSent());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.inErrors, networkIF.getInErrors());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.outErrors, networkIF.getOutErrors());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.inDrops, networkIF.getInDrops());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.collisions, networkIF.getCollisions());
+      networkInterfaceStat.setLong(NetworkInterfaceStats.speed, networkIF.getSpeed());
+    }
+
+  }
 }