Github user tillrohrmann commented on a diff in the pull request: https://github.com/apache/flink/pull/553#discussion_r29744552 --- Diff: flink-runtime/src/main/scala/org/apache/flink/runtime/taskmanager/TaskManager.scala --- @@ -1894,6 +1895,49 @@ object TaskManager { override def getValue: Double = ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage() }) + + // Preprocessing steps for registering cpuLoad + // dummy initialisation + var fetchCPULoad:(Any) => Double = (obj:Any) => -1 + + // define the fetchCPULoad method as per the fetched getProcessCpuLoad method + getMethodToFetchCPULoad() match { + case Some(method) => fetchCPULoad = (obj:Any) => method.asInstanceOf[Method].invoke(obj). + asInstanceOf[Double] + // Log getProcessCpuLoad method not available for Java 6 + case None => LOG.warn("getProcessCpuLoad method not available in the Operating System Bean" + + "implementation for this Java runtime environment\n" + Thread.currentThread().getStackTrace) + } + + metricRegistry.register("cpuLoad", new Gauge[Double] { + override def getValue: Double = { + try{ + val osMXBean = ManagementFactory.getOperatingSystemMXBean(). + asInstanceOf[com.sun.management.OperatingSystemMXBean] + fetchCPULoad(osMXBean) + } catch { + case t: Throwable => { + LOG.warn("Error retrieving CPU Load through OperatingSystemMXBean", t) + -1 + } + } + } + }) metricRegistry } + + /** + * Fetches getProcessCpuLoad method if available in the + * OperatingSystemMXBean implementation else returns None + * @return + */ + private def getMethodToFetchCPULoad(): Option[Method] = { + val methodsList = classOf[com.sun.management.OperatingSystemMXBean].getMethods() + val method = methodsList.filter(_.getName == "getProcessCpuLoad") --- End diff -- Shortcut: use `headOption` to obtain result.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---