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