aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2012, 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: #ifndef SHARE_VM_MEMORY_THREADLOCALALLOCBUFFER_HPP aoqi@0: #define SHARE_VM_MEMORY_THREADLOCALALLOCBUFFER_HPP aoqi@0: aoqi@0: #include "gc_implementation/shared/gcUtil.hpp" aoqi@0: #include "oops/typeArrayOop.hpp" aoqi@0: #include "runtime/perfData.hpp" aoqi@0: aoqi@0: class GlobalTLABStats; aoqi@0: aoqi@0: // ThreadLocalAllocBuffer: a descriptor for thread-local storage used by aoqi@0: // the threads for allocation. aoqi@0: // It is thread-private at any time, but maybe multiplexed over aoqi@0: // time across multiple threads. The park()/unpark() pair is aoqi@0: // used to make it available for such multiplexing. aoqi@0: class ThreadLocalAllocBuffer: public CHeapObj { aoqi@0: friend class VMStructs; aoqi@0: private: aoqi@0: HeapWord* _start; // address of TLAB aoqi@0: HeapWord* _top; // address after last allocation aoqi@0: HeapWord* _pf_top; // allocation prefetch watermark aoqi@0: HeapWord* _end; // allocation end (excluding alignment_reserve) aoqi@0: size_t _desired_size; // desired size (including alignment_reserve) aoqi@0: size_t _refill_waste_limit; // hold onto tlab if free() is larger than this aoqi@0: size_t _allocated_before_last_gc; // total bytes allocated up until the last gc aoqi@0: aoqi@0: static size_t _max_size; // maximum size of any TLAB aoqi@0: static unsigned _target_refills; // expected number of refills between GCs aoqi@0: aoqi@0: unsigned _number_of_refills; aoqi@0: unsigned _fast_refill_waste; aoqi@0: unsigned _slow_refill_waste; aoqi@0: unsigned _gc_waste; aoqi@0: unsigned _slow_allocations; aoqi@0: aoqi@0: AdaptiveWeightedAverage _allocation_fraction; // fraction of eden allocated in tlabs aoqi@0: aoqi@0: void accumulate_statistics(); aoqi@0: void initialize_statistics(); aoqi@0: aoqi@0: void set_start(HeapWord* start) { _start = start; } aoqi@0: void set_end(HeapWord* end) { _end = end; } aoqi@0: void set_top(HeapWord* top) { _top = top; } aoqi@0: void set_pf_top(HeapWord* pf_top) { _pf_top = pf_top; } aoqi@0: void set_desired_size(size_t desired_size) { _desired_size = desired_size; } aoqi@0: void set_refill_waste_limit(size_t waste) { _refill_waste_limit = waste; } aoqi@0: aoqi@0: size_t initial_refill_waste_limit() { return desired_size() / TLABRefillWasteFraction; } aoqi@0: aoqi@0: static int target_refills() { return _target_refills; } aoqi@0: size_t initial_desired_size(); aoqi@0: aoqi@0: size_t remaining() const { return end() == NULL ? 0 : pointer_delta(hard_end(), top()); } aoqi@0: aoqi@0: // Make parsable and release it. aoqi@0: void reset(); aoqi@0: aoqi@0: // Resize based on amount of allocation, etc. aoqi@0: void resize(); aoqi@0: aoqi@0: void invariants() const { assert(top() >= start() && top() <= end(), "invalid tlab"); } aoqi@0: aoqi@0: void initialize(HeapWord* start, HeapWord* top, HeapWord* end); aoqi@0: aoqi@0: void print_stats(const char* tag); aoqi@0: aoqi@0: Thread* myThread(); aoqi@0: aoqi@0: // statistics aoqi@0: aoqi@0: int number_of_refills() const { return _number_of_refills; } aoqi@0: int fast_refill_waste() const { return _fast_refill_waste; } aoqi@0: int slow_refill_waste() const { return _slow_refill_waste; } aoqi@0: int gc_waste() const { return _gc_waste; } aoqi@0: int slow_allocations() const { return _slow_allocations; } aoqi@0: aoqi@0: static GlobalTLABStats* _global_stats; aoqi@0: static GlobalTLABStats* global_stats() { return _global_stats; } aoqi@0: aoqi@0: public: aoqi@0: ThreadLocalAllocBuffer() : _allocation_fraction(TLABAllocationWeight), _allocated_before_last_gc(0) { aoqi@0: // do nothing. tlabs must be inited by initialize() calls aoqi@0: } aoqi@0: aoqi@0: static const size_t min_size() { return align_object_size(MinTLABSize / HeapWordSize); } aoqi@0: static const size_t max_size() { assert(_max_size != 0, "max_size not set up"); return _max_size; } aoqi@0: static void set_max_size(size_t max_size) { _max_size = max_size; } aoqi@0: aoqi@0: HeapWord* start() const { return _start; } aoqi@0: HeapWord* end() const { return _end; } aoqi@0: HeapWord* hard_end() const { return _end + alignment_reserve(); } aoqi@0: HeapWord* top() const { return _top; } aoqi@0: HeapWord* pf_top() const { return _pf_top; } aoqi@0: size_t desired_size() const { return _desired_size; } aoqi@0: size_t used() const { return pointer_delta(top(), start()); } aoqi@0: size_t used_bytes() const { return pointer_delta(top(), start(), 1); } aoqi@0: size_t free() const { return pointer_delta(end(), top()); } aoqi@0: // Don't discard tlab if remaining space is larger than this. aoqi@0: size_t refill_waste_limit() const { return _refill_waste_limit; } aoqi@0: aoqi@0: // Allocate size HeapWords. The memory is NOT initialized to zero. aoqi@0: inline HeapWord* allocate(size_t size); aoqi@0: aoqi@0: // Reserve space at the end of TLAB aoqi@0: static size_t end_reserve() { aoqi@0: int reserve_size = typeArrayOopDesc::header_size(T_INT); aoqi@0: return MAX2(reserve_size, VM_Version::reserve_for_allocation_prefetch()); aoqi@0: } aoqi@0: static size_t alignment_reserve() { return align_object_size(end_reserve()); } aoqi@0: static size_t alignment_reserve_in_bytes() { return alignment_reserve() * HeapWordSize; } aoqi@0: aoqi@0: // Return tlab size or remaining space in eden such that the aoqi@0: // space is large enough to hold obj_size and necessary fill space. aoqi@0: // Otherwise return 0; aoqi@0: inline size_t compute_size(size_t obj_size); aoqi@0: aoqi@0: // Record slow allocation aoqi@0: inline void record_slow_allocation(size_t obj_size); aoqi@0: aoqi@0: // Initialization at startup aoqi@0: static void startup_initialization(); aoqi@0: aoqi@0: // Make an in-use tlab parsable, optionally also retiring it. aoqi@0: void make_parsable(bool retire); aoqi@0: aoqi@0: // Retire in-use tlab before allocation of a new tlab aoqi@0: void clear_before_allocation(); aoqi@0: aoqi@0: // Accumulate statistics across all tlabs before gc aoqi@0: static void accumulate_statistics_before_gc(); aoqi@0: aoqi@0: // Resize tlabs for all threads aoqi@0: static void resize_all_tlabs(); aoqi@0: aoqi@0: void fill(HeapWord* start, HeapWord* top, size_t new_size); aoqi@0: void initialize(); aoqi@0: aoqi@0: static size_t refill_waste_limit_increment() { return TLABWasteIncrement; } aoqi@0: aoqi@0: // Code generation support aoqi@0: static ByteSize start_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _start); } aoqi@0: static ByteSize end_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _end ); } aoqi@0: static ByteSize top_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _top ); } aoqi@0: static ByteSize pf_top_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _pf_top ); } aoqi@0: static ByteSize size_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _desired_size ); } aoqi@0: static ByteSize refill_waste_limit_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _refill_waste_limit ); } aoqi@0: aoqi@0: static ByteSize number_of_refills_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _number_of_refills ); } aoqi@0: static ByteSize fast_refill_waste_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _fast_refill_waste ); } aoqi@0: static ByteSize slow_allocations_offset() { return byte_offset_of(ThreadLocalAllocBuffer, _slow_allocations ); } aoqi@0: aoqi@0: void verify(); aoqi@0: }; aoqi@0: aoqi@0: class GlobalTLABStats: public CHeapObj { aoqi@0: private: aoqi@0: aoqi@0: // Accumulate perfdata in private variables because aoqi@0: // PerfData should be write-only for security reasons aoqi@0: // (see perfData.hpp) aoqi@0: unsigned _allocating_threads; aoqi@0: unsigned _total_refills; aoqi@0: unsigned _max_refills; aoqi@0: size_t _total_allocation; aoqi@0: size_t _total_gc_waste; aoqi@0: size_t _max_gc_waste; aoqi@0: size_t _total_slow_refill_waste; aoqi@0: size_t _max_slow_refill_waste; aoqi@0: size_t _total_fast_refill_waste; aoqi@0: size_t _max_fast_refill_waste; aoqi@0: unsigned _total_slow_allocations; aoqi@0: unsigned _max_slow_allocations; aoqi@0: aoqi@0: PerfVariable* _perf_allocating_threads; aoqi@0: PerfVariable* _perf_total_refills; aoqi@0: PerfVariable* _perf_max_refills; aoqi@0: PerfVariable* _perf_allocation; aoqi@0: PerfVariable* _perf_gc_waste; aoqi@0: PerfVariable* _perf_max_gc_waste; aoqi@0: PerfVariable* _perf_slow_refill_waste; aoqi@0: PerfVariable* _perf_max_slow_refill_waste; aoqi@0: PerfVariable* _perf_fast_refill_waste; aoqi@0: PerfVariable* _perf_max_fast_refill_waste; aoqi@0: PerfVariable* _perf_slow_allocations; aoqi@0: PerfVariable* _perf_max_slow_allocations; aoqi@0: aoqi@0: AdaptiveWeightedAverage _allocating_threads_avg; aoqi@0: aoqi@0: public: aoqi@0: GlobalTLABStats(); aoqi@0: aoqi@0: // Initialize all counters aoqi@0: void initialize(); aoqi@0: aoqi@0: // Write all perf counters to the perf_counters aoqi@0: void publish(); aoqi@0: aoqi@0: void print(); aoqi@0: aoqi@0: // Accessors aoqi@0: unsigned allocating_threads_avg() { aoqi@0: return MAX2((unsigned)(_allocating_threads_avg.average() + 0.5), 1U); aoqi@0: } aoqi@0: aoqi@0: size_t allocation() { aoqi@0: return _total_allocation; aoqi@0: } aoqi@0: aoqi@0: // Update methods aoqi@0: aoqi@0: void update_allocating_threads() { aoqi@0: _allocating_threads++; aoqi@0: } aoqi@0: void update_number_of_refills(unsigned value) { aoqi@0: _total_refills += value; aoqi@0: _max_refills = MAX2(_max_refills, value); aoqi@0: } aoqi@0: void update_allocation(size_t value) { aoqi@0: _total_allocation += value; aoqi@0: } aoqi@0: void update_gc_waste(size_t value) { aoqi@0: _total_gc_waste += value; aoqi@0: _max_gc_waste = MAX2(_max_gc_waste, value); aoqi@0: } aoqi@0: void update_fast_refill_waste(size_t value) { aoqi@0: _total_fast_refill_waste += value; aoqi@0: _max_fast_refill_waste = MAX2(_max_fast_refill_waste, value); aoqi@0: } aoqi@0: void update_slow_refill_waste(size_t value) { aoqi@0: _total_slow_refill_waste += value; aoqi@0: _max_slow_refill_waste = MAX2(_max_slow_refill_waste, value); aoqi@0: } aoqi@0: void update_slow_allocations(unsigned value) { aoqi@0: _total_slow_allocations += value; aoqi@0: _max_slow_allocations = MAX2(_max_slow_allocations, value); aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_MEMORY_THREADLOCALALLOCBUFFER_HPP