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/os.hpp"
  32 #include "trace/tracing.hpp"
  33 #include "trace/traceBackend.hpp"
  34 #if INCLUDE_ALL_GCS
  35 #include "gc_implementation/g1/evacuationInfo.hpp"
  36 #include "gc_implementation/g1/g1YCTypes.hpp"
  37 #endif
  38 
  39 // All GC dependencies against the trace framework is contained within this file.
  40 
  41 typedef uintptr_t TraceAddress;
  42 
  43 void GCTracer::send_garbage_collection_event() const {
  44   EventGCGarbageCollection event(UNTIMED);
  45   if (event.should_commit()) {
  46     event.set_gcId(_shared_gc_info.id());
  47     event.set_name(_shared_gc_info.name());
  48     event.set_cause((u2) _shared_gc_info.cause());
  49     event.set_sumOfPauses(_shared_gc_info.sum_of_pauses());
  50     event.set_longestPause(_shared_gc_info.longest_pause());
  51     event.set_starttime(_shared_gc_info.start_timestamp());
  52     event.set_endtime(_shared_gc_info.end_timestamp());
  53     event.commit();
  54   }
  55 }
  56 
  57 void GCTracer::send_reference_stats_event(ReferenceType type, size_t count) const {
  58   EventGCReferenceStatistics e;
  59   if (e.should_commit()) {
  60       e.set_gcId(_shared_gc_info.id());
  61       e.set_type((u1)type);
  62       e.set_count(count);
  63       e.commit();
  64   }
  65 }
  66 
  67 void GCTracer::send_metaspace_chunk_free_list_summary(GCWhen::Type when,
  68                                                       Metaspace::MetadataType mdtype,
  69                                                       const MetaspaceChunkFreeListSummary& summary) const {
  70     EventMetaspaceChunkFreeListSummary e;
  71     if (e.should_commit()) {
  72         e.set_gcId(_shared_gc_info.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.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.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 void OldGCTracer::send_old_gc_event() const {
 115   EventGCOldGarbageCollection e(UNTIMED);
 116   if (e.should_commit()) {
 117     e.set_gcId(_shared_gc_info.id());
 118     e.set_starttime(_shared_gc_info.start_timestamp());
 119     e.set_endtime(_shared_gc_info.end_timestamp());
 120     e.commit();
 121   }
 122 }
 123 
 124 static TraceStructCopyFailed to_trace_struct(const CopyFailedInfo& cf_info) {
 125   TraceStructCopyFailed failed_info;
 126   failed_info.set_objectCount(cf_info.failed_count());
 127   failed_info.set_firstSize(cf_info.first_size());
 128   failed_info.set_smallestSize(cf_info.smallest_size());
 129   failed_info.set_totalSize(cf_info.total_size());
 130   return failed_info;
 131 }
 132 
 133 void YoungGCTracer::send_promotion_failed_event(const PromotionFailedInfo& pf_info) const {
 134   EventPromotionFailed e;
 135   if (e.should_commit()) {
 136     e.set_gcId(_shared_gc_info.id());
 137     e.set_data(to_trace_struct(pf_info));
 138     e.set_thread(pf_info.thread()->thread_id());
 139     e.commit();
 140   }
 141 }
 142 
 143 // Common to CMS and G1
 144 void OldGCTracer::send_concurrent_mode_failure_event() {
 145   EventConcurrentModeFailure e;
 146   if (e.should_commit()) {
 147     e.set_gcId(_shared_gc_info.id());
 148     e.commit();
 149   }
 150 }
 151 
 152 #if INCLUDE_ALL_GCS
 153 void G1NewTracer::send_g1_young_gc_event() {
 154   EventGCG1GarbageCollection e(UNTIMED);
 155   if (e.should_commit()) {
 156     e.set_gcId(_shared_gc_info.id());
 157     e.set_type(_g1_young_gc_info.type());
 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 void G1NewTracer::send_evacuation_info_event(EvacuationInfo* info) {
 165   EventEvacuationInfo e;
 166   if (e.should_commit()) {
 167     e.set_gcId(_shared_gc_info.id());
 168     e.set_cSetRegions(info->collectionset_regions());
 169     e.set_cSetUsedBefore(info->collectionset_used_before());
 170     e.set_cSetUsedAfter(info->collectionset_used_after());
 171     e.set_allocationRegions(info->allocation_regions());
 172     e.set_allocRegionsUsedBefore(info->alloc_regions_used_before());
 173     e.set_allocRegionsUsedAfter(info->alloc_regions_used_before() + info->bytes_copied());
 174     e.set_bytesCopied(info->bytes_copied());
 175     e.set_regionsFreed(info->regions_freed());
 176     e.commit();
 177   }
 178 }
 179 
 180 void G1NewTracer::send_evacuation_failed_event(const EvacuationFailedInfo& ef_info) const {
 181   EventEvacuationFailed e;
 182   if (e.should_commit()) {
 183     e.set_gcId(_shared_gc_info.id());
 184     e.set_data(to_trace_struct(ef_info));
 185     e.commit();
 186   }
 187 }
 188 #endif
 189 
 190 static TraceStructVirtualSpace to_trace_struct(const VirtualSpaceSummary& summary) {
 191   TraceStructVirtualSpace space;
 192   space.set_start((TraceAddress)summary.start());
 193   space.set_committedEnd((TraceAddress)summary.committed_end());
 194   space.set_committedSize(summary.committed_size());
 195   space.set_reservedEnd((TraceAddress)summary.reserved_end());
 196   space.set_reservedSize(summary.reserved_size());
 197   return space;
 198 }
 199 
 200 static TraceStructObjectSpace to_trace_struct(const SpaceSummary& summary) {
 201   TraceStructObjectSpace space;
 202   space.set_start((TraceAddress)summary.start());
 203   space.set_end((TraceAddress)summary.end());
 204   space.set_used(summary.used());
 205   space.set_size(summary.size());
 206   return space;
 207 }
 208 
 209 class GCHeapSummaryEventSender : public GCHeapSummaryVisitor {
 210   GCId _id;
 211   GCWhen::Type _when;
 212  public:
 213   GCHeapSummaryEventSender(GCId id, GCWhen::Type when) : _id(id), _when(when) {}
 214 
 215   void visit(const GCHeapSummary* heap_summary) const {
 216     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 217 
 218     EventGCHeapSummary e;
 219     if (e.should_commit()) {
 220       e.set_gcId(_id);
 221       e.set_when((u1)_when);
 222       e.set_heapSpace(to_trace_struct(heap_space));
 223       e.set_heapUsed(heap_summary->used());
 224       e.commit();
 225     }
 226   }
 227 
 228   void visit(const PSHeapSummary* ps_heap_summary) const {
 229     visit((GCHeapSummary*)ps_heap_summary);
 230 
 231     const VirtualSpaceSummary& old_summary = ps_heap_summary->old();
 232     const SpaceSummary& old_space = ps_heap_summary->old_space();
 233     const VirtualSpaceSummary& young_summary = ps_heap_summary->young();
 234     const SpaceSummary& eden_space = ps_heap_summary->eden();
 235     const SpaceSummary& from_space = ps_heap_summary->from();
 236     const SpaceSummary& to_space = ps_heap_summary->to();
 237 
 238     EventPSHeapSummary e;
 239     if (e.should_commit()) {
 240       e.set_gcId(_id);
 241       e.set_when((u1)_when);
 242 
 243       e.set_oldSpace(to_trace_struct(ps_heap_summary->old()));
 244       e.set_oldObjectSpace(to_trace_struct(ps_heap_summary->old_space()));
 245       e.set_youngSpace(to_trace_struct(ps_heap_summary->young()));
 246       e.set_edenSpace(to_trace_struct(ps_heap_summary->eden()));
 247       e.set_fromSpace(to_trace_struct(ps_heap_summary->from()));
 248       e.set_toSpace(to_trace_struct(ps_heap_summary->to()));
 249       e.commit();
 250     }
 251   }
 252 };
 253 
 254 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 255   GCHeapSummaryEventSender visitor(_shared_gc_info.id(), when);
 256   heap_summary.accept(&visitor);
 257 }
 258 
 259 static TraceStructMetaspaceSizes to_trace_struct(const MetaspaceSizes& sizes) {
 260   TraceStructMetaspaceSizes meta_sizes;
 261 
 262   meta_sizes.set_capacity(sizes.capacity());
 263   meta_sizes.set_used(sizes.used());
 264   meta_sizes.set_reserved(sizes.reserved());
 265 
 266   return meta_sizes;
 267 }
 268 
 269 void GCTracer::send_meta_space_summary_event(GCWhen::Type when, const MetaspaceSummary& meta_space_summary) const {
 270   EventMetaspaceSummary e;
 271   if (e.should_commit()) {
 272     e.set_gcId(_shared_gc_info.id());
 273     e.set_when((u1) when);
 274     e.set_gcThreshold(meta_space_summary.gc_threshold());
 275     e.set_metaspace(to_trace_struct(meta_space_summary.meta_space()));
 276     e.set_dataSpace(to_trace_struct(meta_space_summary.data_space()));
 277     e.set_classSpace(to_trace_struct(meta_space_summary.class_space()));
 278     e.commit();
 279   }
 280 }
 281 
 282 class PhaseSender : public PhaseVisitor {
 283   GCId _gc_id;
 284  public:
 285   PhaseSender(GCId gc_id) : _gc_id(gc_id) {}
 286 
 287   template<typename T>
 288   void send_phase(PausePhase* pause) {
 289     T event(UNTIMED);
 290     if (event.should_commit()) {
 291       event.set_gcId(_gc_id);
 292       event.set_name(pause->name());
 293       event.set_starttime(pause->start());
 294       event.set_endtime(pause->end());
 295       event.commit();
 296     }
 297   }
 298 
 299   void visit(GCPhase* pause) { ShouldNotReachHere(); }
 300   void visit(ConcurrentPhase* pause) { Unimplemented(); }
 301   void visit(PausePhase* pause) {
 302     assert(PhasesStack::PHASE_LEVELS == 5, "Need more event types");
 303 
 304     switch (pause->level()) {
 305       case 0: send_phase<EventGCPhasePause>(pause); break;
 306       case 1: send_phase<EventGCPhasePauseLevel1>(pause); break;
 307       case 2: send_phase<EventGCPhasePauseLevel2>(pause); break;
 308       case 3: send_phase<EventGCPhasePauseLevel3>(pause); break;
 309       default: /* Ignore sending this phase */ break;
 310     }
 311   }
 312 };
 313 
 314 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 315   PhaseSender phase_reporter(_shared_gc_info.id());
 316 
 317   TimePartitionPhasesIterator iter(time_partitions);
 318   while (iter.has_next()) {
 319     GCPhase* phase = iter.next();
 320     phase->accept(&phase_reporter);
 321   }
 322 }