Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions test/hotspot/jtreg/runtime/os/AvailableProcessors.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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<String> 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
Expand All @@ -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;
}
}