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