duke@435: /* poonam@9965: * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "memory/genCollectedHeap.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "memory/threadLocalAllocBuffer.inline.hpp" stefank@2314: #include "memory/universe.inline.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@4299: #include "runtime/thread.inline.hpp" stefank@2314: #include "utilities/copy.hpp" stefank@2314: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: duke@435: // Thread-Local Edens support duke@435: duke@435: // static member initialization brutisso@6376: size_t ThreadLocalAllocBuffer::_max_size = 0; duke@435: unsigned ThreadLocalAllocBuffer::_target_refills = 0; duke@435: GlobalTLABStats* ThreadLocalAllocBuffer::_global_stats = NULL; duke@435: duke@435: void ThreadLocalAllocBuffer::clear_before_allocation() { duke@435: _slow_refill_waste += (unsigned)remaining(); duke@435: make_parsable(true); // also retire the TLAB duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::accumulate_statistics_before_gc() { duke@435: global_stats()->initialize(); duke@435: brutisso@6376: for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { duke@435: thread->tlab().accumulate_statistics(); duke@435: thread->tlab().initialize_statistics(); duke@435: } duke@435: duke@435: // Publish new stats if some allocation occurred. duke@435: if (global_stats()->allocation() != 0) { duke@435: global_stats()->publish(); duke@435: if (PrintTLAB) { duke@435: global_stats()->print(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::accumulate_statistics() { brutisso@6376: Thread* thread = myThread(); brutisso@6376: size_t capacity = Universe::heap()->tlab_capacity(thread); brutisso@6376: size_t used = Universe::heap()->tlab_used(thread); duke@435: duke@435: _gc_waste += (unsigned)remaining(); brutisso@6376: size_t total_allocated = thread->allocated_bytes(); brutisso@6376: size_t allocated_since_last_gc = total_allocated - _allocated_before_last_gc; brutisso@6376: _allocated_before_last_gc = total_allocated; duke@435: duke@435: if (PrintTLAB && (_number_of_refills > 0 || Verbose)) { duke@435: print_stats("gc"); duke@435: } duke@435: duke@435: if (_number_of_refills > 0) { brutisso@6376: // Update allocation history if a reasonable amount of eden was allocated. brutisso@6376: bool update_allocation_history = used > 0.5 * capacity; duke@435: duke@435: if (update_allocation_history) { duke@435: // Average the fraction of eden allocated in a tlab by this duke@435: // thread for use in the next resize operation. duke@435: // _gc_waste is not subtracted because it's included in duke@435: // "used". brutisso@6376: // The result can be larger than 1.0 due to direct to old allocations. brutisso@6376: // These allocations should ideally not be counted but since it is not possible brutisso@6376: // to filter them out here we just cap the fraction to be at most 1.0. poonam@9965: // Keep alloc_frac as float and not double to avoid the double to float conversion poonam@9965: float alloc_frac = MIN2(1.0f, allocated_since_last_gc / (float) used); duke@435: _allocation_fraction.sample(alloc_frac); duke@435: } duke@435: global_stats()->update_allocating_threads(); duke@435: global_stats()->update_number_of_refills(_number_of_refills); duke@435: global_stats()->update_allocation(_number_of_refills * desired_size()); duke@435: global_stats()->update_gc_waste(_gc_waste); duke@435: global_stats()->update_slow_refill_waste(_slow_refill_waste); duke@435: global_stats()->update_fast_refill_waste(_fast_refill_waste); duke@435: duke@435: } else { duke@435: assert(_number_of_refills == 0 && _fast_refill_waste == 0 && duke@435: _slow_refill_waste == 0 && _gc_waste == 0, duke@435: "tlab stats == 0"); duke@435: } duke@435: global_stats()->update_slow_allocations(_slow_allocations); duke@435: } duke@435: duke@435: // Fills the current tlab with a dummy filler array to create duke@435: // an illusion of a contiguous Eden and optionally retires the tlab. duke@435: // Waste accounting should be done in caller as appropriate; see, duke@435: // for example, clear_before_allocation(). duke@435: void ThreadLocalAllocBuffer::make_parsable(bool retire) { duke@435: if (end() != NULL) { duke@435: invariants(); phh@2423: phh@2423: if (retire) { phh@2423: myThread()->incr_allocated_bytes(used_bytes()); phh@2423: } phh@2423: johnc@1600: CollectedHeap::fill_with_object(top(), hard_end(), retire); duke@435: duke@435: if (retire || ZeroTLAB) { // "Reset" the TLAB duke@435: set_start(NULL); duke@435: set_top(NULL); duke@435: set_pf_top(NULL); duke@435: set_end(NULL); duke@435: } duke@435: } duke@435: assert(!(retire || ZeroTLAB) || duke@435: (start() == NULL && end() == NULL && top() == NULL), duke@435: "TLAB must be reset"); duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::resize_all_tlabs() { brutisso@6376: if (ResizeTLAB) { brutisso@6376: for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { brutisso@6376: thread->tlab().resize(); brutisso@6376: } duke@435: } duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::resize() { brutisso@6376: // Compute the next tlab size using expected allocation amount brutisso@6376: assert(ResizeTLAB, "Should not call this otherwise"); brutisso@6376: size_t alloc = (size_t)(_allocation_fraction.average() * brutisso@6376: (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize)); brutisso@6376: size_t new_size = alloc / _target_refills; duke@435: brutisso@6376: new_size = MIN2(MAX2(new_size, min_size()), max_size()); duke@435: brutisso@6376: size_t aligned_new_size = align_object_size(new_size); duke@435: brutisso@6376: if (PrintTLAB && Verbose) { brutisso@6376: gclog_or_tty->print("TLAB new size: thread: " INTPTR_FORMAT " [id: %2d]" brutisso@6376: " refills %d alloc: %8.6f desired_size: " SIZE_FORMAT " -> " SIZE_FORMAT "\n", brutisso@6376: myThread(), myThread()->osthread()->thread_id(), brutisso@6376: _target_refills, _allocation_fraction.average(), desired_size(), aligned_new_size); duke@435: } brutisso@6376: set_desired_size(aligned_new_size); brutisso@6376: set_refill_waste_limit(initial_refill_waste_limit()); duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::initialize_statistics() { duke@435: _number_of_refills = 0; duke@435: _fast_refill_waste = 0; duke@435: _slow_refill_waste = 0; duke@435: _gc_waste = 0; duke@435: _slow_allocations = 0; duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::fill(HeapWord* start, duke@435: HeapWord* top, duke@435: size_t new_size) { duke@435: _number_of_refills++; duke@435: if (PrintTLAB && Verbose) { duke@435: print_stats("fill"); duke@435: } duke@435: assert(top <= start + new_size - alignment_reserve(), "size too small"); duke@435: initialize(start, top, start + new_size - alignment_reserve()); duke@435: duke@435: // Reset amount of internal fragmentation duke@435: set_refill_waste_limit(initial_refill_waste_limit()); duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::initialize(HeapWord* start, duke@435: HeapWord* top, duke@435: HeapWord* end) { duke@435: set_start(start); duke@435: set_top(top); duke@435: set_pf_top(top); duke@435: set_end(end); duke@435: invariants(); duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::initialize() { duke@435: initialize(NULL, // start duke@435: NULL, // top duke@435: NULL); // end duke@435: duke@435: set_desired_size(initial_desired_size()); duke@435: dbuck@9289: // Following check is needed because at startup the main duke@435: // thread is initialized before the heap is. The initialization for duke@435: // this thread is redone in startup_initialization below. duke@435: if (Universe::heap() != NULL) { duke@435: size_t capacity = Universe::heap()->tlab_capacity(myThread()) / HeapWordSize; poonam@9965: // Keep alloc_frac as float and not double to avoid the double to float conversion poonam@9965: float alloc_frac = desired_size() * target_refills() / (float) capacity; duke@435: _allocation_fraction.sample(alloc_frac); duke@435: } duke@435: duke@435: set_refill_waste_limit(initial_refill_waste_limit()); duke@435: duke@435: initialize_statistics(); duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::startup_initialization() { duke@435: duke@435: // Assuming each thread's active tlab is, on average, duke@435: // 1/2 full at a GC duke@435: _target_refills = 100 / (2 * TLABWasteTargetPercent); duke@435: _target_refills = MAX2(_target_refills, (unsigned)1U); duke@435: duke@435: _global_stats = new GlobalTLABStats(); duke@435: dbuck@9289: // During jvm startup, the main thread is initialized duke@435: // before the heap is initialized. So reinitialize it now. duke@435: guarantee(Thread::current()->is_Java_thread(), "tlab initialization thread not Java thread"); duke@435: Thread::current()->tlab().initialize(); duke@435: duke@435: if (PrintTLAB && Verbose) { duke@435: gclog_or_tty->print("TLAB min: " SIZE_FORMAT " initial: " SIZE_FORMAT " max: " SIZE_FORMAT "\n", duke@435: min_size(), Thread::current()->tlab().initial_desired_size(), max_size()); duke@435: } duke@435: } duke@435: duke@435: size_t ThreadLocalAllocBuffer::initial_desired_size() { mgerdin@7470: size_t init_sz = 0; duke@435: duke@435: if (TLABSize > 0) { mgerdin@7470: init_sz = TLABSize / HeapWordSize; mgerdin@7470: } else if (global_stats() != NULL) { duke@435: // Initial size is a function of the average number of allocating threads. duke@435: unsigned nof_threads = global_stats()->allocating_threads_avg(); duke@435: duke@435: init_sz = (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize) / duke@435: (nof_threads * target_refills()); duke@435: init_sz = align_object_size(init_sz); duke@435: } mgerdin@7470: init_sz = MIN2(MAX2(init_sz, min_size()), max_size()); duke@435: return init_sz; duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::print_stats(const char* tag) { duke@435: Thread* thrd = myThread(); duke@435: size_t waste = _gc_waste + _slow_refill_waste + _fast_refill_waste; duke@435: size_t alloc = _number_of_refills * _desired_size; duke@435: double waste_percent = alloc == 0 ? 0.0 : duke@435: 100.0 * waste / alloc; brutisso@6376: size_t tlab_used = Universe::heap()->tlab_used(thrd); duke@435: gclog_or_tty->print("TLAB: %s thread: " INTPTR_FORMAT " [id: %2d]" duke@435: " desired_size: " SIZE_FORMAT "KB" duke@435: " slow allocs: %d refill waste: " SIZE_FORMAT "B" duke@435: " alloc:%8.5f %8.0fKB refills: %d waste %4.1f%% gc: %dB" duke@435: " slow: %dB fast: %dB\n", duke@435: tag, thrd, thrd->osthread()->thread_id(), duke@435: _desired_size / (K / HeapWordSize), duke@435: _slow_allocations, _refill_waste_limit * HeapWordSize, duke@435: _allocation_fraction.average(), duke@435: _allocation_fraction.average() * tlab_used / K, duke@435: _number_of_refills, waste_percent, duke@435: _gc_waste * HeapWordSize, duke@435: _slow_refill_waste * HeapWordSize, duke@435: _fast_refill_waste * HeapWordSize); duke@435: } duke@435: duke@435: void ThreadLocalAllocBuffer::verify() { duke@435: HeapWord* p = start(); duke@435: HeapWord* t = top(); duke@435: HeapWord* prev_p = NULL; duke@435: while (p < t) { duke@435: oop(p)->verify(); duke@435: prev_p = p; duke@435: p += oop(p)->size(); duke@435: } duke@435: guarantee(p == top(), "end of last object must match end of space"); duke@435: } duke@435: duke@435: Thread* ThreadLocalAllocBuffer::myThread() { duke@435: return (Thread*)(((char *)this) + duke@435: in_bytes(start_offset()) - duke@435: in_bytes(Thread::tlab_start_offset())); duke@435: } duke@435: duke@435: duke@435: GlobalTLABStats::GlobalTLABStats() : duke@435: _allocating_threads_avg(TLABAllocationWeight) { duke@435: duke@435: initialize(); duke@435: duke@435: _allocating_threads_avg.sample(1); // One allocating thread at startup duke@435: duke@435: if (UsePerfData) { duke@435: duke@435: EXCEPTION_MARK; duke@435: ResourceMark rm; duke@435: duke@435: char* cname = PerfDataManager::counter_name("tlab", "allocThreads"); duke@435: _perf_allocating_threads = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "fills"); duke@435: _perf_total_refills = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "maxFills"); duke@435: _perf_max_refills = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "alloc"); duke@435: _perf_allocation = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "gcWaste"); duke@435: _perf_gc_waste = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "maxGcWaste"); duke@435: _perf_max_gc_waste = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "slowWaste"); duke@435: _perf_slow_refill_waste = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "maxSlowWaste"); duke@435: _perf_max_slow_refill_waste = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "fastWaste"); duke@435: _perf_fast_refill_waste = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "maxFastWaste"); duke@435: _perf_max_fast_refill_waste = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "slowAlloc"); duke@435: _perf_slow_allocations = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); duke@435: duke@435: cname = PerfDataManager::counter_name("tlab", "maxSlowAlloc"); duke@435: _perf_max_slow_allocations = duke@435: PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); duke@435: } duke@435: } duke@435: duke@435: void GlobalTLABStats::initialize() { duke@435: // Clear counters summarizing info from all threads duke@435: _allocating_threads = 0; duke@435: _total_refills = 0; duke@435: _max_refills = 0; duke@435: _total_allocation = 0; duke@435: _total_gc_waste = 0; duke@435: _max_gc_waste = 0; duke@435: _total_slow_refill_waste = 0; duke@435: _max_slow_refill_waste = 0; duke@435: _total_fast_refill_waste = 0; duke@435: _max_fast_refill_waste = 0; duke@435: _total_slow_allocations = 0; duke@435: _max_slow_allocations = 0; duke@435: } duke@435: duke@435: void GlobalTLABStats::publish() { duke@435: _allocating_threads_avg.sample(_allocating_threads); duke@435: if (UsePerfData) { duke@435: _perf_allocating_threads ->set_value(_allocating_threads); duke@435: _perf_total_refills ->set_value(_total_refills); duke@435: _perf_max_refills ->set_value(_max_refills); duke@435: _perf_allocation ->set_value(_total_allocation); duke@435: _perf_gc_waste ->set_value(_total_gc_waste); duke@435: _perf_max_gc_waste ->set_value(_max_gc_waste); duke@435: _perf_slow_refill_waste ->set_value(_total_slow_refill_waste); duke@435: _perf_max_slow_refill_waste->set_value(_max_slow_refill_waste); duke@435: _perf_fast_refill_waste ->set_value(_total_fast_refill_waste); duke@435: _perf_max_fast_refill_waste->set_value(_max_fast_refill_waste); duke@435: _perf_slow_allocations ->set_value(_total_slow_allocations); duke@435: _perf_max_slow_allocations ->set_value(_max_slow_allocations); duke@435: } duke@435: } duke@435: duke@435: void GlobalTLABStats::print() { duke@435: size_t waste = _total_gc_waste + _total_slow_refill_waste + _total_fast_refill_waste; duke@435: double waste_percent = _total_allocation == 0 ? 0.0 : duke@435: 100.0 * waste / _total_allocation; duke@435: gclog_or_tty->print("TLAB totals: thrds: %d refills: %d max: %d" duke@435: " slow allocs: %d max %d waste: %4.1f%%" duke@435: " gc: " SIZE_FORMAT "B max: " SIZE_FORMAT "B" duke@435: " slow: " SIZE_FORMAT "B max: " SIZE_FORMAT "B" duke@435: " fast: " SIZE_FORMAT "B max: " SIZE_FORMAT "B\n", duke@435: _allocating_threads, duke@435: _total_refills, _max_refills, duke@435: _total_slow_allocations, _max_slow_allocations, duke@435: waste_percent, duke@435: _total_gc_waste * HeapWordSize, duke@435: _max_gc_waste * HeapWordSize, duke@435: _total_slow_refill_waste * HeapWordSize, duke@435: _max_slow_refill_waste * HeapWordSize, duke@435: _total_fast_refill_waste * HeapWordSize, duke@435: _max_fast_refill_waste * HeapWordSize); duke@435: }