vmGCOperations.hpp 8.6 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27
#ifndef SHARE_VM_GC_IMPLEMENTATION_SHARED_VMGCOPERATIONS_HPP
#define SHARE_VM_GC_IMPLEMENTATION_SHARED_VMGCOPERATIONS_HPP

28
#include "gc_implementation/shared/gcId.hpp"
29 30 31 32 33
#include "memory/heapInspection.hpp"
#include "runtime/handles.hpp"
#include "runtime/jniHandles.hpp"
#include "runtime/synchronizer.hpp"
#include "runtime/vm_operations.hpp"
34
#include "prims/jvmtiExport.hpp"
35

D
duke 已提交
36 37 38 39 40 41 42 43 44
// The following class hierarchy represents
// a set of operations (VM_Operation) related to GC.
//
//  VM_Operation
//      VM_GC_Operation
//          VM_GC_HeapInspection
//          VM_GenCollectFull
//          VM_GenCollectFullConcurrent
//          VM_ParallelGCSystemGC
45 46 47
//          VM_CollectForAllocation
//              VM_GenCollectForAllocation
//              VM_ParallelGCFailedAllocation
D
duke 已提交
48 49 50 51 52 53 54 55
//  VM_GC_Operation
//   - implements methods common to all classes in the hierarchy:
//     prevents multiple gc requests and manages lock on heap;
//
//  VM_GC_HeapInspection
//   - prints class histogram on SIGBREAK if PrintClassHistogram
//     is specified; and also the attach "inspectheap" operation
//
56
//  VM_CollectForAllocation
D
duke 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
//  VM_GenCollectForAllocation
//  VM_ParallelGCFailedAllocation
//   - this operation is invoked when allocation is failed;
//     operation performs garbage collection and tries to
//     allocate afterwards;
//
//  VM_GenCollectFull
//  VM_GenCollectFullConcurrent
//  VM_ParallelGCSystemGC
//   - these operations preform full collection of heaps of
//     different kind
//

class VM_GC_Operation: public VM_Operation {
 protected:
72 73 74 75 76
  BasicLock      _pending_list_basic_lock; // for refs pending list notification (PLL)
  uint           _gc_count_before;         // gc count before acquiring PLL
  uint           _full_gc_count_before;    // full gc count before acquiring PLL
  bool           _full;                    // whether a "full" collection
  bool           _prologue_succeeded;      // whether doit_prologue succeeded
D
duke 已提交
77
  GCCause::Cause _gc_cause;                // the putative cause for this gc op
78
  bool           _gc_locked;               // will be set if gc was locked
D
duke 已提交
79 80 81 82 83 84 85 86

  virtual bool skip_operation() const;

  // java.lang.ref.Reference support
  void acquire_pending_list_lock();
  void release_and_notify_pending_list_lock();

 public:
87
  VM_GC_Operation(uint gc_count_before,
B
brutisso 已提交
88
                  GCCause::Cause _cause,
89
                  uint full_gc_count_before = 0,
D
duke 已提交
90 91 92 93 94 95
                  bool full = false) {
    _full = full;
    _prologue_succeeded = false;
    _gc_count_before    = gc_count_before;

    // A subclass constructor will likely overwrite the following
B
brutisso 已提交
96
    _gc_cause           = _cause;
D
duke 已提交
97 98 99

    _gc_locked = false;

100
    _full_gc_count_before = full_gc_count_before;
101 102 103 104 105 106 107 108 109 110 111 112
    // In ParallelScavengeHeap::mem_allocate() collections can be
    // executed within a loop and _all_soft_refs_clear can be set
    // true after they have been cleared by a collection and another
    // collection started so that _all_soft_refs_clear can be true
    // when this collection is started.  Don't assert that
    // _all_soft_refs_clear have to be false here even though
    // mutators have run.  Soft refs will be cleared again in this
    // collection.
  }
  ~VM_GC_Operation() {
    CollectedHeap* ch = Universe::heap();
    ch->collector_policy()->set_all_soft_refs_clear(false);
D
duke 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  }

  // Acquire the reference synchronization lock
  virtual bool doit_prologue();
  // Do notifyAll (if needed) and release held lock
  virtual void doit_epilogue();

  virtual bool allow_nested_vm_operations() const  { return true; }
  bool prologue_succeeded() const { return _prologue_succeeded; }

  void set_gc_locked() { _gc_locked = true; }
  bool gc_locked() const  { return _gc_locked; }

  static void notify_gc_begin(bool full = false);
  static void notify_gc_end();
};


