< prev index next >

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

Print this page
@  rev 57446 : Review changes
|
o  rev 57445 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~
o  rev 56863 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv
~


  24  */
  25 
  26 package jdk.internal.platform;
  27 
  28 import java.io.IOException;
  29 import java.math.BigInteger;
  30 import java.nio.file.Path;
  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 long RETVAL_UNLIMITED = -1;
  45     public static final long RETVAL_NOT_SUPPORTED = -2;
  46     public static final long RETVAL_ERROR = -3;
  47     public static final String EMPTY_STR = "";
  48 
  49     public String path();
  50 
  51     /**
  52      * getStringValue
  53      *
  54      * Return the first line of the file "parm" argument from the controller.
  55      *
  56      * TODO:  Consider using weak references for caching BufferedReader object.
  57      *
  58      * @param controller
  59      * @param parm
  60      * @return Returns the contents of the file specified by param.
  61      */
  62     public static String getStringValue(CgroupSubsystemController controller, String param) {
  63         if (controller == null) return null;
  64 
  65         try {
  66             return CgroupUtil.readStringValue(controller, param);
  67         }
  68         catch (IOException e) {
  69             return null;
  70         }
  71 
  72     }
  73 
  74     public static long getLongValueMatchingLine(CgroupSubsystemController controller,
  75                                                      String param,
  76                                                      String match,
  77                                                      Function<String, Long> conversion) {
  78         long retval = CgroupSubsystemController.RETVAL_UNLIMITED;
  79         try {
  80             Path filePath = Paths.get(controller.path(), param);
  81             List<String> lines = CgroupUtil.readAllLinesPrivileged(filePath);
  82             for (String line : lines) {
  83                 if (line.startsWith(match)) {
  84                     retval = conversion.apply(line);
  85                     break;
  86                 }
  87             }
  88         } catch (IOException e) {
  89             // Ignore. Default is unlimited.
  90         }
  91         return retval;
  92     }
  93 
  94     public static long getLongValue(CgroupSubsystemController controller,
  95                                     String parm,
  96                                     Function<String, Long> conversion) {
  97         String strval = getStringValue(controller, parm);
  98         return conversion.apply(strval);


 132                                            .map(line -> line[1])
 133                                            .findFirst();
 134 
 135             return result.isPresent() ? Long.parseLong(result.get()) : 0L;
 136         }
 137         catch (IOException e) {
 138             return 0L;
 139         }
 140     }
 141 
 142     /**
 143      * StringRangeToIntArray
 144      *
 145      * Convert a string in the form of  1,3-4,6 to an array of
 146      * integers containing all the numbers in the range.
 147      *
 148      * @param range
 149      * @return int[] containing a sorted list of processors or memory nodes
 150      */
 151     public static int[] stringRangeToIntArray(String range) {
 152         int[] ints = new int[0];
 153 
 154         if (range == null || EMPTY_STR.equals(range)) return ints;
 155 

 156         ArrayList<Integer> results = new ArrayList<>();
 157         String strs[] = range.split(",");
 158         for (String str : strs) {
 159             if (str.contains("-")) {
 160                 String lohi[] = str.split("-");
 161                 // validate format
 162                 if (lohi.length != 2) {
 163                     continue;
 164                 }
 165                 int lo = Integer.parseInt(lohi[0]);
 166                 int hi = Integer.parseInt(lohi[1]);
 167                 for (int i = lo; i <= hi; i++) {
 168                     results.add(i);
 169                 }
 170             }
 171             else {
 172                 results.add(Integer.parseInt(str));
 173             }
 174         }
 175 




  24  */
  25 
  26 package jdk.internal.platform;
  27 
  28 import java.io.IOException;
  29 import java.math.BigInteger;
  30 import java.nio.file.Path;
  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);


 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      *
 145      * @param range
 146      * @return int[] containing a sorted list of processors or memory nodes
 147      */
 148     public static int[] stringRangeToIntArray(String range) {
 149         if (range == null || EMPTY_STR.equals(range)) return null;


 150 
 151         int[] ints = new int[0];
 152         ArrayList<Integer> results = new ArrayList<>();
 153         String strs[] = range.split(",");
 154         for (String str : strs) {
 155             if (str.contains("-")) {
 156                 String lohi[] = str.split("-");
 157                 // validate format
 158                 if (lohi.length != 2) {
 159                     continue;
 160                 }
 161                 int lo = Integer.parseInt(lohi[0]);
 162                 int hi = Integer.parseInt(lohi[1]);
 163                 for (int i = lo; i <= hi; i++) {
 164                     results.add(i);
 165                 }
 166             }
 167             else {
 168                 results.add(Integer.parseInt(str));
 169             }
 170         }
 171 


< prev index next >