Path: blob/master/src/hotspot/share/gc/serial/tenuredGeneration.cpp
41149 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "gc/serial/genMarkSweep.hpp"26#include "gc/serial/tenuredGeneration.inline.hpp"27#include "gc/shared/blockOffsetTable.inline.hpp"28#include "gc/shared/cardGeneration.inline.hpp"29#include "gc/shared/collectorCounters.hpp"30#include "gc/shared/gcTimer.hpp"31#include "gc/shared/gcTrace.hpp"32#include "gc/shared/genCollectedHeap.hpp"33#include "gc/shared/genOopClosures.inline.hpp"34#include "gc/shared/generationSpec.hpp"35#include "gc/shared/space.hpp"36#include "logging/log.hpp"37#include "memory/allocation.inline.hpp"38#include "oops/oop.inline.hpp"39#include "runtime/java.hpp"40#include "utilities/macros.hpp"4142TenuredGeneration::TenuredGeneration(ReservedSpace rs,43size_t initial_byte_size,44size_t min_byte_size,45size_t max_byte_size,46CardTableRS* remset) :47CardGeneration(rs, initial_byte_size, remset)48{49HeapWord* bottom = (HeapWord*) _virtual_space.low();50HeapWord* end = (HeapWord*) _virtual_space.high();51_the_space = new TenuredSpace(_bts, MemRegion(bottom, end));52_the_space->reset_saved_mark();53_shrink_factor = 0;54_capacity_at_prologue = 0;5556_gc_stats = new GCStats();5758// initialize performance counters5960const char* gen_name = "old";61// Generation Counters -- generation 1, 1 subspace62_gen_counters = new GenerationCounters(gen_name, 1, 1,63min_byte_size, max_byte_size, &_virtual_space);6465_gc_counters = new CollectorCounters("Serial full collection pauses", 1);6667_space_counters = new CSpaceCounters(gen_name, 0,68_virtual_space.reserved_size(),69_the_space, _gen_counters);70}7172void TenuredGeneration::gc_prologue(bool full) {73_capacity_at_prologue = capacity();74_used_at_prologue = used();75}7677bool TenuredGeneration::should_collect(bool full,78size_t size,79bool is_tlab) {80// This should be one big conditional or (||), but I want to be able to tell81// why it returns what it returns (without re-evaluating the conditionals82// in case they aren't idempotent), so I'm doing it this way.83// DeMorgan says it's okay.84if (full) {85log_trace(gc)("TenuredGeneration::should_collect: because full");86return true;87}88if (should_allocate(size, is_tlab)) {89log_trace(gc)("TenuredGeneration::should_collect: because should_allocate(" SIZE_FORMAT ")", size);90return true;91}92// If we don't have very much free space.93// XXX: 10000 should be a percentage of the capacity!!!94if (free() < 10000) {95log_trace(gc)("TenuredGeneration::should_collect: because free(): " SIZE_FORMAT, free());96return true;97}98// If we had to expand to accommodate promotions from the young generation99if (_capacity_at_prologue < capacity()) {100log_trace(gc)("TenuredGeneration::should_collect: because_capacity_at_prologue: " SIZE_FORMAT " < capacity(): " SIZE_FORMAT,101_capacity_at_prologue, capacity());102return true;103}104105return false;106}107108void TenuredGeneration::compute_new_size() {109assert_locked_or_safepoint(Heap_lock);110111// Compute some numbers about the state of the heap.112const size_t used_after_gc = used();113const size_t capacity_after_gc = capacity();114115CardGeneration::compute_new_size();116117assert(used() == used_after_gc && used_after_gc <= capacity(),118"used: " SIZE_FORMAT " used_after_gc: " SIZE_FORMAT119" capacity: " SIZE_FORMAT, used(), used_after_gc, capacity());120}121122void TenuredGeneration::update_gc_stats(Generation* current_generation,123bool full) {124// If the young generation has been collected, gather any statistics125// that are of interest at this point.126bool current_is_young = GenCollectedHeap::heap()->is_young_gen(current_generation);127if (!full && current_is_young) {128// Calculate size of data promoted from the young generation129// before doing the collection.130size_t used_before_gc = used();131132// If the young gen collection was skipped, then the133// number of promoted bytes will be 0 and adding it to the134// average will incorrectly lessen the average. It is, however,135// also possible that no promotion was needed.136if (used_before_gc >= _used_at_prologue) {137size_t promoted_in_bytes = used_before_gc - _used_at_prologue;138gc_stats()->avg_promoted()->sample(promoted_in_bytes);139}140}141}142143void TenuredGeneration::update_counters() {144if (UsePerfData) {145_space_counters->update_all();146_gen_counters->update_all();147}148}149150bool TenuredGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {151size_t available = max_contiguous_available();152size_t av_promo = (size_t)gc_stats()->avg_promoted()->padded_average();153bool res = (available >= av_promo) || (available >= max_promotion_in_bytes);154155log_trace(gc)("Tenured: promo attempt is%s safe: available(" SIZE_FORMAT ") %s av_promo(" SIZE_FORMAT "), max_promo(" SIZE_FORMAT ")",156res? "":" not", available, res? ">=":"<", av_promo, max_promotion_in_bytes);157158return res;159}160161void TenuredGeneration::collect(bool full,162bool clear_all_soft_refs,163size_t size,164bool is_tlab) {165GenCollectedHeap* gch = GenCollectedHeap::heap();166167// Temporarily expand the span of our ref processor, so168// refs discovery is over the entire heap, not just this generation169ReferenceProcessorSpanMutator170x(ref_processor(), gch->reserved_region());171172STWGCTimer* gc_timer = GenMarkSweep::gc_timer();173gc_timer->register_gc_start();174175SerialOldTracer* gc_tracer = GenMarkSweep::gc_tracer();176gc_tracer->report_gc_start(gch->gc_cause(), gc_timer->gc_start());177178gch->pre_full_gc_dump(gc_timer);179180GenMarkSweep::invoke_at_safepoint(ref_processor(), clear_all_soft_refs);181182gch->post_full_gc_dump(gc_timer);183184gc_timer->register_gc_end();185186gc_tracer->report_gc_end(gc_timer->gc_end(), gc_timer->time_partitions());187}188189HeapWord*190TenuredGeneration::expand_and_allocate(size_t word_size,191bool is_tlab,192bool parallel) {193assert(!is_tlab, "TenuredGeneration does not support TLAB allocation");194if (parallel) {195MutexLocker x(ParGCRareEvent_lock);196HeapWord* result = NULL;197size_t byte_size = word_size * HeapWordSize;198while (true) {199expand(byte_size, _min_heap_delta_bytes);200if (GCExpandToAllocateDelayMillis > 0) {201os::naked_sleep(GCExpandToAllocateDelayMillis);202}203result = _the_space->par_allocate(word_size);204if ( result != NULL) {205return result;206} else {207// If there's not enough expansion space available, give up.208if (_virtual_space.uncommitted_size() < byte_size) {209return NULL;210}211// else try again212}213}214} else {215expand(word_size*HeapWordSize, _min_heap_delta_bytes);216return _the_space->allocate(word_size);217}218}219220bool TenuredGeneration::expand(size_t bytes, size_t expand_bytes) {221GCMutexLocker x(ExpandHeap_lock);222return CardGeneration::expand(bytes, expand_bytes);223}224225size_t TenuredGeneration::unsafe_max_alloc_nogc() const {226return _the_space->free();227}228229size_t TenuredGeneration::contiguous_available() const {230return _the_space->free() + _virtual_space.uncommitted_size();231}232233void TenuredGeneration::assert_correct_size_change_locking() {234assert_locked_or_safepoint(ExpandHeap_lock);235}236237// Currently nothing to do.238void TenuredGeneration::prepare_for_verify() {}239240void TenuredGeneration::object_iterate(ObjectClosure* blk) {241_the_space->object_iterate(blk);242}243244void TenuredGeneration::save_marks() {245_the_space->set_saved_mark();246}247248void TenuredGeneration::reset_saved_marks() {249_the_space->reset_saved_mark();250}251252bool TenuredGeneration::no_allocs_since_save_marks() {253return _the_space->saved_mark_at_top();254}255256void TenuredGeneration::gc_epilogue(bool full) {257// update the generation and space performance counters258update_counters();259if (ZapUnusedHeapArea) {260_the_space->check_mangled_unused_area_complete();261}262}263264void TenuredGeneration::record_spaces_top() {265assert(ZapUnusedHeapArea, "Not mangling unused space");266_the_space->set_top_for_allocations();267}268269void TenuredGeneration::verify() {270_the_space->verify();271}272273void TenuredGeneration::print_on(outputStream* st) const {274Generation::print_on(st);275st->print(" the");276_the_space->print_on(st);277}278279280