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