Removed restricted code usage to get total and free memory. Added tests

This commit is contained in:
lucafrosini 2020-10-07 14:03:17 +02:00
parent 70f69ba26d
commit 07cb3c0296
3 changed files with 95 additions and 8 deletions

12
pom.xml
View File

@ -64,6 +64,18 @@
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -34,6 +34,13 @@ import java.util.concurrent.ScheduledFuture;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.xml.bind.annotation.XmlRootElement;
import org.gcube.common.authorization.client.proxy.AuthorizationProxy;
@ -605,20 +612,41 @@ public class HostingNodeManager extends ContainerHandler {
return memoryFacet;
}
private static final long BYTE_TO_MB = 1024*1024;
private MemoryFacet getRamInfo(MemoryFacet memoryFacet) {
if (memoryFacet == null) {
memoryFacet = new MemoryFacetImpl();
}
OperatingSystemMXBean mxbean = ManagementFactory
.getOperatingSystemMXBean();
/*
OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean;
long freeMemory = sunmxbean.getFreePhysicalMemorySize() / 1048576; // in
// MB
long totalMemory = sunmxbean.getTotalPhysicalMemorySize() / 1048576; // in
// MB
long freeMemory = sunmxbean.getFreePhysicalMemorySize() / 1048576; // in MB
long totalMemory = sunmxbean.getTotalPhysicalMemorySize() / 1048576; // in MB
*/
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
long freeMemory;
try {
freeMemory = Long.parseLong(mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "FreePhysicalMemorySize").toString()) / BYTE_TO_MB;
} catch (NumberFormatException | InstanceNotFoundException | AttributeNotFoundException
| MalformedObjectNameException | ReflectionException | MBeanException e) {
logger.warn("Unable to get free memory from Operating System. Going to get JVM Memory. Better than nothing");
long allocatedMemory = (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
freeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory;
}
long totalMemory;
try {
totalMemory = Long.parseLong(mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize").toString()) / BYTE_TO_MB;
} catch (NumberFormatException | InstanceNotFoundException | AttributeNotFoundException
| MalformedObjectNameException | ReflectionException | MBeanException e) {
logger.warn("Unable to total memory from Operating System. Going to get JVM Memory. Better than nothing");
totalMemory = Runtime.getRuntime().maxMemory();
}
memoryFacet.setUnit(MemoryUnit.MB);
memoryFacet.setSize(totalMemory);
memoryFacet.setUsed(totalMemory - freeMemory);

View File

@ -0,0 +1,47 @@
package org.gcube.smartgears.handler.resourceregistry;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HostingNodeManagerTest {
private static final Logger logger = LoggerFactory.getLogger(HostingNodeManagerTest.class);
@Test
public void compare() throws Exception {
logger.debug("Runtime.getRuntime().maxMemory() : {}", Runtime.getRuntime().maxMemory());
logger.debug("Runtime.getRuntime().totalMemory() : {}", Runtime.getRuntime().totalMemory());
logger.debug("Runtime.getRuntime().freeMemory() : {}", Runtime.getRuntime().freeMemory());
long allocatedMemory = (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
long presumableFreeMemory = Runtime.getRuntime().maxMemory() - allocatedMemory;
logger.debug("allocatedMemory : {}", allocatedMemory);
logger.debug("presumableFreeMemory : {}", presumableFreeMemory);
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Object total = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "TotalPhysicalMemorySize");
logger.debug("Total Memory : {}", total);
Object free = mBeanServer.getAttribute(new ObjectName("java.lang","type","OperatingSystem"), "FreePhysicalMemorySize");
logger.debug("Free Memory : {}", free);
OperatingSystemMXBean mxbean = ManagementFactory.getOperatingSystemMXBean();
@SuppressWarnings("restriction")
com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean;
@SuppressWarnings("restriction")
long freeMemory = sunmxbean.getFreePhysicalMemorySize();
@SuppressWarnings("restriction")
long totalMemory = sunmxbean.getTotalPhysicalMemorySize();
logger.debug("freeMemory : {}", freeMemory);
logger.debug("totalMemory : {}", totalMemory);
}
}