8170888: [linux] Experimental support for cgroup memory limits in container (ie Docker) environments

Thu, 05 Jan 2017 18:55:20 -0500

author
dholmes
date
Thu, 05 Jan 2017 18:55:20 -0500
changeset 8712
c7140a91e56a
parent 8671
b4bdf3484720
child 8713
8dfbb002197a

8170888: [linux] Experimental support for cgroup memory limits in container (ie Docker) environments
Summary: Set apparent physical memory to cgroup memory limit when UseCGroupMemoryLimitForHeap is true
Reviewed-by: acorn, gtriantafill
Contributed-by: Christine Flood <chf@redhat.com>

src/share/vm/runtime/arguments.cpp file | annotate | diff | comparison | revisions
src/share/vm/runtime/globals.hpp file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/vm/runtime/arguments.cpp	Thu Jan 05 01:40:00 2017 +0000
     1.2 +++ b/src/share/vm/runtime/arguments.cpp	Thu Jan 05 18:55:20 2017 -0500
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -1768,10 +1768,39 @@
    1.11      FLAG_SET_CMDLINE(uintx, MaxRAMFraction, DefaultMaxRAMFraction);
    1.12    }
    1.13  
    1.14 -  const julong phys_mem =
    1.15 +  julong phys_mem =
    1.16      FLAG_IS_DEFAULT(MaxRAM) ? MIN2(os::physical_memory(), (julong)MaxRAM)
    1.17                              : (julong)MaxRAM;
    1.18  
    1.19 +  // Experimental support for CGroup memory limits
    1.20 +  if (UseCGroupMemoryLimitForHeap) {
    1.21 +    // This is a rough indicator that a CGroup limit may be in force
    1.22 +    // for this process
    1.23 +    const char* lim_file = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
    1.24 +    FILE *fp = fopen(lim_file, "r");
    1.25 +    if (fp != NULL) {
    1.26 +      julong cgroup_max = 0;
    1.27 +      int ret = fscanf(fp, JULONG_FORMAT, &cgroup_max);
    1.28 +      if (ret == 1 && cgroup_max > 0) {
    1.29 +        // If unlimited, cgroup_max will be a very large, but unspecified
    1.30 +        // value, so use initial phys_mem as a limit
    1.31 +        if (PrintGCDetails && Verbose) {
    1.32 +          // Cannot use gclog_or_tty yet.
    1.33 +          tty->print_cr("Setting phys_mem to the min of cgroup limit ("
    1.34 +                        JULONG_FORMAT "MB) and initial phys_mem ("
    1.35 +                        JULONG_FORMAT "MB)", cgroup_max/M, phys_mem/M);
    1.36 +        }
    1.37 +        phys_mem = MIN2(cgroup_max, phys_mem);
    1.38 +      } else {
    1.39 +        warning("Unable to read/parse cgroup memory limit from %s: %s",
    1.40 +                lim_file, errno != 0 ? strerror(errno) : "unknown error");
    1.41 +      }
    1.42 +      fclose(fp);
    1.43 +    } else {
    1.44 +      warning("Unable to open cgroup memory limit file %s (%s)", lim_file, strerror(errno));
    1.45 +    }
    1.46 +  }
    1.47 +
    1.48    // If the maximum heap size has not been set with -Xmx,
    1.49    // then set it as fraction of the size of physical memory,
    1.50    // respecting the maximum and minimum sizes of the heap.
     2.1 --- a/src/share/vm/runtime/globals.hpp	Thu Jan 05 01:40:00 2017 +0000
     2.2 +++ b/src/share/vm/runtime/globals.hpp	Thu Jan 05 18:55:20 2017 -0500
     2.3 @@ -1,5 +1,5 @@
     2.4  /*
     2.5 - * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     2.6 + * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     2.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.8   *
     2.9   * This code is free software; you can redistribute it and/or modify it
    2.10 @@ -2068,6 +2068,10 @@
    2.11            "Maximum ergonomically set heap size (in bytes); zero means use " \
    2.12            "MaxRAM / MaxRAMFraction")                                        \
    2.13                                                                              \
    2.14 +  experimental(bool, UseCGroupMemoryLimitForHeap, false,                    \
    2.15 +          "Use CGroup memory limit as physical memory limit for heap "      \
    2.16 +          "sizing")                                                         \
    2.17 +                                                                            \
    2.18    product(uintx, MaxRAMFraction, 4,                                         \
    2.19            "Maximum fraction (1/n) of real memory used for maximum heap "    \
    2.20            "size")                                                           \

mercurial