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 #ifndef SERIALGC
  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_SERVICES
 127 void GCTracer::send_object_count_after_gc_event(Klass* klass, jlong count, julong total_size) const {
 128   EventObjectCountAfterGC e;
 129   if (e.should_commit()) {
 130     e.set_gcId(_shared_gc_info.id());
 131     e.set_class(klass);
 132     e.set_count(count);
 133     e.set_totalSize(total_size);
 134     e.commit();
 135   }
 136 }
 137 #endif
 138 
 139 bool GCTracer::should_send_object_count_after_gc_event() const {
 140 #if INCLUDE_TRACE
 141   return Tracing::is_event_enabled(EventObjectCountAfterGC::eventId);
 142 #else
 143   return false;
 144 #endif
 145 }
 146 
 147 #ifndef SERIALGC
 148 void G1NewTracer::send_g1_young_gc_event() {
 149   EventGCG1GarbageCollection e(UNTIMED);
 150   if (e.should_commit()) {
 151     e.set_gcId(_shared_gc_info.id());
 152     e.set_type(_g1_young_gc_info.type());
 153     e.set_starttime(_shared_gc_info.start_timestamp());
 154     e.set_endtime(_shared_gc_info.end_timestamp());
 155     e.commit();
 156   }
 157 }
 158 
 159 void G1NewTracer::send_evacuation_info_event(EvacuationInfo* info) {
 160   EventEvacuationInfo e;
 161   if (e.should_commit()) {
 162     e.set_gcId(_shared_gc_info.id());
 163     e.set_cSetRegions(info->collectionset_regions());
 164     e.set_cSetUsedBefore(info->collectionset_used_before());
 165     e.set_cSetUsedAfter(info->collectionset_used_after());
 166     e.set_allocationRegions(info->allocation_regions());
 167     e.set_allocRegionsUsedBefore(info->alloc_regions_used_before());
 168     e.set_allocRegionsUsedAfter(info->alloc_regions_used_before() + info->bytes_copied());
 169     e.set_bytesCopied(info->bytes_copied());
 170     e.set_regionsFreed(info->regions_freed());
 171     e.commit();
 172   }
 173 }
 174 
 175 void G1NewTracer::send_evacuation_failed_event(const EvacuationFailedInfo& ef_info) const {
 176   EventEvacuationFailed e;
 177   if (e.should_commit()) {
 178     e.set_gcId(_shared_gc_info.id());
 179     e.set_data(to_trace_struct(ef_info));
 180     e.commit();
 181   }
 182 }
 183 #endif
 184 
 185 static TraceStructVirtualSpace to_trace_struct(const VirtualSpaceSummary& summary) {
 186   TraceStructVirtualSpace 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 TraceStructObjectSpace to_trace_struct(const SpaceSummary& summary) {
 196   TraceStructObjectSpace 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   GCId _id;
 206   GCWhen::Type _when;
 207  public:
 208   GCHeapSummaryEventSender(GCId id, GCWhen::Type when) : _id(id), _when(when) {}
 209 
 210   void visit(const GCHeapSummary* heap_summary) const {
 211     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 212 
 213     EventGCHeapSummary e;
 214     if (e.should_commit()) {
 215       e.set_gcId(_id);
 216       e.set_when((u1)_when);
 217       e.set_heapSpace(to_trace_struct(heap_space));
 218       e.set_heapUsed(heap_summary->used());
 219       e.commit();
 220     }
 221   }
 222 
 223   void visit(const PSHeapSummary* ps_heap_summary) const {
 224     visit((GCHeapSummary*)ps_heap_summary);
 225 
 226     const VirtualSpaceSummary& old_summary = ps_heap_summary->old();
 227     const SpaceSummary& old_space = ps_heap_summary->old_space();
 228     const VirtualSpaceSummary& young_summary = ps_heap_summary->young();
 229     const SpaceSummary& eden_space = ps_heap_summary->eden();
 230     const SpaceSummary& from_space = ps_heap_summary->from();
 231     const SpaceSummary& to_space = ps_heap_summary->to();
 232 
 233     EventPSHeapSummary e;
 234     if (e.should_commit()) {
 235       e.set_gcId(_id);
 236       e.set_when((u1)_when);
 237 
 238       e.set_oldSpace(to_trace_struct(ps_heap_summary->old()));
 239       e.set_oldObjectSpace(to_trace_struct(ps_heap_summary->old_space()));
 240       e.set_youngSpace(to_trace_struct(ps_heap_summary->young()));
 241       e.set_edenSpace(to_trace_struct(ps_heap_summary->eden()));
 242       e.set_fromSpace(to_trace_struct(ps_heap_summary->from()));
 243       e.set_toSpace(to_trace_struct(ps_heap_summary->to()));
 244       e.commit();
 245     }
 246   }
 247 };
 248 
 249 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 250   GCHeapSummaryEventSender visitor(_shared_gc_info.id(), when);
 251   heap_summary.accept(&visitor);
 252 }
 253 
 254 static TraceStructMetaspaceSizes to_trace_struct(const MetaspaceSizes& sizes) {
 255   TraceStructMetaspaceSizes meta_sizes;
 256 
 257   meta_sizes.set_capacity(sizes.capacity());
 258   meta_sizes.set_used(sizes.used());
 259   meta_sizes.set_reserved(sizes.reserved());
 260 
 261   return meta_sizes;
 262 }
 263 
 264 void GCTracer::send_meta_space_summary_event(GCWhen::Type when, const MetaspaceSummary& meta_space_summary) const {
 265   EventMetaspaceSummary e;
 266   if (e.should_commit()) {
 267     e.set_gcId(_shared_gc_info.id());
 268     e.set_when((u1) when);
 269     e.set_metaspace(to_trace_struct(meta_space_summary.meta_space()));
 270     e.set_dataSpace(to_trace_struct(meta_space_summary.data_space()));
 271     e.set_classSpace(to_trace_struct(meta_space_summary.class_space()));
 272     e.commit();
 273   }
 274 }
 275 
 276 class PhaseSender : public PhaseVisitor {
 277   GCId _gc_id;
 278  public:
 279   PhaseSender(GCId gc_id) : _gc_id(gc_id) {}
 280 
 281   template<typename T>
 282   void send_phase(PausePhase* pause) {
 283     T event(UNTIMED);
 284     if (event.should_commit()) {
 285       event.set_gcId(_gc_id);
 286       event.set_name(pause->name());
 287       event.set_starttime(pause->start());
 288       event.set_endtime(pause->end());
 289       event.commit();
 290     }
 291   }
 292 
 293   void visit(GCPhase* pause) { ShouldNotReachHere(); }
 294   void visit(ConcurrentPhase* pause) { Unimplemented(); }
 295   void visit(PausePhase* pause) {
 296     assert(PhasesStack::PHASE_LEVELS == 5, "Need more event types");
 297 
 298     switch (pause->level()) {
 299       case 0: send_phase<EventGCPhasePause>(pause); break;
 300       case 1: send_phase<EventGCPhasePauseLevel1>(pause); break;
 301       case 2: send_phase<EventGCPhasePauseLevel2>(pause); break;
 302       case 3: send_phase<EventGCPhasePauseLevel3>(pause); break;
 303       default: /* Ignore sending this phase */ break;
 304     }
 305   }
 306 
 307 #undef send_phase
 308 };
 309 
 310 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 311   PhaseSender phase_reporter(_shared_gc_info.id());
 312 
 313   TimePartitionPhasesIterator iter(time_partitions);
 314   while (iter.has_next()) {
 315     GCPhase* phase = iter.next();
 316     phase->accept(&phase_reporter);
 317   }
 318 }