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