1 /*
   2  * Copyright (c) 2012, 2018, 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/shared/copyFailedInfo.hpp"
  27 #include "gc/shared/gcHeapSummary.hpp"
  28 #include "gc/shared/gcId.hpp"
  29 #include "gc/shared/gcTimer.hpp"
  30 #include "gc/shared/gcTrace.hpp"
  31 #include "gc/shared/objectCountEventSender.hpp"
  32 #include "gc/shared/referenceProcessorStats.hpp"
  33 #include "memory/metaspace/metaspaceEnums.hpp"
  34 #include "memory/heapInspection.hpp"
  35 #include "memory/resourceArea.hpp"
  36 #include "runtime/os.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 #include "utilities/macros.hpp"
  39 #include "utilities/ticks.hpp"
  40 #if INCLUDE_G1GC
  41 #include "gc/g1/g1EvacuationInfo.hpp"
  42 #endif
  43 
  44 void GCTracer::report_gc_start_impl(GCCause::Cause cause, const Ticks& timestamp) {
  45   _shared_gc_info.set_cause(cause);
  46   _shared_gc_info.set_start_timestamp(timestamp);
  47 }
  48 
  49 void GCTracer::report_gc_start(GCCause::Cause cause, const Ticks& timestamp) {
  50   report_gc_start_impl(cause, timestamp);
  51 }
  52 
  53 void GCTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
  54   _shared_gc_info.set_sum_of_pauses(time_partitions->sum_of_pauses());
  55   _shared_gc_info.set_longest_pause(time_partitions->longest_pause());
  56   _shared_gc_info.set_end_timestamp(timestamp);
  57 
  58   send_phase_events(time_partitions);
  59   send_garbage_collection_event();
  60 }
  61 
  62 void GCTracer::report_gc_end(const Ticks& timestamp, TimePartitions* time_partitions) {
  63   report_gc_end_impl(timestamp, time_partitions);
  64 }
  65 
  66 void GCTracer::report_gc_reference_stats(const ReferenceProcessorStats& rps) const {
  67   send_reference_stats_event(REF_SOFT, rps.soft_count());
  68   send_reference_stats_event(REF_WEAK, rps.weak_count());
  69   send_reference_stats_event(REF_FINAL, rps.final_count());
  70   send_reference_stats_event(REF_PHANTOM, rps.phantom_count());
  71 }
  72 
  73 #if INCLUDE_SERVICES
  74 class ObjectCountEventSenderClosure : public KlassInfoClosure {
  75   const double _size_threshold_percentage;
  76   const size_t _total_size_in_words;
  77   const Ticks _timestamp;
  78 
  79  public:
  80   ObjectCountEventSenderClosure(size_t total_size_in_words, const Ticks& timestamp) :
  81     _size_threshold_percentage(ObjectCountCutOffPercent / 100),
  82     _total_size_in_words(total_size_in_words),
  83     _timestamp(timestamp)
  84   {}
  85 
  86   virtual void do_cinfo(KlassInfoEntry* entry) {
  87     if (should_send_event(entry)) {
  88       ObjectCountEventSender::send(entry, _timestamp);
  89     }
  90   }
  91 
  92  private:
  93   bool should_send_event(const KlassInfoEntry* entry) const {
  94     double percentage_of_heap = ((double) entry->words()) / _total_size_in_words;
  95     return percentage_of_heap >= _size_threshold_percentage;
  96   }
  97 };
  98 
  99 void GCTracer::report_object_count_after_gc(BoolObjectClosure* is_alive_cl) {
 100   assert(is_alive_cl != NULL, "Must supply function to check liveness");
 101 
 102   if (ObjectCountEventSender::should_send_event()) {
 103     ResourceMark rm;
 104 
 105     KlassInfoTable cit(false);
 106     if (!cit.allocation_failed()) {
 107       HeapInspection hi(false, false, false, NULL);
 108       hi.populate_table(&cit, is_alive_cl);
 109       ObjectCountEventSenderClosure event_sender(cit.size_of_instances_in_words(), Ticks::now());
 110       cit.iterate(&event_sender);
 111     }
 112   }
 113 }
 114 #endif // INCLUDE_SERVICES
 115 
 116 void GCTracer::report_gc_heap_summary(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 117   send_gc_heap_summary_event(when, heap_summary);
 118 }
 119 
 120 void GCTracer::report_metaspace_summary(GCWhen::Type when, const MetaspaceSummary& summary) const {
 121   send_meta_space_summary_event(when, summary);
 122 
 123   send_metaspace_chunk_free_list_summary(when, metaspace::NonClassType, summary.metaspace_chunk_free_list_summary());
 124   if (UseCompressedClassPointers) {
 125     send_metaspace_chunk_free_list_summary(when, metaspace::ClassType, summary.class_chunk_free_list_summary());
 126   }
 127 }
 128 
 129 void YoungGCTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 130   assert(_tenuring_threshold != UNSET_TENURING_THRESHOLD, "Tenuring threshold has not been reported");
 131 
 132   GCTracer::report_gc_end_impl(timestamp, time_partitions);
 133   send_young_gc_event();
 134 
 135   _tenuring_threshold = UNSET_TENURING_THRESHOLD;
 136 }
 137 
 138 void YoungGCTracer::report_promotion_failed(const PromotionFailedInfo& pf_info) const {
 139   send_promotion_failed_event(pf_info);
 140 }
 141 
 142 void YoungGCTracer::report_tenuring_threshold(const uint tenuring_threshold) {
 143   _tenuring_threshold = tenuring_threshold;
 144 }
 145 
 146 bool YoungGCTracer::should_report_promotion_events() const {
 147   return should_report_promotion_in_new_plab_event() ||
 148           should_report_promotion_outside_plab_event();
 149 }
 150 
 151 bool YoungGCTracer::should_report_promotion_in_new_plab_event() const {
 152   return should_send_promotion_in_new_plab_event();
 153 }
 154 
 155 bool YoungGCTracer::should_report_promotion_outside_plab_event() const {
 156   return should_send_promotion_outside_plab_event();
 157 }
 158 
 159 void YoungGCTracer::report_promotion_in_new_plab_event(Klass* klass, size_t obj_size,
 160                                                        uint age, bool tenured,
 161                                                        size_t plab_size) const {
 162   send_promotion_in_new_plab_event(klass, obj_size, age, tenured, plab_size);
 163 }
 164 
 165 void YoungGCTracer::report_promotion_outside_plab_event(Klass* klass, size_t obj_size,
 166                                                         uint age, bool tenured) const {
 167   send_promotion_outside_plab_event(klass, obj_size, age, tenured);
 168 }
 169 
 170 void OldGCTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 171   GCTracer::report_gc_end_impl(timestamp, time_partitions);
 172   send_old_gc_event();
 173 }
 174 
 175 void ParallelOldTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 176   OldGCTracer::report_gc_end_impl(timestamp, time_partitions);
 177   send_parallel_old_event();
 178 }
 179 
 180 void ParallelOldTracer::report_dense_prefix(void* dense_prefix) {
 181   _parallel_old_gc_info.report_dense_prefix(dense_prefix);
 182 }
 183 
 184 void OldGCTracer::report_concurrent_mode_failure() {
 185   send_concurrent_mode_failure_event();
 186 }
 187 
 188 #if INCLUDE_G1GC
 189 void G1MMUTracer::report_mmu(double time_slice_sec, double gc_time_sec, double max_time_sec) {
 190   send_g1_mmu_event(time_slice_sec * MILLIUNITS,
 191                     gc_time_sec * MILLIUNITS,
 192                     max_time_sec * MILLIUNITS);
 193 }
 194 
 195 void G1NewTracer::report_yc_type(G1YCType type) {
 196   _g1_young_gc_info.set_type(type);
 197 }
 198 
 199 void G1NewTracer::report_gc_end_impl(const Ticks& timestamp, TimePartitions* time_partitions) {
 200   YoungGCTracer::report_gc_end_impl(timestamp, time_partitions);
 201   send_g1_young_gc_event();
 202 }
 203 
 204 void G1NewTracer::report_evacuation_info(G1EvacuationInfo* info) {
 205   send_evacuation_info_event(info);
 206 }
 207 
 208 void G1NewTracer::report_evacuation_failed(EvacuationFailedInfo& ef_info) {
 209   send_evacuation_failed_event(ef_info);
 210   ef_info.reset();
 211 }
 212 
 213 void G1NewTracer::report_evacuation_statistics(const G1EvacSummary& young_summary, const G1EvacSummary& old_summary) const {
 214   send_young_evacuation_statistics(young_summary);
 215   send_old_evacuation_statistics(old_summary);
 216 }
 217 
 218 void G1NewTracer::report_basic_ihop_statistics(size_t threshold,
 219                                                size_t target_ccupancy,
 220                                                size_t current_occupancy,
 221                                                size_t last_allocation_size,
 222                                                double last_allocation_duration,
 223                                                double last_marking_length) {
 224   send_basic_ihop_statistics(threshold,
 225                              target_ccupancy,
 226                              current_occupancy,
 227                              last_allocation_size,
 228                              last_allocation_duration,
 229                              last_marking_length);
 230 }
 231 
 232 void G1NewTracer::report_adaptive_ihop_statistics(size_t threshold,
 233                                                   size_t internal_target_occupancy,
 234                                                   size_t current_occupancy,
 235                                                   size_t additional_buffer_size,
 236                                                   double predicted_allocation_rate,
 237                                                   double predicted_marking_length,
 238                                                   bool prediction_active) {
 239   send_adaptive_ihop_statistics(threshold,
 240                                 internal_target_occupancy,
 241                                 additional_buffer_size,
 242                                 current_occupancy,
 243                                 predicted_allocation_rate,
 244                                 predicted_marking_length,
 245                                 prediction_active);
 246 }
 247 
 248 void G1OldTracer::report_gc_start_impl(GCCause::Cause cause, const Ticks& timestamp) {
 249   _shared_gc_info.set_start_timestamp(timestamp);
 250 }
 251 
 252 void G1OldTracer::set_gc_cause(GCCause::Cause cause) {
 253   _shared_gc_info.set_cause(cause);
 254 }
 255 
 256 #endif // INCLUDE_G1GC