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