1 /* 2 * Copyright (c) 1999, 2015, 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 "memory/resourceArea.hpp" 29 #include "memory/universe.inline.hpp" 30 #include "oops/oop.inline.hpp" 31 #include "runtime/thread.inline.hpp" 32 #include "utilities/copy.hpp" 33 34 // Thread-Local Edens support 35 36 // static member initialization 37 size_t ThreadLocalAllocBuffer::_max_size = 0; 38 unsigned ThreadLocalAllocBuffer::_target_refills = 0; 39 GlobalTLABStats* ThreadLocalAllocBuffer::_global_stats = NULL; 40 41 void ThreadLocalAllocBuffer::clear_before_allocation() { 42 _slow_refill_waste += (unsigned)remaining(); 43 make_parsable(true); // also retire the TLAB 44 } 45 46 void ThreadLocalAllocBuffer::accumulate_statistics_before_gc() { 47 global_stats()->initialize(); 48 49 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 50 thread->tlab().accumulate_statistics(); 51 thread->tlab().initialize_statistics(); 52 } 53 54 Universe::heap()->accumulate_statistics_all_gclabs(); 55 56 // Publish new stats if some allocation occurred. 57 if (global_stats()->allocation() != 0) { 58 global_stats()->publish(); 59 if (PrintTLAB) { 60 global_stats()->print(); 61 } 62 } 63 } 64 65 void ThreadLocalAllocBuffer::accumulate_statistics() { 66 Thread* thread = myThread(); 67 size_t capacity = Universe::heap()->tlab_capacity(thread); 68 size_t used = Universe::heap()->tlab_used(thread); 69 70 _gc_waste += (unsigned)remaining(); 71 size_t total_allocated = _gclab ? thread->allocated_bytes_gclab() : thread->allocated_bytes(); 72 size_t allocated_since_last_gc = total_allocated - _allocated_before_last_gc; 73 _allocated_before_last_gc = total_allocated; 74 75 if (PrintTLAB && (_number_of_refills > 0 || Verbose)) { 76 print_stats("gc"); 77 } 78 79 if (_number_of_refills > 0) { 80 // Update allocation history if a reasonable amount of eden was allocated. 81 bool update_allocation_history = used > 0.5 * capacity; 82 83 if (update_allocation_history) { 84 // Average the fraction of eden allocated in a tlab by this 85 // thread for use in the next resize operation. 86 // _gc_waste is not subtracted because it's included in 87 // "used". 88 // The result can be larger than 1.0 due to direct to old allocations. 89 // These allocations should ideally not be counted but since it is not possible 90 // to filter them out here we just cap the fraction to be at most 1.0. 91 double alloc_frac = MIN2(1.0, (double) allocated_since_last_gc / used); 92 _allocation_fraction.sample(alloc_frac); 93 } 94 global_stats()->update_allocating_threads(); 95 global_stats()->update_number_of_refills(_number_of_refills); 96 global_stats()->update_allocation(_number_of_refills * desired_size()); 97 global_stats()->update_gc_waste(_gc_waste); 98 global_stats()->update_slow_refill_waste(_slow_refill_waste); 99 global_stats()->update_fast_refill_waste(_fast_refill_waste); 100 101 } else { 102 assert(_number_of_refills == 0 && _fast_refill_waste == 0 && 103 _slow_refill_waste == 0 && _gc_waste == 0, 104 "tlab stats == 0"); 105 } 106 global_stats()->update_slow_allocations(_slow_allocations); 107 } 108 109 // Fills the current tlab with a dummy filler array to create 110 // an illusion of a contiguous Eden and optionally retires the tlab. 111 // Waste accounting should be done in caller as appropriate; see, 112 // for example, clear_before_allocation(). 113 void ThreadLocalAllocBuffer::make_parsable(bool retire) { 114 if (end() != NULL) { 115 invariants(); 116 117 if (retire) { 118 if (_gclab) { 119 myThread()->incr_allocated_bytes_gclab(used_bytes()); 120 } else { 121 myThread()->incr_allocated_bytes(used_bytes()); 122 } 123 } 124 125 HeapWord* obj = Universe::heap()->tlab_post_allocation_setup(top()); 126 CollectedHeap::fill_with_object(obj, hard_end(), retire); 127 128 if (retire || ZeroTLAB) { // "Reset" the TLAB 129 set_start(NULL); 130 set_top(NULL); 131 set_pf_top(NULL); 132 set_end(NULL); 133 } 134 } 135 assert(!(retire || ZeroTLAB) || 136 (start() == NULL && end() == NULL && top() == NULL), 137 "TLAB must be reset"); 138 } 139 140 void ThreadLocalAllocBuffer::resize_all_tlabs() { 141 if (ResizeTLAB) { 142 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) { 143 thread->tlab().resize(); 144 } 145 } 146 } 147 148 void ThreadLocalAllocBuffer::resize() { 149 // Compute the next tlab size using expected allocation amount 150 assert(ResizeTLAB, "Should not call this otherwise"); 151 size_t alloc = (size_t)(_allocation_fraction.average() * 152 (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize)); 153 size_t new_size = alloc / _target_refills; 154 155 new_size = MIN2(MAX2(new_size, min_size()), max_size()); 156 157 size_t aligned_new_size = align_object_size(new_size); 158 159 if (PrintTLAB && Verbose) { 160 gclog_or_tty->print("TLAB new size: thread: " INTPTR_FORMAT " [id: %2d]" 161 " refills %d alloc: %8.6f desired_size: " SIZE_FORMAT " -> " SIZE_FORMAT "\n", 162 p2i(myThread()), myThread()->osthread()->thread_id(), 163 _target_refills, _allocation_fraction.average(), desired_size(), aligned_new_size); 164 } 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 } 176 177 void ThreadLocalAllocBuffer::fill(HeapWord* start, 178 HeapWord* top, 179 size_t new_size) { 180 _number_of_refills++; 181 if (PrintTLAB && Verbose) { 182 print_stats("fill"); 183 } 184 assert(top <= start + new_size - alignment_reserve(), "size too small"); 185 initialize(start, top, start + new_size - alignment_reserve()); 186 187 // Reset amount of internal fragmentation 188 set_refill_waste_limit(initial_refill_waste_limit()); 189 } 190 191 void ThreadLocalAllocBuffer::initialize(HeapWord* start, 192 HeapWord* top, 193 HeapWord* end) { 194 set_start(start); 195 set_top(top); 196 set_pf_top(top); 197 set_end(end); 198 invariants(); 199 } 200 201 void ThreadLocalAllocBuffer::initialize(bool gclab) { 202 _gclab = gclab; 203 initialize(NULL, // start 204 NULL, // top 205 NULL); // end 206 207 set_desired_size(initial_desired_size()); 208 209 // Following check is needed because at startup the main (primordial) 210 // thread is initialized before the heap is. The initialization for 211 // this thread is redone in startup_initialization below. 212 if (Universe::heap() != NULL) { 213 size_t capacity = Universe::heap()->tlab_capacity(myThread()) / HeapWordSize; 214 double alloc_frac = desired_size() * target_refills() / (double) capacity; 215 _allocation_fraction.sample(alloc_frac); 216 } 217 218 set_refill_waste_limit(initial_refill_waste_limit()); 219 220 initialize_statistics(); 221 } 222 223 void ThreadLocalAllocBuffer::startup_initialization() { 224 225 // Assuming each thread's active tlab is, on average, 226 // 1/2 full at a GC 227 _target_refills = 100 / (2 * TLABWasteTargetPercent); 228 _target_refills = MAX2(_target_refills, (unsigned)1U); 229 230 _global_stats = new GlobalTLABStats(); 231 232 // During jvm startup, the main (primordial) thread is initialized 233 // before the heap is initialized. So reinitialize it now. 234 guarantee(Thread::current()->is_Java_thread(), "tlab initialization thread not Java thread"); 235 Thread::current()->tlab().initialize(false); 236 Thread::current()->gclab().initialize(true); 237 238 if (PrintTLAB && Verbose) { 239 gclog_or_tty->print("TLAB min: " SIZE_FORMAT " initial: " SIZE_FORMAT " max: " SIZE_FORMAT "\n", 240 min_size(), Thread::current()->tlab().initial_desired_size(), max_size()); 241 } 242 } 243 244 size_t ThreadLocalAllocBuffer::initial_desired_size() { 245 size_t init_sz = 0; 246 247 if (TLABSize > 0) { 248 init_sz = TLABSize / HeapWordSize; 249 } else if (global_stats() != NULL) { 250 // Initial size is a function of the average number of allocating threads. 251 unsigned nof_threads = global_stats()->allocating_threads_avg(); 252 253 init_sz = (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize) / 254 (nof_threads * target_refills()); 255 init_sz = align_object_size(init_sz); 256 } 257 init_sz = MIN2(MAX2(init_sz, min_size()), max_size()); 258 return init_sz; 259 } 260 261 void ThreadLocalAllocBuffer::print_stats(const char* tag) { 262 Thread* thrd = myThread(); 263 size_t waste = _gc_waste + _slow_refill_waste + _fast_refill_waste; 264 size_t alloc = _number_of_refills * _desired_size; 265 double waste_percent = alloc == 0 ? 0.0 : 266 100.0 * waste / alloc; 267 size_t tlab_used = Universe::heap()->tlab_used(thrd); 268 gclog_or_tty->print("TLAB: %s %s thread: " INTPTR_FORMAT " [id: %2d]" 269 " desired_size: " SIZE_FORMAT "KB" 270 " slow allocs: %d refill waste: " SIZE_FORMAT "B" 271 " alloc:%8.5f %8.0fKB refills: %d waste %4.1f%% gc: %dB" 272 " slow: %dB fast: %dB\n", 273 tag, _gclab ? "gclab" : "tlab ", p2i(thrd), thrd->osthread()->thread_id(), 274 _desired_size / (K / HeapWordSize), 275 _slow_allocations, _refill_waste_limit * HeapWordSize, 276 _allocation_fraction.average(), 277 _allocation_fraction.average() * tlab_used / K, 278 _number_of_refills, waste_percent, 279 _gc_waste * HeapWordSize, 280 _slow_refill_waste * HeapWordSize, 281 _fast_refill_waste * HeapWordSize); 282 } 283 284 void ThreadLocalAllocBuffer::verify() { 285 HeapWord* p = start(); 286 HeapWord* t = top(); 287 HeapWord* prev_p = NULL; 288 while (p < t) { 289 oop(p)->verify(); 290 prev_p = p; 291 p += oop(p)->size(); 292 } 293 guarantee(p == top(), "end of last object must match end of space"); 294 } 295 296 Thread* ThreadLocalAllocBuffer::myThread() { 297 ByteSize gclab_offset = Thread::gclab_start_offset(); 298 ByteSize tlab_offset = Thread::tlab_start_offset(); 299 ByteSize offs = _gclab ? gclab_offset : tlab_offset; 300 Thread* thread = (Thread*)(((char *)this) + 301 in_bytes(start_offset()) - in_bytes(offs)); 302 #ifdef ASSERT 303 assert(this == (_gclab ? &thread->gclab() : &thread->tlab()), "must be"); 304 #endif 305 return thread; 306 } 307 308 size_t ThreadLocalAllocBuffer::end_reserve() { 309 int reserve_size = typeArrayOopDesc::header_size(T_INT) + Universe::heap()->oop_extra_words(); 310 return MAX2(reserve_size, VM_Version::reserve_for_allocation_prefetch()); 311 } 312 313 void ThreadLocalAllocBuffer::rollback(size_t size) { 314 HeapWord* old_top = top(); 315 if (old_top != NULL) { // Pathological case: we accept that we can't rollback. 316 set_top(old_top - size); 317 } 318 } 319 320 321 GlobalTLABStats::GlobalTLABStats() : 322 _allocating_threads_avg(TLABAllocationWeight) { 323 324 initialize(); 325 326 _allocating_threads_avg.sample(1); // One allocating thread at startup 327 328 if (UsePerfData) { 329 330 EXCEPTION_MARK; 331 ResourceMark rm; 332 333 char* cname = PerfDataManager::counter_name("tlab", "allocThreads"); 334 _perf_allocating_threads = 335 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); 336 337 cname = PerfDataManager::counter_name("tlab", "fills"); 338 _perf_total_refills = 339 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); 340 341 cname = PerfDataManager::counter_name("tlab", "maxFills"); 342 _perf_max_refills = 343 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); 344 345 cname = PerfDataManager::counter_name("tlab", "alloc"); 346 _perf_allocation = 347 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 348 349 cname = PerfDataManager::counter_name("tlab", "gcWaste"); 350 _perf_gc_waste = 351 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 352 353 cname = PerfDataManager::counter_name("tlab", "maxGcWaste"); 354 _perf_max_gc_waste = 355 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 356 357 cname = PerfDataManager::counter_name("tlab", "slowWaste"); 358 _perf_slow_refill_waste = 359 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 360 361 cname = PerfDataManager::counter_name("tlab", "maxSlowWaste"); 362 _perf_max_slow_refill_waste = 363 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 364 365 cname = PerfDataManager::counter_name("tlab", "fastWaste"); 366 _perf_fast_refill_waste = 367 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 368 369 cname = PerfDataManager::counter_name("tlab", "maxFastWaste"); 370 _perf_max_fast_refill_waste = 371 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK); 372 373 cname = PerfDataManager::counter_name("tlab", "slowAlloc"); 374 _perf_slow_allocations = 375 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); 376 377 cname = PerfDataManager::counter_name("tlab", "maxSlowAlloc"); 378 _perf_max_slow_allocations = 379 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK); 380 } 381 } 382 383 void GlobalTLABStats::initialize() { 384 // Clear counters summarizing info from all threads 385 _allocating_threads = 0; 386 _total_refills = 0; 387 _max_refills = 0; 388 _total_allocation = 0; 389 _total_gc_waste = 0; 390 _max_gc_waste = 0; 391 _total_slow_refill_waste = 0; 392 _max_slow_refill_waste = 0; 393 _total_fast_refill_waste = 0; 394 _max_fast_refill_waste = 0; 395 _total_slow_allocations = 0; 396 _max_slow_allocations = 0; 397 } 398 399 void GlobalTLABStats::publish() { 400 _allocating_threads_avg.sample(_allocating_threads); 401 if (UsePerfData) { 402 _perf_allocating_threads ->set_value(_allocating_threads); 403 _perf_total_refills ->set_value(_total_refills); 404 _perf_max_refills ->set_value(_max_refills); 405 _perf_allocation ->set_value(_total_allocation); 406 _perf_gc_waste ->set_value(_total_gc_waste); 407 _perf_max_gc_waste ->set_value(_max_gc_waste); 408 _perf_slow_refill_waste ->set_value(_total_slow_refill_waste); 409 _perf_max_slow_refill_waste->set_value(_max_slow_refill_waste); 410 _perf_fast_refill_waste ->set_value(_total_fast_refill_waste); 411 _perf_max_fast_refill_waste->set_value(_max_fast_refill_waste); 412 _perf_slow_allocations ->set_value(_total_slow_allocations); 413 _perf_max_slow_allocations ->set_value(_max_slow_allocations); 414 } 415 } 416 417 void GlobalTLABStats::print() { 418 size_t waste = _total_gc_waste + _total_slow_refill_waste + _total_fast_refill_waste; 419 double waste_percent = _total_allocation == 0 ? 0.0 : 420 100.0 * waste / _total_allocation; 421 gclog_or_tty->print("TLAB totals: thrds: %d refills: %d max: %d" 422 " slow allocs: %d max %d waste: %4.1f%%" 423 " gc: " SIZE_FORMAT "B max: " SIZE_FORMAT "B" 424 " slow: " SIZE_FORMAT "B max: " SIZE_FORMAT "B" 425 " fast: " SIZE_FORMAT "B max: " SIZE_FORMAT "B\n", 426 _allocating_threads, 427 _total_refills, _max_refills, 428 _total_slow_allocations, _max_slow_allocations, 429 waste_percent, 430 _total_gc_waste * HeapWordSize, 431 _max_gc_waste * HeapWordSize, 432 _total_slow_refill_waste * HeapWordSize, 433 _max_slow_refill_waste * HeapWordSize, 434 _total_fast_refill_waste * HeapWordSize, 435 _max_fast_refill_waste * HeapWordSize); 436 }