< prev index next >

src/java.base/linux/classes/jdk/internal/platform/CgroupSubsystemController.java

Print this page
@  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
~


  31 import java.nio.file.Paths;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 import java.util.Optional;
  35 import java.util.function.Function;
  36 import java.util.stream.Stream;
  37 
  38 /**
  39  * Cgroup version agnostic controller logic
  40  *
  41  */
  42 public interface CgroupSubsystemController {
  43 
  44     public static final String EMPTY_STR = "";
  45 
  46     public String path();
  47 
  48     /**
  49      * getStringValue
  50      *
  51      * Return the first line of the file "parm" argument from the controller.
  52      *
  53      * TODO:  Consider using weak references for caching BufferedReader object.
  54      *
  55      * @param controller
  56      * @param parm
  57      * @return Returns the contents of the file specified by param.
  58      */
  59     public static String getStringValue(CgroupSubsystemController controller, String param) {
  60         if (controller == null) return null;
  61 
  62         try {
  63             return CgroupUtil.readStringValue(controller, param);
  64         }
  65         catch (IOException e) {
  66             return null;
  67         }
  68 
  69     }
  70 
  71     public static long getLongValueMatchingLine(CgroupSubsystemController controller,
  72                                                      String param,
  73                                                      String match,
  74                                                      Function<String, Long> conversion) {
  75         long retval = Metrics.LONG_RETVAL_UNLIMITED;
  76         try {
  77             Path filePath = Paths.get(controller.path(), param);
  78             List<String> lines = CgroupUtil.readAllLinesPrivileged(filePath);
  79             for (String line : lines) {
  80                 if (line.startsWith(match)) {
  81                     retval = conversion.apply(line);
  82                     break;
  83                 }
  84             }
  85         } catch (IOException e) {
  86             // Ignore. Default is unlimited.
  87         }
  88         return retval;
  89     }
  90 
  91     public static long getLongValue(CgroupSubsystemController controller,
  92                                     String parm,
  93                                     Function<String, Long> conversion) {
  94         String strval = getStringValue(controller, parm);
  95         return conversion.apply(strval);
  96     }
  97 
  98     public static double getDoubleValue(CgroupSubsystemController controller, String parm) {
  99         String strval = getStringValue(controller, parm);
 100 
 101         if (strval == null) return 0L;
 102 
 103         double retval = Double.parseDouble(strval);
 104 
 105         return retval;
 106     }
 107 
 108     /**
 109      * getSubSystemlongEntry
 110      *
 111      * Return the long value from the line containing the string "entryname"
 112      * within file "parm" in the "subsystem".
 113      *
 114      * TODO:  Consider using weak references for caching BufferedReader object.
 115      *
 116      * @param controller
 117      * @param parm
 118      * @param entryname
 119      * @return long value
 120      */
 121     public static long getLongEntry(CgroupSubsystemController controller, String parm, String entryname) {
 122         if (controller == null) return 0L;
 123 
 124         try (Stream<String> lines = CgroupUtil.readFilePrivileged(Paths.get(controller.path(), parm))) {
 125 
 126             Optional<String> result = lines.map(line -> line.split(" "))
 127                                            .filter(line -> (line.length == 2 &&
 128                                                    line[0].equals(entryname)))
 129                                            .map(line -> line[1])
 130                                            .findFirst();
 131 
 132             return result.isPresent() ? Long.parseLong(result.get()) : 0L;
 133         }
 134         catch (IOException e) {
 135             return 0L;
 136         }
 137     }
 138 
 139     /**
 140      * StringRangeToIntArray
 141      *
 142      * Convert a string in the form of  1,3-4,6 to an array of
 143      * integers containing all the numbers in the range.
 144      *




  31 import java.nio.file.Paths;
  32 import java.util.ArrayList;
  33 import java.util.List;
  34 import java.util.Optional;
  35 import java.util.function.Function;
  36 import java.util.stream.Stream;
  37 
  38 /**
  39  * Cgroup version agnostic controller logic
  40  *
  41  */
  42 public interface CgroupSubsystemController {
  43 
  44     public static final String EMPTY_STR = "";
  45 
  46     public String path();
  47 
  48     /**
  49      * getStringValue
  50      *
  51      * Return the first line of the file "param" argument from the controller.
  52      *
  53      * TODO:  Consider using weak references for caching BufferedReader object.
  54      *
  55      * @param controller
  56      * @param param
  57      * @return Returns the contents of the file specified by param.
  58      */
  59     public static String getStringValue(CgroupSubsystemController controller, String param) {
  60         if (controller == null) return null;
  61 
  62         try {
  63             return CgroupUtil.readStringValue(controller, param);
  64         }
  65         catch (IOException e) {
  66             return null;
  67         }
  68 
  69     }
  70 
  71     public static long getLongValueMatchingLine(CgroupSubsystemController controller,
  72                                                      String param,
  73                                                      String match,
  74                                                      Function<String, Long> conversion) {
  75         long retval = CgroupSubsystem.LONG_RETVAL_UNLIMITED;
  76         try {
  77             Path filePath = Paths.get(controller.path(), param);
  78             List<String> lines = CgroupUtil.readAllLinesPrivileged(filePath);
  79             for (String line : lines) {
  80                 if (line.startsWith(match)) {
  81                     retval = conversion.apply(line);
  82                     break;
  83                 }
  84             }
  85         } catch (IOException e) {
  86             // Ignore. Default is unlimited.
  87         }
  88         return retval;
  89     }
  90 
  91     public static long getLongValue(CgroupSubsystemController controller,
  92                                     String parm,
  93                                     Function<String, Long> conversion) {
  94         String strval = getStringValue(controller, parm);
  95         return conversion.apply(strval);
  96     }
  97 
  98     public static double getDoubleValue(CgroupSubsystemController controller, String parm) {
  99         String strval = getStringValue(controller, parm);
 100 
 101         if (strval == null) return 0L;
 102 
 103         double retval = Double.parseDouble(strval);
 104 
 105         return retval;
 106     }
 107 
 108     /**
 109      * getSubSystemlongEntry
 110      *
 111      * Return the long value from the line containing the string "entryname"
 112      * within file "param" in the "subsystem".
 113      *
 114      * TODO:  Consider using weak references for caching BufferedReader object.
 115      *
 116      * @param controller
 117      * @param param
 118      * @param entryname
 119      * @return long value
 120      */
 121     public static long getLongEntry(CgroupSubsystemController controller, String param, String entryname) {
 122         if (controller == null) return 0L;
 123 
 124         try (Stream<String> lines = CgroupUtil.readFilePrivileged(Paths.get(controller.path(), param))) {
 125 
 126             Optional<String> result = lines.map(line -> line.split(" "))
 127                                            .filter(line -> (line.length == 2 &&
 128                                                    line[0].equals(entryname)))
 129                                            .map(line -> line[1])
 130                                            .findFirst();
 131 
 132             return result.isPresent() ? Long.parseLong(result.get()) : 0L;
 133         }
 134         catch (IOException e) {
 135             return 0L;
 136         }
 137     }
 138 
 139     /**
 140      * StringRangeToIntArray
 141      *
 142      * Convert a string in the form of  1,3-4,6 to an array of
 143      * integers containing all the numbers in the range.
 144      *


< prev index next >