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.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 class StringPoolWriteOp  {
 143  public:
 144   typedef JfrStringPoolBuffer Type;
 145  private:
 146   UnBufferedWriteToChunk<Type> _writer;
 147   Thread* _thread;
 148   size_t _strings_processed;
 149  public:
 150   StringPoolWriteOp(JfrChunkWriter& writer, Thread* thread) : _writer(writer), _thread(thread), _strings_processed(0) {}
 151   bool write(Type* buffer, const u1* data, size_t size) {
 152     buffer->acquire(_thread); // blocking
 153     const uint64_t nof_strings_used = buffer->string_count();
 154     assert(nof_strings_used > 0, "invariant");
 155     buffer->set_string_top(buffer->string_top() + nof_strings_used);
 156     // "size processed" for string pool buffers is the number of processed string elements
 157     _strings_processed += nof_strings_used;
 158     const bool ret = _writer.write(buffer, data, size);
 159     buffer->release();
 160     return ret;
 161   }
 162   size_t processed() { return _strings_processed; }
 163 };
 164 
 165 typedef StringPoolWriteOp WriteOperation;
 166 typedef ConcurrentWriteOp<WriteOperation> ConcurrentWriteOperation;
 167 
 168 size_t JfrStringPool::write() {
 169   Thread* const thread = Thread::current();
 170   WriteOperation wo(_chunkwriter, thread);
 171   ConcurrentWriteOperation cwo(wo);
 172   assert(_free_list_mspace->is_full_empty(), "invariant");
 173   process_free_list(cwo, _free_list_mspace);
 174   return wo.processed();
 175 }
 176 
 177 typedef MutexedWriteOp<WriteOperation> MutexedWriteOperation;
 178 typedef ReleaseOp<JfrStringPoolMspace> StringPoolReleaseOperation;
 179 typedef CompositeOperation<MutexedWriteOperation, StringPoolReleaseOperation> StringPoolWriteOperation;
 180 
 181 size_t JfrStringPool::write_at_safepoint() {
 182   assert(SafepointSynchronize::is_at_safepoint(), "invariant");
 183   Thread* const thread = Thread::current();
 184   WriteOperation wo(_chunkwriter, thread);
 185   MutexedWriteOperation mwo(wo);
 186   StringPoolReleaseOperation spro(_free_list_mspace, thread, false);
 187   StringPoolWriteOperation spwo(&mwo, &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 class StringPoolBufferDiscarder {
 194  private:
 195   Thread* _thread;
 196   size_t _processed;
 197  public:
 198   typedef JfrStringPoolBuffer Type;
 199   StringPoolBufferDiscarder() : _thread(Thread::current()), _processed(0) {}
 200   bool process(Type* buffer) {
 201     buffer->acquire(_thread); // serialized access
 202     const u1* const current_top = buffer->top();
 203     const size_t unflushed_size = buffer->pos() - current_top;
 204     if (unflushed_size == 0) {
 205       assert(buffer->string_count() == 0, "invariant");
 206       buffer->release();
 207       return true;
 208     }
 209     buffer->set_top(current_top + unflushed_size);
 210     const uint64_t nof_strings_used = buffer->string_count();
 211     buffer->set_string_top(buffer->string_top() + nof_strings_used);
 212     // "size processed" for string pool buffers is the number of string elements
 213     _processed += (size_t)nof_strings_used;
 214     buffer->release();
 215     return true;
 216   }
 217   size_t processed() const { return _processed; }
 218 };
 219 
 220 size_t JfrStringPool::clear() {
 221   StringPoolBufferDiscarder discard_operation;
 222   assert(_free_list_mspace->is_full_empty(), "invariant");
 223   process_free_list(discard_operation, _free_list_mspace);
 224   return discard_operation.processed();
 225 }
 226 
 227 void JfrStringPool::register_full(BufferPtr t, Thread* thread) {
 228   // nothing here at the moment
 229   assert(t->retired(), "invariant");
 230 }
 231 
 232 void JfrStringPool::lock() {
 233   assert(!_lock->owned_by_self(), "invariant");
 234   _lock->lock_without_safepoint_check();
 235 }
 236 
 237 void JfrStringPool::unlock() {
 238   _lock->unlock();
 239 }
 240 
 241 #ifdef ASSERT
 242 bool JfrStringPool::is_locked() const {
 243   return _lock->owned_by_self();
 244 }
 245 #endif