HBASE-27445 fix the result of DirectMemoryUtils#getDirectMemorySize (#4846)

Co-authored-by: huiruan <huiruan@tencent.com>
Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
Ruanhui 2022-11-23 20:09:41 +08:00 committed by GitHub
parent c691091f78
commit 2f4758ee59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 32 deletions

View File

@ -18,12 +18,9 @@
package org.apache.hadoop.hbase.util; package org.apache.hadoop.hbase.util;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.List;
import java.util.Locale;
import javax.management.JMException; import javax.management.JMException;
import javax.management.MBeanServer; import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException; import javax.management.MalformedObjectNameException;
@ -37,6 +34,7 @@ import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;
import org.apache.hbase.thirdparty.io.netty.buffer.ByteBufAllocatorMetric; import org.apache.hbase.thirdparty.io.netty.buffer.ByteBufAllocatorMetric;
import org.apache.hbase.thirdparty.io.netty.buffer.ByteBufAllocatorMetricProvider; import org.apache.hbase.thirdparty.io.netty.buffer.ByteBufAllocatorMetricProvider;
import org.apache.hbase.thirdparty.io.netty.buffer.PooledByteBufAllocator; import org.apache.hbase.thirdparty.io.netty.buffer.PooledByteBufAllocator;
import org.apache.hbase.thirdparty.io.netty.util.internal.PlatformDependent;
/** /**
* Utilities for interacting with and monitoring DirectByteBuffer allocations. * Utilities for interacting with and monitoring DirectByteBuffer allocations.
@ -49,6 +47,7 @@ public class DirectMemoryUtils {
private static final MBeanServer BEAN_SERVER; private static final MBeanServer BEAN_SERVER;
private static final ObjectName NIO_DIRECT_POOL; private static final ObjectName NIO_DIRECT_POOL;
private static final boolean HAS_MEMORY_USED_ATTRIBUTE; private static final boolean HAS_MEMORY_USED_ATTRIBUTE;
private static final long MAX_DIRECT_MEMORY = PlatformDependent.estimateMaxDirectMemory();
static { static {
// initialize singletons. Only maintain a reference to the MBeanServer if // initialize singletons. Only maintain a reference to the MBeanServer if
@ -77,36 +76,9 @@ public class DirectMemoryUtils {
HAS_MEMORY_USED_ATTRIBUTE = a != null; HAS_MEMORY_USED_ATTRIBUTE = a != null;
} }
/** /** Returns the direct memory limit of the current progress */
* @return the setting of -XX:MaxDirectMemorySize as a long. Returns 0 if -XX:MaxDirectMemorySize
* is not set.
*/
public static long getDirectMemorySize() { public static long getDirectMemorySize() {
RuntimeMXBean runtimemxBean = ManagementFactory.getRuntimeMXBean(); return MAX_DIRECT_MEMORY;
List<String> arguments = runtimemxBean.getInputArguments();
long multiplier = 1; // for the byte case.
for (String s : arguments) {
if (s.contains("-XX:MaxDirectMemorySize=")) {
String memSize = s.toLowerCase(Locale.ROOT).replace("-xx:maxdirectmemorysize=", "").trim();
if (memSize.contains("k")) {
multiplier = 1024;
}
else if (memSize.contains("m")) {
multiplier = 1048576;
}
else if (memSize.contains("g")) {
multiplier = 1073741824;
}
memSize = memSize.replaceAll("[^\\d]", "");
long retValue = Long.parseLong(memSize);
return retValue * multiplier;
}
}
return 0;
} }
/** Returns the current amount of direct memory used. */ /** Returns the current amount of direct memory used. */