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

Mon, 12 Aug 2019 18:30:40 +0300

author
apetushkov
date
Mon, 12 Aug 2019 18:30:40 +0300
changeset 9858
b985cbb00e68
child 9879
d2b51a10084d
permissions
-rw-r--r--

8223147: JFR Backport
8199712: Flight Recorder
8203346: JFR: Inconsistent signature of jfr_add_string_constant
8195817: JFR.stop should require name of recording
8195818: JFR.start should increase autogenerated name by one
8195819: Remove recording=x from jcmd JFR.check output
8203921: JFR thread sampling is missing fixes from JDK-8194552
8203929: Limit amount of data for JFR.dump
8203664: JFR start failure after AppCDS archive created with JFR StartFlightRecording
8003209: JFR events for network utilization
8207392: [PPC64] Implement JFR profiling
8202835: jfr/event/os/TestSystemProcess.java fails on missing events
Summary: Backport JFR from JDK11. Initial integration
Reviewed-by: neugens

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

mercurial