1 /*
   2  * Copyright (c) 2016, 2019, 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 "jfr/jni/jfrJavaSupport.hpp"
  27 #include "jfr/recorder/checkpoint/jfrMetadataEvent.hpp"
  28 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  29 #include "oops/klass.inline.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "oops/typeArrayOop.hpp"
  32 #include "runtime/semaphore.hpp"
  33 #include "runtime/thread.inline.hpp"
  34 #include "trace/traceDataTypes.hpp"
  35 
  36 static jbyteArray _metadata_blob = NULL;
  37 static Semaphore metadata_mutex_semaphore(1);
  38 
  39 void JfrMetadataEvent::lock() {
  40   metadata_mutex_semaphore.wait();
  41 }
  42 
  43 void JfrMetadataEvent::unlock() {
  44   metadata_mutex_semaphore.signal();
  45 }
  46 
  47 static void write_metadata_blob(JfrChunkWriter& chunkwriter, jbyteArray metadata_blob) {
  48   if (metadata_blob != NULL) {
  49     const typeArrayOop arr = (typeArrayOop)JfrJavaSupport::resolve_non_null(metadata_blob);
  50     assert(arr != NULL, "invariant");
  51     const int length = arr->length();
  52     const Klass* k = arr->klass();
  53     assert(k != NULL && k->oop_is_array(), "invariant");
  54     const TypeArrayKlass* const byte_arr_klass = TypeArrayKlass::cast(k);
  55     const jbyte* const data_address = arr->byte_at_addr(0);
  56     chunkwriter.write_unbuffered(data_address, length);
  57   }
  58 }
  59 
  60 // the semaphore is assumed to be locked  (was locked previous safepoint)
  61 size_t JfrMetadataEvent::write(JfrChunkWriter& chunkwriter, jlong metadata_offset) {
  62   assert(chunkwriter.is_valid(), "invariant");
  63   assert(chunkwriter.current_offset() == metadata_offset, "invariant");
  64   // header
  65   chunkwriter.reserve(sizeof(u4));
  66   chunkwriter.write<u8>(EVENT_METADATA); // ID 0
  67   // time data
  68   chunkwriter.write(JfrTraceTime::now().value());
  69   chunkwriter.write((u8)0); // duration
  70   chunkwriter.write((u8)0); // metadata id
  71   write_metadata_blob(chunkwriter, _metadata_blob); // payload
  72   unlock(); // open up for java to provide updated metadata
  73   // fill in size of metadata descriptor event
  74   const jlong size_written = chunkwriter.current_offset() - metadata_offset;
  75   chunkwriter.write_padded_at_offset((u4)size_written, metadata_offset);
  76   return size_written;
  77 }
  78 
  79 void JfrMetadataEvent::update(jbyteArray metadata) {
  80   JavaThread* thread = (JavaThread*)Thread::current();
  81   assert(thread->is_Java_thread(), "invariant");
  82   DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(thread));
  83   lock();
  84   if (_metadata_blob != NULL) {
  85     JfrJavaSupport::destroy_global_jni_handle(_metadata_blob);
  86   }
  87   const oop new_desc_oop = JfrJavaSupport::resolve_non_null(metadata);
  88   _metadata_blob = new_desc_oop != NULL ? (jbyteArray)JfrJavaSupport::global_jni_handle(new_desc_oop, thread) : NULL;
  89   unlock();
  90 }