1 /*
   2  * Copyright (c) 2012, 2019, 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/gcTimer.hpp"
  29 #include "gc/shared/gcTrace.hpp"
  30 #include "gc/shared/gcWhen.hpp"
  31 #include "jfr/jfrEvents.hpp"
  32 #include "runtime/os.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 // All GC dependencies against the trace framework is contained within this file.
  36 
  37 typedef uintptr_t TraceAddress;
  38 
  39 void GCTracer::send_garbage_collection_event() const {
  40   EventGarbageCollection event(UNTIMED);
  41   if (event.should_commit()) {
  42     event.set_gcId(GCId::current());
  43     event.set_name(_shared_gc_info.name());
  44     event.set_cause((u2) _shared_gc_info.cause());
  45     event.set_sumOfPauses(_shared_gc_info.sum_of_pauses());
  46     event.set_longestPause(_shared_gc_info.longest_pause());
  47     event.set_starttime(_shared_gc_info.start_timestamp());
  48     event.set_endtime(_shared_gc_info.end_timestamp());
  49     event.commit();
  50   }
  51 }
  52 
  53 void GCTracer::send_reference_stats_event(ReferenceType type, size_t count) const {
  54   EventGCReferenceStatistics e;
  55   if (e.should_commit()) {
  56       e.set_gcId(GCId::current());
  57       e.set_type((u1)type);
  58       e.set_count(count);
  59       e.commit();
  60   }
  61 }
  62 
  63 void GCTracer::send_metaspace_chunk_free_list_summary(GCWhen::Type when, Metaspace::MetadataType mdtype,
  64                                                       const MetaspaceChunkFreeListSummary& summary) const {
  65   EventMetaspaceChunkFreeListSummary e;
  66   if (e.should_commit()) {
  67     e.set_gcId(GCId::current());
  68     e.set_when(when);
  69     e.set_metadataType(mdtype);
  70 
  71     e.set_specializedChunks(summary.num_specialized_chunks());
  72     e.set_specializedChunksTotalSize(summary.specialized_chunks_size_in_bytes());
  73 
  74     e.set_smallChunks(summary.num_small_chunks());
  75     e.set_smallChunksTotalSize(summary.small_chunks_size_in_bytes());
  76 
  77     e.set_mediumChunks(summary.num_medium_chunks());
  78     e.set_mediumChunksTotalSize(summary.medium_chunks_size_in_bytes());
  79 
  80     e.set_humongousChunks(summary.num_humongous_chunks());
  81     e.set_humongousChunksTotalSize(summary.humongous_chunks_size_in_bytes());
  82 
  83     e.commit();
  84   }
  85 }
  86 
  87 void ParallelOldTracer::send_parallel_old_event() const {
  88   EventParallelOldGarbageCollection e(UNTIMED);
  89   if (e.should_commit()) {
  90     e.set_gcId(GCId::current());
  91     e.set_densePrefix((TraceAddress)_parallel_old_gc_info.dense_prefix());
  92     e.set_starttime(_shared_gc_info.start_timestamp());
  93     e.set_endtime(_shared_gc_info.end_timestamp());
  94     e.commit();
  95   }
  96 }
  97 
  98 void YoungGCTracer::send_young_gc_event() const {
  99   EventYoungGarbageCollection e(UNTIMED);
 100   if (e.should_commit()) {
 101     e.set_gcId(GCId::current());
 102     e.set_tenuringThreshold(_tenuring_threshold);
 103     e.set_starttime(_shared_gc_info.start_timestamp());
 104     e.set_endtime(_shared_gc_info.end_timestamp());
 105     e.commit();
 106   }
 107 }
 108 
 109 bool YoungGCTracer::should_send_promotion_in_new_plab_event() const {
 110   return EventPromoteObjectInNewPLAB::is_enabled();
 111 }
 112 
 113 bool YoungGCTracer::should_send_promotion_outside_plab_event() const {
 114   return EventPromoteObjectOutsidePLAB::is_enabled();
 115 }
 116 
 117 void YoungGCTracer::send_promotion_in_new_plab_event(Klass* klass, size_t obj_size,
 118                                                      uint age, bool tenured,
 119                                                      size_t plab_size) const {
 120 
 121   EventPromoteObjectInNewPLAB event;
 122   if (event.should_commit()) {
 123     event.set_gcId(GCId::current());
 124     event.set_objectClass(klass);
 125     event.set_objectSize(obj_size);
 126     event.set_tenured(tenured);
 127     event.set_tenuringAge(age);
 128     event.set_plabSize(plab_size);
 129     event.commit();
 130   }
 131 }
 132 
 133 void YoungGCTracer::send_promotion_outside_plab_event(Klass* klass, size_t obj_size,
 134                                                       uint age, bool tenured) const {
 135 
 136   EventPromoteObjectOutsidePLAB event;
 137   if (event.should_commit()) {
 138     event.set_gcId(GCId::current());
 139     event.set_objectClass(klass);
 140     event.set_objectSize(obj_size);
 141     event.set_tenured(tenured);
 142     event.set_tenuringAge(age);
 143     event.commit();
 144   }
 145 }
 146 
 147 void OldGCTracer::send_old_gc_event() const {
 148   EventOldGarbageCollection e(UNTIMED);
 149   if (e.should_commit()) {
 150     e.set_gcId(GCId::current());
 151     e.set_starttime(_shared_gc_info.start_timestamp());
 152     e.set_endtime(_shared_gc_info.end_timestamp());
 153     e.commit();
 154   }
 155 }
 156 
 157 static JfrStructCopyFailed to_struct(const CopyFailedInfo& cf_info) {
 158   JfrStructCopyFailed failed_info;
 159   failed_info.set_objectCount(cf_info.failed_count());
 160   failed_info.set_firstSize(cf_info.first_size());
 161   failed_info.set_smallestSize(cf_info.smallest_size());
 162   failed_info.set_totalSize(cf_info.total_size());
 163   return failed_info;
 164 }
 165 
 166 void YoungGCTracer::send_promotion_failed_event(const PromotionFailedInfo& pf_info) const {
 167   EventPromotionFailed e;
 168   if (e.should_commit()) {
 169     e.set_gcId(GCId::current());
 170     e.set_promotionFailed(to_struct(pf_info));
 171     e.set_thread(pf_info.thread_trace_id());
 172     e.commit();
 173   }
 174 }
 175 
 176 // Common to CMS and G1
 177 void OldGCTracer::send_concurrent_mode_failure_event() {
 178   EventConcurrentModeFailure e;
 179   if (e.should_commit()) {
 180     e.set_gcId(GCId::current());
 181     e.commit();
 182   }
 183 }
 184 
 185 static JfrStructVirtualSpace to_struct(const VirtualSpaceSummary& summary) {
 186   JfrStructVirtualSpace space;
 187   space.set_start((TraceAddress)summary.start());
 188   space.set_committedEnd((TraceAddress)summary.committed_end());
 189   space.set_committedSize(summary.committed_size());
 190   space.set_reservedEnd((TraceAddress)summary.reserved_end());
 191   space.set_reservedSize(summary.reserved_size());
 192   return space;
 193 }
 194 
 195 static JfrStructObjectSpace to_struct(const SpaceSummary& summary) {
 196   JfrStructObjectSpace space;
 197   space.set_start((TraceAddress)summary.start());
 198   space.set_end((TraceAddress)summary.end());
 199   space.set_used(summary.used());
 200   space.set_size(summary.size());
 201   return space;
 202 }
 203 
 204 class GCHeapSummaryEventSender : public GCHeapSummaryVisitor {
 205   GCWhen::Type _when;
 206  public:
 207   GCHeapSummaryEventSender(GCWhen::Type when) : _when(when) {}
 208 
 209   void visit(const GCHeapSummary* heap_summary) const {
 210     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 211 
 212     EventGCHeapSummary e;
 213     if (e.should_commit()) {
 214       e.set_gcId(GCId::current());
 215       e.set_when((u1)_when);
 216       e.set_heapSpace(to_struct(heap_space));
 217       e.set_heapUsed(heap_summary->used());
 218       e.commit();
 219     }
 220   }
 221 
 222   void visit(const G1HeapSummary* g1_heap_summary) const {
 223     visit((GCHeapSummary*)g1_heap_summary);
 224 
 225     EventG1HeapSummary e;
 226     if (e.should_commit()) {
 227       e.set_gcId(GCId::current());
 228       e.set_when((u1)_when);
 229       e.set_edenUsedSize(g1_heap_summary->edenUsed());
 230       e.set_edenTotalSize(g1_heap_summary->edenCapacity());
 231       e.set_survivorUsedSize(g1_heap_summary->survivorUsed());
 232       e.set_numberOfRegions(g1_heap_summary->numberOfRegions());
 233       e.commit();
 234     }
 235   }
 236 
 237   void visit(const PSHeapSummary* ps_heap_summary) const {
 238     visit((GCHeapSummary*)ps_heap_summary);
 239 
 240     const VirtualSpaceSummary& old_summary = ps_heap_summary->old();
 241     const SpaceSummary& old_space = ps_heap_summary->old_space();
 242     const VirtualSpaceSummary& young_summary = ps_heap_summary->young();
 243     const SpaceSummary& eden_space = ps_heap_summary->eden();
 244     const SpaceSummary& from_space = ps_heap_summary->from();
 245     const SpaceSummary& to_space = ps_heap_summary->to();
 246 
 247     EventPSHeapSummary e;
 248     if (e.should_commit()) {
 249       e.set_gcId(GCId::current());
 250       e.set_when((u1)_when);
 251 
 252       e.set_oldSpace(to_struct(ps_heap_summary->old()));
 253       e.set_oldObjectSpace(to_struct(ps_heap_summary->old_space()));
 254       e.set_youngSpace(to_struct(ps_heap_summary->young()));
 255       e.set_edenSpace(to_struct(ps_heap_summary->eden()));
 256       e.set_fromSpace(to_struct(ps_heap_summary->from()));
 257       e.set_toSpace(to_struct(ps_heap_summary->to()));
 258       e.commit();
 259     }
 260   }
 261 };
 262 
 263 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 264   GCHeapSummaryEventSender visitor(when);
 265   heap_summary.accept(&visitor);
 266 }
 267 
 268 static JfrStructMetaspaceSizes to_struct(const MetaspaceSizes& sizes) {
 269   JfrStructMetaspaceSizes meta_sizes;
 270 
 271   meta_sizes.set_committed(sizes.committed());
 272   meta_sizes.set_used(sizes.used());
 273   meta_sizes.set_reserved(sizes.reserved());
 274 
 275   return meta_sizes;
 276 }
 277 
 278 void GCTracer::send_meta_space_summary_event(GCWhen::Type when, const MetaspaceSummary& meta_space_summary) const {
 279   EventMetaspaceSummary e;
 280   if (e.should_commit()) {
 281     e.set_gcId(GCId::current());
 282     e.set_when((u1) when);
 283     e.set_gcThreshold(meta_space_summary.capacity_until_GC());
 284     e.set_metaspace(to_struct(meta_space_summary.meta_space()));
 285     e.set_dataSpace(to_struct(meta_space_summary.data_space()));
 286     e.set_classSpace(to_struct(meta_space_summary.class_space()));
 287     e.commit();
 288   }
 289 }
 290 
 291 class PhaseSender : public PhaseVisitor {
 292   void visit_pause(GCPhase* phase) {
 293     assert(phase->level() < PhasesStack::PHASE_LEVELS, "Need more event types for PausePhase");
 294 
 295     switch (phase->level()) {
 296       case 0: send_phase<EventGCPhasePause>(phase); break;
 297       case 1: send_phase<EventGCPhasePauseLevel1>(phase); break;
 298       case 2: send_phase<EventGCPhasePauseLevel2>(phase); break;
 299       case 3: send_phase<EventGCPhasePauseLevel3>(phase); break;
 300       case 4: send_phase<EventGCPhasePauseLevel4>(phase); break;
 301       default: /* Ignore sending this phase */ break;
 302     }
 303   }
 304 
 305   void visit_concurrent(GCPhase* phase) {
 306     assert(phase->level() < 1, "There is only one level for ConcurrentPhase");
 307 
 308     switch (phase->level()) {
 309       case 0: send_phase<EventGCPhaseConcurrent>(phase); break;
 310       default: /* Ignore sending this phase */ break;
 311     }
 312   }
 313 
 314  public:
 315   template<typename T>
 316   void send_phase(GCPhase* phase) {
 317     T event(UNTIMED);
 318     if (event.should_commit()) {
 319       event.set_gcId(GCId::current());
 320       event.set_name(phase->name());
 321       event.set_starttime(phase->start());
 322       event.set_endtime(phase->end());
 323       event.commit();
 324     }
 325   }
 326 
 327   void visit(GCPhase* phase) {
 328     if (phase->type() == GCPhase::PausePhaseType) {
 329       visit_pause(phase);
 330     } else {
 331       assert(phase->type() == GCPhase::ConcurrentPhaseType, "Should be ConcurrentPhaseType");
 332       visit_concurrent(phase);
 333     }
 334   }
 335 };
 336 
 337 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 338   PhaseSender phase_reporter;
 339 
 340   TimePartitionPhasesIterator iter(time_partitions);
 341   while (iter.has_next()) {
 342     GCPhase* phase = iter.next();
 343     phase->accept(&phase_reporter);
 344   }
 345 }