1 /* 2 * Copyright (c) 2016, 2018, 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.inline.hpp" 27 #include "jfr/recorder/access/jfrOptionSet.hpp" 28 #include "jfr/recorder/checkpoint/constant/traceid/jfrTraceIdEpoch.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 "runtime/atomic.hpp" 35 #include "runtime/mutexLocker.hpp" 36 #include "runtime/orderAccess.inline.hpp" 37 #include "runtime/safepoint.hpp" 38 #include "runtime/thread.inline.hpp" 39 #include "tracefiles/traceTypes.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), _transient_mspace(NULL), _lock(NULL), _chunkwriter(cw) {} 62 63 JfrStringPool::~JfrStringPool() { 64 if (_free_list_mspace != NULL) { 65 delete _free_list_mspace; 66 } 67 if (_transient_mspace != NULL) { 68 delete _transient_mspace; 69 } 70 if (_lock != NULL) { 71 delete _lock; 72 } 73 } 74 75 static const size_t unlimited_mspace_size = 0; 76 static const size_t string_pool_cache_count = 1; 77 static const size_t string_pool_buffer_size = M; 78 79 static JfrStringPoolMspace* create_mspace(size_t buffer_size, size_t limit, size_t cache_count, JfrStringPool* system) { 80 JfrStringPoolMspace* mspace = new JfrStringPoolMspace(buffer_size, limit, cache_count, system); 81 if (mspace != NULL) { 82 mspace->initialize(); 83 } 84 return mspace; 85 } 86 87 bool JfrStringPool::initialize() { 88 assert(_free_list_mspace == NULL, "invariant"); 89 _free_list_mspace = create_mspace(string_pool_buffer_size, unlimited_mspace_size, string_pool_cache_count, this); 90 if (_free_list_mspace == NULL) { 91 return false; 92 } 93 _transient_mspace = create_mspace((size_t)JfrOptionSet::global_buffer_size(), unlimited_mspace_size, 0, this); 94 if (_transient_mspace == NULL) { 95 return false; 96 } 97 assert(_lock == NULL, "invariant"); 98 _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag, Monitor::_safepoint_check_never); 99 return _lock != NULL; 100 } 101 102 /* 103 * 1. If the buffer was a "lease" from the global system, release back. 104 * 2. If the buffer is transient (temporal dynamically allocated), retire and release. 105 * 106 * The buffer is effectively invalidated for the thread post-return, 107 * and the caller should take means to ensure that it is not referenced any longer. 108 */ 109 static void release(BufferPtr buffer, Thread* thread) { 110 assert(buffer != NULL, "invariant"); 111 assert(buffer->lease(), "invariant"); 112 assert(buffer->acquired_by_self(), "invariant"); 113 buffer->clear_lease(); 114 if (buffer->transient()) { 115 buffer->set_retired(); 116 } 117 buffer->release(); 118 } 119 120 BufferPtr JfrStringPool::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) { 121 assert(old != NULL, "invariant"); 122 assert(old->lease(), "invariant"); 123 if (0 == requested) { 124 // indicates a lease is being returned 125 release(old, thread); 126 return NULL; 127 } 128 // migration of in-flight information 129 BufferPtr const new_buffer = lease_buffer(thread, used + requested); 130 if (new_buffer != NULL) { 131 migrate_outstanding_writes(old, new_buffer, used, requested); 132 } 133 release(old, thread); 134 return new_buffer; // might be NULL 135 } 136 137 static BufferPtr lease_transient(size_t size, JfrStringPoolMspace* transient_mspace, Thread* thread) { 138 BufferPtr buffer = mspace_allocate_transient_lease_to_full(size, transient_mspace, thread); 139 if (buffer == NULL) { 140 log_warning(jfr)("Unable to allocate " SIZE_FORMAT " bytes of transient memory.", size); 141 return NULL; 142 } 143 assert(buffer->acquired_by_self(), "invariant"); 144 assert(buffer->transient(), "invariant"); 145 assert(buffer->lease(), "invariant"); 146 return buffer; 147 } 148 149 static const size_t lease_retry = 10; 150 151 static BufferPtr lease_free(size_t size, JfrStringPoolMspace* mspace, Thread* thread) { 152 return mspace_get_free_lease_with_retry(size, mspace, lease_retry, thread); 153 } 154 155 BufferPtr JfrStringPool::lease_buffer(Thread* thread, size_t size /* 0 */) { 156 static const size_t max_elem_size = instance()._free_list_mspace->min_elem_size(); // min is max 157 if (size <= max_elem_size) { 158 BufferPtr const buffer = lease_free(size, instance()._free_list_mspace, thread); 159 if (buffer != NULL) { 160 assert(buffer->acquired_by_self(), "invariant"); 161 assert(!buffer->transient(), "invariant"); 162 assert(buffer->lease(), "invariant"); 163 return buffer; 164 } 165 } 166 return lease_transient(size, instance()._transient_mspace, thread); 167 } 168 169 bool JfrStringPool::add(bool epoch, jlong id, jstring string, JavaThread* jt) { 170 assert(jt != NULL, "invariant"); 171 const bool current_epoch = JfrTraceIdEpoch::epoch(); 172 if (current_epoch == epoch) { 173 JfrStringPoolWriter writer(jt); 174 writer.write(id); 175 writer.write(string); 176 writer.inc_nof_strings(); 177 } 178 return current_epoch; 179 } 180 181 class StringPoolWriteOp { 182 public: 183 typedef JfrStringPoolBuffer Type; 184 private: 185 UnBufferedWriteToChunk<Type> _writer; 186 Thread* _thread; 187 size_t _strings_processed; 188 public: 189 StringPoolWriteOp(JfrChunkWriter& writer, Thread* thread) : _writer(writer), _thread(thread), _strings_processed(0) {} 190 bool write(Type* buffer, const u1* data, size_t size) { 191 buffer->acquire(_thread); // blocking 192 const u4 nof_strings_used = (const u4)buffer->string_count(); 193 assert(nof_strings_used > 0, "invariant"); 194 buffer->set_string_top(buffer->string_top() + nof_strings_used); 195 // "size processed" for string pool buffers is the number of processed string elements 196 _strings_processed += nof_strings_used; 197 const bool ret = _writer.write(buffer, data, size); 198 buffer->release(); 199 return ret; 200 } 201 size_t processed() { return _strings_processed; } 202 }; 203 204 typedef StringPoolWriteOp WriteOperation; 205 typedef ConcurrentWriteOp<WriteOperation> ConcurrentWriteOperation; 206 typedef MutexedReleaseOp<JfrStringPoolMspace> StringPoolReleaseOperation; 207 typedef CompositeOperation<ConcurrentWriteOperation, StringPoolReleaseOperation> StringPoolWriteOperation; 208 209 size_t JfrStringPool::write() { 210 Thread* const thread = Thread::current(); 211 WriteOperation wo(_chunkwriter, thread); 212 ConcurrentWriteOperation cwo(wo); 213 StringPoolReleaseOperation spro(_free_list_mspace, thread); 214 StringPoolWriteOperation spwo(&cwo, &spro); 215 assert(_free_list_mspace->is_full_empty(), "invariant"); 216 process_free_list(spwo, _free_list_mspace); 217 StringPoolReleaseOperation transient_release(_transient_mspace, thread); 218 StringPoolWriteOperation transient_writer(&cwo, &transient_release); 219 assert(_transient_mspace->is_free_empty(), "invariant"); 220 process_full_list(transient_writer, _transient_mspace); 221 return wo.processed(); 222 } 223 224 class StringPoolBufferDiscarder { 225 private: 226 Thread* _thread; 227 size_t _processed; 228 public: 229 typedef JfrStringPoolBuffer Type; 230 StringPoolBufferDiscarder() : _thread(Thread::current()), _processed(0) {} 231 bool discard(Type* buffer, const u1* data, size_t size) { 232 buffer->acquire(_thread); // serialized access 233 const u4 nof_strings_used = buffer->string_count(); 234 buffer->set_string_top(buffer->string_top() + nof_strings_used); 235 // "size processed" for string pool buffers is the number of string elements 236 _processed += nof_strings_used; 237 buffer->release(); 238 return true; 239 } 240 size_t processed() const { return _processed; } 241 }; 242 243 typedef DiscardOp<StringPoolBufferDiscarder> DiscardOperation; 244 size_t JfrStringPool::clear() { 245 DiscardOperation discard_operation(concurrent); // concurrent discard mode 246 assert(_free_list_mspace->is_full_empty(), "invariant"); 247 assert(_transient_mspace->is_free_empty(), "invariant"); 248 process_full_list(discard_operation, _transient_mspace); 249 process_free_list(discard_operation, _free_list_mspace); 250 return discard_operation.processed(); 251 } 252 253 void JfrStringPool::register_full(BufferPtr t, Thread* thread) { 254 // nothing here at the moment 255 assert(t->retired(), "invariant"); 256 } 257 258 void JfrStringPool::lock() { 259 assert(!_lock->owned_by_self(), "invariant"); 260 _lock->lock_without_safepoint_check(); 261 } 262 263 void JfrStringPool::unlock() { 264 _lock->unlock(); 265 }