1 /* 2 * Copyright (c) 2005, 2015, 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_VM_GC_IMPLEMENTATION_SHARED_VMGCOPERATIONS_HPP 26 #define SHARE_VM_GC_IMPLEMENTATION_SHARED_VMGCOPERATIONS_HPP 27 28 #include "gc_interface/collectedHeap.hpp" 29 #include "gc_implementation/shared/gcId.hpp" 30 #include "memory/heapInspection.hpp" 31 #include "runtime/handles.hpp" 32 #include "runtime/jniHandles.hpp" 33 #include "runtime/synchronizer.hpp" 34 #include "runtime/vm_operations.hpp" 35 #include "prims/jvmtiExport.hpp" 36 37 // The following class hierarchy represents 38 // a set of operations (VM_Operation) related to GC. 39 // 40 // VM_Operation 41 // VM_GC_Operation 42 // VM_GC_HeapInspection 43 // VM_GenCollectFull 44 // VM_GenCollectFullConcurrent 45 // VM_ParallelGCSystemGC 46 // VM_CollectForAllocation 47 // VM_GenCollectForAllocation 48 // VM_ParallelGCFailedAllocation 49 // VM_GC_Operation 50 // - implements methods common to all classes in the hierarchy: 51 // prevents multiple gc requests and manages lock on heap; 52 // 53 // VM_GC_HeapInspection 54 // - prints class histogram on SIGBREAK if PrintClassHistogram 55 // is specified; and also the attach "inspectheap" operation 56 // 57 // VM_CollectForAllocation 58 // VM_GenCollectForAllocation 59 // VM_ParallelGCFailedAllocation 60 // - this operation is invoked when allocation is failed; 61 // operation performs garbage collection and tries to 62 // allocate afterwards; 63 // 64 // VM_GenCollectFull 65 // VM_GenCollectFullConcurrent 66 // VM_ParallelGCSystemGC 67 // - these operations preform full collection of heaps of 68 // different kind 69 // 70 71 class VM_GC_Operation: public VM_Operation { 72 protected: 73 BasicLock _pending_list_basic_lock; // for refs pending list notification (PLL) 74 uint _gc_count_before; // gc count before acquiring PLL 75 uint _full_gc_count_before; // full gc count before acquiring PLL 76 bool _full; // whether a "full" collection 77 bool _prologue_succeeded; // whether doit_prologue succeeded 78 GCCause::Cause _gc_cause; // the putative cause for this gc op 79 bool _gc_locked; // will be set if gc was locked 80 81 virtual bool skip_operation() const; 82 83 // java.lang.ref.Reference support 84 void acquire_pending_list_lock(); 85 void release_and_notify_pending_list_lock(); 86 87 public: 88 VM_GC_Operation(uint gc_count_before, 89 GCCause::Cause _cause, 90 uint full_gc_count_before = 0, 91 bool full = false) { 92 _full = full; 93 _prologue_succeeded = false; 94 _gc_count_before = gc_count_before; 95 96 // A subclass constructor will likely overwrite the following 97 _gc_cause = _cause; 98 99 _gc_locked = false; 100 101 _full_gc_count_before = full_gc_count_before; 102 // In ParallelScavengeHeap::mem_allocate() collections can be 103 // executed within a loop and _all_soft_refs_clear can be set 104 // true after they have been cleared by a collection and another 105 // collection started so that _all_soft_refs_clear can be true 106 // when this collection is started. Don't assert that 107 // _all_soft_refs_clear have to be false here even though 108 // mutators have run. Soft refs will be cleared again in this 109 // collection. 110 } 111 ~VM_GC_Operation(); 112 113 // Acquire the reference synchronization lock 114 virtual bool doit_prologue(); 115 // Do notifyAll (if needed) and release held lock 116 virtual void doit_epilogue(); 117 118 virtual bool allow_nested_vm_operations() const { return true; } 119 bool prologue_succeeded() const { return _prologue_succeeded; } 120 121 void set_gc_locked() { _gc_locked = true; } 122 bool gc_locked() const { return _gc_locked; } 123 124 static void notify_gc_begin(bool full = false); 125 static void notify_gc_end(); 126 }; 127 128 129 class VM_GC_HeapInspection: public VM_GC_Operation { 130 private: 131 outputStream* _out; 132 bool _full_gc; 133 bool _csv_format; // "comma separated values" format for spreadsheet. 134 bool _print_help; 135 bool _print_class_stats; 136 const char* _columns; 137 public: 138 VM_GC_HeapInspection(outputStream* out, bool request_full_gc) : 139 VM_GC_Operation(0 /* total collections, dummy, ignored */, 140 GCCause::_heap_inspection /* GC Cause */, 141 0 /* total full collections, dummy, ignored */, 142 request_full_gc) { 143 _out = out; 144 _full_gc = request_full_gc; 145 _csv_format = false; 146 _print_help = false; 147 _print_class_stats = false; 148 _columns = NULL; 149 } 150 151 ~VM_GC_HeapInspection() {} 152 virtual VMOp_Type type() const { return VMOp_GC_HeapInspection; } 153 virtual bool skip_operation() const; 154 virtual bool doit_prologue(); 155 virtual void doit(); 156 void set_csv_format(bool value) {_csv_format = value;} 157 void set_print_help(bool value) {_print_help = value;} 158 void set_print_class_stats(bool value) {_print_class_stats = value;} 159 void set_columns(const char* value) {_columns = value;} 160 protected: 161 bool collect(); 162 }; 163 164 class VM_CollectForAllocation : public VM_GC_Operation { 165 protected: 166 size_t _word_size; // Size of object to be allocated (in number of words) 167 HeapWord* _result; // Allocation result (NULL if allocation failed) 168 uint _gc_attempt; // Collection attempt for this allocation. 169 GCId _gcid; // Predicted GCId for this operation. 170 171 public: 172 VM_CollectForAllocation(size_t word_size, uint gc_count_before, GCCause::Cause cause, uint gc_attempt); 173 174 // The epilogue is run by the requesting thread after and if the collection happened. 175 // It is extended here to trace collections performed due to failed allocations. 176 void doit_epilogue(); 177 178 HeapWord* result() const { 179 return _result; 180 } 181 }; 182 183 class VM_GenCollectForAllocation : public VM_CollectForAllocation { 184 private: 185 bool _tlab; // alloc is of a tlab. 186 public: 187 VM_GenCollectForAllocation(size_t word_size, 188 bool tlab, 189 uint gc_count_before, 190 uint gc_attempt) 191 : VM_CollectForAllocation(word_size, gc_count_before, GCCause::_allocation_failure, gc_attempt), 192 _tlab(tlab) { 193 assert(word_size != 0, "An allocation should always be requested with this operation."); 194 } 195 ~VM_GenCollectForAllocation() {} 196 virtual VMOp_Type type() const { return VMOp_GenCollectForAllocation; } 197 virtual void doit(); 198 }; 199 200 // VM operation to invoke a collection of the heap as a 201 // GenCollectedHeap heap. 202 class VM_GenCollectFull: public VM_GC_Operation { 203 private: 204 int _max_level; 205 public: 206 VM_GenCollectFull(uint gc_count_before, 207 uint full_gc_count_before, 208 GCCause::Cause gc_cause, 209 int max_level) 210 : VM_GC_Operation(gc_count_before, gc_cause, full_gc_count_before, true /* full */), 211 _max_level(max_level) { } 212 ~VM_GenCollectFull() {} 213 virtual VMOp_Type type() const { return VMOp_GenCollectFull; } 214 virtual void doit(); 215 }; 216 217 class VM_CollectForMetadataAllocation: public VM_GC_Operation { 218 private: 219 MetaWord* _result; 220 size_t _size; // size of object to be allocated 221 Metaspace::MetadataType _mdtype; 222 ClassLoaderData* _loader_data; 223 uint _gc_attempt; // Collection attempt for this allocation. 224 GCId _gcid; // Predicted GCId for this operation. 225 public: 226 VM_CollectForMetadataAllocation(ClassLoaderData* loader_data, 227 size_t size, Metaspace::MetadataType mdtype, 228 uint gc_count_before, 229 uint full_gc_count_before, 230 GCCause::Cause gc_cause, 231 uint gc_attempt); 232 virtual VMOp_Type type() const { return VMOp_CollectForMetadataAllocation; } 233 virtual void doit(); 234 void doit_epilogue(); 235 MetaWord* result() const { return _result; } 236 237 bool initiate_concurrent_GC(); 238 }; 239 240 class SvcGCMarker : public StackObj { 241 private: 242 JvmtiGCMarker _jgcm; 243 public: 244 typedef enum { MINOR, FULL, OTHER } reason_type; 245 246 SvcGCMarker(reason_type reason ) { 247 VM_GC_Operation::notify_gc_begin(reason == FULL); 248 } 249 250 ~SvcGCMarker() { 251 VM_GC_Operation::notify_gc_end(); 252 } 253 }; 254 255 #endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_VMGCOPERATIONS_HPP