Wed, 17 Jun 2020 11:43:05 +0300
8220293: Deadlock in JFR string pool
Reviewed-by: rehn, egahlin
apetushkov@9858 | 1 | /* |
apetushkov@9928 | 2 | * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
apetushkov@9858 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
apetushkov@9858 | 4 | * |
apetushkov@9858 | 5 | * This code is free software; you can redistribute it and/or modify it |
apetushkov@9858 | 6 | * under the terms of the GNU General Public License version 2 only, as |
apetushkov@9858 | 7 | * published by the Free Software Foundation. |
apetushkov@9858 | 8 | * |
apetushkov@9858 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
apetushkov@9858 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
apetushkov@9858 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
apetushkov@9858 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
apetushkov@9858 | 13 | * accompanied this code). |
apetushkov@9858 | 14 | * |
apetushkov@9858 | 15 | * You should have received a copy of the GNU General Public License version |
apetushkov@9858 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
apetushkov@9858 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
apetushkov@9858 | 18 | * |
apetushkov@9858 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
apetushkov@9858 | 20 | * or visit www.oracle.com if you need additional information or have any |
apetushkov@9858 | 21 | * questions. |
apetushkov@9858 | 22 | * |
apetushkov@9858 | 23 | */ |
apetushkov@9858 | 24 | |
apetushkov@9858 | 25 | #include "precompiled.hpp" |
apetushkov@9858 | 26 | #include "classfile/javaClasses.hpp" |
apetushkov@9858 | 27 | #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp" |
apetushkov@9858 | 28 | #include "jfr/recorder/service/jfrOptionSet.hpp" |
apetushkov@9858 | 29 | #include "jfr/recorder/storage/jfrMemorySpace.inline.hpp" |
apetushkov@9858 | 30 | #include "jfr/recorder/repository/jfrChunkWriter.hpp" |
apetushkov@9858 | 31 | #include "jfr/recorder/storage/jfrStorageUtils.inline.hpp" |
apetushkov@9858 | 32 | #include "jfr/recorder/stringpool/jfrStringPool.hpp" |
apetushkov@9858 | 33 | #include "jfr/recorder/stringpool/jfrStringPoolWriter.hpp" |
apetushkov@9858 | 34 | #include "jfr/utilities/jfrTypes.hpp" |
apetushkov@9858 | 35 | #include "runtime/atomic.hpp" |
apetushkov@9858 | 36 | #include "runtime/mutexLocker.hpp" |
apetushkov@9858 | 37 | #include "runtime/orderAccess.hpp" |
apetushkov@9858 | 38 | #include "runtime/safepoint.hpp" |
apetushkov@9858 | 39 | #include "runtime/thread.inline.hpp" |
apetushkov@9858 | 40 | |
apetushkov@9858 | 41 | typedef JfrStringPool::Buffer* BufferPtr; |
apetushkov@9858 | 42 | |
apetushkov@9858 | 43 | static JfrStringPool* _instance = NULL; |
apetushkov@9858 | 44 | |
apetushkov@9858 | 45 | JfrStringPool& JfrStringPool::instance() { |
apetushkov@9858 | 46 | return *_instance; |
apetushkov@9858 | 47 | } |
apetushkov@9858 | 48 | |
apetushkov@9858 | 49 | JfrStringPool* JfrStringPool::create(JfrChunkWriter& cw) { |
apetushkov@9858 | 50 | assert(_instance == NULL, "invariant"); |
apetushkov@9858 | 51 | _instance = new JfrStringPool(cw); |
apetushkov@9858 | 52 | return _instance; |
apetushkov@9858 | 53 | } |
apetushkov@9858 | 54 | |
apetushkov@9858 | 55 | void JfrStringPool::destroy() { |
apetushkov@9858 | 56 | assert(_instance != NULL, "invariant"); |
apetushkov@9858 | 57 | delete _instance; |
apetushkov@9858 | 58 | _instance = NULL; |
apetushkov@9858 | 59 | } |
apetushkov@9858 | 60 | |
apetushkov@9858 | 61 | JfrStringPool::JfrStringPool(JfrChunkWriter& cw) : _free_list_mspace(NULL), _lock(NULL), _chunkwriter(cw) {} |
apetushkov@9858 | 62 | |
apetushkov@9858 | 63 | JfrStringPool::~JfrStringPool() { |
apetushkov@9858 | 64 | if (_free_list_mspace != NULL) { |
apetushkov@9858 | 65 | delete _free_list_mspace; |
apetushkov@9858 | 66 | } |
apetushkov@9858 | 67 | if (_lock != NULL) { |
apetushkov@9858 | 68 | delete _lock; |
apetushkov@9858 | 69 | } |
apetushkov@9858 | 70 | } |
apetushkov@9858 | 71 | |
apetushkov@9858 | 72 | static const size_t unlimited_mspace_size = 0; |
apetushkov@9858 | 73 | static const size_t string_pool_cache_count = 2; |
apetushkov@9858 | 74 | static const size_t string_pool_buffer_size = 512 * K; |
apetushkov@9858 | 75 | |
apetushkov@9858 | 76 | bool JfrStringPool::initialize() { |
apetushkov@9858 | 77 | assert(_free_list_mspace == NULL, "invariant"); |
apetushkov@9858 | 78 | _free_list_mspace = new JfrStringPoolMspace(string_pool_buffer_size, unlimited_mspace_size, string_pool_cache_count, this); |
apetushkov@9858 | 79 | if (_free_list_mspace == NULL || !_free_list_mspace->initialize()) { |
apetushkov@9858 | 80 | return false; |
apetushkov@9858 | 81 | } |
apetushkov@9858 | 82 | assert(_lock == NULL, "invariant"); |
apetushkov@9858 | 83 | _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag); |
apetushkov@9858 | 84 | return _lock != NULL; |
apetushkov@9858 | 85 | } |
apetushkov@9858 | 86 | |
apetushkov@9858 | 87 | /* |
apetushkov@9858 | 88 | * If the buffer was a "lease" from the global system, release back. |
apetushkov@9858 | 89 | * |
apetushkov@9858 | 90 | * The buffer is effectively invalidated for the thread post-return, |
apetushkov@9858 | 91 | * and the caller should take means to ensure that it is not referenced any longer. |
apetushkov@9858 | 92 | */ |
apetushkov@9858 | 93 | static void release(BufferPtr buffer, Thread* thread) { |
apetushkov@9858 | 94 | assert(buffer != NULL, "invariant"); |
apetushkov@9858 | 95 | assert(buffer->lease(), "invariant"); |
apetushkov@9858 | 96 | assert(buffer->acquired_by_self(), "invariant"); |
apetushkov@9858 | 97 | buffer->clear_lease(); |
apetushkov@9858 | 98 | buffer->release(); |
apetushkov@9858 | 99 | } |
apetushkov@9858 | 100 | |
apetushkov@9858 | 101 | BufferPtr JfrStringPool::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) { |
apetushkov@9858 | 102 | assert(old != NULL, "invariant"); |
apetushkov@9858 | 103 | assert(old->lease(), "invariant"); |
apetushkov@9858 | 104 | if (0 == requested) { |
apetushkov@9858 | 105 | // indicates a lease is being returned |
apetushkov@9858 | 106 | release(old, thread); |
apetushkov@9858 | 107 | return NULL; |
apetushkov@9858 | 108 | } |
apetushkov@9858 | 109 | // migration of in-flight information |
apetushkov@9858 | 110 | BufferPtr const new_buffer = lease_buffer(thread, used + requested); |
apetushkov@9858 | 111 | if (new_buffer != NULL) { |
apetushkov@9858 | 112 | migrate_outstanding_writes(old, new_buffer, used, requested); |
apetushkov@9858 | 113 | } |
apetushkov@9858 | 114 | release(old, thread); |
apetushkov@9858 | 115 | return new_buffer; // might be NULL |
apetushkov@9858 | 116 | } |
apetushkov@9858 | 117 | |
apetushkov@9858 | 118 | static const size_t lease_retry = 10; |
apetushkov@9858 | 119 | |
apetushkov@9858 | 120 | BufferPtr JfrStringPool::lease_buffer(Thread* thread, size_t size /* 0 */) { |
apetushkov@9858 | 121 | BufferPtr buffer = mspace_get_free_lease_with_retry(size, instance()._free_list_mspace, lease_retry, thread); |
apetushkov@9858 | 122 | if (buffer == NULL) { |
apetushkov@9858 | 123 | buffer = mspace_allocate_transient_lease_to_free(size, instance()._free_list_mspace, thread); |
apetushkov@9858 | 124 | } |
apetushkov@9858 | 125 | assert(buffer->acquired_by_self(), "invariant"); |
apetushkov@9858 | 126 | assert(buffer->lease(), "invariant"); |
apetushkov@9858 | 127 | return buffer; |
apetushkov@9858 | 128 | } |
apetushkov@9858 | 129 | |
apetushkov@9858 | 130 | bool JfrStringPool::add(bool epoch, jlong id, jstring string, JavaThread* jt) { |
apetushkov@9858 | 131 | assert(jt != NULL, "invariant"); |
apetushkov@9858 | 132 | const bool current_epoch = (JfrTraceIdEpoch::epoch() != 0); |
apetushkov@9858 | 133 | if (current_epoch == epoch) { |
apetushkov@9858 | 134 | JfrStringPoolWriter writer(jt); |
apetushkov@9858 | 135 | writer.write(id); |
apetushkov@9858 | 136 | writer.write(string); |
apetushkov@9858 | 137 | writer.inc_nof_strings(); |
apetushkov@9858 | 138 | } |
apetushkov@9858 | 139 | return current_epoch; |
apetushkov@9858 | 140 | } |
apetushkov@9858 | 141 | |
apetushkov@9928 | 142 | template <template <typename> class Operation> |
apetushkov@9928 | 143 | class StringPoolOp { |
apetushkov@9858 | 144 | public: |
apetushkov@9858 | 145 | typedef JfrStringPoolBuffer Type; |
apetushkov@9858 | 146 | private: |
apetushkov@9928 | 147 | Operation<Type> _op; |
apetushkov@9858 | 148 | Thread* _thread; |
apetushkov@9858 | 149 | size_t _strings_processed; |
apetushkov@9858 | 150 | public: |
apetushkov@9928 | 151 | StringPoolOp() : _op(), _thread(Thread::current()), _strings_processed(0) {} |
apetushkov@9928 | 152 | StringPoolOp(JfrChunkWriter& writer, Thread* thread) : _op(writer), _thread(thread), _strings_processed(0) {} |
apetushkov@9858 | 153 | bool write(Type* buffer, const u1* data, size_t size) { |
apetushkov@9928 | 154 | assert(buffer->acquired_by(_thread) || buffer->retired(), "invariant"); |
apetushkov@9858 | 155 | const uint64_t nof_strings_used = buffer->string_count(); |
apetushkov@9858 | 156 | assert(nof_strings_used > 0, "invariant"); |
apetushkov@9858 | 157 | buffer->set_string_top(buffer->string_top() + nof_strings_used); |
apetushkov@9858 | 158 | // "size processed" for string pool buffers is the number of processed string elements |
apetushkov@9858 | 159 | _strings_processed += nof_strings_used; |
apetushkov@9928 | 160 | return _op.write(buffer, data, size); |
apetushkov@9858 | 161 | } |
apetushkov@9858 | 162 | size_t processed() { return _strings_processed; } |
apetushkov@9858 | 163 | }; |
apetushkov@9858 | 164 | |
apetushkov@9928 | 165 | template <typename Type> |
apetushkov@9928 | 166 | class StringPoolDiscarderStub { |
apetushkov@9928 | 167 | public: |
apetushkov@9928 | 168 | bool write(Type* buffer, const u1* data, size_t size) { |
apetushkov@9928 | 169 | // stub only, discard happens at higher level |
apetushkov@9928 | 170 | return true; |
apetushkov@9928 | 171 | } |
apetushkov@9928 | 172 | }; |
apetushkov@9928 | 173 | |
apetushkov@9928 | 174 | typedef StringPoolOp<UnBufferedWriteToChunk> WriteOperation; |
apetushkov@9928 | 175 | typedef StringPoolOp<StringPoolDiscarderStub> DiscardOperation; |
apetushkov@9928 | 176 | typedef ExclusiveOp<WriteOperation> ExclusiveWriteOperation; |
apetushkov@9928 | 177 | typedef ExclusiveOp<DiscardOperation> ExclusiveDiscardOperation; |
apetushkov@9928 | 178 | typedef ReleaseOp<JfrStringPoolMspace> StringPoolReleaseOperation; |
apetushkov@9928 | 179 | typedef CompositeOperation<ExclusiveWriteOperation, StringPoolReleaseOperation> StringPoolWriteOperation; |
apetushkov@9928 | 180 | typedef CompositeOperation<ExclusiveDiscardOperation, StringPoolReleaseOperation> StringPoolDiscardOperation; |
apetushkov@9858 | 181 | |
apetushkov@9858 | 182 | size_t JfrStringPool::write() { |
apetushkov@9858 | 183 | Thread* const thread = Thread::current(); |
apetushkov@9858 | 184 | WriteOperation wo(_chunkwriter, thread); |
apetushkov@9928 | 185 | ExclusiveWriteOperation ewo(wo); |
apetushkov@9858 | 186 | StringPoolReleaseOperation spro(_free_list_mspace, thread, false); |
apetushkov@9928 | 187 | StringPoolWriteOperation spwo(&ewo, &spro); |
apetushkov@9858 | 188 | assert(_free_list_mspace->is_full_empty(), "invariant"); |
apetushkov@9858 | 189 | process_free_list(spwo, _free_list_mspace); |
apetushkov@9858 | 190 | return wo.processed(); |
apetushkov@9858 | 191 | } |
apetushkov@9858 | 192 | |
apetushkov@9928 | 193 | size_t JfrStringPool::write_at_safepoint() { |
apetushkov@9928 | 194 | assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
apetushkov@9928 | 195 | return write(); |
apetushkov@9928 | 196 | } |
apetushkov@9858 | 197 | |
apetushkov@9858 | 198 | size_t JfrStringPool::clear() { |
apetushkov@9928 | 199 | DiscardOperation discard_operation; |
apetushkov@9928 | 200 | ExclusiveDiscardOperation edo(discard_operation); |
apetushkov@9928 | 201 | StringPoolReleaseOperation spro(_free_list_mspace, Thread::current(), false); |
apetushkov@9928 | 202 | StringPoolDiscardOperation spdo(&edo, &spro); |
apetushkov@9858 | 203 | assert(_free_list_mspace->is_full_empty(), "invariant"); |
apetushkov@9928 | 204 | process_free_list(spdo, _free_list_mspace); |
apetushkov@9858 | 205 | return discard_operation.processed(); |
apetushkov@9858 | 206 | } |
apetushkov@9858 | 207 | |
apetushkov@9858 | 208 | void JfrStringPool::register_full(BufferPtr t, Thread* thread) { |
apetushkov@9858 | 209 | // nothing here at the moment |
apetushkov@9928 | 210 | assert(t != NULL, "invariant"); |
apetushkov@9928 | 211 | assert(t->acquired_by(thread), "invariant"); |
apetushkov@9858 | 212 | assert(t->retired(), "invariant"); |
apetushkov@9858 | 213 | } |
apetushkov@9858 | 214 | |
apetushkov@9858 | 215 | void JfrStringPool::lock() { |
apetushkov@9858 | 216 | assert(!_lock->owned_by_self(), "invariant"); |
apetushkov@9858 | 217 | _lock->lock_without_safepoint_check(); |
apetushkov@9858 | 218 | } |
apetushkov@9858 | 219 | |
apetushkov@9858 | 220 | void JfrStringPool::unlock() { |
apetushkov@9858 | 221 | _lock->unlock(); |
apetushkov@9858 | 222 | } |
apetushkov@9858 | 223 | |
apetushkov@9858 | 224 | #ifdef ASSERT |
apetushkov@9858 | 225 | bool JfrStringPool::is_locked() const { |
apetushkov@9858 | 226 | return _lock->owned_by_self(); |
apetushkov@9858 | 227 | } |
apetushkov@9858 | 228 | #endif |