< prev index next >

src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java

Print this page
@  rev 54202 : 8217338: [Containers] Improve systemd slice memory limit support
|  Summary: Use hierachical memory limit in addition to memory_limits_in_bytes
~  Reviewed-by: duke


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.platform.cgroupv1;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.IOException;
  30 import java.math.BigInteger;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.util.ArrayList;

  35 import java.util.Optional;

  36 import java.util.stream.Stream;
  37 
  38 public class SubSystem {
  39     String root;
  40     String mountPoint;
  41     String path;
  42 
  43     public SubSystem(String root, String mountPoint) {
  44         this.root = root;
  45         this.mountPoint = mountPoint;
  46     }
  47 
  48     public void setPath(String cgroupPath) {
  49         if (root != null && cgroupPath != null) {
  50             if (root.equals("/")) {
  51                 if (!cgroupPath.equals("/")) {
  52                     path = mountPoint + cgroupPath;
  53                 }
  54                 else {
  55                     path = mountPoint;


  82      *
  83      * TODO:  Consider using weak references for caching BufferedReader object.
  84      *
  85      * @param subsystem
  86      * @param parm
  87      * @return Returns the contents of the file specified by param.
  88      */
  89     public static String getStringValue(SubSystem subsystem, String parm) {
  90         if (subsystem == null) return null;
  91 
  92         try(BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(subsystem.path(), parm))) {
  93             String line = bufferedReader.readLine();
  94             return line;
  95         }
  96         catch (IOException e) {
  97             return null;
  98         }
  99 
 100     }
 101 



















 102     public static long getLongValue(SubSystem subsystem, String parm) {
 103         String strval = getStringValue(subsystem, parm);
 104         long retval = 0;

 105 


 106         if (strval == null) return 0L;
 107 
 108         try {
 109             retval = Long.parseLong(strval);
 110         } catch (NumberFormatException e) {
 111             // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
 112             // In this case, return Long.max
 113             BigInteger b = new BigInteger(strval);
 114             if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
 115                 return Long.MAX_VALUE;
 116             }
 117         }
 118         return retval;
 119     }
 120 
 121     public static double getDoubleValue(SubSystem subsystem, String parm) {
 122         String strval = getStringValue(subsystem, parm);
 123 
 124         if (strval == null) return 0L;
 125 


 197                 for (int i = lo; i <= hi; i++) {
 198                     results.add(i);
 199                 }
 200             }
 201             else {
 202                 results.add(Integer.parseInt(str));
 203             }
 204         }
 205 
 206         // sort results
 207         results.sort(null);
 208 
 209         // convert ArrayList to primitive int array
 210         ints = new int[results.size()];
 211         int i = 0;
 212         for (Integer n : results) {
 213             ints[i++] = n;
 214         }
 215 
 216         return ints;


















 217     }
 218 }


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.platform.cgroupv1;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.IOException;
  30 import java.math.BigInteger;
  31 import java.nio.file.Files;

  32 import java.nio.file.Paths;
  33 import java.util.ArrayList;
  34 import java.util.List;
  35 import java.util.Optional;
  36 import java.util.function.Function;
  37 import java.util.stream.Stream;
  38 
  39 public class SubSystem {
  40     String root;
  41     String mountPoint;
  42     String path;
  43 
  44     public SubSystem(String root, String mountPoint) {
  45         this.root = root;
  46         this.mountPoint = mountPoint;
  47     }
  48 
  49     public void setPath(String cgroupPath) {
  50         if (root != null && cgroupPath != null) {
  51             if (root.equals("/")) {
  52                 if (!cgroupPath.equals("/")) {
  53                     path = mountPoint + cgroupPath;
  54                 }
  55                 else {
  56                     path = mountPoint;


  83      *
  84      * TODO:  Consider using weak references for caching BufferedReader object.
  85      *
  86      * @param subsystem
  87      * @param parm
  88      * @return Returns the contents of the file specified by param.
  89      */
  90     public static String getStringValue(SubSystem subsystem, String parm) {
  91         if (subsystem == null) return null;
  92 
  93         try(BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(subsystem.path(), parm))) {
  94             String line = bufferedReader.readLine();
  95             return line;
  96         }
  97         catch (IOException e) {
  98             return null;
  99         }
 100 
 101     }
 102 
 103     public static long getLongValueMatchingLine(SubSystem subsystem,
 104                                                      String param,
 105                                                      String match,
 106                                                      Function<String, Long> conversion) {
 107         long retval = Metrics.unlimited_minimum + 1; // default unlimited
 108         try {
 109             List<String> lines = Files.readAllLines(Paths.get(subsystem.path(), param));
 110             for (String line: lines) {
 111                 if (line.contains(match)) {
 112                     retval = conversion.apply(line);
 113                     break;
 114                 }
 115             }
 116         } catch (IOException e) {
 117             // Ignore. Default is unlimited.
 118         }
 119         return retval;
 120     }
 121 
 122     public static long getLongValue(SubSystem subsystem, String parm) {
 123         String strval = getStringValue(subsystem, parm);
 124         return convertStringToLong(strval);
 125     }
 126 
 127     public static long convertStringToLong(String strval) {
 128         long retval = 0;
 129         if (strval == null) return 0L;
 130 
 131         try {
 132             retval = Long.parseLong(strval);
 133         } catch (NumberFormatException e) {
 134             // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
 135             // In this case, return Long.max
 136             BigInteger b = new BigInteger(strval);
 137             if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
 138                 return Long.MAX_VALUE;
 139             }
 140         }
 141         return retval;
 142     }
 143 
 144     public static double getDoubleValue(SubSystem subsystem, String parm) {
 145         String strval = getStringValue(subsystem, parm);
 146 
 147         if (strval == null) return 0L;
 148 


 220                 for (int i = lo; i <= hi; i++) {
 221                     results.add(i);
 222                 }
 223             }
 224             else {
 225                 results.add(Integer.parseInt(str));
 226             }
 227         }
 228 
 229         // sort results
 230         results.sort(null);
 231 
 232         // convert ArrayList to primitive int array
 233         ints = new int[results.size()];
 234         int i = 0;
 235         for (Integer n : results) {
 236             ints[i++] = n;
 237         }
 238 
 239         return ints;
 240     }
 241 
 242     public static class MemorySubSystem extends SubSystem {
 243 
 244         private boolean hierarchical;
 245 
 246         public MemorySubSystem(String root, String mountPoint) {
 247             super(root, mountPoint);
 248         }
 249 
 250         boolean isHierarchical() {
 251             return hierarchical;
 252         }
 253 
 254         void setHierarchical(boolean hierarchical) {
 255             this.hierarchical = hierarchical;
 256         }
 257 
 258     }
 259 }
< prev index next >