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_OOPSTORAGE_HPP 26 #define SHARE_GC_SHARED_OOPSTORAGE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "oops/oop.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 #include "utilities/macros.hpp" 32 #include "utilities/singleWriterSynchronizer.hpp" 33 34 class Mutex; 35 class outputStream; 36 37 // OopStorage supports management of off-heap references to objects allocated 38 // in the Java heap. An OopStorage object provides a set of Java object 39 // references (oop values), which clients refer to via oop* handles to the 40 // associated OopStorage entries. Clients allocate entries to create a 41 // (possibly weak) reference to a Java object, use that reference, and release 42 // the reference when no longer needed. 43 // 44 // The garbage collector must know about all OopStorage objects and their 45 // reference strength. OopStorage provides the garbage collector with support 46 // for iteration over all the allocated entries. 47 // 48 // There are several categories of interaction with an OopStorage object. 49 // 50 // (1) allocation and release of entries, by the mutator or the VM. 51 // (2) iteration by the garbage collector, possibly concurrent with mutator. 52 // (3) iteration by other, non-GC, tools (only at safepoints). 53 // (4) cleanup of unused internal storage, possibly concurrent with mutator. 54 // 55 // A goal of OopStorage is to make these interactions thread-safe, while 56 // minimizing potential lock contention issues within and between these 57 // categories. In particular, support for concurrent iteration by the garbage 58 // collector, under certain restrictions, is required. Further, it must not 59 // block nor be blocked by other operations for long periods. 60 // 61 // Internally, OopStorage is a set of Block objects, from which entries are 62 // allocated and released. A block contains an oop[] and a bitmask indicating 63 // which entries are in use (have been allocated and not yet released). New 64 // blocks are constructed and added to the storage object when an entry 65 // allocation request is made and there are no blocks with unused entries. 66 // Blocks may be removed and deleted when empty. 67 // 68 // There are two important (and somewhat intertwined) protocols governing 69 // concurrent access to a storage object. These are the Concurrent Iteration 70 // Protocol and the Allocation Protocol. See the ParState class for a 71 // discussion of concurrent iteration and the management of thread 72 // interactions for this protocol. Similarly, see the allocate() function for 73 // a discussion of allocation. 74 75 class OopStorage : public CHeapObj<mtGC> { 76 public: 77 OopStorage(const char* name, Mutex* allocation_mutex, Mutex* active_mutex); 78 ~OopStorage(); 79 80 // These count and usage accessors are racy unless at a safepoint. 81 82 // The number of allocated and not yet released entries. 83 size_t allocation_count() const; 84 85 // The number of blocks of entries. Useful for sizing parallel iteration. 86 size_t block_count() const; 87 88 // Total number of blocks * memory allocation per block, plus 89 // bookkeeping overhead, including this storage object. 90 size_t total_memory_usage() const; 91 92 enum EntryStatus { 93 INVALID_ENTRY, 94 UNALLOCATED_ENTRY, 95 ALLOCATED_ENTRY 96 }; 97 98 // Locks _allocation_mutex. 99 // precondition: ptr != NULL. 100 EntryStatus allocation_status(const oop* ptr) const; 101 102 // Allocates and returns a new entry. Returns NULL if memory allocation 103 // failed. Locks _allocation_mutex. 104 // postcondition: *result == NULL. 105 oop* allocate(); 106 107 // Deallocates ptr. No locking. 108 // precondition: ptr is a valid allocated entry. 109 // precondition: *ptr == NULL. 110 void release(const oop* ptr); 111 112 // Releases all the ptrs. Possibly faster than individual calls to 113 // release(oop*). Best if ptrs is sorted by address. No locking. 114 // precondition: All elements of ptrs are valid allocated entries. 115 // precondition: *ptrs[i] == NULL, for i in [0,size). 116 void release(const oop* const* ptrs, size_t size); 117 118 // Applies f to each allocated entry's location. f must be a function or 119 // function object. Assume p is either a const oop* or an oop*, depending 120 // on whether the associated storage is const or non-const, respectively. 121 // Then f(p) must be a valid expression. The result of invoking f(p) must 122 // be implicitly convertible to bool. Iteration terminates and returns 123 // false if any invocation of f returns false. Otherwise, the result of 124 // iteration is true. 125 // precondition: at safepoint. 126 template<typename F> inline bool iterate_safepoint(F f); 127 template<typename F> inline bool iterate_safepoint(F f) const; 128 129 // oops_do and weak_oops_do are wrappers around iterate_safepoint, providing 130 // an adaptation layer allowing the use of existing is-alive closures and 131 // OopClosures. Assume p is either const oop* or oop*, depending on whether 132 // the associated storage is const or non-const, respectively. Then 133 // 134 // - closure->do_oop(p) must be a valid expression whose value is ignored. 135 // 136 // - is_alive->do_object_b(*p) must be a valid expression whose value is 137 // convertible to bool. 138 // 139 // For weak_oops_do, if *p == NULL then neither is_alive nor closure will be 140 // invoked for p. If is_alive->do_object_b(*p) is false, then closure will 141 // not be invoked on p, and *p will be set to NULL. 142 143 template<typename Closure> inline void oops_do(Closure* closure); 144 template<typename Closure> inline void oops_do(Closure* closure) const; 145 template<typename Closure> inline void weak_oops_do(Closure* closure); 146 147 template<typename IsAliveClosure, typename Closure> 148 inline void weak_oops_do(IsAliveClosure* is_alive, Closure* closure); 149 150 // Parallel iteration is for the exclusive use of the GC. 151 // Other clients must use serial iteration. 152 template<bool concurrent, bool is_const> class ParState; 153 154 // Block cleanup functions are for the exclusive use of the GC. 155 // Both stop deleting if there is an in-progress concurrent iteration. 156 // Concurrent deletion locks both the _allocation_mutex and the _active_mutex. 157 void delete_empty_blocks_safepoint(); 158 void delete_empty_blocks_concurrent(); 159 160 // Debugging and logging support. 161 const char* name() const; 162 void print_on(outputStream* st) const PRODUCT_RETURN; 163 164 // Provides access to storage internals, for unit testing. 165 // Declare, but not define, the public class OopStorage::TestAccess. 166 // That class is defined as part of the unit-test. It "exports" the needed 167 // private types by providing public typedefs for them. 168 class TestAccess; 169 170 // xlC on AIX can't compile test_oopStorage.cpp with following private 171 // classes. C++03 introduced access for nested classes with DR45, but xlC 172 // version 12 rejects it. 173 NOT_AIX( private: ) 174 class Block; // Fixed-size array of oops, plus bookkeeping. 175 class ActiveArray; // Array of Blocks, plus bookkeeping. 176 class AllocationListEntry; // Provides AllocationList links in a Block. 177 178 // Doubly-linked list of Blocks. 179 class AllocationList { 180 const Block* _head; 181 const Block* _tail; 182 183 // Noncopyable. 184 AllocationList(const AllocationList&); 185 AllocationList& operator=(const AllocationList&); 186 187 public: 188 AllocationList(); 189 ~AllocationList(); 190 191 Block* head(); 192 Block* tail(); 193 const Block* chead() const; 194 const Block* ctail() const; 195 196 Block* prev(Block& block); 197 Block* next(Block& block); 198 199 const Block* prev(const Block& block) const; 200 const Block* next(const Block& block) const; 201 202 void push_front(const Block& block); 203 void push_back(const Block& block); 204 void unlink(const Block& block); 205 }; 206 207 private: 208 const char* _name; 209 ActiveArray* _active_array; 210 AllocationList _allocation_list; 211 Block* volatile _deferred_updates; 212 213 Mutex* _allocation_mutex; 214 Mutex* _active_mutex; 215 216 // Volatile for racy unlocked accesses. 217 volatile size_t _allocation_count; 218 219 // Protection for _active_array. 220 mutable SingleWriterSynchronizer _protect_active; 221 222 // mutable because this gets set even for const iteration. 223 mutable int _concurrent_iteration_active; 224 225 Block* find_block_or_null(const oop* ptr) const; 226 void delete_empty_block(const Block& block); 227 bool reduce_deferred_updates(); 228 229 // Managing _active_array. 230 bool expand_active_array(); 231 void replace_active_array(ActiveArray* new_array); 232 ActiveArray* obtain_active_array() const; 233 void relinquish_block_array(ActiveArray* array) const; 234 class WithActiveArray; // RAII helper for active array access. 235 236 template<typename F, typename Storage> 237 static bool iterate_impl(F f, Storage* storage); 238 239 // Implementation support for parallel iteration 240 class BasicParState; 241 242 // Wrapper for OopClosure-style function, so it can be used with 243 // iterate. Assume p is of type oop*. Then cl->do_oop(p) must be a 244 // valid expression whose value may be ignored. 245 template<typename Closure> class OopFn; 246 template<typename Closure> static OopFn<Closure> oop_fn(Closure* cl); 247 248 // Wrapper for BoolObjectClosure + iteration handler pair, so they 249 // can be used with iterate. 250 template<typename IsAlive, typename F> class IfAliveFn; 251 template<typename IsAlive, typename F> 252 static IfAliveFn<IsAlive, F> if_alive_fn(IsAlive* is_alive, F f); 253 254 // Wrapper for iteration handler, automatically skipping NULL entries. 255 template<typename F> class SkipNullFn; 256 template<typename F> static SkipNullFn<F> skip_null_fn(F f); 257 }; 258 259 #endif // include guard