1 /*
  2  * Copyright (c) 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_OOPSTORAGEPARSTATE_HPP
 26 #define SHARE_GC_SHARED_OOPSTORAGEPARSTATE_HPP
 27 
 28 #include "gc/shared/oopStorage.hpp"
 29 #include "utilities/macros.hpp"
 30 
 31 //////////////////////////////////////////////////////////////////////////////
 32 // Support for parallel and optionally concurrent state iteration.
 33 //
 34 // Parallel iteration is for the exclusive use of the GC.  Other iteration
 35 // clients must use serial iteration.
 36 //
 37 // Concurrent Iteration
 38 //
 39 // Iteration involves the _active_array (an ActiveArray), which contains all
 40 // of the blocks owned by a storage object.
 41 //
 42 // At most one concurrent ParState can exist at a time for a given storage
 43 // object.
 44 //
 45 // A concurrent ParState sets the associated storage's
 46 // _concurrent_iteration_active flag true when the state is constructed, and
 47 // sets it false when the state is destroyed.  These assignments are made with
 48 // _active_mutex locked.  Meanwhile, empty block deletion is not done while
 49 // _concurrent_iteration_active is true.  The flag check and the dependent
 50 // removal of a block from the _active_array is performed with _active_mutex
 51 // locked.  This prevents concurrent iteration and empty block deletion from
 52 // interfering with with each other.
 53 //
 54 // Both allocate() and delete_empty_blocks_concurrent() lock the
 55 // _allocation_mutex while performing their respective list and array
 56 // manipulations, preventing them from interfering with each other.
 57 //
 58 // When allocate() creates a new block, it is added to the end of the
 59 // _active_array.  Then _active_array's _block_count is incremented to account
 60 // for the new block.  When concurrent iteration is started (by a parallel
 61 // worker thread calling the state's iterate() function), the current
 62 // _active_array and its _block_count are captured for use by the iteration,
 63 // with iteration processing all blocks in that array up to that block count.
 64 //
 65 // As a result, the sequence over which concurrent iteration operates is
 66 // stable.  However, once the iteration is started, later allocations may add
 67 // blocks to the end of the array that won't be examined by the iteration.
 68 // An allocation may even require expansion of the array, so the iteration is
 69 // no longer processing the current array, but rather the previous one.
 70 // And while the sequence is stable, concurrent allocate() and release()
 71 // operations may change the set of allocated entries in a block at any time
 72 // during the iteration.
 73 //
 74 // As a result, a concurrent iteration handler must accept that some
 75 // allocations and releases that occur after the iteration started will not be
 76 // seen by the iteration.  Further, some may overlap examination by the
 77 // iteration.  To help with this, allocate() and release() have an invariant
 78 // that an entry's value must be NULL when it is not in use.
 79 //
 80 // An in-progress delete_empty_blocks_concurrent() operation can contend with
 81 // the start of a concurrent iteration over the _active_mutex.  Since both are
 82 // under GC control, that potential contention can be eliminated by never
 83 // scheduling both operations to run at the same time.
 84 //
 85 // ParState<concurrent, is_const>
 86 //   concurrent must be true if iteration is concurrent with the
 87 //   mutator, false if iteration is at a safepoint.
 88 //
 89 //   is_const must be true if the iteration is over a constant storage
 90 //   object, false if the iteration may modify the storage object.
 91 //
 92 // ParState([const] OopStorage* storage)
 93 //   Construct an object for managing an iteration over storage.  For a
 94 //   concurrent ParState, empty block deletion for the associated storage
 95 //   is inhibited for the life of the ParState.  There can be no more
 96 //   than one live concurrent ParState at a time for a given storage object.
 97 //
 98 // template<typename F> void iterate(F f)
 99 //   Repeatedly claims a block from the associated storage that has
100 //   not been processed by this iteration (possibly by other threads),
101 //   and applies f to each entry in the claimed block. Assume p is of
102 //   type const oop* or oop*, according to is_const. Then f(p) must be
103 //   a valid expression whose value is ignored.  Concurrent uses must
104 //   be prepared for an entry's value to change at any time, due to
105 //   mutator activity.
106 //
107 // template<typename Closure> void oops_do(Closure* cl)
108 //   Wrapper around iterate, providing an adaptation layer allowing
109 //   the use of OopClosures and similar objects for iteration.  Assume
110 //   p is of type const oop* or oop*, according to is_const.  Then
111 //   cl->do_oop(p) must be a valid expression whose value is ignored.
112 //   Concurrent uses must be prepared for the entry's value to change
113 //   at any time, due to mutator activity.
114 //
115 // Optional operations, provided only if !concurrent && !is_const.
116 // These are not provided when is_const, because the storage object
117 // may be modified by the iteration infrastructure, even if the
118 // provided closure doesn't modify the storage object.  These are not
119 // provided when concurrent because any pre-filtering behavior by the
120 // iteration infrastructure is inappropriate for concurrent iteration;
121 // modifications of the storage by the mutator could result in the
122 // pre-filtering being applied (successfully or not) to objects that
123 // are unrelated to what the closure finds in the entry.
124 //
125 // template<typename Closure> void weak_oops_do(Closure* cl)
126 // template<typename IsAliveClosure, typename Closure>
127 // void weak_oops_do(IsAliveClosure* is_alive, Closure* cl)
128 //   Wrappers around iterate, providing an adaptation layer allowing
129 //   the use of is-alive closures and OopClosures for iteration.
130 //   Assume p is of type oop*.  Then
131 //
132 //   - cl->do_oop(p) must be a valid expression whose value is ignored.
133 //
134 //   - is_alive->do_object_b(*p) must be a valid expression whose value
135 //   is convertible to bool.
136 //
137 //   If *p == NULL then neither is_alive nor cl will be invoked for p.
138 //   If is_alive->do_object_b(*p) is false, then cl will not be
139 //   invoked on p.
140 
141 class OopStorage::BasicParState {
142   const OopStorage* _storage;
143   ActiveArray* _active_array;
144   size_t _block_count;
145   volatile size_t _next_block;
146   uint _estimated_thread_count;
147   bool _concurrent;
148 
149   // Noncopyable.
150   BasicParState(const BasicParState&);
151   BasicParState& operator=(const BasicParState&);
152 
153   struct IterationData;
154 
155   void update_iteration_state(bool value);
156   bool claim_next_segment(IterationData* data);
157   bool finish_iteration(const IterationData* data) const;
158 
159   // Wrapper for iteration handler; ignore handler result and return true.
160   template<typename F> class AlwaysTrueFn;
161 
162 public:
163   BasicParState(const OopStorage* storage,
164                 uint estimated_thread_count,
165                 bool concurrent);
166   ~BasicParState();
167 
168   template<bool is_const, typename F> void iterate(F f);
169 
170   static uint default_estimated_thread_count(bool concurrent);
171 };
172 
173 template<bool concurrent, bool is_const>
174 class OopStorage::ParState {
175   BasicParState _basic_state;
176 
177   typedef typename Conditional<is_const,
178                                const OopStorage*,
179                                OopStorage*>::type StoragePtr;
180 
181 public:
182   ParState(StoragePtr storage,
183            uint estimated_thread_count = BasicParState::default_estimated_thread_count(concurrent)) :
184     _basic_state(storage, estimated_thread_count, concurrent)
185   {}
186 
187   template<typename F> void iterate(F f);
188   template<typename Closure> void oops_do(Closure* cl);
189 };
190 
191 template<>
192 class OopStorage::ParState<false, false> {
193   BasicParState _basic_state;
194 
195 public:
196   ParState(OopStorage* storage,
197            uint estimated_thread_count = BasicParState::default_estimated_thread_count(false)) :
198     _basic_state(storage, estimated_thread_count, false)
199   {}
200 
201   template<typename F> void iterate(F f);
202   template<typename Closure> void oops_do(Closure* cl);
203   template<typename Closure> void weak_oops_do(Closure* cl);
204   template<typename IsAliveClosure, typename Closure>
205   void weak_oops_do(IsAliveClosure* is_alive, Closure* cl);
206 };
207 
208 #endif // SHARE_GC_SHARED_OOPSTORAGEPARSTATE_HPP