|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 jdk.internal.vm; |
| 26 | + |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.List; |
| 29 | +import java.util.stream.IntStream; |
| 30 | +import jdk.internal.access.JavaLangAccess; |
| 31 | +import jdk.internal.access.SharedSecrets; |
| 32 | +import sun.nio.ch.Poller; |
| 33 | + |
| 34 | +/** |
| 35 | + * The implementation for the jcmd Thread.vthread_* diagnostic commands. These methods are |
| 36 | + * called from the "Attach Listener" thread. |
| 37 | + */ |
| 38 | +public class JcmdVThreadCommands { |
| 39 | + private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess(); |
| 40 | + |
| 41 | + private JcmdVThreadCommands() { } |
| 42 | + |
| 43 | + /** |
| 44 | + * Invoked by the VM to print the virtual scheduler to a byte[]. |
| 45 | + */ |
| 46 | + private static byte[] printScheduler() { |
| 47 | + StringBuilder sb = new StringBuilder(); |
| 48 | + |
| 49 | + // virtual thread scheduler |
| 50 | + sb.append(JLA.virtualThreadDefaultScheduler()) |
| 51 | + .append(System.lineSeparator()); |
| 52 | + |
| 53 | + // break |
| 54 | + sb.append(System.lineSeparator()); |
| 55 | + |
| 56 | + // delayed task schedulers |
| 57 | + sb.append("Delayed task schedulers:").append(System.lineSeparator()); |
| 58 | + var delayedTaskSchedulers = JLA.virtualThreadDelayedTaskSchedulers().toList(); |
| 59 | + IntStream.range(0, delayedTaskSchedulers.size()) |
| 60 | + .forEach(i -> sb.append('[') |
| 61 | + .append(i) |
| 62 | + .append("] ") |
| 63 | + .append(delayedTaskSchedulers.get(i)) |
| 64 | + .append(System.lineSeparator())); |
| 65 | + |
| 66 | + return sb.toString().getBytes(StandardCharsets.UTF_8); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Invoked by the VM to print the I/O pollers to a byte[]. |
| 71 | + */ |
| 72 | + private static byte[] printPollers() { |
| 73 | + StringBuilder sb = new StringBuilder(); |
| 74 | + |
| 75 | + Poller masterPoller = Poller.masterPoller(); |
| 76 | + List<Poller> readPollers = Poller.readPollers(); |
| 77 | + List<Poller> writePollers = Poller.writePollers(); |
| 78 | + |
| 79 | + if (masterPoller != null) { |
| 80 | + sb.append("Master I/O poller:") |
| 81 | + .append(System.lineSeparator()) |
| 82 | + .append(masterPoller) |
| 83 | + .append(System.lineSeparator()); |
| 84 | + |
| 85 | + // break |
| 86 | + sb.append(System.lineSeparator()); |
| 87 | + } |
| 88 | + |
| 89 | + sb.append("Read I/O pollers:"); |
| 90 | + sb.append(System.lineSeparator()); |
| 91 | + IntStream.range(0, readPollers.size()) |
| 92 | + .forEach(i -> sb.append('[') |
| 93 | + .append(i) |
| 94 | + .append("] ") |
| 95 | + .append(readPollers.get(i)) |
| 96 | + .append(System.lineSeparator())); |
| 97 | + |
| 98 | + // break |
| 99 | + sb.append(System.lineSeparator()); |
| 100 | + |
| 101 | + sb.append("Write I/O pollers:"); |
| 102 | + sb.append(System.lineSeparator()); |
| 103 | + IntStream.range(0, writePollers.size()) |
| 104 | + .forEach(i -> sb.append('[') |
| 105 | + .append(i) |
| 106 | + .append("] ") |
| 107 | + .append(writePollers.get(i)) |
| 108 | + .append(System.lineSeparator())); |
| 109 | + |
| 110 | + return sb.toString().getBytes(StandardCharsets.UTF_8); |
| 111 | + } |
| 112 | +} |
0 commit comments