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/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 static TraceStructCopyFailed to_trace_struct(const CopyFailedInfo& cf_info) {
  96   TraceStructCopyFailed failed_info;
  97   failed_info.set_objectCount(cf_info.failed_count());
  98   failed_info.set_firstSize(cf_info.first_size());
  99   failed_info.set_smallestSize(cf_info.smallest_size());
 100   failed_info.set_totalSize(cf_info.total_size());
 101   failed_info.set_thread(cf_info.thread()->thread_id());
 102   return failed_info;
 103 }
 104 
 105 void YoungGCTracer::send_promotion_failed_event(const PromotionFailedInfo& pf_info) const {
 106   EventPromotionFailed e;
 107   if (e.should_commit()) {
 108     e.set_gcId(_shared_gc_info.id());
 109     e.set_data(to_trace_struct(pf_info));
 110     e.commit();
 111   }
 112 }
 113 
 114 #ifndef SERIALGC
 115 void G1NewTracer::send_g1_young_gc_event() {
 116   EventGCG1GarbageCollection e(UNTIMED);
 117   if (e.should_commit()) {
 118     e.set_gcId(_shared_gc_info.id());
 119     e.set_type(_g1_young_gc_info.type());
 120     e.set_starttime(_shared_gc_info.start_timestamp());
 121     e.set_endtime(_shared_gc_info.end_timestamp());
 122     e.commit();
 123   }
 124 }
 125 #endif
 126 
 127 static TraceStructVirtualSpace to_trace_struct(const VirtualSpaceSummary& summary) {
 128   TraceStructVirtualSpace space;
 129   space.set_start((TraceAddress)summary.start());
 130   space.set_committedEnd((TraceAddress)summary.committed_end());
 131   space.set_committedSize(summary.committed_size());
 132   space.set_reservedEnd((TraceAddress)summary.reserved_end());
 133   space.set_reservedSize(summary.reserved_size());
 134   return space;
 135 }
 136 
 137 static TraceStructObjectSpace to_trace_struct(const SpaceSummary& summary) {
 138   TraceStructObjectSpace space;
 139   space.set_start((TraceAddress)summary.start());
 140   space.set_end((TraceAddress)summary.end());
 141   space.set_used(summary.used());
 142   space.set_size(summary.size());
 143   return space;
 144 }
 145 
 146 class GCHeapSummaryEventSender : public GCHeapSummaryVisitor {
 147   GCId _id;
 148   GCWhen::Type _when;
 149  public:
 150   GCHeapSummaryEventSender(GCId id, GCWhen::Type when) : _id(id), _when(when) {}
 151 
 152   void visit(const GCHeapSummary* heap_summary) const {
 153     const VirtualSpaceSummary& heap_space = heap_summary->heap();
 154 
 155     EventGCHeapSummary e;
 156     if (e.should_commit()) {
 157       e.set_gcId(_id);
 158       e.set_when((u1)_when);
 159       e.set_heapSpace(to_trace_struct(heap_space));
 160       e.set_heapUsed(heap_summary->used());
 161       e.commit();
 162     }
 163   }
 164 
 165   void visit(const PSHeapSummary* ps_heap_summary) const {
 166     visit((GCHeapSummary*)ps_heap_summary);
 167 
 168     EventPSHeapSummary e;
 169     if (e.should_commit()) {
 170       e.set_gcId(_id);
 171       e.set_when((u1)_when);
 172       e.set_oldSpace(to_trace_struct(ps_heap_summary->old()));
 173       e.set_oldObjectSpace(to_trace_struct(ps_heap_summary->old_space()));
 174       e.set_youngSpace(to_trace_struct(ps_heap_summary->young()));
 175       e.set_edenSpace(to_trace_struct(ps_heap_summary->eden()));
 176       e.set_fromSpace(to_trace_struct(ps_heap_summary->from()));
 177       e.set_toSpace(to_trace_struct(ps_heap_summary->to()));
 178       e.commit();
 179     }
 180   }
 181 };
 182 
 183 void GCTracer::send_gc_heap_summary_event(GCWhen::Type when, const GCHeapSummary& heap_summary) const {
 184   GCHeapSummaryEventSender visitor(_shared_gc_info.id(), when);
 185   heap_summary.accept(&visitor);
 186 }
 187 
 188 void GCTracer::send_perm_gen_summary_event(GCWhen::Type when, const PermGenSummary& perm_gen_summary) const {
 189   const VirtualSpaceSummary& perm_space = perm_gen_summary.perm_space();
 190   const SpaceSummary& object_space = perm_gen_summary.object_space();
 191 
 192   EventPermGenSummary e;
 193   if (e.should_commit()) {
 194     e.set_gcId(_shared_gc_info.id());
 195     e.set_when((u1) when);
 196     e.set_permSpace(to_trace_struct(perm_space));
 197     e.set_objectSpace(to_trace_struct(object_space));
 198     e.commit();
 199   }
 200 }
 201 
 202 class PhaseSender : public PhaseVisitor {
 203   GCId _gc_id;
 204  public:
 205   PhaseSender(GCId gc_id) : _gc_id(gc_id) {}
 206 
 207   template<typename T>
 208   void send_phase(PausePhase* pause) {
 209     T event(UNTIMED);
 210     if (event.should_commit()) {
 211       event.set_gcId(_gc_id);
 212       event.set_name(pause->name());
 213       event.set_starttime(pause->start());
 214       event.set_endtime(pause->end());
 215       event.commit();
 216     }
 217   }
 218 
 219   void visit(GCPhase* pause) { ShouldNotReachHere(); }
 220   void visit(ConcurrentPhase* pause) { Unimplemented(); }
 221   void visit(PausePhase* pause) {
 222     assert(PhasesStack::PHASE_LEVELS == 5, "Need more event types");
 223 
 224     switch (pause->level()) {
 225       case 0: send_phase<EventGCPhasePause>(pause); break;
 226       case 1: send_phase<EventGCPhasePauseLevel1>(pause); break;
 227       case 2: send_phase<EventGCPhasePauseLevel2>(pause); break;
 228       case 3: send_phase<EventGCPhasePauseLevel3>(pause); break;
 229       default: /* Ignore sending this phase */ break;
 230     }
 231   }
 232 
 233 #undef send_phase
 234 };
 235 
 236 void GCTracer::send_phase_events(TimePartitions* time_partitions) const {
 237   PhaseSender phase_reporter(_shared_gc_info.id());
 238 
 239   TimePartitionPhasesIterator iter(time_partitions);
 240   while (iter.has_next()) {
 241     GCPhase* phase = iter.next();
 242     phase->accept(&phase_reporter);
 243   }
 244 }