< prev index next >

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

Print this page
o  rev 57734 : Review feedback
|
o  rev 57733 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv, mchung
~
o  rev 56862 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -23,30 +23,23 @@
  * questions.
  */
 
 package jdk.internal.platform.cgroupv1;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.math.BigInteger;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.security.AccessController;
-import java.security.PrivilegedActionException;
-import java.security.PrivilegedExceptionAction;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
-import java.util.function.Function;
-import java.util.stream.Stream;
+import jdk.internal.platform.CgroupSubsystem;
+import jdk.internal.platform.CgroupSubsystemController;
 
-public class SubSystem {
+public class CgroupV1SubsystemController implements CgroupSubsystemController {
+
+    private static final double DOUBLE_RETVAL_UNLIMITED = CgroupSubsystem.LONG_RETVAL_UNLIMITED;
+    // Values returned larger than this number are unlimited.
+    static long UNLIMITED_MIN = 0x7FFFFFFFFF000000L;
     String root;
     String mountPoint;
     String path;
 
-    public SubSystem(String root, String mountPoint) {
+    public CgroupV1SubsystemController(String root, String mountPoint) {
         this.root = root;
         this.mountPoint = mountPoint;
     }
 
     public void setPath(String cgroupPath) {

@@ -73,211 +66,53 @@
                 }
             }
         }
     }
 
+    @Override
     public String path() {
         return path;
     }
 
