1 /*
2  * Copyright (c) 2001, 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 #ifndef SHARE_GC_SHARED_SATBMARKQUEUE_HPP
26 #define SHARE_GC_SHARED_SATBMARKQUEUE_HPP
27 
28 #include "gc/shared/ptrQueue.hpp"
29 #include "memory/allocation.hpp"
30 
31 class JavaThread;
32 class SATBMarkQueueSet;
33 
34 // Base class for processing the contents of a SATB buffer.
35 class SATBBufferClosure : public StackObj {
36 protected:
~SATBBufferClosure()37   ~SATBBufferClosure() { }
38 
39 public:
40   // Process the SATB entries in the designated buffer range.
41   virtual void do_buffer(void** buffer, size_t size) = 0;
42 };
43 
44 // A PtrQueue whose elements are (possibly stale) pointers to object heads.
45 class SATBMarkQueue: public PtrQueue {
46   friend class SATBMarkQueueSet;
47 
48 private:
49   // Filter out unwanted entries from the buffer.
50   inline void filter();
51 
52   // Removes entries from the buffer that are no longer needed.
53   template<typename Filter>
54   inline void apply_filter(Filter filter_out);
55 
56 public:
57   SATBMarkQueue(SATBMarkQueueSet* qset, bool permanent = false);
58 
59   // Process queue entries and free resources.
60   void flush();
61 
62   // Apply cl to the active part of the buffer.
63   // Prerequisite: Must be at a safepoint.
64   void apply_closure_and_empty(SATBBufferClosure* cl);
65 
66   // Overrides PtrQueue::should_enqueue_buffer(). See the method's
67   // definition for more information.
68   virtual bool should_enqueue_buffer();
69 
70 #ifndef PRODUCT
71   // Helpful for debugging
72   void print(const char* name);
73 #endif // PRODUCT
74 
75   // Compiler support.
byte_offset_of_index()76   static ByteSize byte_offset_of_index() {
77     return PtrQueue::byte_offset_of_index<SATBMarkQueue>();
78   }
79   using PtrQueue::byte_width_of_index;
80 
byte_offset_of_buf()81   static ByteSize byte_offset_of_buf() {
82     return PtrQueue::byte_offset_of_buf<SATBMarkQueue>();
83   }
84   using PtrQueue::byte_width_of_buf;
85 
byte_offset_of_active()86   static ByteSize byte_offset_of_active() {
87     return PtrQueue::byte_offset_of_active<SATBMarkQueue>();
88   }
89   using PtrQueue::byte_width_of_active;
90 
91 };
92 
93 class SATBMarkQueueSet: public PtrQueueSet {
94   SATBMarkQueue _shared_satb_queue;
95   size_t _buffer_enqueue_threshold;
96 
97 #ifdef ASSERT
98   void dump_active_states(bool expected_active);
99   void verify_active_states(bool expected_active);
100 #endif // ASSERT
101 
102 protected:
103   SATBMarkQueueSet();
~SATBMarkQueueSet()104   ~SATBMarkQueueSet() {}
105 
106   template<typename Filter>
apply_filter(Filter filter,SATBMarkQueue * queue)107   void apply_filter(Filter filter, SATBMarkQueue* queue) {
108     queue->apply_filter(filter);
109   }
110 
111   void initialize(Monitor* cbl_mon,
112                   BufferNode::Allocator* allocator,
113                   size_t process_completed_buffers_threshold,
114                   uint buffer_enqueue_threshold_percentage,
115                   Mutex* lock);
116 
117 public:
118   virtual SATBMarkQueue& satb_queue_for_thread(JavaThread* const t) const = 0;
119 
120   // Apply "set_active(active)" to all SATB queues in the set. It should be
121   // called only with the world stopped. The method will assert that the
122   // SATB queues of all threads it visits, as well as the SATB queue
123   // set itself, has an active value same as expected_active.
124   void set_active_all_threads(bool active, bool expected_active);
125 
buffer_enqueue_threshold() const126   size_t buffer_enqueue_threshold() const { return _buffer_enqueue_threshold; }
127   virtual void filter(SATBMarkQueue* queue) = 0;
128 
129   // Filter all the currently-active SATB buffers.
130   void filter_thread_buffers();
131 
132   // If there exists some completed buffer, pop and process it, and
133   // return true.  Otherwise return false.  Processing a buffer
134   // consists of applying the closure to the active range of the
135   // buffer; the leading entries may be excluded due to filtering.
136   bool apply_closure_to_completed_buffer(SATBBufferClosure* cl);
137 
138 #ifndef PRODUCT
139   // Helpful for debugging
140   void print_all(const char* msg);
141 #endif // PRODUCT
142 
shared_satb_queue()143   SATBMarkQueue* shared_satb_queue() { return &_shared_satb_queue; }
144 
145   // If a marking is being abandoned, reset any unprocessed log buffers.
146   void abandon_partial_marking();
147 };
148 
filter()149 inline void SATBMarkQueue::filter() {
150   static_cast<SATBMarkQueueSet*>(qset())->filter(this);
151 }
152 
153 // Removes entries from the buffer that are no longer needed, as
154 // determined by filter. If e is a void* entry in the buffer,
155 // filter_out(e) must be a valid expression whose value is convertible
156 // to bool. Entries are removed (filtered out) if the result is true,
157 // retained if false.
158 template<typename Filter>
apply_filter(Filter filter_out)159 inline void SATBMarkQueue::apply_filter(Filter filter_out) {
160   void** buf = this->_buf;
161 
162   if (buf == NULL) {
163     // nothing to do
164     return;
165   }
166 
167   // Two-fingered compaction toward the end.
168   void** src = &buf[this->index()];
169   void** dst = &buf[this->capacity()];
170   assert(src <= dst, "invariant");
171   for ( ; src < dst; ++src) {
172     // Search low to high for an entry to keep.
173     void* entry = *src;
174     if (!filter_out(entry)) {
175       // Found keeper.  Search high to low for an entry to discard.
176       while (src < --dst) {
177         if (filter_out(*dst)) {
178           *dst = entry;         // Replace discard with keeper.
179           break;
180         }
181       }
182       // If discard search failed (src == dst), the outer loop will also end.
183     }
184   }
185   // dst points to the lowest retained entry, or the end of the buffer
186   // if all the entries were filtered out.
187   this->set_index(dst - buf);
188 }
189 
190 #endif // SHARE_GC_SHARED_SATBMARKQUEUE_HPP
191