aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@1: /* aoqi@1: * This file has been modified by Loongson Technology in 2015. These aoqi@1: * modifications are Copyright (c) 2015 Loongson Technology, and are made aoqi@1: * available on the same license terms set forth above. aoqi@1: */ aoqi@1: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/java.hpp" aoqi@0: #include "runtime/javaCalls.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: #include "runtime/statSampler.hpp" aoqi@0: #ifdef TARGET_ARCH_x86 aoqi@0: # include "vm_version_x86.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_sparc aoqi@0: # include "vm_version_sparc.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_zero aoqi@0: # include "vm_version_zero.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_arm aoqi@0: # include "vm_version_arm.hpp" aoqi@0: #endif aoqi@0: #ifdef TARGET_ARCH_ppc aoqi@0: # include "vm_version_ppc.hpp" aoqi@0: #endif aoqi@1: #ifdef TARGET_ARCH_mips aoqi@1: # include "vm_version_mips.hpp" aoqi@1: #endif aoqi@0: aoqi@0: // -------------------------------------------------------- aoqi@0: // StatSamplerTask aoqi@0: aoqi@0: class StatSamplerTask : public PeriodicTask { aoqi@0: public: aoqi@0: StatSamplerTask(int interval_time) : PeriodicTask(interval_time) {} aoqi@0: void task() { StatSampler::collect_sample(); } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: //---------------------------------------------------------- aoqi@0: // Implementation of StatSampler aoqi@0: aoqi@0: StatSamplerTask* StatSampler::_task = NULL; aoqi@0: PerfDataList* StatSampler::_sampled = NULL; aoqi@0: aoqi@0: /* aoqi@0: * the initialize method is called from the engage() method aoqi@0: * and is responsible for initializing various global variables. aoqi@0: */ aoqi@0: void StatSampler::initialize() { aoqi@0: aoqi@0: if (!UsePerfData) return; aoqi@0: aoqi@0: // create performance data that could not be created prior aoqi@0: // to vm_init_globals() or otherwise have no logical home. aoqi@0: aoqi@0: create_misc_perfdata(); aoqi@0: aoqi@0: // get copy of the sampled list aoqi@0: _sampled = PerfDataManager::sampled(); aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * The engage() method is called at initialization time via aoqi@0: * Thread::create_vm() to initialize the StatSampler and aoqi@0: * register it with the WatcherThread as a periodic task. aoqi@0: */ aoqi@0: void StatSampler::engage() { aoqi@0: aoqi@0: if (!UsePerfData) return; aoqi@0: aoqi@0: if (!is_active()) { aoqi@0: aoqi@0: initialize(); aoqi@0: aoqi@0: // start up the periodic task aoqi@0: _task = new StatSamplerTask(PerfDataSamplingInterval); aoqi@0: _task->enroll(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /* aoqi@0: * the disengage() method is responsible for deactivating the periodic aoqi@0: * task and, if logging was enabled, for logging the final sample. This aoqi@0: * method is called from before_exit() in java.cpp and is only called aoqi@0: * after the WatcherThread has been stopped. aoqi@0: */ aoqi@0: void StatSampler::disengage() { aoqi@0: aoqi@0: if (!UsePerfData) return; aoqi@0: aoqi@0: if (!is_active()) aoqi@0: return; aoqi@0: aoqi@0: // remove StatSamplerTask aoqi@0: _task->disenroll(); aoqi@0: delete _task; aoqi@0: _task = NULL; aoqi@0: aoqi@0: // force a final sample aoqi@0: sample_data(_sampled); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * the destroy method is responsible for releasing any resources used by aoqi@0: * the StatSampler prior to shutdown of the VM. this method is called from aoqi@0: * before_exit() in java.cpp and is only called after the WatcherThread aoqi@0: * has stopped. aoqi@0: */ aoqi@0: void StatSampler::destroy() { aoqi@0: aoqi@0: if (!UsePerfData) return; aoqi@0: aoqi@0: if (_sampled != NULL) { aoqi@0: delete(_sampled); aoqi@0: _sampled = NULL; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * The sample_data() method is responsible for sampling the aoqi@0: * the data value for each PerfData instance in the given list. aoqi@0: */ aoqi@0: void StatSampler::sample_data(PerfDataList* list) { aoqi@0: aoqi@0: assert(list != NULL, "null list unexpected"); aoqi@0: aoqi@0: for (int index = 0; index < list->length(); index++) { aoqi@0: PerfData* item = list->at(index); aoqi@0: item->sample(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * the collect_sample() method is the method invoked by the aoqi@0: * WatcherThread via the PeriodicTask::task() method. This method aoqi@0: * is responsible for collecting data samples from sampled aoqi@0: * PerfData instances every PerfDataSamplingInterval milliseconds. aoqi@0: * It is also responsible for logging the requested set of aoqi@0: * PerfData instances every _sample_count milliseconds. While aoqi@0: * logging data, it will output a column header after every _print_header aoqi@0: * rows of data have been logged. aoqi@0: */ aoqi@0: void StatSampler::collect_sample() { aoqi@0: aoqi@0: // future - check for new PerfData objects. PerfData objects might aoqi@0: // get added to the PerfDataManager lists after we have already aoqi@0: // built our local copies. aoqi@0: // aoqi@0: // if (PerfDataManager::count() > previous) { aoqi@0: // // get a new copy of the sampled list aoqi@0: // if (_sampled != NULL) { aoqi@0: // delete(_sampled); aoqi@0: // _sampled = NULL; aoqi@0: // } aoqi@0: // _sampled = PerfDataManager::sampled(); aoqi@0: // } aoqi@0: aoqi@0: assert(_sampled != NULL, "list not initialized"); aoqi@0: aoqi@0: sample_data(_sampled); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * method to upcall into Java to return the value of the specified aoqi@0: * property as a utf8 string, or NULL if does not exist. The caller aoqi@0: * is responsible for setting a ResourceMark for proper cleanup of aoqi@0: * the utf8 strings. aoqi@0: */ aoqi@0: const char* StatSampler::get_system_property(const char* name, TRAPS) { aoqi@0: aoqi@0: // setup the arguments to getProperty aoqi@0: Handle key_str = java_lang_String::create_from_str(name, CHECK_NULL); aoqi@0: aoqi@0: // return value aoqi@0: JavaValue result(T_OBJECT); aoqi@0: aoqi@0: // public static String getProperty(String key, String def); aoqi@0: JavaCalls::call_static(&result, aoqi@0: KlassHandle(THREAD, SystemDictionary::System_klass()), aoqi@0: vmSymbols::getProperty_name(), aoqi@0: vmSymbols::string_string_signature(), aoqi@0: key_str, aoqi@0: CHECK_NULL); aoqi@0: aoqi@0: oop value_oop = (oop)result.get_jobject(); aoqi@0: if (value_oop == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: // convert Java String to utf8 string aoqi@0: char* value = java_lang_String::as_utf8_string(value_oop); aoqi@0: aoqi@0: return value; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * The list of System Properties that have corresponding PerfData aoqi@0: * string instrumentation created by retrieving the named property's aoqi@0: * value from System.getProperty() and unconditionally creating a aoqi@0: * PerfStringConstant object initialized to the retreived value. This aoqi@0: * is not an exhustive list of Java properties with corresponding string aoqi@0: * instrumentation as the create_system_property_instrumentation() method aoqi@0: * creates other property based instrumentation conditionally. aoqi@0: */ aoqi@0: aoqi@0: // stable interface, supported counters aoqi@0: static const char* property_counters_ss[] = { aoqi@0: "java.vm.specification.version", aoqi@0: "java.vm.specification.name", aoqi@0: "java.vm.specification.vendor", aoqi@0: "java.vm.version", aoqi@0: "java.vm.name", aoqi@0: "java.vm.vendor", aoqi@0: "java.vm.info", aoqi@0: "java.library.path", aoqi@0: "java.class.path", aoqi@0: "java.endorsed.dirs", aoqi@0: "java.ext.dirs", aoqi@0: "java.version", aoqi@0: "java.home", aoqi@0: NULL aoqi@0: }; aoqi@0: aoqi@0: // unstable interface, supported counters aoqi@0: static const char* property_counters_us[] = { aoqi@0: NULL aoqi@0: }; aoqi@0: aoqi@0: // unstable interface, unsupported counters aoqi@0: static const char* property_counters_uu[] = { aoqi@0: "sun.boot.class.path", aoqi@0: "sun.boot.library.path", aoqi@0: NULL aoqi@0: }; aoqi@0: aoqi@0: typedef struct { aoqi@0: const char** property_list; aoqi@0: CounterNS name_space; aoqi@0: } PropertyCounters; aoqi@0: aoqi@0: static PropertyCounters property_counters[] = { aoqi@0: { property_counters_ss, JAVA_PROPERTY }, aoqi@0: { property_counters_us, COM_PROPERTY }, aoqi@0: { property_counters_uu, SUN_PROPERTY }, aoqi@0: { NULL, SUN_PROPERTY } aoqi@0: }; aoqi@0: aoqi@0: aoqi@0: /* aoqi@0: * Method to create PerfData string instruments that contain the values aoqi@0: * of various system properties. String instruments are created for each aoqi@0: * property specified in the property lists provided in property_counters[]. aoqi@0: * Property counters have a counter name space prefix prepended to the aoqi@0: * property name as indicated in property_counters[]. aoqi@0: */ aoqi@0: void StatSampler::create_system_property_instrumentation(TRAPS) { aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: for (int i = 0; property_counters[i].property_list != NULL; i++) { aoqi@0: aoqi@0: for (int j = 0; property_counters[i].property_list[j] != NULL; j++) { aoqi@0: aoqi@0: const char* property_name = property_counters[i].property_list[j]; aoqi@0: assert(property_name != NULL, "property name should not be NULL"); aoqi@0: aoqi@0: const char* value = get_system_property(property_name, CHECK); aoqi@0: aoqi@0: // the property must exist aoqi@0: assert(value != NULL, "property name should be valid"); aoqi@0: aoqi@0: if (value != NULL) { aoqi@0: // create the property counter aoqi@0: PerfDataManager::create_string_constant(property_counters[i].name_space, aoqi@0: property_name, value, CHECK); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * The create_misc_perfdata() method provides a place to create aoqi@0: * PerfData instances that would otherwise have no better place aoqi@0: * to exist. aoqi@0: */ aoqi@0: void StatSampler::create_misc_perfdata() { aoqi@0: aoqi@0: ResourceMark rm; aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: // numeric constants aoqi@0: aoqi@0: // frequency of the native high resolution timer aoqi@0: PerfDataManager::create_constant(SUN_OS, "hrt.frequency", aoqi@0: PerfData::U_Hertz, os::elapsed_frequency(), aoqi@0: CHECK); aoqi@0: aoqi@0: // string constants aoqi@0: aoqi@0: // create string instrumentation for various Java properties. aoqi@0: create_system_property_instrumentation(CHECK); aoqi@0: aoqi@0: // hotspot flags (from .hotspotrc) and args (from command line) aoqi@0: // aoqi@0: PerfDataManager::create_string_constant(JAVA_RT, "vmFlags", aoqi@0: Arguments::jvm_flags(), CHECK); aoqi@0: PerfDataManager::create_string_constant(JAVA_RT, "vmArgs", aoqi@0: Arguments::jvm_args(), CHECK); aoqi@0: aoqi@0: // java class name/jar file and arguments to main class aoqi@0: // note: name is cooridnated with launcher and Arguments.cpp aoqi@0: PerfDataManager::create_string_constant(SUN_RT, "javaCommand", aoqi@0: Arguments::java_command(), CHECK); aoqi@0: aoqi@0: // the Java VM Internal version string aoqi@0: PerfDataManager::create_string_constant(SUN_RT, "internalVersion", aoqi@0: VM_Version::internal_vm_info_string(), aoqi@0: CHECK); aoqi@0: aoqi@0: // create sampled instrumentation objects aoqi@0: create_sampled_perfdata(); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * helper class to provide for sampling of the elapsed_counter value aoqi@0: * maintained in the OS class. aoqi@0: */ aoqi@0: class HighResTimeSampler : public PerfSampleHelper { aoqi@0: public: aoqi@0: jlong take_sample() { return os::elapsed_counter(); } aoqi@0: }; aoqi@0: aoqi@0: /* aoqi@0: * the create_sampled_perdata() method provides a place to instantiate aoqi@0: * sampled PerfData instances that would otherwise have no better place aoqi@0: * to exist. aoqi@0: */ aoqi@0: void StatSampler::create_sampled_perfdata() { aoqi@0: aoqi@0: EXCEPTION_MARK; aoqi@0: aoqi@0: // setup sampling of the elapsed time counter maintained in the aoqi@0: // the os class. This counter can be used as either a time stamp aoqi@0: // for each logged entry or as a liveness indicator for the VM. aoqi@0: PerfSampleHelper* psh = new HighResTimeSampler(); aoqi@0: PerfDataManager::create_counter(SUN_OS, "hrt.ticks", aoqi@0: PerfData::U_Ticks, psh, CHECK); aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * the statSampler_exit() function is called from os_init.cpp on aoqi@0: * exit of the vm. aoqi@0: */ aoqi@0: void statSampler_exit() { aoqi@0: aoqi@0: if (!UsePerfData) return; aoqi@0: aoqi@0: StatSampler::destroy(); aoqi@0: }