1 /*
   2  * Copyright (c) 2011, 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 "jfr/recorder/storage/jfrBuffer.hpp"
  27 #include "runtime/atomic.inline.hpp"
  28 #include "runtime/orderAccess.inline.hpp"
  29 #include "runtime/thread.inline.hpp"
  30 #include "jfr/recorder/jfrRecorder.hpp"
  31 
  32 static const u1* const MUTEX_CLAIM = NULL;
  33 
  34 JfrBuffer::JfrBuffer() : _next(NULL),
  35                          _prev(NULL),
  36                          _identity(NULL),
  37                          _pos(NULL),
  38                          _top(NULL),
  39                          _flags(0),
  40                          _header_size(0),
  41                          _size(0) {}
  42 
  43 bool JfrBuffer::initialize(size_t header_size, size_t size, const void* id /* NULL */) {
  44   _header_size = (u2)header_size;
  45   _size = (u4)(size / BytesPerWord);
  46   assert(_identity == NULL, "invariant");
  47   _identity = id;
  48   set_pos(start());
  49   set_top(start());
  50   assert(_next == NULL, "invariant");
  51   assert(free_size() == size, "invariant");
  52   assert(!transient(), "invariant");
  53   assert(!lease(), "invariant");
  54   assert(!retired(), "invariant");
  55   return true;
  56 }
  57 
  58 void JfrBuffer::reinitialize() {
  59   assert(!lease(), "invariant");
  60   assert(!transient(), "invariant");
  61   set_pos(start());
  62   clear_retired();
  63   set_top(start());
  64 }
  65 
  66 void JfrBuffer::concurrent_reinitialization() {
  67   concurrent_top();
  68   assert(!lease(), "invariant");
  69   assert(!transient(), "invariant");
  70   set_pos(start());
  71   set_concurrent_top(start());
  72   clear_retired();
  73 }
  74 
  75 size_t JfrBuffer::discard() {
  76   size_t discard_size = unflushed_size();
  77   set_top(pos());
  78   return discard_size;
  79 }
  80 
  81 const u1* JfrBuffer::stable_top() const {
  82   const u1* current_top;
  83   do {
  84     current_top = (const u1*)OrderAccess::load_ptr_acquire(&_top);
  85   } while (MUTEX_CLAIM == current_top);
  86   return current_top;
  87 }
  88 
  89 const u1* JfrBuffer::top() const {
  90   return _top;
  91 }
  92 
  93 void JfrBuffer::set_top(const u1* new_top) {
  94   _top = new_top;
  95 }
  96 
  97 const u1* JfrBuffer::concurrent_top() const {
  98   do {
  99     const u1* current_top = stable_top();
 100     if (Atomic::cmpxchg_ptr((void*)MUTEX_CLAIM, &_top, (void*)current_top) == current_top) {
 101       return current_top;
 102     }
 103   } while (true);
 104 }
 105 
 106 void JfrBuffer::set_concurrent_top(const u1* new_top) {
 107   assert(new_top != MUTEX_CLAIM, "invariant");
 108   assert(new_top <= end(), "invariant");
 109   assert(new_top >= start(), "invariant");
 110   assert(top() == MUTEX_CLAIM, "invariant");
 111   OrderAccess::release_store_ptr(&_top, (void*)new_top);
 112 }
 113 
 114 size_t JfrBuffer::unflushed_size() const {
 115   return pos() - stable_top();
 116 }
 117 
 118 void JfrBuffer::acquire(const void* id) {
 119   assert(id != NULL, "invariant");
 120   const void* current_id;
 121   do {
 122     current_id = OrderAccess::load_ptr_acquire(&_identity);
 123   } while (current_id != NULL || Atomic::cmpxchg_ptr((void*)id, &_identity, (void*)current_id) != current_id);
 124 }
 125 
 126 bool JfrBuffer::try_acquire(const void* id) {
 127   assert(id != NULL, "invariant");
 128   const void* const current_id = OrderAccess::load_ptr_acquire(&_identity);
 129   return current_id == NULL && Atomic::cmpxchg_ptr((void*)id, &_identity, (void*)current_id) == current_id;
 130 }
 131 
 132 void JfrBuffer::release() {
 133   OrderAccess::release_store_ptr(&_identity, (void*)NULL);
 134 }
 135 
 136 void JfrBuffer::clear_identity() {
 137   _identity = NULL;
 138 }
 139 
 140 #ifdef ASSERT
 141 static bool validate_to(const JfrBuffer* const to, size_t size) {
 142   assert(to != NULL, "invariant");
 143   if (!JfrRecorder::is_shutting_down()) assert(to->acquired_by_self(), "invariant");
 144   assert(to->free_size() >= size, "invariant");
 145   return true;
 146 }
 147 
 148 static bool validate_concurrent_this(const JfrBuffer* const t, size_t size) {
 149   assert(t->top() == MUTEX_CLAIM, "invariant");
 150   return true;
 151 }
 152 
 153 static bool validate_this(const JfrBuffer* const t, size_t size) {
 154   assert(t->top() + size <= t->pos(), "invariant");
 155   return true;
 156 }
 157 
 158 bool JfrBuffer::acquired_by_self() const {
 159   return identity() == Thread::current();
 160 }
 161 #endif // ASSERT
 162 
 163 void JfrBuffer::move(JfrBuffer* const to, size_t size) {
 164   assert(validate_to(to, size), "invariant");
 165   assert(validate_this(this, size), "invariant");
 166   const u1* current_top = top();
 167   assert(current_top != NULL, "invariant");
 168   memcpy(to->pos(), current_top, size);
 169   to->set_pos(size);
 170   to->release();
 171   set_top(current_top + size);
 172 }
 173 
 174 void JfrBuffer::concurrent_move_and_reinitialize(JfrBuffer* const to, size_t size) {
 175   assert(validate_to(to, size), "invariant");
 176   const u1* current_top = concurrent_top();
 177   assert(validate_concurrent_this(this, size), "invariant");
 178   const size_t actual_size = MIN2(size, (size_t)(pos() - current_top));
 179   assert(actual_size <= size, "invariant");
 180   memcpy(to->pos(), current_top, actual_size);
 181   to->set_pos(actual_size);
 182   set_pos(start());
 183   to->release();
 184   set_concurrent_top(start());
 185 }
 186 
 187 // flags
 188 enum FLAG {
 189   RETIRED = 1,
 190   TRANSIENT = 2,
 191   LEASE = 4
 192 };
 193 
 194 bool JfrBuffer::transient() const {
 195   return (u1)TRANSIENT == (_flags & (u1)TRANSIENT);
 196 }
 197 
 198 void JfrBuffer::set_transient() {
 199   _flags |= (u1)TRANSIENT;
 200   assert(transient(), "invariant");
 201 }
 202 
 203 void JfrBuffer::clear_transient() {
 204   if (transient()) {
 205     _flags ^= (u1)TRANSIENT;
 206   }
 207   assert(!transient(), "invariant");
 208 }
 209 
 210 bool JfrBuffer::lease() const {
 211   return (u1)LEASE == (_flags & (u1)LEASE);
 212 }
 213 
 214 void JfrBuffer::set_lease() {
 215   _flags |= (u1)LEASE;
 216   assert(lease(), "invariant");
 217 }
 218 
 219 void JfrBuffer::clear_lease() {
 220   if (lease()) {
 221     _flags ^= (u1)LEASE;
 222   }
 223   assert(!lease(), "invariant");
 224 }
 225 
 226 static u2 load_acquire_flags(const u2* const flags) {
 227   return OrderAccess::load_acquire((volatile jushort *)flags);
 228 }
 229 
 230 static void release_store_flags(u2* const flags, u2 new_flags) {
 231   OrderAccess::release_store(flags, new_flags);
 232 }
 233 
 234 bool JfrBuffer::retired() const {
 235   return (u1)RETIRED == (load_acquire_flags(&_flags) & (u1)RETIRED);
 236 }
 237 
 238 void JfrBuffer::set_retired() {
 239   const u2 new_flags = load_acquire_flags(&_flags) | (u1)RETIRED;
 240   release_store_flags(&_flags, new_flags);
 241 }
 242 
 243 void JfrBuffer::clear_retired() {
 244   u2 new_flags = load_acquire_flags(&_flags);
 245   if ((u1)RETIRED == (new_flags & (u1)RETIRED)) {
 246     new_flags ^= (u1)RETIRED;
 247     release_store_flags(&_flags, new_flags);
 248   }
 249 }