7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.management.internal;
26
27 import java.util.concurrent.Executor;
28 import java.util.concurrent.ForkJoinPool;
29 import javax.management.ObjectName;
30 import jdk.management.VirtualThreadSchedulerMXBean;
31 import jdk.internal.access.JavaLangAccess;
32 import jdk.internal.access.SharedSecrets;
33 import jdk.internal.vm.ContinuationSupport;
34 import sun.management.Util;
35
36 /**
37 * Provides the implementation of the management interface for the JDK's default virtual
38 * thread scheduler.
39 */
40 public class VirtualThreadSchedulerImpls {
41 private VirtualThreadSchedulerImpls() {
42 }
43
44 public static VirtualThreadSchedulerMXBean create() {
45 if (ContinuationSupport.isSupported()) {
46 return new VirtualThreadSchedulerImpl();
47 } else {
48 return new BoundVirtualThreadSchedulerImpl();
49 }
50 }
51
73
74 private void append(StringBuilder sb, String name, long value) {
75 sb.append(", ").append(name).append('=');
76 if (value >= 0) {
77 sb.append(value);
78 } else {
79 sb.append("<unavailable>");
80 }
81 }
82 }
83
84 /**
85 * Implementation of VirtualThreadSchedulerMXBean when virtual threads are
86 * implemented with continuations + scheduler.
87 */
88 private static final class VirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
89 /**
90 * Holder class for scheduler.
91 */
92 private static class Scheduler {
93 private static final Executor scheduler =
94 SharedSecrets.getJavaLangAccess().virtualThreadDefaultScheduler();
95 static Executor instance() {
96 return scheduler;
97 }
98 }
99
100 @Override
101 public int getParallelism() {
102 if (Scheduler.instance() instanceof ForkJoinPool pool) {
103 return pool.getParallelism();
104 }
105 throw new InternalError(); // should not get here
106 }
107
108 @Override
109 public void setParallelism(int size) {
110 if (Scheduler.instance() instanceof ForkJoinPool pool) {
111 pool.setParallelism(size);
112 if (pool.getPoolSize() < size) {
113 // FJ worker thread creation is on-demand
114 Thread.startVirtualThread(() -> { });
115 }
116
117 return;
118 }
119 throw new UnsupportedOperationException(); // should not get here
120 }
121
122 @Override
123 public int getPoolSize() {
124 if (Scheduler.instance() instanceof ForkJoinPool pool) {
125 return pool.getPoolSize();
126 }
127 return -1; // should not get here
128 }
129
130 @Override
131 public int getMountedVirtualThreadCount() {
132 if (Scheduler.instance() instanceof ForkJoinPool pool) {
133 return pool.getActiveThreadCount();
134 }
135 return -1; // should not get here
136 }
137
138 @Override
139 public long getQueuedVirtualThreadCount() {
140 if (Scheduler.instance() instanceof ForkJoinPool pool) {
141 return pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount();
142 }
143 return -1L; // should not get here
144 }
145 }
146
147 /**
148 * Implementation of VirtualThreadSchedulerMXBean when virtual threads are backed
149 * by platform threads.
150 */
151 private static final class BoundVirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
152 @Override
153 public int getParallelism() {
154 return Integer.MAX_VALUE;
155 }
156
157 @Override
158 public void setParallelism(int size) {
159 throw new UnsupportedOperationException();
160 }
161
162 @Override
163 public int getPoolSize() {
|
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package com.sun.management.internal;
26
27 import java.util.concurrent.ForkJoinPool;
28 import javax.management.ObjectName;
29 import jdk.management.VirtualThreadSchedulerMXBean;
30 import jdk.internal.access.SharedSecrets;
31 import jdk.internal.vm.ContinuationSupport;
32 import sun.management.Util;
33
34 /**
35 * Provides the implementation of the management interface for the JDK's default virtual
36 * thread scheduler.
37 */
38 public class VirtualThreadSchedulerImpls {
39 private VirtualThreadSchedulerImpls() {
40 }
41
42 public static VirtualThreadSchedulerMXBean create() {
43 if (ContinuationSupport.isSupported()) {
44 return new VirtualThreadSchedulerImpl();
45 } else {
46 return new BoundVirtualThreadSchedulerImpl();
47 }
48 }
49
71
72 private void append(StringBuilder sb, String name, long value) {
73 sb.append(", ").append(name).append('=');
74 if (value >= 0) {
75 sb.append(value);
76 } else {
77 sb.append("<unavailable>");
78 }
79 }
80 }
81
82 /**
83 * Implementation of VirtualThreadSchedulerMXBean when virtual threads are
84 * implemented with continuations + scheduler.
85 */
86 private static final class VirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
87 /**
88 * Holder class for scheduler.
89 */
90 private static class Scheduler {
91 private static final Thread.VirtualThreadScheduler SCHEDULER =
92 SharedSecrets.getJavaLangAccess().defaultVirtualThreadScheduler();
93 static Thread.VirtualThreadScheduler instance() {
94 return SCHEDULER;
95 }
96 }
97
98 @Override
99 public int getParallelism() {
100 if (Scheduler.instance() instanceof ForkJoinPool pool) {
101 return pool.getParallelism();
102 }
103 return -1; // unknown
104 }
105
106 @Override
107 public void setParallelism(int size) {
108 if (Scheduler.instance() instanceof ForkJoinPool pool) {
109 pool.setParallelism(size);
110 if (pool.getPoolSize() < size) {
111 // FJ worker thread creation is on-demand
112 Thread.startVirtualThread(() -> { });
113 }
114 return;
115 }
116 throw new UnsupportedOperationException();
117 }
118
119 @Override
120 public int getPoolSize() {
121 if (Scheduler.instance() instanceof ForkJoinPool pool) {
122 return pool.getPoolSize();
123 }
124 return -1; // unknown
125 }
126
127 @Override
128 public int getMountedVirtualThreadCount() {
129 if (Scheduler.instance() instanceof ForkJoinPool pool) {
130 return pool.getActiveThreadCount();
131 }
132 return -1; // unknown
133 }
134
135 @Override
136 public long getQueuedVirtualThreadCount() {
137 if (Scheduler.instance() instanceof ForkJoinPool pool) {
138 return pool.getQueuedTaskCount() + pool.getQueuedSubmissionCount();
139 }
140 return -1L; // unknown
141 }
142 }
143
144 /**
145 * Implementation of VirtualThreadSchedulerMXBean when virtual threads are backed
146 * by platform threads.
147 */
148 private static final class BoundVirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
149 @Override
150 public int getParallelism() {
151 return Integer.MAX_VALUE;
152 }
153
154 @Override
155 public void setParallelism(int size) {
156 throw new UnsupportedOperationException();
157 }
158
159 @Override
160 public int getPoolSize() {
|