Mon, 12 Aug 2019 18:30:40 +0300
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
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 */
25 #include "precompiled.hpp"
26 #include "jni.h"
27 #include "classfile/symbolTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "jfr/jni/jfrJavaSupport.hpp"
31 #include "jfr/recorder/storage/jfrStorage.hpp"
32 #include "jfr/support/jfrThreadId.hpp"
33 #include "jfr/utilities/jfrTypes.hpp"
34 #include "jfr/writers/jfrJavaEventWriter.hpp"
35 #include "oops/instanceKlass.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "runtime/fieldDescriptor.hpp"
38 #include "runtime/handles.hpp"
39 #include "runtime/jniHandles.hpp"
40 #include "runtime/thread.inline.hpp"
42 static int start_pos_offset = invalid_offset;
43 static int start_pos_address_offset = invalid_offset;
44 static int current_pos_offset = invalid_offset;
45 static int max_pos_offset = invalid_offset;
46 static int max_event_size_offset = invalid_offset;
47 static int notified_offset = invalid_offset;
48 static int thread_id_offset = invalid_offset;
49 static int valid_offset = invalid_offset;
51 static bool find_field(InstanceKlass* ik,
52 Symbol* name_symbol,
53 Symbol* signature_symbol,
54 fieldDescriptor* fd,
55 bool is_static = false,
56 bool allow_super = false) {
57 if (allow_super || is_static) {
58 return ik->find_field(name_symbol, signature_symbol, is_static, fd) != NULL;
59 } else {
60 return ik->find_local_field(name_symbol, signature_symbol, fd);
61 }
62 }
64 static void compute_offset(int &dest_offset,
65 Klass* klass,
66 Symbol* name_symbol,
67 Symbol* signature_symbol,
68 bool is_static = false, bool allow_super = false) {
69 fieldDescriptor fd;
70 InstanceKlass* ik = InstanceKlass::cast(klass);
71 if (!find_field(ik, name_symbol, signature_symbol, &fd, is_static, allow_super)) {
72 assert(false, "invariant");
73 }
74 dest_offset = fd.offset();
75 }
77 static bool setup_event_writer_offsets(TRAPS) {
78 const char class_name[] = "jdk/jfr/internal/EventWriter";
79 Symbol* const k_sym = SymbolTable::lookup(class_name, sizeof class_name - 1, CHECK_false);
80 assert(k_sym != NULL, "invariant");
81 Klass* klass = SystemDictionary::resolve_or_fail(k_sym, true, CHECK_false);
82 assert(klass != NULL, "invariant");
84 const char start_pos_name[] = "startPosition";
85 Symbol* const start_pos_sym = SymbolTable::lookup(start_pos_name, sizeof start_pos_name - 1, CHECK_false);
86 assert(start_pos_sym != NULL, "invariant");
87 assert(invalid_offset == start_pos_offset, "invariant");
88 compute_offset(start_pos_offset, klass, start_pos_sym, vmSymbols::long_signature());
89 assert(start_pos_offset != invalid_offset, "invariant");
91 const char start_pos_address_name[] = "startPositionAddress";
92 Symbol* const start_pos_address_sym = SymbolTable::lookup(start_pos_address_name, sizeof start_pos_address_name - 1, CHECK_false);
93 assert(start_pos_address_sym != NULL, "invariant");
94 assert(invalid_offset == start_pos_address_offset, "invariant");
95 compute_offset(start_pos_address_offset, klass, start_pos_address_sym, vmSymbols::long_signature());
96 assert(start_pos_address_offset != invalid_offset, "invariant");
98 const char event_pos_name[] = "currentPosition";
99 Symbol* const event_pos_sym = SymbolTable::lookup(event_pos_name, sizeof event_pos_name - 1, CHECK_false);
100 assert(event_pos_sym != NULL, "invariant");
101 assert(invalid_offset == current_pos_offset, "invariant");
102 compute_offset(current_pos_offset, klass, event_pos_sym,vmSymbols::long_signature());
103 assert(current_pos_offset != invalid_offset, "invariant");
105 const char max_pos_name[] = "maxPosition";
106 Symbol* const max_pos_sym = SymbolTable::lookup(max_pos_name, sizeof max_pos_name - 1, CHECK_false);
107 assert(max_pos_sym != NULL, "invariant");
108 assert(invalid_offset == max_pos_offset, "invariant");
109 compute_offset(max_pos_offset, klass, max_pos_sym, vmSymbols::long_signature());
110 assert(max_pos_offset != invalid_offset, "invariant");
112 const char max_event_size_name[] = "maxEventSize";
113 Symbol* const max_event_size_sym = SymbolTable::lookup(max_event_size_name, sizeof max_event_size_name - 1, CHECK_false);
114 assert (max_event_size_sym != NULL, "invariant");
115 assert(invalid_offset == max_event_size_offset, "invariant");
116 compute_offset(max_event_size_offset, klass, max_event_size_sym, vmSymbols::int_signature());
117 assert(max_event_size_offset != invalid_offset, "invariant");
119 const char notified_name[] = "notified";
120 Symbol* const notified_sym = SymbolTable::lookup(notified_name, sizeof notified_name - 1, CHECK_false);
121 assert (notified_sym != NULL, "invariant");
122 assert(invalid_offset == notified_offset, "invariant");
123 compute_offset(notified_offset, klass, notified_sym, vmSymbols::bool_signature());
124 assert(notified_offset != invalid_offset, "invariant");
126 const char valid_name[] = "valid";
127 Symbol* const valid_sym = SymbolTable::lookup(valid_name, sizeof valid_name - 1, CHECK_false);
128 assert (valid_sym != NULL, "invariant");
129 assert(invalid_offset == valid_offset, "invariant");
130 compute_offset(valid_offset, klass, valid_sym, vmSymbols::bool_signature());
131 assert(valid_offset != invalid_offset, "invariant");
132 return true;
133 }
135 bool JfrJavaEventWriter::has_required_classes(TRAPS) {
136 const char class_name[] = "jdk/jfr/internal/EventWriter";
137 Symbol* const k_sym = SymbolTable::lookup(class_name, sizeof class_name - 1, CHECK_false);
138 Klass* klass = SystemDictionary::resolve_or_null(k_sym, CHECK_false);
139 return (klass != NULL);
140 }
142 bool JfrJavaEventWriter::initialize() {
143 static bool initialized = false;
144 if (!initialized) {
145 Thread* thread = Thread::current();
146 initialized = setup_event_writer_offsets(thread);
147 }
148 return initialized;
149 }
151 jboolean JfrJavaEventWriter::flush(jobject writer, jint used, jint requested, JavaThread* jt) {
152 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(jt));
153 assert(writer != NULL, "invariant");
154 oop const w = JNIHandles::resolve_non_null(writer);
155 assert(w != NULL, "invariant");
156 JfrBuffer* const current = jt->jfr_thread_local()->java_buffer();
157 assert(current != NULL, "invariant");
158 JfrBuffer* const buffer = JfrStorage::flush(current, used, requested, false, jt);
159 assert(buffer != NULL, "invariant");
160 // "validity" is contextually defined here to mean
161 // that some memory location was provided that is
162 // large enough to accommodate the "requested size".
163 const bool is_valid = buffer->free_size() >= (size_t)(used + requested);
164 u1* const new_current_position = is_valid ? buffer->pos() + used : buffer->pos();
165 w->long_field_put(start_pos_offset, (jlong)buffer->pos());
166 w->long_field_put(current_pos_offset, (jlong)new_current_position);
167 // only update java writer if underlying memory changed
168 if (buffer != current) {
169 w->long_field_put(start_pos_address_offset, (jlong)buffer->pos_address());
170 w->long_field_put(max_pos_offset, (jlong)buffer->end());
171 }
172 if (!is_valid) {
173 // mark writer as invalid for this write attempt
174 w->release_bool_field_put(valid_offset, JNI_FALSE);
175 return JNI_FALSE;
176 }
177 // An exclusive use of a leased buffer is treated equivalent to
178 // holding a system resource. As such, it should be released as soon as possible.
179 // Returning true here signals that the thread will need to call flush again
180 // on EventWriter.endEvent() and that flush will return the lease.
181 return buffer->lease() ? JNI_TRUE : JNI_FALSE;
182 }
184 class JfrJavaEventWriterNotificationClosure : public ThreadClosure {
185 public:
186 void do_thread(Thread* t) {
187 if (t->is_Java_thread()) {
188 JfrJavaEventWriter::notify((JavaThread*)t);
189 }
190 }
191 };
193 void JfrJavaEventWriter::notify() {
194 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
195 JfrJavaEventWriterNotificationClosure closure;
196 Threads::threads_do(&closure);
197 }
199 void JfrJavaEventWriter::notify(JavaThread* jt) {
200 assert(jt != NULL, "invariant");
201 assert(SafepointSynchronize::is_at_safepoint(), "invariant");
202 if (jt->jfr_thread_local()->has_java_event_writer()) {
203 oop buffer_writer = JNIHandles::resolve_non_null(jt->jfr_thread_local()->java_event_writer());
204 assert(buffer_writer != NULL, "invariant");
205 buffer_writer->release_bool_field_put(notified_offset, JNI_TRUE);
206 }
207 }
209 static jobject create_new_event_writer(JfrBuffer* buffer, TRAPS) {
210 assert(buffer != NULL, "invariant");
211 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
212 HandleMark hm(THREAD);
213 static const char klass[] = "jdk/jfr/internal/EventWriter";
214 static const char method[] = "<init>";
215 static const char signature[] = "(JJJJZ)V";
216 JavaValue result(T_OBJECT);
217 JfrJavaArguments args(&result, klass, method, signature, CHECK_NULL);
218 // parameters
219 args.push_long((jlong)buffer->pos());
220 args.push_long((jlong)buffer->end());
221 args.push_long((jlong)buffer->pos_address());
222 args.push_long((jlong)JFR_THREAD_ID(THREAD));
223 args.push_int((int)JNI_TRUE);
224 JfrJavaSupport::new_object_global_ref(&args, CHECK_NULL);
225 return result.get_jobject();
226 }
228 jobject JfrJavaEventWriter::event_writer(Thread* t) {
229 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(t));
230 JfrThreadLocal* const tl = t->jfr_thread_local();
231 assert(tl->shelved_buffer() == NULL, "invariant");
232 return tl->java_event_writer();
233 }
235 jobject JfrJavaEventWriter::new_event_writer(TRAPS) {
236 DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD));
237 assert(event_writer(THREAD) == NULL, "invariant");
238 JfrThreadLocal* const tl = THREAD->jfr_thread_local();
239 assert(!tl->has_java_buffer(), "invariant");
240 JfrBuffer* const buffer = tl->java_buffer();
241 if (buffer == NULL) {
242 JfrJavaSupport::throw_out_of_memory_error("OOME for thread local buffer", THREAD);
243 return NULL;
244 }
245 jobject java_event_writer = create_new_event_writer(buffer, CHECK_NULL);
246 tl->set_java_event_writer(java_event_writer);
247 assert(tl->has_java_event_writer(), "invariant");
248 return java_event_writer;
249 }