Repository navigation
Description
Java 10 has introduced some new capabilities for determining the memory limit when the process is running inside cgroups (which tends to be the case when running in Docker on linux), as well as the ability to specify the max heap size as a percentage of total memory instead of an explicit value. These features were backported to Java 8 in jdk8_u191. However, it does not appear that there are any new API calls in core java (e.g. java.lang.Runtime or java.lang.System) to expose the physical memory limit (Runtime.maxMemory() gives you the max heap size).
For situations where an app is running inside a Docker container and working with off-heap memory, it'd be great to have the ability to query the amount of physical memory available to the container, so you could calculate the amount of memory potentially[1] available for off-heap allocation. Since the JVM doesn't expose it directly, this would be a good feature for oshi to deliver.
Here's the code which the jvm uses for determining the cgroup memory limit:
// Experimental support for CGroup memory limits
if (UseCGroupMemoryLimitForHeap) {
// This is a rough indicator that a CGroup limit may be in force
// for this process
const char* lim_file = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
FILE *fp = fopen(lim_file, "r");
if (fp != NULL) {
julong cgroup_max = 0;
int ret = fscanf(fp, JULONG_FORMAT, &cgroup_max);
if (ret == 1 && cgroup_max > 0) {
// If unlimited, cgroup_max will be a very large, but unspecified
// value, so use initial phys_mem as a limit
log_info(gc, heap)("Setting phys_mem to the min of cgroup limit ("
JULONG_FORMAT "MB) and initial phys_mem ("
JULONG_FORMAT "MB)", cgroup_max/M, phys_mem/M);
phys_mem = MIN2(cgroup_max, phys_mem);
} else {
warning("Unable to read/parse cgroup memory limit from %s: %s",
lim_file, errno != 0 ? strerror(errno) : "unknown error");
}
fclose(fp);
} else {
warning("Unable to open cgroup memory limit file %s (%s)", lim_file, strerror(errno));
}
}[1] - I say "potentially" because there are still JVM overheads to consider, so the amount of memory available for off-heap allocation would always be less than phys_mem - Runtime.maxMemory()
Activity
dbwiddis commentedon Jun 13, 2019
This is an interesting idea. At first I thought it might be a nice addition but after looking into it, I'm not so sure we need to add the overhead.
I'm always skeptical about adding code that only applies to one operating system, but given that this does provide resource usage I gave it a second look. But it looks like the code above simply reads and parses the contents of a sysfs file. OSHI already has the capability of reading that file using FileUtil.getLongFromFile().
So the above code could simply be:
long cgroup_max = FileUtil.getLongFfomFile("/sys/fs/cgroup/memory/memory.limit_in_bytes");Is it more complex than this?
sworisbreathing commentedon Jun 13, 2019
@dbwiddis - good point. I don't think it's any more complicated than that.
dbwiddis commentedon Jun 13, 2019
I'll leave this up for a little bit to consider a more general "Container" API with potentially other resource limits.
dbwiddis commentedon Jun 13, 2019
Initial peeking around seems like there could be "requests" and "limits" for resources of "cpu" and "memory". So 4 data points per container. Assuming we're inside a container with OSHI, can we be cross-platform (assuming anyone uses anything other than Docker)? Is there an OCI standard?
dbwiddis commentedon Jun 14, 2019
Are there environment variables
CONTAINER_MAX_MEMORYandCONTAINER_MAX_CPUorMAX_CORE_LIMITavailable?Files relevant to container limits:
cores:
cpu_period_file="/sys/fs/cgroup/cpu/cpu.cfs_period_us"
cpu_quota_file="/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
memory:
mem_file="/sys/fs/cgroup/memory/memory.limit_in_bytes"
dbwiddis commentedon Jun 14, 2019
This article is very enlightening.
It appears the community is rallying around replacements for the
/procfilesystem (e.g.,/proc/meminfo). The python "equivalent" to OSHI, psutil, has a parameterPROCFS_PATHthat can be set to aprocplugin replacement (such as procg) to have its API return containerized values. (Perhaps this is something we should add toProcUtil?)dbwiddis commentedon Jul 10, 2019
So we've added a global config file allowing a custom
/procfilesystem. Can you experiment withprocgor another replacement to see if this works for you?dbwiddis commentedon Aug 26, 2019
I'm going to close this issue as I think it's been adressed:
getLongFromFile()to predictable locations/procsystems are being supported and OSHI has enabled the ability to use thoseFeel free to re-open if you have a good idea on what we can do other than the two solutions above.