1 /* 2 * Copyright (c) 2001, 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 #ifndef SHARE_GC_SHARED_PTRQUEUE_HPP 26 #define SHARE_GC_SHARED_PTRQUEUE_HPP 27 28 #include "memory/padded.hpp" 29 #include "utilities/align.hpp" 30 #include "utilities/debug.hpp" 31 #include "utilities/lockFreeStack.hpp" 32 #include "utilities/sizes.hpp" 33 34 class Mutex; 35 36 // There are various techniques that require threads to be able to log 37 // addresses. For example, a generational write barrier might log 38 // the addresses of modified old-generation objects. This type supports 39 // this operation. 40 41 class BufferNode; 42 class PtrQueueSet; 43 class PtrQueue { 44 friend class VMStructs; 45 46 // Noncopyable - not defined. 47 PtrQueue(const PtrQueue&); 48 PtrQueue& operator=(const PtrQueue&); 49 50 // The ptr queue set to which this queue belongs. 51 PtrQueueSet* const _qset; 52 53 // Whether updates should be logged. 54 bool _active; 55 56 // If true, the queue is permanent, and doesn't need to deallocate 57 // its buffer in the destructor (since that obtains a lock which may not 58 // be legally locked by then. 59 const bool _permanent; 60 61 // The (byte) index at which an object was last enqueued. Starts at 62 // capacity_in_bytes (indicating an empty buffer) and goes towards zero. 63 // Value is always pointer-size aligned. 64 size_t _index; 65 66 // Size of the current buffer, in bytes. 67 // Value is always pointer-size aligned. 68 size_t _capacity_in_bytes; 69 70 static const size_t _element_size = sizeof(void*); 71 72 // Get the capacity, in bytes. The capacity must have been set. 73 size_t capacity_in_bytes() const { 74 assert(_capacity_in_bytes > 0, "capacity not set"); 75 return _capacity_in_bytes; 76 } 77 78 void set_capacity(size_t entries) { 79 size_t byte_capacity = index_to_byte_index(entries); 80 assert(_capacity_in_bytes == 0 || _capacity_in_bytes == byte_capacity, 81 "changing capacity " SIZE_FORMAT " -> " SIZE_FORMAT, 82 _capacity_in_bytes, byte_capacity); 83 _capacity_in_bytes = byte_capacity; 84 } 85 86 static size_t byte_index_to_index(size_t ind) { 87 assert(is_aligned(ind, _element_size), "precondition"); 88 return ind / _element_size; 89 } 90 91 static size_t index_to_byte_index(size_t ind) { 92 return ind * _element_size; 93 } 94 95 protected: 96 // The buffer. 97 void** _buf; 98 99 size_t index() const { 100 return byte_index_to_index(_index); 101 } 102 103 void set_index(size_t new_index) { 104 size_t byte_index = index_to_byte_index(new_index); 105 assert(byte_index <= capacity_in_bytes(), "precondition"); 106 _index = byte_index; 107 } 108 109 size_t capacity() const { 110 return byte_index_to_index(capacity_in_bytes()); 111 } 112 113 // If there is a lock associated with this buffer, this is that lock. 114 Mutex* _lock; 115 116 PtrQueueSet* qset() { return _qset; } 117 bool is_permanent() const { return _permanent; } 118 119 // Process queue entries and release resources. 120 void flush_impl(); 121 122 // Initialize this queue to contain a null buffer, and be part of the 123 // given PtrQueueSet. 124 PtrQueue(PtrQueueSet* qset, bool permanent = false, bool active = false); 125 126 // Requires queue flushed or permanent. 127 ~PtrQueue(); 128 129 public: 130 131 // Associate a lock with a ptr queue. 132 void set_lock(Mutex* lock) { _lock = lock; } 133 134 // Forcibly set empty. 135 void reset() { 136 if (_buf != NULL) { 137 _index = capacity_in_bytes(); 138 } 139 } 140 141 void enqueue(volatile void* ptr) { 142 enqueue((void*)(ptr)); 143 } 144 145 // Enqueues the given "obj". 146 void enqueue(void* ptr) { 147 if (!_active) return; 148 else enqueue_known_active(ptr); 149 } 150 151 // This method is called when we're doing the zero index handling 152 // and gives a chance to the queues to do any pre-enqueueing 153 // processing they might want to do on the buffer. It should return 154 // true if the buffer should be enqueued, or false if enough 155 // entries were cleared from it so that it can be re-used. It should 156 // not return false if the buffer is still full (otherwise we can 157 // get into an infinite loop). 158 virtual bool should_enqueue_buffer() { return true; } 159 void handle_zero_index(); 160 161 void enqueue_known_active(void* ptr); 162 163 // Return the size of the in-use region. 164 size_t size() const { 165 size_t result = 0; 166 if (_buf != NULL) { 167 assert(_index <= capacity_in_bytes(), "Invariant"); 168 result = byte_index_to_index(capacity_in_bytes() - _index); 169 } 170 return result; 171 } 172 173 bool is_empty() const { 174 return _buf == NULL || capacity_in_bytes() == _index; 175 } 176 177 // Set the "active" property of the queue to "b". An enqueue to an 178 // inactive thread is a no-op. Setting a queue to inactive resets its 179 // log to the empty state. 180 void set_active(bool b) { 181 _active = b; 182 if (!b && _buf != NULL) { 183 reset(); 184 } else if (b && _buf != NULL) { 185 assert(index() == capacity(), 186 "invariant: queues are empty when activated."); 187 } 188 } 189 190 bool is_active() const { return _active; } 191 192 // To support compiler. 193 194 protected: 195 template<typename Derived> 196 static ByteSize byte_offset_of_index() { 197 return byte_offset_of(Derived, _index); 198 } 199 200 static ByteSize byte_width_of_index() { return in_ByteSize(sizeof(size_t)); } 201 202 template<typename Derived> 203 static ByteSize byte_offset_of_buf() { 204 return byte_offset_of(Derived, _buf); 205 } 206 207 static ByteSize byte_width_of_buf() { return in_ByteSize(_element_size); } 208 209 template<typename Derived> 210 static ByteSize byte_offset_of_active() { 211 return byte_offset_of(Derived, _active); 212 } 213 214 static ByteSize byte_width_of_active() { return in_ByteSize(sizeof(bool)); } 215 216 }; 217 218 class BufferNode { 219 size_t _index; 220 BufferNode* volatile _next; 221 void* _buffer[1]; // Pseudo flexible array member. 222 223 BufferNode() : _index(0), _next(NULL) { } 224 ~BufferNode() { } 225 226 static size_t buffer_offset() { 227 return offset_of(BufferNode, _buffer); 228 } 229 230 static BufferNode* volatile* next_ptr(BufferNode& bn) { return &bn._next; } 231 232 AIX_ONLY(public:) // xlC 12 on AIX doesn't implement C++ DR45. 233 // Allocate a new BufferNode with the "buffer" having size elements. 234 static BufferNode* allocate(size_t size); 235 236 // Free a BufferNode. 237 static void deallocate(BufferNode* node); 238 239 public: 240 typedef LockFreeStack<BufferNode, &next_ptr> Stack; 241 242 BufferNode* next() const { return _next; } 243 void set_next(BufferNode* n) { _next = n; } 244 size_t index() const { return _index; } 245 void set_index(size_t i) { _index = i; } 246 247 // Return the BufferNode containing the buffer, after setting its index. 248 static BufferNode* make_node_from_buffer(void** buffer, size_t index) { 249 BufferNode* node = 250 reinterpret_cast<BufferNode*>( 251 reinterpret_cast<char*>(buffer) - buffer_offset()); 252 node->set_index(index); 253 return node; 254 } 255 256 // Return the buffer for node. 257 static void** make_buffer_from_node(BufferNode *node) { 258 // &_buffer[0] might lead to index out of bounds warnings. 259 return reinterpret_cast<void**>( 260 reinterpret_cast<char*>(node) + buffer_offset()); 261 } 262 263 class Allocator; // Free-list based allocator. 264 class TestSupport; // Unit test support. 265 }; 266 267 // Allocation is based on a lock-free free list of nodes, linked through 268 // BufferNode::_next (see BufferNode::Stack). To solve the ABA problem, 269 // popping a node from the free list is performed within a GlobalCounter 270 // critical section, and pushing nodes onto the free list is done after 271 // a GlobalCounter synchronization associated with the nodes to be pushed. 272 // This is documented behavior so that other parts of the node life-cycle 273 // can depend on and make use of it too. 274 class BufferNode::Allocator { 275 friend class TestSupport; 276 277 // Since we don't expect many instances, and measured >15% speedup 278 // on stress gtest, padding seems like a good tradeoff here. 279 #define DECLARE_PADDED_MEMBER(Id, Type, Name) \ 280 Type Name; DEFINE_PAD_MINUS_SIZE(Id, DEFAULT_CACHE_LINE_SIZE, sizeof(Type)) 281 282 const size_t _buffer_size; 283 char _name[DEFAULT_CACHE_LINE_SIZE - sizeof(size_t)]; // Use name as padding. 284 DECLARE_PADDED_MEMBER(1, Stack, _pending_list); 285 DECLARE_PADDED_MEMBER(2, Stack, _free_list); 286 DECLARE_PADDED_MEMBER(3, volatile size_t, _pending_count); 287 DECLARE_PADDED_MEMBER(4, volatile size_t, _free_count); 288 DECLARE_PADDED_MEMBER(5, volatile bool, _transfer_lock); 289 290 #undef DECLARE_PADDED_MEMBER 291 292 void delete_list(BufferNode* list); 293 bool try_transfer_pending(); 294 295 public: 296 Allocator(const char* name, size_t buffer_size); 297 ~Allocator(); 298 299 const char* name() const { return _name; } 300 size_t buffer_size() const { return _buffer_size; } 301 size_t free_count() const; 302 BufferNode* allocate(); 303 void release(BufferNode* node); 304 305 // Deallocate some of the available buffers. remove_goal is the target 306 // number to remove. Returns the number actually deallocated, which may 307 // be less than the goal if there were fewer available. 308 size_t reduce_free_list(size_t remove_goal); 309 }; 310 311 // A PtrQueueSet represents resources common to a set of pointer queues. 312 // In particular, the individual queues allocate buffers from this shared 313 // set, and return completed buffers to the set. 314 class PtrQueueSet { 315 BufferNode::Allocator* _allocator; 316 317 Monitor* _cbl_mon; // Protects the fields below. 318 BufferNode* _completed_buffers_head; 319 BufferNode* _completed_buffers_tail; 320 size_t _n_completed_buffers; 321 322 size_t _process_completed_buffers_threshold; 323 volatile bool _process_completed_buffers; 324 325 // If true, notify_all on _cbl_mon when the threshold is reached. 326 bool _notify_when_complete; 327 328 // Maximum number of elements allowed on completed queue: after that, 329 // enqueuer does the work itself. 330 size_t _max_completed_buffers; 331 size_t _completed_buffers_padding; 332 333 void assert_completed_buffers_list_len_correct_locked() NOT_DEBUG_RETURN; 334 335 protected: 336 bool _all_active; 337 338 // A mutator thread does the the work of processing a buffer. 339 // Returns "true" iff the work is complete (and the buffer may be 340 // deallocated). 341 virtual bool mut_process_buffer(BufferNode* node) { 342 ShouldNotReachHere(); 343 return false; 344 } 345 346 // Create an empty ptr queue set. 347 PtrQueueSet(bool notify_when_complete = false); 348 ~PtrQueueSet(); 349 350 // Because of init-order concerns, we can't pass these as constructor 351 // arguments. 352 void initialize(Monitor* cbl_mon, BufferNode::Allocator* allocator); 353 354 // For (unlocked!) iteration over the completed buffers. 355 BufferNode* completed_buffers_head() const { return _completed_buffers_head; } 356 357 // Deallocate all of the completed buffers. 358 void abandon_completed_buffers(); 359 360 public: 361 362 // Return the buffer for a BufferNode of size buffer_size(). 363 void** allocate_buffer(); 364 365 // Return an empty buffer to the free list. The node is required 366 // to have been allocated with a size of buffer_size(). 367 void deallocate_buffer(BufferNode* node); 368 369 // A completed buffer is a buffer the mutator is finished with, and 370 // is ready to be processed by the collector. It need not be full. 371 372 // Adds node to the completed buffer list. 373 void enqueue_completed_buffer(BufferNode* node); 374 375 // If the number of completed buffers is > stop_at, then remove and 376 // return a completed buffer from the list. Otherwise, return NULL. 377 BufferNode* get_completed_buffer(size_t stop_at = 0); 378 379 // To be invoked by the mutator. 380 bool process_or_enqueue_completed_buffer(BufferNode* node); 381 382 bool process_completed_buffers() { return _process_completed_buffers; } 383 void set_process_completed_buffers(bool x) { _process_completed_buffers = x; } 384 385 bool is_active() { return _all_active; } 386 387 size_t buffer_size() const { 388 return _allocator->buffer_size(); 389 } 390 391 // Get/Set the number of completed buffers that triggers log processing. 392 // Log processing should be done when the number of buffers exceeds the 393 // threshold. 394 void set_process_completed_buffers_threshold(size_t sz) { 395 _process_completed_buffers_threshold = sz; 396 } 397 size_t process_completed_buffers_threshold() const { 398 return _process_completed_buffers_threshold; 399 } 400 static const size_t ProcessCompletedBuffersThresholdNever = ~size_t(0); 401 402 size_t completed_buffers_num() const { return _n_completed_buffers; } 403 404 void merge_bufferlists(PtrQueueSet* src); 405 406 void set_max_completed_buffers(size_t m) { 407 _max_completed_buffers = m; 408 } 409 size_t max_completed_buffers() const { 410 return _max_completed_buffers; 411 } 412 static const size_t MaxCompletedBuffersUnlimited = ~size_t(0); 413 414 void set_completed_buffers_padding(size_t padding) { 415 _completed_buffers_padding = padding; 416 } 417 size_t completed_buffers_padding() const { 418 return _completed_buffers_padding; 419 } 420 421 // Notify the consumer if the number of buffers crossed the threshold 422 void notify_if_necessary(); 423 }; 424 425 #endif // SHARE_GC_SHARED_PTRQUEUE_HPP