src/share/vm/runtime/statSampler.cpp

Mon, 03 Jan 2011 14:09:11 -0500

author
coleenp
date
Mon, 03 Jan 2011 14:09:11 -0500
changeset 2418
36c186bcc085
parent 2314
f95d63e2154a
child 2497
3582bf76420e
permissions
-rw-r--r--

6302804: Hotspot VM dies ungraceful death when C heap is exhausted in various places.
Summary: enhance the error reporting mechanism to help user to fix the problem rather than making it look like a VM error.
Reviewed-by: kvn, kamg

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

mercurial