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() {
 76   size_t total = 0;
 77   for (size_t* p = sizes; p < sizes + table_size; ++p) {
 78     total += *p;
 79   }
 80   return total == 0;
 81 }
 82 #endif // !PRODUCT
 83 
 84 void AgeTable::merge(const AgeTable* subTable) {
 85   for (int i = 0; i < table_size; i++) {
 86     sizes[i]+= subTable->sizes[i];
 87   }
 88 }
 89 
 90 uint AgeTable::compute_tenuring_threshold(size_t desired_survivor_size) {
 91   uint result;
 92 
 93   if (AlwaysTenure || NeverTenure) {
 94     assert(MaxTenuringThreshold == 0 || MaxTenuringThreshold == markWord::max_age + 1,
 95            "MaxTenuringThreshold should be 0 or markWord::max_age + 1, but is %u", MaxTenuringThreshold);
 96     result = MaxTenuringThreshold;
 97   } else {
 98     size_t total = 0;
 99     uint age = 1;
100     assert(sizes[0] == 0, "no objects with age zero should be recorded");
101     while (age < table_size) {
102       total += sizes[age];
103       // check if including objects of age 'age' made us pass the desired
104       // size, if so 'age' is the new threshold
105       if (total > desired_survivor_size) break;
106       age++;
107     }
108     result = age < MaxTenuringThreshold ? age : MaxTenuringThreshold;
109   }
110 
111 
112   log_debug(gc, age)("Desired survivor size %zu bytes, new threshold " UINTX_FORMAT " (max threshold %u)",
113                      desired_survivor_size * oopSize, (uintx) result, MaxTenuringThreshold);
114 
115   return result;
116 }
117 
118 void AgeTable::print_age_table(uint tenuring_threshold) {
119   LogTarget(Trace, gc, age) lt;
120   if (lt.is_enabled() || _use_perf_data || AgeTableTracer::is_tenuring_distribution_event_enabled()) {
121     LogStream st(lt);
122     print_on(&st, tenuring_threshold);
123   }
124 }
125 
126 void AgeTable::print_on(outputStream* st, uint tenuring_threshold) {
127   st->print_cr("Age table with threshold %u (max threshold %u)",
128                tenuring_threshold, MaxTenuringThreshold);
129 
130   size_t total = 0;
131   uint age = 1;
132   while (age < table_size) {
133     size_t word_size = sizes[age];
134     total += word_size;
135     if (word_size > 0) {
136       st->print_cr("- age %3u: " SIZE_FORMAT_W(10) " bytes, " SIZE_FORMAT_W(10) " total",
137                    age, word_size * oopSize, total * oopSize);
138     }
139     AgeTableTracer::send_tenuring_distribution_event(age, word_size * oopSize);
140     if (_use_perf_data) {
141       _perf_sizes[age]->set_value(word_size * oopSize);
142     }
143     age++;
144   }
145 }