1 /*
  2  * Copyright (c) 1997, 2021, 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 "gc/shared/ageTable.inline.hpp"
 27 #include "gc/shared/ageTableTracer.hpp"
 28 #include "gc/shared/collectedHeap.hpp"
 29 #include "gc/shared/gc_globals.hpp"
 30 #include "jvm.h"
 31 #include "logging/log.hpp"
 32 #include "memory/resourceArea.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "runtime/perfData.hpp"
 35 #include "utilities/copy.hpp"
 36 #include "logging/logStream.hpp"
 37 
 38 /* Copyright (c) 1992, 2021, Oracle and/or its affiliates, and Stanford University.
 39    See the LICENSE file for license information. */
 40 
 41 AgeTable::AgeTable(bool global) : _use_perf_data(UsePerfData && global) {
 42 
 43   clear();
 44 
 45   if (_use_perf_data) {
 46 
 47     ResourceMark rm;
 48     EXCEPTION_MARK;
 49 
 50     const char* agetable_ns = "generation.0.agetable";
 51     const char* bytes_ns = PerfDataManager::name_space(agetable_ns, "bytes");
 52 
 53     for(int age = 0; age < table_size; age ++) {
 54       char age_name[10];
 55       jio_snprintf(age_name, sizeof(age_name), "%2.2d", age);
 56       const char* cname = PerfDataManager::counter_name(bytes_ns, age_name);
 57       _perf_sizes[age] = PerfDataManager::create_variable(SUN_GC, cname,
 58                                                           PerfData::U_Bytes,
 59                                                           CHECK);
 60     }
 61 
 62     const char* cname = PerfDataManager::counter_name(agetable_ns, "size");
 63     PerfDataManager::create_constant(SUN_GC, cname, PerfData::U_None,
 64                                      table_size, CHECK);
 65   }
 66 }
 67 
 68 void AgeTable::clear() {
 69   for (size_t* p = sizes; p < sizes + table_size; ++p) {
 70     *p = 0;
 71   }
 72 }
 73 
 74 #ifndef PRODUCT
 75 bool AgeTable::is_clear() const {
 76   for (const size_t* p = sizes; p < sizes + table_size; ++p) {
 77     if (*p != 0) return false;
 78   }
 79   return true;
 80 }
 81 #endif // !PRODUCT
 82 
 83 void AgeTable::merge(const AgeTable* subTable) {
 84   for (int i = 0; i < table_size; i++) {
 85     sizes[i]+= subTable->sizes[i];
 86   }
 87 }
 88 
 89 uint AgeTable::compute_tenuring_threshold(size_t desired_survivor_size) {
 90   uint result;
 91 
 92   if (AlwaysTenure || NeverTenure) {
 93     assert(MaxTenuringThreshold == 0 || MaxTenuringThreshold == markWord::max_age + 1,
 94            "MaxTenuringThreshold should be 0 or markWord::max_age + 1, but is " UINTX_FORMAT, MaxTenuringThreshold);
 95     result = MaxTenuringThreshold;
 96   } else {
 97     size_t total = 0;
 98     uint age = 1;
 99     assert(sizes[0] == 0, "no objects with age zero should be recorded");
100     while (age < table_size) {
101       total += sizes[age];
102       // check if including objects of age 'age' made us pass the desired
103       // size, if so 'age' is the new threshold
104       if (total > desired_survivor_size) break;
105       age++;
106     }
107     result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold;
108   }
109 
110 
111   log_debug(gc, age)("Desired survivor size " SIZE_FORMAT " bytes, new threshold " UINTX_FORMAT " (max threshold " UINTX_FORMAT ")",
112                      desired_survivor_size * oopSize, (uintx) result, MaxTenuringThreshold);
113 
114   return result;
115 }
116 
117 void AgeTable::print_age_table(uint tenuring_threshold) {
118   LogTarget(Trace, gc, age) lt;
119   if (lt.is_enabled() || _use_perf_data || AgeTableTracer::is_tenuring_distribution_event_enabled()) {
120     LogStream st(lt);
121     print_on(&st, tenuring_threshold);
122   }
123 }
124 
125 void AgeTable::print_on(outputStream* st, uint tenuring_threshold) {
126   st->print_cr("Age table with threshold %u (max threshold " UINTX_FORMAT ")",
127                tenuring_threshold, MaxTenuringThreshold);
128 
129   size_t total = 0;
130   uint age = 1;
131   while (age < table_size) {
132     size_t word_size = sizes[age];
133     total += word_size;
134     if (word_size > 0) {
135       st->print_cr("- age %3u: " SIZE_FORMAT_W(10) " bytes, " SIZE_FORMAT_W(10) " total",
136                    age, word_size * oopSize, total * oopSize);
137     }
138     AgeTableTracer::send_tenuring_distribution_event(age, word_size * oopSize);
139     if (_use_perf_data) {
140       _perf_sizes[age]->set_value(word_size * oopSize);
141     }
142     age++;
143   }
144 }