1/* 2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25#include "precompiled.hpp" 26#include "classfile/systemDictionary.hpp" 27#include "classfile/vmSymbols.hpp" 28#include "memory/allocation.inline.hpp" 29#include "memory/resourceArea.hpp" 30#include "oops/oop.inline.hpp" 31#include "runtime/arguments.hpp" 32#include "runtime/java.hpp" 33#include "runtime/javaCalls.hpp" 34#include "runtime/os.hpp" 35#include "runtime/statSampler.hpp" 36#include "runtime/vm_version.hpp" 37 38// -------------------------------------------------------- 39// StatSamplerTask 40 41class StatSamplerTask : public PeriodicTask { 42 public: 43 StatSamplerTask(int interval_time) : PeriodicTask(interval_time) {} 44 void task() { StatSampler::collect_sample(); } 45}; 46 47 48//---------------------------------------------------------- 49// Implementation of StatSampler 50 51StatSamplerTask* StatSampler::_task = NULL; 52PerfDataList* StatSampler::_sampled = NULL; 53 54/* 55 * the initialize method is called from the engage() method 56 * and is responsible for initializing various global variables. 57 */ 58void StatSampler::initialize() { 59 60 if (!UsePerfData) return; 61 62 // create performance data that could not be created prior 63 // to vm_init_globals() or otherwise have no logical home. 64 65 create_misc_perfdata(); 66 67 // get copy of the sampled list 68 _sampled = PerfDataManager::sampled(); 69 70} 71 72/* 73 * The engage() method is called at initialization time via 74 * Thread::create_vm() to initialize the StatSampler and 75 * register it with the WatcherThread as a periodic task. 76 */ 77void StatSampler::engage() { 78 79 if (!UsePerfData) return; 80 81 if (!is_active()) { 82 83 initialize(); 84 85 // start up the periodic task 86 _task = new StatSamplerTask(PerfDataSamplingInterval); 87 _task->enroll(); 88 } 89} 90 91 92/* 93 * the disengage() method is responsible for deactivating the periodic 94 * task and, if logging was enabled, for logging the final sample. This 95 * method is called from before_exit() in java.cpp and is only called 96 * after the WatcherThread has been stopped. 97 */ 98void StatSampler::disengage() { 99 100 if (!UsePerfData) return; 101 102 if (!is_active()) 103 return; 104 105 // remove StatSamplerTask 106 _task->disenroll(); 107 delete _task; 108 _task = NULL; 109 110 // force a final sample 111 sample_data(_sampled); 112} 113 114/* 115 * the destroy method is responsible for releasing any resources used by 116 * the StatSampler prior to shutdown of the VM. this method is called from 117 * before_exit() in java.cpp and is only called after the WatcherThread 118 * has stopped. 119 */ 120void StatSampler::destroy() { 121 122 if (!UsePerfData) return; 123 124 if (_sampled != NULL) { 125 delete(_sampled); 126 _sampled = NULL; 127 } 128} 129 130/* 131 * The sample_data() method is responsible for sampling the 132 * the data value for each PerfData instance in the given list. 133 */ 134void StatSampler::sample_data(PerfDataList* list) { 135 136 assert(list != NULL, "null list unexpected"); 137 138 for (int index = 0; index < list->length(); index++) { 139 PerfData* item = list->at(index); 140 item->sample(); 141 } 142} 143 144/* 145 * the collect_sample() method is the method invoked by the 146 * WatcherThread via the PeriodicTask::task() method. This method 147 * is responsible for collecting data samples from sampled 148 * PerfData instances every PerfDataSamplingInterval milliseconds. 149 * It is also responsible for logging the requested set of 150 * PerfData instances every _sample_count milliseconds. While 151 * logging data, it will output a column header after every _print_header 152 * rows of data have been logged. 153 */ 154void StatSampler::collect_sample() { 155 156 // future - check for new PerfData objects. PerfData objects might 157 // get added to the PerfDataManager lists after we have already 158 // built our local copies. 159 // 160 // if (PerfDataManager::count() > previous) { 161 // // get a new copy of the sampled list 162 // if (_sampled != NULL) { 163 // delete(_sampled); 164 // _sampled = NULL; 165 // } 166 // _sampled = PerfDataManager::sampled(); 167 // } 168 169 assert(_sampled != NULL, "list not initialized"); 170 171 sample_data(_sampled); 172} 173 174/* 175 * method to upcall into Java to return the value of the specified 176 * property as a utf8 string, or NULL if does not exist. The caller 177 * is responsible for setting a ResourceMark for proper cleanup of 178 * the utf8 strings. 179 */ 180const char* StatSampler::get_system_property(const char* name, TRAPS) { 181 182 // setup the arguments to getProperty 183 Handle key_str = java_lang_String::create_from_str(name, CHECK_NULL); 184 185 // return value 186 JavaValue result(T_OBJECT); 187 188 // public static String getProperty(String key, String def); 189 JavaCalls::call_static(&result, 190 KlassHandle(THREAD, SystemDictionary::System_klass()), 191 vmSymbols::getProperty_name(), 192 vmSymbols::string_string_signature(), 193 key_str, 194 CHECK_NULL); 195 196 oop value_oop = (oop)result.get_jobject(); 197 if (value_oop == NULL) { 198 return NULL; 199 } 200 201 // convert Java String to utf8 string 202 char* value = java_lang_String::as_utf8_string(value_oop); 203 204 return value; 205} 206 207/* 208 * The list of System Properties that have corresponding PerfData 209 * string instrumentation created by retrieving the named property's 210 * value from System.getProperty() and unconditionally creating a 211 * PerfStringConstant object initialized to the retrieved value. This 212 * is not an exhaustive list of Java properties with corresponding string 213 * instrumentation as the create_system_property_instrumentation() method 214 * creates other property based instrumentation conditionally. 215 */ 216 217// stable interface, supported counters 218static const char* property_counters_ss[] = { 219 "java.vm.specification.version", 220 "java.vm.specification.name", 221 "java.vm.specification.vendor", 222 "java.vm.version", 223 "java.vm.name", 224 "java.vm.vendor", 225 "java.vm.info", 226 "jdk.debug", 227 "java.library.path", 228 "java.class.path", 229 "java.version", 230 "java.home", 231 NULL 232}; 233 234// unstable interface, supported counters 235static const char* property_counters_us[] = { 236 NULL 237}; 238 239// unstable interface, unsupported counters 240static const char* property_counters_uu[] = { 241 "sun.boot.library.path", 242 NULL 243}; 244 245typedef struct { 246 const char** property_list; 247 CounterNS name_space; 248} PropertyCounters; 249 250static PropertyCounters property_counters[] = { 251 { property_counters_ss, JAVA_PROPERTY }, 252 { property_counters_us, COM_PROPERTY }, 253 { property_counters_uu, SUN_PROPERTY }, 254 { NULL, SUN_PROPERTY } 255}; 256 257 258/* 259 * Method to create PerfData string instruments that contain the values 260 * of various system properties. String instruments are created for each 261 * property specified in the property lists provided in property_counters[]. 262 * Property counters have a counter name space prefix prepended to the 263 * property name as indicated in property_counters[]. 264 */ 265void StatSampler::create_system_property_instrumentation(TRAPS) { 266 267 ResourceMark rm; 268 269 for (int i = 0; property_counters[i].property_list != NULL; i++) { 270 271 for (int j = 0; property_counters[i].property_list[j] != NULL; j++) { 272 273 const char* property_name = property_counters[i].property_list[j]; 274 assert(property_name != NULL, "property name should not be NULL"); 275 276 const char* value = get_system_property(property_name, CHECK); 277 278 // the property must exist 279 assert(value != NULL, "property name should be valid"); 280 281 if (value != NULL) { 282 // create the property counter 283 PerfDataManager::create_string_constant(property_counters[i].name_space, 284 property_name, value, CHECK); 285 } 286 } 287 } 288} 289 290/* 291 * The create_misc_perfdata() method provides a place to create 292 * PerfData instances that would otherwise have no better place 293 * to exist. 294 */ 295void StatSampler::create_misc_perfdata() { 296 297 ResourceMark rm; 298 EXCEPTION_MARK; 299 300 // numeric constants 301 302 // frequency of the native high resolution timer 303 PerfDataManager::create_constant(SUN_OS, "hrt.frequency", 304 PerfData::U_Hertz, os::elapsed_frequency(), 305 CHECK); 306 307 // string constants 308 309 // create string instrumentation for various Java properties. 310 create_system_property_instrumentation(CHECK); 311 312 // HotSpot flags (from .hotspotrc) and args (from command line) 313 // 314 PerfDataManager::create_string_constant(JAVA_RT, "vmFlags", 315 Arguments::jvm_flags(), CHECK); 316 PerfDataManager::create_string_constant(JAVA_RT, "vmArgs", 317 Arguments::jvm_args(), CHECK); 318 319 // java class name/jar file and arguments to main class 320 // note: name is cooridnated with launcher and Arguments.cpp 321 PerfDataManager::create_string_constant(SUN_RT, "javaCommand", 322 Arguments::java_command(), CHECK); 323 324 // the Java VM Internal version string 325 PerfDataManager::create_string_constant(SUN_RT, "internalVersion", 326 VM_Version::internal_vm_info_string(), 327 CHECK); 328 329 // create sampled instrumentation objects 330 create_sampled_perfdata(); 331} 332 333/* 334 * helper class to provide for sampling of the elapsed_counter value 335 * maintained in the OS class. 336 */ 337class HighResTimeSampler : public PerfSampleHelper { 338 public: 339 jlong take_sample() { return os::elapsed_counter(); } 340}; 341 342/* 343 * the create_sampled_perdata() method provides a place to instantiate 344 * sampled PerfData instances that would otherwise have no better place 345 * to exist. 346 */ 347void StatSampler::create_sampled_perfdata() { 348 349 EXCEPTION_MARK; 350 351 // setup sampling of the elapsed time counter maintained in the 352 // the os class. This counter can be used as either a time stamp 353 // for each logged entry or as a liveness indicator for the VM. 354 PerfSampleHelper* psh = new HighResTimeSampler(); 355 PerfDataManager::create_counter(SUN_OS, "hrt.ticks", 356 PerfData::U_Ticks, psh, CHECK); 357} 358 359/* 360 * the statSampler_exit() function is called from os_init.cpp on 361 * exit of the vm. 362 */ 363void statSampler_exit() { 364 365 if (!UsePerfData) return; 366 367 StatSampler::destroy(); 368} 369