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