1 /*
   2  * Copyright (c) 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 "runtime/atomic.hpp"
  27 #include "runtime/atomic.inline.hpp"
  28 #include "runtime/os.hpp"
  29 #include "utilities/debug.hpp"
  30 #include "utilities/macros.hpp"
  31 #include "utilities/tableStatistics.hpp"
  32 #if INCLUDE_JFR
  33 #include "jfr/jfr.hpp"
  34 #endif
  35 
  36 TableRateStatistics::TableRateStatistics() :
  37   _added_items(0), _removed_items(0),
  38   _time_stamp(0), _seconds_stamp(0),
  39   _added_items_stamp(0), _added_items_stamp_prev(0),
  40   _removed_items_stamp(0), _removed_items_stamp_prev(0) {}
  41 
  42 TableRateStatistics::~TableRateStatistics() { };
  43 
  44 void TableRateStatistics::add() {
  45 #if INCLUDE_JFR
  46   if (Jfr::is_recording()) {
  47     Atomic::inc_ptr(&_added_items);
  48   }
  49 #endif
  50 }
  51 
  52 void TableRateStatistics::remove() {
  53 #if INCLUDE_JFR
  54   if (Jfr::is_recording()) {
  55     Atomic::inc_ptr(&_removed_items);
  56   }
  57 #endif
  58 }
  59 
  60 void TableRateStatistics::stamp() {
  61   jlong now = os::javaTimeNanos();
  62 
  63   _added_items_stamp_prev = _added_items_stamp;
  64   _removed_items_stamp_prev = _removed_items_stamp;
  65 
  66   _added_items_stamp = _added_items;
  67   _removed_items_stamp = _removed_items;
  68 
  69   if (_time_stamp == 0) {
  70     _time_stamp = now - 1000000000;
  71   }
  72   jlong diff = (now - _time_stamp);
  73   _seconds_stamp = (float)diff / 1000000000.0;
  74   _time_stamp = now;
  75 }
  76 
  77 float TableRateStatistics::get_add_rate() {
  78   return (float)((_added_items_stamp - _added_items_stamp_prev) / _seconds_stamp);
  79 }
  80 
  81 float TableRateStatistics::get_remove_rate() {
  82   return (float)((_removed_items_stamp - _removed_items_stamp_prev) / _seconds_stamp);
  83 }
  84 
  85 TableStatistics::TableStatistics() :
  86   _literal_bytes(0),
  87   _number_of_buckets(0), _number_of_entries(0),
  88   _maximum_bucket_size(0), _average_bucket_size(0),
  89   _variance_of_bucket_size(0), _stddev_of_bucket_size(0),
  90   _bucket_bytes(0), _entry_bytes(0), _total_footprint(0),
  91   _bucket_size(0), _entry_size(0),
  92   _add_rate(0), _remove_rate(0) {
  93 }
  94 
  95 TableStatistics::TableStatistics(TableRateStatistics& rate_stats, NumberSeq summary, size_t literal_bytes, size_t bucket_bytes, size_t node_bytes) :
  96   _literal_bytes(literal_bytes),
  97   _number_of_buckets(0), _number_of_entries(0),
  98   _maximum_bucket_size(0), _average_bucket_size(0),
  99   _variance_of_bucket_size(0), _stddev_of_bucket_size(0),
 100   _bucket_bytes(0), _entry_bytes(0), _total_footprint(0),
 101   _bucket_size(0), _entry_size(0),
 102   _add_rate(0), _remove_rate(0) {
 103 
 104   _number_of_buckets = summary.num();
 105   _number_of_entries = (size_t)summary.sum();
 106 
 107   _maximum_bucket_size = (size_t)summary.maximum();
 108   _average_bucket_size = summary.avg();
 109   _variance_of_bucket_size = summary.variance();
 110   _stddev_of_bucket_size = summary.sd();
 111 
 112   _bucket_bytes = _number_of_buckets * bucket_bytes;
 113   _entry_bytes = _number_of_entries * node_bytes;
 114   _total_footprint = _literal_bytes + _bucket_bytes + _entry_bytes;
 115 
 116   _bucket_size = (_number_of_buckets <= 0) ? 0 : (_bucket_bytes / _number_of_buckets);
 117   _entry_size = (_number_of_entries <= 0) ? 0 : (_entry_bytes / _number_of_entries);
 118 
 119 #if INCLUDE_JFR
 120   if (Jfr::is_recording()) {
 121     rate_stats.stamp();
 122     _add_rate = rate_stats.get_add_rate();
 123     _remove_rate = rate_stats.get_remove_rate();
 124   }
 125 #endif
 126 }
 127 
 128 TableStatistics::~TableStatistics() { }
 129 
 130 void TableStatistics::print(outputStream* st, const char *table_name) {
 131   st->print_cr("%s statistics:", table_name);
 132   st->print_cr("Number of buckets       : %9" PRIuPTR " = %9" PRIuPTR
 133                " bytes, each " SIZE_FORMAT,
 134               _number_of_buckets, _bucket_bytes, _bucket_size);
 135   st->print_cr("Number of entries       : %9" PRIuPTR " = %9" PRIuPTR
 136                " bytes, each " SIZE_FORMAT,
 137                _number_of_entries, _entry_bytes, _entry_size);
 138   if (_literal_bytes != 0) {
 139     float literal_avg = (_number_of_entries <= 0) ? 0 : (_literal_bytes / _number_of_entries);
 140     st->print_cr("Number of literals      : %9" PRIuPTR " = %9" PRIuPTR
 141                  " bytes, avg %7.3f",
 142                  _number_of_entries, _literal_bytes, literal_avg);
 143   }
 144   st->print_cr("Total footprint         : %9s = %9" PRIuPTR " bytes", "", _total_footprint);
 145   st->print_cr("Average bucket size     : %9.3f", _average_bucket_size);
 146   st->print_cr("Variance of bucket size : %9.3f", _variance_of_bucket_size);
 147   st->print_cr("Std. dev. of bucket size: %9.3f", _stddev_of_bucket_size);
 148   st->print_cr("Maximum bucket size     : %9" PRIuPTR, _maximum_bucket_size);
 149 }
 150