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/promotionFailedInfo.hpp"
  31 #include "trace/tracing.hpp"
  32 #ifndef SERIALGC
  33 #include "gc_implementation/g1/g1YCTypes.hpp"
  34 #endif
  35 
  36 // All GC dependencies against the trace framework is contained within this file.
  37 
  38 typedef uintptr_t TraceAddress;
  39 
  40 void GCTracer::send_garbage_collection_event() const {
  41   EventGCGarbageCollection event(UNTIMED);
  42   if (event.should_commit()) {
  43     event.set_gcId(_shared_gc_info.id());
  44     event.set_name(_shared_gc_info.name());
  45     event.set_cause((u2) _shared_gc_info.cause());
  46     event.set_sumOfPauses(_shared_gc_info.sum_of_pauses());
  47     event.set_longestPause(_shared_gc_info.longest_pause());
  48     event.set_starttime(_shared_gc_info.start_timestamp());
  49     event.set_endtime(_shared_gc_info.end_timestamp());
  50     event.commit();
  51   }
  52 }
  53 
  54 void GCTracer::send_reference_stats_event(ReferenceType type, size_t count) const {
  55   EventGCReferenceStatistics e;
  56   if (e.should_commit()) {
  57       e.set_gcId(_shared_gc_info.id());
  58       e.set_type((u1)type);
  59       e.set_count(count);
  60       e.commit();
  61   }
  62 }
  63 
  64 void ParallelOldTracer::send_parallel_old_event() const {
  65   EventGCParallelOld e(UNTIMED);
  66   if (e.should_commit()) {
  67     e.set_gcId(_shared_gc_info.id());
  68     e.set_densePrefix((TraceAddress)_parallel_old_gc_info.dense_prefix());
  69     e.set_starttime(_shared_gc_info.start_timestamp());
  70     e.set_endtime(_shared_gc_info.end_timestamp());
  71     e.commit();
  72   }
  73 }
  74 
  75 void YoungGCTracer::send_young_gc_event() const {
  76   EventGCYoungGarbageCollection e(UNTIMED);
  77   if (e.should_commit()) {
  78     e.set_gcId(_shared_gc_info.id());
  79     e.set_starttime(_shared_gc_info.start_timestamp());
  80     e.set_endtime(_shared_gc_info.end_timestamp());
  81     e.commit();
  82   }
  83 }
  84 
  85 void OldGCTracer::send_old_gc_event() const {
  86   EventGCOldGarbageCollection e(UNTIMED);
  87   if (e.should_commit()) {
  88     e.set_gcId(_shared_gc_info.id());
  89     e.set_starttime(_shared_gc_info.start_timestamp());
  90     e.set_endtime(_shared_gc_info.end_timestamp());
  91     e.commit();
  92   }
  93 }
  94 
  95 void YoungGCTracer::send_promotion_failed_event(const PromotionFailedInfo& pf_info) const {
  96   EventPromotionFailed e;
  97   if (e.should_commit()) {
  98     e.set_gcId(_shared_gc_info.id());
  99     e.set_objectCount(pf_info.promotion_failed_count());
 100     e.set_firstSize(pf_info.first_size());
 101     e.set_smallestSize(pf_info.smallest_size());
 102     e.set_totalSize(pf_info.total_size());
 103     e.set_thread(pf_info.thread()->thread_id());
 104     e.commit();
 105   }
 106 }
 107 
 108 #ifndef SERIALGC
 109 void G1NewTracer::send_g1_young_gc_event() {
 110   EventGCG1GarbageCollection e(UNTIMED);
 111   if (e.should_commit()) {
 112     e.set_gcId(_shared_gc_info.id());
 113     e.set_type(_g1_young_gc_info.type());
 114     e.set_starttime(_shared_gc_info.start_timestamp());
 115     e.set_endtime(_shared_gc_info.end_timestamp());
 116     e.commit();
 117   }
 118 }
 119 #endif
 120 
 121 static TraceStructVirtualSpace to_trace_struct(const VirtualSpaceSummary& summary) {
 122   TraceStructVirtualSpace space;
 123   space.set_start((TraceAddress)summary.start());
 124   space.set_committedEnd((TraceAddress)summary.committed_end());
 125   space.set_committedSize(summary.committed_size());
 126   space.set_reservedEnd((TraceAddress)summary.reserved_end());
 127   space.set_reservedSize(summary.reserved_size());
 128   return space;
 129 }
 130 
 131 static TraceStructObjectSpace to_trace_struct(const SpaceSummary& summary) {
 132   TraceStructObjectSpace space;
 133   space.set_start((TraceAddress)summary.start());
 134   space.set_end((TraceAddress)summary.end());
 135   space.set_used(summary.used());
 136   space.set_size(summary.size());
 137   return space;
 138 }
 139 
 140 class GCHeapSummaryEventSender : public GCHeapSummaryVisitor {
 141   GCId _id;
 142   GCWhen::Type _when;
 143  public:
 144   GCHeapSummaryEventSender(GCId id, GCWhen::Type when) : _id(id), _when(when) {}
 145 
 146   void visit(const GCHeapSummary* heap_summary) const {
 147     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 148 
 149     EventGCHeapSummary e;
 150     if (e.should_commit()) {
 151       e.set_gcId(_id);
 152       e.set_when((u1)_when);
 153       e.set_heapSpace(to_trace_struct(heap_space));
 154       e.set_heapUsed(heap_summary->used());
 155       e.commit();
 156     }
 157   }
 158 
 159   void visit(const PSHeapSummary* ps_heap_summary) const {
 160     visit((GCHeapSummary*)ps_heap_summary);
 161 
 162     EventPSHeapSummary e;
 163     if (e.should_commit()) {
 164       e.set_gcId(_id);
 165       e.set_when((u1)_when);
 166       e.set_oldSpace(to_trace_struct(ps_heap_summary->old()));
 167       e.set_oldObjectSpace(to_trace_struct(ps_heap_summary->old_space()));
 168       e.set_youngSpace(to_trace_struct(ps_heap_summary->young()));
 169       e.set_edenSpace(to_trace_struct(ps_heap_summary->eden()));
 170       e.set_fromSpace(to_trace_struct(ps_heap_summary->from()));
 171       e.set_toSpace(to_trace_struct(ps_heap_summary->to()));
 172       e.commit();
 173     }
 174   }
 175 };
 176 
 177 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 178   GCHeapSummaryEventSender visitor(_shared_gc_info.id(), when);
 179   heap_summary.accept(&visitor);
 180 }
 181 
 182 void GCTracer::send_perm_gen_summary_event(GCWhen::Type when, const PermGenSummary& perm_gen_summary) const {
 183   const VirtualSpaceSummary& perm_space = perm_gen_summary.perm_space();
 184   const SpaceSummary& object_space = perm_gen_summary.object_space();
 185 
 186   EventPermGenSummary e;
 187   if (e.should_commit()) {
 188     e.set_gcId(_shared_gc_info.id());
 189     e.set_when((u1) when);
 190     e.set_permSpace(to_trace_struct(perm_space));
 191     e.set_objectSpace(to_trace_struct(object_space));
 192     e.commit();
 193   }
 194 }
 195 
 196 class PhaseSender : public PhaseVisitor {
 197   GCId _gc_id;
 198  public:
 199   PhaseSender(GCId gc_id) : _gc_id(gc_id) {}
 200 
 201   template<typename T>
 202   void send_phase(PausePhase* pause) {
 203     T event(UNTIMED);
 204     if (event.should_commit()) {
 205       event.set_gcId(_gc_id);
 206       event.set_name(pause->name());
 207       event.set_starttime(pause->start());
 208       event.set_endtime(pause->end());
 209       event.commit();
 210     }
 211   }
 212 
 213   void visit(GCPhase* pause) { ShouldNotReachHere(); }
 214   void visit(ConcurrentPhase* pause) { Unimplemented(); }
 215   void visit(PausePhase* pause) {
 216     assert(PhasesStack::PHASE_LEVELS == 5, "Need more event types");
 217 
 218     switch (pause->level()) {
 219       case 0: send_phase<EventGCPhasePause>(pause); break;
 220       case 1: send_phase<EventGCPhasePauseLevel1>(pause); break;
 221       case 2: send_phase<EventGCPhasePauseLevel2>(pause); break;
 222       case 3: send_phase<EventGCPhasePauseLevel3>(pause); break;
 223       default: /* Ignore sending this phase */ break;
 224     }
 225   }
 226 
 227 #undef send_phase
 228 };
 229 
 230 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 231   PhaseSender phase_reporter(_shared_gc_info.id());
 232 
 233   TimePartitionPhasesIterator iter(time_partitions);
 234   while (iter.has_next()) {
 235     GCPhase* phase = iter.next();
 236     phase->accept(&phase_reporter);
 237   }
 238 }