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