test/lib/jdk/test/lib/containers/cgroup/CPUSetsReader.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File open Sdiff test/lib/jdk/test/lib/containers/cgroup

test/lib/jdk/test/lib/containers/cgroup/CPUSetsReader.java

Print this page


   1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.io.BufferedReader;
  24 import java.io.File;

  25 import java.io.IOException;
  26 import java.nio.file.Files;
  27 import java.nio.file.Paths;
  28 import java.io.FileReader;
  29 import java.util.ArrayList;
  30 import java.util.Optional;
  31 import java.util.List;

  32 import java.util.stream.Collectors;

  33 import java.util.stream.Stream;
  34 import jdk.test.lib.Asserts;
  35 
  36 
  37 // A simple CPU sets reader and parser
  38 public class CPUSetsReader {
  39     public static String PROC_SELF_STATUS_PATH="/proc/self/status";
  40 
  41     // Test the parser
  42     public static void test() {
  43         assertParse("0-7", "0,1,2,3,4,5,6,7");
  44         assertParse("1,3,6", "1,3,6");
  45         assertParse("0,2-4,6,10-11", "0,2,3,4,6,10,11");
  46         assertParse("0", "0");
  47     }
  48 
  49 
  50     private static void assertParse(String cpuSet, String expectedResult) {
  51         Asserts.assertEquals(listToString(parseCpuSet(cpuSet)), expectedResult);
  52     }
  53 










  54 
  55     public static String readFromProcStatus(String setType) {
  56         String path = PROC_SELF_STATUS_PATH;
  57         Optional<String> o = Optional.empty();
  58 
  59         System.out.println("readFromProcStatus() entering for: " + setType);
  60 
  61         try (Stream<String> stream = Files.lines(Paths.get(path))) {
  62             o = stream
  63                 .filter(line -> line.contains(setType))
  64                 .findFirst();
  65         } catch (IOException e) {
  66             return null;
  67         }
  68 
  69         if (!o.isPresent()) {
  70             return null;    // entry not found
  71         }
  72 
  73         String[] parts = o.get().replaceAll("\\s","").split(":");
  74 
  75         // Should be 2 parts, before and after ":"
  76         Asserts.assertEquals(parts.length, 2);
  77 
  78         String result = parts[1];
  79         System.out.println("readFromProcStatus() returning: " + result);
  80         return result;
  81     }
  82 
  83 
  84     public static List<Integer> parseCpuSet(String value) {
  85         ArrayList<Integer> result = new ArrayList<Integer>();
  86 
  87         try {
  88             String[] commaSeparated = value.split(",");
  89 
  90             for (String item : commaSeparated) {
  91                 if (item.contains("-")) {
  92                     addRange(result, item);
  93                 } else {
  94                     result.add(Integer.parseInt(item));
  95                 }
  96             }
  97         } catch (Exception e) {
  98             System.err.println("Exception in getMaxCpuSets(): " + e);
  99             return null;
 100         }
 101 
 102         return result;
 103     }
 104 
 105 
 106     private static void addRange(ArrayList<Integer> list, String s) {
 107         String[] range = s.split("-");
 108         if ( range.length != 2 ) {
 109             throw new RuntimeException("Range should only contain two items, but contains "
 110                                        + range.length + " items");
 111         }
 112 
 113         int min = Integer.parseInt(range[0]);
 114         int max = Integer.parseInt(range[1]);
 115 
 116         if (min >= max) {
 117             String msg = String.format("min is greater or equals to max, min = %d, max = %d",
 118                                        min, max);
 119             throw new RuntimeException(msg);
 120         }
 121 
 122         for (int i = min; i <= max; i++) {
 123             list.add(i);
 124         }
 125     }
 126 
 127 
 128     // Convert list of integers to string with comma-separated values
 129     public static String listToString(List<Integer> list) {
 130         return listToString(list, Integer.MAX_VALUE);
 131     }
 132 
 133     // Convert list of integers to a string with comma-separated values;
 134     // include up to maxCount.
 135     public static String listToString(List<Integer> list, int maxCount) {
 136         return list.stream()
 137             .limit(maxCount)
 138             .map(Object::toString)
 139             .collect(Collectors.joining(","));




 140     }
 141 }
   1 /*
   2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.containers.cgroup;
  25 
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.Paths;

  29 import java.util.ArrayList;

  30 import java.util.List;
  31 import java.util.Optional;
  32 import java.util.stream.Collectors;
  33 import java.util.stream.IntStream;
  34 import java.util.stream.Stream;
  35 import jdk.test.lib.Asserts;
  36 
  37 
  38 // A simple CPU sets reader and parser
  39 public class CPUSetsReader {
  40     public static String PROC_SELF_STATUS_PATH = "/proc/self/status";
  41 
  42     // Test the parser
  43     public static void test() {
  44         assertParse("0-7", "0,1,2,3,4,5,6,7");
  45         assertParse("1,3,6", "1,3,6");
  46         assertParse("0,2-4,6,10-11", "0,2,3,4,6,10,11");
  47         assertParse("0", "0");
  48     }
  49 
  50 
  51     private static void assertParse(String cpuSet, String expectedResult) {
  52         Asserts.assertEquals(listToString(parseCpuSet(cpuSet)), expectedResult);
  53     }
  54 
  55     public static int getNumCpus() {
  56         String path = "/proc/cpuinfo";
  57         try {
  58             Stream<String> stream = Files.lines(Paths.get(path));
  59             return (int) stream.filter(line -> line.startsWith("processor")).count();
  60         } catch (IOException e) {
  61             return 0;
  62         }
  63     }
  64 
  65 
  66     public static String readFromProcStatus(String setType) {
  67         String path = PROC_SELF_STATUS_PATH;
  68         Optional<String> o = Optional.empty();
  69 
  70         System.out.println("readFromProcStatus() entering for: " + setType);
  71 
  72         try (Stream<String> stream = Files.lines(Paths.get(path))) {
  73             o = stream
  74                     .filter(line -> line.contains(setType))
  75                     .findFirst();
  76         } catch (IOException e) {
  77             return null;
  78         }
  79 
  80         if (!o.isPresent()) {
  81             return null;    // entry not found
  82         }
  83 
  84         String[] parts = o.get().replaceAll("\\s", "").split(":");
  85 
  86         // Should be 2 parts, before and after ":"
  87         Asserts.assertEquals(parts.length, 2);
  88 
  89         String result = parts[1];
  90         System.out.println("readFromProcStatus() returning: " + result);
  91         return result;
  92     }
  93 
  94 
  95     public static List<Integer> parseCpuSet(String value) {
  96         ArrayList<Integer> result = new ArrayList<Integer>();
  97 
  98         try {
  99             String[] commaSeparated = value.split(",");
 100 
 101             for (String item : commaSeparated) {
 102                 if (item.contains("-")) {
 103                     addRange(result, item);
 104                 } else {
 105                     result.add(Integer.parseInt(item));
 106                 }
 107             }
 108         } catch (Exception e) {
 109             System.err.println("Exception in getMaxCpuSets(): " + e);
 110             return null;
 111         }
 112 
 113         return result;
 114     }
 115 

 116     private static void addRange(ArrayList<Integer> list, String s) {
 117         String[] range = s.split("-");
 118         if (range.length != 2) {
 119             throw new RuntimeException("Range should only contain two items, but contains "
 120                     + range.length + " items");
 121         }
 122 
 123         int min = Integer.parseInt(range[0]);
 124         int max = Integer.parseInt(range[1]);
 125 
 126         if (min >= max) {
 127             String msg = String.format("min is greater or equals to max, min = %d, max = %d",
 128                     min, max);
 129             throw new RuntimeException(msg);
 130         }
 131 
 132         for (int i = min; i <= max; i++) {
 133             list.add(i);
 134         }
 135     }
 136 
 137 
 138     // Convert list of integers to string with comma-separated values
 139     public static String listToString(List<Integer> list) {
 140         return listToString(list, Integer.MAX_VALUE);
 141     }
 142 
 143     // Convert list of integers to a string with comma-separated values;
 144     // include up to maxCount.
 145     public static String listToString(List<Integer> list, int maxCount) {
 146         return list.stream()
 147                 .limit(maxCount)
 148                 .map(Object::toString)
 149                 .collect(Collectors.joining(","));
 150     }
 151 
 152     public static String numberToString(int num) {
 153         return IntStream.range(0, num).boxed().map(Object::toString).collect(Collectors.joining(","));
 154     }
 155 }
test/lib/jdk/test/lib/containers/cgroup/CPUSetsReader.java
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File