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.FileReader;
  25 import java.util.List;
  26 import java.util.ArrayList;
  27 import jdk.test.lib.Asserts;
  28 
  29 
  30 // A simple CPU sets reader and parser
  31 public class CPUSetsReader {
  32 
  33     // Test the parser
  34     public static void test() {
  35         assertParse("0-7", "0,1,2,3,4,5,6,7");
  36         assertParse("1,3,6", "1,3,6");
  37         assertParse("0,2-4,6,10-11", "0,2,3,4,6,10,11");
  38         assertParse("0", "0");
  39     }
  40 
  41 
  42     private static void assertParse(String cpuSet, String expectedResult) {
  43         Asserts.assertEquals(listToString(parseCpuSet(cpuSet)), expectedResult);
  44     }
  45 
  46 
  47     public static String read(String fileName) {
  48         String path = "/sys/fs/cgroup/cpuset/" + fileName;
  49         try {
  50             return readLineFromFile(path);
  51         } catch (Exception e) {
  52             System.err.println("Exception reading " + path);
  53             System.err.println("Exception: " + e);
  54         }
  55 
  56         return null;
  57     }
  58 
  59 
  60     private static String readLineFromFile(String path) throws Exception {
  61         try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
  62             return reader.readLine();
  63         }
  64     }
  65 
  66 
  67     public static List<Integer> parseCpuSet(String value) {
  68         ArrayList<Integer> result = new ArrayList<Integer>();
  69 
  70         try {
  71             String[] commaSeparated = value.split(",");
  72 
  73             for (String item : commaSeparated) {
  74                 if (item.contains("-")) {
  75                     addRange(result, item);
  76                 } else {
  77                     result.add(Integer.parseInt(item));
  78                 }
  79             }
  80         } catch (Exception e) {
  81             System.err.println("Exception in getMaxCpuSets(): " + e);
  82             return null;
  83         }
  84 
  85         return result;
  86     }
  87 
  88 
  89     private static void addRange(ArrayList<Integer> list, String s) {
  90         String[] range = s.split("-");
  91         if ( range.length != 2 ) {
  92             throw new RuntimeException("Range should only contain two items, but contains "
  93                                        + range.length + " items");
  94         }
  95 
  96         int min = Integer.parseInt(range[0]);
  97         int max = Integer.parseInt(range[1]);
  98 
  99         if (min >= max) {
 100             String msg = String.format("min is greater or equals to max, min = %d, max = %d",
 101                                        min, max);
 102             throw new RuntimeException(msg);
 103         }
 104 
 105         for (int i = min; i <= max; i++) {
 106             list.add(i);
 107         }
 108     }
 109 
 110 
 111     // Convert list of integers to string with comma-separated values
 112     public static String listToString(List<Integer> list) {
 113         return listToString(list, Integer.MAX_VALUE);
 114     }
 115 
 116 
 117     // Convert list of integers to a string with comma-separated values;
 118     // include up to maxCount.
 119     public static String listToString(List<Integer> list, int maxCount) {
 120         StringBuilder sb = new StringBuilder();
 121         int count = 0;
 122 
 123         for (int item : list) {
 124             sb.append("" + item + ",");
 125             count++;
 126             if (count >= maxCount) {
 127                 break;
 128             }
 129         }
 130 
 131         // remove last comma
 132         sb.deleteCharAt(sb.length() - 1);
 133 
 134         return sb.toString();
 135     }
 136 }