1 /*
   2  * Copyright (c) 2020, 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 #include "precompiled.hpp"
  25 #include "ci/ciMethod.hpp"
  26 #include "compiler/compilerEvent.hpp"
  27 #include "jfr/jfr.hpp"
  28 #include "jfr/jfrEvents.hpp"
  29 #include "jfr/metadata/jfrSerializer.hpp"
  30 #include "runtime/semaphore.inline.hpp"
  31 #include "utilities/growableArray.hpp"
  32 
  33 // Synchronizes access to phases_names.
  34 class PhaseTypeGuard : public StackObj {
  35  private:
  36   static Semaphore _mutex_semaphore;
  37  public:
  38   PhaseTypeGuard() {
  39     _mutex_semaphore.wait();
  40   }
  41   ~PhaseTypeGuard() {
  42     _mutex_semaphore.signal();
  43   }
  44 };
  45 
  46 Semaphore PhaseTypeGuard::_mutex_semaphore(1);
  47 
  48 static void write_phases(JfrCheckpointWriter& writer, u4 base_idx, GrowableArray<const char*>* phases) {
  49   assert(phases != NULL, "invariant");
  50   assert(phases->is_nonempty(), "invariant");
  51   const u4 nof_entries = phases->length();
  52   writer.write_count(nof_entries);
  53   for (u4 i = 0; i < nof_entries; i++) {
  54     writer.write_key(base_idx + i);
  55     writer.write(phases->at(i));
  56   }
  57 }
  58 
  59 static GrowableArray<const char*>* phase_names = NULL;
  60 
  61 class CompilerPhaseTypeConstant : public JfrSerializer {
  62  public:
  63   void serialize(JfrCheckpointWriter& writer) {
  64     PhaseTypeGuard guard;
  65     write_phases(writer, 0, phase_names);
  66   }
  67 };
  68 
  69 // This function provides support for adding dynamic entries to JFR type CompilerPhaseType.
  70 // The mapping for CompilerPhaseType is maintained as growable array phase_names.
  71 // The serializer CompilerPhaseTypeConstant must be registered with JFR at vm init.
  72 // Registration of new phase names creates mapping, serialize it for current chunk and registers its serializer with JFR if it is not already done.
  73 int CompilerEvent::PhaseEvent::register_phases(GrowableArray<const char*>* new_phases) {
  74   int idx = -1;
  75   if (new_phases == NULL || new_phases->is_empty()) {
  76     return idx;
  77   }
  78   bool register_jfr_serializer = false;
  79   {
  80     PhaseTypeGuard guard;
  81     if (phase_names == NULL) {
  82       phase_names = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<const char*>(100, true);
  83       register_jfr_serializer = true;
  84     }
  85     idx = phase_names->length();
  86     phase_names->appendAll(new_phases);
  87     guarantee(phase_names->length() < 256, "exceeds maximum supported phases");
  88   }
  89   if (register_jfr_serializer) {
  90     JfrSerializer::register_serializer(TYPE_COMPILERPHASETYPE, false, new CompilerPhaseTypeConstant());
  91   } else if (Jfr::is_recording()) {
  92     // serialize new_phases.
  93     JfrCheckpointWriter writer;
  94     writer.write_type(TYPE_COMPILERPHASETYPE);
  95     write_phases(writer, idx, new_phases);
  96   }
  97   return idx;
  98 }
  99 
 100 void CompilerEvent::CompilationEvent::post(EventCompilation& event, int compile_id, CompilerType compiler_type, Method* method, int compile_level, bool success, bool is_osr, int code_size, int inlined_bytecodes) {
 101   event.set_compileId(compile_id);
 102   event.set_compiler(compiler_type);
 103   event.set_method(method);
 104   event.set_compileLevel((short)compile_level);
 105   event.set_succeded(success);
 106   event.set_isOsr(is_osr);
 107   event.set_codeSize(code_size);
 108   event.set_inlinedBytes(inlined_bytecodes);
 109   event.commit();
 110 }
 111 
 112 void CompilerEvent::CompilationFailureEvent::post(EventCompilationFailure& event, int compile_id, const char* reason) {
 113   event.set_compileId(compile_id);
 114   event.set_failureMessage(reason);
 115   event.commit();
 116 }
 117 
 118 void CompilerEvent::PhaseEvent::post(EventCompilerPhase& event, const Ticks& start_time, int phase, int compile_id, int level) {
 119   event.set_starttime(start_time);
 120   event.set_phase((u1) phase);
 121   event.set_compileId(compile_id);
 122   event.set_phaseLevel((short)level);
 123   event.commit();
 124 }
 125 
 126 void CompilerEvent::InlineEvent::post(EventCompilerInlining& event, int compile_id, Method* caller, const JfrStructCalleeMethod& callee, bool success, const char* msg, int bci) {
 127   event.set_compileId(compile_id);
 128   event.set_caller(caller);
 129   event.set_callee(callee);
 130   event.set_succeeded(success);
 131   event.set_message(msg);
 132   event.set_bci(bci);
 133   event.commit();
 134 }
 135 
 136 void CompilerEvent::InlineEvent::post(EventCompilerInlining& event, int compile_id, Method* caller, Method* callee, bool success, const char* msg, int bci) {
 137   JfrStructCalleeMethod callee_struct;
 138   callee_struct.set_type(callee->klass_name()->as_utf8());
 139   callee_struct.set_name(callee->name()->as_utf8());
 140   callee_struct.set_descriptor(callee->signature()->as_utf8());
 141   post(event, compile_id, caller, callee_struct, success, msg, bci);
 142 }
 143 
 144 void CompilerEvent::InlineEvent::post(EventCompilerInlining& event, int compile_id, Method* caller, ciMethod* callee, bool success, const char* msg, int bci) {
 145   JfrStructCalleeMethod callee_struct;
 146   callee_struct.set_type(callee->holder()->name()->as_utf8());
 147   callee_struct.set_name(callee->name()->as_utf8());
 148   callee_struct.set_descriptor(callee->signature()->as_symbol()->as_utf8());
 149   post(event, compile_id, caller, callee_struct, success, msg, bci);
 150 }