From dbd211a1ad6c9afe885fadef4ba2597212bc1cdf Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Thu, 22 Apr 2021 20:24:13 +0200 Subject: [PATCH] removed/replaced reflection in memory tool --- htroot/api/localpeers.java | 1 + source/net/yacy/cora/util/Memory.java | 68 ++++++++++++++++++++------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/htroot/api/localpeers.java b/htroot/api/localpeers.java index 920008bc5..b131245eb 100644 --- a/htroot/api/localpeers.java +++ b/htroot/api/localpeers.java @@ -35,6 +35,7 @@ import net.yacy.server.serverObjects; import net.yacy.server.serverSwitch; import net.yacy.server.servletProperties; +// http://localhost:8090/api/localpeers.json public class localpeers { public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, @SuppressWarnings("unused") final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) { diff --git a/source/net/yacy/cora/util/Memory.java b/source/net/yacy/cora/util/Memory.java index d0a171cf8..3f348198b 100644 --- a/source/net/yacy/cora/util/Memory.java +++ b/source/net/yacy/cora/util/Memory.java @@ -22,7 +22,6 @@ package net.yacy.cora.util; import java.lang.management.ManagementFactory; import java.lang.management.ThreadInfo; -import java.lang.reflect.Method; import java.util.LinkedHashMap; import java.util.Map; @@ -82,29 +81,63 @@ public class Memory { } /** - * get the system load within the last minute - * @return the system load or a negative number if the load is not available + * Returns the system load average for the last minute. + * The system load average is the sum of the number of runnable entities + * queued to the {@linkplain #getAvailableProcessors available processors} + * and the number of runnable entities running on the available processors + * averaged over a period of time. + * The way in which the load average is calculated is operating system + * specific but is typically a damped time-dependent average. + *

+ * If the load average is not available, a negative value is returned. + *

+ * This method is designed to provide a hint about the system load + * and may be queried frequently. + * The load average may be unavailable on some platform where it is + * expensive to implement this method. + * + * @return the system load average; or a negative value if not available. */ public static double getSystemLoadAverage() { return ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage(); } + /** + * Returns the "recent cpu usage" for the operating environment. This value + * is a double in the [0.0,1.0] interval. A value of 0.0 means that all CPUs + * were idle during the recent period of time observed, while a value + * of 1.0 means that all CPUs were actively running 100% of the time + * during the recent period being observed. All values betweens 0.0 and + * 1.0 are possible depending of the activities going on. + * If the recent cpu usage is not available, the method returns a + * negative value. + * + * @return the "recent cpu usage" for the whole operating environment; + * a negative value if not available. + */ public static double getSystemCpuLoad() { - return getOSBean("getSystemCpuLoad"); + com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + return operatingSystemMXBean.getCpuLoad(); // getSystemCpuLoad is deprecated, getCpuLoad is the replacement } + /** + * Returns the "recent cpu usage" for the Java Virtual Machine process. + * This value is a double in the [0.0,1.0] interval. A value of 0.0 means + * that none of the CPUs were running threads from the JVM process during + * the recent period of time observed, while a value of 1.0 means that all + * CPUs were actively running threads from the JVM 100% of the time + * during the recent period being observed. Threads from the JVM include + * the application threads as well as the JVM internal threads. All values + * betweens 0.0 and 1.0 are possible depending of the activities going on + * in the JVM process and the whole system. If the Java Virtual Machine + * recent CPU usage is not available, the method returns a negative value. + * + * @return the "recent cpu usage" for the Java Virtual Machine process; + * a negative value if not available. + */ public static double getProcessCpuLoad() { - return getOSBean("getProcessCpuLoad"); - } - - private static double getOSBean(String name) { - try { - Method m = ManagementFactory.getOperatingSystemMXBean().getClass().getMethod(name); - m.setAccessible(true); - Object o = m.invoke(ManagementFactory.getOperatingSystemMXBean()); - if (o instanceof Double) return ((Double) o).doubleValue(); - } catch (Throwable e) {} - return 0.0d; + com.sun.management.OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + return operatingSystemMXBean.getProcessCpuLoad(); } public static Map status() { @@ -117,8 +150,9 @@ public class Memory { status.put("cores", runtime.availableProcessors()); status.put("threads", Thread.activeCount()); status.put("deadlocks", deadlocks()); - status.put("load_system_average", Memory.getSystemLoadAverage()); - status.put("load_process_cpu", Memory.getProcessCpuLoad()); + status.put("load_system_load_average", Memory.getSystemLoadAverage()); + status.put("load_system_cpu_load", Memory.getSystemCpuLoad()); + status.put("load_process_cpu_load", Memory.getProcessCpuLoad()); YaCyHttpServer server = Switchboard.getSwitchboard().getHttpServer(); status.put("server_threads", server == null ? 0 : server.getServerThreads()); return status;