< prev index next >

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

Print this page
@  rev 57734 : Review feedback
|
~


  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         }




  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 
  36 import jdk.test.lib.Asserts;
  37 
  38 
  39 // A simple CPU sets reader and parser
  40 public class CPUSetsReader {
  41     public static String PROC_SELF_STATUS_PATH = "/proc/self/status";
  42 
  43     // Test the parser
  44     public static void test() {
  45         assertParse("0-7", "0,1,2,3,4,5,6,7");
  46         assertParse("1,3,6", "1,3,6");
  47         assertParse("0,2-4,6,10-11", "0,2,3,4,6,10,11");
  48         assertParse("0", "0");
  49     }
  50 
  51 
  52     private static void assertParse(String cpuSet, String expectedResult) {
  53         Asserts.assertEquals(listToString(parseCpuSet(cpuSet)), expectedResult);
  54     }
  55 
  56     public static int getNumCpus() {
  57         String path = "/proc/cpuinfo";
  58         try(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         }


< prev index next >