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