1 /*
   2  * Copyright (c) 2012, 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 #include "precompiled.hpp"
  26 #include "gc_implementation/shared/gcHeapSummary.hpp"
  27 #include "gc_implementation/shared/gcTimer.hpp"
  28 #include "gc_implementation/shared/gcTrace.hpp"
  29 #include "gc_implementation/shared/copyFailedInfo.hpp"
  30 #include "memory/heapInspection.hpp"
  31 #include "memory/iterator.hpp"
  32 #include "memory/referenceProcessorStats.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 #ifndef SERIALGC
  36 #include "gc_implementation/g1/evacuationInfo.hpp"
  37 #endif
  38 
  39 #define assert_unset_gc_id() assert(_shared_gc_info.id() == SharedGCInfo::UNSET_GCID, "GC already started?")
  40 #define assert_set_gc_id() assert(_shared_gc_info.id() != SharedGCInfo::UNSET_GCID, "GC not started?")
  41 
  42 static jlong GCTracer_next_gc_id = 0;
  43 static GCId create_new_gc_id() {
  44   return GCTracer_next_gc_id++;
  45 }
  46 
  47 void GCTracer::report_gc_start_impl(GCCause::Cause cause, jlong timestamp) {
  48   assert_unset_gc_id();
  49 
  50   GCId gc_id = create_new_gc_id();
  51   _shared_gc_info.set_id(gc_id);
  52   _shared_gc_info.set_cause(cause);
  53   _shared_gc_info.set_start_timestamp(timestamp);
  54 }
  55 
  56 void GCTracer::report_gc_start(GCCause::Cause cause, jlong timestamp) {
  57   assert_unset_gc_id();
  58 
  59   report_gc_start_impl(cause, timestamp);
  60 }
  61 
  62 bool GCTracer::has_reported_gc_start() const {
  63   return _shared_gc_info.id() != SharedGCInfo::UNSET_GCID;
  64 }
  65 
  66 void GCTracer::report_gc_end_impl(jlong timestamp, TimePartitions* time_partitions) {
  67   assert_set_gc_id();
  68 
  69   _shared_gc_info.set_sum_of_pauses(time_partitions->sum_of_pauses());
  70   _shared_gc_info.set_longest_pause(time_partitions->longest_pause());
  71   _shared_gc_info.set_end_timestamp(timestamp);
  72 
  73   send_phase_events(time_partitions);
  74   send_garbage_collection_event();
  75 }
  76 
  77 void GCTracer::report_gc_end(jlong timestamp, TimePartitions* time_partitions) {
  78   assert_set_gc_id();
  79 
  80   report_gc_end_impl(timestamp, time_partitions);
  81 
  82   _shared_gc_info.set_id(SharedGCInfo::UNSET_GCID);
  83 }
  84 
  85 void GCTracer::report_gc_reference_stats(const ReferenceProcessorStats& rps) const {
  86   assert_set_gc_id();
  87 
  88   send_reference_stats_event(REF_SOFT, rps.soft_count());
  89   send_reference_stats_event(REF_WEAK, rps.weak_count());
  90   send_reference_stats_event(REF_FINAL, rps.final_count());
  91   send_reference_stats_event(REF_PHANTOM, rps.phantom_count());
  92 }
  93 
  94 class ObjectCountEventSenderClosure : public KlassInfoClosure {
  95   GCTracer* _gc_tracer;
  96  public:
  97   ObjectCountEventSenderClosure(GCTracer* gc_tracer) : _gc_tracer(gc_tracer) {}
  98  private:
  99   void do_cinfo(KlassInfoEntry* entry) {
 100     _gc_tracer->send_object_count_after_gc_event(entry->klass(), entry->count(),
 101                                                  entry->words() * BytesPerWord);
 102   }
 103 };
 104 
 105 void GCTracer::report_object_count_after_gc(BoolObjectClosure *is_alive_cl) {
 106   if (should_send_object_count_after_gc_event()) {
 107     ResourceMark rm;
 108 
 109     KlassInfoTable cit(HeapInspection::start_of_perm_gen());
 110     if (!cit.allocation_failed()) {
 111       ObjectCountEventSenderClosure event_sender(this);
 112       HeapInspection::instance_inspection(&cit, &event_sender, false, is_alive_cl);
 113     }
 114   }
 115 }
 116 
 117 void GCTracer::report_gc_heap_summary(GCWhen::Type when, const GCHeapSummary& heap_summary, const PermGenSummary& perm_gen_summary) const {
 118   assert_set_gc_id();
 119 
 120   send_gc_heap_summary_event(when, heap_summary);
 121   send_perm_gen_summary_event(when, perm_gen_summary);
 122 }
 123 
 124 void YoungGCTracer::report_gc_end_impl(jlong timestamp, TimePartitions* time_partitions) {
 125   assert_set_gc_id();
 126   assert(_tenuring_threshold != UNSET_TENURING_THRESHOLD, "Tenuring threshold has not been reported");
 127 
 128   GCTracer::report_gc_end_impl(timestamp, time_partitions);
 129   send_young_gc_event();
 130 
 131   _tenuring_threshold = UNSET_TENURING_THRESHOLD;
 132 }
 133 
 134 void YoungGCTracer::report_promotion_failed(const PromotionFailedInfo& pf_info) {
 135   assert_set_gc_id();
 136 
 137   send_promotion_failed_event(pf_info);
 138 }
 139 
 140 void YoungGCTracer::report_tenuring_threshold(const uint tenuring_threshold) {
 141   _tenuring_threshold = tenuring_threshold;
 142 }
 143 
 144 void OldGCTracer::report_gc_end_impl(jlong timestamp, TimePartitions* time_partitions) {
 145   assert_set_gc_id();
 146 
 147   GCTracer::report_gc_end_impl(timestamp, time_partitions);
 148   send_old_gc_event();
 149 }
 150 
 151 void ParallelOldTracer::report_gc_end_impl(jlong timestamp, TimePartitions* time_partitions) {
 152   assert_set_gc_id();
 153 
 154   OldGCTracer::report_gc_end_impl(timestamp, time_partitions);
 155   send_parallel_old_event();
 156 }
 157 
 158 void ParallelOldTracer::report_dense_prefix(void* dense_prefix) {
 159   assert_set_gc_id();
 160 
 161   _parallel_old_gc_info.report_dense_prefix(dense_prefix);
 162 }
 163 
 164 void OldGCTracer::report_concurrent_mode_failure() {
 165   assert_set_gc_id();
 166 
 167   send_concurrent_mode_failure_event();
 168 }
 169 
 170 #ifndef SERIALGC
 171 void G1NewTracer::report_yc_type(G1YCType type) {
 172   assert_set_gc_id();
 173 
 174   _g1_young_gc_info.set_type(type);
 175 }
 176 
 177 void G1NewTracer::report_gc_end_impl(jlong timestamp, TimePartitions* time_partitions) {
 178   assert_set_gc_id();
 179 
 180   YoungGCTracer::report_gc_end_impl(timestamp, time_partitions);
 181   send_g1_young_gc_event();
 182 }
 183 
 184 void G1NewTracer::report_evacuation_info(EvacuationInfo* info) {
 185   assert_set_gc_id();
 186 
 187   send_evacuation_info_event(info);
 188 }
 189 
 190 void G1NewTracer::report_evacuation_failed(EvacuationFailedInfo& ef_info) {
 191   assert_set_gc_id();
 192 
 193   send_evacuation_failed_event(ef_info);
 194   ef_info.reset();
 195 }
 196 #endif