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_G1_G1DIRTYCARDQUEUE_HPP 26 #define SHARE_GC_G1_G1DIRTYCARDQUEUE_HPP 27 28 #include "gc/g1/g1BufferNodeList.hpp" 29 #include "gc/g1/g1FreeIdSet.hpp" 30 #include "gc/shared/ptrQueue.hpp" 31 #include "memory/allocation.hpp" 32 33 class G1DirtyCardQueueSet; 34 class G1RedirtyCardsQueueSet; 35 class Thread; 36 class Monitor; 37 38 // A ptrQueue whose elements are "oops", pointers to object heads. 39 class G1DirtyCardQueue: public PtrQueue { 40 protected: 41 virtual void handle_completed_buffer(); 42 43 public: 44 G1DirtyCardQueue(G1DirtyCardQueueSet* qset); 45 46 // Flush before destroying; queue may be used to capture pending work while 47 // doing something else, with auto-flush on completion. 48 ~G1DirtyCardQueue(); 49 50 // Process queue entries and release resources. 51 void flush() { flush_impl(); } 52 53 inline G1DirtyCardQueueSet* dirty_card_qset() const; 54 55 // Compiler support. 56 static ByteSize byte_offset_of_index() { 57 return PtrQueue::byte_offset_of_index<G1DirtyCardQueue>(); 58 } 59 using PtrQueue::byte_width_of_index; 60 61 static ByteSize byte_offset_of_buf() { 62 return PtrQueue::byte_offset_of_buf<G1DirtyCardQueue>(); 63 } 64 using PtrQueue::byte_width_of_buf; 65 66 }; 67 68 class G1DirtyCardQueueSet: public PtrQueueSet { 69 Monitor* _cbl_mon; // Protects the list and count members. 70 BufferNode* _completed_buffers_head; 71 BufferNode* _completed_buffers_tail; 72 73 // Number of actual cards in the list of completed buffers. 74 volatile size_t _num_cards; 75 76 size_t _process_cards_threshold; 77 volatile bool _process_completed_buffers; 78 79 void abandon_completed_buffers(); 80 81 // Refine the cards in "node" from it's index to buffer_size. 82 // Stops processing if SuspendibleThreadSet::should_yield() is true. 83 // Returns true if the entire buffer was processed, false if there 84 // is a pending yield request. The node's index is updated to exclude 85 // the processed elements, e.g. up to the element before processing 86 // stopped, or one past the last element if the entire buffer was 87 // processed. 88 bool refine_buffer(BufferNode* node, uint worker_id); 89 90 bool mut_process_buffer(BufferNode* node); 91 92 // If the queue contains more cards than configured here, the 93 // mutator must start doing some of the concurrent refinement work. 94 size_t _max_cards; 95 size_t _max_cards_padding; 96 static const size_t MaxCardsUnlimited = SIZE_MAX; 97 98 G1FreeIdSet _free_ids; 99 100 // The number of completed buffers processed by mutator and rs thread, 101 // respectively. 102 jint _processed_buffers_mut; 103 jint _processed_buffers_rs_thread; 104 105 public: 106 G1DirtyCardQueueSet(Monitor* cbl_mon, BufferNode::Allocator* allocator); 107 ~G1DirtyCardQueueSet(); 108 109 // The number of parallel ids that can be claimed to allow collector or 110 // mutator threads to do card-processing work. 111 static uint num_par_ids(); 112 113 static void handle_zero_index_for_thread(Thread* t); 114 115 // Either process the entire buffer and return true, or enqueue the 116 // buffer and return false. If the buffer is completely processed, 117 // it can be reused in place. 118 bool process_or_enqueue_completed_buffer(BufferNode* node); 119 120 virtual void enqueue_completed_buffer(BufferNode* node); 121 122 // If the number of completed buffers is > stop_at, then remove and 123 // return a completed buffer from the list. Otherwise, return NULL. 124 BufferNode* get_completed_buffer(size_t stop_at = 0); 125 126 // The number of cards in completed buffers. Read without synchronization. 127 size_t num_cards() const { return _num_cards; } 128 129 // Verify that _num_cards is equal to the sum of actual cards 130 // in the completed buffers. 131 void verify_num_cards() const NOT_DEBUG_RETURN; 132 133 bool process_completed_buffers() { return _process_completed_buffers; } 134 void set_process_completed_buffers(bool x) { _process_completed_buffers = x; } 135 136 // Get/Set the number of cards that triggers log processing. 137 // Log processing should be done when the number of cards exceeds the 138 // threshold. 139 void set_process_cards_threshold(size_t sz) { 140 _process_cards_threshold = sz; 141 } 142 size_t process_cards_threshold() const { 143 return _process_cards_threshold; 144 } 145 static const size_t ProcessCardsThresholdNever = SIZE_MAX; 146 147 // Notify the consumer if the number of buffers crossed the threshold 148 void notify_if_necessary(); 149 150 void merge_bufferlists(G1RedirtyCardsQueueSet* src); 151 152 G1BufferNodeList take_all_completed_buffers(); 153 154 // If there are more than stop_at cards in the completed buffers, pop 155 // a buffer, refine its contents, and return true. Otherwise return 156 // false. 157 // 158 // Stops processing a buffer if SuspendibleThreadSet::should_yield(), 159 // returning the incompletely processed buffer to the completed buffer 160 // list, for later processing of the remainder. 161 bool refine_completed_buffer_concurrently(uint worker_i, size_t stop_at); 162 163 // If a full collection is happening, reset partial logs, and release 164 // completed ones: the full collection will make them all irrelevant. 165 void abandon_logs(); 166 167 // If any threads have partial logs, add them to the global list of logs. 168 void concatenate_logs(); 169 170 void set_max_cards(size_t m) { 171 _max_cards = m; 172 } 173 size_t max_cards() const { 174 return _max_cards; 175 } 176 177 void set_max_cards_padding(size_t padding) { 178 _max_cards_padding = padding; 179 } 180 size_t max_cards_padding() const { 181 return _max_cards_padding; 182 } 183 184 jint processed_buffers_mut() { 185 return _processed_buffers_mut; 186 } 187 jint processed_buffers_rs_thread() { 188 return _processed_buffers_rs_thread; 189 } 190 191 }; 192 193 inline G1DirtyCardQueueSet* G1DirtyCardQueue::dirty_card_qset() const { 194 return static_cast<G1DirtyCardQueueSet*>(qset()); 195 } 196 197 #endif // SHARE_GC_G1_G1DIRTYCARDQUEUE_HPP