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 ParallelOldTracer::send_parallel_old_event() const {
  68   EventGCParallelOld e(UNTIMED);
  69   if (e.should_commit()) {
  70     e.set_gcId(_shared_gc_info.id());
  71     e.set_densePrefix((TraceAddress)_parallel_old_gc_info.dense_prefix());
  72     e.set_starttime(_shared_gc_info.start_timestamp());
  73     e.set_endtime(_shared_gc_info.end_timestamp());
  74     e.commit();
  75   }
  76 }
  77 
  78 void YoungGCTracer::send_young_gc_event() const {
  79   EventGCYoungGarbageCollection e(UNTIMED);
  80   if (e.should_commit()) {
  81     e.set_gcId(_shared_gc_info.id());
  82     e.set_tenuringThreshold(_tenuring_threshold);
  83     e.set_starttime(_shared_gc_info.start_timestamp());
  84     e.set_endtime(_shared_gc_info.end_timestamp());
  85     e.commit();
  86   }
  87 }
  88 
  89 void OldGCTracer::send_old_gc_event() const {
  90   EventGCOldGarbageCollection e(UNTIMED);
  91   if (e.should_commit()) {
  92     e.set_gcId(_shared_gc_info.id());
  93     e.set_starttime(_shared_gc_info.start_timestamp());
  94     e.set_endtime(_shared_gc_info.end_timestamp());
  95     e.commit();
  96   }
  97 }
  98 
  99 static TraceStructCopyFailed to_trace_struct(const CopyFailedInfo& cf_info) {
 100   TraceStructCopyFailed failed_info;
 101   failed_info.set_objectCount(cf_info.failed_count());
 102   failed_info.set_firstSize(cf_info.first_size());
 103   failed_info.set_smallestSize(cf_info.smallest_size());
 104   failed_info.set_totalSize(cf_info.total_size());
 105   return failed_info;
 106 }
 107 
 108 void YoungGCTracer::send_promotion_failed_event(const PromotionFailedInfo& pf_info) const {
 109   EventPromotionFailed e;
 110   if (e.should_commit()) {
 111     e.set_gcId(_shared_gc_info.id());
 112     e.set_data(to_trace_struct(pf_info));
 113     e.set_thread(pf_info.thread()->thread_id());
 114     e.commit();
 115   }
 116 }
 117 
 118 // Common to CMS and G1
 119 void OldGCTracer::send_concurrent_mode_failure_event() {
 120   EventConcurrentModeFailure e;
 121   if (e.should_commit()) {
 122     e.set_gcId(_shared_gc_info.id());
 123     e.commit();
 124   }
 125 }
 126 
 127 #if INCLUDE_ALL_GCS
 128 void G1NewTracer::send_g1_young_gc_event() {
 129   EventGCG1GarbageCollection e(UNTIMED);
 130   if (e.should_commit()) {
 131     e.set_gcId(_shared_gc_info.id());
 132     e.set_type(_g1_young_gc_info.type());
 133     e.set_starttime(_shared_gc_info.start_timestamp());
 134     e.set_endtime(_shared_gc_info.end_timestamp());
 135     e.commit();
 136   }
 137 }
 138 
 139 void G1NewTracer::send_evacuation_info_event(EvacuationInfo* info) {
 140   EventEvacuationInfo e;
 141   if (e.should_commit()) {
 142     e.set_gcId(_shared_gc_info.id());
 143     e.set_cSetRegions(info->collectionset_regions());
 144     e.set_cSetUsedBefore(info->collectionset_used_before());
 145     e.set_cSetUsedAfter(info->collectionset_used_after());
 146     e.set_allocationRegions(info->allocation_regions());
 147     e.set_allocRegionsUsedBefore(info->alloc_regions_used_before());
 148     e.set_allocRegionsUsedAfter(info->alloc_regions_used_before() + info->bytes_copied());
 149     e.set_bytesCopied(info->bytes_copied());
 150     e.set_regionsFreed(info->regions_freed());
 151     e.commit();
 152   }
 153 }
 154 
 155 void G1NewTracer::send_evacuation_failed_event(const EvacuationFailedInfo& ef_info) const {
 156   EventEvacuationFailed e;
 157   if (e.should_commit()) {
 158     e.set_gcId(_shared_gc_info.id());
 159     e.set_data(to_trace_struct(ef_info));
 160     e.commit();
 161   }
 162 }
 163 #endif
 164 
 165 static TraceStructVirtualSpace to_trace_struct(const VirtualSpaceSummary& summary) {
 166   TraceStructVirtualSpace space;
 167   space.set_start((TraceAddress)summary.start());
 168   space.set_committedEnd((TraceAddress)summary.committed_end());
 169   space.set_committedSize(summary.committed_size());
 170   space.set_reservedEnd((TraceAddress)summary.reserved_end());
 171   space.set_reservedSize(summary.reserved_size());
 172   return space;
 173 }
 174 
 175 static TraceStructObjectSpace to_trace_struct(const SpaceSummary& summary) {
 176   TraceStructObjectSpace space;
 177   space.set_start((TraceAddress)summary.start());
 178   space.set_end((TraceAddress)summary.end());
 179   space.set_used(summary.used());
 180   space.set_size(summary.size());
 181   return space;
 182 }
 183 
 184 class GCHeapSummaryEventSender : public GCHeapSummaryVisitor {
 185   GCId _id;
 186   GCWhen::Type _when;
 187  public:
 188   GCHeapSummaryEventSender(GCId id, GCWhen::Type when) : _id(id), _when(when) {}
 189 
 190   void visit(const GCHeapSummary* heap_summary) const {
 191     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 192 
 193     EventGCHeapSummary e;
 194     if (e.should_commit()) {
 195       e.set_gcId(_id);
 196       e.set_when((u1)_when);
 197       e.set_heapSpace(to_trace_struct(heap_space));
 198       e.set_heapUsed(heap_summary->used());
 199       e.commit();
 200     }
 201   }
 202 
 203   void visit(const PSHeapSummary* ps_heap_summary) const {
 204     visit((GCHeapSummary*)ps_heap_summary);
 205 
 206     const VirtualSpaceSummary& old_summary = ps_heap_summary->old();
 207     const SpaceSummary& old_space = ps_heap_summary->old_space();
 208     const VirtualSpaceSummary& young_summary = ps_heap_summary->young();
 209     const SpaceSummary& eden_space = ps_heap_summary->eden();
 210     const SpaceSummary& from_space = ps_heap_summary->from();
 211     const SpaceSummary& to_space = ps_heap_summary->to();
 212 
 213     EventPSHeapSummary e;
 214     if (e.should_commit()) {
 215       e.set_gcId(_id);
 216       e.set_when((u1)_when);
 217 
 218       e.set_oldSpace(to_trace_struct(ps_heap_summary->old()));
 219       e.set_oldObjectSpace(to_trace_struct(ps_heap_summary->old_space()));
 220       e.set_youngSpace(to_trace_struct(ps_heap_summary->young()));
 221       e.set_edenSpace(to_trace_struct(ps_heap_summary->eden()));
 222       e.set_fromSpace(to_trace_struct(ps_heap_summary->from()));
 223       e.set_toSpace(to_trace_struct(ps_heap_summary->to()));
 224       e.commit();
 225     }
 226   }
 227 };
 228 
 229 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 230   GCHeapSummaryEventSender visitor(_shared_gc_info.id(), when);
 231   heap_summary.accept(&visitor);
 232 }
 233 
 234 static TraceStructMetaspaceSizes to_trace_struct(const MetaspaceSizes& sizes) {
 235   TraceStructMetaspaceSizes meta_sizes;
 236 
 237   meta_sizes.set_capacity(sizes.capacity());
 238   meta_sizes.set_used(sizes.used());
 239   meta_sizes.set_reserved(sizes.reserved());
 240 
 241   return meta_sizes;
 242 }
 243 
 244 void GCTracer::send_meta_space_summary_event(GCWhen::Type when, const MetaspaceSummary& meta_space_summary) const {
 245   EventMetaspaceSummary e;
 246   if (e.should_commit()) {
 247     e.set_gcId(_shared_gc_info.id());
 248     e.set_when((u1) when);
 249     e.set_metaspace(to_trace_struct(meta_space_summary.meta_space()));
 250     e.set_dataSpace(to_trace_struct(meta_space_summary.data_space()));
 251     e.set_classSpace(to_trace_struct(meta_space_summary.class_space()));
 252     e.commit();
 253   }
 254 }
 255 
 256 class PhaseSender : public PhaseVisitor {
 257   GCId _gc_id;
 258  public:
 259   PhaseSender(GCId gc_id) : _gc_id(gc_id) {}
 260 
 261   template<typename T>
 262   void send_phase(PausePhase* pause) {
 263     T event(UNTIMED);
 264     if (event.should_commit()) {
 265       event.set_gcId(_gc_id);
 266       event.set_name(pause->name());
 267       event.set_starttime(pause->start());
 268       event.set_endtime(pause->end());
 269       event.commit();
 270     }
 271   }
 272 
 273   void visit(GCPhase* pause) { ShouldNotReachHere(); }
 274   void visit(ConcurrentPhase* pause) { Unimplemented(); }
 275   void visit(PausePhase* pause) {
 276     assert(PhasesStack::PHASE_LEVELS == 5, "Need more event types");
 277 
 278     switch (pause->level()) {
 279       case 0: send_phase<EventGCPhasePause>(pause); break;
 280       case 1: send_phase<EventGCPhasePauseLevel1>(pause); break;
 281       case 2: send_phase<EventGCPhasePauseLevel2>(pause); break;
 282       case 3: send_phase<EventGCPhasePauseLevel3>(pause); break;
 283       default: /* Ignore sending this phase */ break;
 284     }
 285   }
 286 };
 287 
 288 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 289   PhaseSender phase_reporter(_shared_gc_info.id());
 290 
 291   TimePartitionPhasesIterator iter(time_partitions);
 292   while (iter.has_next()) {
 293     GCPhase* phase = iter.next();
 294     phase->accept(&phase_reporter);
 295   }
 296 }