1 /*
2 * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 
25 #include "precompiled.hpp"
26 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
27 #include "gc/shared/tlab_globals.hpp"
28 #include "jfr/jfrEvents.hpp"
29 #include "jfr/support/jfrObjectAllocationSample.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 
32 static THREAD_LOCAL int64_t _last_allocated_bytes = 0;
33 
send_allocation_sample(const Klass * klass,int64_t allocated_bytes)34 inline void send_allocation_sample(const Klass* klass, int64_t allocated_bytes) {
35   assert(allocated_bytes > 0, "invariant");
36   EventObjectAllocationSample event;
37   if (event.should_commit()) {
38     const size_t weight = allocated_bytes - _last_allocated_bytes;
39     assert(weight > 0, "invariant");
40     event.set_objectClass(klass);
41     event.set_weight(weight);
42     event.commit();
43     _last_allocated_bytes = allocated_bytes;
44   }
45 }
46 
send_allocation_sample_with_result(const Klass * klass,int64_t allocated_bytes)47 inline bool send_allocation_sample_with_result(const Klass* klass, int64_t allocated_bytes) {
48   assert(allocated_bytes > 0, "invariant");
49   EventObjectAllocationSample event;
50   if (event.should_commit()) {
51     const size_t weight = allocated_bytes - _last_allocated_bytes;
52     assert(weight > 0, "invariant");
53     event.set_objectClass(klass);
54     event.set_weight(weight);
55     event.commit();
56     _last_allocated_bytes = allocated_bytes;
57     return true;
58   }
59   return false;
60 }
61 
estimate_tlab_size_bytes(Thread * thread)62 inline intptr_t estimate_tlab_size_bytes(Thread* thread) {
63   const size_t desired_tlab_size_bytes = thread->tlab().desired_size() * HeapWordSize;
64   const size_t alignment_reserve_bytes = thread->tlab().alignment_reserve_in_bytes();
65   assert(desired_tlab_size_bytes > alignment_reserve_bytes, "invariant");
66   return static_cast<intptr_t>(desired_tlab_size_bytes - alignment_reserve_bytes);
67 }
68 
load_allocated_bytes(Thread * thread)69 inline int64_t load_allocated_bytes(Thread* thread) {
70   assert(thread != NULL, "invariant");
71   const int64_t allocated_bytes = thread->allocated_bytes();
72   if (allocated_bytes < _last_allocated_bytes) {
73     // A hw thread can detach and reattach to the VM, and when it does,
74     // it gets a new JavaThread representation. The thread local variable
75     // tracking _last_allocated_bytes is mapped to the existing hw thread,
76     // so it needs to be reset.
77     _last_allocated_bytes = 0;
78   }
79   return allocated_bytes == _last_allocated_bytes ? 0 : allocated_bytes;
80 }
81 
82 // To avoid large objects from being undersampled compared to the regular TLAB samples,
83 // the data amount is normalized as if it was a TLAB, giving a number of TLAB sampling attempts to the large object.
normalize_as_tlab_and_send_allocation_samples(const Klass * klass,intptr_t obj_alloc_size_bytes,Thread * thread)84 static void normalize_as_tlab_and_send_allocation_samples(const Klass* klass, intptr_t obj_alloc_size_bytes, Thread* thread) {
85   const int64_t allocated_bytes = load_allocated_bytes(thread);
86   assert(allocated_bytes > 0, "invariant"); // obj_alloc_size_bytes is already attributed to allocated_bytes at this point.
87   if (!UseTLAB) {
88     send_allocation_sample(klass, allocated_bytes);
89     return;
90   }
91   const intptr_t tlab_size_bytes = estimate_tlab_size_bytes(thread);
92   if (allocated_bytes - _last_allocated_bytes < tlab_size_bytes) {
93     return;
94   }
95   assert(obj_alloc_size_bytes > 0, "invariant");
96   do {
97     if (send_allocation_sample_with_result(klass, allocated_bytes)) {
98       return;
99     }
100     obj_alloc_size_bytes -= tlab_size_bytes;
101   } while (obj_alloc_size_bytes > 0);
102 }
103 
send_event(const Klass * klass,size_t alloc_size,bool outside_tlab,Thread * thread)104 void JfrObjectAllocationSample::send_event(const Klass* klass, size_t alloc_size, bool outside_tlab, Thread* thread) {
105   if (outside_tlab) {
106     normalize_as_tlab_and_send_allocation_samples(klass, static_cast<intptr_t>(alloc_size), thread);
107     return;
108   }
109   const int64_t allocated_bytes = load_allocated_bytes(thread);
110   if (allocated_bytes == 0) {
111     return;
112   }
113   send_allocation_sample(klass, allocated_bytes);
114 }
115