1
+ /*
2
+ * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
+ *
5
+ * This code is free software; you can redistribute it and/or modify it
6
+ * under the terms of the GNU General Public License version 2 only, as
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
+
52
+ /**
53
+ * Base implementation of VirtualThreadSchedulerMXBean.
54
+ */
55
+ private abstract static class BaseVirtualThreadSchedulerImpl
56
+ implements VirtualThreadSchedulerMXBean {
57
+
58
+ abstract void implSetParallelism (int size );
59
+
60
+ @ Override
61
+ public final void setParallelism (int size ) {
62
+ Util .checkControlAccess ();
63
+ implSetParallelism (size );
64
+ }
65
+
66
+ @ Override
67
+ public final ObjectName getObjectName () {
68
+ return Util .newObjectName ("jdk.management:type=VirtualThreadScheduler" );
69
+ }
70
+
71
+ @ Override
72
+ public String toString () {
73
+ var sb = new StringBuilder ("[parallelism=" );
74
+ sb .append (getParallelism ());
75
+ append (sb , "size" , getPoolSize ());
76
+ append (sb , "mounted" , getMountedVirtualThreadCount ());
77
+ append (sb , "queued" , getQueuedVirtualThreadCount ());
78
+ sb .append (']' );
79
+ return sb .toString ();
80
+ }
81
+
82
+ private void append (StringBuilder sb , String name , long value ) {
83
+ sb .append (", " ).append (name ).append ('=' );
84
+ if (value >= 0 ) {
85
+ sb .append (value );
86
+ } else {
87
+ sb .append ("<unavailable>" );
88
+ }
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Implementation of VirtualThreadSchedulerMXBean when virtual threads are
94
+ * implemented with continuations + scheduler.
95
+ */
96
+ private static final class VirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
97
+ /**
98
+ * Holder class for scheduler.
99
+ */
100
+ private static class Scheduler {
101
+ private static final Executor scheduler =
102
+ SharedSecrets .getJavaLangAccess ().virtualThreadDefaultScheduler ();
103
+ static Executor instance () {
104
+ return scheduler ;
105
+ }
106
+ }
107
+
108
+ @ Override
109
+ public int getParallelism () {
110
+ if (Scheduler .instance () instanceof ForkJoinPool pool ) {
111
+ return pool .getParallelism ();
112
+ }
113
+ throw new InternalError (); // should not get here
114
+ }
115
+
116
+ @ Override
117
+ void implSetParallelism (int size ) {
118
+ if (Scheduler .instance () instanceof ForkJoinPool pool ) {
119
+ pool .setParallelism (size );
120
+ if (pool .getPoolSize () < size ) {
121
+ // FJ worker thread creation is on-demand
122
+ Thread .startVirtualThread (() -> { });
123
+ }
124
+
125
+ return ;
126
+ }
127
+ throw new UnsupportedOperationException (); // should not get here
128
+ }
129
+
130
+ @ Override
131
+ public int getPoolSize () {
132
+ if (Scheduler .instance () instanceof ForkJoinPool pool ) {
133
+ return pool .getPoolSize ();
134
+ }
135
+ return -1 ; // should not get here
136
+ }
137
+
138
+ @ Override
139
+ public int getMountedVirtualThreadCount () {
140
+ if (Scheduler .instance () instanceof ForkJoinPool pool ) {
141
+ return pool .getActiveThreadCount ();
142
+ }
143
+ return -1 ; // should not get here
144
+ }
145
+
146
+ @ Override
147
+ public long getQueuedVirtualThreadCount () {
148
+ if (Scheduler .instance () instanceof ForkJoinPool pool ) {
149
+ return pool .getQueuedTaskCount () + pool .getQueuedSubmissionCount ();
150
+ }
151
+ return -1L ; // should not get here
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Implementation of VirtualThreadSchedulerMXBean when virtual threads are backed
157
+ * by platform threads.
158
+ */
159
+ private static final class BoundVirtualThreadSchedulerImpl extends BaseVirtualThreadSchedulerImpl {
160
+ @ Override
161
+ public int getParallelism () {
162
+ return Integer .MAX_VALUE ;
163
+ }
164
+
165
+ @ Override
166
+ void implSetParallelism (int size ) {
167
+ throw new UnsupportedOperationException ();
168
+ }
169
+
170
+ @ Override
171
+ public int getPoolSize () {
172
+ return -1 ;
173
+ }
174
+
175
+ @ Override
176
+ public int getMountedVirtualThreadCount () {
177
+ return -1 ;
178
+ }
179
+
180
+ @ Override
181
+ public long getQueuedVirtualThreadCount () {
182
+ return -1L ;
183
+ }
184
+ }
185
+ }
1 commit comments
openjdk-notifier[bot] commentedon Sep 10, 2024
Review
Issues