-    /**
-     * getSubSystemStringValue
-     *
-     * Return the first line of the file "parm" argument from the subsystem.
-     *
-     * TODO:  Consider using weak references for caching BufferedReader object.
-     *
-     * @param subsystem
-     * @param parm
-     * @return Returns the contents of the file specified by param.
-     */
-    public static String getStringValue(SubSystem subsystem, String parm) {
-        if (subsystem == null) return null;
-
-        try {
-            return subsystem.readStringValue(parm);
-        } catch (IOException e) {
-            return null;
-        }
-    }
-
-    private String readStringValue(String param) throws IOException {
-        PrivilegedExceptionAction<BufferedReader> pea = () ->
-                Files.newBufferedReader(Paths.get(path(), param));
-        try (BufferedReader bufferedReader =
-                     AccessController.doPrivileged(pea)) {
-            String line = bufferedReader.readLine();
-            return line;
-        } catch (PrivilegedActionException e) {
-            Metrics.unwrapIOExceptionAndRethrow(e);
-            throw new InternalError(e.getCause());
-        }
+    public static long getLongEntry(CgroupSubsystemController controller, String param, String entryname) {
+        return CgroupSubsystemController.getLongEntry(controller,
+                                                      param,
+                                                      entryname,
+                                                      CgroupSubsystem.LONG_RETVAL_UNLIMITED /* retval on error */);
     }
 
-    public static long getLongValueMatchingLine(SubSystem subsystem,
-                                                     String param,
-                                                     String match,
-                                                     Function<String, Long> conversion) {
-        long retval = Metrics.unlimited_minimum + 1; // default unlimited
-        try {
-            List<String> lines = subsystem.readMatchingLines(param);
-            for (String line : lines) {
-                if (line.startsWith(match)) {
-                    retval = conversion.apply(line);
-                    break;
-                }
-            }
-        } catch (IOException e) {
-            // Ignore. Default is unlimited.
-        }
-        return retval;
-    }
-
-    private List<String> readMatchingLines(String param) throws IOException {
-        try {
-            PrivilegedExceptionAction<List<String>> pea = () ->
-                    Files.readAllLines(Paths.get(path(), param));
-            return AccessController.doPrivileged(pea);
-        } catch (PrivilegedActionException e) {
-            Metrics.unwrapIOExceptionAndRethrow(e);
-            throw new InternalError(e.getCause());
-        }
-    }
-
-    public static long getLongValue(SubSystem subsystem, String parm) {
-        String strval = getStringValue(subsystem, parm);
-        return convertStringToLong(strval);
+    public static double getDoubleValue(CgroupSubsystemController controller, String parm) {
+        return CgroupSubsystemController.getDoubleValue(controller,
+                                                        parm,
+                                                        DOUBLE_RETVAL_UNLIMITED /* retval on error */);
     }
 
     public static long convertStringToLong(String strval) {
-        long retval = 0;
-        if (strval == null) return 0L;
-
-        try {
-            retval = Long.parseLong(strval);
-        } catch (NumberFormatException e) {
-            // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
-            // In this case, return Long.MAX_VALUE
-            BigInteger b = new BigInteger(strval);
-            if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
-                return Long.MAX_VALUE;
-            }
-        }
-        return retval;
-    }
-
-    public static double getDoubleValue(SubSystem subsystem, String parm) {
-        String strval = getStringValue(subsystem, parm);
-
-        if (strval == null) return 0L;
-
-        double retval = Double.parseDouble(strval);
-
-        return retval;
-    }
-
-    /**
-     * getSubSystemlongEntry
-     *
-     * Return the long value from the line containing the string "entryname"
-     * within file "parm" in the "subsystem".
-     *
-     * TODO:  Consider using weak references for caching BufferedReader object.
-     *
-     * @param subsystem
-     * @param parm
-     * @param entryname
-     * @return long value
-     */
-    public static long getLongEntry(SubSystem subsystem, String parm, String entryname) {
-        String val = null;
-
-        if (subsystem == null) return 0L;
-
-        try (Stream<String> lines = Metrics.readFilePrivileged(Paths.get(subsystem.path(), parm))) {
-
-            Optional<String> result = lines.map(line -> line.split(" "))
-                                           .filter(line -> (line.length == 2 &&
-                                                   line[0].equals(entryname)))
-                                           .map(line -> line[1])
-                                           .findFirst();
-
-            return result.isPresent() ? Long.parseLong(result.get()) : 0L;
-        }
-        catch (IOException e) {
-            return 0L;
-        }
+        return CgroupSubsystemController.convertStringToLong(strval,
+                                                             Long.MAX_VALUE /* overflow value */,
+                                                             CgroupSubsystem.LONG_RETVAL_UNLIMITED /* retval on error */);
     }
 
-    public static int getIntValue(SubSystem subsystem, String parm) {
-        String val = getStringValue(subsystem, parm);
-
-        if (val == null) return 0;
-
-        return Integer.parseInt(val);
+    public static long longValOrUnlimited(long value) {
+        return value > UNLIMITED_MIN ? CgroupSubsystem.LONG_RETVAL_UNLIMITED : value;
     }
 
-    /**
-     * StringRangeToIntArray
-     *
-     * Convert a string in the form of  1,3-4,6 to an array of
-     * integers containing all the numbers in the range.
-     *
-     * @param range
-     * @return int[] containing a sorted list of processors or memory nodes
-     */
-    public static int[] StringRangeToIntArray(String range) {
-        int[] ints = new int[0];
-
-        if (range == null) return ints;
-
-        ArrayList<Integer> results = new ArrayList<>();
-        String strs[] = range.split(",");
-        for (String str : strs) {
-            if (str.contains("-")) {
-                String lohi[] = str.split("-");
-                // validate format
-                if (lohi.length != 2) {
-                    continue;
-                }
-                int lo = Integer.parseInt(lohi[0]);
-                int hi = Integer.parseInt(lohi[1]);
-                for (int i = lo; i <= hi; i++) {
-                    results.add(i);
-                }
-            }
-            else {
-                results.add(Integer.parseInt(str));
-            }
-        }
-
-        // sort results
-        results.sort(null);
-
-        // convert ArrayList to primitive int array
-        ints = new int[results.size()];
-        int i = 0;
-        for (Integer n : results) {
-            ints[i++] = n;
-        }
-
-        return ints;
-    }
-
-    public static class MemorySubSystem extends SubSystem {
-
-        private boolean hierarchical;
-
-        public MemorySubSystem(String root, String mountPoint) {
-            super(root, mountPoint);
+    public static long getLongValueMatchingLine(CgroupSubsystemController controller,
+                                                String param,
+                                                String match) {
+        return CgroupSubsystemController.getLongValueMatchingLine(controller,
+                                                                  param,
+                                                                  match,
+                                                                  CgroupV1SubsystemController::convertHierachicalLimitLine,
+                                                                  CgroupSubsystem.LONG_RETVAL_UNLIMITED);
         }
 
-        boolean isHierarchical() {
-            return hierarchical;
+    public static long convertHierachicalLimitLine(String line) {
+        String[] tokens = line.split("\\s");
+        if (tokens.length == 2) {
+            String strVal = tokens[1];
+            return CgroupV1SubsystemController.convertStringToLong(strVal);
         }
-
-        void setHierarchical(boolean hierarchical) {
-            this.hierarchical = hierarchical;
+        return CgroupV1SubsystemController.UNLIMITED_MIN + 1; // unlimited
         }
 
-    }
 }
< prev index next >