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 22, 2024
1 parent 681d4c2 commit 6edc540
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.PlatformManagedObject;

/**
* 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 +74,34 @@ 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.4.2
*/
public VirtualThreadsInfo getVirtualThreads() {
try {
@SuppressWarnings("unchecked")
Class<? extends PlatformManagedObject> mxBeanClass =
(Class<? extends PlatformManagedObject>) 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 +114,46 @@ public String getOwner() {
return this.owner;
}

/**
* Virtual threads information.
*
* @since 3.4.2
*/
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 @@ -16,16 +16,21 @@

package org.springframework.boot.info;

import java.lang.management.ManagementFactory;
import java.lang.management.PlatformManagedObject;

import org.junit.jupiter.api.Test;

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

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 +59,28 @@ void memoryInfoIsAvailable() {
assertThat(nonHeapUsageInfo.getMax()).isEqualTo(-1);
}

@Test
void virtualThreadsInfoIsNullWhenMXBeanIsNotAccessible() {
if (!isVirtualThreadMXBeanAvailable()) {
ProcessInfo processInfo = new ProcessInfo();

VirtualThreadsInfo virtualThreadsInfo = processInfo.getVirtualThreads();

assertThat(virtualThreadsInfo).isNull();
}
}

private boolean isVirtualThreadMXBeanAvailable() {
try {
@SuppressWarnings("unchecked")
Class<? extends PlatformManagedObject> mxBeanClass =
(Class<? extends PlatformManagedObject>) Class.forName("jdk.management.VirtualThreadSchedulerMXBean");

ManagementFactory.getPlatformMXBean(mxBeanClass);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}

}

0 comments on commit 6edc540

Please sign in to comment.