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