1 /*
   2  * Copyright (c) 2012, 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/traceBackend.hpp"
  32 #include "trace/tracing.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   failed_info.set_thread(cf_info.thread()->thread_id());
 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.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 void GCTracer::send_object_count_after_gc_event(klassOop klass, jlong count, julong total_size) const {
 127   EventObjectCountAfterGC e;
 128   if (e.should_commit()) {
 129     e.set_gcId(_shared_gc_info.id());
 130     e.set_class(klass);
 131     e.set_count(count);
 132     e.set_totalSize(total_size);
 133     e.commit();
 134   }
 135 }
 136 
 137 bool GCTracer::should_send_object_count_after_gc_event() const {
 138 #if INCLUDE_TRACE
 139   return Tracing::enabled(EventObjectCountAfterGC::eventId);
 140 #else
 141   return false;
 142 #endif
 143 }
 144 
 145 #ifndef SERIALGC
 146 void G1NewTracer::send_g1_young_gc_event() {
 147   EventGCG1GarbageCollection e(UNTIMED);
 148   if (e.should_commit()) {
 149     e.set_gcId(_shared_gc_info.id());
 150     e.set_type(_g1_young_gc_info.type());
 151     e.set_starttime(_shared_gc_info.start_timestamp());
 152     e.set_endtime(_shared_gc_info.end_timestamp());
 153     e.commit();
 154   }
 155 }
 156 
 157 void G1NewTracer::send_evacuation_info_event(EvacuationInfo* info) {
 158   EventEvacuationInfo e;
 159   if (e.should_commit()) {
 160     e.set_gcId(_shared_gc_info.id());
 161     e.set_cSetRegions(info->collectionset_regions());
 162     e.set_cSetUsedBefore(info->collectionset_used_before());
 163     e.set_cSetUsedAfter(info->collectionset_used_after());
 164     e.set_allocationRegions(info->allocation_regions());
 165     e.set_allocRegionsUsedBefore(info->alloc_regions_used_before());
 166     e.set_allocRegionsUsedAfter(info->alloc_regions_used_before() + info->bytes_copied());
 167     e.set_bytesCopied(info->bytes_copied());
 168     e.set_regionsFreed(info->regions_freed());
 169     e.commit();
 170   }
 171 }
 172 #endif
 173 
 174 static TraceStructVirtualSpace to_trace_struct(const VirtualSpaceSummary& summary) {
 175   TraceStructVirtualSpace space;
 176   space.set_start((TraceAddress)summary.start());
 177   space.set_committedEnd((TraceAddress)summary.committed_end());
 178   space.set_committedSize(summary.committed_size());
 179   space.set_reservedEnd((TraceAddress)summary.reserved_end());
 180   space.set_reservedSize(summary.reserved_size());
 181   return space;
 182 }
 183 
 184 static TraceStructObjectSpace to_trace_struct(const SpaceSummary& summary) {
 185   TraceStructObjectSpace space;
 186   space.set_start((TraceAddress)summary.start());
 187   space.set_end((TraceAddress)summary.end());
 188   space.set_used(summary.used());
 189   space.set_size(summary.size());
 190   return space;
 191 }
 192 
 193 class GCHeapSummaryEventSender : public GCHeapSummaryVisitor {
 194   GCId _id;
 195   GCWhen::Type _when;
 196  public:
 197   GCHeapSummaryEventSender(GCId id, GCWhen::Type when) : _id(id), _when(when) {}
 198 
 199   void visit(const GCHeapSummary* heap_summary) const {
 200     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 201 
 202     EventGCHeapSummary e;
 203     if (e.should_commit()) {
 204       e.set_gcId(_id);
 205       e.set_when((u1)_when);
 206       e.set_heapSpace(to_trace_struct(heap_space));
 207       e.set_heapUsed(heap_summary->used());
 208       e.commit();
 209     }
 210   }
 211 
 212   void visit(const PSHeapSummary* ps_heap_summary) const {
 213     visit((GCHeapSummary*)ps_heap_summary);
 214 
 215     EventPSHeapSummary e;
 216     if (e.should_commit()) {
 217       e.set_gcId(_id);
 218       e.set_when((u1)_when);
 219       e.set_oldSpace(to_trace_struct(ps_heap_summary->old()));
 220       e.set_oldObjectSpace(to_trace_struct(ps_heap_summary->old_space()));
 221       e.set_youngSpace(to_trace_struct(ps_heap_summary->young()));
 222       e.set_edenSpace(to_trace_struct(ps_heap_summary->eden()));
 223       e.set_fromSpace(to_trace_struct(ps_heap_summary->from()));
 224       e.set_toSpace(to_trace_struct(ps_heap_summary->to()));
 225       e.commit();
 226     }
 227   }
 228 };
 229 
 230 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 231   GCHeapSummaryEventSender visitor(_shared_gc_info.id(), when);
 232   heap_summary.accept(&visitor);
 233 }
 234 
 235 void GCTracer::send_perm_gen_summary_event(GCWhen::Type when, const PermGenSummary& perm_gen_summary) const {
 236   const VirtualSpaceSummary& perm_space = perm_gen_summary.perm_space();
 237   const SpaceSummary& object_space = perm_gen_summary.object_space();
 238 
 239   EventPermGenSummary e;
 240   if (e.should_commit()) {
 241     e.set_gcId(_shared_gc_info.id());
 242     e.set_when((u1) when);
 243     e.set_permSpace(to_trace_struct(perm_space));
 244     e.set_objectSpace(to_trace_struct(object_space));
 245     e.commit();
 246   }
 247 }
 248 
 249 class PhaseSender : public PhaseVisitor {
 250   GCId _gc_id;
 251  public:
 252   PhaseSender(GCId gc_id) : _gc_id(gc_id) {}
 253 
 254   template<typename T>
 255   void send_phase(PausePhase* pause) {
 256     T event(UNTIMED);
 257     if (event.should_commit()) {
 258       event.set_gcId(_gc_id);
 259       event.set_name(pause->name());
 260       event.set_starttime(pause->start());
 261       event.set_endtime(pause->end());
 262       event.commit();
 263     }
 264   }
 265 
 266   void visit(GCPhase* pause) { ShouldNotReachHere(); }
 267   void visit(ConcurrentPhase* pause) { Unimplemented(); }
 268   void visit(PausePhase* pause) {
 269     assert(PhasesStack::PHASE_LEVELS == 5, "Need more event types");
 270 
 271     switch (pause->level()) {
 272       case 0: send_phase<EventGCPhasePause>(pause); break;
 273       case 1: send_phase<EventGCPhasePauseLevel1>(pause); break;
 274       case 2: send_phase<EventGCPhasePauseLevel2>(pause); break;
 275       case 3: send_phase<EventGCPhasePauseLevel3>(pause); break;
 276       default: /* Ignore sending this phase */ break;
 277     }
 278   }
 279 
 280 #undef send_phase
 281 };
 282 
 283 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 284   PhaseSender phase_reporter(_shared_gc_info.id());
 285 
 286   TimePartitionPhasesIterator iter(time_partitions);
 287   while (iter.has_next()) {
 288     GCPhase* phase = iter.next();
 289     phase->accept(&phase_reporter);
 290   }
 291 }