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 }