apetushkov@9858: /* apetushkov@9858: * Copyright (c) 2012, 2018, 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/recorder/service/jfrMemorySizer.hpp" apetushkov@9858: #include "jfr/recorder/service/jfrOptionSet.hpp" apetushkov@9858: #include "jfr/utilities/jfrAllocation.hpp" apetushkov@9858: #include "memory/allocation.inline.hpp" apetushkov@9858: #include "memory/resourceArea.hpp" apetushkov@9858: #include "runtime/java.hpp" apetushkov@9858: #include "runtime/thread.inline.hpp" apetushkov@9858: #include "services/diagnosticArgument.hpp" apetushkov@9858: #include "services/diagnosticFramework.hpp" apetushkov@9858: #include "utilities/growableArray.hpp" apetushkov@9858: #include "utilities/ostream.hpp" apetushkov@9858: apetushkov@9858: struct ObsoleteOption { apetushkov@9858: const char* name; apetushkov@9858: const char* message; apetushkov@9858: }; apetushkov@9858: apetushkov@9858: static const ObsoleteOption OBSOLETE_OPTIONS[] = { apetushkov@9858: {"checkpointbuffersize", ""}, apetushkov@9858: {"maxsize", "Use -XX:StartFlightRecording=maxsize=... instead."}, apetushkov@9858: {"maxage", "Use -XX:StartFlightRecording=maxage=... instead."}, apetushkov@9858: {"settings", "Use -XX:StartFlightRecording=settings=... instead."}, apetushkov@9858: {"defaultrecording", "Use -XX:StartFlightRecording=disk=false to create an in-memory recording."}, apetushkov@9858: {"disk", "Use -XX:StartFlightRecording=disk=... instead."}, apetushkov@9858: {"dumponexit", "Use -XX:StartFlightRecording=dumponexit=... instead."}, apetushkov@9858: {"dumponexitpath", "Use -XX:StartFlightRecording=filename=... instead."}, apetushkov@9858: {"loglevel", "Use -Xlog:jfr=... instead."} apetushkov@9858: }; apetushkov@9858: apetushkov@9858: jlong JfrOptionSet::max_chunk_size() { apetushkov@9858: return _max_chunk_size; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_max_chunk_size(jlong value) { apetushkov@9858: _max_chunk_size = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: jlong JfrOptionSet::global_buffer_size() { apetushkov@9858: return _global_buffer_size; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_global_buffer_size(jlong value) { apetushkov@9858: _global_buffer_size = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: jlong JfrOptionSet::thread_buffer_size() { apetushkov@9858: return _thread_buffer_size; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_thread_buffer_size(jlong value) { apetushkov@9858: _thread_buffer_size = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: jlong JfrOptionSet::memory_size() { apetushkov@9858: return _memory_size; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_memory_size(jlong value) { apetushkov@9858: _memory_size = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: jlong JfrOptionSet::num_global_buffers() { apetushkov@9858: return _num_global_buffers; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_num_global_buffers(jlong value) { apetushkov@9858: _num_global_buffers = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: jint JfrOptionSet::old_object_queue_size() { apetushkov@9858: return (jint)_old_object_queue_size; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_old_object_queue_size(jlong value) { apetushkov@9858: _old_object_queue_size = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: u4 JfrOptionSet::stackdepth() { apetushkov@9858: return _stack_depth; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static const u4 STACK_DEPTH_DEFAULT = 64; apetushkov@9858: static const u4 MIN_STACK_DEPTH = 1; apetushkov@9858: static const u4 MAX_STACK_DEPTH = 2048; apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_stackdepth(u4 depth) { apetushkov@9858: if (depth < MIN_STACK_DEPTH) { apetushkov@9858: _stack_depth = MIN_STACK_DEPTH; apetushkov@9858: } else if (depth > MAX_STACK_DEPTH) { apetushkov@9858: _stack_depth = MAX_STACK_DEPTH; apetushkov@9858: } else { apetushkov@9858: _stack_depth = depth; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::sample_threads() { apetushkov@9858: return _sample_threads == JNI_TRUE; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_sample_threads(jboolean sample) { apetushkov@9858: _sample_threads = sample; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::can_retransform() { apetushkov@9858: return _retransform == JNI_TRUE; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::set_retransform(jboolean value) { apetushkov@9858: _retransform = value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::sample_protection() { apetushkov@9858: return _sample_protection == JNI_TRUE; apetushkov@9858: } apetushkov@9858: apetushkov@9858: #ifdef ASSERT apetushkov@9858: void JfrOptionSet::set_sample_protection(jboolean protection) { apetushkov@9858: _sample_protection = protection; apetushkov@9858: } apetushkov@9858: #endif apetushkov@9858: apetushkov@9858: bool JfrOptionSet::compressed_integers() { apetushkov@9858: // Set this to false for debugging purposes. apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::allow_retransforms() { apetushkov@9858: #if INCLUDE_JVMTI apetushkov@9858: return true; apetushkov@9858: #else apetushkov@9858: return false; apetushkov@9858: #endif apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::allow_event_retransforms() { apetushkov@9858: return allow_retransforms() && (DumpSharedSpaces || can_retransform()); apetushkov@9858: } apetushkov@9858: apetushkov@9858: // default options for the dcmd parser apetushkov@9858: const char* const default_repository = NULL; apetushkov@9858: const char* const default_global_buffer_size = "512k"; apetushkov@9858: const char* const default_num_global_buffers = "20"; apetushkov@9858: const char* const default_memory_size = "10m"; apetushkov@9858: const char* const default_thread_buffer_size = "8k"; apetushkov@9858: const char* const default_max_chunk_size = "12m"; apetushkov@9858: const char* const default_sample_threads = "true"; apetushkov@9858: const char* const default_stack_depth = "64"; apetushkov@9858: const char* const default_retransform = "true"; apetushkov@9858: const char* const default_old_object_queue_size = "256"; apetushkov@9858: DEBUG_ONLY(const char* const default_sample_protection = "false";) apetushkov@9858: apetushkov@9858: // statics apetushkov@9858: static DCmdArgument _dcmd_repository( apetushkov@9858: "repository", apetushkov@9858: "Flight recorder disk repository location", apetushkov@9858: "STRING", apetushkov@9858: false, apetushkov@9858: default_repository); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_threadbuffersize( apetushkov@9858: "threadbuffersize", apetushkov@9858: "Thread buffer size", apetushkov@9858: "MEMORY SIZE", apetushkov@9858: false, apetushkov@9858: default_thread_buffer_size); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_memorysize( apetushkov@9858: "memorysize", apetushkov@9858: "Size of memory to be used by Flight Recorder", apetushkov@9858: "MEMORY SIZE", apetushkov@9858: false, apetushkov@9858: default_memory_size); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_globalbuffersize( apetushkov@9858: "globalbuffersize", apetushkov@9858: "Global buffer size", apetushkov@9858: "MEMORY SIZE", apetushkov@9858: false, apetushkov@9858: default_global_buffer_size); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_numglobalbuffers( apetushkov@9858: "numglobalbuffers", apetushkov@9858: "Number of global buffers", apetushkov@9858: "JULONG", apetushkov@9858: false, apetushkov@9858: default_num_global_buffers); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_maxchunksize( apetushkov@9858: "maxchunksize", apetushkov@9858: "Maximum size of a single repository disk chunk", apetushkov@9858: "MEMORY SIZE", apetushkov@9858: false, apetushkov@9858: default_max_chunk_size); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_old_object_queue_size ( apetushkov@9858: "old-object-queue-size", apetushkov@9858: "Maximum number of old objects to track", apetushkov@9858: "JINT", apetushkov@9858: false, apetushkov@9858: default_old_object_queue_size); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_sample_threads( apetushkov@9858: "samplethreads", apetushkov@9858: "Thread sampling enable / disable (only sampling when event enabled and sampling enabled)", apetushkov@9858: "BOOLEAN", apetushkov@9858: false, apetushkov@9858: default_sample_threads); apetushkov@9858: apetushkov@9858: #ifdef ASSERT apetushkov@9858: static DCmdArgument _dcmd_sample_protection( apetushkov@9858: "sampleprotection", apetushkov@9858: "Safeguard for stackwalking while sampling threads (false by default)", apetushkov@9858: "BOOLEAN", apetushkov@9858: false, apetushkov@9858: default_sample_protection); apetushkov@9858: #endif apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_stackdepth( apetushkov@9858: "stackdepth", apetushkov@9858: "Stack depth for stacktraces (minimum 1, maximum 2048)", apetushkov@9858: "JULONG", apetushkov@9858: false, apetushkov@9858: default_stack_depth); apetushkov@9858: apetushkov@9858: static DCmdArgument _dcmd_retransform( apetushkov@9858: "retransform", apetushkov@9858: "If event classes should be instrumented using JVMTI (by default true)", apetushkov@9858: "BOOLEAN", apetushkov@9858: true, apetushkov@9858: default_retransform); apetushkov@9858: apetushkov@9858: static DCmdParser _parser; apetushkov@9858: apetushkov@9858: static void register_parser_options() { apetushkov@9858: _parser.add_dcmd_option(&_dcmd_repository); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_threadbuffersize); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_memorysize); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_globalbuffersize); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_numglobalbuffers); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_maxchunksize); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_stackdepth); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_sample_threads); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_retransform); apetushkov@9858: _parser.add_dcmd_option(&_dcmd_old_object_queue_size); apetushkov@9858: DEBUG_ONLY(_parser.add_dcmd_option(&_dcmd_sample_protection);) apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool parse_flight_recorder_options_internal(TRAPS) { apetushkov@9858: if (FlightRecorderOptions == NULL) { apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: const size_t length = strlen((const char*)FlightRecorderOptions); apetushkov@9858: CmdLine cmdline((const char*)FlightRecorderOptions, length, true); apetushkov@9858: _parser.parse(&cmdline, ',', THREAD); apetushkov@9858: if (HAS_PENDING_EXCEPTION) { apetushkov@9858: for (int index = 0; index < 9; index++) { apetushkov@9858: ObsoleteOption option = OBSOLETE_OPTIONS[index]; apetushkov@9858: const char* p = strstr((const char*)FlightRecorderOptions, option.name); apetushkov@9858: const size_t option_length = strlen(option.name); apetushkov@9858: if (p != NULL && p[option_length] == '=') { apetushkov@9858: tty->print_cr("-XX:FlightRecorderOptions=%s=... has been removed. %s", option.name, option.message); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: ResourceMark rm(THREAD); apetushkov@9858: oop message = java_lang_Throwable::message(PENDING_EXCEPTION); apetushkov@9858: if (message != NULL) { apetushkov@9858: const char* msg = java_lang_String::as_utf8_string(message); apetushkov@9858: tty->print_cr("%s", msg); apetushkov@9858: } apetushkov@9858: CLEAR_PENDING_EXCEPTION; apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: jlong JfrOptionSet::_max_chunk_size = 0; apetushkov@9858: jlong JfrOptionSet::_global_buffer_size = 0; apetushkov@9858: jlong JfrOptionSet::_thread_buffer_size = 0; apetushkov@9858: jlong JfrOptionSet::_memory_size = 0; apetushkov@9858: jlong JfrOptionSet::_num_global_buffers = 0; apetushkov@9858: jlong JfrOptionSet::_old_object_queue_size = 0; apetushkov@9858: u4 JfrOptionSet::_stack_depth = STACK_DEPTH_DEFAULT; apetushkov@9858: jboolean JfrOptionSet::_sample_threads = JNI_TRUE; apetushkov@9858: jboolean JfrOptionSet::_retransform = JNI_TRUE; apetushkov@9858: #ifdef ASSERT apetushkov@9858: jboolean JfrOptionSet::_sample_protection = JNI_FALSE; apetushkov@9858: #else apetushkov@9858: jboolean JfrOptionSet::_sample_protection = JNI_TRUE; apetushkov@9858: #endif apetushkov@9858: apetushkov@9858: bool JfrOptionSet::initialize(Thread* thread) { apetushkov@9858: register_parser_options(); apetushkov@9858: if (!parse_flight_recorder_options_internal(thread)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (_dcmd_retransform.is_set()) { apetushkov@9858: set_retransform(_dcmd_retransform.value()); apetushkov@9858: } apetushkov@9858: set_old_object_queue_size(_dcmd_old_object_queue_size.value()); apetushkov@9858: return adjust_memory_options(); apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::configure(TRAPS) { apetushkov@9858: if (FlightRecorderOptions == NULL) { apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: ResourceMark rm(THREAD); apetushkov@9858: bufferedStream st; apetushkov@9858: // delegate to DCmd execution apetushkov@9858: JfrConfigureFlightRecorderDCmd configure(&st, false); apetushkov@9858: configure._repository_path.set_is_set(_dcmd_repository.is_set()); apetushkov@9858: char* repo = _dcmd_repository.value(); apetushkov@9858: if (repo != NULL) { apetushkov@9858: const size_t len = strlen(repo); apetushkov@9858: char* repo_copy = JfrCHeapObj::new_array(len + 1); apetushkov@9858: if (NULL == repo_copy) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: strncpy(repo_copy, repo, len + 1); apetushkov@9858: configure._repository_path.set_value(repo_copy); apetushkov@9858: } apetushkov@9858: apetushkov@9858: configure._stack_depth.set_is_set(_dcmd_stackdepth.is_set()); apetushkov@9858: configure._stack_depth.set_value(_dcmd_stackdepth.value()); apetushkov@9858: apetushkov@9858: configure._thread_buffer_size.set_is_set(_dcmd_threadbuffersize.is_set()); ysuenaga@9870: configure._thread_buffer_size.set_value(_dcmd_threadbuffersize.value()); apetushkov@9858: apetushkov@9858: configure._global_buffer_count.set_is_set(_dcmd_numglobalbuffers.is_set()); apetushkov@9858: configure._global_buffer_count.set_value(_dcmd_numglobalbuffers.value()); apetushkov@9858: apetushkov@9858: configure._global_buffer_size.set_is_set(_dcmd_globalbuffersize.is_set()); ysuenaga@9870: configure._global_buffer_size.set_value(_dcmd_globalbuffersize.value()); apetushkov@9858: apetushkov@9858: configure._max_chunk_size.set_is_set(_dcmd_maxchunksize.is_set()); ysuenaga@9870: configure._max_chunk_size.set_value(_dcmd_maxchunksize.value()); apetushkov@9858: apetushkov@9858: configure._memory_size.set_is_set(_dcmd_memorysize.is_set()); ysuenaga@9870: configure._memory_size.set_value(_dcmd_memorysize.value()); apetushkov@9858: apetushkov@9858: configure._sample_threads.set_is_set(_dcmd_sample_threads.is_set()); apetushkov@9858: configure._sample_threads.set_value(_dcmd_sample_threads.value()); apetushkov@9858: apetushkov@9858: configure.execute(DCmd_Source_Internal, THREAD); apetushkov@9858: 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: template apetushkov@9858: static julong divide_with_user_unit(Argument& memory_argument, julong value) { apetushkov@9858: if (memory_argument.value()._size != memory_argument.value()._val) { apetushkov@9858: switch (memory_argument.value()._multiplier) { apetushkov@9858: case 'k': case 'K': apetushkov@9858: return value / K; apetushkov@9858: case 'm': case 'M': apetushkov@9858: return value / M; apetushkov@9858: case 'g': case 'G': apetushkov@9858: return value / G; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: return value; apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: static void log_lower_than_min_value(Argument& memory_argument, julong min_value) { apetushkov@9858: if (memory_argument.value()._size != memory_argument.value()._val) { apetushkov@9858: // has multiplier apetushkov@9858: tty->print_cr( apetushkov@9858: "This value is lower than the minimum size required " JULONG_FORMAT "%c", apetushkov@9858: divide_with_user_unit(memory_argument, min_value), apetushkov@9858: memory_argument.value()._multiplier); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: tty->print_cr( apetushkov@9858: "This value is lower than the minimum size required " JULONG_FORMAT, apetushkov@9858: divide_with_user_unit(memory_argument, min_value)); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: static void log_set_value(Argument& memory_argument) { apetushkov@9858: if (memory_argument.value()._size != memory_argument.value()._val) { apetushkov@9858: // has multiplier apetushkov@9858: tty->print_cr( apetushkov@9858: "Value specified for option \"%s\" is " JULONG_FORMAT "%c", apetushkov@9858: memory_argument.name(), apetushkov@9858: memory_argument.value()._val, apetushkov@9858: memory_argument.value()._multiplier); apetushkov@9858: return; apetushkov@9858: } apetushkov@9858: tty->print_cr( apetushkov@9858: "Value specified for option \"%s\" is " JULONG_FORMAT, apetushkov@9858: memory_argument.name(), memory_argument.value()._val); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: static void log_adjustments(MemoryArg& original_memory_size, julong new_memory_size, const char* msg) { apetushkov@9858: if (LogJFR && Verbose) tty->print_cr( apetushkov@9858: "%s size (original) " JULONG_FORMAT " B (user defined: %s)", apetushkov@9858: msg, apetushkov@9858: original_memory_size.value()._size, apetushkov@9858: original_memory_size.is_set() ? "true" : "false"); apetushkov@9858: if (LogJFR && Verbose) tty->print_cr( apetushkov@9858: "%s size (adjusted) " JULONG_FORMAT " B (modified: %s)", apetushkov@9858: msg, apetushkov@9858: new_memory_size, apetushkov@9858: original_memory_size.value()._size != new_memory_size ? "true" : "false"); apetushkov@9858: if (LogJFR && Verbose) tty->print_cr( apetushkov@9858: "%s size (adjustment) %s" JULONG_FORMAT " B", apetushkov@9858: msg, apetushkov@9858: new_memory_size < original_memory_size.value()._size ? "-" : "+", apetushkov@9858: new_memory_size < original_memory_size.value()._size ? apetushkov@9858: original_memory_size.value()._size - new_memory_size : apetushkov@9858: new_memory_size - original_memory_size.value()._size); apetushkov@9858: } apetushkov@9858: apetushkov@9858: // All "triangular" options are explicitly set apetushkov@9858: // check that they are congruent and not causing apetushkov@9858: // an ambiguous situtation apetushkov@9858: template apetushkov@9858: static bool check_for_ambiguity(MemoryArg& memory_size, MemoryArg& global_buffer_size, NumberArg& num_global_buffers) { apetushkov@9858: assert(memory_size.is_set(), "invariant"); apetushkov@9858: assert(global_buffer_size.is_set(), "invariant"); apetushkov@9858: assert(num_global_buffers.is_set(), "invariant"); apetushkov@9858: const julong calc_size = global_buffer_size.value()._size * (julong)num_global_buffers.value(); apetushkov@9858: if (calc_size != memory_size.value()._size) { apetushkov@9858: // ambiguous apetushkov@9858: log_set_value(global_buffer_size); apetushkov@9858: tty->print_cr( apetushkov@9858: "Value specified for option \"%s\" is " JLONG_FORMAT, apetushkov@9858: num_global_buffers.name(), num_global_buffers.value()); apetushkov@9858: log_set_value(memory_size); apetushkov@9858: tty->print_cr( apetushkov@9858: "These values are causing an ambiguity when trying to determine how much memory to use"); apetushkov@9858: tty->print_cr("\"%s\" * \"%s\" do not equal \"%s\"", apetushkov@9858: global_buffer_size.name(), apetushkov@9858: num_global_buffers.name(), apetushkov@9858: memory_size.name()); apetushkov@9858: tty->print_cr( apetushkov@9858: "Try to remove one of the involved options or make sure they are unambigous"); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: static bool ensure_minimum_count(Argument& buffer_count_argument, jlong min_count) { apetushkov@9858: if (buffer_count_argument.value() < min_count) { apetushkov@9858: tty->print_cr( apetushkov@9858: "Value specified for option \"%s\" is " JLONG_FORMAT, apetushkov@9858: buffer_count_argument.name(), buffer_count_argument.value()); apetushkov@9858: tty->print_cr( apetushkov@9858: "This value is lower than the minimum required number " JLONG_FORMAT, apetushkov@9858: min_count); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: // global buffer size and num global buffers specified apetushkov@9858: // ensure that particular combination to be ihigher than minimum memory size apetushkov@9858: template apetushkov@9858: static bool ensure_calculated_gteq(MemoryArg& global_buffer_size, NumberArg& num_global_buffers, julong min_value) { apetushkov@9858: assert(global_buffer_size.is_set(), "invariant"); apetushkov@9858: assert(num_global_buffers.is_set(), "invariant"); apetushkov@9858: const julong calc_size = global_buffer_size.value()._size * (julong)num_global_buffers.value(); apetushkov@9858: if (calc_size < min_value) { apetushkov@9858: log_set_value(global_buffer_size); apetushkov@9858: tty->print_cr( apetushkov@9858: "Value specified for option \"%s\" is " JLONG_FORMAT, apetushkov@9858: num_global_buffers.name(), num_global_buffers.value()); apetushkov@9858: tty->print_cr("\"%s\" * \"%s\" (" JULONG_FORMAT apetushkov@9858: ") is lower than minimum memory size required " JULONG_FORMAT, apetushkov@9858: global_buffer_size.name(), apetushkov@9858: num_global_buffers.name(), apetushkov@9858: calc_size, apetushkov@9858: min_value); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: static bool ensure_first_gteq_second(Argument& first_argument, Argument& second_argument) { apetushkov@9858: if (second_argument.value()._size > first_argument.value()._size) { apetushkov@9858: log_set_value(first_argument); apetushkov@9858: log_set_value(second_argument); apetushkov@9858: tty->print_cr( apetushkov@9858: "The value for option \"%s\" should not be larger than the value specified for option \"%s\"", apetushkov@9858: second_argument.name(), first_argument.name()); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool valid_memory_relations(const JfrMemoryOptions& options) { apetushkov@9858: if (options.global_buffer_size_configured) { apetushkov@9858: if (options.memory_size_configured) { apetushkov@9858: if (!ensure_first_gteq_second(_dcmd_memorysize, _dcmd_globalbuffersize)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: if (options.thread_buffer_size_configured) { apetushkov@9858: if (!ensure_first_gteq_second(_dcmd_globalbuffersize, _dcmd_threadbuffersize)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: if (options.buffer_count_configured) { apetushkov@9858: if (!ensure_calculated_gteq(_dcmd_globalbuffersize, _dcmd_numglobalbuffers, MIN_MEMORY_SIZE)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static void post_process_adjusted_memory_options(const JfrMemoryOptions& options) { apetushkov@9858: assert(options.memory_size >= MIN_MEMORY_SIZE, "invariant"); apetushkov@9858: assert(options.global_buffer_size >= MIN_GLOBAL_BUFFER_SIZE, "invariant"); apetushkov@9858: assert(options.buffer_count >= MIN_BUFFER_COUNT, "invariant"); apetushkov@9858: assert(options.thread_buffer_size >= MIN_THREAD_BUFFER_SIZE, "invariant"); apetushkov@9858: log_adjustments(_dcmd_memorysize, options.memory_size, "Memory"); apetushkov@9858: log_adjustments(_dcmd_globalbuffersize, options.global_buffer_size, "Global buffer"); apetushkov@9858: log_adjustments(_dcmd_threadbuffersize, options.thread_buffer_size, "Thread local buffer"); apetushkov@9858: if (LogJFR && Verbose) tty->print_cr("Number of global buffers (original) " JLONG_FORMAT " (user defined: %s)", apetushkov@9858: _dcmd_numglobalbuffers.value(), apetushkov@9858: _dcmd_numglobalbuffers.is_set() ? "true" : "false"); apetushkov@9858: if (LogJFR && Verbose) tty->print_cr( "Number of global buffers (adjusted) " JULONG_FORMAT " (modified: %s)", apetushkov@9858: options.buffer_count, apetushkov@9858: _dcmd_numglobalbuffers.value() != (jlong)options.buffer_count ? "true" : "false"); apetushkov@9858: if (LogJFR && Verbose) tty->print_cr("Number of global buffers (adjustment) %s" JLONG_FORMAT, apetushkov@9858: (jlong)options.buffer_count < _dcmd_numglobalbuffers.value() ? "" : "+", apetushkov@9858: (jlong)options.buffer_count - _dcmd_numglobalbuffers.value()); apetushkov@9858: apetushkov@9858: MemorySizeArgument adjusted_memory_size; apetushkov@9858: adjusted_memory_size._val = divide_with_user_unit(_dcmd_memorysize, options.memory_size); apetushkov@9858: adjusted_memory_size._multiplier = _dcmd_memorysize.value()._multiplier; apetushkov@9858: adjusted_memory_size._size = options.memory_size; apetushkov@9858: apetushkov@9858: MemorySizeArgument adjusted_global_buffer_size; apetushkov@9858: adjusted_global_buffer_size._val = divide_with_user_unit(_dcmd_globalbuffersize, options.global_buffer_size); apetushkov@9858: adjusted_global_buffer_size._multiplier = _dcmd_globalbuffersize.value()._multiplier; apetushkov@9858: adjusted_global_buffer_size._size = options.global_buffer_size; apetushkov@9858: apetushkov@9858: MemorySizeArgument adjusted_thread_buffer_size; apetushkov@9858: adjusted_thread_buffer_size._val = divide_with_user_unit(_dcmd_threadbuffersize, options.thread_buffer_size); apetushkov@9858: adjusted_thread_buffer_size._multiplier = _dcmd_threadbuffersize.value()._multiplier; apetushkov@9858: adjusted_thread_buffer_size._size = options.thread_buffer_size; apetushkov@9858: apetushkov@9858: // store back to dcmd apetushkov@9858: _dcmd_memorysize.set_value(adjusted_memory_size); apetushkov@9858: _dcmd_memorysize.set_is_set(true); apetushkov@9858: _dcmd_globalbuffersize.set_value(adjusted_global_buffer_size); apetushkov@9858: _dcmd_globalbuffersize.set_is_set(true); apetushkov@9858: _dcmd_numglobalbuffers.set_value((jlong)options.buffer_count); apetushkov@9858: _dcmd_numglobalbuffers.set_is_set(true); apetushkov@9858: _dcmd_threadbuffersize.set_value(adjusted_thread_buffer_size); apetushkov@9858: _dcmd_threadbuffersize.set_is_set(true); apetushkov@9858: } apetushkov@9858: apetushkov@9858: static void initialize_memory_options_from_dcmd(JfrMemoryOptions& options) { apetushkov@9858: options.memory_size = _dcmd_memorysize.value()._size; apetushkov@9858: options.global_buffer_size = MAX2(_dcmd_globalbuffersize.value()._size, (julong)os::vm_page_size()); apetushkov@9858: options.buffer_count = (julong)_dcmd_numglobalbuffers.value(); apetushkov@9858: options.thread_buffer_size = MAX2(_dcmd_threadbuffersize.value()._size, (julong)os::vm_page_size()); apetushkov@9858: // determine which options have been explicitly set apetushkov@9858: options.memory_size_configured = _dcmd_memorysize.is_set(); apetushkov@9858: options.global_buffer_size_configured = _dcmd_globalbuffersize.is_set(); apetushkov@9858: options.buffer_count_configured = _dcmd_numglobalbuffers.is_set(); apetushkov@9858: options.thread_buffer_size_configured = _dcmd_threadbuffersize.is_set(); apetushkov@9858: assert(options.memory_size >= MIN_MEMORY_SIZE, "invariant"); apetushkov@9858: assert(options.global_buffer_size >= MIN_GLOBAL_BUFFER_SIZE, "invariant"); apetushkov@9858: assert(options.buffer_count >= MIN_BUFFER_COUNT, "invariant"); apetushkov@9858: assert(options.thread_buffer_size >= MIN_THREAD_BUFFER_SIZE, "invariant"); apetushkov@9858: } apetushkov@9858: apetushkov@9858: template apetushkov@9858: static bool ensure_gteq(Argument& memory_argument, const jlong value) { apetushkov@9858: if ((jlong)memory_argument.value()._size < value) { apetushkov@9858: log_set_value(memory_argument); apetushkov@9858: log_lower_than_min_value(memory_argument, value); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static bool ensure_valid_minimum_sizes() { apetushkov@9858: // ensure valid minimum memory sizes apetushkov@9858: if (_dcmd_memorysize.is_set()) { apetushkov@9858: if (!ensure_gteq(_dcmd_memorysize, MIN_MEMORY_SIZE)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: if (_dcmd_globalbuffersize.is_set()) { apetushkov@9858: if (!ensure_gteq(_dcmd_globalbuffersize, MIN_GLOBAL_BUFFER_SIZE)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: if (_dcmd_numglobalbuffers.is_set()) { apetushkov@9858: if (!ensure_minimum_count(_dcmd_numglobalbuffers, MIN_BUFFER_COUNT)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: if (_dcmd_threadbuffersize.is_set()) { apetushkov@9858: if (!ensure_gteq(_dcmd_threadbuffersize, MIN_THREAD_BUFFER_SIZE)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: /** apetushkov@9858: * Starting with the initial set of memory values from the user, apetushkov@9858: * sanitize, enforce min/max rules and adjust to a set of consistent options. apetushkov@9858: * apetushkov@9858: * Adjusted memory sizes will be page aligned. apetushkov@9858: */ apetushkov@9858: bool JfrOptionSet::adjust_memory_options() { apetushkov@9858: if (!ensure_valid_minimum_sizes()) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: JfrMemoryOptions options; apetushkov@9858: initialize_memory_options_from_dcmd(options); apetushkov@9858: if (!valid_memory_relations(options)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: if (!JfrMemorySizer::adjust_options(&options)) { apetushkov@9858: if (!check_for_ambiguity(_dcmd_memorysize, _dcmd_globalbuffersize, _dcmd_numglobalbuffers)) { apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: } apetushkov@9858: post_process_adjusted_memory_options(options); apetushkov@9858: return true; apetushkov@9858: } apetushkov@9858: apetushkov@9858: bool JfrOptionSet::parse_flight_recorder_option(const JavaVMOption** option, char* delimiter) { apetushkov@9858: assert(option != NULL, "invariant"); apetushkov@9858: assert(delimiter != NULL, "invariant"); apetushkov@9858: assert((*option)->optionString != NULL, "invariant"); apetushkov@9858: assert(strncmp((*option)->optionString, "-XX:FlightRecorderOptions", 25) == 0, "invariant"); apetushkov@9858: if (*delimiter == '\0') { apetushkov@9858: // -XX:FlightRecorderOptions without any delimiter and values apetushkov@9858: } else { apetushkov@9858: // -XX:FlightRecorderOptions[=|:] apetushkov@9858: // set delimiter to '=' apetushkov@9858: *delimiter = '='; apetushkov@9858: } apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: apetushkov@9858: static GrowableArray* startup_recording_options_array = NULL; apetushkov@9858: apetushkov@9858: bool JfrOptionSet::parse_start_flight_recording_option(const JavaVMOption** option, char* delimiter) { apetushkov@9858: assert(option != NULL, "invariant"); apetushkov@9858: assert(delimiter != NULL, "invariant"); apetushkov@9858: assert((*option)->optionString != NULL, "invariant"); apetushkov@9858: assert(strncmp((*option)->optionString, "-XX:StartFlightRecording", 24) == 0, "invariant"); apetushkov@9858: const char* value = NULL; apetushkov@9858: if (*delimiter == '\0') { apetushkov@9858: // -XX:StartFlightRecording without any delimiter and values apetushkov@9858: // Add dummy value "dumponexit=false" so -XX:StartFlightRecording can be used without explicit values. apetushkov@9858: // The existing option->optionString points to stack memory so no need to deallocate. apetushkov@9858: const_cast(*option)->optionString = (char*)"-XX:StartFlightRecording=dumponexit=false"; apetushkov@9858: value = (*option)->optionString + 25; apetushkov@9858: } else { apetushkov@9858: // -XX:StartFlightRecording[=|:] apetushkov@9858: // set delimiter to '=' apetushkov@9858: *delimiter = '='; apetushkov@9858: value = delimiter + 1; apetushkov@9858: } apetushkov@9858: assert(value != NULL, "invariant"); apetushkov@9858: const size_t value_length = strlen(value); apetushkov@9858: apetushkov@9858: if (startup_recording_options_array == NULL) { apetushkov@9858: startup_recording_options_array = new (ResourceObj::C_HEAP, mtTracing) GrowableArray(8, true, mtTracing); apetushkov@9858: } apetushkov@9858: assert(startup_recording_options_array != NULL, "invariant"); apetushkov@9858: char* const startup_value = NEW_C_HEAP_ARRAY(char, value_length + 1, mtTracing); apetushkov@9858: strncpy(startup_value, value, value_length + 1); apetushkov@9858: assert(strncmp(startup_value, value, value_length) == 0, "invariant"); apetushkov@9858: startup_recording_options_array->append(startup_value); apetushkov@9858: return false; apetushkov@9858: } apetushkov@9858: apetushkov@9858: const GrowableArray* JfrOptionSet::startup_recording_options() { apetushkov@9858: return startup_recording_options_array; apetushkov@9858: } apetushkov@9858: apetushkov@9858: void JfrOptionSet::release_startup_recording_options() { apetushkov@9858: if (startup_recording_options_array != NULL) { apetushkov@9858: const int length = startup_recording_options_array->length(); apetushkov@9858: for (int i = 0; i < length; ++i) { apetushkov@9858: FREE_C_HEAP_ARRAY(char, startup_recording_options_array->at(i), mtTracing); apetushkov@9858: } apetushkov@9858: delete startup_recording_options_array; apetushkov@9858: startup_recording_options_array = NULL; apetushkov@9858: } apetushkov@9858: }