src/share/vm/memory/threadLocalAllocBuffer.cpp

Mon, 23 Mar 2020 17:57:13 +0000

author
poonam
date
Mon, 23 Mar 2020 17:57:13 +0000
changeset 9965
c39172598323
parent 9289
427b2fb1944f
child 10015
eb7ce841ccec
permissions
-rw-r--r--

8231779: crash HeapWord*ParallelScavengeHeap::failed_mem_allocate
Reviewed-by: dlong, tschatzl, pliden

duke@435 1 /*
poonam@9965 2 * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "memory/genCollectedHeap.hpp"
stefank@2314 27 #include "memory/resourceArea.hpp"
stefank@2314 28 #include "memory/threadLocalAllocBuffer.inline.hpp"
stefank@2314 29 #include "memory/universe.inline.hpp"
stefank@2314 30 #include "oops/oop.inline.hpp"
stefank@4299 31 #include "runtime/thread.inline.hpp"
stefank@2314 32 #include "utilities/copy.hpp"
stefank@2314 33
drchase@6680 34 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
drchase@6680 35
duke@435 36 // Thread-Local Edens support
duke@435 37
duke@435 38 // static member initialization
brutisso@6376 39 size_t ThreadLocalAllocBuffer::_max_size = 0;
duke@435 40 unsigned ThreadLocalAllocBuffer::_target_refills = 0;
duke@435 41 GlobalTLABStats* ThreadLocalAllocBuffer::_global_stats = NULL;
duke@435 42
duke@435 43 void ThreadLocalAllocBuffer::clear_before_allocation() {
duke@435 44 _slow_refill_waste += (unsigned)remaining();
duke@435 45 make_parsable(true); // also retire the TLAB
duke@435 46 }
duke@435 47
duke@435 48 void ThreadLocalAllocBuffer::accumulate_statistics_before_gc() {
duke@435 49 global_stats()->initialize();
duke@435 50
brutisso@6376 51 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
duke@435 52 thread->tlab().accumulate_statistics();
duke@435 53 thread->tlab().initialize_statistics();
duke@435 54 }
duke@435 55
duke@435 56 // Publish new stats if some allocation occurred.
duke@435 57 if (global_stats()->allocation() != 0) {
duke@435 58 global_stats()->publish();
duke@435 59 if (PrintTLAB) {
duke@435 60 global_stats()->print();
duke@435 61 }
duke@435 62 }
duke@435 63 }
duke@435 64
duke@435 65 void ThreadLocalAllocBuffer::accumulate_statistics() {
brutisso@6376 66 Thread* thread = myThread();
brutisso@6376 67 size_t capacity = Universe::heap()->tlab_capacity(thread);
brutisso@6376 68 size_t used = Universe::heap()->tlab_used(thread);
duke@435 69
duke@435 70 _gc_waste += (unsigned)remaining();
brutisso@6376 71 size_t total_allocated = thread->allocated_bytes();
brutisso@6376 72 size_t allocated_since_last_gc = total_allocated - _allocated_before_last_gc;
brutisso@6376 73 _allocated_before_last_gc = total_allocated;
duke@435 74
duke@435 75 if (PrintTLAB && (_number_of_refills > 0 || Verbose)) {
duke@435 76 print_stats("gc");
duke@435 77 }
duke@435 78
duke@435 79 if (_number_of_refills > 0) {
brutisso@6376 80 // Update allocation history if a reasonable amount of eden was allocated.
brutisso@6376 81 bool update_allocation_history = used > 0.5 * capacity;
duke@435 82
duke@435 83 if (update_allocation_history) {
duke@435 84 // Average the fraction of eden allocated in a tlab by this
duke@435 85 // thread for use in the next resize operation.
duke@435 86 // _gc_waste is not subtracted because it's included in
duke@435 87 // "used".
brutisso@6376 88 // The result can be larger than 1.0 due to direct to old allocations.
brutisso@6376 89 // These allocations should ideally not be counted but since it is not possible
brutisso@6376 90 // to filter them out here we just cap the fraction to be at most 1.0.
poonam@9965 91 // Keep alloc_frac as float and not double to avoid the double to float conversion
poonam@9965 92 float alloc_frac = MIN2(1.0f, allocated_since_last_gc / (float) used);
duke@435 93 _allocation_fraction.sample(alloc_frac);
duke@435 94 }
duke@435 95 global_stats()->update_allocating_threads();
duke@435 96 global_stats()->update_number_of_refills(_number_of_refills);
duke@435 97 global_stats()->update_allocation(_number_of_refills * desired_size());
duke@435 98 global_stats()->update_gc_waste(_gc_waste);
duke@435 99 global_stats()->update_slow_refill_waste(_slow_refill_waste);
duke@435 100 global_stats()->update_fast_refill_waste(_fast_refill_waste);
duke@435 101
duke@435 102 } else {
duke@435 103 assert(_number_of_refills == 0 && _fast_refill_waste == 0 &&
duke@435 104 _slow_refill_waste == 0 && _gc_waste == 0,
duke@435 105 "tlab stats == 0");
duke@435 106 }
duke@435 107 global_stats()->update_slow_allocations(_slow_allocations);
duke@435 108 }
duke@435 109
duke@435 110 // Fills the current tlab with a dummy filler array to create
duke@435 111 // an illusion of a contiguous Eden and optionally retires the tlab.
duke@435 112 // Waste accounting should be done in caller as appropriate; see,
duke@435 113 // for example, clear_before_allocation().
duke@435 114 void ThreadLocalAllocBuffer::make_parsable(bool retire) {
duke@435 115 if (end() != NULL) {
duke@435 116 invariants();
phh@2423 117
phh@2423 118 if (retire) {
phh@2423 119 myThread()->incr_allocated_bytes(used_bytes());
phh@2423 120 }
phh@2423 121
johnc@1600 122 CollectedHeap::fill_with_object(top(), hard_end(), retire);
duke@435 123
duke@435 124 if (retire || ZeroTLAB) { // "Reset" the TLAB
duke@435 125 set_start(NULL);
duke@435 126 set_top(NULL);
duke@435 127 set_pf_top(NULL);
duke@435 128 set_end(NULL);
duke@435 129 }
duke@435 130 }
duke@435 131 assert(!(retire || ZeroTLAB) ||
duke@435 132 (start() == NULL && end() == NULL && top() == NULL),
duke@435 133 "TLAB must be reset");
duke@435 134 }
duke@435 135
duke@435 136 void ThreadLocalAllocBuffer::resize_all_tlabs() {
brutisso@6376 137 if (ResizeTLAB) {
brutisso@6376 138 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
brutisso@6376 139 thread->tlab().resize();
brutisso@6376 140 }
duke@435 141 }
duke@435 142 }
duke@435 143
duke@435 144 void ThreadLocalAllocBuffer::resize() {
brutisso@6376 145 // Compute the next tlab size using expected allocation amount
brutisso@6376 146 assert(ResizeTLAB, "Should not call this otherwise");
brutisso@6376 147 size_t alloc = (size_t)(_allocation_fraction.average() *
brutisso@6376 148 (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize));
brutisso@6376 149 size_t new_size = alloc / _target_refills;
duke@435 150
brutisso@6376 151 new_size = MIN2(MAX2(new_size, min_size()), max_size());
duke@435 152
brutisso@6376 153 size_t aligned_new_size = align_object_size(new_size);
duke@435 154
brutisso@6376 155 if (PrintTLAB && Verbose) {
brutisso@6376 156 gclog_or_tty->print("TLAB new size: thread: " INTPTR_FORMAT " [id: %2d]"
brutisso@6376 157 " refills %d alloc: %8.6f desired_size: " SIZE_FORMAT " -> " SIZE_FORMAT "\n",
brutisso@6376 158 myThread(), myThread()->osthread()->thread_id(),
brutisso@6376 159 _target_refills, _allocation_fraction.average(), desired_size(), aligned_new_size);
duke@435 160 }
brutisso@6376 161 set_desired_size(aligned_new_size);
brutisso@6376 162 set_refill_waste_limit(initial_refill_waste_limit());
duke@435 163 }
duke@435 164
duke@435 165 void ThreadLocalAllocBuffer::initialize_statistics() {
duke@435 166 _number_of_refills = 0;
duke@435 167 _fast_refill_waste = 0;
duke@435 168 _slow_refill_waste = 0;
duke@435 169 _gc_waste = 0;
duke@435 170 _slow_allocations = 0;
duke@435 171 }
duke@435 172
duke@435 173 void ThreadLocalAllocBuffer::fill(HeapWord* start,
duke@435 174 HeapWord* top,
duke@435 175 size_t new_size) {
duke@435 176 _number_of_refills++;
duke@435 177 if (PrintTLAB && Verbose) {
duke@435 178 print_stats("fill");
duke@435 179 }
duke@435 180 assert(top <= start + new_size - alignment_reserve(), "size too small");
duke@435 181 initialize(start, top, start + new_size - alignment_reserve());
duke@435 182
duke@435 183 // Reset amount of internal fragmentation
duke@435 184 set_refill_waste_limit(initial_refill_waste_limit());
duke@435 185 }
duke@435 186
duke@435 187 void ThreadLocalAllocBuffer::initialize(HeapWord* start,
duke@435 188 HeapWord* top,
duke@435 189 HeapWord* end) {
duke@435 190 set_start(start);
duke@435 191 set_top(top);
duke@435 192 set_pf_top(top);
duke@435 193 set_end(end);
duke@435 194 invariants();
duke@435 195 }
duke@435 196
duke@435 197 void ThreadLocalAllocBuffer::initialize() {
duke@435 198 initialize(NULL, // start
duke@435 199 NULL, // top
duke@435 200 NULL); // end
duke@435 201
duke@435 202 set_desired_size(initial_desired_size());
duke@435 203
dbuck@9289 204 // Following check is needed because at startup the main
duke@435 205 // thread is initialized before the heap is. The initialization for
duke@435 206 // this thread is redone in startup_initialization below.
duke@435 207 if (Universe::heap() != NULL) {
duke@435 208 size_t capacity = Universe::heap()->tlab_capacity(myThread()) / HeapWordSize;
poonam@9965 209 // Keep alloc_frac as float and not double to avoid the double to float conversion
poonam@9965 210 float alloc_frac = desired_size() * target_refills() / (float) capacity;
duke@435 211 _allocation_fraction.sample(alloc_frac);
duke@435 212 }
duke@435 213
duke@435 214 set_refill_waste_limit(initial_refill_waste_limit());
duke@435 215
duke@435 216 initialize_statistics();
duke@435 217 }
duke@435 218
duke@435 219 void ThreadLocalAllocBuffer::startup_initialization() {
duke@435 220
duke@435 221 // Assuming each thread's active tlab is, on average,
duke@435 222 // 1/2 full at a GC
duke@435 223 _target_refills = 100 / (2 * TLABWasteTargetPercent);
duke@435 224 _target_refills = MAX2(_target_refills, (unsigned)1U);
duke@435 225
duke@435 226 _global_stats = new GlobalTLABStats();
duke@435 227
dbuck@9289 228 // During jvm startup, the main thread is initialized
duke@435 229 // before the heap is initialized. So reinitialize it now.
duke@435 230 guarantee(Thread::current()->is_Java_thread(), "tlab initialization thread not Java thread");
duke@435 231 Thread::current()->tlab().initialize();
duke@435 232
duke@435 233 if (PrintTLAB && Verbose) {
duke@435 234 gclog_or_tty->print("TLAB min: " SIZE_FORMAT " initial: " SIZE_FORMAT " max: " SIZE_FORMAT "\n",
duke@435 235 min_size(), Thread::current()->tlab().initial_desired_size(), max_size());
duke@435 236 }
duke@435 237 }
duke@435 238
duke@435 239 size_t ThreadLocalAllocBuffer::initial_desired_size() {
mgerdin@7470 240 size_t init_sz = 0;
duke@435 241
duke@435 242 if (TLABSize > 0) {
mgerdin@7470 243 init_sz = TLABSize / HeapWordSize;
mgerdin@7470 244 } else if (global_stats() != NULL) {
duke@435 245 // Initial size is a function of the average number of allocating threads.
duke@435 246 unsigned nof_threads = global_stats()->allocating_threads_avg();
duke@435 247
duke@435 248 init_sz = (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize) /
duke@435 249 (nof_threads * target_refills());
duke@435 250 init_sz = align_object_size(init_sz);
duke@435 251 }
mgerdin@7470 252 init_sz = MIN2(MAX2(init_sz, min_size()), max_size());
duke@435 253 return init_sz;
duke@435 254 }
duke@435 255
duke@435 256 void ThreadLocalAllocBuffer::print_stats(const char* tag) {
duke@435 257 Thread* thrd = myThread();
duke@435 258 size_t waste = _gc_waste + _slow_refill_waste + _fast_refill_waste;
duke@435 259 size_t alloc = _number_of_refills * _desired_size;
duke@435 260 double waste_percent = alloc == 0 ? 0.0 :
duke@435 261 100.0 * waste / alloc;
brutisso@6376 262 size_t tlab_used = Universe::heap()->tlab_used(thrd);
duke@435 263 gclog_or_tty->print("TLAB: %s thread: " INTPTR_FORMAT " [id: %2d]"
duke@435 264 " desired_size: " SIZE_FORMAT "KB"
duke@435 265 " slow allocs: %d refill waste: " SIZE_FORMAT "B"
duke@435 266 " alloc:%8.5f %8.0fKB refills: %d waste %4.1f%% gc: %dB"
duke@435 267 " slow: %dB fast: %dB\n",
duke@435 268 tag, thrd, thrd->osthread()->thread_id(),
duke@435 269 _desired_size / (K / HeapWordSize),
duke@435 270 _slow_allocations, _refill_waste_limit * HeapWordSize,
duke@435 271 _allocation_fraction.average(),
duke@435 272 _allocation_fraction.average() * tlab_used / K,
duke@435 273 _number_of_refills, waste_percent,
duke@435 274 _gc_waste * HeapWordSize,
duke@435 275 _slow_refill_waste * HeapWordSize,
duke@435 276 _fast_refill_waste * HeapWordSize);
duke@435 277 }
duke@435 278
duke@435 279 void ThreadLocalAllocBuffer::verify() {
duke@435 280 HeapWord* p = start();
duke@435 281 HeapWord* t = top();
duke@435 282 HeapWord* prev_p = NULL;
duke@435 283 while (p < t) {
duke@435 284 oop(p)->verify();
duke@435 285 prev_p = p;
duke@435 286 p += oop(p)->size();
duke@435 287 }
duke@435 288 guarantee(p == top(), "end of last object must match end of space");
duke@435 289 }
duke@435 290
duke@435 291 Thread* ThreadLocalAllocBuffer::myThread() {
duke@435 292 return (Thread*)(((char *)this) +
duke@435 293 in_bytes(start_offset()) -
duke@435 294 in_bytes(Thread::tlab_start_offset()));
duke@435 295 }
duke@435 296
duke@435 297
duke@435 298 GlobalTLABStats::GlobalTLABStats() :
duke@435 299 _allocating_threads_avg(TLABAllocationWeight) {
duke@435 300
duke@435 301 initialize();
duke@435 302
duke@435 303 _allocating_threads_avg.sample(1); // One allocating thread at startup
duke@435 304
duke@435 305 if (UsePerfData) {
duke@435 306
duke@435 307 EXCEPTION_MARK;
duke@435 308 ResourceMark rm;
duke@435 309
duke@435 310 char* cname = PerfDataManager::counter_name("tlab", "allocThreads");
duke@435 311 _perf_allocating_threads =
duke@435 312 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 313
duke@435 314 cname = PerfDataManager::counter_name("tlab", "fills");
duke@435 315 _perf_total_refills =
duke@435 316 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 317
duke@435 318 cname = PerfDataManager::counter_name("tlab", "maxFills");
duke@435 319 _perf_max_refills =
duke@435 320 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 321
duke@435 322 cname = PerfDataManager::counter_name("tlab", "alloc");
duke@435 323 _perf_allocation =
duke@435 324 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 325
duke@435 326 cname = PerfDataManager::counter_name("tlab", "gcWaste");
duke@435 327 _perf_gc_waste =
duke@435 328 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 329
duke@435 330 cname = PerfDataManager::counter_name("tlab", "maxGcWaste");
duke@435 331 _perf_max_gc_waste =
duke@435 332 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 333
duke@435 334 cname = PerfDataManager::counter_name("tlab", "slowWaste");
duke@435 335 _perf_slow_refill_waste =
duke@435 336 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 337
duke@435 338 cname = PerfDataManager::counter_name("tlab", "maxSlowWaste");
duke@435 339 _perf_max_slow_refill_waste =
duke@435 340 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 341
duke@435 342 cname = PerfDataManager::counter_name("tlab", "fastWaste");
duke@435 343 _perf_fast_refill_waste =
duke@435 344 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 345
duke@435 346 cname = PerfDataManager::counter_name("tlab", "maxFastWaste");
duke@435 347 _perf_max_fast_refill_waste =
duke@435 348 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 349
duke@435 350 cname = PerfDataManager::counter_name("tlab", "slowAlloc");
duke@435 351 _perf_slow_allocations =
duke@435 352 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 353
duke@435 354 cname = PerfDataManager::counter_name("tlab", "maxSlowAlloc");
duke@435 355 _perf_max_slow_allocations =
duke@435 356 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 357 }
duke@435 358 }
duke@435 359
duke@435 360 void GlobalTLABStats::initialize() {
duke@435 361 // Clear counters summarizing info from all threads
duke@435 362 _allocating_threads = 0;
duke@435 363 _total_refills = 0;
duke@435 364 _max_refills = 0;
duke@435 365 _total_allocation = 0;
duke@435 366 _total_gc_waste = 0;
duke@435 367 _max_gc_waste = 0;
duke@435 368 _total_slow_refill_waste = 0;
duke@435 369 _max_slow_refill_waste = 0;
duke@435 370 _total_fast_refill_waste = 0;
duke@435 371 _max_fast_refill_waste = 0;
duke@435 372 _total_slow_allocations = 0;
duke@435 373 _max_slow_allocations = 0;
duke@435 374 }
duke@435 375
duke@435 376 void GlobalTLABStats::publish() {
duke@435 377 _allocating_threads_avg.sample(_allocating_threads);
duke@435 378 if (UsePerfData) {
duke@435 379 _perf_allocating_threads ->set_value(_allocating_threads);
duke@435 380 _perf_total_refills ->set_value(_total_refills);
duke@435 381 _perf_max_refills ->set_value(_max_refills);
duke@435 382 _perf_allocation ->set_value(_total_allocation);
duke@435 383 _perf_gc_waste ->set_value(_total_gc_waste);
duke@435 384 _perf_max_gc_waste ->set_value(_max_gc_waste);
duke@435 385 _perf_slow_refill_waste ->set_value(_total_slow_refill_waste);
duke@435 386 _perf_max_slow_refill_waste->set_value(_max_slow_refill_waste);
duke@435 387 _perf_fast_refill_waste ->set_value(_total_fast_refill_waste);
duke@435 388 _perf_max_fast_refill_waste->set_value(_max_fast_refill_waste);
duke@435 389 _perf_slow_allocations ->set_value(_total_slow_allocations);
duke@435 390 _perf_max_slow_allocations ->set_value(_max_slow_allocations);
duke@435 391 }
duke@435 392 }
duke@435 393
duke@435 394 void GlobalTLABStats::print() {
duke@435 395 size_t waste = _total_gc_waste + _total_slow_refill_waste + _total_fast_refill_waste;
duke@435 396 double waste_percent = _total_allocation == 0 ? 0.0 :
duke@435 397 100.0 * waste / _total_allocation;
duke@435 398 gclog_or_tty->print("TLAB totals: thrds: %d refills: %d max: %d"
duke@435 399 " slow allocs: %d max %d waste: %4.1f%%"
duke@435 400 " gc: " SIZE_FORMAT "B max: " SIZE_FORMAT "B"
duke@435 401 " slow: " SIZE_FORMAT "B max: " SIZE_FORMAT "B"
duke@435 402 " fast: " SIZE_FORMAT "B max: " SIZE_FORMAT "B\n",
duke@435 403 _allocating_threads,
duke@435 404 _total_refills, _max_refills,
duke@435 405 _total_slow_allocations, _max_slow_allocations,
duke@435 406 waste_percent,
duke@435 407 _total_gc_waste * HeapWordSize,
duke@435 408 _max_gc_waste * HeapWordSize,
duke@435 409 _total_slow_refill_waste * HeapWordSize,
duke@435 410 _max_slow_refill_waste * HeapWordSize,
duke@435 411 _total_fast_refill_waste * HeapWordSize,
duke@435 412 _max_fast_refill_waste * HeapWordSize);
duke@435 413 }

mercurial