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