--- old/src/share/vm/gc/shared/collectedHeap.cpp 2017-06-12 10:25:10.870262634 -0700 +++ new/src/share/vm/gc/shared/collectedHeap.cpp 2017-06-12 10:25:10.750263101 -0700 @@ -38,6 +38,7 @@ #include "memory/resourceArea.hpp" #include "oops/instanceMirrorKlass.hpp" #include "oops/oop.inline.hpp" +#include "runtime/heapMonitoring.hpp" #include "runtime/init.hpp" #include "runtime/thread.inline.hpp" #include "services/heapDumper.hpp" @@ -296,6 +297,27 @@ #endif HeapWord* CollectedHeap::allocate_from_tlab_slow(Klass* klass, Thread* thread, size_t size) { + // We can come here for three reasons: + // - We either really did fill the tlab. + // - We pretended to everyone we did and we want to sample. + // - Both of the above reasons are true at the same time. + if (HeapMonitoring::enabled()) { + if (thread->tlab().should_sample()) { + // The tlab could still have space after this sample. + thread->tlab().set_back_actual_end(); + + // Try to allocate again: it could work now. + HeapWord* obj = thread->tlab().allocate(size); + if (obj != NULL) { + // Object got allocated, sample it now. + HeapMonitoring::object_alloc_do_sample(thread, + reinterpret_cast(obj), + size); + thread->tlab().pick_next_sample(); + return obj; + } + } + } // Retain tlab and allocate object in shared space if // the amount free in the tlab is too large to discard. @@ -336,6 +358,16 @@ #endif // ASSERT } thread->tlab().fill(obj, obj + size, new_tlab_size); + + if (HeapMonitoring::enabled()) { + if (thread->tlab().should_sample()) { + HeapMonitoring::object_alloc_do_sample(thread, + reinterpret_cast(obj), + size); + } + // Always pick a next sample here. + thread->tlab().pick_next_sample(); + } return obj; }