src/share/vm/jfr/recorder/jfrRecorder.cpp

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

author
jbachorik
date
Fri, 12 Jun 2020 02:59:56 +0100
changeset 9925
30fb8c8cceb9
parent 9884
1258121876f8
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 /*
redestad@9879 2 * Copyright (c) 2012, 2019, 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 "jfr/dcmd/jfrDcmds.hpp"
apetushkov@9858 27 #include "jfr/instrumentation/jfrJvmtiAgent.hpp"
apetushkov@9858 28 #include "jfr/jni/jfrJavaSupport.hpp"
apetushkov@9858 29 #include "jfr/periodic/jfrOSInterface.hpp"
apetushkov@9858 30 #include "jfr/periodic/sampling/jfrThreadSampler.hpp"
apetushkov@9858 31 #include "jfr/recorder/jfrRecorder.hpp"
apetushkov@9858 32 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
apetushkov@9858 33 #include "jfr/recorder/repository/jfrRepository.hpp"
apetushkov@9858 34 #include "jfr/recorder/service/jfrOptionSet.hpp"
apetushkov@9858 35 #include "jfr/recorder/service/jfrPostBox.hpp"
apetushkov@9858 36 #include "jfr/recorder/service/jfrRecorderService.hpp"
apetushkov@9858 37 #include "jfr/recorder/service/jfrRecorderThread.hpp"
apetushkov@9858 38 #include "jfr/recorder/storage/jfrStorage.hpp"
apetushkov@9858 39 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
apetushkov@9858 40 #include "jfr/recorder/stringpool/jfrStringPool.hpp"
apetushkov@9858 41 #include "jfr/utilities/jfrTime.hpp"
apetushkov@9858 42 #include "jfr/writers/jfrJavaEventWriter.hpp"
apetushkov@9858 43 #include "memory/resourceArea.hpp"
apetushkov@9858 44 #include "runtime/handles.inline.hpp"
redestad@9879 45 #include "runtime/globals_extension.hpp"
apetushkov@9858 46 #include "utilities/growableArray.hpp"
jbachorik@9925 47 #ifdef ASSERT
jbachorik@9925 48 #include "prims/jvmtiEnvBase.hpp"
jbachorik@9925 49 #endif
apetushkov@9858 50
apetushkov@9858 51 bool JfrRecorder::_shutting_down = false;
apetushkov@9858 52
apetushkov@9858 53 bool JfrRecorder::is_disabled() {
redestad@9879 54 // True if -XX:-FlightRecorder has been explicitly set on the
redestad@9879 55 // command line
redestad@9879 56 return FLAG_IS_CMDLINE(FlightRecorder) ? !FlightRecorder : false;
apetushkov@9858 57 }
apetushkov@9858 58
apetushkov@9858 59 static bool _enabled = false;
apetushkov@9858 60
apetushkov@9858 61 static bool enable() {
apetushkov@9858 62 assert(!_enabled, "invariant");
jbachorik@9925 63 if (!FlightRecorder) {
jbachorik@9925 64 FLAG_SET_MGMT(bool, FlightRecorder, true);
jbachorik@9925 65 }
redestad@9879 66 _enabled = FlightRecorder;
redestad@9879 67 assert(_enabled, "invariant");
apetushkov@9858 68 return _enabled;
apetushkov@9858 69 }
apetushkov@9858 70
apetushkov@9858 71 bool JfrRecorder::is_enabled() {
apetushkov@9858 72 return _enabled;
apetushkov@9858 73 }
apetushkov@9858 74
jbachorik@9925 75 bool JfrRecorder::on_create_vm_1() {
apetushkov@9858 76 if (!is_disabled()) {
apetushkov@9858 77 if (FlightRecorder || StartFlightRecording != NULL) {
apetushkov@9858 78 enable();
apetushkov@9858 79 }
apetushkov@9858 80 }
apetushkov@9858 81 // fast time initialization
apetushkov@9858 82 return JfrTime::initialize();
apetushkov@9858 83 }
apetushkov@9858 84
apetushkov@9858 85 static GrowableArray<JfrStartFlightRecordingDCmd*>* dcmd_recordings_array = NULL;
apetushkov@9858 86
apetushkov@9858 87 static void release_recordings() {
apetushkov@9858 88 if (dcmd_recordings_array != NULL) {
apetushkov@9858 89 const int length = dcmd_recordings_array->length();
apetushkov@9858 90 for (int i = 0; i < length; ++i) {
apetushkov@9858 91 delete dcmd_recordings_array->at(i);
apetushkov@9858 92 }
apetushkov@9858 93 delete dcmd_recordings_array;
apetushkov@9858 94 dcmd_recordings_array = NULL;
apetushkov@9858 95 }
apetushkov@9858 96 }
apetushkov@9858 97
apetushkov@9858 98 static void teardown_startup_support() {
apetushkov@9858 99 release_recordings();
jbachorik@9925 100 JfrOptionSet::release_start_flight_recording_options();
apetushkov@9858 101 }
apetushkov@9858 102
apetushkov@9858 103 // Parsing options here to detect errors as soon as possible
apetushkov@9858 104 static bool parse_recording_options(const char* options, JfrStartFlightRecordingDCmd* dcmd_recording, TRAPS) {
apetushkov@9858 105 assert(options != NULL, "invariant");
apetushkov@9858 106 assert(dcmd_recording != NULL, "invariant");
apetushkov@9858 107 CmdLine cmdline(options, strlen(options), true);
apetushkov@9858 108 dcmd_recording->parse(&cmdline, ',', THREAD);
apetushkov@9858 109 if (HAS_PENDING_EXCEPTION) {
apetushkov@9858 110 java_lang_Throwable::print(PENDING_EXCEPTION, tty);
apetushkov@9858 111 CLEAR_PENDING_EXCEPTION;
apetushkov@9858 112 return false;
apetushkov@9858 113 }
apetushkov@9858 114 return true;
apetushkov@9858 115 }
apetushkov@9858 116
apetushkov@9858 117 static bool validate_recording_options(TRAPS) {
jbachorik@9925 118 const GrowableArray<const char*>* options = JfrOptionSet::start_flight_recording_options();
apetushkov@9858 119 if (options == NULL) {
apetushkov@9858 120 return true;
apetushkov@9858 121 }
apetushkov@9858 122 const int length = options->length();
apetushkov@9858 123 assert(length >= 1, "invariant");
apetushkov@9858 124 assert(dcmd_recordings_array == NULL, "invariant");
apetushkov@9858 125 dcmd_recordings_array = new (ResourceObj::C_HEAP, mtTracing)GrowableArray<JfrStartFlightRecordingDCmd*>(length, true, mtTracing);
apetushkov@9858 126 assert(dcmd_recordings_array != NULL, "invariant");
apetushkov@9858 127 for (int i = 0; i < length; ++i) {
apetushkov@9858 128 JfrStartFlightRecordingDCmd* const dcmd_recording = new(ResourceObj::C_HEAP, mtTracing) JfrStartFlightRecordingDCmd(tty, true);
apetushkov@9858 129 assert(dcmd_recording != NULL, "invariant");
apetushkov@9858 130 dcmd_recordings_array->append(dcmd_recording);
apetushkov@9858 131 if (!parse_recording_options(options->at(i), dcmd_recording, THREAD)) {
apetushkov@9858 132 return false;
apetushkov@9858 133 }
apetushkov@9858 134 }
apetushkov@9858 135 return true;
apetushkov@9858 136 }
apetushkov@9858 137
apetushkov@9858 138 static bool launch_recording(JfrStartFlightRecordingDCmd* dcmd_recording, TRAPS) {
apetushkov@9858 139 assert(dcmd_recording != NULL, "invariant");
apetushkov@9858 140 if (LogJFR && Verbose) tty->print_cr("Starting a recording");
apetushkov@9858 141 dcmd_recording->execute(DCmd_Source_Internal, THREAD);
apetushkov@9858 142 if (HAS_PENDING_EXCEPTION) {
apetushkov@9858 143 if (LogJFR) tty->print_cr("Exception while starting a recording");
apetushkov@9858 144 CLEAR_PENDING_EXCEPTION;
apetushkov@9858 145 return false;
apetushkov@9858 146 }
apetushkov@9858 147 if (LogJFR && Verbose) tty->print_cr("Finished starting a recording");
apetushkov@9858 148 return true;
apetushkov@9858 149 }
apetushkov@9858 150
jbachorik@9925 151 static bool launch_command_line_recordings(TRAPS) {
apetushkov@9858 152 bool result = true;
apetushkov@9858 153 if (dcmd_recordings_array != NULL) {
apetushkov@9858 154 const int length = dcmd_recordings_array->length();
apetushkov@9858 155 assert(length >= 1, "invariant");
apetushkov@9858 156 for (int i = 0; i < length; ++i) {
apetushkov@9858 157 if (!launch_recording(dcmd_recordings_array->at(i), THREAD)) {
apetushkov@9858 158 result = false;
apetushkov@9858 159 break;
apetushkov@9858 160 }
apetushkov@9858 161 }
apetushkov@9858 162 }
apetushkov@9858 163 teardown_startup_support();
apetushkov@9858 164 return result;
apetushkov@9858 165 }
apetushkov@9858 166
apetushkov@9858 167 static bool is_cds_dump_requested() {
apetushkov@9858 168 // we will not be able to launch recordings if a cds dump is being requested
jbachorik@9925 169 if (DumpSharedSpaces && (JfrOptionSet::start_flight_recording_options() != NULL)) {
apetushkov@9858 170 warning("JFR will be disabled during CDS dumping");
apetushkov@9858 171 teardown_startup_support();
apetushkov@9858 172 return true;
apetushkov@9858 173 }
apetushkov@9858 174 return false;
apetushkov@9858 175 }
apetushkov@9858 176
jbachorik@9925 177 bool JfrRecorder::on_create_vm_2() {
apetushkov@9858 178 if (is_cds_dump_requested()) {
apetushkov@9858 179 return true;
apetushkov@9858 180 }
apetushkov@9858 181 Thread* const thread = Thread::current();
apetushkov@9858 182 if (!JfrJavaEventWriter::has_required_classes(thread)) {
apetushkov@9858 183 // assume it is compact profile of jfr.jar is missed for some reasons
apetushkov@9858 184 // skip further initialization.
apetushkov@9858 185 return true;
apetushkov@9858 186 }
apetushkov@9858 187 if (!JfrOptionSet::initialize(thread)) {
apetushkov@9858 188 return false;
apetushkov@9858 189 }
apetushkov@9858 190 if (!register_jfr_dcmds()) {
apetushkov@9858 191 return false;
apetushkov@9858 192 }
apetushkov@9858 193
apetushkov@9858 194 if (!validate_recording_options(thread)) {
apetushkov@9858 195 return false;
apetushkov@9858 196 }
apetushkov@9858 197 if (!JfrOptionSet::configure(thread)) {
apetushkov@9858 198 return false;
apetushkov@9858 199 }
apetushkov@9858 200
apetushkov@9858 201 if (!is_enabled()) {
apetushkov@9858 202 return true;
apetushkov@9858 203 }
jbachorik@9925 204 return true;
jbachorik@9925 205 }
apetushkov@9858 206
jbachorik@9925 207 bool JfrRecorder::on_create_vm_3() {
jbachorik@9925 208 assert(JvmtiEnvBase::get_phase() == JVMTI_PHASE_LIVE, "invalid init sequence");
jbachorik@9925 209 return launch_command_line_recordings(Thread::current());
jbachorik@9925 210 }
apetushkov@9858 211
apetushkov@9858 212 static bool _created = false;
apetushkov@9858 213
apetushkov@9858 214 //
apetushkov@9858 215 // Main entry point for starting Jfr functionality.
apetushkov@9858 216 // Non-protected initializations assume single-threaded setup.
apetushkov@9858 217 //
apetushkov@9858 218 bool JfrRecorder::create(bool simulate_failure) {
apetushkov@9858 219 assert(!is_disabled(), "invariant");
apetushkov@9858 220 assert(!is_created(), "invariant");
apetushkov@9858 221 if (!is_enabled()) {
apetushkov@9858 222 enable();
apetushkov@9858 223 }
apetushkov@9858 224 if (!create_components() || simulate_failure) {
apetushkov@9858 225 destroy_components();
apetushkov@9858 226 return false;
apetushkov@9858 227 }
apetushkov@9858 228 if (!create_recorder_thread()) {
apetushkov@9858 229 destroy_components();
apetushkov@9858 230 return false;
apetushkov@9858 231 }
apetushkov@9858 232 _created = true;
apetushkov@9858 233 return true;
apetushkov@9858 234 }
apetushkov@9858 235
apetushkov@9858 236 bool JfrRecorder::is_created() {
apetushkov@9858 237 return _created;
apetushkov@9858 238 }
apetushkov@9858 239
apetushkov@9858 240 bool JfrRecorder::create_components() {
apetushkov@9858 241 ResourceMark rm;
apetushkov@9858 242 HandleMark hm;
apetushkov@9858 243
mgronlun@9884 244 if (!create_java_event_writer()) {
mgronlun@9884 245 return false;
mgronlun@9884 246 }
apetushkov@9858 247 if (!create_jvmti_agent()) {
apetushkov@9858 248 return false;
apetushkov@9858 249 }
apetushkov@9858 250 if (!create_post_box()) {
apetushkov@9858 251 return false;
apetushkov@9858 252 }
apetushkov@9858 253 if (!create_chunk_repository()) {
apetushkov@9858 254 return false;
apetushkov@9858 255 }
apetushkov@9858 256 if (!create_storage()) {
apetushkov@9858 257 return false;
apetushkov@9858 258 }
apetushkov@9858 259 if (!create_checkpoint_manager()) {
apetushkov@9858 260 return false;
apetushkov@9858 261 }
apetushkov@9858 262 if (!create_stacktrace_repository()) {
apetushkov@9858 263 return false;
apetushkov@9858 264 }
apetushkov@9858 265 if (!create_os_interface()) {
apetushkov@9858 266 return false;
apetushkov@9858 267 }
apetushkov@9858 268 if (!create_stringpool()) {
apetushkov@9858 269 return false;
apetushkov@9858 270 }
apetushkov@9858 271 if (!create_thread_sampling()) {
apetushkov@9858 272 return false;
apetushkov@9858 273 }
apetushkov@9858 274 return true;
apetushkov@9858 275 }
apetushkov@9858 276
apetushkov@9858 277 // subsystems
apetushkov@9858 278 static JfrPostBox* _post_box = NULL;
apetushkov@9858 279 static JfrStorage* _storage = NULL;
apetushkov@9858 280 static JfrCheckpointManager* _checkpoint_manager = NULL;
apetushkov@9858 281 static JfrRepository* _repository = NULL;
apetushkov@9858 282 static JfrStackTraceRepository* _stack_trace_repository;
apetushkov@9858 283 static JfrStringPool* _stringpool = NULL;
apetushkov@9858 284 static JfrOSInterface* _os_interface = NULL;
apetushkov@9858 285 static JfrThreadSampling* _thread_sampling = NULL;
apetushkov@9858 286
mgronlun@9884 287 bool JfrRecorder::create_java_event_writer() {
mgronlun@9884 288 return JfrJavaEventWriter::initialize();
mgronlun@9884 289 }
mgronlun@9884 290
apetushkov@9858 291 bool JfrRecorder::create_jvmti_agent() {
apetushkov@9858 292 return JfrOptionSet::allow_retransforms() ? JfrJvmtiAgent::create() : true;
apetushkov@9858 293 }
apetushkov@9858 294
apetushkov@9858 295 bool JfrRecorder::create_post_box() {
apetushkov@9858 296 assert(_post_box == NULL, "invariant");
apetushkov@9858 297 _post_box = JfrPostBox::create();
apetushkov@9858 298 return _post_box != NULL;
apetushkov@9858 299 }
apetushkov@9858 300
apetushkov@9858 301 bool JfrRecorder::create_chunk_repository() {
apetushkov@9858 302 assert(_repository == NULL, "invariant");
apetushkov@9858 303 assert(_post_box != NULL, "invariant");
apetushkov@9858 304 _repository = JfrRepository::create(*_post_box);
apetushkov@9858 305 return _repository != NULL && _repository->initialize();
apetushkov@9858 306 }
apetushkov@9858 307
apetushkov@9858 308 bool JfrRecorder::create_os_interface() {
apetushkov@9858 309 assert(_os_interface == NULL, "invariant");
apetushkov@9858 310 _os_interface = JfrOSInterface::create();
apetushkov@9858 311 return _os_interface != NULL && _os_interface->initialize();
apetushkov@9858 312 }
apetushkov@9858 313
apetushkov@9858 314 bool JfrRecorder::create_storage() {
apetushkov@9858 315 assert(_repository != NULL, "invariant");
apetushkov@9858 316 assert(_post_box != NULL, "invariant");
apetushkov@9858 317 _storage = JfrStorage::create(_repository->chunkwriter(), *_post_box);
apetushkov@9858 318 return _storage != NULL && _storage->initialize();
apetushkov@9858 319 }
apetushkov@9858 320
apetushkov@9858 321 bool JfrRecorder::create_checkpoint_manager() {
apetushkov@9858 322 assert(_checkpoint_manager == NULL, "invariant");
apetushkov@9858 323 assert(_repository != NULL, "invariant");
apetushkov@9858 324 _checkpoint_manager = JfrCheckpointManager::create(_repository->chunkwriter());
apetushkov@9858 325 return _checkpoint_manager != NULL && _checkpoint_manager->initialize();
apetushkov@9858 326 }
apetushkov@9858 327
apetushkov@9858 328 bool JfrRecorder::create_stacktrace_repository() {
apetushkov@9858 329 assert(_stack_trace_repository == NULL, "invariant");
apetushkov@9858 330 _stack_trace_repository = JfrStackTraceRepository::create();
apetushkov@9858 331 return _stack_trace_repository != NULL && _stack_trace_repository->initialize();
apetushkov@9858 332 }
apetushkov@9858 333
apetushkov@9858 334 bool JfrRecorder::create_stringpool() {
apetushkov@9858 335 assert(_stringpool == NULL, "invariant");
apetushkov@9858 336 assert(_repository != NULL, "invariant");
apetushkov@9858 337 _stringpool = JfrStringPool::create(_repository->chunkwriter());
apetushkov@9858 338 return _stringpool != NULL && _stringpool->initialize();
apetushkov@9858 339 }
apetushkov@9858 340
apetushkov@9858 341 bool JfrRecorder::create_thread_sampling() {
apetushkov@9858 342 assert(_thread_sampling == NULL, "invariant");
apetushkov@9858 343 _thread_sampling = JfrThreadSampling::create();
apetushkov@9858 344 return _thread_sampling != NULL;
apetushkov@9858 345 }
apetushkov@9858 346
apetushkov@9858 347 void JfrRecorder::destroy_components() {
apetushkov@9858 348 JfrJvmtiAgent::destroy();
apetushkov@9858 349 if (_post_box != NULL) {
apetushkov@9858 350 JfrPostBox::destroy();
apetushkov@9858 351 _post_box = NULL;
apetushkov@9858 352 }
apetushkov@9858 353 if (_repository != NULL) {
apetushkov@9858 354 JfrRepository::destroy();
apetushkov@9858 355 _repository = NULL;
apetushkov@9858 356 }
apetushkov@9858 357 if (_storage != NULL) {
apetushkov@9858 358 JfrStorage::destroy();
apetushkov@9858 359 _storage = NULL;
apetushkov@9858 360 }
apetushkov@9858 361 if (_checkpoint_manager != NULL) {
apetushkov@9858 362 JfrCheckpointManager::destroy();
apetushkov@9858 363 _checkpoint_manager = NULL;
apetushkov@9858 364 }
apetushkov@9858 365 if (_stack_trace_repository != NULL) {
apetushkov@9858 366 JfrStackTraceRepository::destroy();
apetushkov@9858 367 _stack_trace_repository = NULL;
apetushkov@9858 368 }
apetushkov@9858 369 if (_stringpool != NULL) {
apetushkov@9858 370 JfrStringPool::destroy();
apetushkov@9858 371 _stringpool = NULL;
apetushkov@9858 372 }
apetushkov@9858 373 if (_os_interface != NULL) {
apetushkov@9858 374 JfrOSInterface::destroy();
apetushkov@9858 375 _os_interface = NULL;
apetushkov@9858 376 }
apetushkov@9858 377 if (_thread_sampling != NULL) {
apetushkov@9858 378 JfrThreadSampling::destroy();
apetushkov@9858 379 _thread_sampling = NULL;
apetushkov@9858 380 }
apetushkov@9858 381 }
apetushkov@9858 382
apetushkov@9858 383 bool JfrRecorder::create_recorder_thread() {
apetushkov@9858 384 return JfrRecorderThread::start(_checkpoint_manager, _post_box, Thread::current());
apetushkov@9858 385 }
apetushkov@9858 386
apetushkov@9858 387 void JfrRecorder::destroy() {
apetushkov@9858 388 assert(is_created(), "invariant");
apetushkov@9858 389 _post_box->post(MSG_SHUTDOWN);
apetushkov@9858 390 JfrJvmtiAgent::destroy();
apetushkov@9858 391 }
apetushkov@9858 392
apetushkov@9858 393 void JfrRecorder::on_recorder_thread_exit() {
apetushkov@9858 394 assert(!is_recording(), "invariant");
apetushkov@9858 395 // intent is to destroy the recorder instance and components,
apetushkov@9858 396 // but need sensitive coordination not yet in place
apetushkov@9858 397 //
apetushkov@9858 398 // destroy_components();
apetushkov@9858 399 //
apetushkov@9858 400 if (LogJFR) tty->print_cr("Recorder thread STOPPED");
apetushkov@9858 401 }
apetushkov@9858 402
apetushkov@9858 403 void JfrRecorder::start_recording() {
apetushkov@9858 404 _post_box->post(MSG_START);
apetushkov@9858 405 }
apetushkov@9858 406
apetushkov@9858 407 bool JfrRecorder::is_recording() {
apetushkov@9858 408 return JfrRecorderService::is_recording();
apetushkov@9858 409 }
apetushkov@9858 410
apetushkov@9858 411 void JfrRecorder::stop_recording() {
apetushkov@9858 412 _post_box->post(MSG_STOP);
apetushkov@9858 413 }

mercurial