|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package com.oracle.truffle.espresso.blocking; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | +import java.util.concurrent.ExecutionException; |
| 31 | +import java.util.concurrent.Future; |
| 32 | + |
| 33 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 34 | +import com.oracle.truffle.api.ThreadLocalAction; |
| 35 | +import com.oracle.truffle.api.TruffleSafepoint; |
| 36 | +import com.oracle.truffle.api.nodes.Node; |
| 37 | +import com.oracle.truffle.espresso.impl.SuppressFBWarnings; |
| 38 | +import com.oracle.truffle.espresso.meta.EspressoError; |
| 39 | +import com.oracle.truffle.espresso.runtime.EspressoContext; |
| 40 | +import com.oracle.truffle.espresso.runtime.staticobject.StaticObject; |
| 41 | +import com.oracle.truffle.espresso.threads.ThreadAccess; |
| 42 | +import com.oracle.truffle.espresso.vm.InterpreterToVM; |
| 43 | +import com.oracle.truffle.espresso.vm.VM; |
| 44 | + |
| 45 | +public final class ThreadRequests { |
| 46 | + private static final Thread[] EMPTY_THREAD_ARRAY = new Thread[0]; |
| 47 | + |
| 48 | + public abstract static class Request<T> extends ThreadLocalAction { |
| 49 | + private final Map<Thread, T> result; |
| 50 | + |
| 51 | + /** |
| 52 | + * Performs the request on the given thread, and return the result of that action. |
| 53 | + * <p> |
| 54 | + * If this returns {@code null}, the |
| 55 | + * {@link #request(EspressoContext, Request, Node, StaticObject[], Object[]) request} will |
| 56 | + * use {@link #placeHolderValue()} for its result. |
| 57 | + */ |
| 58 | + public abstract T action(Thread current); |
| 59 | + |
| 60 | + /** |
| 61 | + * The placeholder value for threads that are unresponsive. |
| 62 | + * <p> |
| 63 | + * Responsive threads for which {@link #action(Thread)} returns {@code null} will also use |
| 64 | + * this value as their result. |
| 65 | + */ |
| 66 | + public T placeHolderValue() { |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + @TruffleBoundary |
| 71 | + public Request(boolean hasSideEffects, boolean synchronous) { |
| 72 | + super(hasSideEffects, synchronous); |
| 73 | + this.result = new ConcurrentHashMap<>(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected final void perform(Access access) { |
| 78 | + T value = action(access.getThread()); |
| 79 | + if (value != null) { |
| 80 | + result.put(access.getThread(), value); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + final Map<Thread, T> result() { |
| 85 | + return result; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static VM.StackTrace[] getStackTraces(EspressoContext ctx, int maxDepth, Node location, StaticObject... threads) { |
| 90 | + StaticObject[] threadList = getThreadList(ctx, threads); |
| 91 | + VM.StackTrace[] result = new VM.StackTrace[threadList.length]; |
| 92 | + request(ctx, new GetStackTrace(maxDepth), location, threadList, result); |
| 93 | + return result; |
| 94 | + } |
| 95 | + |
| 96 | + public static StaticObject[] findDeadlocks(EspressoContext ctx, boolean monitorsOnly, Node location, StaticObject... threads) { |
| 97 | + StaticObject[] threadList = getThreadList(ctx, threads); |
| 98 | + FindDeadLocks findDeadLocks = new FindDeadLocks(ctx, Thread.currentThread(), monitorsOnly); |
| 99 | + request(ctx, findDeadLocks, location, threadList, new Void[threadList.length]); |
| 100 | + return findDeadLocks.getDeadLockedThreads(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * From the given {@code threads} array, finds the threads which are |
| 105 | + * {@link ThreadAccess#isResponsive(StaticObject) responsive}, then submits the {@code request} |
| 106 | + * as a {@link ThreadLocalAction} only on these responsive threads. |
| 107 | + * <p> |
| 108 | + * The {@code result} array will then be filled with the result of the request, with |
| 109 | + * {@code result[i]} corresponding to the result of running the action on the thread in |
| 110 | + * {@code threads[i]}. |
| 111 | + * <p> |
| 112 | + * The passed {@code threads} array must be non-null. Consider using |
| 113 | + * {@link EspressoContext#getActiveThreads()} for requesting on all threads. |
| 114 | + */ |
| 115 | + @TruffleBoundary |
| 116 | + public static <T> void request(EspressoContext ctx, Request<T> request, Node location, StaticObject[] threads, T[] result) { |
| 117 | + Objects.requireNonNull(threads); |
| 118 | + Objects.requireNonNull(result); |
| 119 | + if (threads.length != result.length) { |
| 120 | + throw EspressoError.shouldNotReachHere("Wrong usage of ThreadRequests.request"); |
| 121 | + } |
| 122 | + try { |
| 123 | + // Prevent responsive threads from entering native. |
| 124 | + freeze(ctx, threads, true); |
| 125 | + |
| 126 | + // Filter to remove unresponsive threads. |
| 127 | + ThreadAccess access = ctx.getThreadAccess(); |
| 128 | + List<Thread> running = new ArrayList<>(); |
| 129 | + |
| 130 | + for (int i = 0; i < threads.length; i++) { |
| 131 | + StaticObject t = threads[i]; |
| 132 | + if (t == access.getCurrentGuestThread() || // current thread is always responsive. |
| 133 | + StaticObject.notNull(t) && isResponsive(access, t)) { |
| 134 | + running.add(access.getHost(t)); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Submit the request to responsive threads, and wait for completion. |
| 139 | + Future<Void> future = ctx.getEnv().submitThreadLocal(running.toArray(EMPTY_THREAD_ARRAY), request); |
| 140 | + TruffleSafepoint.setBlockedThreadInterruptible(location, f -> { |
| 141 | + try { |
| 142 | + future.get(); |
| 143 | + } catch (ExecutionException e) { |
| 144 | + throw EspressoError.shouldNotReachHere(e); |
| 145 | + } |
| 146 | + }, future); |
| 147 | + |
| 148 | + // Build the result map. |
| 149 | + Map<Thread, T> tlaResult = request.result(); |
| 150 | + for (int i = 0; i < threads.length; i++) { |
| 151 | + StaticObject t = threads[i]; |
| 152 | + if (StaticObject.notNull(t)) { |
| 153 | + Thread host = access.getHost(t); |
| 154 | + if (tlaResult.containsKey(host)) { |
| 155 | + result[i] = tlaResult.get(host); |
| 156 | + continue; |
| 157 | + } |
| 158 | + } |
| 159 | + result[i] = request.placeHolderValue(); |
| 160 | + } |
| 161 | + } finally { |
| 162 | + // Re-allow threads to enter native. |
| 163 | + freeze(ctx, threads, false); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private static StaticObject[] getThreadList(EspressoContext ctx, StaticObject[] threads) { |
| 168 | + StaticObject[] threadList = threads; |
| 169 | + if (threadList == null) { |
| 170 | + threadList = ctx.getActiveThreads(); |
| 171 | + } |
| 172 | + return threadList; |
| 173 | + } |
| 174 | + |
| 175 | + private static boolean isResponsive(ThreadAccess access, StaticObject thread) { |
| 176 | + return StaticObject.notNull(thread) && |
| 177 | + access.isAlive(thread) && |
| 178 | + !access.isVirtualThread(thread) && |
| 179 | + access.isResponsive(thread); |
| 180 | + } |
| 181 | + |
| 182 | + @SuppressFBWarnings(value = {"IMSE"}, justification = "Not dubious, may happen if failed to lock all native transitions.") |
| 183 | + private static void freeze(EspressoContext ctx, StaticObject[] threads, boolean block) { |
| 184 | + for (StaticObject t : threads) { |
| 185 | + if (StaticObject.notNull(t)) { |
| 186 | + try { |
| 187 | + ctx.getThreadAccess().blockNativeTransitions(t, block); |
| 188 | + } catch (IllegalMonitorStateException e) { |
| 189 | + // This may happen on unlock if a safepoint threw while we were locking at the |
| 190 | + // start of the request. |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + private static final class GetStackTrace extends Request<VM.StackTrace> { |
| 197 | + GetStackTrace(int maxDepth) { |
| 198 | + super(false, false); |
| 199 | + this.maxDepth = maxDepth; |
| 200 | + } |
| 201 | + |
| 202 | + private final int maxDepth; |
| 203 | + |
| 204 | + @Override |
| 205 | + public VM.StackTrace action(Thread current) { |
| 206 | + return InterpreterToVM.getStackTrace(InterpreterToVM.DefaultHiddenFramesFilter.INSTANCE, maxDepth); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private static final class FindDeadLocks extends Request<Void> { |
| 211 | + FindDeadLocks(EspressoContext ctx, Thread initiatingThread, boolean monitorsOnly) { |
| 212 | + super(false, true); |
| 213 | + this.context = ctx; |
| 214 | + this.initiatingThread = initiatingThread; |
| 215 | + this.monitorsOnly = monitorsOnly; |
| 216 | + } |
| 217 | + |
| 218 | + private final EspressoContext context; |
| 219 | + private final Thread initiatingThread; |
| 220 | + private final boolean monitorsOnly; |
| 221 | + |
| 222 | + private volatile StaticObject[] deadLockedThreads = null; |
| 223 | + |
| 224 | + @Override |
| 225 | + public Void action(Thread current) { |
| 226 | + if (current == initiatingThread) { |
| 227 | + deadLockedThreads = context.getEspressoEnv().getThreadRegistry().findDeadlocks(monitorsOnly); |
| 228 | + } |
| 229 | + return null; |
| 230 | + } |
| 231 | + |
| 232 | + public StaticObject[] getDeadLockedThreads() { |
| 233 | + return deadLockedThreads; |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments