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 "prims/jvm.h"
  30 #include "runtime/handles.inline.hpp"
  31 #include "runtime/java.hpp"
  32 #include "runtime/mutex.hpp"
  33 #include "runtime/mutexLocker.hpp"
  34 #include "runtime/os.hpp"
  35 #include "runtime/perfData.hpp"
  36 #include "utilities/exceptions.hpp"
  37 #include "utilities/globalDefinitions.hpp"
  38 
  39 PerfDataList*   PerfDataManager::_all = NULL;
  40 PerfDataList*   PerfDataManager::_sampled = NULL;
  41 PerfDataList*   PerfDataManager::_constants = NULL;
  42 volatile 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  */
  51 const 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 
  81 PerfData::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 
 115 PerfData::~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 
 124 void 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   log_debug(perf, datacreation)("name = %s, dtype = %d, variability = %d,"
 172                                 " units = %d, dsize = " SIZE_FORMAT ", vlen = " SIZE_FORMAT ","
 173                                 " pad_length = " SIZE_FORMAT ", size = " SIZE_FORMAT ", on_c_heap = %s,"
 174                                 " address = " INTPTR_FORMAT ","
 175                                 " data address = " INTPTR_FORMAT "\n",
 176                                 cname, dtype, variability(),
 177                                 units(), dsize, vlen,
 178                                 pad_length, size, is_on_c_heap() ? "TRUE":"FALSE",
 179                                 p2i(psmp), p2i(valuep));
 180 
 181   // record the start of the entry and the location of the data field.
 182   _pdep = pdep;
 183   _valuep = valuep;
 184 
 185   // mark the PerfData memory region as having been updated.
 186   PerfMemory::mark_updated();
 187 }
 188 
 189 PerfLong::PerfLong(CounterNS ns, const char* namep, Units u, Variability v)
 190                  : PerfData(ns, namep, u, v) {
 191 
 192   create_entry(T_LONG, sizeof(jlong));
 193 }
 194 
 195 int PerfLong::format(char* buffer, int length) {
 196   return jio_snprintf(buffer, length, JLONG_FORMAT, *(jlong*)_valuep);
 197 }
 198 
 199 PerfLongVariant::PerfLongVariant(CounterNS ns, const char* namep, Units u,
 200                                  Variability v, jlong* sampled)
 201                                 : PerfLong(ns, namep, u, v),
 202                                   _sampled(sampled), _sample_helper(NULL) {
 203 
 204   sample();
 205 }
 206 
 207 PerfLongVariant::PerfLongVariant(CounterNS ns, const char* namep, Units u,
 208                                  Variability v, PerfLongSampleHelper* helper)
 209                                 : PerfLong(ns, namep, u, v),
 210                                   _sampled(NULL), _sample_helper(helper) {
 211 
 212   sample();
 213 }
 214 
 215 void PerfLongVariant::sample() {
 216   if (_sample_helper != NULL) {
 217     *(jlong*)_valuep = _sample_helper->take_sample();
 218   }
 219 }
 220 
 221 PerfByteArray::PerfByteArray(CounterNS ns, const char* namep, Units u,
 222                              Variability v, jint length)
 223                             : PerfData(ns, namep, u, v), _length(length) {
 224 
 225   create_entry(T_BYTE, sizeof(jbyte), (size_t)_length);
 226 }
 227 
 228 void PerfString::set_string(const char* s2) {
 229 
 230   // copy n bytes of the string, assuring the null string is
 231   // copied if s2 == NULL.
 232   strncpy((char *)_valuep, s2 == NULL ? "" : s2, _length);
 233 
 234   // assure the string is null terminated when strlen(s2) >= _length
 235   ((char*)_valuep)[_length-1] = '\0';
 236 }
 237 
 238 int PerfString::format(char* buffer, int length) {
 239   return jio_snprintf(buffer, length, "%s", (char*)_valuep);
 240 }
 241 
 242 PerfStringConstant::PerfStringConstant(CounterNS ns, const char* namep,
 243                                        const char* initial_value)
 244                      : PerfString(ns, namep, V_Constant,
 245                                   initial_value == NULL ? 1 :
 246                                   MIN2((jint)(strlen((char*)initial_value)+1),
 247                                        (jint)(PerfMaxStringConstLength+1)),
 248                                   initial_value) {
 249 
 250   if (PrintMiscellaneous && Verbose) {
 251     if (is_valid() && initial_value != NULL &&
 252         ((jint)strlen(initial_value) > (jint)PerfMaxStringConstLength)) {
 253 
 254       warning("Truncating PerfStringConstant: name = %s,"
 255               " length = " INT32_FORMAT ","
 256               " PerfMaxStringConstLength = " INT32_FORMAT "\n",
 257               namep,
 258               (jint)strlen(initial_value),
 259               (jint)PerfMaxStringConstLength);
 260     }
 261   }
 262 }
 263 
 264 
 265 void PerfDataManager::destroy() {
 266 
 267   if (_all == NULL)
 268     // destroy already called, or initialization never happened
 269     return;
 270 
 271   // Clear the flag before we free the PerfData counters. Thus begins
 272   // the race between this thread and another thread that has just
 273   // queried PerfDataManager::has_PerfData() and gotten back 'true'.
 274   // The hope is that the other thread will finish its PerfData
 275   // manipulation before we free the memory. The two alternatives are
 276   // 1) leak the PerfData memory or 2) do some form of synchronized
 277   // access or check before every PerfData operation.
 278   _has_PerfData = false;
 279   os::naked_short_sleep(1);  // 1ms sleep to let other thread(s) run
 280 
 281   for (int index = 0; index < _all->length(); index++) {
 282     PerfData* p = _all->at(index);
 283     delete p;
 284   }
 285 
 286   delete(_all);
 287   delete(_sampled);
 288   delete(_constants);
 289 
 290   _all = NULL;
 291   _sampled = NULL;
 292   _constants = NULL;
 293 }
 294 
 295 void PerfDataManager::add_item(PerfData* p, bool sampled) {
 296 
 297   MutexLocker ml(PerfDataManager_lock);
 298 
 299   if (_all == NULL) {
 300     _all = new PerfDataList(100);
 301     _has_PerfData = true;
 302   }
 303 
 304   assert(!_all->contains(p->name()), "duplicate name added");
 305 
 306   // add to the list of all perf data items
 307   _all->append(p);
 308 
 309   if (p->variability() == PerfData::V_Constant) {
 310     if (_constants == NULL) {
 311       _constants = new PerfDataList(25);
 312     }
 313     _constants->append(p);
 314     return;
 315   }
 316 
 317   if (sampled) {
 318     if (_sampled == NULL) {
 319       _sampled = new PerfDataList(25);
 320     }
 321     _sampled->append(p);
 322   }
 323 }
 324 
 325 PerfData* PerfDataManager::find_by_name(const char* name) {
 326   return _all->find_by_name(name);
 327 }
 328 
 329 PerfDataList* PerfDataManager::all() {
 330 
 331   MutexLocker ml(PerfDataManager_lock);
 332 
 333   if (_all == NULL)
 334     return NULL;
 335 
 336   PerfDataList* clone = _all->clone();
 337   return clone;
 338 }
 339 
 340 PerfDataList* PerfDataManager::sampled() {
 341 
 342   MutexLocker ml(PerfDataManager_lock);
 343 
 344   if (_sampled == NULL)
 345     return NULL;
 346 
 347   PerfDataList* clone = _sampled->clone();
 348   return clone;
 349 }
 350 
 351 PerfDataList* PerfDataManager::constants() {
 352 
 353   MutexLocker ml(PerfDataManager_lock);
 354 
 355   if (_constants == NULL)
 356     return NULL;
 357 
 358   PerfDataList* clone = _constants->clone();
 359   return clone;
 360 }
 361 
 362 char* PerfDataManager::counter_name(const char* ns, const char* name) {
 363    assert(ns != NULL, "ns string required");
 364    assert(name != NULL, "name string required");
 365 
 366    size_t len = strlen(ns) + strlen(name) + 2;
 367    char* result = NEW_RESOURCE_ARRAY(char, len);
 368    sprintf(result, "%s.%s", ns, name);
 369    return result;
 370 }
 371 
 372 char* PerfDataManager::name_space(const char* ns, const char* sub,
 373                                   int instance) {
 374    char intbuf[40];
 375    jio_snprintf(intbuf, 40, UINT32_FORMAT, instance);
 376    return name_space(ns, name_space(sub, intbuf));
 377 }
 378 
 379 char *PerfDataManager::name_space(const char* ns, int instance) {
 380    char intbuf[40];
 381    jio_snprintf(intbuf, 40, UINT32_FORMAT, instance);
 382    return name_space(ns, intbuf);
 383 }
 384 
 385 PerfStringConstant* PerfDataManager::create_string_constant(CounterNS ns,
 386                                                             const char* name,
 387                                                             const char* s,
 388                                                             TRAPS) {
 389 
 390   PerfStringConstant* p = new PerfStringConstant(ns, name, s);
 391 
 392   if (!p->is_valid()) {
 393     // allocation of native resources failed.
 394     delete p;
 395     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 396   }
 397 
 398   add_item(p, false);
 399 
 400   return p;
 401 }
 402 
 403 PerfLongConstant* PerfDataManager::create_long_constant(CounterNS ns,
 404                                                         const char* name,
 405                                                         PerfData::Units u,
 406                                                         jlong val, TRAPS) {
 407 
 408   PerfLongConstant* p = new PerfLongConstant(ns, name, u, val);
 409 
 410   if (!p->is_valid()) {
 411     // allocation of native resources failed.
 412     delete p;
 413     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 414   }
 415 
 416   add_item(p, false);
 417 
 418   return p;
 419 }
 420 
 421 PerfStringVariable* PerfDataManager::create_string_variable(CounterNS ns,
 422                                                             const char* name,
 423                                                             jint max_length,
 424                                                             const char* s,
 425                                                             TRAPS) {
 426 
 427   if (max_length == 0 && s != NULL) max_length = (jint)strlen(s);
 428 
 429   assert(max_length != 0, "PerfStringVariable with length 0");
 430 
 431   PerfStringVariable* p = new PerfStringVariable(ns, name, max_length, s);
 432 
 433   if (!p->is_valid()) {
 434     // allocation of native resources failed.
 435     delete p;
 436     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 437   }
 438 
 439   add_item(p, false);
 440 
 441   return p;
 442 }
 443 
 444 PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns,
 445                                                         const char* name,
 446                                                         PerfData::Units u,
 447                                                         jlong ival, TRAPS) {
 448 
 449   PerfLongVariable* p = new PerfLongVariable(ns, name, u, ival);
 450 
 451   if (!p->is_valid()) {
 452     // allocation of native resources failed.
 453     delete p;
 454     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 455   }
 456 
 457   add_item(p, false);
 458 
 459   return p;
 460 }
 461 
 462 PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns,
 463                                                         const char* name,
 464                                                         PerfData::Units u,
 465                                                         jlong* sp, TRAPS) {
 466 
 467   // Sampled counters not supported if UsePerfData is false
 468   if (!UsePerfData) return NULL;
 469 
 470   PerfLongVariable* p = new PerfLongVariable(ns, name, u, sp);
 471 
 472   if (!p->is_valid()) {
 473     // allocation of native resources failed.
 474     delete p;
 475     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 476   }
 477 
 478   add_item(p, true);
 479 
 480   return p;
 481 }
 482 
 483 PerfLongVariable* PerfDataManager::create_long_variable(CounterNS ns,
 484                                                         const char* name,
 485                                                         PerfData::Units u,
 486                                                         PerfSampleHelper* sh,
 487                                                         TRAPS) {
 488 
 489   // Sampled counters not supported if UsePerfData is false
 490   if (!UsePerfData) return NULL;
 491 
 492   PerfLongVariable* p = new PerfLongVariable(ns, name, u, sh);
 493 
 494   if (!p->is_valid()) {
 495     // allocation of native resources failed.
 496     delete p;
 497     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 498   }
 499 
 500   add_item(p, true);
 501 
 502   return p;
 503 }
 504 
 505 PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns,
 506                                                       const char* name,
 507                                                       PerfData::Units u,
 508                                                       jlong ival, TRAPS) {
 509 
 510   PerfLongCounter* p = new PerfLongCounter(ns, name, u, ival);
 511 
 512   if (!p->is_valid()) {
 513     // allocation of native resources failed.
 514     delete p;
 515     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 516   }
 517 
 518   add_item(p, false);
 519 
 520   return p;
 521 }
 522 
 523 PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns,
 524                                                       const char* name,
 525                                                       PerfData::Units u,
 526                                                       jlong* sp, TRAPS) {
 527 
 528   // Sampled counters not supported if UsePerfData is false
 529   if (!UsePerfData) return NULL;
 530 
 531   PerfLongCounter* p = new PerfLongCounter(ns, name, u, sp);
 532 
 533   if (!p->is_valid()) {
 534     // allocation of native resources failed.
 535     delete p;
 536     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 537   }
 538 
 539   add_item(p, true);
 540 
 541   return p;
 542 }
 543 
 544 PerfLongCounter* PerfDataManager::create_long_counter(CounterNS ns,
 545                                                       const char* name,
 546                                                       PerfData::Units u,
 547                                                       PerfSampleHelper* sh,
 548                                                       TRAPS) {
 549 
 550   // Sampled counters not supported if UsePerfData is false
 551   if (!UsePerfData) return NULL;
 552 
 553   PerfLongCounter* p = new PerfLongCounter(ns, name, u, sh);
 554 
 555   if (!p->is_valid()) {
 556     // allocation of native resources failed.
 557     delete p;
 558     THROW_0(vmSymbols::java_lang_OutOfMemoryError());
 559   }
 560 
 561   add_item(p, true);
 562 
 563   return p;
 564 }
 565 
 566 PerfDataList::PerfDataList(int length) {
 567 
 568   _set = new(ResourceObj::C_HEAP, mtInternal) PerfDataArray(length, true);
 569 }
 570 
 571 PerfDataList::PerfDataList(PerfDataList* p) {
 572 
 573   _set = new(ResourceObj::C_HEAP, mtInternal) PerfDataArray(p->length(), true);
 574 
 575   _set->appendAll(p->get_impl());
 576 }
 577 
 578 PerfDataList::~PerfDataList() {
 579 
 580   delete _set;
 581 
 582 }
 583 
 584 bool PerfDataList::by_name(void* name, PerfData* pd) {
 585 
 586   if (pd == NULL)
 587     return false;
 588 
 589   return strcmp((const char*)name, pd->name()) == 0;
 590 }
 591 
 592 PerfData* PerfDataList::find_by_name(const char* name) {
 593 
 594   // if add_item hasn't been called the list won't be initialized
 595   if (this == NULL)
 596     return NULL;
 597 
 598   int i = _set->find((void*)name, PerfDataList::by_name);
 599 
 600   if (i >= 0 && i <= _set->length())
 601     return _set->at(i);
 602   else
 603     return NULL;
 604 }
 605 
 606 PerfDataList* PerfDataList::clone() {
 607 
 608   PerfDataList* copy = new PerfDataList(this);
 609 
 610   assert(copy != NULL, "just checking");
 611 
 612   return copy;
 613 }