Path: blob/master/src/hotspot/share/jfr/writers/jfrJavaEventWriter.cpp
41152 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "jni.h"26#include "classfile/symbolTable.hpp"27#include "classfile/systemDictionary.hpp"28#include "classfile/vmSymbols.hpp"29#include "jfr/jni/jfrJavaSupport.hpp"30#include "jfr/recorder/storage/jfrStorage.hpp"31#include "jfr/support/jfrThreadId.hpp"32#include "jfr/utilities/jfrTypes.hpp"33#include "jfr/writers/jfrJavaEventWriter.hpp"34#include "memory/iterator.hpp"35#include "oops/instanceKlass.hpp"36#include "oops/oop.inline.hpp"37#include "runtime/fieldDescriptor.inline.hpp"38#include "runtime/handles.inline.hpp"39#include "runtime/jniHandles.inline.hpp"40#include "runtime/thread.inline.hpp"4142static int start_pos_offset = invalid_offset;43static int start_pos_address_offset = invalid_offset;44static int current_pos_offset = invalid_offset;45static int max_pos_offset = invalid_offset;46static int notified_offset = invalid_offset;47static int thread_id_offset = invalid_offset;48static int valid_offset = invalid_offset;4950static bool find_field(InstanceKlass* ik,51Symbol* name_symbol,52Symbol* signature_symbol,53fieldDescriptor* fd,54bool is_static = false,55bool allow_super = false) {56if (allow_super || is_static) {57return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL;58} else {59return ik->find_local_field(name_symbol, signature_symbol, fd);60}61}6263static void compute_offset(int &dest_offset,64Klass* klass,65Symbol* name_symbol,66Symbol* signature_symbol,67bool is_static = false, bool allow_super = false) {68fieldDescriptor fd;69InstanceKlass* ik = InstanceKlass::cast(klass);70if (!find_field(ik, name_symbol, signature_symbol, &fd, is_static, allow_super)) {71assert(false, "invariant");72}73dest_offset = fd.offset();74}7576static bool setup_event_writer_offsets(TRAPS) {77const char class_name[] = "jdk/jfr/internal/EventWriter";78Symbol* const k_sym = SymbolTable::new_symbol(class_name);79assert(k_sym != NULL, "invariant");80Klass* klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);81assert(klass != NULL, "invariant");8283const char start_pos_name[] = "startPosition";84Symbol* const start_pos_sym = SymbolTable::new_symbol(start_pos_name);85assert(start_pos_sym != NULL, "invariant");86assert(invalid_offset == start_pos_offset, "invariant");87compute_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());88assert(start_pos_offset != invalid_offset, "invariant");8990const char start_pos_address_name[] = "startPositionAddress";91Symbol* const start_pos_address_sym = SymbolTable::new_symbol(start_pos_address_name);92assert(start_pos_address_sym != NULL, "invariant");93assert(invalid_offset == start_pos_address_offset, "invariant");94compute_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());95assert(start_pos_address_offset != invalid_offset, "invariant");9697const char event_pos_name[] = "currentPosition";98Symbol* const event_pos_sym = SymbolTable::new_symbol(event_pos_name);99assert(event_pos_sym != NULL, "invariant");100assert(invalid_offset == current_pos_offset, "invariant");101compute_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());102assert(current_pos_offset != invalid_offset, "invariant");103104const char max_pos_name[] = "maxPosition";105Symbol* const max_pos_sym = SymbolTable::new_symbol(max_pos_name);106assert(max_pos_sym != NULL, "invariant");107assert(invalid_offset == max_pos_offset, "invariant");108compute_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());109assert(max_pos_offset != invalid_offset, "invariant");110111const char notified_name[] = "notified";112Symbol* const notified_sym = SymbolTable::new_symbol(notified_name);113assert (notified_sym != NULL, "invariant");114assert(invalid_offset == notified_offset, "invariant");115compute_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());116assert(notified_offset != invalid_offset, "invariant");117118const char valid_name[] = "valid";119Symbol* const valid_sym = SymbolTable::new_symbol(valid_name);120assert (valid_sym != NULL, "invariant");121assert(invalid_offset == valid_offset, "invariant");122compute_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());123assert(valid_offset != invalid_offset, "invariant");124return true;125}126127bool JfrJavaEventWriter::initialize() {128static bool initialized = false;129if (!initialized) {130initialized = setup_event_writer_offsets(JavaThread::current());131}132return initialized;133}134135jboolean JfrJavaEventWriter::flush(jobject writer, jint used, jint requested, JavaThread* jt) {136DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));137assert(writer != NULL, "invariant");138oop const w = JNIHandles::resolve_non_null(writer);139assert(w != NULL, "invariant");140JfrBuffer* const current = jt->jfr_thread_local()->java_buffer();141assert(current != NULL, "invariant");142JfrBuffer* const buffer = JfrStorage::flush(current, used, requested, false, jt);143assert(buffer != NULL, "invariant");144// "validity" is contextually defined here to mean145// that some memory location was provided that is146// large enough to accommodate the "requested size".147const bool is_valid = buffer->free_size() >= (size_t)(used + requested);148u1* const new_current_position = is_valid ? buffer->pos() + used : buffer->pos();149assert(start_pos_offset != invalid_offset, "invariant");150w->long_field_put(start_pos_offset, (jlong)buffer->pos());151w->long_field_put(current_pos_offset, (jlong)new_current_position);152// only update java writer if underlying memory changed153if (buffer != current) {154w->long_field_put(start_pos_address_offset, (jlong)buffer->pos_address());155w->long_field_put(max_pos_offset, (jlong)buffer->end());156}157if (!is_valid) {158// mark writer as invalid for this write attempt159w->release_bool_field_put(valid_offset, JNI_FALSE);160return JNI_FALSE;161}162// An exclusive use of a leased buffer is treated equivalent to163// holding a system resource. As such, it should be released as soon as possible.164// Returning true here signals that the thread will need to call flush again165// on EventWriter.endEvent() and that flush will return the lease.166return buffer->lease() ? JNI_TRUE : JNI_FALSE;167}168169class JfrJavaEventWriterNotificationClosure : public ThreadClosure {170public:171void do_thread(Thread* t) {172if (t->is_Java_thread()) {173JfrJavaEventWriter::notify(t->as_Java_thread());174}175}176};177178void JfrJavaEventWriter::notify() {179assert(SafepointSynchronize::is_at_safepoint(), "invariant");180JfrJavaEventWriterNotificationClosure closure;181Threads::threads_do(&closure);182}183184void JfrJavaEventWriter::notify(JavaThread* jt) {185assert(jt != NULL, "invariant");186assert(SafepointSynchronize::is_at_safepoint(), "invariant");187if (jt->jfr_thread_local()->has_java_event_writer()) {188oop buffer_writer = JNIHandles::resolve_non_null(jt->jfr_thread_local()->java_event_writer());189assert(buffer_writer != NULL, "invariant");190buffer_writer->release_bool_field_put(notified_offset, JNI_TRUE);191}192}193194static jobject create_new_event_writer(JfrBuffer* buffer, TRAPS) {195assert(buffer != NULL, "invariant");196DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));197HandleMark hm(THREAD);198static const char klass[] = "jdk/jfr/internal/EventWriter";199static const char method[] = "<init>";200static const char signature[] = "(JJJJZ)V";201JavaValue result(T_OBJECT);202JfrJavaArguments args(&result, klass, method, signature, CHECK_NULL);203// parameters204args.push_long((jlong)buffer->pos());205args.push_long((jlong)buffer->end());206args.push_long((jlong)buffer->pos_address());207args.push_long((jlong)JFR_THREAD_ID(THREAD));208args.push_int((int)JNI_TRUE);209JfrJavaSupport::new_object_global_ref(&args, CHECK_NULL);210return result.get_jobject();211}212213jobject JfrJavaEventWriter::event_writer(JavaThread* t) {214DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));215JfrThreadLocal* const tl = t->jfr_thread_local();216assert(tl->shelved_buffer() == NULL, "invariant");217return tl->java_event_writer();218}219220jobject JfrJavaEventWriter::new_event_writer(TRAPS) {221DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));222assert(event_writer(THREAD) == NULL, "invariant");223JfrThreadLocal* const tl = THREAD->jfr_thread_local();224assert(!tl->has_java_buffer(), "invariant");225JfrBuffer* const buffer = tl->java_buffer();226if (buffer == NULL) {227JfrJavaSupport::throw_out_of_memory_error("OOME for thread local buffer", THREAD);228return NULL;229}230jobject java_event_writer = create_new_event_writer(buffer, CHECK_NULL);231tl->set_java_event_writer(java_event_writer);232assert(tl->has_java_event_writer(), "invariant");233return java_event_writer;234}235236237