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