Skip to content

Commit 8440e30

Browse files
LucaCanalisrowen
authored andcommitted
[SPARK-25228][CORE] Add executor CPU time metric.
## What changes were proposed in this pull request? Add a new metric to measure the executor's process (JVM) CPU time. ## How was this patch tested? Manually tested on a Spark cluster (see SPARK-25228 for an example screenshot). Closes apache#22218 from LucaCanali/AddExecutrCPUTimeMetric. Authored-by: LucaCanali <[email protected]> Signed-off-by: Sean Owen <[email protected]>
1 parent 341b55a commit 8440e30

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

core/src/main/scala/org/apache/spark/executor/ExecutorSource.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717

1818
package org.apache.spark.executor
1919

20+
import java.lang.management.ManagementFactory
2021
import java.util.concurrent.ThreadPoolExecutor
22+
import javax.management.{MBeanServer, ObjectName}
2123

2224
import scala.collection.JavaConverters._
25+
import scala.util.control.NonFatal
2326

2427
import com.codahale.metrics.{Gauge, MetricRegistry}
2528
import org.apache.hadoop.fs.FileSystem
@@ -73,6 +76,24 @@ class ExecutorSource(threadPool: ThreadPoolExecutor, executorId: String) extends
7376
registerFileSystemStat(scheme, "write_ops", _.getWriteOps(), 0)
7477
}
7578

79+
// Dropwizard metrics gauge measuring the executor's process CPU time.
80+
// This Gauge will try to get and return the JVM Process CPU time or return -1 otherwise.
81+
// The CPU time value is returned in nanoseconds.
82+
// It will use proprietary extensions such as com.sun.management.OperatingSystemMXBean or
83+
// com.ibm.lang.management.OperatingSystemMXBean, if available.
84+
metricRegistry.register(MetricRegistry.name("jvmCpuTime"), new Gauge[Long] {
85+
val mBean: MBeanServer = ManagementFactory.getPlatformMBeanServer
86+
val name = new ObjectName("java.lang", "type", "OperatingSystem")
87+
override def getValue: Long = {
88+
try {
89+
// return JVM process CPU time if the ProcessCpuTime method is available
90+
mBean.getAttribute(name, "ProcessCpuTime").asInstanceOf[Long]
91+
} catch {
92+
case NonFatal(_) => -1L
93+
}
94+
}
95+
})
96+
7697
// Expose executor task metrics using the Dropwizard metrics system.
7798
// The list is taken from TaskMetrics.scala
7899
val METRIC_CPU_TIME = metricRegistry.counter(MetricRegistry.name("cpuTime"))

0 commit comments

Comments
 (0)