< prev index next >

src/hotspot/share/gc/shared/threadLocalAllocBuffer.cpp

Print this page
rev 50392 : JEP 331


  28 #include "memory/resourceArea.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "runtime/threadSMR.hpp"
  33 #include "utilities/copy.hpp"
  34 
  35 // Thread-Local Edens support
  36 
  37 // static member initialization
  38 size_t           ThreadLocalAllocBuffer::_max_size       = 0;
  39 int              ThreadLocalAllocBuffer::_reserve_for_allocation_prefetch = 0;
  40 unsigned         ThreadLocalAllocBuffer::_target_refills = 0;
  41 GlobalTLABStats* ThreadLocalAllocBuffer::_global_stats   = NULL;
  42 
  43 void ThreadLocalAllocBuffer::clear_before_allocation() {
  44   _slow_refill_waste += (unsigned)remaining();
  45   make_parsable(true);   // also retire the TLAB
  46 }
  47 








  48 void ThreadLocalAllocBuffer::accumulate_statistics_before_gc() {
  49   global_stats()->initialize();
  50 
  51   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
  52     thread->tlab().accumulate_statistics();
  53     thread->tlab().initialize_statistics();
  54   }
  55 
  56   // Publish new stats if some allocation occurred.
  57   if (global_stats()->allocation() != 0) {
  58     global_stats()->publish();
  59     global_stats()->print();
  60   }
  61 }
  62 
  63 void ThreadLocalAllocBuffer::accumulate_statistics() {
  64   Thread* thread = myThread();
  65   size_t capacity = Universe::heap()->tlab_capacity(thread);
  66   size_t used     = Universe::heap()->tlab_used(thread);
  67 


 104 
 105 // Fills the current tlab with a dummy filler array to create
 106 // an illusion of a contiguous Eden and optionally retires the tlab.
 107 // Waste accounting should be done in caller as appropriate; see,
 108 // for example, clear_before_allocation().
 109 void ThreadLocalAllocBuffer::make_parsable(bool retire, bool zap) {
 110   if (end() != NULL) {
 111     invariants();
 112 
 113     if (retire) {
 114       myThread()->incr_allocated_bytes(used_bytes());
 115     }
 116 
 117     CollectedHeap::fill_with_object(top(), hard_end(), retire && zap);
 118 
 119     if (retire || ZeroTLAB) {  // "Reset" the TLAB
 120       set_start(NULL);
 121       set_top(NULL);
 122       set_pf_top(NULL);
 123       set_end(NULL);

 124     }
 125   }
 126   assert(!(retire || ZeroTLAB)  ||
 127          (start() == NULL && end() == NULL && top() == NULL),

 128          "TLAB must be reset");
 129 }
 130 
 131 void ThreadLocalAllocBuffer::resize_all_tlabs() {
 132   if (ResizeTLAB) {
 133     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
 134       thread->tlab().resize();
 135     }
 136   }
 137 }
 138 
 139 void ThreadLocalAllocBuffer::resize() {
 140   // Compute the next tlab size using expected allocation amount
 141   assert(ResizeTLAB, "Should not call this otherwise");
 142   size_t alloc = (size_t)(_allocation_fraction.average() *
 143                           (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize));
 144   size_t new_size = alloc / _target_refills;
 145 
 146   new_size = MIN2(MAX2(new_size, min_size()), max_size());
 147 


 155   set_desired_size(aligned_new_size);
 156   set_refill_waste_limit(initial_refill_waste_limit());
 157 }
 158 
 159 void ThreadLocalAllocBuffer::initialize_statistics() {
 160   _number_of_refills = 0;
 161   _fast_refill_waste = 0;
 162   _slow_refill_waste = 0;
 163   _gc_waste          = 0;
 164   _slow_allocations  = 0;
 165   _allocated_size    = 0;
 166 }
 167 
 168 void ThreadLocalAllocBuffer::fill(HeapWord* start,
 169                                   HeapWord* top,
 170                                   size_t    new_size) {
 171   _number_of_refills++;
 172   _allocated_size += new_size;
 173   print_stats("fill");
 174   assert(top <= start + new_size - alignment_reserve(), "size too small");

 175   initialize(start, top, start + new_size - alignment_reserve());
 176 




 177   // Reset amount of internal fragmentation
 178   set_refill_waste_limit(initial_refill_waste_limit());
 179 }
 180 
 181 void ThreadLocalAllocBuffer::initialize(HeapWord* start,
 182                                         HeapWord* top,
 183                                         HeapWord* end) {
 184   set_start(start);
 185   set_top(top);
 186   set_pf_top(top);
 187   set_end(end);

 188   invariants();
 189 }
 190 
 191 void ThreadLocalAllocBuffer::initialize() {
 192   initialize(NULL,                    // start
 193              NULL,                    // top
 194              NULL);                   // end
 195 
 196   set_desired_size(initial_desired_size());
 197 
 198   // Following check is needed because at startup the main
 199   // thread is initialized before the heap is.  The initialization for
 200   // this thread is redone in startup_initialization below.
 201   if (Universe::heap() != NULL) {
 202     size_t capacity   = Universe::heap()->tlab_capacity(myThread()) / HeapWordSize;
 203     double alloc_frac = desired_size() * target_refills() / (double) capacity;
 204     _allocation_fraction.sample(alloc_frac);
 205   }
 206 
 207   set_refill_waste_limit(initial_refill_waste_limit());


 289             _allocation_fraction.average(),
 290             _allocation_fraction.average() * tlab_used / K,
 291             _number_of_refills, waste_percent,
 292             _gc_waste * HeapWordSize,
 293             _slow_refill_waste * HeapWordSize,
 294             _fast_refill_waste * HeapWordSize);
 295 }
 296 
 297 void ThreadLocalAllocBuffer::verify() {
 298   HeapWord* p = start();
 299   HeapWord* t = top();
 300   HeapWord* prev_p = NULL;
 301   while (p < t) {
 302     oop(p)->verify();
 303     prev_p = p;
 304     p += oop(p)->size();
 305   }
 306   guarantee(p == top(), "end of last object must match end of space");
 307 }
 308 














 309 Thread* ThreadLocalAllocBuffer::myThread() {
 310   return (Thread*)(((char *)this) +
 311                    in_bytes(start_offset()) -
 312                    in_bytes(Thread::tlab_start_offset()));
 313 }
 314 



















 315 
 316 GlobalTLABStats::GlobalTLABStats() :
 317   _allocating_threads_avg(TLABAllocationWeight) {
 318 
 319   initialize();
 320 
 321   _allocating_threads_avg.sample(1); // One allocating thread at startup
 322 
 323   if (UsePerfData) {
 324 
 325     EXCEPTION_MARK;
 326     ResourceMark rm;
 327 
 328     char* cname = PerfDataManager::counter_name("tlab", "allocThreads");
 329     _perf_allocating_threads =
 330       PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
 331 
 332     cname = PerfDataManager::counter_name("tlab", "fills");
 333     _perf_total_refills =
 334       PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);




  28 #include "memory/resourceArea.hpp"
  29 #include "memory/universe.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "runtime/threadSMR.hpp"
  33 #include "utilities/copy.hpp"
  34 
  35 // Thread-Local Edens support
  36 
  37 // static member initialization
  38 size_t           ThreadLocalAllocBuffer::_max_size       = 0;
  39 int              ThreadLocalAllocBuffer::_reserve_for_allocation_prefetch = 0;
  40 unsigned         ThreadLocalAllocBuffer::_target_refills = 0;
  41 GlobalTLABStats* ThreadLocalAllocBuffer::_global_stats   = NULL;
  42 
  43 void ThreadLocalAllocBuffer::clear_before_allocation() {
  44   _slow_refill_waste += (unsigned)remaining();
  45   make_parsable(true);   // also retire the TLAB
  46 }
  47 
  48 size_t ThreadLocalAllocBuffer::remaining() {
  49   if (end() == NULL) {
  50     return 0;
  51   }
  52 
  53   return pointer_delta(hard_end(), top());
  54 }
  55 
  56 void ThreadLocalAllocBuffer::accumulate_statistics_before_gc() {
  57   global_stats()->initialize();
  58 
  59   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
  60     thread->tlab().accumulate_statistics();
  61     thread->tlab().initialize_statistics();
  62   }
  63 
  64   // Publish new stats if some allocation occurred.
  65   if (global_stats()->allocation() != 0) {
  66     global_stats()->publish();
  67     global_stats()->print();
  68   }
  69 }
  70 
  71 void ThreadLocalAllocBuffer::accumulate_statistics() {
  72   Thread* thread = myThread();
  73   size_t capacity = Universe::heap()->tlab_capacity(thread);
  74   size_t used     = Universe::heap()->tlab_used(thread);
  75 


 112 
 113 // Fills the current tlab with a dummy filler array to create
 114 // an illusion of a contiguous Eden and optionally retires the tlab.
 115 // Waste accounting should be done in caller as appropriate; see,
 116 // for example, clear_before_allocation().
 117 void ThreadLocalAllocBuffer::make_parsable(bool retire, bool zap) {
 118   if (end() != NULL) {
 119     invariants();
 120 
 121     if (retire) {
 122       myThread()->incr_allocated_bytes(used_bytes());
 123     }
 124 
 125     CollectedHeap::fill_with_object(top(), hard_end(), retire && zap);
 126 
 127     if (retire || ZeroTLAB) {  // "Reset" the TLAB
 128       set_start(NULL);
 129       set_top(NULL);
 130       set_pf_top(NULL);
 131       set_end(NULL);
 132       set_allocation_end(NULL);
 133     }
 134   }
 135   assert(!(retire || ZeroTLAB)  ||
 136          (start() == NULL && end() == NULL && top() == NULL &&
 137           _allocation_end == NULL),
 138          "TLAB must be reset");
 139 }
 140 
 141 void ThreadLocalAllocBuffer::resize_all_tlabs() {
 142   if (ResizeTLAB) {
 143     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thread = jtiwh.next(); ) {
 144       thread->tlab().resize();
 145     }
 146   }
 147 }
 148 
 149 void ThreadLocalAllocBuffer::resize() {
 150   // Compute the next tlab size using expected allocation amount
 151   assert(ResizeTLAB, "Should not call this otherwise");
 152   size_t alloc = (size_t)(_allocation_fraction.average() *
 153                           (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize));
 154   size_t new_size = alloc / _target_refills;
 155 
 156   new_size = MIN2(MAX2(new_size, min_size()), max_size());
 157 


 165   set_desired_size(aligned_new_size);
 166   set_refill_waste_limit(initial_refill_waste_limit());
 167 }
 168 
 169 void ThreadLocalAllocBuffer::initialize_statistics() {
 170   _number_of_refills = 0;
 171   _fast_refill_waste = 0;
 172   _slow_refill_waste = 0;
 173   _gc_waste          = 0;
 174   _slow_allocations  = 0;
 175   _allocated_size    = 0;
 176 }
 177 
 178 void ThreadLocalAllocBuffer::fill(HeapWord* start,
 179                                   HeapWord* top,
 180                                   size_t    new_size) {
 181   _number_of_refills++;
 182   _allocated_size += new_size;
 183   print_stats("fill");
 184   assert(top <= start + new_size - alignment_reserve(), "size too small");
 185 
 186   initialize(start, top, start + new_size - alignment_reserve());
 187 
 188   if (ThreadHeapSampler::enabled()) {
 189     set_sample_end();
 190   }
 191 
 192   // Reset amount of internal fragmentation
 193   set_refill_waste_limit(initial_refill_waste_limit());
 194 }
 195 
 196 void ThreadLocalAllocBuffer::initialize(HeapWord* start,
 197                                         HeapWord* top,
 198                                         HeapWord* end) {
 199   set_start(start);
 200   set_top(top);
 201   set_pf_top(top);
 202   set_end(end);
 203   set_allocation_end(end);
 204   invariants();
 205 }
 206 
 207 void ThreadLocalAllocBuffer::initialize() {
 208   initialize(NULL,                    // start
 209              NULL,                    // top
 210              NULL);                   // end
 211 
 212   set_desired_size(initial_desired_size());
 213 
 214   // Following check is needed because at startup the main
 215   // thread is initialized before the heap is.  The initialization for
 216   // this thread is redone in startup_initialization below.
 217   if (Universe::heap() != NULL) {
 218     size_t capacity   = Universe::heap()->tlab_capacity(myThread()) / HeapWordSize;
 219     double alloc_frac = desired_size() * target_refills() / (double) capacity;
 220     _allocation_fraction.sample(alloc_frac);
 221   }
 222 
 223   set_refill_waste_limit(initial_refill_waste_limit());


 305             _allocation_fraction.average(),
 306             _allocation_fraction.average() * tlab_used / K,
 307             _number_of_refills, waste_percent,
 308             _gc_waste * HeapWordSize,
 309             _slow_refill_waste * HeapWordSize,
 310             _fast_refill_waste * HeapWordSize);
 311 }
 312 
 313 void ThreadLocalAllocBuffer::verify() {
 314   HeapWord* p = start();
 315   HeapWord* t = top();
 316   HeapWord* prev_p = NULL;
 317   while (p < t) {
 318     oop(p)->verify();
 319     prev_p = p;
 320     p += oop(p)->size();
 321   }
 322   guarantee(p == top(), "end of last object must match end of space");
 323 }
 324 
 325 void ThreadLocalAllocBuffer::set_sample_end() {
 326   size_t heap_words_remaining = pointer_delta(_end, _top);
 327   size_t bytes_until_sample = myThread()->heap_sampler().bytes_until_sample();
 328   size_t words_until_sample = bytes_until_sample / HeapWordSize;;
 329 
 330   if (heap_words_remaining > words_until_sample) {
 331     HeapWord* new_end = _top + words_until_sample;
 332     set_end(new_end);
 333     _bytes_since_last_sample_point = bytes_until_sample;
 334   } else {
 335     _bytes_since_last_sample_point = heap_words_remaining * HeapWordSize;;
 336   }
 337 }
 338 
 339 Thread* ThreadLocalAllocBuffer::myThread() {
 340   return (Thread*)(((char *)this) +
 341                    in_bytes(start_offset()) -
 342                    in_bytes(Thread::tlab_start_offset()));
 343 }
 344 
 345 void ThreadLocalAllocBuffer::set_back_allocation_end() {
 346   _end = _allocation_end;
 347 }
 348 
 349 HeapWord* ThreadLocalAllocBuffer::allocate_sampled_object(size_t size) {
 350   set_back_allocation_end();
 351   HeapWord* result = allocate(size);
 352 
 353   if (result) {
 354     myThread()->heap_sampler().check_for_sampling(result, size * HeapWordSize, _bytes_since_last_sample_point);
 355     set_sample_end();
 356   }
 357 
 358   return result;
 359 }
 360 
 361 HeapWord* ThreadLocalAllocBuffer::hard_end() {
 362   return _allocation_end + alignment_reserve();
 363 }
 364 
 365 GlobalTLABStats::GlobalTLABStats() :
 366   _allocating_threads_avg(TLABAllocationWeight) {
 367 
 368   initialize();
 369 
 370   _allocating_threads_avg.sample(1); // One allocating thread at startup
 371 
 372   if (UsePerfData) {
 373 
 374     EXCEPTION_MARK;
 375     ResourceMark rm;
 376 
 377     char* cname = PerfDataManager::counter_name("tlab", "allocThreads");
 378     _perf_allocating_threads =
 379       PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
 380 
 381     cname = PerfDataManager::counter_name("tlab", "fills");
 382     _perf_total_refills =
 383       PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);


< prev index next >