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