1 /* 2 * Copyright (c) 2016, 2019, 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 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/javaClasses.hpp" 27 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp" 28 #include "jfr/recorder/service/jfrOptionSet.hpp" 29 #include "jfr/recorder/storage/jfrMemorySpace.inline.hpp" 30 #include "jfr/recorder/repository/jfrChunkWriter.hpp" 31 #include "jfr/recorder/storage/jfrStorageUtils.inline.hpp" 32 #include "jfr/recorder/stringpool/jfrStringPool.hpp" 33 #include "jfr/recorder/stringpool/jfrStringPoolWriter.hpp" 34 #include "jfr/utilities/jfrTypes.hpp" 35 #include "runtime/atomic.hpp" 36 #include "runtime/mutexLocker.hpp" 37 #include "runtime/orderAccess.hpp" 38 #include "runtime/safepoint.hpp" 39 #include "runtime/thread.inline.hpp" 40 41 typedef JfrStringPool::Buffer* BufferPtr; 42 43 static JfrStringPool* _instance = NULL; 44 45 JfrStringPool& JfrStringPool::instance() { 46 return *_instance; 47 } 48 49 JfrStringPool* JfrStringPool::create(JfrChunkWriter& cw) { 50 assert(_instance == NULL, "invariant"); 51 _instance = new JfrStringPool(cw); 52 return _instance; 53 } 54 55 void JfrStringPool::destroy() { 56 assert(_instance != NULL, "invariant"); 57 delete _instance; 58 _instance = NULL; 59 } 60 61 JfrStringPool::JfrStringPool(JfrChunkWriter& cw) : _free_list_mspace(NULL), _lock(NULL), _chunkwriter(cw) {} 62 63 JfrStringPool::~JfrStringPool() { 64 if (_free_list_mspace != NULL) { 65 delete _free_list_mspace; 66 } 67 if (_lock != NULL) { 68 delete _lock; 69 } 70 } 71 72 static const size_t unlimited_mspace_size = 0; 73 static const size_t string_pool_cache_count = 2; 74 static const size_t string_pool_buffer_size = 512 * K; 75 76 bool JfrStringPool::initialize() { 77 assert(_free_list_mspace == NULL, "invariant"); 78 _free_list_mspace = new JfrStringPoolMspace(string_pool_buffer_size, unlimited_mspace_size, string_pool_cache_count, this); 79 if (_free_list_mspace == NULL || !_free_list_mspace->initialize()) { 80 return false; 81 } 82 assert(_lock == NULL, "invariant"); 83 _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag); 84 return _lock != NULL; 85 } 86 87 /* 88 * If the buffer was a "lease" from the global system, release back. 89 * 90 * The buffer is effectively invalidated for the thread post-return, 91 * and the caller should take means to ensure that it is not referenced any longer. 92 */ 93 static void release(BufferPtr buffer, Thread* thread) { 94 assert(buffer != NULL, "invariant"); 95 assert(buffer->lease(), "invariant"); 96 assert(buffer->acquired_by_self(), "invariant"); 97 buffer->clear_lease(); 98 buffer->release(); 99 } 100 101 BufferPtr JfrStringPool::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) { 102 assert(old != NULL, "invariant"); 103 assert(old->lease(), "invariant"); 104 if (0 == requested) { 105 // indicates a lease is being returned 106 release(old, thread); 107 return NULL; 108 } 109 // migration of in-flight information 110 BufferPtr const new_buffer = lease_buffer(thread, used + requested); 111 if (new_buffer != NULL) { 112 migrate_outstanding_writes(old, new_buffer, used, requested); 113 } 114 release(old, thread); 115 return new_buffer; // might be NULL 116 } 117 118 static const size_t lease_retry = 10; 119 120 BufferPtr JfrStringPool::lease_buffer(Thread* thread, size_t size /* 0 */) { 121 BufferPtr buffer = mspace_get_free_lease_with_retry(size, instance()._free_list_mspace, lease_retry, thread); 122 if (buffer == NULL) { 123 buffer = mspace_allocate_transient_lease_to_free(size, instance()._free_list_mspace, thread); 124 } 125 assert(buffer->acquired_by_self(), "invariant"); 126 assert(buffer->lease(), "invariant"); 127 return buffer; 128 } 129 130 bool JfrStringPool::add(bool epoch, jlong id, jstring string, JavaThread* jt) { 131 assert(jt != NULL, "invariant"); 132 const bool current_epoch = (JfrTraceIdEpoch::epoch() != 0); 133 if (current_epoch == epoch) { 134 JfrStringPoolWriter writer(jt); 135 writer.write(id); 136 writer.write(string); 137 writer.inc_nof_strings(); 138 } 139 return current_epoch; 140 } 141 142 template <template <typename> class Operation> 143 class StringPoolOp { 144 public: 145 typedef JfrStringPoolBuffer Type; 146 private: 147 Operation<Type> _op; 148 Thread* _thread; 149 size_t _strings_processed; 150 public: 151 StringPoolOp() : _op(), _thread(Thread::current()), _strings_processed(0) {} 152 StringPoolOp(JfrChunkWriter& writer, Thread* thread) : _op(writer), _thread(thread), _strings_processed(0) {} 153 bool write(Type* buffer, const u1* data, size_t size) { 154 assert(buffer->acquired_by(_thread) || buffer->retired(), "invariant"); 155 const uint64_t nof_strings_used = buffer->string_count(); 156 assert(nof_strings_used > 0, "invariant"); 157 buffer->set_string_top(buffer->string_top() + nof_strings_used); 158 // "size processed" for string pool buffers is the number of processed string elements 159 _strings_processed += nof_strings_used; 160 return _op.write(buffer, data, size); 161 } 162 size_t processed() { return _strings_processed; } 163 }; 164 165 template <typename Type> 166 class StringPoolDiscarderStub { 167 public: 168 bool write(Type* buffer, const u1* data, size_t size) { 169 // stub only, discard happens at higher level 170 return true; 171 } 172 }; 173 174 typedef StringPoolOp<UnBufferedWriteToChunk> WriteOperation; 175 typedef StringPoolOp<StringPoolDiscarderStub> DiscardOperation; 176 typedef ExclusiveOp<WriteOperation> ExclusiveWriteOperation; 177 typedef ExclusiveOp<DiscardOperation> ExclusiveDiscardOperation; 178 typedef ReleaseOp<JfrStringPoolMspace> StringPoolReleaseOperation; 179 typedef CompositeOperation<ExclusiveWriteOperation, StringPoolReleaseOperation> StringPoolWriteOperation; 180 typedef CompositeOperation<ExclusiveDiscardOperation, StringPoolReleaseOperation> StringPoolDiscardOperation; 181 182 size_t JfrStringPool::write() { 183 Thread* const thread = Thread::current(); 184 WriteOperation wo(_chunkwriter, thread); 185 ExclusiveWriteOperation ewo(wo); 186 StringPoolReleaseOperation spro(_free_list_mspace, thread, false); 187 StringPoolWriteOperation spwo(&ewo, &spro); 188 assert(_free_list_mspace->is_full_empty(), "invariant"); 189 process_free_list(spwo, _free_list_mspace); 190 return wo.processed(); 191 } 192 193 size_t JfrStringPool::write_at_safepoint() { 194 assert(SafepointSynchronize::is_at_safepoint(), "invariant"); 195 return write(); 196 } 197 198 size_t JfrStringPool::clear() { 199 DiscardOperation discard_operation; 200 ExclusiveDiscardOperation edo(discard_operation); 201 StringPoolReleaseOperation spro(_free_list_mspace, Thread::current(), false); 202 StringPoolDiscardOperation spdo(&edo, &spro); 203 assert(_free_list_mspace->is_full_empty(), "invariant"); 204 process_free_list(spdo, _free_list_mspace); 205 return discard_operation.processed(); 206 } 207 208 void JfrStringPool::register_full(BufferPtr t, Thread* thread) { 209 // nothing here at the moment 210 assert(t != NULL, "invariant"); 211 assert(t->acquired_by(thread), "invariant"); 212 assert(t->retired(), "invariant"); 213 } 214 215 void JfrStringPool::lock() { 216 assert(!_lock->owned_by_self(), "invariant"); 217 _lock->lock_without_safepoint_check(); 218 } 219 220 void JfrStringPool::unlock() { 221 _lock->unlock(); 222 } 223 224 #ifdef ASSERT 225 bool JfrStringPool::is_locked() const { 226 return _lock->owned_by_self(); 227 } 228 #endif