--- old/src/hotspot/share/runtime/os.cpp 2019-06-21 21:03:24.205706868 +0900 +++ new/src/hotspot/share/runtime/os.cpp 2019-06-21 21:03:24.112704418 +0900 @@ -362,8 +362,21 @@ case SIGBREAK: { // Check if the signal is a trigger to start the Attach Listener - in that // case don't print stack traces. - if (!DisableAttachMechanism && AttachListener::is_init_trigger()) { - continue; + if (!DisableAttachMechanism) { + // Attempt to transit state to AL_INITIALIZING. + AttachListenerState cur_state = AttachListener::transit_state(AL_INITIALIZING, AL_NOT_INITIALIZED); + if (cur_state == AL_INITIALIZING) { + // Attach Listener has been started to initialize. Ignore this signal. + continue; + } else if (cur_state == AL_NOT_INITIALIZED) { + // Start to initialize. + if (!AttachListener::is_init_trigger()) { + // Attach Listener could not be started. + // So we need to transit the state to AL_NOT_INITIALIZED. + AttachListener::set_state(AL_NOT_INITIALIZED); + } + continue; + } } // Print stack traces // Any SIGBREAK operations added here should make sure to flush --- old/src/hotspot/share/services/attachListener.cpp 2019-06-21 21:03:24.559716192 +0900 +++ new/src/hotspot/share/services/attachListener.cpp 2019-06-21 21:03:24.472713900 +0900 @@ -45,7 +45,7 @@ #include "utilities/debug.hpp" #include "utilities/formatBuffer.hpp" -volatile bool AttachListener::_initialized; +volatile AttachListenerState AttachListener::_state = AL_NOT_INITIALIZED; // Implementation of "properties" command. // @@ -372,6 +372,7 @@ "Should already be setup"); if (AttachListener::pd_init() != 0) { + AttachListener::set_state(AL_NOT_INITIALIZED); return; } AttachListener::set_initialized(); @@ -379,6 +380,7 @@ for (;;) { AttachOperation* op = AttachListener::dequeue(); if (op == NULL) { + AttachListener::set_state(AL_NOT_INITIALIZED); return; // dequeue failed or shutdown } @@ -422,6 +424,8 @@ // operation complete - send result and output to client op->complete(res, &st); } + + AttachListener::set_state(AL_NOT_INITIALIZED); } bool AttachListener::has_init_error(TRAPS) { @@ -445,6 +449,7 @@ const char thread_name[] = "Attach Listener"; Handle string = java_lang_String::create_from_str(thread_name, THREAD); if (has_init_error(THREAD)) { + set_state(AL_NOT_INITIALIZED); return; } @@ -456,6 +461,7 @@ string, THREAD); if (has_init_error(THREAD)) { + set_state(AL_NOT_INITIALIZED); return; } @@ -469,6 +475,7 @@ thread_oop, THREAD); if (has_init_error(THREAD)) { + set_state(AL_NOT_INITIALIZED); return; } --- old/src/hotspot/share/services/attachListener.hpp 2019-06-21 21:03:24.914725542 +0900 +++ new/src/hotspot/share/services/attachListener.hpp 2019-06-21 21:03:24.827723250 +0900 @@ -26,6 +26,8 @@ #define SHARE_SERVICES_ATTACHLISTENER_HPP #include "memory/allocation.hpp" +#include "metaprogramming/isRegisteredEnum.hpp" +#include "runtime/atomic.hpp" #include "utilities/debug.hpp" #include "utilities/globalDefinitions.hpp" #include "utilities/macros.hpp" @@ -49,6 +51,14 @@ AttachOperationFunction func; }; +enum AttachListenerState { + AL_NOT_INITIALIZED, + AL_INITIALIZING, + AL_INITIALIZED +}; + +template<> struct IsRegisteredEnum : public TrueType {}; + class AttachListener: AllStatic { public: static void vm_start() NOT_SERVICES_RETURN; @@ -67,12 +77,31 @@ #if !INCLUDE_SERVICES static bool is_attach_supported() { return false; } #else + private: - static volatile bool _initialized; + static volatile AttachListenerState _state; public: - static bool is_initialized() { return _initialized; } - static void set_initialized() { _initialized = true; } + static void set_state(AttachListenerState new_state) { + Atomic::store(new_state, &_state); + } + + static AttachListenerState get_state() { + return Atomic::load(&_state); + } + + static AttachListenerState transit_state(AttachListenerState new_state, + AttachListenerState cmp_state) { + return Atomic::cmpxchg(new_state, &_state, cmp_state); + } + + static bool is_initialized() { + return Atomic::load(&_state) == AL_INITIALIZED; + } + + static void set_initialized() { + Atomic::store(AL_INITIALIZED, &_state); + } // indicates if this VM supports attach-on-demand static bool is_attach_supported() { return !DisableAttachMechanism; } --- /dev/null 2019-06-21 20:58:38.514742500 +0900 +++ new/test/hotspot/jtreg/serviceability/attach/ConcAttachTest.java 2019-06-21 21:03:25.172732337 +0900 @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8225690 + * @library /test/lib + * @modules jdk.attach/com.sun.tools.attach + * @run main ConcAttachTest + */ + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; + +import com.sun.tools.attach.VirtualMachine; +import com.sun.tools.attach.AttachNotSupportedException; + +import jdk.test.lib.apps.LingeredApp; +import jdk.test.lib.Asserts; +import jdk.test.lib.JDKToolLauncher; +import jdk.test.lib.process.OutputAnalyzer; + +public class ConcAttachTest implements Runnable { + + private static final int NUM_CONC_REQUESTS = 100; + + private static final int THREAD_POOL_TIMEOUT_IN_SEC = 30; + + private static CountDownLatch latch; + + private static String strPID; + + // Attach to LingeredApp concurrently. + public void run() { + VirtualMachine vm = null; + + try { + latch.countDown(); + latch.await(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + try { + vm = VirtualMachine.attach(strPID); + } catch (AttachNotSupportedException | IOException e) { + throw new RuntimeException(e); + } finally { + try { + vm.detach(); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private static void checkAttachListenerThread() throws InterruptedException, IOException { + JDKToolLauncher jcmd = JDKToolLauncher.createUsingTestJDK("jcmd"); + jcmd.addToolArg(strPID); + jcmd.addToolArg("Thread.print"); + + ProcessBuilder pb = new ProcessBuilder(jcmd.getCommand()); + Process jcmdProc = pb.start(); + + OutputAnalyzer out = new OutputAnalyzer(jcmdProc); + + jcmdProc.waitFor(); + + System.out.println(out.getStdout()); + System.err.println(out.getStderr()); + + long numOfAttachListener = out.asLines() + .stream() + .filter(l -> l.contains("Attach Listener")) + .count(); + + Asserts.assertEquals(1L, numOfAttachListener, "AttachListener should exist only 1 thread."); + } + + public static void main(String... args) throws Exception { + LingeredApp app = null; + latch = new CountDownLatch(NUM_CONC_REQUESTS); + ExecutorService pool = Executors.newFixedThreadPool(NUM_CONC_REQUESTS); + + try { + app = LingeredApp.startApp(); + strPID = Long.toString(app.getPid()); + + for (int i = 0; i < NUM_CONC_REQUESTS; i++) { + pool.submit(new ConcAttachTest()); + } + + pool.shutdown(); + pool.awaitTermination(THREAD_POOL_TIMEOUT_IN_SEC, TimeUnit.SECONDS); + + checkAttachListenerThread(); + } finally { + LingeredApp.stopApp(app); + } + } + +}