Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add info contributor support for JDK 24's VirtualThreadSchedulerMXBean #43594

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Information about the process of the application.
*
* @author Jonatan Ivanov
* @author Andrey Litvitski
* @since 3.3.0
*/
public class ProcessInfo {
Expand Down Expand Up @@ -72,6 +73,29 @@ public MemoryInfo getMemory() {
return new MemoryInfo();
}

/**
* Virtual threads information for the process. These values provide details about the
* current state of virtual threads, including the number of mounted threads, queued threads,
* the parallelism level, and the thread pool size.
* @return an instance of {@link VirtualThreadsInfo} containing information about virtual threads,
* or {@code null} if the VirtualThreadSchedulerMXBean is not available.
* @since 3.5.0
*/
public VirtualThreadsInfo getVirtualThreads() {
try {
Class mxBeanClass = Class.forName("jdk.management.VirtualThreadSchedulerMXBean");
Object bean = ManagementFactory.getPlatformMXBean(mxBeanClass);
return new VirtualThreadsInfo(
(Integer) mxBeanClass.getMethod("getMountedVirtualThreadCount").invoke(bean),
(Long) mxBeanClass.getMethod("getQueuedVirtualThreadCount").invoke(bean),
(Integer) mxBeanClass.getMethod("getParallelism").invoke(bean),
(Integer) mxBeanClass.getMethod("getPoolSize").invoke(bean)
);
} catch (ReflectiveOperationException e) {
return null;
}
}

public long getPid() {
return this.pid;
}
Expand All @@ -84,6 +108,46 @@ public String getOwner() {
return this.owner;
}

/**
* Virtual threads information.
*
* @since 3.5.0
*/
public static class VirtualThreadsInfo {

private final int mounted;

private final long queued;

private final int parallelism;

private final int poolSize;

public VirtualThreadsInfo(int mounted, long queued, int parallelism, int poolSize) {
this.mounted = mounted;
this.queued = queued;
this.parallelism = parallelism;
this.poolSize = poolSize;
}

public long getMounted() {
return this.mounted;
}

public long getQueued() {
return this.queued;
}

public int getParallelism() {
return this.parallelism;
}

public int getPoolSize() {
return this.poolSize;
}

}

/**
* Memory information.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import org.junit.jupiter.api.Test;

import org.springframework.boot.info.ProcessInfo.MemoryInfo.MemoryUsageInfo;
import org.springframework.boot.info.ProcessInfo.VirtualThreadsInfo;
import org.springframework.util.ClassUtils;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for {@link ProcessInfo}.
*
* @author Jonatan Ivanov
* @author Andrey Litvitski
*/
class ProcessInfoTests {

Expand Down Expand Up @@ -54,4 +57,23 @@ void memoryInfoIsAvailable() {
assertThat(nonHeapUsageInfo.getMax()).isEqualTo(-1);
}

@Test
void virtualThreadsInfoIsNullWhenMXBeanIsNotAccessible() {
if (ClassUtils.isPresent("jdk.management.VirtualThreadSchedulerMXBean", null)) {
ProcessInfo processInfo = new ProcessInfo();

VirtualThreadsInfo virtualThreadsInfo = processInfo.getVirtualThreads();

assertThat(virtualThreadsInfo).isNotNull();
assertThat(virtualThreadsInfo.getMounted()).isGreaterThanOrEqualTo(0);
assertThat(virtualThreadsInfo.getQueued()).isGreaterThanOrEqualTo(0);
assertThat(virtualThreadsInfo.getParallelism()).isGreaterThan(0);
assertThat(virtualThreadsInfo.getPoolSize()).isGreaterThan(0);
} else {
ProcessInfo processInfo = new ProcessInfo();

assertThat(processInfo.getVirtualThreads()).isNull();
}
}

}