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

Tue, 15 Jan 2019 21:17:35 +0100

author
redestad
date
Tue, 15 Jan 2019 21:17:35 +0100
changeset 9879
d2b51a10084d
parent 9858
b985cbb00e68
child 9884
1258121876f8
permissions
-rw-r--r--

8216995: Clean up JFR command line processing
Reviewed-by: gziemski, mgronlun

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

mercurial