Skip to content

Commit

Permalink
Add info contributor support for JDK 24's VirtualThreadSchedulerMXBean
Browse files Browse the repository at this point in the history
  • Loading branch information
panic08 committed Dec 25, 2024
1 parent 681d4c2 commit 1f1b9b8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
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();
}
}

}

0 comments on commit 1f1b9b8

Please sign in to comment.