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