apetushkov@9858: /* redestad@9879: * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. apetushkov@9858: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. apetushkov@9858: * apetushkov@9858: * This code is free software; you can redistribute it and/or modify it apetushkov@9858: * under the terms of the GNU General Public License version 2 only, as apetushkov@9858: * published by the Free Software Foundation. apetushkov@9858: * apetushkov@9858: * This code is distributed in the hope that it will be useful, but WITHOUT apetushkov@9858: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or apetushkov@9858: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License apetushkov@9858: * version 2 for more details (a copy is included in the LICENSE file that apetushkov@9858: * accompanied this code). apetushkov@9858: * apetushkov@9858: * You should have received a copy of the GNU General Public License version apetushkov@9858: * 2 along with this work; if not, write to the Free Software Foundation, apetushkov@9858: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. apetushkov@9858: * apetushkov@9858: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA apetushkov@9858: * or visit www.oracle.com if you need additional information or have any apetushkov@9858: * questions. apetushkov@9858: * apetushkov@9858: */ apetushkov@9858: apetushkov@9858: #include "precompiled.hpp" apetushkov@9858: #include "jfr/dcmd/jfrDcmds.hpp" apetushkov@9858: #include "jfr/instrumentation/jfrJvmtiAgent.hpp" apetushkov@9858: #include "jfr/jni/jfrJavaSupport.hpp" apetushkov@9858: #include "jfr/periodic/jfrOSInterface.hpp" apetushkov@9858: #include "jfr/periodic/sampling/jfrThreadSampler.hpp" apetushkov@9858: #include "jfr/recorder/jfrRecorder.hpp" apetushkov@9858: #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" apetushkov@9858: #include "jfr/recorder/repository/jfrRepository.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrOptionSet.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrPostBox.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrRecorderService.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrRecorderThread.hpp" apetushkov@9858: #include "jfr/recorder/storage/jfrStorage.hpp" apetushkov@9858: #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp" apetushkov@9858: #include "jfr/recorder/stringpool/jfrStringPool.hpp" apetushkov@9858: #include "jfr/utilities/jfrTime.hpp" apetushkov@9858: #include "jfr/writers/jfrJavaEventWriter.hpp" apetushkov@9858: #include "memory/resourceArea.hpp" apetushkov@9858: #include "runtime/handles.inline.hpp" redestad@9879: #include "runtime/globals_extension.hpp" apetushkov@9858: #include "utilities/growableArray.hpp" apetushkov@9858: apetushkov@9858: bool JfrRecorder::_shutting_down = false; apetushkov@9858: apetushkov@9858: bool JfrRecorder::is_disabled() { redestad@9879: // True if -XX:-FlightRecorder has been explicitly set on the redestad@9879: // command line redestad@9879: return FLAG_IS_CMDLINE(FlightRecorder) ? !FlightRecorder : false; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool _enabled = false; apetushkov@9858: apetushkov@9858: static bool enable() { apetushkov@9858: assert(!_enabled, "invariant"); redestad@9879: FLAG_SET_MGMT(bool, FlightRecorder, true); redestad@9879: _enabled = FlightRecorder; redestad@9879: assert(_enabled, "invariant"); apetushkov@9858: return _enabled; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::is_enabled() { apetushkov@9858: return _enabled; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::on_vm_init() { apetushkov@9858: if (!is_disabled()) { apetushkov@9858: if (FlightRecorder || StartFlightRecording != NULL) { apetushkov@9858: enable(); apetushkov@9858: } apetushkov@9858: } apetushkov@9858: // fast time initialization apetushkov@9858: return JfrTime::initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: static GrowableArray* dcmd_recordings_array = NULL; apetushkov@9858: apetushkov@9858: static void release_recordings() { apetushkov@9858: if (dcmd_recordings_array != NULL) { apetushkov@9858: const int length = dcmd_recordings_array->length(); apetushkov@9858: for (int i = 0; i < length; ++i) { apetushkov@9858: delete dcmd_recordings_array->at(i); apetushkov@9858: } apetushkov@9858: delete dcmd_recordings_array; apetushkov@9858: dcmd_recordings_array = NULL; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: static void teardown_startup_support() { apetushkov@9858: release_recordings(); apetushkov@9858: JfrOptionSet::release_startup_recording_options(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: // Parsing options here to detect errors as soon as possible apetushkov@9858: static bool parse_recording_options(const char* options, JfrStartFlightRecordingDCmd* dcmd_recording, TRAPS) { apetushkov@9858: assert(options != NULL, "invariant"); apetushkov@9858: assert(dcmd_recording != NULL, "invariant"); apetushkov@9858: CmdLine cmdline(options, strlen(options), true); apetushkov@9858: dcmd_recording->parse(&cmdline, ',', THREAD); apetushkov@9858: if (HAS_PENDING_EXCEPTION) { apetushkov@9858: java_lang_Throwable::print(PENDING_EXCEPTION, tty); apetushkov@9858: CLEAR_PENDING_EXCEPTION; apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool validate_recording_options(TRAPS) { apetushkov@9858: const GrowableArray* options = JfrOptionSet::startup_recording_options(); apetushkov@9858: if (options == NULL) { apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: const int length = options->length(); apetushkov@9858: assert(length >= 1, "invariant"); apetushkov@9858: assert(dcmd_recordings_array == NULL, "invariant"); apetushkov@9858: dcmd_recordings_array = new (ResourceObj::C_HEAP, mtTracing)GrowableArray(length, true, mtTracing); apetushkov@9858: assert(dcmd_recordings_array != NULL, "invariant"); apetushkov@9858: for (int i = 0; i < length; ++i) { apetushkov@9858: JfrStartFlightRecordingDCmd* const dcmd_recording = new(ResourceObj::C_HEAP, mtTracing) JfrStartFlightRecordingDCmd(tty, true); apetushkov@9858: assert(dcmd_recording != NULL, "invariant"); apetushkov@9858: dcmd_recordings_array->append(dcmd_recording); apetushkov@9858: if (!parse_recording_options(options->at(i), dcmd_recording, THREAD)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool launch_recording(JfrStartFlightRecordingDCmd* dcmd_recording, TRAPS) { apetushkov@9858: assert(dcmd_recording != NULL, "invariant"); apetushkov@9858: if (LogJFR && Verbose) tty->print_cr("Starting a recording"); apetushkov@9858: dcmd_recording->execute(DCmd_Source_Internal, THREAD); apetushkov@9858: if (HAS_PENDING_EXCEPTION) { apetushkov@9858: if (LogJFR) tty->print_cr("Exception while starting a recording"); apetushkov@9858: CLEAR_PENDING_EXCEPTION; apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (LogJFR && Verbose) tty->print_cr("Finished starting a recording"); apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool launch_recordings(TRAPS) { apetushkov@9858: bool result = true; apetushkov@9858: if (dcmd_recordings_array != NULL) { apetushkov@9858: const int length = dcmd_recordings_array->length(); apetushkov@9858: assert(length >= 1, "invariant"); apetushkov@9858: for (int i = 0; i < length; ++i) { apetushkov@9858: if (!launch_recording(dcmd_recordings_array->at(i), THREAD)) { apetushkov@9858: result = false; apetushkov@9858: break; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: } apetushkov@9858: teardown_startup_support(); apetushkov@9858: return result; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool is_cds_dump_requested() { apetushkov@9858: // we will not be able to launch recordings if a cds dump is being requested apetushkov@9858: if (DumpSharedSpaces && (JfrOptionSet::startup_recording_options() != NULL)) { apetushkov@9858: warning("JFR will be disabled during CDS dumping"); apetushkov@9858: teardown_startup_support(); apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::on_vm_start() { apetushkov@9858: if (is_cds_dump_requested()) { apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: Thread* const thread = Thread::current(); apetushkov@9858: if (!JfrJavaEventWriter::has_required_classes(thread)) { apetushkov@9858: // assume it is compact profile of jfr.jar is missed for some reasons apetushkov@9858: // skip further initialization. apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: if (!JfrOptionSet::initialize(thread)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!register_jfr_dcmds()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: apetushkov@9858: if (!validate_recording_options(thread)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!JfrJavaEventWriter::initialize()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!JfrOptionSet::configure(thread)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: apetushkov@9858: if (!is_enabled()) { apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: return launch_recordings(thread); apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool _created = false; apetushkov@9858: apetushkov@9858: // apetushkov@9858: // Main entry point for starting Jfr functionality. apetushkov@9858: // Non-protected initializations assume single-threaded setup. apetushkov@9858: // apetushkov@9858: bool JfrRecorder::create(bool simulate_failure) { apetushkov@9858: assert(!is_disabled(), "invariant"); apetushkov@9858: assert(!is_created(), "invariant"); apetushkov@9858: if (!is_enabled()) { apetushkov@9858: enable(); apetushkov@9858: } apetushkov@9858: if (!create_components() || simulate_failure) { apetushkov@9858: destroy_components(); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_recorder_thread()) { apetushkov@9858: destroy_components(); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: _created = true; apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::is_created() { apetushkov@9858: return _created; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_components() { apetushkov@9858: ResourceMark rm; apetushkov@9858: HandleMark hm; apetushkov@9858: apetushkov@9858: if (!create_jvmti_agent()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_post_box()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_chunk_repository()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_storage()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_checkpoint_manager()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_stacktrace_repository()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_os_interface()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_stringpool()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!create_thread_sampling()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: // subsystems apetushkov@9858: static JfrJvmtiAgent* _jvmti_agent = NULL; apetushkov@9858: static JfrPostBox* _post_box = NULL; apetushkov@9858: static JfrStorage* _storage = NULL; apetushkov@9858: static JfrCheckpointManager* _checkpoint_manager = NULL; apetushkov@9858: static JfrRepository* _repository = NULL; apetushkov@9858: static JfrStackTraceRepository* _stack_trace_repository; apetushkov@9858: static JfrStringPool* _stringpool = NULL; apetushkov@9858: static JfrOSInterface* _os_interface = NULL; apetushkov@9858: static JfrThreadSampling* _thread_sampling = NULL; apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_jvmti_agent() { apetushkov@9858: return JfrOptionSet::allow_retransforms() ? JfrJvmtiAgent::create() : true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_post_box() { apetushkov@9858: assert(_post_box == NULL, "invariant"); apetushkov@9858: _post_box = JfrPostBox::create(); apetushkov@9858: return _post_box != NULL; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_chunk_repository() { apetushkov@9858: assert(_repository == NULL, "invariant"); apetushkov@9858: assert(_post_box != NULL, "invariant"); apetushkov@9858: _repository = JfrRepository::create(*_post_box); apetushkov@9858: return _repository != NULL && _repository->initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_os_interface() { apetushkov@9858: assert(_os_interface == NULL, "invariant"); apetushkov@9858: _os_interface = JfrOSInterface::create(); apetushkov@9858: return _os_interface != NULL && _os_interface->initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_storage() { apetushkov@9858: assert(_repository != NULL, "invariant"); apetushkov@9858: assert(_post_box != NULL, "invariant"); apetushkov@9858: _storage = JfrStorage::create(_repository->chunkwriter(), *_post_box); apetushkov@9858: return _storage != NULL && _storage->initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_checkpoint_manager() { apetushkov@9858: assert(_checkpoint_manager == NULL, "invariant"); apetushkov@9858: assert(_repository != NULL, "invariant"); apetushkov@9858: _checkpoint_manager = JfrCheckpointManager::create(_repository->chunkwriter()); apetushkov@9858: return _checkpoint_manager != NULL && _checkpoint_manager->initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_stacktrace_repository() { apetushkov@9858: assert(_stack_trace_repository == NULL, "invariant"); apetushkov@9858: _stack_trace_repository = JfrStackTraceRepository::create(); apetushkov@9858: return _stack_trace_repository != NULL && _stack_trace_repository->initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_stringpool() { apetushkov@9858: assert(_stringpool == NULL, "invariant"); apetushkov@9858: assert(_repository != NULL, "invariant"); apetushkov@9858: _stringpool = JfrStringPool::create(_repository->chunkwriter()); apetushkov@9858: return _stringpool != NULL && _stringpool->initialize(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_thread_sampling() { apetushkov@9858: assert(_thread_sampling == NULL, "invariant"); apetushkov@9858: _thread_sampling = JfrThreadSampling::create(); apetushkov@9858: return _thread_sampling != NULL; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrRecorder::destroy_components() { apetushkov@9858: JfrJvmtiAgent::destroy(); apetushkov@9858: if (_post_box != NULL) { apetushkov@9858: JfrPostBox::destroy(); apetushkov@9858: _post_box = NULL; apetushkov@9858: } apetushkov@9858: if (_repository != NULL) { apetushkov@9858: JfrRepository::destroy(); apetushkov@9858: _repository = NULL; apetushkov@9858: } apetushkov@9858: if (_storage != NULL) { apetushkov@9858: JfrStorage::destroy(); apetushkov@9858: _storage = NULL; apetushkov@9858: } apetushkov@9858: if (_checkpoint_manager != NULL) { apetushkov@9858: JfrCheckpointManager::destroy(); apetushkov@9858: _checkpoint_manager = NULL; apetushkov@9858: } apetushkov@9858: if (_stack_trace_repository != NULL) { apetushkov@9858: JfrStackTraceRepository::destroy(); apetushkov@9858: _stack_trace_repository = NULL; apetushkov@9858: } apetushkov@9858: if (_stringpool != NULL) { apetushkov@9858: JfrStringPool::destroy(); apetushkov@9858: _stringpool = NULL; apetushkov@9858: } apetushkov@9858: if (_os_interface != NULL) { apetushkov@9858: JfrOSInterface::destroy(); apetushkov@9858: _os_interface = NULL; apetushkov@9858: } apetushkov@9858: if (_thread_sampling != NULL) { apetushkov@9858: JfrThreadSampling::destroy(); apetushkov@9858: _thread_sampling = NULL; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::create_recorder_thread() { apetushkov@9858: return JfrRecorderThread::start(_checkpoint_manager, _post_box, Thread::current()); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrRecorder::destroy() { apetushkov@9858: assert(is_created(), "invariant"); apetushkov@9858: _post_box->post(MSG_SHUTDOWN); apetushkov@9858: JfrJvmtiAgent::destroy(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrRecorder::on_recorder_thread_exit() { apetushkov@9858: assert(!is_recording(), "invariant"); apetushkov@9858: // intent is to destroy the recorder instance and components, apetushkov@9858: // but need sensitive coordination not yet in place apetushkov@9858: // apetushkov@9858: // destroy_components(); apetushkov@9858: // apetushkov@9858: if (LogJFR) tty->print_cr("Recorder thread STOPPED"); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrRecorder::start_recording() { apetushkov@9858: _post_box->post(MSG_START); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrRecorder::is_recording() { apetushkov@9858: return JfrRecorderService::is_recording(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrRecorder::stop_recording() { apetushkov@9858: _post_box->post(MSG_STOP); apetushkov@9858: }