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