Skip to content

Feature Request: expose cgroup memory limits when running in Docker #893

Closed

Description

@sworisbreathing

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

  1. dbwiddis commented on Jun 13, 2019

    @dbwiddis
    Member

    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?

  2. sworisbreathing commented on Jun 13, 2019

    @sworisbreathing
    Author

    @dbwiddis - good point. I don't think it's any more complicated than that.

  3. dbwiddis commented on Jun 13, 2019

    @dbwiddis
    Member

    I'll leave this up for a little bit to consider a more general "Container" API with potentially other resource limits.

  4. added
    design discussion
    new feature
    on Jun 13, 2019
  5. dbwiddis commented on Jun 13, 2019

    @dbwiddis
    Member

    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?

  6. dbwiddis commented on Jun 14, 2019

    @dbwiddis
    Member

    Are there environment variables CONTAINER_MAX_MEMORY and CONTAINER_MAX_CPU or MAX_CORE_LIMIT available?

    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"

  7. dbwiddis commented on Jun 14, 2019

    @dbwiddis
    Member

    This article is very enlightening.

    It appears the community is rallying around replacements for the /proc filesystem (e.g., /proc/meminfo). The python "equivalent" to OSHI, psutil, has a parameter PROCFS_PATH that can be set to a proc plugin replacement (such as procg) to have its API return containerized values. (Perhaps this is something we should add to ProcUtil?)

  8. dbwiddis commented on Jul 10, 2019

    @dbwiddis
    Member

    So we've added a global config file allowing a custom /proc filesystem. Can you experiment with procg or another replacement to see if this works for you?

  9. dbwiddis commented on Aug 26, 2019

    @dbwiddis
    Member

    I'm going to close this issue as I think it's been adressed:

    • for the specific Docker case, it's easy to use the existing getLongFromFile() to predictable locations
    • cross-platform, it looks like alternative /proc systems are being supported and OSHI has enabled the ability to use those

    Feel free to re-open if you have a good idea on what we can do other than the two solutions above.

  10. added
    containers
    on Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    containersdesign discussionnew feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions