Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Nov 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
package org.apache.hadoop.hbase.util;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
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;
Expand All @@ -37,6 +34,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.
Expand All @@ -49,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 final long MAX_DIRECT_MEMORY = PlatformDependent.estimateMaxDirectMemory();

static {
// initialize singletons. Only maintain a reference to the MBeanServer if
Expand Down Expand Up @@ -77,36 +76,9 @@ 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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this method be called by multiple threads? Let's initialize it while loading the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently not likely, so I make this lazily.

But I think you are right, it's better to evaluate it first. Pushed a new commit to address this. Thanks Duo. @Apache9

RuntimeMXBean runtimemxBean = ManagementFactory.getRuntimeMXBean();
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;
return MAX_DIRECT_MEMORY;
}

/** Returns the current amount of direct memory used. */
Expand Down