Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

jstorm 获取系统(linux)获取可用内存应该使用/proc/meminfo中的MemAvailable #427

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -202,7 +202,7 @@ private List<Integer> calculatorAvailablePorts() {
if (cpuUsage <= 0.0 || !ConfigExtension.isSupervisorEnableAutoAdjustSlots(conf)) {
return defaultPortList;
}
long freeMemory = JStormUtils.getFreePhysicalMem() * 1024L;
long freeMemory = JStormUtils.getAvailablePhysicalMem() * 1024L;

long reserveMemory = ConfigExtension.getStormMachineReserveMem(conf);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ public static Long getFreePhysicalMem() {
return LinuxResource.getFreePhysicalMem();
}

public static Long getAvailablePhysicalMem() {
return LinuxResource.getAvailablePhysicalMem();
}

public static int getNumProcessors() {
return LinuxResource.getProcessNum();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -26,6 +27,7 @@ public class LinuxResource {
private static final Pattern CPU_TIME_FORMAT = Pattern.compile("^cpu[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([0-9]+)");

private static final String PROCFS_MEMINFO = "/proc/meminfo";
private static final String MEMINFO_MEMAVAILABLE = "MemAvailable";
public static final long JIFFY_LENGTH_IN_MILLIS;
static {
long jiffiesPerSecond = getJiffies();
Expand Down Expand Up @@ -185,7 +187,23 @@ public static Long getFreePhysicalMem() {
}
return 0L;
}
public static Long getAvailablePhysicalMem() {
if (!OSInfo.isLinux()) {
return 0L;
}
try {

List<String> lines = IOUtils.readLines(new FileInputStream(PROCFS_MEMINFO));
String free = lines.get(2).split("\\s+")[1];
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的逻辑,我觉得应该反过来,默认使用mem free,当存在MemAvailable的时候才用这个。
另外,默认的情况下,需要考虑mem free + cache

if( StringUtils.indexOfIgnoreCase(lines.get(2), MEMINFO_MEMAVAILABLE) < 0 ){
free = lines.get(1).split("\\s+")[1];
}
return Long.valueOf(free);
} catch (Exception ignored) {
LOG.warn("failed to get total free memory.");
}
return 0L;
}
/**
* calcute the disk usage at current filesystem
* @return
Expand Down