3030#include " gc/shared/memAllocator.hpp"
3131#include " gc/shared/threadLocalAllocBuffer.inline.hpp"
3232#include " gc/shared/tlab_globals.hpp"
33+ #include " jfr/jfrEvents.hpp"
3334#include " memory/universe.hpp"
3435#include " oops/arrayOop.hpp"
3536#include " oops/oop.inline.hpp"
4142#include " services/lowMemoryDetector.hpp"
4243#include " utilities/align.hpp"
4344#include " utilities/copy.hpp"
45+ #include " utilities/dtrace.hpp"
4446#include " utilities/globalDefinitions.hpp"
4547
4648class MemAllocator ::Allocation: StackObj {
@@ -171,7 +173,8 @@ void MemAllocator::Allocation::notify_allocation_jvmti_sampler() {
171173 return ;
172174 }
173175
174- if (!_allocated_outside_tlab && _allocated_tlab_size == 0 && !_tlab_end_reset_for_sample) {
176+ bool hit_mark = _allocated_tlab_size != 0 || _tlab_end_reset_for_sample;
177+ if (!_allocated_outside_tlab && !hit_mark) {
175178 // Sample if it's a non-TLAB allocation, or a TLAB allocation that either refills the TLAB
176179 // or expands it due to taking a sampler induced slow path.
177180 return ;
@@ -181,23 +184,21 @@ void MemAllocator::Allocation::notify_allocation_jvmti_sampler() {
181184 // before doing the callback. The callback is done in the destructor of
182185 // the JvmtiSampledObjectAllocEventCollector.
183186 size_t bytes_since_last = 0 ;
184-
187+ size_t bytes_since_allocation = 0 ;
185188 {
186189 PreserveObj obj_h (_thread, _obj_ptr);
187190 JvmtiSampledObjectAllocEventCollector collector;
188191 size_t size_in_bytes = _allocator._word_size * HeapWordSize;
189- ThreadLocalAllocBuffer& tlab = _thread->tlab ();
190192
191- if (!_allocated_outside_tlab) {
192- bytes_since_last = tlab. bytes_since_last_sample_point ( );
193+ if (_thread-> heap_samplers (). jvmti (). check_for_sampling (&bytes_since_allocation, size_in_bytes, !_allocated_outside_tlab) ) {
194+ JvmtiExport::sampled_object_alloc_event_collector ( obj_h () );
193195 }
194-
195- _thread->heap_sampler ().check_for_sampling (obj_h (), size_in_bytes, bytes_since_last);
196196 }
197197
198- if (_tlab_end_reset_for_sample || _allocated_tlab_size != 0 ) {
198+ if (hit_mark) {
199+ ThreadHeapSampler& sampler = _thread->heap_samplers ().jvmti ();
199200 // Tell tlab to forget bytes_since_last if we passed it to the heap sampler.
200- _thread->tlab ().set_sample_end (bytes_since_last != 0 );
201+ sampler. update_bytes ( _thread->tlab ().set_sample_end (sampler. bytes_until_sample ()), !_allocated_outside_tlab );
201202 }
202203}
203204
@@ -217,6 +218,36 @@ void MemAllocator::Allocation::notify_allocation_jfr_sampler() {
217218 AllocTracer::send_allocation_in_new_tlab (obj ()->klass (), mem, _allocated_tlab_size * HeapWordSize,
218219 size_in_bytes, _thread);
219220 }
221+
222+ EventObjectAllocationSample event;
223+ if (!event.should_commit ()) {
224+ return ;
225+ }
226+
227+ bool hit_mark = _allocated_tlab_size != 0 || _tlab_end_reset_for_sample;
228+ if (!_allocated_outside_tlab && !hit_mark) {
229+ // Sample if it's a non-TLAB allocation, or a TLAB allocation that either refills the TLAB
230+ // or expands it due to taking a sampler induced slow path.
231+ return ;
232+ }
233+
234+ ThreadHeapSampler& sampler = _thread->heap_samplers ().jfr ();
235+ if (sampler.bytes_until_sample () == static_cast <size_t >(-1 )) {
236+ return ;
237+ }
238+
239+ size_t bytes_since_allocation = 0 ;
240+ if (sampler.check_for_sampling (&bytes_since_allocation, size_in_bytes, !_allocated_outside_tlab)) {
241+ size_t weight = bytes_since_allocation == 0 ? size_in_bytes : bytes_since_allocation;
242+ AllocTracer::send_allocation_sample (obj ()->klass (), mem, size_in_bytes, weight, _allocated_outside_tlab, _thread);
243+ HOTSPOT_GC_ALLOCOBJECT_SAMPLE (obj ()->klass ()->name ()->as_C_string (), size_in_bytes, weight);
244+ }
245+
246+ if (hit_mark) {
247+ // Tell tlab to forget bytes_since_last if we passed it to the heap sampler.
248+ size_t bytes_inc = _thread->tlab ().set_sample_end (sampler.bytes_until_sample ());
249+ sampler.update_bytes (bytes_inc, !_allocated_outside_tlab);
250+ }
220251}
221252
222253void MemAllocator::Allocation::notify_allocation_dtrace_sampler () {
@@ -258,7 +289,9 @@ HeapWord* MemAllocator::mem_allocate_inside_tlab_slow(Allocation& allocation) co
258289 HeapWord* mem = nullptr ;
259290 ThreadLocalAllocBuffer& tlab = _thread->tlab ();
260291
261- if (JvmtiExport::should_post_sampled_object_alloc ()) {
292+ EventObjectAllocationSample event;
293+
294+ if (JvmtiExport::should_post_sampled_object_alloc () || event.should_commit ()) {
262295 tlab.set_back_allocation_end ();
263296 mem = tlab.allocate (_word_size);
264297
0 commit comments