perfData.cpp revision 8943:f44314157fcb
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/vmSymbols.hpp" 27#include "oops/oop.inline.hpp" 28#include "runtime/handles.inline.hpp" 29#include "runtime/java.hpp" 30#include "runtime/mutex.hpp" 31#include "runtime/mutexLocker.hpp" 32#include "runtime/os.hpp" 33#include "runtime/perfData.hpp" 34#include "utilities/exceptions.hpp" 35#include "utilities/globalDefinitions.hpp" 36 37PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 38 39PerfDataList* PerfDataManager::_all = NULL; 40PerfDataList* PerfDataManager::_sampled = NULL; 41PerfDataList* PerfDataManager::_constants = NULL; 42volatile bool PerfDataManager::_has_PerfData = 0; 43 44/* 45 * The jvmstat global and subsystem jvmstat counter name spaces. The top 46 * level name spaces imply the interface stability level of the counter, 47 * which generally follows the Java package, class, and property naming 48 * conventions. The CounterNS enumeration values should be used to index 49 * into this array. 50 */ 51const char* PerfDataManager::_name_spaces[] = { 52 // top level name spaces 53 "java", // stable and supported name space 54 "com.sun", // unstable but supported name space 55 "sun", // unstable and unsupported name space 56 // subsystem name spaces 57 "java.gc", // Garbage Collection name spaces 58 "com.sun.gc", 59 "sun.gc", 60 "java.ci", // Compiler name spaces 61 "com.sun.ci", 62 "sun.ci", 63 "java.cls", // Class Loader name spaces 64 "com.sun.cls", 65 "sun.cls", 66 "java.rt", // Runtime name spaces 67 "com.sun.rt", 68 "sun.rt", 69 "java.os", // Operating System name spaces 70 "com.sun.os", 71 "sun.os", 72 "java.threads", // Threads System name spaces 73 "com.sun.threads", 74 "sun.threads", 75 "java.property", // Java Property name spaces 76 "com.sun.property", 77 "sun.property", 78 "", 79}; 80 81PerfData::PerfData(CounterNS ns, const char* name, Units u, Variability v) 82 : _name(NULL), _u(u), _v(v), _valuep(NULL), 83 _on_c_heap(false) { 84 85 const char* prefix = PerfDataManager::ns_to_string(ns); 86 87 _name = NEW_C_HEAP_ARRAY(char, strlen(name) + strlen(prefix) + 2, mtInternal); 88 assert(_name != NULL && strlen(name) != 0, "invalid name"); 89 90 if (ns == NULL_NS) { 91 // No prefix is added to counters with the NULL_NS namespace. 92 strcpy(_name, name); 93 // set the F_Supported flag based on the counter name prefix. 94 if (PerfDataManager::is_stable_supported(_name) || 95 PerfDataManager::is_unstable_supported(_name)) { 96 _flags = F_Supported; 97 } 98 else { 99 _flags = F_None; 100 } 101 } 102 else { 103 sprintf(_name, "%s.%s", prefix, name); 104 // set the F_Supported flag based on the given namespace. 105 if (PerfDataManager::is_stable_supported(ns) || 106 PerfDataManager::is_unstable_supported(ns)) { 107 _flags = F_Supported; 108 } 109 else { 110 _flags = F_None; 111 } 112 } 113} 114 115PerfData::~PerfData() { 116 if (_name != NULL) { 117 FREE_C_HEAP_ARRAY(char, _name); 118 } 119 if (is_on_c_heap()) { 120 FREE_C_HEAP_ARRAY(PerfDataEntry, _pdep); 121 } 122} 123 124void PerfData::create_entry(BasicType dtype, size_t dsize, size_t vlen) { 125 126 size_t dlen = vlen==0 ? 1 : vlen; 127 128 size_t namelen = strlen(name()) + 1; // include null terminator 129 size_t size = sizeof(PerfDataEntry) + namelen; 130 size_t pad_length = ((size % dsize) == 0) ? 0 : dsize - (size % dsize); 131 size += pad_length; 132 size_t data_start = size; 133 size += (dsize * dlen); 134 135 // align size to assure allocation in units of 8 bytes 136 int align = sizeof(jlong) - 1; 137 size = ((size + align) & ~align); 138 char* psmp = PerfMemory::alloc(size); 139 140 if (psmp == NULL) { 141 // out of PerfMemory memory resources. allocate on the C heap 142 // to avoid vm termination. 143 psmp = NEW_C_HEAP_ARRAY(char, size, mtInternal); 144 _on_c_heap = true; 145 } 146 147 // compute the addresses for the name and data 148 char* cname = psmp + sizeof(PerfDataEntry); 149 150 // data is in the last dsize*dlen bytes of the entry 151 void* valuep = (void*) (psmp + data_start); 152 153 assert(is_on_c_heap() || PerfMemory::contains(cname), "just checking"); 154 assert(is_on_c_heap() || PerfMemory::contains((char*)valuep), "just checking"); 155 156 // copy the name, including null terminator, into PerfData memory 157 strcpy(cname, name()); 158 159 160 // set the header values in PerfData memory 161 PerfDataEntry* pdep = (PerfDataEntry*)psmp; 162 pdep->entry_length = (jint)size; 163 pdep->name_offset = (jint) ((uintptr_t) cname - (uintptr_t) psmp); 164 pdep->vector_length = (jint)vlen; 165 pdep->data_type = (jbyte) type2char(dtype); 166 pdep->data_units = units(); 167 pdep->data_variability = variability(); 168 pdep->flags = (jbyte)flags(); 169 pdep->data_offset = (jint) data_start; 170 171 if (PerfTraceDataCreation) { 172 tty->print("name = %s, dtype = %d, variability = %d," 173 " units = %d, dsize = %d, vlen = %d," 174 " pad_length = %d, size = %d, on_c_heap = %s," 175 " address = " INTPTR_FORMAT "," 176 " data address = " INTPTR_FORMAT "\n", 177 cname, dtype, variability(), 178 units(), dsize, vlen, 179 pad_length, size, is_on_c_heap() ? "TRUE":"FALSE", 180 psmp, valuep); 181 } 182 183 // record the start of the entry and the location of the data field. 184 _pdep = pdep; 185 _valuep = valuep; 186 187 // mark the PerfData memory region as having been updated. 188 PerfMemory::mark_updated(); 189} 190 191PerfLong::PerfLong(CounterNS ns, const char* namep, Units u, Variability v) 192 : PerfData(ns, namep, u, v) { 193 194 create_entry(T_LONG, sizeof(jlong)); 195} 196 197int PerfLong::format(char* buffer, int length) { 198 return jio_snprintf(buffer, length, JLONG_FORMAT, *(jlong*)_valuep); 199} 200 201PerfLongVariant::PerfLongVariant(CounterNS ns, const char* namep, Units u, 202 Variability v, jlong* sampled) 203 : PerfLong(ns, namep, u, v), 204 _sampled(sampled), _sample_helper(NULL) { 205 206 sample(); 207} 208 209PerfLongVariant::PerfLongVariant(CounterNS ns, const char* namep, Units u, 210 Variability v, PerfLongSampleHelper* helper) 211 : PerfLong(ns, namep, u, v), 212 _sampled(NULL), _sample_helper(helper) { 213 214 sample(); 215} 216 217void PerfLongVariant::sample() { 218 219 // JJJ - This should not happen. Maybe the first sample is taken 220 // while the _sample_helper is being null'ed out. 221 // assert(_sample_helper != NULL || _sampled != NULL, "unexpected state"); 222 if (_sample_helper == NULL) return; 223 224 if (_sample_helper != NULL) { 225 *(jlong*)_valuep = _sample_helper->take_sample(); 226 } 227 else if (_sampled != NULL) { 228 *(jlong*)_valuep = *_sampled; 229 } 230} 231 232PerfByteArray::PerfByteArray(CounterNS ns, const char* namep, Units u, 233 Variability v, jint length) 234 : PerfData(ns, namep, u, v), _length(length) { 235 236 create_entry(T_BYTE, sizeof(jbyte), (size_t)_length); 237} 238 239void PerfString::set_string(const char* s2) { 240 241 // copy n bytes of the string, assuring the null string is 242 // copied if s2 == NULL. 243 strncpy((char *)_valuep, s2 == NULL ? "" : s2, _length); 244 245 // assure the string is null terminated when strlen(s2) >= _length 246 ((char*)_valuep)[_length-1] = '\0'; 247} 248 249int PerfString::format(char* buffer, int length) { 250 return jio_snprintf(buffer, length, "%s", (char*)_valuep); 251} 252 253PerfStringConstant::PerfStringConstant(CounterNS ns, const char* namep, 254 const char* initial_value) 255 : PerfString(ns, namep, V_Constant, 256 initial_value == NULL ? 1 : 257 MIN2((jint)(strlen((char*)initial_value)+1), 258 (jint)(PerfMaxStringConstLength+1)), 259 initial_value) { 260 261 if (PrintMiscellaneous && Verbose) { 262 if (is_valid() && initial_value != NULL && 263 ((jint)strlen(initial_value) > (jint)PerfMaxStringConstLength)) { 264 265 warning("Truncating PerfStringConstant: name = %s," 266 " length = " INT32_FORMAT "," 267 " PerfMaxStringConstLength = " INT32_FORMAT "\n", 268 namep, 269 (jint)strlen(initial_value), 270 (jint)PerfMaxStringConstLength); 271 } 272 } 273} 274 275 276void PerfDataManager::destroy() { 277 278 if (_all == NULL) 279 // destroy already called, or initialization never happened 280 return; 281 282 // Clear the flag before we free the PerfData counters. Thus begins 283 // the race between this thread and another thread that has just 284 // queried PerfDataManager::has_PerfData() and gotten back 'true'. 285 // The hope is that the other thread will finish its PerfData 286 // manipulation before we free the memory. The two alternatives are 287 // 1) leak the PerfData memory or 2) do some form of synchronized 288 // access or check before every PerfData operation. 289 _has_PerfData = false; 290 os::naked_short_sleep(1); // 1ms sleep to let other thread(s) run 291 292 for (int index = 0; index < _all->length(); index++) { 293 PerfData* p = _all->at(index); 294 delete p; 295 } 296 297 delete(_all); 298 delete(_sampled); 299 delete(_constants); 300 301 _all = NULL; 302 _sampled = NULL; 303 _constants = NULL; 304} 305 306void PerfDataManager::add_item(PerfData* p, bool sampled) { 307 308 MutexLocker ml(PerfDataManager_lock); 309 310 if (_all == NULL) { 311 _all = new PerfDataList(100); 312 _has_PerfData = true; 313 } 314 315 assert(!_all->contains(p->name()), "duplicate name added"); 316 317 // add to the list of all perf data items 318 _all->append(p); 319 320 if (p->variability() == PerfData::V_Constant) { 321 if (_constants == NULL) { 322 _constants = new PerfDataList(25); 323 } 324 _constants->append(p); 325 return; 326 } 327 328 if (sampled) { 329 if (_sampled == NULL) { 330 _sampled = new PerfDataList(25); 331 } 332 _sampled->append(p); 333 } 334} 335 336PerfData* PerfDataManager::find_by_name(const char* name) { 337 return _all->find_by_name(name); 338} 339 340PerfDataList* PerfDataManager::all() { 341 342 MutexLocker ml(PerfDataManager_lock); 343 344 if (_all == NULL) 345 return NULL; 346 347 PerfDataList* clone = _all->clone(); 348 return clone; 349} 350 351PerfDataList* PerfDataManager::sampled() { 352 353 MutexLocker ml(PerfDataManager_lock); 354 355 if (_sampled == NULL) 356 return NULL; 357 358 PerfDataList* clone = _sampled->clone(); 359 return clone; 360} 361 362PerfDataList* PerfDataManager::constants() { 363 364 MutexLocker ml(PerfDataManager_lock); 365 366 if (_constants == NULL) 367 return NULL; 368 369 PerfDataList* clone = _constants->clone(); 370 return clone; 371} 372 373char* PerfDataManager::counter_name(const char* ns, const char* name) { 374 assert(ns != NULL, "ns string required"); 375 assert(name != NULL, "name string required"); 376 377 size_t len = strlen(ns) + strlen(name) + 2; 378 char* result = NEW_RESOURCE_ARRAY(char, len); 379 sprintf(result, "%s.%s", ns, name); 380 return result; 381} 382 383char* PerfDataManager::name_space(const char* ns, const char* sub, 384 int instance) { 385 char intbuf[40]; 386 jio_snprintf(intbuf, 40, UINT32_FORMAT, instance); 387 return name_space(ns, name_space(sub, intbuf)); 388} 389 390char *PerfDataManager::name_space(const char* ns, int instance) { 391 char intbuf[40]; 392 jio_snprintf(intbuf, 40, UINT32_FORMAT, instance); 393 return name_space(ns, intbuf); 394} 395 396PerfStringConstant* PerfDataManager::create_string_constant(CounterNS ns, 397 const char* name, 398 const char* s, 399 TRAPS) { 400 401 PerfStringConstant* p = new PerfStringConstant(ns, name, s); 402 403 if (!p->is_valid()) { 404 // allocation of native resources failed. 405 delete p; 406 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 407 } 408 409 add_item(p, false); 410 411 return p; 412} 413 414PerfLongConstant* PerfDataManager::create_long_constant(CounterNS ns, 415 const char* name, 416 PerfData::Units u, 417 jlong val, TRAPS) { 418 419 PerfLongConstant* p = new PerfLongConstant(ns, name, u, val); 420 421 if (!p->is_valid()) { 422 // allocation of native resources failed. 423 delete p; 424 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 425 } 426 427 add_item(p, false); 428 429 return p; 430} 431 432PerfStringVariable* PerfDataManager::create_string_variable(CounterNS ns, 433 const char* name, 434 jint max_length, 435 const char* s, 436 TRAPS) { 437 438 if (max_length == 0 && s != NULL) max_length = (jint)strlen(s); 439 440 assert(max_length != 0, "PerfStringVariable with length 0"); 441 442 PerfStringVariable* p = new PerfStringVariable(ns, name, max_length, s); 443 444 if (!p->is_valid()) { 445 // allocation of native resources failed. 446 delete p; 447 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 448 } 449 450 add_item(p, false); 451 452 return p; 453} 454 455PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns, 456 const char* name, 457 PerfData::Units u, 458 jlong ival, TRAPS) { 459 460 PerfLongVariable* p = new PerfLongVariable(ns, name, u, ival); 461 462 if (!p->is_valid()) { 463 // allocation of native resources failed. 464 delete p; 465 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 466 } 467 468 add_item(p, false); 469 470 return p; 471} 472 473PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns, 474 const char* name, 475 PerfData::Units u, 476 jlong* sp, TRAPS) { 477 478 // Sampled counters not supported if UsePerfData is false 479 if (!UsePerfData) return NULL; 480 481 PerfLongVariable* p = new PerfLongVariable(ns, name, u, sp); 482 483 if (!p->is_valid()) { 484 // allocation of native resources failed. 485 delete p; 486 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 487 } 488 489 add_item(p, true); 490 491 return p; 492} 493 494PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns, 495 const char* name, 496 PerfData::Units u, 497 PerfSampleHelper* sh, 498 TRAPS) { 499 500 // Sampled counters not supported if UsePerfData is false 501 if (!UsePerfData) return NULL; 502 503 PerfLongVariable* p = new PerfLongVariable(ns, name, u, sh); 504 505 if (!p->is_valid()) { 506 // allocation of native resources failed. 507 delete p; 508 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 509 } 510 511 add_item(p, true); 512 513 return p; 514} 515 516PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns, 517 const char* name, 518 PerfData::Units u, 519 jlong ival, TRAPS) { 520 521 PerfLongCounter* p = new PerfLongCounter(ns, name, u, ival); 522 523 if (!p->is_valid()) { 524 // allocation of native resources failed. 525 delete p; 526 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 527 } 528 529 add_item(p, false); 530 531 return p; 532} 533 534PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns, 535 const char* name, 536 PerfData::Units u, 537 jlong* sp, TRAPS) { 538 539 // Sampled counters not supported if UsePerfData is false 540 if (!UsePerfData) return NULL; 541 542 PerfLongCounter* p = new PerfLongCounter(ns, name, u, sp); 543 544 if (!p->is_valid()) { 545 // allocation of native resources failed. 546 delete p; 547 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 548 } 549 550 add_item(p, true); 551 552 return p; 553} 554 555PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns, 556 const char* name, 557 PerfData::Units u, 558 PerfSampleHelper* sh, 559 TRAPS) { 560 561 // Sampled counters not supported if UsePerfData is false 562 if (!UsePerfData) return NULL; 563 564 PerfLongCounter* p = new PerfLongCounter(ns, name, u, sh); 565 566 if (!p->is_valid()) { 567 // allocation of native resources failed. 568 delete p; 569 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); 570 } 571 572 add_item(p, true); 573 574 return p; 575} 576 577PerfDataList::PerfDataList(int length) { 578 579 _set = new(ResourceObj::C_HEAP, mtInternal) PerfDataArray(length, true); 580} 581 582PerfDataList::PerfDataList(PerfDataList* p) { 583 584 _set = new(ResourceObj::C_HEAP, mtInternal) PerfDataArray(p->length(), true); 585 586 _set->appendAll(p->get_impl()); 587} 588 589PerfDataList::~PerfDataList() { 590 591 delete _set; 592 593} 594 595bool PerfDataList::by_name(void* name, PerfData* pd) { 596 597 if (pd == NULL) 598 return false; 599 600 return strcmp((const char*)name, pd->name()) == 0; 601} 602 603PerfData* PerfDataList::find_by_name(const char* name) { 604 605 // if add_item hasn't been called the list won't be initialized 606 if (this == NULL) 607 return NULL; 608 609 int i = _set->find((void*)name, PerfDataList::by_name); 610 611 if (i >= 0 && i <= _set->length()) 612 return _set->at(i); 613 else 614 return NULL; 615} 616 617PerfDataList* PerfDataList::clone() { 618 619 PerfDataList* copy = new PerfDataList(this); 620 621 assert(copy != NULL, "just checking"); 622 623 return copy; 624} 625