test/hotspot/jtreg/runtime/containers/docker/CPUSetsReader.java
changeset 50545 292a4a87c321
parent 50544 5f20bf95c052
child 50546 52b866a1a63a
equal deleted inserted replaced
50544:5f20bf95c052 50545:292a4a87c321
     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 }