Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/containers/docker/CPUSetsReader.java
32285 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.io.BufferedReader;23import java.io.File;24import java.io.IOException;25import java.nio.file.Files;26import java.nio.file.Paths;27import java.io.FileReader;28import java.util.ArrayList;29import java.util.Optional;30import java.util.List;31import java.util.stream.Collectors;32import java.util.stream.Stream;33import com.oracle.java.testlibrary.Asserts;343536// A simple CPU sets reader and parser37public class CPUSetsReader {38public static String PROC_SELF_STATUS_PATH="/proc/self/status";3940// Test the parser41public static void test() {42assertParse("0-7", "0,1,2,3,4,5,6,7");43assertParse("1,3,6", "1,3,6");44assertParse("0,2-4,6,10-11", "0,2,3,4,6,10,11");45assertParse("0", "0");46}474849private static void assertParse(String cpuSet, String expectedResult) {50Asserts.assertEquals(listToString(parseCpuSet(cpuSet)), expectedResult);51}525354public static String readFromProcStatus(String setType) {55String path = PROC_SELF_STATUS_PATH;56Optional<String> o = Optional.empty();5758System.out.println("readFromProcStatus() entering for: " + setType);5960try (Stream<String> stream = Files.lines(Paths.get(path))) {61o = stream62.filter(line -> line.contains(setType))63.findFirst();64} catch (IOException e) {65return null;66}6768if (!o.isPresent()) {69return null; // entry not found70}7172String[] parts = o.get().replaceAll("\\s","").split(":");7374// Should be 2 parts, before and after ":"75Asserts.assertEquals(parts.length, 2);7677String result = parts[1];78System.out.println("readFromProcStatus() returning: " + result);79return result;80}818283public static List<Integer> parseCpuSet(String value) {84ArrayList<Integer> result = new ArrayList<Integer>();8586try {87String[] commaSeparated = value.split(",");8889for (String item : commaSeparated) {90if (item.contains("-")) {91addRange(result, item);92} else {93result.add(Integer.parseInt(item));94}95}96} catch (Exception e) {97System.err.println("Exception in getMaxCpuSets(): " + e);98return null;99}100101return result;102}103104105private static void addRange(ArrayList<Integer> list, String s) {106String[] range = s.split("-");107if ( range.length != 2 ) {108throw new RuntimeException("Range should only contain two items, but contains "109+ range.length + " items");110}111112int min = Integer.parseInt(range[0]);113int max = Integer.parseInt(range[1]);114115if (min >= max) {116String msg = String.format("min is greater or equals to max, min = %d, max = %d",117min, max);118throw new RuntimeException(msg);119}120121for (int i = min; i <= max; i++) {122list.add(i);123}124}125126127// Convert list of integers to string with comma-separated values128public static String listToString(List<Integer> list) {129return listToString(list, Integer.MAX_VALUE);130}131132// Convert list of integers to a string with comma-separated values;133// include up to maxCount.134public static String listToString(List<Integer> list, int maxCount) {135return list.stream()136.limit(maxCount)137.map(Object::toString)138.collect(Collectors.joining(","));139}140}141142143