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