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/gcId.hpp"
  29 #include "gc_implementation/shared/gcTimer.hpp"
  30 #include "gc_implementation/shared/gcTrace.hpp"
  31 #include "gc_implementation/shared/objectCountEventSender.hpp"
  32 #include "memory/heapInspection.hpp"
  33 #include "memory/referenceProcessorStats.hpp"
  34 #include "runtime/handles.hpp"
  35 #include "runtime/os.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 #include "utilities/ticks.inline.hpp"
  38 #include "trace/tracing.hpp"
  39 
  40 #if INCLUDE_ALL_GCS
  41 #include "gc_implementation/g1/evacuationInfo.hpp"
  42 #endif
  43 
  44 #define assert_unset_gc_id() assert(_shared_gc_info.gc_id().is_undefined(), "GC already started?")
  45 #define assert_set_gc_id() assert(!_shared_gc_info.gc_id().is_undefined(), "GC not started?")
  46 
  47 void GCTracer::report_gc_start_impl(GCCause::Cause cause, const Ticks& timestamp) {
  48   assert_unset_gc_id();
  49 
  50   GCId gc_id = GCId::create();
  51   _shared_gc_info.set_gc_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, const Ticks& 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.gc_id().is_undefined();
  64 }
  65 
  66 void GCTracer::report_gc_end_impl(const Ticks& 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(const Ticks& timestamp, TimePartitions* time_partitions) {
  78   assert_set_gc_id();
  79 
  80   report_gc_end_impl(timestamp, time_partitions);
  81 
  82   _shared_gc_info.set_gc_id(GCId::undefined());
  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 #if INCLUDE_SERVICES
  95 class ObjectCountEventSenderClosure : public KlassInfoClosure {
  96   const GCId _gc_id;
  97   const double _size_threshold_percentage;
  98   const size_t _total_size_in_words;
  99   const Ticks _timestamp;
 100 
 101  public:
 102   ObjectCountEventSenderClosure(GCId gc_id, size_t total_size_in_words, const Ticks& timestamp) :
 103     _gc_id(gc_id),
 104     _size_threshold_percentage(ObjectCountCutOffPercent / 100),
 105     _total_size_in_words(total_size_in_words),
 106     _timestamp(timestamp)
 107   {}
 108 
 109   virtual void do_cinfo(KlassInfoEntry* entry) {
 110     if (should_send_event(entry)) {
 111       ObjectCountEventSender::send(entry, _gc_id, _timestamp);
 112     }
 113   }
 114 
 115  private:
 116   bool should_send_event(const KlassInfoEntry* entry) const {
 117     double percentage_of_heap = ((double) entry->words()) / _total_size_in_words;
 118     return percentage_of_heap >= _size_threshold_percentage;
 119   }
 120 };
 121 
 122 void GCTracer::report_object_count_after_gc(BoolObjectClosure* is_alive_cl) {
 123   assert_set_gc_id();
 124   assert(is_alive_cl != NULL, "Must supply function to check liveness");
 125 
 126   if (ObjectCountEventSender::should_send_event()) {
 127     ResourceMark rm;
 128 
 129     KlassInfoTable cit(false);
 130     if (!cit.allocation_failed()) {
 131       HeapInspection hi(false, false, false, NULL);
 132       hi.populate_table(&cit, is_alive_cl);
 133       ObjectCountEventSenderClosure event_sender(_shared_gc_info.gc_id(), cit.size_of_instances_in_words(), Ticks::now());
 134       cit.iterate(&event_sender);
 135     }
 136   }
 137 }
 138 #endif // INCLUDE_SERVICES
 139 
 140 void GCTracer::report_gc_heap_summary(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 141   assert_set_gc_id();
 142 
 143   send_gc_heap_summary_event(when, heap_summary);
 144 }
 145 
 146 void GCTracer::report_metaspace_summary(GCWhen::Type when, const MetaspaceSummary& summary) const {
 147   assert_set_gc_id();
 148 
 149   send_meta_space_summary_event(when, summary);
 150 
 151   send_metaspace_chunk_free_list_summary(when, Metaspace::NonClassType, summary.metaspace_chunk_free_list_summary());
 152   if (UseCompressedClassPointers) {
 153     send_metaspace_chunk_free_list_summary(when, Metaspace::ClassType, summary.class_chunk_free_list_summary());
 154   }
 155 }
 156 
 157 void YoungGCTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 158   assert_set_gc_id();
 159   assert(_tenuring_threshold != UNSET_TENURING_THRESHOLD, "Tenuring threshold has not been reported");
 160 
 161   GCTracer::report_gc_end_impl(timestamp, time_partitions);
 162   send_young_gc_event();
 163 
 164   _tenuring_threshold = UNSET_TENURING_THRESHOLD;
 165 }
 166 
 167 void YoungGCTracer::report_promotion_failed(const PromotionFailedInfo& pf_info) {
 168   assert_set_gc_id();
 169 
 170   send_promotion_failed_event(pf_info);
 171 }
 172 
 173 void YoungGCTracer::report_tenuring_threshold(const uint tenuring_threshold) {
 174   _tenuring_threshold = tenuring_threshold;
 175 }
 176 
 177 void YoungGCTracer::report_promotion_to_new_plab(oop const old, uint age, bool tenured, size_t plab_size) {
 178 
 179   EventPromotionSample event;
 180   
 181   if (event.should_commit()) {
 182     event.set_gcId(_shared_gc_info.gc_id().id());
 183     event.set_class(KlassHandle(old->klass())());
 184     event.set_objectSize((size_t)old->size());
 185     event.set_tenured(tenured);
 186     event.set_tenuringAge(age);
 187 
 188     // If the PLAB size is the same as the object size the copy was done directly 
 189     // to the heap instead of allocating a new PLAB to copy the object to
 190     if ((size_t)old->size() == plab_size) {
 191       event.set_directAllocation(true);
 192       // Set plabSize to 0 as it otherwise will be a random value
 193       event.set_plabSize(0);
 194     } else {
 195       event.set_directAllocation(false);
 196       event.set_plabSize(plab_size);
 197     }
 198 
 199     event.commit();
 200   }
 201 }
 202 
 203 void OldGCTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 204   assert_set_gc_id();
 205 
 206   GCTracer::report_gc_end_impl(timestamp, time_partitions);
 207   send_old_gc_event();
 208 }
 209 
 210 void ParallelOldTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 211   assert_set_gc_id();
 212 
 213   OldGCTracer::report_gc_end_impl(timestamp, time_partitions);
 214   send_parallel_old_event();
 215 }
 216 
 217 void ParallelOldTracer::report_dense_prefix(void* dense_prefix) {
 218   assert_set_gc_id();
 219 
 220   _parallel_old_gc_info.report_dense_prefix(dense_prefix);
 221 }
 222 
 223 void OldGCTracer::report_concurrent_mode_failure() {
 224   assert_set_gc_id();
 225 
 226   send_concurrent_mode_failure_event();
 227 }
 228 
 229 #if INCLUDE_ALL_GCS
 230 void G1NewTracer::report_yc_type(G1YCType type) {
 231   assert_set_gc_id();
 232 
 233   _g1_young_gc_info.set_type(type);
 234 }
 235 
 236 void G1NewTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 237   assert_set_gc_id();
 238 
 239   YoungGCTracer::report_gc_end_impl(timestamp, time_partitions);
 240   send_g1_young_gc_event();
 241 }
 242 
 243 void G1NewTracer::report_evacuation_info(EvacuationInfo* info) {
 244   assert_set_gc_id();
 245 
 246   send_evacuation_info_event(info);
 247 }
 248 
 249 void G1NewTracer::report_evacuation_failed(EvacuationFailedInfo& ef_info) {
 250   assert_set_gc_id();
 251 
 252   send_evacuation_failed_event(ef_info);
 253   ef_info.reset();
 254 }
 255 #endif