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