rev 52072 : 8211955: GC abstraction for LAB reserve

   1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"

  26 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
  27 #include "logging/log.hpp"
  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 size_t       ThreadLocalAllocBuffer::_max_size = 0;
  36 int          ThreadLocalAllocBuffer::_reserve_for_allocation_prefetch = 0;
  37 unsigned int ThreadLocalAllocBuffer::_target_refills = 0;
  38 
  39 size_t ThreadLocalAllocBuffer::remaining() {
  40   if (end() == NULL) {
  41     return 0;
  42   }
  43 
  44   return pointer_delta(hard_end(), top());
  45 }
  46 
  47 void ThreadLocalAllocBuffer::accumulate_and_reset_statistics(ThreadLocalAllocStats* stats) {
  48   Thread* thr     = thread();
  49   size_t capacity = Universe::heap()->tlab_capacity(thr);
  50   size_t used     = Universe::heap()->tlab_used(thr);
  51 
  52   _gc_waste += (unsigned)remaining();
  53   size_t total_allocated = thr->allocated_bytes();
  54   size_t allocated_since_last_gc = total_allocated - _allocated_before_last_gc;
  55   _allocated_before_last_gc = total_allocated;
  56 
  57   print_stats("gc");
  58 
  59   if (_number_of_refills > 0) {
  60     // Update allocation history if a reasonable amount of eden was allocated.
  61     bool update_allocation_history = used > 0.5 * capacity;
  62 
  63     if (update_allocation_history) {
  64       // Average the fraction of eden allocated in a tlab by this
  65       // thread for use in the next resize operation.
  66       // _gc_waste is not subtracted because it's included in
  67       // "used".
  68       // The result can be larger than 1.0 due to direct to old allocations.
  69       // These allocations should ideally not be counted but since it is not possible
  70       // to filter them out here we just cap the fraction to be at most 1.0.
  71       double alloc_frac = MIN2(1.0, (double) allocated_since_last_gc / used);
  72       _allocation_fraction.sample(alloc_frac);
  73     }
  74 
  75     stats->update_fast_allocations(_number_of_refills,
  76                                    _allocated_size,
  77                                    _gc_waste,
  78                                    _fast_refill_waste,
  79                                    _slow_refill_waste);
  80   } else {
  81     assert(_number_of_refills == 0 && _fast_refill_waste == 0 &&
  82            _slow_refill_waste == 0 && _gc_waste          == 0,
  83            "tlab stats == 0");
  84   }
  85 
  86   stats->update_slow_allocations(_slow_allocations);
  87 
  88   reset_statistics();
  89 }
  90 
  91 void ThreadLocalAllocBuffer::insert_filler() {
  92   assert(end() != NULL, "Must not be retired");
  93   Universe::heap()->fill_with_dummy_object(top(), hard_end(), true);
  94 }
  95 
  96 void ThreadLocalAllocBuffer::make_parsable() {
  97   if (end() != NULL) {
  98     invariants();
  99     if (ZeroTLAB) {
 100       retire();
 101     } else {
 102       insert_filler();
 103     }
 104   }
 105 }
 106 
 107 void ThreadLocalAllocBuffer::retire(ThreadLocalAllocStats* stats) {
 108   if (stats != NULL) {
 109     accumulate_and_reset_statistics(stats);
 110   }
 111 
 112   if (end() != NULL) {
 113     invariants();
 114     thread()->incr_allocated_bytes(used_bytes());
 115     insert_filler();
 116     initialize(NULL, NULL, NULL);
 117   }
 118 }
 119 
 120 void ThreadLocalAllocBuffer::retire_before_allocation() {
 121   _slow_refill_waste += (unsigned int)remaining();
 122   retire();
 123 }
 124 
 125 void ThreadLocalAllocBuffer::resize() {
 126   // Compute the next tlab size using expected allocation amount
 127   assert(ResizeTLAB, "Should not call this otherwise");
 128   size_t alloc = (size_t)(_allocation_fraction.average() *
 129                           (Universe::heap()->tlab_capacity(thread()) / HeapWordSize));
 130   size_t new_size = alloc / _target_refills;
 131 
 132   new_size = MIN2(MAX2(new_size, min_size()), max_size());
 133 
 134   size_t aligned_new_size = align_object_size(new_size);
 135 
 136   log_trace(gc, tlab)("TLAB new size: thread: " INTPTR_FORMAT " [id: %2d]"
 137                       " refills %d  alloc: %8.6f desired_size: " SIZE_FORMAT " -> " SIZE_FORMAT,
 138                       p2i(thread()), thread()->osthread()->thread_id(),
 139                       _target_refills, _allocation_fraction.average(), desired_size(), aligned_new_size);
 140 
 141   set_desired_size(aligned_new_size);
 142   set_refill_waste_limit(initial_refill_waste_limit());
 143 }
 144 
 145 void ThreadLocalAllocBuffer::reset_statistics() {
 146   _number_of_refills = 0;
 147   _fast_refill_waste = 0;
 148   _slow_refill_waste = 0;
 149   _gc_waste          = 0;
 150   _slow_allocations  = 0;
 151   _allocated_size    = 0;
 152 }
 153 
 154 void ThreadLocalAllocBuffer::fill(HeapWord* start,
 155                                   HeapWord* top,
 156                                   size_t    new_size) {
 157   _number_of_refills++;
 158   _allocated_size += new_size;
 159   print_stats("fill");
 160   assert(top <= start + new_size - alignment_reserve(), "size too small");
 161 
 162   initialize(start, top, start + new_size - alignment_reserve());
 163 
 164   // Reset amount of internal fragmentation
 165   set_refill_waste_limit(initial_refill_waste_limit());
 166 }
 167 
 168 void ThreadLocalAllocBuffer::initialize(HeapWord* start,
 169                                         HeapWord* top,
 170                                         HeapWord* end) {
 171   set_start(start);
 172   set_top(top);
 173   set_pf_top(top);
 174   set_end(end);
 175   set_allocation_end(end);
 176   invariants();
 177 }
 178 
 179 void ThreadLocalAllocBuffer::initialize() {
 180   initialize(NULL,                    // start
 181              NULL,                    // top
 182              NULL);                   // end
 183 
 184   set_desired_size(initial_desired_size());
 185 
 186   size_t capacity = Universe::heap()->tlab_capacity(thread()) / HeapWordSize;
 187   double alloc_frac = desired_size() * target_refills() / (double) capacity;
 188   _allocation_fraction.sample(alloc_frac);
 189 
 190   set_refill_waste_limit(initial_refill_waste_limit());
 191 
 192   reset_statistics();
 193 }
 194 
 195 void ThreadLocalAllocBuffer::startup_initialization() {
 196   ThreadLocalAllocStats::initialize();
 197 
 198   // Assuming each thread's active tlab is, on average,
 199   // 1/2 full at a GC
 200   _target_refills = 100 / (2 * TLABWasteTargetPercent);
 201   // We need to set initial target refills to 2 to avoid a GC which causes VM
 202   // abort during VM initialization.
 203   _target_refills = MAX2(_target_refills, 2U);
 204 
 205 #ifdef COMPILER2
 206   // If the C2 compiler is present, extra space is needed at the end of
 207   // TLABs, otherwise prefetching instructions generated by the C2
 208   // compiler will fault (due to accessing memory outside of heap).
 209   // The amount of space is the max of the number of lines to
 210   // prefetch for array and for instance allocations. (Extra space must be
 211   // reserved to accommodate both types of allocations.)
 212   //
 213   // Only SPARC-specific BIS instructions are known to fault. (Those
 214   // instructions are generated if AllocatePrefetchStyle==3 and
 215   // AllocatePrefetchInstr==1). To be on the safe side, however,
 216   // extra space is reserved for all combinations of
 217   // AllocatePrefetchStyle and AllocatePrefetchInstr.
 218   //
 219   // If the C2 compiler is not present, no space is reserved.
 220 
 221   // +1 for rounding up to next cache line, +1 to be safe
 222   if (is_server_compilation_mode_vm()) {
 223     int lines =  MAX2(AllocatePrefetchLines, AllocateInstancePrefetchLines) + 2;
 224     _reserve_for_allocation_prefetch = (AllocatePrefetchDistance + AllocatePrefetchStepSize * lines) /
 225                                        (int)HeapWordSize;
 226   }
 227 #endif
 228 
 229   // During jvm startup, the main thread is initialized
 230   // before the heap is initialized.  So reinitialize it now.
 231   guarantee(Thread::current()->is_Java_thread(), "tlab initialization thread not Java thread");
 232   Thread::current()->tlab().initialize();
 233 
 234   log_develop_trace(gc, tlab)("TLAB min: " SIZE_FORMAT " initial: " SIZE_FORMAT " max: " SIZE_FORMAT,
 235                                min_size(), Thread::current()->tlab().initial_desired_size(), max_size());
 236 }
 237 
 238 size_t ThreadLocalAllocBuffer::initial_desired_size() {
 239   size_t init_sz = 0;
 240 
 241   if (TLABSize > 0) {
 242     init_sz = TLABSize / HeapWordSize;
 243   } else {
 244     // Initial size is a function of the average number of allocating threads.
 245     unsigned int nof_threads = ThreadLocalAllocStats::allocating_threads_avg();
 246 
 247     init_sz  = (Universe::heap()->tlab_capacity(thread()) / HeapWordSize) /
 248                       (nof_threads * target_refills());
 249     init_sz = align_object_size(init_sz);
 250   }
 251   init_sz = MIN2(MAX2(init_sz, min_size()), max_size());
 252   return init_sz;
 253 }
 254 
 255 void ThreadLocalAllocBuffer::print_stats(const char* tag) {
 256   Log(gc, tlab) log;
 257   if (!log.is_trace()) {
 258     return;
 259   }
 260 
 261   Thread* thrd = thread();
 262   size_t waste = _gc_waste + _slow_refill_waste + _fast_refill_waste;
 263   double waste_percent = percent_of(waste, _allocated_size);
 264   size_t tlab_used  = Universe::heap()->tlab_used(thrd);
 265   log.trace("TLAB: %s thread: " INTPTR_FORMAT " [id: %2d]"
 266             " desired_size: " SIZE_FORMAT "KB"
 267             " slow allocs: %d  refill waste: " SIZE_FORMAT "B"
 268             " alloc:%8.5f %8.0fKB refills: %d waste %4.1f%% gc: %dB"
 269             " slow: %dB fast: %dB",
 270             tag, p2i(thrd), thrd->osthread()->thread_id(),
 271             _desired_size / (K / HeapWordSize),
 272             _slow_allocations, _refill_waste_limit * HeapWordSize,
 273             _allocation_fraction.average(),
 274             _allocation_fraction.average() * tlab_used / K,
 275             _number_of_refills, waste_percent,
 276             _gc_waste * HeapWordSize,
 277             _slow_refill_waste * HeapWordSize,
 278             _fast_refill_waste * HeapWordSize);
 279 }
 280 
 281 void ThreadLocalAllocBuffer::verify() {
 282   HeapWord* p = start();
 283   HeapWord* t = top();
 284   HeapWord* prev_p = NULL;
 285   while (p < t) {
 286     oopDesc::verify(oop(p));
 287     prev_p = p;
 288     p += oop(p)->size();
 289   }
 290   guarantee(p == top(), "end of last object must match end of space");
 291 }
 292 
 293 void ThreadLocalAllocBuffer::set_sample_end() {
 294   size_t heap_words_remaining = pointer_delta(_end, _top);
 295   size_t bytes_until_sample = thread()->heap_sampler().bytes_until_sample();
 296   size_t words_until_sample = bytes_until_sample / HeapWordSize;
 297 
 298   if (heap_words_remaining > words_until_sample) {
 299     HeapWord* new_end = _top + words_until_sample;
 300     set_end(new_end);
 301     _bytes_since_last_sample_point = bytes_until_sample;
 302   } else {
 303     _bytes_since_last_sample_point = heap_words_remaining * HeapWordSize;
 304   }
 305 }
 306 
 307 Thread* ThreadLocalAllocBuffer::thread() {
 308   return (Thread*)(((char*)this) + in_bytes(start_offset()) - in_bytes(Thread::tlab_start_offset()));
 309 }
 310 
 311 void ThreadLocalAllocBuffer::set_back_allocation_end() {
 312   _end = _allocation_end;
 313 }
 314 
 315 HeapWord* ThreadLocalAllocBuffer::hard_end() {
 316   return _allocation_end + alignment_reserve();
 317 }
 318 
 319 PerfVariable* ThreadLocalAllocStats::_perf_allocating_threads;
 320 PerfVariable* ThreadLocalAllocStats::_perf_total_refills;
 321 PerfVariable* ThreadLocalAllocStats::_perf_max_refills;
 322 PerfVariable* ThreadLocalAllocStats::_perf_total_allocations;
 323 PerfVariable* ThreadLocalAllocStats::_perf_total_gc_waste;
 324 PerfVariable* ThreadLocalAllocStats::_perf_max_gc_waste;
 325 PerfVariable* ThreadLocalAllocStats::_perf_total_slow_refill_waste;
 326 PerfVariable* ThreadLocalAllocStats::_perf_max_slow_refill_waste;
 327 PerfVariable* ThreadLocalAllocStats::_perf_total_fast_refill_waste;
 328 PerfVariable* ThreadLocalAllocStats::_perf_max_fast_refill_waste;
 329 PerfVariable* ThreadLocalAllocStats::_perf_total_slow_allocations;
 330 PerfVariable* ThreadLocalAllocStats::_perf_max_slow_allocations;
 331 AdaptiveWeightedAverage ThreadLocalAllocStats::_allocating_threads_avg(0);
 332 
 333 static PerfVariable* create_perf_variable(const char* name, PerfData::Units unit, TRAPS) {
 334   ResourceMark rm;
 335   return PerfDataManager::create_variable(SUN_GC, PerfDataManager::counter_name("tlab", name), unit, THREAD);
 336 }
 337 
 338 void ThreadLocalAllocStats::initialize() {
 339   _allocating_threads_avg = AdaptiveWeightedAverage(TLABAllocationWeight);
 340   _allocating_threads_avg.sample(1); // One allocating thread at startup
 341 
 342   if (UsePerfData) {
 343     EXCEPTION_MARK;
 344     _perf_allocating_threads      = create_perf_variable("allocThreads", PerfData::U_None,  CHECK);
 345     _perf_total_refills           = create_perf_variable("fills",        PerfData::U_None,  CHECK);
 346     _perf_max_refills             = create_perf_variable("maxFills",     PerfData::U_None,  CHECK);
 347     _perf_total_allocations       = create_perf_variable("alloc",        PerfData::U_Bytes, CHECK);
 348     _perf_total_gc_waste          = create_perf_variable("gcWaste",      PerfData::U_Bytes, CHECK);
 349     _perf_max_gc_waste            = create_perf_variable("maxGcWaste",   PerfData::U_Bytes, CHECK);
 350     _perf_total_slow_refill_waste = create_perf_variable("slowWaste",    PerfData::U_Bytes, CHECK);
 351     _perf_max_slow_refill_waste   = create_perf_variable("maxSlowWaste", PerfData::U_Bytes, CHECK);
 352     _perf_total_fast_refill_waste = create_perf_variable("fastWaste",    PerfData::U_Bytes, CHECK);
 353     _perf_max_fast_refill_waste   = create_perf_variable("maxFastWaste", PerfData::U_Bytes, CHECK);
 354     _perf_total_slow_allocations  = create_perf_variable("slowAlloc",    PerfData::U_None,  CHECK);
 355     _perf_max_slow_allocations    = create_perf_variable("maxSlowAlloc", PerfData::U_None,  CHECK);
 356   }
 357 }
 358 
 359 ThreadLocalAllocStats::ThreadLocalAllocStats() :
 360     _allocating_threads(0),
 361     _total_refills(0),
 362     _max_refills(0),
 363     _total_allocations(0),
 364     _total_gc_waste(0),
 365     _max_gc_waste(0),
 366     _total_fast_refill_waste(0),
 367     _max_fast_refill_waste(0),
 368     _total_slow_refill_waste(0),
 369     _max_slow_refill_waste(0),
 370     _total_slow_allocations(0),
 371     _max_slow_allocations(0) {}
 372 
 373 unsigned int ThreadLocalAllocStats::allocating_threads_avg() {
 374   return MAX2((unsigned int)(_allocating_threads_avg.average() + 0.5), 1U);
 375 }
 376 
 377 void ThreadLocalAllocStats::update_fast_allocations(unsigned int refills,
 378                                        size_t allocations,
 379                                        size_t gc_waste,
 380                                        size_t fast_refill_waste,
 381                                        size_t slow_refill_waste) {
 382   _allocating_threads      += 1;
 383   _total_refills           += refills;
 384   _max_refills              = MAX2(_max_refills, refills);
 385   _total_allocations       += allocations;
 386   _total_gc_waste          += gc_waste;
 387   _max_gc_waste             = MAX2(_max_gc_waste, gc_waste);
 388   _total_fast_refill_waste += fast_refill_waste;
 389   _max_fast_refill_waste    = MAX2(_max_fast_refill_waste, fast_refill_waste);
 390   _total_slow_refill_waste += slow_refill_waste;
 391   _max_slow_refill_waste    = MAX2(_max_slow_refill_waste, slow_refill_waste);
 392 }
 393 
 394 void ThreadLocalAllocStats::update_slow_allocations(unsigned int allocations) {
 395   _total_slow_allocations += allocations;
 396   _max_slow_allocations    = MAX2(_max_slow_allocations, allocations);
 397 }
 398 
 399 void ThreadLocalAllocStats::update(const ThreadLocalAllocStats& other) {
 400   _allocating_threads      += other._allocating_threads;
 401   _total_refills           += other._total_refills;
 402   _max_refills              = MAX2(_max_refills, other._max_refills);
 403   _total_allocations       += other._total_allocations;
 404   _total_gc_waste          += other._total_gc_waste;
 405   _max_gc_waste             = MAX2(_max_gc_waste, other._max_gc_waste);
 406   _total_fast_refill_waste += other._total_fast_refill_waste;
 407   _max_fast_refill_waste    = MAX2(_max_fast_refill_waste, other._max_fast_refill_waste);
 408   _total_slow_refill_waste += other._total_slow_refill_waste;
 409   _max_slow_refill_waste    = MAX2(_max_slow_refill_waste, other._max_slow_refill_waste);
 410   _total_slow_allocations  += other._total_slow_allocations;
 411   _max_slow_allocations     = MAX2(_max_slow_allocations, other._max_slow_allocations);
 412 }
 413 
 414 void ThreadLocalAllocStats::reset() {
 415   _allocating_threads      = 0;
 416   _total_refills           = 0;
 417   _max_refills             = 0;
 418   _total_allocations       = 0;
 419   _total_gc_waste          = 0;
 420   _max_gc_waste            = 0;
 421   _total_fast_refill_waste = 0;
 422   _max_fast_refill_waste   = 0;
 423   _total_slow_refill_waste = 0;
 424   _max_slow_refill_waste   = 0;
 425   _total_slow_allocations  = 0;
 426   _max_slow_allocations    = 0;
 427 }
 428 
 429 void ThreadLocalAllocStats::publish() {
 430   if (_total_allocations == 0) {
 431     return;
 432   }
 433 
 434   _allocating_threads_avg.sample(_allocating_threads);
 435 
 436   const size_t waste = _total_gc_waste + _total_slow_refill_waste + _total_fast_refill_waste;
 437   const double waste_percent = percent_of(waste, _total_allocations);
 438   log_debug(gc, tlab)("TLAB totals: thrds: %d  refills: %d max: %d"
 439                       " slow allocs: %d max %d waste: %4.1f%%"
 440                       " gc: " SIZE_FORMAT "B max: " SIZE_FORMAT "B"
 441                       " slow: " SIZE_FORMAT "B max: " SIZE_FORMAT "B"
 442                       " fast: " SIZE_FORMAT "B max: " SIZE_FORMAT "B",
 443                       _allocating_threads, _total_refills, _max_refills,
 444                       _total_slow_allocations, _max_slow_allocations, waste_percent,
 445                       _total_gc_waste * HeapWordSize, _max_gc_waste * HeapWordSize,
 446                       _total_slow_refill_waste * HeapWordSize, _max_slow_refill_waste * HeapWordSize,
 447                       _total_fast_refill_waste * HeapWordSize, _max_fast_refill_waste * HeapWordSize);
 448 
 449   if (UsePerfData) {
 450     _perf_allocating_threads      ->set_value(_allocating_threads);
 451     _perf_total_refills           ->set_value(_total_refills);
 452     _perf_max_refills             ->set_value(_max_refills);
 453     _perf_total_allocations       ->set_value(_total_allocations);
 454     _perf_total_gc_waste          ->set_value(_total_gc_waste);
 455     _perf_max_gc_waste            ->set_value(_max_gc_waste);
 456     _perf_total_slow_refill_waste ->set_value(_total_slow_refill_waste);
 457     _perf_max_slow_refill_waste   ->set_value(_max_slow_refill_waste);
 458     _perf_total_fast_refill_waste ->set_value(_total_fast_refill_waste);
 459     _perf_max_fast_refill_waste   ->set_value(_max_fast_refill_waste);
 460     _perf_total_slow_allocations  ->set_value(_total_slow_allocations);
 461     _perf_max_slow_allocations    ->set_value(_max_slow_allocations);
 462   }





 463 }
--- EOF ---