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