src/hotspot/share/gc/shared/collectedHeap.cpp
changeset 50578 e2a7f431f65c
parent 50537 d12828b7cd64
child 50882 80abf702eed8
--- a/src/hotspot/share/gc/shared/collectedHeap.cpp	Mon Jun 11 15:28:24 2018 +0200
+++ b/src/hotspot/share/gc/shared/collectedHeap.cpp	Fri Jun 15 00:49:54 2018 -0700
@@ -378,6 +378,27 @@
 }
 
 HeapWord* CollectedHeap::allocate_from_tlab_slow(Klass* klass, size_t size, TRAPS) {
+  HeapWord* obj = NULL;
+
+  // In assertion mode, check that there was a sampling collector present
+  // in the stack. This enforces checking that no path is without a sampling
+  // collector.
+  // Only check if the sampler could actually sample something in this call path.
+  assert(!JvmtiExport::should_post_sampled_object_alloc()
+         || !JvmtiSampledObjectAllocEventCollector::object_alloc_is_safe_to_sample()
+         || THREAD->heap_sampler().sampling_collector_present(),
+         "Sampling collector not present.");
+
+  if (ThreadHeapSampler::enabled()) {
+    // Try to allocate the sampled object from TLAB, it is possible a sample
+    // point was put and the TLAB still has space.
+    obj = THREAD->tlab().allocate_sampled_object(size);
+
+    if (obj != NULL) {
+      return obj;
+    }
+  }
+
   ThreadLocalAllocBuffer& tlab = THREAD->tlab();
 
   // Retain tlab and allocate object in shared space if
@@ -401,7 +422,7 @@
   // between minimal and new_tlab_size is accepted.
   size_t actual_tlab_size = 0;
   size_t min_tlab_size = ThreadLocalAllocBuffer::compute_min_size(size);
-  HeapWord* obj = Universe::heap()->allocate_new_tlab(min_tlab_size, new_tlab_size, &actual_tlab_size);
+  obj = Universe::heap()->allocate_new_tlab(min_tlab_size, new_tlab_size, &actual_tlab_size);
   if (obj == NULL) {
     assert(actual_tlab_size == 0, "Allocation failed, but actual size was updated. min: " SIZE_FORMAT ", desired: " SIZE_FORMAT ", actual: " SIZE_FORMAT,
            min_tlab_size, new_tlab_size, actual_tlab_size);
@@ -425,6 +446,14 @@
     Copy::fill_to_words(obj + hdr_size, actual_tlab_size - hdr_size, badHeapWordVal);
 #endif // ASSERT
   }
+
+  // Send the thread information about this allocation in case a sample is
+  // requested.
+  if (ThreadHeapSampler::enabled()) {
+    size_t tlab_bytes_since_last_sample = THREAD->tlab().bytes_since_last_sample_point();
+    THREAD->heap_sampler().check_for_sampling(obj, size, tlab_bytes_since_last_sample);
+  }
+
   tlab.fill(obj, obj + size, actual_tlab_size);
   return obj;
 }