class VM_GC_HeapInspection: public VM_GC_Operation {
 private:
  outputStream* _out;
  bool _full_gc;
135 136 137 138
  bool _csv_format; // "comma separated values" format for spreadsheet.
  bool _print_help;
  bool _print_class_stats;
  const char* _columns;
D
duke 已提交
139
 public:
S
sla 已提交
140
  VM_GC_HeapInspection(outputStream* out, bool request_full_gc) :
D
duke 已提交
141
    VM_GC_Operation(0 /* total collections,      dummy, ignored */,
B
brutisso 已提交
142
                    GCCause::_heap_inspection /* GC Cause */,
D
duke 已提交
143 144 145 146
                    0 /* total full collections, dummy, ignored */,
                    request_full_gc) {
    _out = out;
    _full_gc = request_full_gc;
147 148 149 150
    _csv_format = false;
    _print_help = false;
    _print_class_stats = false;
    _columns = NULL;
D
duke 已提交
151 152 153 154 155 156 157
  }

  ~VM_GC_HeapInspection() {}
  virtual VMOp_Type type() const { return VMOp_GC_HeapInspection; }
  virtual bool skip_operation() const;
  virtual bool doit_prologue();
  virtual void doit();
158 159 160 161
  void set_csv_format(bool value) {_csv_format = value;}
  void set_print_help(bool value) {_print_help = value;}
  void set_print_class_stats(bool value) {_print_class_stats = value;}
  void set_columns(const char* value) {_columns = value;}
S
sla 已提交
162 163
 protected:
  bool collect();
D
duke 已提交
164 165
};

166 167 168 169 170 171
class VM_CollectForAllocation : public VM_GC_Operation {
 protected:
  size_t    _word_size; // Size of object to be allocated (in number of words)
  HeapWord* _result;    // Allocation result (NULL if allocation failed)

 public:
172
  VM_CollectForAllocation(size_t word_size, uint gc_count_before, GCCause::Cause cause);
173 174 175 176 177

  HeapWord* result() const {
    return _result;
  }
};
D
duke 已提交
178

179
class VM_GenCollectForAllocation : public VM_CollectForAllocation {
D
duke 已提交
180 181 182
 private:
  bool        _tlab;                       // alloc is of a tlab.
 public:
183
  VM_GenCollectForAllocation(size_t word_size,
D
duke 已提交
184
                             bool tlab,
185
                             uint gc_count_before)
186
    : VM_CollectForAllocation(word_size, gc_count_before, GCCause::_allocation_failure),
D
duke 已提交
187
      _tlab(tlab) {
188
    assert(word_size != 0, "An allocation should always be requested with this operation.");
D
duke 已提交
189 190 191 192 193 194 195 196 197 198 199 200
  }
  ~VM_GenCollectForAllocation()  {}
  virtual VMOp_Type type() const { return VMOp_GenCollectForAllocation; }
  virtual void doit();
};

// VM operation to invoke a collection of the heap as a
// GenCollectedHeap heap.
class VM_GenCollectFull: public VM_GC_Operation {
 private:
  int _max_level;
 public:
201 202
  VM_GenCollectFull(uint gc_count_before,
                    uint full_gc_count_before,
D
duke 已提交
203
                    GCCause::Cause gc_cause,
204
                    int max_level)
B
brutisso 已提交
205 206
    : VM_GC_Operation(gc_count_before, gc_cause, full_gc_count_before, true /* full */),
      _max_level(max_level) { }
D
duke 已提交
207 208 209 210
  ~VM_GenCollectFull() {}
  virtual VMOp_Type type() const { return VMOp_GenCollectFull; }
  virtual void doit();
};
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 217
  Metaspace::MetadataType  _mdtype;
  ClassLoaderData*         _loader_data;
218
 public:
219 220
  VM_CollectForMetadataAllocation(ClassLoaderData* loader_data,
                                  size_t size, Metaspace::MetadataType mdtype,
221 222
                                  uint gc_count_before,
                                  uint full_gc_count_before,
223
                                  GCCause::Cause gc_cause);
224
  virtual VMOp_Type type() const { return VMOp_CollectForMetadataAllocation; }
225
  virtual void doit();
226
  MetaWord* result() const       { return _result; }
227 228

  bool initiate_concurrent_GC();
229
};
230

231 232 233 234 235 236 237 238
class SvcGCMarker : public StackObj {
 private:
  JvmtiGCMarker _jgcm;
 public:
  typedef enum { MINOR, FULL, OTHER } reason_type;

  SvcGCMarker(reason_type reason ) {
    VM_GC_Operation::notify_gc_begin(reason == FULL);
239 240
  }

241
  ~SvcGCMarker() {
242 243 244 245
    VM_GC_Operation::notify_gc_end();
  }
};

246
#endif // SHARE_VM_GC_IMPLEMENTATION_SHARED_VMGCOPERATIONS_HPP