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