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 "classfile/javaClasses.hpp"
27 #include "gc/shared/allocTracer.hpp"
28 #include "gc/shared/collectedHeap.hpp"
29 #include "gc/shared/memAllocator.hpp"
30 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/arrayOop.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "prims/jvmtiExport.hpp"
35 #include "runtime/sharedRuntime.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/thread.inline.hpp"
38 #include "services/lowMemoryDetector.hpp"
39 #include "utilities/align.hpp"
40 #include "utilities/copy.hpp"
41
42 class MemAllocator::Allocation: StackObj {
43 friend class MemAllocator;
44
45 const MemAllocator& _allocator;
46 Thread* _thread;
47 oop* _obj_ptr;
48 bool _overhead_limit_exceeded;
49 bool _allocated_outside_tlab;
50 size_t _allocated_tlab_size;
51 bool _tlab_end_reset_for_sample;
52
53 bool check_out_of_memory();
54 void verify_before();
166 }
167
168 #ifdef ASSERT
169 void MemAllocator::Allocation::check_for_valid_allocation_state() const {
170 // How to choose between a pending exception and a potential
171 // OutOfMemoryError? Don't allow pending exceptions.
172 // This is a VM policy failure, so how do we exhaustively test it?
173 assert(!_thread->has_pending_exception(),
174 "shouldn't be allocating with pending exception");
175 if (StrictSafepointChecks) {
176 assert(_thread->allow_allocation(),
177 "Allocation done by thread for which allocation is blocked "
178 "by No_Allocation_Verifier!");
179 // Allocation of an oop can always invoke a safepoint,
180 // hence, the true argument
181 _thread->check_for_valid_safepoint_state(true);
182 }
183 }
184 #endif
185
186 void MemAllocator::Allocation::notify_allocation_jvmti_sampler() {
187 // support for JVMTI VMObjectAlloc event (no-op if not enabled)
188 JvmtiExport::vm_object_alloc_event_collector(obj());
189
190 if (!JvmtiExport::should_post_sampled_object_alloc()) {
191 // Sampling disabled
192 return;
193 }
194
195 if (!_allocated_outside_tlab && _allocated_tlab_size == 0 && !_tlab_end_reset_for_sample) {
196 // Sample if it's a non-TLAB allocation, or a TLAB allocation that either refills the TLAB
197 // or expands it due to taking a sampler induced slow path.
198 return;
199 }
200
201 if (JvmtiExport::should_post_sampled_object_alloc()) {
202 // If we want to be sampling, protect the allocated object with a Handle
203 // before doing the callback. The callback is done in the destructor of
204 // the JvmtiSampledObjectAllocEventCollector.
205 PreserveObj obj_h(_thread, _obj_ptr);
206 JvmtiSampledObjectAllocEventCollector collector;
207 size_t size_in_bytes = _allocator._word_size * HeapWordSize;
208 ThreadLocalAllocBuffer& tlab = _thread->tlab();
209 size_t bytes_since_last = _allocated_outside_tlab ? 0 : tlab.bytes_since_last_sample_point();
210 _thread->heap_sampler().check_for_sampling(obj_h(), size_in_bytes, bytes_since_last);
211 }
|
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 "classfile/javaClasses.hpp"
27 #include "gc/shared/allocTracer.hpp"
28 #include "gc/shared/collectedHeap.hpp"
29 #include "gc/shared/memAllocator.hpp"
30 #include "gc/shared/threadLocalAllocBuffer.inline.hpp"
31 #include "memory/universe.hpp"
32 #include "oops/arrayOop.hpp"
33 #include "oops/oop.inline.hpp"
34 #include "prims/jvmtiExport.hpp"
35 #include "prims/jvmtiEventController.inline.hpp"
36 #include "prims/jvmtiThreadState.inline.hpp"
37 #include "runtime/sharedRuntime.hpp"
38 #include "runtime/handles.inline.hpp"
39 #include "runtime/thread.inline.hpp"
40 #include "services/lowMemoryDetector.hpp"
41 #include "utilities/align.hpp"
42 #include "utilities/copy.hpp"
43
44 class MemAllocator::Allocation: StackObj {
45 friend class MemAllocator;
46
47 const MemAllocator& _allocator;
48 Thread* _thread;
49 oop* _obj_ptr;
50 bool _overhead_limit_exceeded;
51 bool _allocated_outside_tlab;
52 size_t _allocated_tlab_size;
53 bool _tlab_end_reset_for_sample;
54
55 bool check_out_of_memory();
56 void verify_before();
168 }
169
170 #ifdef ASSERT
171 void MemAllocator::Allocation::check_for_valid_allocation_state() const {
172 // How to choose between a pending exception and a potential
173 // OutOfMemoryError? Don't allow pending exceptions.
174 // This is a VM policy failure, so how do we exhaustively test it?
175 assert(!_thread->has_pending_exception(),
176 "shouldn't be allocating with pending exception");
177 if (StrictSafepointChecks) {
178 assert(_thread->allow_allocation(),
179 "Allocation done by thread for which allocation is blocked "
180 "by No_Allocation_Verifier!");
181 // Allocation of an oop can always invoke a safepoint,
182 // hence, the true argument
183 _thread->check_for_valid_safepoint_state(true);
184 }
185 }
186 #endif
187
188 static bool thread_enabled_for_one_jvmti_env() {
189 JavaThread *thread = JavaThread::current();
190 JvmtiThreadState *state = thread->jvmti_thread_state();
191 if (state == NULL) {
192 return false;
193 }
194
195 JvmtiEnvThreadStateIterator it(state);
196 for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
197 if (ets->is_enabled(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC)) {
198 return true;
199 }
200 }
201
202 return false;
203 }
204
205 void MemAllocator::Allocation::notify_allocation_jvmti_sampler() {
206 // support for JVMTI VMObjectAlloc event (no-op if not enabled)
207 JvmtiExport::vm_object_alloc_event_collector(obj());
208
209 if (!JvmtiExport::should_post_sampled_object_alloc()) {
210 // Sampling disabled
211 return;
212 }
213
214 // Sampling is enabled for at least one thread, is it this one?
215 if (!thread_enabled_for_one_jvmti_env()) {
216 return;
217 }
218
219 if (!_allocated_outside_tlab && _allocated_tlab_size == 0 && !_tlab_end_reset_for_sample) {
220 // Sample if it's a non-TLAB allocation, or a TLAB allocation that either refills the TLAB
221 // or expands it due to taking a sampler induced slow path.
222 return;
223 }
224
225 if (JvmtiExport::should_post_sampled_object_alloc()) {
226 // If we want to be sampling, protect the allocated object with a Handle
227 // before doing the callback. The callback is done in the destructor of
228 // the JvmtiSampledObjectAllocEventCollector.
229 PreserveObj obj_h(_thread, _obj_ptr);
230 JvmtiSampledObjectAllocEventCollector collector;
231 size_t size_in_bytes = _allocator._word_size * HeapWordSize;
232 ThreadLocalAllocBuffer& tlab = _thread->tlab();
233 size_t bytes_since_last = _allocated_outside_tlab ? 0 : tlab.bytes_since_last_sample_point();
234 _thread->heap_sampler().check_for_sampling(obj_h(), size_in_bytes, bytes_since_last);
235 }
|