src/share/vm/jfr/jni/jfrJniMethod.cpp

Fri, 12 Jun 2020 02:59:56 +0100

author
jbachorik
date
Fri, 12 Jun 2020 02:59:56 +0100
changeset 9925
30fb8c8cceb9
parent 9875
6388d0d497f7
child 9986
85e682d8ab91
permissions
-rw-r--r--

8233197: Invert JvmtiExport::post_vm_initialized() and Jfr:on_vm_start() start-up order for correct option parsing
8246703: [TESTBUG] Add test for JDK-8233197
Reviewed-by: aph, adinn, neugens

apetushkov@9858 1 /*
apetushkov@9858 2 * Copyright (c) 2014, 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 "jvm.h"
apetushkov@9858 28 #include "jfr/jfr.hpp"
apetushkov@9858 29 #include "jfr/jfrEvents.hpp"
apetushkov@9858 30 #include "jfr/periodic/sampling/jfrThreadSampler.hpp"
apetushkov@9858 31 #include "jfr/recorder/jfrEventSetting.hpp"
apetushkov@9858 32 #include "jfr/recorder/jfrRecorder.hpp"
apetushkov@9858 33 #include "jfr/recorder/checkpoint/jfrMetadataEvent.hpp"
apetushkov@9858 34 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
apetushkov@9858 35 #include "jfr/recorder/repository/jfrRepository.hpp"
mgronlun@9875 36 #include "jfr/recorder/repository/jfrChunkRotation.hpp"
apetushkov@9858 37 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
apetushkov@9858 38 #include "jfr/recorder/service/jfrOptionSet.hpp"
apetushkov@9858 39 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
apetushkov@9858 40 #include "jfr/recorder/stringpool/jfrStringPool.hpp"
apetushkov@9858 41 #include "jfr/jni/jfrGetAllEventClasses.hpp"
apetushkov@9858 42 #include "jfr/jni/jfrJavaSupport.hpp"
apetushkov@9858 43 #include "jfr/jni/jfrJniMethodRegistration.hpp"
apetushkov@9858 44 #include "jfr/instrumentation/jfrEventClassTransformer.hpp"
apetushkov@9858 45 #include "jfr/instrumentation/jfrJvmtiAgent.hpp"
apetushkov@9858 46 #include "jfr/leakprofiler/leakProfiler.hpp"
apetushkov@9858 47 #include "jfr/utilities/jfrJavaLog.hpp"
apetushkov@9858 48 #include "jfr/utilities/jfrTimeConverter.hpp"
apetushkov@9858 49 #include "jfr/utilities/jfrTime.hpp"
apetushkov@9858 50 #include "jfr/writers/jfrJavaEventWriter.hpp"
apetushkov@9858 51 #include "jfrfiles/jfrPeriodic.hpp"
apetushkov@9858 52 #include "memory/resourceArea.hpp"
apetushkov@9858 53 #include "runtime/interfaceSupport.hpp"
apetushkov@9858 54 #include "runtime/mutexLocker.hpp"
apetushkov@9858 55 #include "runtime/os.hpp"
apetushkov@9858 56 #include "runtime/thread.hpp"
apetushkov@9858 57 #include "utilities/debug.hpp"
apetushkov@9858 58
apetushkov@9858 59 #define NO_TRANSITION(result_type, header) extern "C" { result_type JNICALL header {
apetushkov@9858 60 #define NO_TRANSITION_END } }
apetushkov@9858 61
apetushkov@9858 62 /*
apetushkov@9858 63 * NO_TRANSITION entries
apetushkov@9858 64 *
apetushkov@9858 65 * Thread remains _thread_in_native
apetushkov@9858 66 */
apetushkov@9858 67
apetushkov@9858 68 NO_TRANSITION(void, jfr_register_natives(JNIEnv* env, jclass jvmclass))
apetushkov@9858 69 JfrJniMethodRegistration register_native_methods(env);
apetushkov@9858 70 NO_TRANSITION_END
apetushkov@9858 71
apetushkov@9858 72 NO_TRANSITION(jboolean, jfr_is_enabled())
apetushkov@9858 73 return Jfr::is_enabled() ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 74 NO_TRANSITION_END
apetushkov@9858 75
apetushkov@9858 76 NO_TRANSITION(jboolean, jfr_is_disabled())
apetushkov@9858 77 return Jfr::is_disabled() ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 78 NO_TRANSITION_END
apetushkov@9858 79
apetushkov@9858 80 NO_TRANSITION(jboolean, jfr_is_started())
apetushkov@9858 81 return JfrRecorder::is_created() ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 82 NO_TRANSITION_END
apetushkov@9858 83
apetushkov@9858 84 NO_TRANSITION(jstring, jfr_get_pid(JNIEnv* env, jobject jvm))
apetushkov@9858 85 char pid_buf[32] = { 0 };
apetushkov@9858 86 jio_snprintf(pid_buf, sizeof(pid_buf), "%d", os::current_process_id());
apetushkov@9858 87 jstring pid_string = env->NewStringUTF(pid_buf);
apetushkov@9858 88 return pid_string; // exception pending if NULL
apetushkov@9858 89 NO_TRANSITION_END
apetushkov@9858 90
apetushkov@9858 91 NO_TRANSITION(jlong, jfr_elapsed_frequency(JNIEnv* env, jobject jvm))
apetushkov@9858 92 return JfrTime::frequency();
apetushkov@9858 93 NO_TRANSITION_END
apetushkov@9858 94
apetushkov@9858 95 NO_TRANSITION(jlong, jfr_elapsed_counter(JNIEnv* env, jobject jvm))
apetushkov@9858 96 return JfrTicks::now();
apetushkov@9858 97 NO_TRANSITION_END
apetushkov@9858 98
apetushkov@9858 99 NO_TRANSITION(void, jfr_retransform_classes(JNIEnv* env, jobject jvm, jobjectArray classes))
apetushkov@9858 100 JfrJvmtiAgent::retransform_classes(env, classes, JavaThread::thread_from_jni_environment(env));
apetushkov@9858 101 NO_TRANSITION_END
apetushkov@9858 102
apetushkov@9858 103 NO_TRANSITION(void, jfr_set_enabled(JNIEnv* env, jobject jvm, jlong event_type_id, jboolean enabled))
apetushkov@9858 104 JfrEventSetting::set_enabled(event_type_id, JNI_TRUE == enabled);
apetushkov@9858 105 if (EventOldObjectSample::eventId == event_type_id) {
apetushkov@9858 106 ThreadInVMfromNative transition(JavaThread::thread_from_jni_environment(env));
apetushkov@9858 107 if (JNI_TRUE == enabled) {
apetushkov@9858 108 LeakProfiler::start(JfrOptionSet::old_object_queue_size());
apetushkov@9858 109 } else {
apetushkov@9858 110 LeakProfiler::stop();
apetushkov@9858 111 }
apetushkov@9858 112 }
apetushkov@9858 113 NO_TRANSITION_END
apetushkov@9858 114
apetushkov@9858 115 NO_TRANSITION(void, jfr_set_file_notification(JNIEnv* env, jobject jvm, jlong threshold))
mgronlun@9875 116 JfrChunkRotation::set_threshold((intptr_t)threshold);
apetushkov@9858 117 NO_TRANSITION_END
apetushkov@9858 118
apetushkov@9858 119 NO_TRANSITION(void, jfr_set_sample_threads(JNIEnv* env, jobject jvm, jboolean sampleThreads))
apetushkov@9858 120 JfrOptionSet::set_sample_threads(sampleThreads);
apetushkov@9858 121 NO_TRANSITION_END
apetushkov@9858 122
apetushkov@9858 123 NO_TRANSITION(void, jfr_set_stack_depth(JNIEnv* env, jobject jvm, jint depth))
apetushkov@9858 124 JfrOptionSet::set_stackdepth((jlong)depth);
apetushkov@9858 125 NO_TRANSITION_END
apetushkov@9858 126
apetushkov@9858 127 NO_TRANSITION(void, jfr_set_stacktrace_enabled(JNIEnv* env, jobject jvm, jlong event_type_id, jboolean enabled))
apetushkov@9858 128 JfrEventSetting::set_stacktrace(event_type_id, JNI_TRUE == enabled);
apetushkov@9858 129 NO_TRANSITION_END
apetushkov@9858 130
apetushkov@9858 131 NO_TRANSITION(void, jfr_set_global_buffer_count(JNIEnv* env, jobject jvm, jlong count))
apetushkov@9858 132 JfrOptionSet::set_num_global_buffers(count);
apetushkov@9858 133 NO_TRANSITION_END
apetushkov@9858 134
apetushkov@9858 135 NO_TRANSITION(void, jfr_set_global_buffer_size(JNIEnv* env, jobject jvm, jlong size))
apetushkov@9858 136 JfrOptionSet::set_global_buffer_size(size);
apetushkov@9858 137 NO_TRANSITION_END
apetushkov@9858 138
apetushkov@9858 139 NO_TRANSITION(void, jfr_set_thread_buffer_size(JNIEnv* env, jobject jvm, jlong size))
apetushkov@9858 140 JfrOptionSet::set_thread_buffer_size(size);
apetushkov@9858 141 NO_TRANSITION_END
apetushkov@9858 142
apetushkov@9858 143 NO_TRANSITION(void, jfr_set_memory_size(JNIEnv* env, jobject jvm, jlong size))
apetushkov@9858 144 JfrOptionSet::set_memory_size(size);
apetushkov@9858 145 NO_TRANSITION_END
apetushkov@9858 146
apetushkov@9858 147 NO_TRANSITION(jboolean, jfr_set_threshold(JNIEnv* env, jobject jvm, jlong event_type_id, jlong thresholdTicks))
apetushkov@9858 148 return JfrEventSetting::set_threshold(event_type_id, thresholdTicks) ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 149 NO_TRANSITION_END
apetushkov@9858 150
apetushkov@9858 151 NO_TRANSITION(jboolean, jfr_allow_event_retransforms(JNIEnv* env, jobject jvm))
apetushkov@9858 152 return JfrOptionSet::allow_event_retransforms() ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 153 NO_TRANSITION_END
apetushkov@9858 154
apetushkov@9858 155 NO_TRANSITION(jboolean, jfr_is_available(JNIEnv* env, jclass jvm))
apetushkov@9858 156 return !Jfr::is_disabled() ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 157 NO_TRANSITION_END
apetushkov@9858 158
apetushkov@9858 159 NO_TRANSITION(jlong, jfr_get_epoch_address(JNIEnv* env, jobject jvm))
apetushkov@9858 160 return JfrTraceIdEpoch::epoch_address();
apetushkov@9858 161 NO_TRANSITION_END
apetushkov@9858 162
apetushkov@9858 163 NO_TRANSITION(jlong, jfr_get_unloaded_event_classes_count(JNIEnv* env, jobject jvm))
apetushkov@9858 164 return JfrEventClasses::unloaded_event_classes_count();
apetushkov@9858 165 NO_TRANSITION_END
apetushkov@9858 166
apetushkov@9858 167 NO_TRANSITION(jdouble, jfr_time_conv_factor(JNIEnv* env, jobject jvm))
apetushkov@9858 168 return (jdouble)JfrTimeConverter::nano_to_counter_multiplier();
apetushkov@9858 169 NO_TRANSITION_END
apetushkov@9858 170
apetushkov@9858 171 NO_TRANSITION(jboolean, jfr_set_cutoff(JNIEnv* env, jobject jvm, jlong event_type_id, jlong cutoff_ticks))
apetushkov@9858 172 return JfrEventSetting::set_cutoff(event_type_id, cutoff_ticks) ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 173 NO_TRANSITION_END
apetushkov@9858 174
mgronlun@9875 175 NO_TRANSITION(jboolean, jfr_should_rotate_disk(JNIEnv* env, jobject jvm))
mgronlun@9875 176 return JfrChunkRotation::should_rotate() ? JNI_TRUE : JNI_FALSE;
mgronlun@9875 177 NO_TRANSITION_END
apetushkov@9858 178
apetushkov@9858 179 /*
apetushkov@9858 180 * JVM_ENTRY_NO_ENV entries
apetushkov@9858 181 *
apetushkov@9858 182 * Transitions:
apetushkov@9858 183 * Entry: _thread_in_native -> _thread_in_vm
apetushkov@9858 184 * Exit: _thread_in_vm -> _thread_in_native
apetushkov@9858 185 *
apetushkov@9858 186 * Current JavaThread available as "thread" variable
apetushkov@9858 187 */
apetushkov@9858 188
apetushkov@9858 189 JVM_ENTRY_NO_ENV(jboolean, jfr_create_jfr(JNIEnv* env, jobject jvm, jboolean simulate_failure))
apetushkov@9858 190 if (JfrRecorder::is_created()) {
apetushkov@9858 191 return JNI_TRUE;
apetushkov@9858 192 }
apetushkov@9858 193 if (!JfrRecorder::create(simulate_failure == JNI_TRUE)) {
jbachorik@9925 194 if (!thread->has_pending_exception()) {
jbachorik@9925 195 JfrJavaSupport::throw_illegal_state_exception("Unable to start Jfr", thread);
jbachorik@9925 196 }
apetushkov@9858 197 return JNI_FALSE;
apetushkov@9858 198 }
apetushkov@9858 199 return JNI_TRUE;
apetushkov@9858 200 JVM_END
apetushkov@9858 201
apetushkov@9858 202 JVM_ENTRY_NO_ENV(jboolean, jfr_destroy_jfr(JNIEnv* env, jobject jvm))
apetushkov@9858 203 JfrRecorder::destroy();
apetushkov@9858 204 return JNI_TRUE;
apetushkov@9858 205 JVM_END
apetushkov@9858 206
apetushkov@9858 207 JVM_ENTRY_NO_ENV(void, jfr_begin_recording(JNIEnv* env, jobject jvm))
apetushkov@9858 208 if (JfrRecorder::is_recording()) {
apetushkov@9858 209 return;
apetushkov@9858 210 }
apetushkov@9858 211 JfrRecorder::start_recording();
apetushkov@9858 212 JVM_END
apetushkov@9858 213
apetushkov@9858 214 JVM_ENTRY_NO_ENV(void, jfr_end_recording(JNIEnv* env, jobject jvm))
apetushkov@9858 215 if (!JfrRecorder::is_recording()) {
apetushkov@9858 216 return;
apetushkov@9858 217 }
apetushkov@9858 218 JfrRecorder::stop_recording();
apetushkov@9858 219 JVM_END
apetushkov@9858 220
apetushkov@9858 221
apetushkov@9858 222 JVM_ENTRY_NO_ENV(jboolean, jfr_emit_event(JNIEnv* env, jobject jvm, jlong eventTypeId, jlong timeStamp, jlong when))
apetushkov@9858 223 JfrPeriodicEventSet::requestEvent((JfrEventId)eventTypeId);
apetushkov@9858 224 return thread->has_pending_exception() ? JNI_FALSE : JNI_TRUE;
apetushkov@9858 225 JVM_END
apetushkov@9858 226
apetushkov@9858 227 JVM_ENTRY_NO_ENV(jobject, jfr_get_all_event_classes(JNIEnv* env, jobject jvm))
apetushkov@9858 228 return JfrEventClasses::get_all_event_classes(thread);
apetushkov@9858 229 JVM_END
apetushkov@9858 230
apetushkov@9858 231 JVM_ENTRY_NO_ENV(jlong, jfr_class_id(JNIEnv* env, jclass jvm, jclass jc))
apetushkov@9858 232 return JfrTraceId::use(jc);
apetushkov@9858 233 JVM_END
apetushkov@9858 234
apetushkov@9858 235 JVM_ENTRY_NO_ENV(jlong, jfr_stacktrace_id(JNIEnv* env, jobject jvm, jint skip))
apetushkov@9858 236 return JfrStackTraceRepository::record(thread, skip);
apetushkov@9858 237 JVM_END
apetushkov@9858 238
apetushkov@9858 239 JVM_ENTRY_NO_ENV(void, jfr_log(JNIEnv* env, jobject jvm, jint tag_set, jint level, jstring message))
apetushkov@9858 240 JfrJavaLog::log(tag_set, level, message, thread);
apetushkov@9858 241 JVM_END
apetushkov@9858 242
apetushkov@9858 243 JVM_ENTRY_NO_ENV(void, jfr_subscribe_log_level(JNIEnv* env, jobject jvm, jobject log_tag, jint id))
apetushkov@9858 244 JfrJavaLog::subscribe_log_level(log_tag, id, thread);
apetushkov@9858 245 JVM_END
apetushkov@9858 246
apetushkov@9858 247 JVM_ENTRY_NO_ENV(void, jfr_set_output(JNIEnv* env, jobject jvm, jstring path))
apetushkov@9858 248 JfrRepository::set_chunk_path(path, thread);
apetushkov@9858 249 JVM_END
apetushkov@9858 250
apetushkov@9858 251 JVM_ENTRY_NO_ENV(void, jfr_set_method_sampling_interval(JNIEnv* env, jobject jvm, jlong type, jlong intervalMillis))
apetushkov@9858 252 if (intervalMillis < 0) {
apetushkov@9858 253 intervalMillis = 0;
apetushkov@9858 254 }
apetushkov@9858 255 JfrEventId typed_event_id = (JfrEventId)type;
apetushkov@9858 256 assert(EventExecutionSample::eventId == typed_event_id || EventNativeMethodSample::eventId == typed_event_id, "invariant");
apetushkov@9858 257 if (intervalMillis > 0) {
apetushkov@9858 258 JfrEventSetting::set_enabled(typed_event_id, true); // ensure sampling event is enabled
apetushkov@9858 259 }
apetushkov@9858 260 if (EventExecutionSample::eventId == type) {
apetushkov@9858 261 JfrThreadSampling::set_java_sample_interval(intervalMillis);
apetushkov@9858 262 } else {
apetushkov@9858 263 JfrThreadSampling::set_native_sample_interval(intervalMillis);
apetushkov@9858 264 }
apetushkov@9858 265 JVM_END
apetushkov@9858 266
apetushkov@9858 267 JVM_ENTRY_NO_ENV(void, jfr_store_metadata_descriptor(JNIEnv* env, jobject jvm, jbyteArray descriptor))
apetushkov@9858 268 JfrMetadataEvent::update(descriptor);
apetushkov@9858 269 JVM_END
apetushkov@9858 270
apetushkov@9858 271 // trace thread id for a thread object
apetushkov@9858 272 JVM_ENTRY_NO_ENV(jlong, jfr_id_for_thread(JNIEnv* env, jobject jvm, jobject t))
apetushkov@9858 273 return JfrJavaSupport::jfr_thread_id(t);
apetushkov@9858 274 JVM_END
apetushkov@9858 275
apetushkov@9858 276 JVM_ENTRY_NO_ENV(jobject, jfr_get_event_writer(JNIEnv* env, jclass cls))
apetushkov@9858 277 return JfrJavaEventWriter::event_writer(thread);
apetushkov@9858 278 JVM_END
apetushkov@9858 279
apetushkov@9858 280 JVM_ENTRY_NO_ENV(jobject, jfr_new_event_writer(JNIEnv* env, jclass cls))
apetushkov@9858 281 return JfrJavaEventWriter::new_event_writer(thread);
apetushkov@9858 282 JVM_END
apetushkov@9858 283
apetushkov@9858 284 JVM_ENTRY_NO_ENV(jboolean, jfr_event_writer_flush(JNIEnv* env, jclass cls, jobject writer, jint used_size, jint requested_size))
apetushkov@9858 285 return JfrJavaEventWriter::flush(writer, used_size, requested_size, thread);
apetushkov@9858 286 JVM_END
apetushkov@9858 287
apetushkov@9858 288 JVM_ENTRY_NO_ENV(void, jfr_set_repository_location(JNIEnv* env, jobject repo, jstring location))
apetushkov@9858 289 return JfrRepository::set_path(location, thread);
apetushkov@9858 290 JVM_END
apetushkov@9858 291
apetushkov@9858 292 JVM_ENTRY_NO_ENV(void, jfr_uncaught_exception(JNIEnv* env, jobject jvm, jobject t, jthrowable throwable))
apetushkov@9858 293 JfrJavaSupport::uncaught_exception(throwable, thread);
apetushkov@9858 294 JVM_END
apetushkov@9858 295
apetushkov@9858 296 JVM_ENTRY_NO_ENV(void, jfr_abort(JNIEnv* env, jobject jvm, jstring errorMsg))
apetushkov@9858 297 JfrJavaSupport::abort(errorMsg, thread);
apetushkov@9858 298 JVM_END
apetushkov@9858 299
apetushkov@9858 300 JVM_ENTRY_NO_ENV(jlong, jfr_type_id(JNIEnv* env, jobject jvm, jclass jc))
apetushkov@9858 301 return JfrTraceId::get(jc);
apetushkov@9858 302 JVM_END
apetushkov@9858 303
apetushkov@9858 304 JVM_ENTRY_NO_ENV(jboolean, jfr_add_string_constant(JNIEnv* env, jclass jvm, jboolean epoch, jlong id, jstring string))
apetushkov@9858 305 return JfrStringPool::add(epoch == JNI_TRUE, id, string, thread) ? JNI_TRUE : JNI_FALSE;
apetushkov@9858 306 JVM_END
apetushkov@9858 307
apetushkov@9858 308 JVM_ENTRY_NO_ENV(void, jfr_set_force_instrumentation(JNIEnv* env, jobject jvm, jboolean force_instrumentation))
apetushkov@9858 309 JfrEventClassTransformer::set_force_instrumentation(force_instrumentation == JNI_TRUE);
apetushkov@9858 310 JVM_END
apetushkov@9858 311
apetushkov@9858 312 JVM_ENTRY_NO_ENV(void, jfr_emit_old_object_samples(JNIEnv* env, jobject jvm, jlong cutoff_ticks, jboolean emit_all))
apetushkov@9858 313 LeakProfiler::emit_events(cutoff_ticks, emit_all == JNI_TRUE);
apetushkov@9858 314 JVM_END

mercurial