You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@samza.apache.org by GitBox <gi...@apache.org> on 2022/11/04 20:10:07 UTC

[GitHub] [samza] mynameborat commented on a diff in pull request #1636: SAMZA-2762: new cpu usage metric which counts child processes usage

mynameborat commented on code in PR #1636:
URL: https://github.com/apache/samza/pull/1636#discussion_r1013440757


##########
samza-core/src/main/java/org/apache/samza/container/host/DefaultSystemStatisticsGetter.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.samza.container.host;
+
+import com.google.common.annotations.VisibleForTesting;
+
+
+/**
+ * An default implementation of {@link SystemStatisticsGetter} that relies on {@link PosixCommandBasedStatisticsGetter}
+ * and {@link OshiBasedStatisticsGetter} implementations
+ */
+public class DefaultSystemStatisticsGetter implements SystemStatisticsGetter {
+

Review Comment:
   nit: remove extra line



##########
samza-core/src/main/java/org/apache/samza/container/host/ProcessCPUStatistics.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.samza.container.host;
+
+import java.util.Objects;
+
+
+/**
+ * A {@link ProcessCPUStatistics} object represents recent CPU usage percentage about the container process(including its child processes)
+ */
+public class ProcessCPUStatistics {

Review Comment:
   why do we need to wrap this into a container object? 



##########
samza-core/src/main/java/org/apache/samza/container/host/OshiBasedStatisticsGetter.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.samza.container.host;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.commons.lang3.NotImplementedException;
+import oshi.SystemInfo;
+import oshi.software.os.OSProcess;
+import oshi.software.os.OperatingSystem;
+
+
+/**
+ * An implementation of {@link SystemStatisticsGetter} that relies on using oshi framework(https://www.oshi.ooo/)
+ */
+public class OshiBasedStatisticsGetter implements SystemStatisticsGetter {
+  // the snapshots of current JVM process and its child processes
+  private final Map<Integer, OSProcess> previousProcessSnapshots = new HashMap<>();
+
+  private final OperatingSystem os;
+  private final int cpuCount;
+
+  public OshiBasedStatisticsGetter() {
+    this(new SystemInfo());
+  }
+
+  @VisibleForTesting
+  OshiBasedStatisticsGetter(SystemInfo si) {
+    this(si.getOperatingSystem(), si.getHardware().getProcessor().getPhysicalProcessorCount());
+  }
+
+  @VisibleForTesting
+  OshiBasedStatisticsGetter(OperatingSystem os, int cpuCount) {
+    this.os = os;
+    this.cpuCount = cpuCount;
+  }
+
+  @Override
+  public SystemMemoryStatistics getSystemMemoryStatistics() {
+    throw new NotImplementedException("Not implemented");
+  }
+
+  @Override
+  public ProcessCPUStatistics getProcessCPUStatistics() {
+    final List<OSProcess> currentProcessAndChildProcesses = getCurrentProcessAndChildProcesses();
+    final double totalCPUUsage = getTotalCPUUsage(currentProcessAndChildProcesses);
+    refreshProcessSnapshots(currentProcessAndChildProcesses);
+    return new ProcessCPUStatistics(100d * totalCPUUsage / cpuCount);
+  }
+
+  private List<OSProcess> getCurrentProcessAndChildProcesses() {
+    final List<OSProcess> processes = new ArrayList<>();
+    // get current process
+    processes.add(os.getProcess(os.getProcessId()));
+    // get all child processes of current process
+    processes.addAll(os.getChildProcesses(os.getProcessId(), OperatingSystem.ProcessFiltering.ALL_PROCESSES,
+        OperatingSystem.ProcessSorting.NO_SORTING, 0));
+    return processes;
+  }
+
+  private double getTotalCPUUsage(List<OSProcess> processes) {
+    return processes.stream()
+        .mapToDouble(p -> p.getProcessCpuLoadBetweenTicks(previousProcessSnapshots.get(p.getProcessID())))

Review Comment:
   What is the behavior if `previousProcessSnapshots.get(...)` returns `null`? 
   Will `getProcessCpuLoadBetweenTicks(null)` throw errors?



##########
samza-core/src/main/java/org/apache/samza/container/host/PosixCommandBasedStatisticsGetter.java:
##########
@@ -85,4 +86,10 @@ public SystemMemoryStatistics getSystemMemoryStatistics() {
       return null;
     }
   }
+
+  @Override
+  public ProcessCPUStatistics getProcessCPUStatistics() {
+    throw new NotImplementedException(
+        "No appropriate Posix command available for getting recent CPU usage information. For example, the CPU information exposed by ps command 'ps -o %cpu= -p <PID>' represents the percentage of time spent running during the entire lifetime of a process not for the recent CPU usage");
+  }

Review Comment:
   Why not `top`?



##########
samza-core/src/main/scala/org/apache/samza/container/SamzaContainerMetrics.scala:
##########
@@ -49,6 +49,7 @@ class SamzaContainerMetrics(
   val executorWorkFactor = newGauge("executor-work-factor", 1.0)
   val physicalMemoryMb = newGauge("physical-memory-mb", 0.0F)
   val physicalMemoryUtilization = newGauge("physical-memory-utilization", 0.0F)
+  val totalProcessCpuUsage = newGauge("total-process-cpu-usage", 0.0)

Review Comment:
   We are not rolling into the existing metrics is it? Why so? 
   If we want to keep it separate, can we add documentation about the new metric to the website docs?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@samza.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org