src/share/vm/jfr/writers/jfrJavaEventWriter.cpp

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
child 9884
1258121876f8
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

apetushkov@9858 1 /*
apetushkov@9858 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
apetushkov@9858 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
apetushkov@9858 4 *
apetushkov@9858 5 * This code is free software; you can redistribute it and/or modify it
apetushkov@9858 6 * under the terms of the GNU General Public License version 2 only, as
apetushkov@9858 7 * published by the Free Software Foundation.
apetushkov@9858 8 *
apetushkov@9858 9 * This code is distributed in the hope that it will be useful, but WITHOUT
apetushkov@9858 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
apetushkov@9858 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
apetushkov@9858 12 * version 2 for more details (a copy is included in the LICENSE file that
apetushkov@9858 13 * accompanied this code).
apetushkov@9858 14 *
apetushkov@9858 15 * You should have received a copy of the GNU General Public License version
apetushkov@9858 16 * 2 along with this work; if not, write to the Free Software Foundation,
apetushkov@9858 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
apetushkov@9858 18 *
apetushkov@9858 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
apetushkov@9858 20 * or visit www.oracle.com if you need additional information or have any
apetushkov@9858 21 * questions.
apetushkov@9858 22 *
apetushkov@9858 23 */
apetushkov@9858 24
apetushkov@9858 25 #include "precompiled.hpp"
apetushkov@9858 26 #include "jni.h"
apetushkov@9858 27 #include "classfile/symbolTable.hpp"
apetushkov@9858 28 #include "classfile/systemDictionary.hpp"
apetushkov@9858 29 #include "classfile/vmSymbols.hpp"
apetushkov@9858 30 #include "jfr/jni/jfrJavaSupport.hpp"
apetushkov@9858 31 #include "jfr/recorder/storage/jfrStorage.hpp"
apetushkov@9858 32 #include "jfr/support/jfrThreadId.hpp"
apetushkov@9858 33 #include "jfr/utilities/jfrTypes.hpp"
apetushkov@9858 34 #include "jfr/writers/jfrJavaEventWriter.hpp"
apetushkov@9858 35 #include "oops/instanceKlass.hpp"
apetushkov@9858 36 #include "oops/oop.inline.hpp"
apetushkov@9858 37 #include "runtime/fieldDescriptor.hpp"
apetushkov@9858 38 #include "runtime/handles.hpp"
apetushkov@9858 39 #include "runtime/jniHandles.hpp"
apetushkov@9858 40 #include "runtime/thread.inline.hpp"
apetushkov@9858 41
apetushkov@9858 42 static int start_pos_offset = invalid_offset;
apetushkov@9858 43 static int start_pos_address_offset = invalid_offset;
apetushkov@9858 44 static int current_pos_offset = invalid_offset;
apetushkov@9858 45 static int max_pos_offset = invalid_offset;
apetushkov@9858 46 static int max_event_size_offset = invalid_offset;
apetushkov@9858 47 static int notified_offset = invalid_offset;
apetushkov@9858 48 static int thread_id_offset = invalid_offset;
apetushkov@9858 49 static int valid_offset = invalid_offset;
apetushkov@9858 50
apetushkov@9858 51 static bool find_field(InstanceKlass* ik,
apetushkov@9858 52 Symbol* name_symbol,
apetushkov@9858 53 Symbol* signature_symbol,
apetushkov@9858 54 fieldDescriptor* fd,
apetushkov@9858 55 bool is_static = false,
apetushkov@9858 56 bool allow_super = false) {
apetushkov@9858 57 if (allow_super || is_static) {
apetushkov@9858 58 return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL;
apetushkov@9858 59 } else {
apetushkov@9858 60 return ik->find_local_field(name_symbol, signature_symbol, fd);
apetushkov@9858 61 }
apetushkov@9858 62 }
apetushkov@9858 63
apetushkov@9858 64 static void compute_offset(int &dest_offset,
apetushkov@9858 65 Klass* klass,
apetushkov@9858 66 Symbol* name_symbol,
apetushkov@9858 67 Symbol* signature_symbol,
apetushkov@9858 68 bool is_static = false, bool allow_super = false) {
apetushkov@9858 69 fieldDescriptor fd;
apetushkov@9858 70 InstanceKlass* ik = InstanceKlass::cast(klass);
apetushkov@9858 71 if (!find_field(ik, name_symbol, signature_symbol, &fd, is_static, allow_super)) {
apetushkov@9858 72 assert(false, "invariant");
apetushkov@9858 73 }
apetushkov@9858 74 dest_offset = fd.offset();
apetushkov@9858 75 }
apetushkov@9858 76
apetushkov@9858 77 static bool setup_event_writer_offsets(TRAPS) {
apetushkov@9858 78 const char class_name[] = "jdk/jfr/internal/EventWriter";
apetushkov@9858 79 Symbol* const k_sym = SymbolTable::lookup(class_name, sizeof class_name - 1, CHECK_false);
apetushkov@9858 80 assert(k_sym != NULL, "invariant");
apetushkov@9858 81 Klass* klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);
apetushkov@9858 82 assert(klass != NULL, "invariant");
apetushkov@9858 83
apetushkov@9858 84 const char start_pos_name[] = "startPosition";
apetushkov@9858 85 Symbol* const start_pos_sym = SymbolTable::lookup(start_pos_name, sizeof start_pos_name - 1, CHECK_false);
apetushkov@9858 86 assert(start_pos_sym != NULL, "invariant");
apetushkov@9858 87 assert(invalid_offset == start_pos_offset, "invariant");
apetushkov@9858 88 compute_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());
apetushkov@9858 89 assert(start_pos_offset != invalid_offset, "invariant");
apetushkov@9858 90
apetushkov@9858 91 const char start_pos_address_name[] = "startPositionAddress";
apetushkov@9858 92 Symbol* const start_pos_address_sym = SymbolTable::lookup(start_pos_address_name, sizeof start_pos_address_name - 1, CHECK_false);
apetushkov@9858 93 assert(start_pos_address_sym != NULL, "invariant");
apetushkov@9858 94 assert(invalid_offset == start_pos_address_offset, "invariant");
apetushkov@9858 95 compute_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());
apetushkov@9858 96 assert(start_pos_address_offset != invalid_offset, "invariant");
apetushkov@9858 97
apetushkov@9858 98 const char event_pos_name[] = "currentPosition";
apetushkov@9858 99 Symbol* const event_pos_sym = SymbolTable::lookup(event_pos_name, sizeof event_pos_name - 1, CHECK_false);
apetushkov@9858 100 assert(event_pos_sym != NULL, "invariant");
apetushkov@9858 101 assert(invalid_offset == current_pos_offset, "invariant");
apetushkov@9858 102 compute_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());
apetushkov@9858 103 assert(current_pos_offset != invalid_offset, "invariant");
apetushkov@9858 104
apetushkov@9858 105 const char max_pos_name[] = "maxPosition";
apetushkov@9858 106 Symbol* const max_pos_sym = SymbolTable::lookup(max_pos_name, sizeof max_pos_name - 1, CHECK_false);
apetushkov@9858 107 assert(max_pos_sym != NULL, "invariant");
apetushkov@9858 108 assert(invalid_offset == max_pos_offset, "invariant");
apetushkov@9858 109 compute_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());
apetushkov@9858 110 assert(max_pos_offset != invalid_offset, "invariant");
apetushkov@9858 111
apetushkov@9858 112 const char max_event_size_name[] = "maxEventSize";
apetushkov@9858 113 Symbol* const max_event_size_sym = SymbolTable::lookup(max_event_size_name, sizeof max_event_size_name - 1, CHECK_false);
apetushkov@9858 114 assert (max_event_size_sym != NULL, "invariant");
apetushkov@9858 115 assert(invalid_offset == max_event_size_offset, "invariant");
apetushkov@9858 116 compute_offset(max_event_size_offset, klass, max_event_size_sym, vmSymbols::int_signature());
apetushkov@9858 117 assert(max_event_size_offset != invalid_offset, "invariant");
apetushkov@9858 118
apetushkov@9858 119 const char notified_name[] = "notified";
apetushkov@9858 120 Symbol* const notified_sym = SymbolTable::lookup(notified_name, sizeof notified_name - 1, CHECK_false);
apetushkov@9858 121 assert (notified_sym != NULL, "invariant");
apetushkov@9858 122 assert(invalid_offset == notified_offset, "invariant");
apetushkov@9858 123 compute_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());
apetushkov@9858 124 assert(notified_offset != invalid_offset, "invariant");
apetushkov@9858 125
apetushkov@9858 126 const char valid_name[] = "valid";
apetushkov@9858 127 Symbol* const valid_sym = SymbolTable::lookup(valid_name, sizeof valid_name - 1, CHECK_false);
apetushkov@9858 128 assert (valid_sym != NULL, "invariant");
apetushkov@9858 129 assert(invalid_offset == valid_offset, "invariant");
apetushkov@9858 130 compute_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());
apetushkov@9858 131 assert(valid_offset != invalid_offset, "invariant");
apetushkov@9858 132 return true;
apetushkov@9858 133 }
apetushkov@9858 134
apetushkov@9858 135 bool JfrJavaEventWriter::has_required_classes(TRAPS) {
apetushkov@9858 136 const char class_name[] = "jdk/jfr/internal/EventWriter";
apetushkov@9858 137 Symbol* const k_sym = SymbolTable::lookup(class_name, sizeof class_name - 1, CHECK_false);
apetushkov@9858 138 Klass* klass = SystemDictionary::resolve_or_null(k_sym, CHECK_false);
apetushkov@9858 139 return (klass != NULL);
apetushkov@9858 140 }
apetushkov@9858 141
apetushkov@9858 142 bool JfrJavaEventWriter::initialize() {
apetushkov@9858 143 static bool initialized = false;
apetushkov@9858 144 if (!initialized) {
apetushkov@9858 145 Thread* thread = Thread::current();
apetushkov@9858 146 initialized = setup_event_writer_offsets(thread);
apetushkov@9858 147 }
apetushkov@9858 148 return initialized;
apetushkov@9858 149 }
apetushkov@9858 150
apetushkov@9858 151 jboolean JfrJavaEventWriter::flush(jobject writer, jint used, jint requested, JavaThread* jt) {
apetushkov@9858 152 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));
apetushkov@9858 153 assert(writer != NULL, "invariant");
apetushkov@9858 154 oop const w = JNIHandles::resolve_non_null(writer);
apetushkov@9858 155 assert(w != NULL, "invariant");
apetushkov@9858 156 JfrBuffer* const current = jt->jfr_thread_local()->java_buffer();
apetushkov@9858 157 assert(current != NULL, "invariant");
apetushkov@9858 158 JfrBuffer* const buffer = JfrStorage::flush(current, used, requested, false, jt);
apetushkov@9858 159 assert(buffer != NULL, "invariant");
apetushkov@9858 160 // "validity" is contextually defined here to mean
apetushkov@9858 161 // that some memory location was provided that is
apetushkov@9858 162 // large enough to accommodate the "requested size".
apetushkov@9858 163 const bool is_valid = buffer->free_size() >= (size_t)(used + requested);
apetushkov@9858 164 u1* const new_current_position = is_valid ? buffer->pos() + used : buffer->pos();
apetushkov@9858 165 w->long_field_put(start_pos_offset, (jlong)buffer->pos());
apetushkov@9858 166 w->long_field_put(current_pos_offset, (jlong)new_current_position);
apetushkov@9858 167 // only update java writer if underlying memory changed
apetushkov@9858 168 if (buffer != current) {
apetushkov@9858 169 w->long_field_put(start_pos_address_offset, (jlong)buffer->pos_address());
apetushkov@9858 170 w->long_field_put(max_pos_offset, (jlong)buffer->end());
apetushkov@9858 171 }
apetushkov@9858 172 if (!is_valid) {
apetushkov@9858 173 // mark writer as invalid for this write attempt
apetushkov@9858 174 w->release_bool_field_put(valid_offset, JNI_FALSE);
apetushkov@9858 175 return JNI_FALSE;
apetushkov@9858 176 }
apetushkov@9858 177 // An exclusive use of a leased buffer is treated equivalent to
apetushkov@9858 178 // holding a system resource. As such, it should be released as soon as possible.
apetushkov@9858 179 // Returning true here signals that the thread will need to call flush again
apetushkov@9858 180 // on EventWriter.endEvent() and that flush will return the lease.
apetushkov@9858 181 return buffer->lease() ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 182 }
apetushkov@9858 183
apetushkov@9858 184 class JfrJavaEventWriterNotificationClosure : public ThreadClosure {
apetushkov@9858 185 public:
apetushkov@9858 186 void do_thread(Thread* t) {
apetushkov@9858 187 if (t->is_Java_thread()) {
apetushkov@9858 188 JfrJavaEventWriter::notify((JavaThread*)t);
apetushkov@9858 189 }
apetushkov@9858 190 }
apetushkov@9858 191 };
apetushkov@9858 192
apetushkov@9858 193 void JfrJavaEventWriter::notify() {
apetushkov@9858 194 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
apetushkov@9858 195 JfrJavaEventWriterNotificationClosure closure;
apetushkov@9858 196 Threads::threads_do(&closure);
apetushkov@9858 197 }
apetushkov@9858 198
apetushkov@9858 199 void JfrJavaEventWriter::notify(JavaThread* jt) {
apetushkov@9858 200 assert(jt != NULL, "invariant");
apetushkov@9858 201 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
apetushkov@9858 202 if (jt->jfr_thread_local()->has_java_event_writer()) {
apetushkov@9858 203 oop buffer_writer = JNIHandles::resolve_non_null(jt->jfr_thread_local()->java_event_writer());
apetushkov@9858 204 assert(buffer_writer != NULL, "invariant");
apetushkov@9858 205 buffer_writer->release_bool_field_put(notified_offset, JNI_TRUE);
apetushkov@9858 206 }
apetushkov@9858 207 }
apetushkov@9858 208
apetushkov@9858 209 static jobject create_new_event_writer(JfrBuffer* buffer, TRAPS) {
apetushkov@9858 210 assert(buffer != NULL, "invariant");
apetushkov@9858 211 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
apetushkov@9858 212 HandleMark hm(THREAD);
apetushkov@9858 213 static const char klass[] = "jdk/jfr/internal/EventWriter";
apetushkov@9858 214 static const char method[] = "<init>";
apetushkov@9858 215 static const char signature[] = "(JJJJZ)V";
apetushkov@9858 216 JavaValue result(T_OBJECT);
apetushkov@9858 217 JfrJavaArguments args(&result, klass, method, signature, CHECK_NULL);
apetushkov@9858 218 // parameters
apetushkov@9858 219 args.push_long((jlong)buffer->pos());
apetushkov@9858 220 args.push_long((jlong)buffer->end());
apetushkov@9858 221 args.push_long((jlong)buffer->pos_address());
apetushkov@9858 222 args.push_long((jlong)JFR_THREAD_ID(THREAD));
apetushkov@9858 223 args.push_int((int)JNI_TRUE);
apetushkov@9858 224 JfrJavaSupport::new_object_global_ref(&args, CHECK_NULL);
apetushkov@9858 225 return result.get_jobject();
apetushkov@9858 226 }
apetushkov@9858 227
apetushkov@9858 228 jobject JfrJavaEventWriter::event_writer(Thread* t) {
apetushkov@9858 229 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));
apetushkov@9858 230 JfrThreadLocal* const tl = t->jfr_thread_local();
apetushkov@9858 231 assert(tl->shelved_buffer() == NULL, "invariant");
apetushkov@9858 232 return tl->java_event_writer();
apetushkov@9858 233 }
apetushkov@9858 234
apetushkov@9858 235 jobject JfrJavaEventWriter::new_event_writer(TRAPS) {
apetushkov@9858 236 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
apetushkov@9858 237 assert(event_writer(THREAD) == NULL, "invariant");
apetushkov@9858 238 JfrThreadLocal* const tl = THREAD->jfr_thread_local();
apetushkov@9858 239 assert(!tl->has_java_buffer(), "invariant");
apetushkov@9858 240 JfrBuffer* const buffer = tl->java_buffer();
apetushkov@9858 241 if (buffer == NULL) {
apetushkov@9858 242 JfrJavaSupport::throw_out_of_memory_error("OOME for thread local buffer", THREAD);
apetushkov@9858 243 return NULL;
apetushkov@9858 244 }
apetushkov@9858 245 jobject java_event_writer = create_new_event_writer(buffer, CHECK_NULL);
apetushkov@9858 246 tl->set_java_event_writer(java_event_writer);
apetushkov@9858 247 assert(tl->has_java_event_writer(), "invariant");
apetushkov@9858 248 return java_event_writer;
apetushkov@9858 249 }

mercurial