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