src/share/vm/memory/threadLocalAllocBuffer.cpp

Wed, 31 Jan 2018 19:24:57 -0500

author
dbuck
date
Wed, 31 Jan 2018 19:24:57 -0500
changeset 9289
427b2fb1944f
parent 7470
060cdf93040c
child 9448
73d689add964
child 9965
c39172598323
permissions
-rw-r--r--

8189170: Add option to disable stack overflow checking in primordial thread for use with JNI_CreateJavaJVM
Reviewed-by: dcubed

duke@435 1 /*
dbuck@9289 2 * Copyright (c) 1999, 2018, 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.
brutisso@6376 91 double alloc_frac = MIN2(1.0, (double) allocated_since_last_gc / used);
duke@435 92 _allocation_fraction.sample(alloc_frac);
duke@435 93 }
duke@435 94 global_stats()->update_allocating_threads();
duke@435 95 global_stats()->update_number_of_refills(_number_of_refills);
duke@435 96 global_stats()->update_allocation(_number_of_refills * desired_size());
duke@435 97 global_stats()->update_gc_waste(_gc_waste);
duke@435 98 global_stats()->update_slow_refill_waste(_slow_refill_waste);
duke@435 99 global_stats()->update_fast_refill_waste(_fast_refill_waste);
duke@435 100
duke@435 101 } else {
duke@435 102 assert(_number_of_refills == 0 && _fast_refill_waste == 0 &&
duke@435 103 _slow_refill_waste == 0 && _gc_waste == 0,
duke@435 104 "tlab stats == 0");
duke@435 105 }
duke@435 106 global_stats()->update_slow_allocations(_slow_allocations);
duke@435 107 }
duke@435 108
duke@435 109 // Fills the current tlab with a dummy filler array to create
duke@435 110 // an illusion of a contiguous Eden and optionally retires the tlab.
duke@435 111 // Waste accounting should be done in caller as appropriate; see,
duke@435 112 // for example, clear_before_allocation().
duke@435 113 void ThreadLocalAllocBuffer::make_parsable(bool retire) {
duke@435 114 if (end() != NULL) {
duke@435 115 invariants();
phh@2423 116
phh@2423 117 if (retire) {
phh@2423 118 myThread()->incr_allocated_bytes(used_bytes());
phh@2423 119 }
phh@2423 120
johnc@1600 121 CollectedHeap::fill_with_object(top(), hard_end(), retire);
duke@435 122
duke@435 123 if (retire || ZeroTLAB) { // "Reset" the TLAB
duke@435 124 set_start(NULL);
duke@435 125 set_top(NULL);
duke@435 126 set_pf_top(NULL);
duke@435 127 set_end(NULL);
duke@435 128 }
duke@435 129 }
duke@435 130 assert(!(retire || ZeroTLAB) ||
duke@435 131 (start() == NULL && end() == NULL && top() == NULL),
duke@435 132 "TLAB must be reset");
duke@435 133 }
duke@435 134
duke@435 135 void ThreadLocalAllocBuffer::resize_all_tlabs() {
brutisso@6376 136 if (ResizeTLAB) {
brutisso@6376 137 for (JavaThread *thread = Threads::first(); thread != NULL; thread = thread->next()) {
brutisso@6376 138 thread->tlab().resize();
brutisso@6376 139 }
duke@435 140 }
duke@435 141 }
duke@435 142
duke@435 143 void ThreadLocalAllocBuffer::resize() {
brutisso@6376 144 // Compute the next tlab size using expected allocation amount
brutisso@6376 145 assert(ResizeTLAB, "Should not call this otherwise");
brutisso@6376 146 size_t alloc = (size_t)(_allocation_fraction.average() *
brutisso@6376 147 (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize));
brutisso@6376 148 size_t new_size = alloc / _target_refills;
duke@435 149
brutisso@6376 150 new_size = MIN2(MAX2(new_size, min_size()), max_size());
duke@435 151
brutisso@6376 152 size_t aligned_new_size = align_object_size(new_size);
duke@435 153
brutisso@6376 154 if (PrintTLAB && Verbose) {
brutisso@6376 155 gclog_or_tty->print("TLAB new size: thread: " INTPTR_FORMAT " [id: %2d]"
brutisso@6376 156 " refills %d alloc: %8.6f desired_size: " SIZE_FORMAT " -> " SIZE_FORMAT "\n",
brutisso@6376 157 myThread(), myThread()->osthread()->thread_id(),
brutisso@6376 158 _target_refills, _allocation_fraction.average(), desired_size(), aligned_new_size);
duke@435 159 }
brutisso@6376 160 set_desired_size(aligned_new_size);
brutisso@6376 161 set_refill_waste_limit(initial_refill_waste_limit());
duke@435 162 }
duke@435 163
duke@435 164 void ThreadLocalAllocBuffer::initialize_statistics() {
duke@435 165 _number_of_refills = 0;
duke@435 166 _fast_refill_waste = 0;
duke@435 167 _slow_refill_waste = 0;
duke@435 168 _gc_waste = 0;
duke@435 169 _slow_allocations = 0;
duke@435 170 }
duke@435 171
duke@435 172 void ThreadLocalAllocBuffer::fill(HeapWord* start,
duke@435 173 HeapWord* top,
duke@435 174 size_t new_size) {
duke@435 175 _number_of_refills++;
duke@435 176 if (PrintTLAB && Verbose) {
duke@435 177 print_stats("fill");
duke@435 178 }
duke@435 179 assert(top <= start + new_size - alignment_reserve(), "size too small");
duke@435 180 initialize(start, top, start + new_size - alignment_reserve());
duke@435 181
duke@435 182 // Reset amount of internal fragmentation
duke@435 183 set_refill_waste_limit(initial_refill_waste_limit());
duke@435 184 }
duke@435 185
duke@435 186 void ThreadLocalAllocBuffer::initialize(HeapWord* start,
duke@435 187 HeapWord* top,
duke@435 188 HeapWord* end) {
duke@435 189 set_start(start);
duke@435 190 set_top(top);
duke@435 191 set_pf_top(top);
duke@435 192 set_end(end);
duke@435 193 invariants();
duke@435 194 }
duke@435 195
duke@435 196 void ThreadLocalAllocBuffer::initialize() {
duke@435 197 initialize(NULL, // start
duke@435 198 NULL, // top
duke@435 199 NULL); // end
duke@435 200
duke@435 201 set_desired_size(initial_desired_size());
duke@435 202
dbuck@9289 203 // Following check is needed because at startup the main
duke@435 204 // thread is initialized before the heap is. The initialization for
duke@435 205 // this thread is redone in startup_initialization below.
duke@435 206 if (Universe::heap() != NULL) {
duke@435 207 size_t capacity = Universe::heap()->tlab_capacity(myThread()) / HeapWordSize;
duke@435 208 double alloc_frac = desired_size() * target_refills() / (double) capacity;
duke@435 209 _allocation_fraction.sample(alloc_frac);
duke@435 210 }
duke@435 211
duke@435 212 set_refill_waste_limit(initial_refill_waste_limit());
duke@435 213
duke@435 214 initialize_statistics();
duke@435 215 }
duke@435 216
duke@435 217 void ThreadLocalAllocBuffer::startup_initialization() {
duke@435 218
duke@435 219 // Assuming each thread's active tlab is, on average,
duke@435 220 // 1/2 full at a GC
duke@435 221 _target_refills = 100 / (2 * TLABWasteTargetPercent);
duke@435 222 _target_refills = MAX2(_target_refills, (unsigned)1U);
duke@435 223
duke@435 224 _global_stats = new GlobalTLABStats();
duke@435 225
dbuck@9289 226 // During jvm startup, the main thread is initialized
duke@435 227 // before the heap is initialized. So reinitialize it now.
duke@435 228 guarantee(Thread::current()->is_Java_thread(), "tlab initialization thread not Java thread");
duke@435 229 Thread::current()->tlab().initialize();
duke@435 230
duke@435 231 if (PrintTLAB && Verbose) {
duke@435 232 gclog_or_tty->print("TLAB min: " SIZE_FORMAT " initial: " SIZE_FORMAT " max: " SIZE_FORMAT "\n",
duke@435 233 min_size(), Thread::current()->tlab().initial_desired_size(), max_size());
duke@435 234 }
duke@435 235 }
duke@435 236
duke@435 237 size_t ThreadLocalAllocBuffer::initial_desired_size() {
mgerdin@7470 238 size_t init_sz = 0;
duke@435 239
duke@435 240 if (TLABSize > 0) {
mgerdin@7470 241 init_sz = TLABSize / HeapWordSize;
mgerdin@7470 242 } else if (global_stats() != NULL) {
duke@435 243 // Initial size is a function of the average number of allocating threads.
duke@435 244 unsigned nof_threads = global_stats()->allocating_threads_avg();
duke@435 245
duke@435 246 init_sz = (Universe::heap()->tlab_capacity(myThread()) / HeapWordSize) /
duke@435 247 (nof_threads * target_refills());
duke@435 248 init_sz = align_object_size(init_sz);
duke@435 249 }
mgerdin@7470 250 init_sz = MIN2(MAX2(init_sz, min_size()), max_size());
duke@435 251 return init_sz;
duke@435 252 }
duke@435 253
duke@435 254 void ThreadLocalAllocBuffer::print_stats(const char* tag) {
duke@435 255 Thread* thrd = myThread();
duke@435 256 size_t waste = _gc_waste + _slow_refill_waste + _fast_refill_waste;
duke@435 257 size_t alloc = _number_of_refills * _desired_size;
duke@435 258 double waste_percent = alloc == 0 ? 0.0 :
duke@435 259 100.0 * waste / alloc;
brutisso@6376 260 size_t tlab_used = Universe::heap()->tlab_used(thrd);
duke@435 261 gclog_or_tty->print("TLAB: %s thread: " INTPTR_FORMAT " [id: %2d]"
duke@435 262 " desired_size: " SIZE_FORMAT "KB"
duke@435 263 " slow allocs: %d refill waste: " SIZE_FORMAT "B"
duke@435 264 " alloc:%8.5f %8.0fKB refills: %d waste %4.1f%% gc: %dB"
duke@435 265 " slow: %dB fast: %dB\n",
duke@435 266 tag, thrd, thrd->osthread()->thread_id(),
duke@435 267 _desired_size / (K / HeapWordSize),
duke@435 268 _slow_allocations, _refill_waste_limit * HeapWordSize,
duke@435 269 _allocation_fraction.average(),
duke@435 270 _allocation_fraction.average() * tlab_used / K,
duke@435 271 _number_of_refills, waste_percent,
duke@435 272 _gc_waste * HeapWordSize,
duke@435 273 _slow_refill_waste * HeapWordSize,
duke@435 274 _fast_refill_waste * HeapWordSize);
duke@435 275 }
duke@435 276
duke@435 277 void ThreadLocalAllocBuffer::verify() {
duke@435 278 HeapWord* p = start();
duke@435 279 HeapWord* t = top();
duke@435 280 HeapWord* prev_p = NULL;
duke@435 281 while (p < t) {
duke@435 282 oop(p)->verify();
duke@435 283 prev_p = p;
duke@435 284 p += oop(p)->size();
duke@435 285 }
duke@435 286 guarantee(p == top(), "end of last object must match end of space");
duke@435 287 }
duke@435 288
duke@435 289 Thread* ThreadLocalAllocBuffer::myThread() {
duke@435 290 return (Thread*)(((char *)this) +
duke@435 291 in_bytes(start_offset()) -
duke@435 292 in_bytes(Thread::tlab_start_offset()));
duke@435 293 }
duke@435 294
duke@435 295
duke@435 296 GlobalTLABStats::GlobalTLABStats() :
duke@435 297 _allocating_threads_avg(TLABAllocationWeight) {
duke@435 298
duke@435 299 initialize();
duke@435 300
duke@435 301 _allocating_threads_avg.sample(1); // One allocating thread at startup
duke@435 302
duke@435 303 if (UsePerfData) {
duke@435 304
duke@435 305 EXCEPTION_MARK;
duke@435 306 ResourceMark rm;
duke@435 307
duke@435 308 char* cname = PerfDataManager::counter_name("tlab", "allocThreads");
duke@435 309 _perf_allocating_threads =
duke@435 310 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 311
duke@435 312 cname = PerfDataManager::counter_name("tlab", "fills");
duke@435 313 _perf_total_refills =
duke@435 314 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 315
duke@435 316 cname = PerfDataManager::counter_name("tlab", "maxFills");
duke@435 317 _perf_max_refills =
duke@435 318 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 319
duke@435 320 cname = PerfDataManager::counter_name("tlab", "alloc");
duke@435 321 _perf_allocation =
duke@435 322 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 323
duke@435 324 cname = PerfDataManager::counter_name("tlab", "gcWaste");
duke@435 325 _perf_gc_waste =
duke@435 326 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 327
duke@435 328 cname = PerfDataManager::counter_name("tlab", "maxGcWaste");
duke@435 329 _perf_max_gc_waste =
duke@435 330 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 331
duke@435 332 cname = PerfDataManager::counter_name("tlab", "slowWaste");
duke@435 333 _perf_slow_refill_waste =
duke@435 334 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 335
duke@435 336 cname = PerfDataManager::counter_name("tlab", "maxSlowWaste");
duke@435 337 _perf_max_slow_refill_waste =
duke@435 338 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 339
duke@435 340 cname = PerfDataManager::counter_name("tlab", "fastWaste");
duke@435 341 _perf_fast_refill_waste =
duke@435 342 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 343
duke@435 344 cname = PerfDataManager::counter_name("tlab", "maxFastWaste");
duke@435 345 _perf_max_fast_refill_waste =
duke@435 346 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_Bytes, CHECK);
duke@435 347
duke@435 348 cname = PerfDataManager::counter_name("tlab", "slowAlloc");
duke@435 349 _perf_slow_allocations =
duke@435 350 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 351
duke@435 352 cname = PerfDataManager::counter_name("tlab", "maxSlowAlloc");
duke@435 353 _perf_max_slow_allocations =
duke@435 354 PerfDataManager::create_variable(SUN_GC, cname, PerfData::U_None, CHECK);
duke@435 355 }
duke@435 356 }
duke@435 357
duke@435 358 void GlobalTLABStats::initialize() {
duke@435 359 // Clear counters summarizing info from all threads
duke@435 360 _allocating_threads = 0;
duke@435 361 _total_refills = 0;
duke@435 362 _max_refills = 0;
duke@435 363 _total_allocation = 0;
duke@435 364 _total_gc_waste = 0;
duke@435 365 _max_gc_waste = 0;
duke@435 366 _total_slow_refill_waste = 0;
duke@435 367 _max_slow_refill_waste = 0;
duke@435 368 _total_fast_refill_waste = 0;
duke@435 369 _max_fast_refill_waste = 0;
duke@435 370 _total_slow_allocations = 0;
duke@435 371 _max_slow_allocations = 0;
duke@435 372 }
duke@435 373
duke@435 374 void GlobalTLABStats::publish() {
duke@435 375 _allocating_threads_avg.sample(_allocating_threads);
duke@435 376 if (UsePerfData) {
duke@435 377 _perf_allocating_threads ->set_value(_allocating_threads);
duke@435 378 _perf_total_refills ->set_value(_total_refills);
duke@435 379 _perf_max_refills ->set_value(_max_refills);
duke@435 380 _perf_allocation ->set_value(_total_allocation);
duke@435 381 _perf_gc_waste ->set_value(_total_gc_waste);
duke@435 382 _perf_max_gc_waste ->set_value(_max_gc_waste);
duke@435 383 _perf_slow_refill_waste ->set_value(_total_slow_refill_waste);
duke@435 384 _perf_max_slow_refill_waste->set_value(_max_slow_refill_waste);
duke@435 385 _perf_fast_refill_waste ->set_value(_total_fast_refill_waste);
duke@435 386 _perf_max_fast_refill_waste->set_value(_max_fast_refill_waste);
duke@435 387 _perf_slow_allocations ->set_value(_total_slow_allocations);
duke@435 388 _perf_max_slow_allocations ->set_value(_max_slow_allocations);
duke@435 389 }
duke@435 390 }
duke@435 391
duke@435 392 void GlobalTLABStats::print() {
duke@435 393 size_t waste = _total_gc_waste + _total_slow_refill_waste + _total_fast_refill_waste;
duke@435 394 double waste_percent = _total_allocation == 0 ? 0.0 :
duke@435 395 100.0 * waste / _total_allocation;
duke@435 396 gclog_or_tty->print("TLAB totals: thrds: %d refills: %d max: %d"
duke@435 397 " slow allocs: %d max %d waste: %4.1f%%"
duke@435 398 " gc: " SIZE_FORMAT "B max: " SIZE_FORMAT "B"
duke@435 399 " slow: " SIZE_FORMAT "B max: " SIZE_FORMAT "B"
duke@435 400 " fast: " SIZE_FORMAT "B max: " SIZE_FORMAT "B\n",
duke@435 401 _allocating_threads,
duke@435 402 _total_refills, _max_refills,
duke@435 403 _total_slow_allocations, _max_slow_allocations,
duke@435 404 waste_percent,
duke@435 405 _total_gc_waste * HeapWordSize,
duke@435 406 _max_gc_waste * HeapWordSize,
duke@435 407 _total_slow_refill_waste * HeapWordSize,
duke@435 408 _max_slow_refill_waste * HeapWordSize,
duke@435 409 _total_fast_refill_waste * HeapWordSize,
duke@435 410 _max_fast_refill_waste * HeapWordSize);
duke@435 411 }

mercurial