--- old/src/share/vm/gc/shared/collectedHeap.cpp 2017-06-27 14:19:06.364532173 -0700 +++ new/src/share/vm/gc/shared/collectedHeap.cpp 2017-06-27 14:19:06.240532610 -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" @@ -295,7 +296,40 @@ } #endif +HeapWord* CollectedHeap::handle_heap_sampling(Thread* thread, HeapWord* obj, 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()) { + // If we don't have an object yet, try to allocate it. + if (obj == NULL) { + // The tlab could still have space after this sample. + thread->tlab().set_back_actual_end(); + obj = thread->tlab().allocate(size); + } + + // Is the object allocated now? + if (obj != NULL) { + // Object is allocated, sample it now. + HeapMonitoring::object_alloc_do_sample(thread, + reinterpret_cast(obj), + size); + } + } + } + + thread->tlab().pick_next_sample(); + return obj; +} + HeapWord* CollectedHeap::allocate_from_tlab_slow(Klass* klass, Thread* thread, size_t size) { + HeapWord* obj = handle_heap_sampling(thread, NULL, size); + + if (obj != NULL) { + return obj; + } // Retain tlab and allocate object in shared space if // the amount free in the tlab is too large to discard. @@ -315,7 +349,7 @@ } // Allocate a new TLAB... - HeapWord* obj = Universe::heap()->allocate_new_tlab(new_tlab_size); + obj = Universe::heap()->allocate_new_tlab(new_tlab_size); if (obj == NULL) { return NULL; } @@ -336,6 +370,7 @@ #endif // ASSERT } thread->tlab().fill(obj, obj + size, new_tlab_size); + handle_heap_sampling(thread, obj, size); return obj; }