From 0d66efd47f7bbebb8ab76c353d7d29572247f111 Mon Sep 17 00:00:00 2001 From: huiruan Date: Wed, 26 Oct 2022 13:30:59 +0800 Subject: [PATCH 1/5] fix the result of DirectMemoryUtils#getDirectMemorySize --- .../java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java index 15f94ecc46bd..b9bf2027b392 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java @@ -85,7 +85,8 @@ public static long getDirectMemorySize() { RuntimeMXBean runtimemxBean = ManagementFactory.getRuntimeMXBean(); List arguments = runtimemxBean.getInputArguments(); long multiplier = 1; // for the byte case. - for (String s : arguments) { + for (int i = arguments.size() - 1; i >= 0; i--) { + String s = arguments.get(i); if (s.contains("-XX:MaxDirectMemorySize=")) { String memSize = s.toLowerCase(Locale.ROOT).replace("-xx:maxdirectmemorysize=", "").trim(); From 2a53cb0d117d8e7e05a3a9525dcc319ac1d08878 Mon Sep 17 00:00:00 2001 From: huiruan Date: Sun, 6 Nov 2022 03:42:38 +0800 Subject: [PATCH 2/5] fix the result of DirectMemoryUtils#getDirectMemorySize --- .../hadoop/hbase/util/DirectMemoryUtils.java | 42 +++++-------------- 1 file changed, 10 insertions(+), 32 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java index b9bf2027b392..80303e2bb2c7 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java @@ -18,12 +18,10 @@ package org.apache.hadoop.hbase.util; import java.lang.management.ManagementFactory; -import java.lang.management.RuntimeMXBean; +import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.ByteBuffer; -import java.util.List; -import java.util.Locale; import javax.management.JMException; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; @@ -37,6 +35,7 @@ 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.PooledByteBufAllocator; +import org.apache.hbase.thirdparty.io.netty.util.internal.PlatformDependent; /** * Utilities for interacting with and monitoring DirectByteBuffer allocations. @@ -77,37 +76,16 @@ public class DirectMemoryUtils { HAS_MEMORY_USED_ATTRIBUTE = a != null; } - /** - * @return the setting of -XX:MaxDirectMemorySize as a long. Returns 0 if -XX:MaxDirectMemorySize - * is not set. - */ + /** Returns the direct memory limit of the current progress */ public static long getDirectMemorySize() { - RuntimeMXBean runtimemxBean = ManagementFactory.getRuntimeMXBean(); - List arguments = runtimemxBean.getInputArguments(); - long multiplier = 1; // for the byte case. - for (int i = arguments.size() - 1; i >= 0; i--) { - String s = arguments.get(i); - 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; - } + try { + Field directMemoryLimit = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_LIMIT"); + directMemoryLimit.setAccessible(true); + return directMemoryLimit.getLong(PlatformDependent.class); + } catch (Exception e) { + LOG.warn("Failed get direct memory size", e); + return 0L; } - return 0; } /** Returns the current amount of direct memory used. */ From 92fce165c0ca98de3721f7c42ba2d21a77de77c6 Mon Sep 17 00:00:00 2001 From: huiruan Date: Sun, 6 Nov 2022 03:53:02 +0800 Subject: [PATCH 3/5] fix the result of DirectMemoryUtils#getDirectMemorySize --- .../java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java index 80303e2bb2c7..f707c82b93cc 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java @@ -79,7 +79,7 @@ public class DirectMemoryUtils { /** Returns the direct memory limit of the current progress */ public static long getDirectMemorySize() { try { - Field directMemoryLimit = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_LIMIT"); + Field directMemoryLimit = PlatformDependent.class.getDeclaredField("MAX_DIRECT_MEMORY"); directMemoryLimit.setAccessible(true); return directMemoryLimit.getLong(PlatformDependent.class); } catch (Exception e) { From 4c33454c6932e7799ee629709558297c25e5e48e Mon Sep 17 00:00:00 2001 From: huiruan Date: Thu, 17 Nov 2022 00:10:09 +0800 Subject: [PATCH 4/5] fix the result of DirectMemoryUtils#getDirectMemorySize --- .../apache/hadoop/hbase/util/DirectMemoryUtils.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java index f707c82b93cc..d6a343e84d3d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java @@ -18,7 +18,6 @@ package org.apache.hadoop.hbase.util; import java.lang.management.ManagementFactory; -import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.ByteBuffer; @@ -48,6 +47,7 @@ public class DirectMemoryUtils { private static final MBeanServer BEAN_SERVER; private static final ObjectName NIO_DIRECT_POOL; private static final boolean HAS_MEMORY_USED_ATTRIBUTE; + private static long maxDirectMemory = -1L; static { // initialize singletons. Only maintain a reference to the MBeanServer if @@ -78,14 +78,11 @@ public class DirectMemoryUtils { /** Returns the direct memory limit of the current progress */ public static long getDirectMemorySize() { - try { - Field directMemoryLimit = PlatformDependent.class.getDeclaredField("MAX_DIRECT_MEMORY"); - directMemoryLimit.setAccessible(true); - return directMemoryLimit.getLong(PlatformDependent.class); - } catch (Exception e) { - LOG.warn("Failed get direct memory size", e); - return 0L; + if (maxDirectMemory >= 0L) { + return maxDirectMemory; } + maxDirectMemory = PlatformDependent.estimateMaxDirectMemory(); + return maxDirectMemory; } /** Returns the current amount of direct memory used. */ From af7fae924ada404c47fec4a765602d497aed303d Mon Sep 17 00:00:00 2001 From: huiruan Date: Mon, 21 Nov 2022 23:06:22 +0800 Subject: [PATCH 5/5] fix the result of DirectMemoryUtils#getDirectMemorySize --- .../org/apache/hadoop/hbase/util/DirectMemoryUtils.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java index d6a343e84d3d..b944093dc367 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/DirectMemoryUtils.java @@ -47,7 +47,7 @@ public class DirectMemoryUtils { private static final MBeanServer BEAN_SERVER; private static final ObjectName NIO_DIRECT_POOL; private static final boolean HAS_MEMORY_USED_ATTRIBUTE; - private static long maxDirectMemory = -1L; + private static final long MAX_DIRECT_MEMORY = PlatformDependent.estimateMaxDirectMemory(); static { // initialize singletons. Only maintain a reference to the MBeanServer if @@ -78,11 +78,7 @@ public class DirectMemoryUtils { /** Returns the direct memory limit of the current progress */ public static long getDirectMemorySize() { - if (maxDirectMemory >= 0L) { - return maxDirectMemory; - } - maxDirectMemory = PlatformDependent.estimateMaxDirectMemory(); - return maxDirectMemory; + return MAX_DIRECT_MEMORY; } /** Returns the current amount of direct memory used. */