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