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