Skip to content

Commit 51118e0

Browse files
author
SendaoYan
committed
8380316: Test runtime/os/AvailableProcessors.java fails Invalid argument
Backport-of: 5c2ef74
1 parent f4d3e77 commit 51118e0

1 file changed

Lines changed: 46 additions & 3 deletions

File tree

test/hotspot/jtreg/runtime/os/AvailableProcessors.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -38,6 +38,9 @@
3838

3939
import java.util.ArrayList;
4040
import java.io.File;
41+
import java.io.BufferedReader;
42+
import java.io.FileReader;
43+
import java.io.IOException;
4144

4245
public class AvailableProcessors {
4346

@@ -73,13 +76,17 @@ public static void main(String[] args) throws Exception {
7376
"AvailableProcessors");
7477

7578
int[] expected = new int[] { 1, available/2, available-1, available };
79+
int cpuId = getFirstAllowedCpu();
80+
if (cpuId == -1) {
81+
throw new SkippedException("Could not determine allowed CPU cores");
82+
}
7683

7784
for (int i : expected) {
7885
System.out.println("Testing for " + i + " processors ...");
79-
int max = i - 1;
86+
int max = i - 1 + cpuId;
8087
ArrayList<String> cmdline = new ArrayList<>(master.command());
8188
// prepend taskset command
82-
cmdline.add(0, "0-" + max);
89+
cmdline.add(0, cpuId + "-" + max);
8390
cmdline.add(0, "-c");
8491
cmdline.add(0, taskset);
8592
// append expected processor count
@@ -104,4 +111,40 @@ static void checkProcessors(int expected) {
104111
else
105112
System.out.println(SUCCESS_STRING + available);
106113
}
114+
115+
/**
116+
* Retrieves the first available CPU core ID allowed for the current process on Linux.
117+
*
118+
* @return The first CPU ID in Cpus_allowed_list, or -1 if unavailable.
119+
*/
120+
static int getFirstAllowedCpu() {
121+
final String statusFile = "/proc/self/status";
122+
final String targetKey = "Cpus_allowed_list:";
123+
124+
try (BufferedReader br = new BufferedReader(new FileReader(statusFile))) {
125+
String line;
126+
while ((line = br.readLine()) != null) {
127+
// Look for the line starting with "Cpus_allowed_list:"
128+
if (line.startsWith(targetKey)) {
129+
// Extract the value part, e.g., "0-15,32-47" or "80,82,84"
130+
String listValue = line.substring(targetKey.length()).trim();
131+
if (listValue.isEmpty()) return -1;
132+
133+
// Get the first segment before any comma (e.g., "0-15" from "0-15,32")
134+
String firstSegment = listValue.split(",")[0];
135+
136+
// If it is a range (e.g., "80-159"), take the start number
137+
if (firstSegment.contains("-")) {
138+
return Integer.parseInt(firstSegment.split("-")[0]);
139+
} else {
140+
// If it is a single ID (e.g., "1"), parse it directly
141+
return Integer.parseInt(firstSegment);
142+
}
143+
}
144+
}
145+
} catch (IOException | NumberFormatException | ArrayIndexOutOfBoundsException e) {
146+
throw new RuntimeException("Failed to read or parse " + statusFile, e);
147+
}
148+
return -1;
149+
}
107150
}

0 commit comments

Comments
 (0)