From 4afacef8c73dd1285140357cde15c5ccac1d483e Mon Sep 17 00:00:00 2001 From: duke Date: Mon, 23 Mar 2026 16:02:38 +0000 Subject: [PATCH] Backport 5c2ef74931c05cac1aa589a8e1f9c242361dc247 --- .../jtreg/runtime/os/AvailableProcessors.java | 49 +++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/test/hotspot/jtreg/runtime/os/AvailableProcessors.java b/test/hotspot/jtreg/runtime/os/AvailableProcessors.java index 18201d99127..51d6742b832 100644 --- a/test/hotspot/jtreg/runtime/os/AvailableProcessors.java +++ b/test/hotspot/jtreg/runtime/os/AvailableProcessors.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -38,6 +38,9 @@ import java.util.ArrayList; import java.io.File; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; public class AvailableProcessors { @@ -73,13 +76,17 @@ public static void main(String[] args) throws Exception { "AvailableProcessors"); int[] expected = new int[] { 1, available/2, available-1, available }; + int cpuId = getFirstAllowedCpu(); + if (cpuId == -1) { + throw new SkippedException("Could not determine allowed CPU cores"); + } for (int i : expected) { System.out.println("Testing for " + i + " processors ..."); - int max = i - 1; + int max = i - 1 + cpuId; ArrayList cmdline = new ArrayList<>(master.command()); // prepend taskset command - cmdline.add(0, "0-" + max); + cmdline.add(0, cpuId + "-" + max); cmdline.add(0, "-c"); cmdline.add(0, taskset); // append expected processor count @@ -104,4 +111,40 @@ static void checkProcessors(int expected) { else System.out.println(SUCCESS_STRING + available); } + + /** + * Retrieves the first available CPU core ID allowed for the current process on Linux. + * + * @return The first CPU ID in Cpus_allowed_list, or -1 if unavailable. + */ + static int getFirstAllowedCpu() { + final String statusFile = "/proc/self/status"; + final String targetKey = "Cpus_allowed_list:"; + + try (BufferedReader br = new BufferedReader(new FileReader(statusFile))) { + String line; + while ((line = br.readLine()) != null) { + // Look for the line starting with "Cpus_allowed_list:" + if (line.startsWith(targetKey)) { + // Extract the value part, e.g., "0-15,32-47" or "80,82,84" + String listValue = line.substring(targetKey.length()).trim(); + if (listValue.isEmpty()) return -1; + + // Get the first segment before any comma (e.g., "0-15" from "0-15,32") + String firstSegment = listValue.split(",")[0]; + + // If it is a range (e.g., "80-159"), take the start number + if (firstSegment.contains("-")) { + return Integer.parseInt(firstSegment.split("-")[0]); + } else { + // If it is a single ID (e.g., "1"), parse it directly + return Integer.parseInt(firstSegment); + } + } + } + } catch (IOException | NumberFormatException | ArrayIndexOutOfBoundsException e) { + throw new RuntimeException("Failed to read or parse " + statusFile, e); + } + return -1; + } }