Add Java Heap Profiler sampling to large space allocations.

This cl adds both large space and non-movable allocations.

Test: Passing Tests
Local Testing

Bug: 160214819
Change-Id: Ice8950c12bd0ef8d765da21aeb2cc294850b2a07
diff --git a/runtime/gc/heap-inl.h b/runtime/gc/heap-inl.h
index 3fb2f45..50cfc6e 100644
--- a/runtime/gc/heap-inl.h
+++ b/runtime/gc/heap-inl.h
@@ -287,9 +287,11 @@
   // Save and restore the class in case it moves.
   StackHandleScope<1> hs(self);
   auto klass_wrapper = hs.NewHandleWrapper(klass);
-  return AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>(self, *klass, byte_count,
-                                                                         kAllocatorTypeLOS,
-                                                                         pre_fence_visitor);
+  mirror::Object* obj = AllocObjectWithAllocator<kInstrumented, false, PreFenceVisitor>
+                        (self, *klass, byte_count, kAllocatorTypeLOS, pre_fence_visitor);
+  // Java Heap Profiler check and sample allocation.
+  JHPCheckNonTlabSampleAllocation(self, obj, byte_count);
+  return obj;
 }
 
 template <const bool kInstrumented, const bool kGrow>
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 93b1bcf..5397d62 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -4146,11 +4146,11 @@
   return GetHeapSampler().IsEnabled();
 }
 
-void Heap::JHPCheckNonTlabSampleAllocation(Thread* self, mirror::Object* ret, size_t alloc_size) {
+void Heap::JHPCheckNonTlabSampleAllocation(Thread* self, mirror::Object* obj, size_t alloc_size) {
   bool take_sample = false;
   size_t bytes_until_sample = 0;
   HeapSampler& prof_heap_sampler = GetHeapSampler();
-  if (ret != nullptr && prof_heap_sampler.IsEnabled()) {
+  if (obj != nullptr && prof_heap_sampler.IsEnabled()) {
     // An allocation occurred, sample it, even if non-Tlab.
     // In case take_sample is already set from the previous GetSampleOffset
     // because we tried the Tlab allocation first, we will not use this value.
@@ -4163,9 +4163,9 @@
                                       &bytes_until_sample);
     prof_heap_sampler.SetBytesUntilSample(bytes_until_sample);
     if (take_sample) {
-      prof_heap_sampler.ReportSample(ret, alloc_size);
+      prof_heap_sampler.ReportSample(obj, alloc_size);
     }
-    VLOG(heap) << "JHP:NonTlab:AllocNonvirtual";
+    VLOG(heap) << "JHP:NonTlab Non-moving or Large Allocation";
   }
 }
 
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 53eed6b..5cad3f1 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -259,11 +259,14 @@
                !*backtrace_lock_,
                !process_state_update_lock_,
                !Roles::uninterruptible_) {
-    return AllocObjectWithAllocator<kInstrumented>(self,
-                                                   klass,
-                                                   num_bytes,
-                                                   GetCurrentNonMovingAllocator(),
-                                                   pre_fence_visitor);
+    mirror::Object* obj = AllocObjectWithAllocator<kInstrumented>(self,
+                                                                  klass,
+                                                                  num_bytes,
+                                                                  GetCurrentNonMovingAllocator(),
+                                                                  pre_fence_visitor);
+    // Java Heap Profiler check and sample allocation.
+    JHPCheckNonTlabSampleAllocation(self, obj, num_bytes);
+    return obj;
   }
 
   template <bool kInstrumented = true, bool kCheckLargeObject = true, typename PreFenceVisitor>
@@ -863,6 +866,8 @@
   int CheckPerfettoJHPEnabled();
   // In NonTlab case: Check whether we should report a sample allocation and if so report it.
   // Also update state (bytes_until_sample).
+  // By calling JHPCheckNonTlabSampleAllocation from different functions for Large allocations and
+  // non-moving allocations we are able to use the stack to identify these allocations separately.
   void JHPCheckNonTlabSampleAllocation(Thread* self,
                                        mirror::Object* ret,
                                        size_t alloc_size);