1/*
2 * Copyright (c) 1997, 2018, 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/collectedHeap.hpp"
27#include "gc/shared/collectedHeap.inline.hpp"
28#include "logging/log.hpp"
29#include "memory/resourceArea.hpp"
30#include "memory/universe.hpp"
31#include "runtime/atomic.hpp"
32#include "runtime/frame.inline.hpp"
33#include "runtime/handles.inline.hpp"
34#include "runtime/init.hpp"
35#include "runtime/interfaceSupport.inline.hpp"
36#include "runtime/orderAccess.hpp"
37#include "runtime/os.inline.hpp"
38#include "runtime/thread.inline.hpp"
39#include "runtime/safepointVerifiers.hpp"
40#include "runtime/vframe.hpp"
41#include "runtime/vmThread.hpp"
42#include "utilities/preserveException.hpp"
43
44// Implementation of InterfaceSupport
45
46#ifdef ASSERT
47VMEntryWrapper::VMEntryWrapper() {
48 if (VerifyLastFrame) {
49 InterfaceSupport::verify_last_frame();
50 }
51}
52
53VMEntryWrapper::~VMEntryWrapper() {
54 InterfaceSupport::check_gc_alot();
55 if (WalkStackALot) {
56 InterfaceSupport::walk_stack();
57 }
58#ifdef COMPILER2
59 // This option is not used by Compiler 1
60 if (StressDerivedPointers) {
61 InterfaceSupport::stress_derived_pointers();
62 }
63#endif
64 if (DeoptimizeALot || DeoptimizeRandom) {
65 InterfaceSupport::deoptimizeAll();
66 }
67 if (ZombieALot) {
68 InterfaceSupport::zombieAll();
69 }
70 // do verification AFTER potential deoptimization
71 if (VerifyStack) {
72 InterfaceSupport::verify_stack();
73 }
74}
75
76long InterfaceSupport::_number_of_calls = 0;
77long InterfaceSupport::_scavenge_alot_counter = 1;
78long InterfaceSupport::_fullgc_alot_counter = 1;
79long InterfaceSupport::_fullgc_alot_invocation = 0;
80
81Histogram* RuntimeHistogram;
82
83RuntimeHistogramElement::RuntimeHistogramElement(const char* elementName) {
84 static volatile int RuntimeHistogram_lock = 0;
85 _name = elementName;
86 uintx count = 0;
87
88 while (Atomic::cmpxchg(1, &RuntimeHistogram_lock, 0) != 0) {
89 while (OrderAccess::load_acquire(&RuntimeHistogram_lock) != 0) {
90 count +=1;
91 if ( (WarnOnStalledSpinLock > 0)
92 && (count % WarnOnStalledSpinLock == 0)) {
93 warning("RuntimeHistogram_lock seems to be stalled");
94 }
95 }
96 }
97
98 if (RuntimeHistogram == NULL) {
99 RuntimeHistogram = new Histogram("VM Runtime Call Counts",200);
100 }
101
102 RuntimeHistogram->add_element(this);
103 Atomic::dec(&RuntimeHistogram_lock);
104}
105
106void InterfaceSupport::gc_alot() {
107 Thread *thread = Thread::current();
108 if (!thread->is_Java_thread()) return; // Avoid concurrent calls
109 // Check for new, not quite initialized thread. A thread in new mode cannot initiate a GC.
110 JavaThread *current_thread = (JavaThread *)thread;
111 if (current_thread->active_handles() == NULL) return;
112
113 // Short-circuit any possible re-entrant gc-a-lot attempt
114 if (thread->skip_gcalot()) return;
115
116 if (Threads::is_vm_complete()) {
117
118 if (++_fullgc_alot_invocation < FullGCALotStart) {
119 return;
120 }
121
122 // Use this line if you want to block at a specific point,
123 // e.g. one number_of_calls/scavenge/gc before you got into problems
124 if (FullGCALot) _fullgc_alot_counter--;
125
126 // Check if we should force a full gc
127 if (_fullgc_alot_counter == 0) {
128 // Release dummy so objects are forced to move
129 if (!Universe::release_fullgc_alot_dummy()) {
130 warning("FullGCALot: Unable to release more dummies at bottom of heap");
131 }
132 HandleMark hm(thread);
133 Universe::heap()->collect(GCCause::_full_gc_alot);
134 unsigned int invocations = Universe::heap()->total_full_collections();
135 // Compute new interval
136 if (FullGCALotInterval > 1) {
137 _fullgc_alot_counter = 1+(long)((double)FullGCALotInterval*os::random()/(max_jint+1.0));
138 log_trace(gc)("Full gc no: %u\tInterval: %ld", invocations, _fullgc_alot_counter);
139 } else {
140 _fullgc_alot_counter = 1;
141 }
142 // Print progress message
143 if (invocations % 100 == 0) {
144 log_trace(gc)("Full gc no: %u", invocations);
145 }
146 } else {
147 if (ScavengeALot) _scavenge_alot_counter--;
148 // Check if we should force a scavenge
149 if (_scavenge_alot_counter == 0) {
150 HandleMark hm(thread);
151 Universe::heap()->collect(GCCause::_scavenge_alot);
152 unsigned int invocations = Universe::heap()->total_collections() - Universe::heap()->total_full_collections();
153 // Compute new interval
154 if (ScavengeALotInterval > 1) {
155 _scavenge_alot_counter = 1+(long)((double)ScavengeALotInterval*os::random()/(max_jint+1.0));
156 log_trace(gc)("Scavenge no: %u\tInterval: %ld", invocations, _scavenge_alot_counter);
157 } else {
158 _scavenge_alot_counter = 1;
159 }
160 // Print progress message
161 if (invocations % 1000 == 0) {
162 log_trace(gc)("Scavenge no: %u", invocations);
163 }
164 }
165 }
166 }
167}
168
169
170vframe* vframe_array[50];
171int walk_stack_counter = 0;
172
173void InterfaceSupport::walk_stack_from(vframe* start_vf) {
174 // walk
175 int i = 0;
176 for (vframe* f = start_vf; f; f = f->sender() ) {
177 if (i < 50) vframe_array[i++] = f;
178 }
179}
180
181
182void InterfaceSupport::walk_stack() {
183 JavaThread* thread = JavaThread::current();
184 walk_stack_counter++;
185 if (!thread->has_last_Java_frame()) return;
186 ResourceMark rm(thread);
187 RegisterMap reg_map(thread);
188 walk_stack_from(thread->last_java_vframe(&reg_map));
189}
190
191// invocation counter for InterfaceSupport::deoptimizeAll/zombieAll functions
192int deoptimizeAllCounter = 0;
193int zombieAllCounter = 0;
194
195void InterfaceSupport::zombieAll() {
196 // This method is called by all threads when a thread make
197 // transition to VM state (for example, runtime calls).
198 // Divide number of calls by number of threads to avoid
199 // dependence of ZombieAll events frequency on number of threads.
200 int value = zombieAllCounter / Threads::number_of_threads();
201 if (is_init_completed() && value > ZombieALotInterval) {
202 zombieAllCounter = 0;
203 VM_ZombieAll op;
204 VMThread::execute(&op);
205 }
206 zombieAllCounter++;
207}
208
209void InterfaceSupport::deoptimizeAll() {
210 // This method is called by all threads when a thread make
211 // transition to VM state (for example, runtime calls).
212 // Divide number of calls by number of threads to avoid
213 // dependence of DeoptimizeAll events frequency on number of threads.
214 int value = deoptimizeAllCounter / Threads::number_of_threads();
215 if (is_init_completed()) {
216 if (DeoptimizeALot && value > DeoptimizeALotInterval) {
217 deoptimizeAllCounter = 0;
218 VM_DeoptimizeAll op;
219 VMThread::execute(&op);
220 } else if (DeoptimizeRandom && (value & 0x1F) == (os::random() & 0x1F)) {
221 VM_DeoptimizeAll op;
222 VMThread::execute(&op);
223 }
224 }
225 deoptimizeAllCounter++;
226}
227
228
229void InterfaceSupport::stress_derived_pointers() {
230#ifdef COMPILER2
231 JavaThread *thread = JavaThread::current();
232 if (!is_init_completed()) return;
233 ResourceMark rm(thread);
234 bool found = false;
235 for (StackFrameStream sfs(thread); !sfs.is_done() && !found; sfs.next()) {
236 CodeBlob* cb = sfs.current()->cb();
237 if (cb != NULL && cb->oop_maps() ) {
238 // Find oopmap for current method
239 const ImmutableOopMap* map = cb->oop_map_for_return_address(sfs.current()->pc());
240 assert(map != NULL, "no oopmap found for pc");
241 found = map->has_derived_pointer();
242 }
243 }
244 if (found) {
245 // $$$ Not sure what to do here.
246 /*
247 Scavenge::invoke(0);
248 */
249 }
250#endif
251}
252
253
254void InterfaceSupport::verify_stack() {
255 JavaThread* thread = JavaThread::current();
256 ResourceMark rm(thread);
257 // disabled because it throws warnings that oop maps should only be accessed
258 // in VM thread or during debugging
259
260 if (!thread->has_pending_exception()) {
261 // verification does not work if there are pending exceptions
262 StackFrameStream sfs(thread);
263 CodeBlob* cb = sfs.current()->cb();
264 // In case of exceptions we might not have a runtime_stub on
265 // top of stack, hence, all callee-saved registers are not going
266 // to be setup correctly, hence, we cannot do stack verify
267 if (cb != NULL && !(cb->is_runtime_stub() || cb->is_uncommon_trap_stub())) return;
268
269 for (; !sfs.is_done(); sfs.next()) {
270 sfs.current()->verify(sfs.register_map());
271 }
272 }
273}
274
275
276void InterfaceSupport::verify_last_frame() {
277 JavaThread* thread = JavaThread::current();
278 ResourceMark rm(thread);
279 RegisterMap reg_map(thread);
280 frame fr = thread->last_frame();
281 fr.verify(&reg_map);
282}
283
284
285#endif // ASSERT
286
287
288void InterfaceSupport_init() {
289#ifdef ASSERT
290 if (ScavengeALot || FullGCALot) {
291 srand(ScavengeALotInterval * FullGCALotInterval);
292 }
293#endif
294}
295
296#ifdef ASSERT
297// JRT_LEAF rules:
298// A JRT_LEAF method may not interfere with safepointing by
299// 1) acquiring or blocking on a Mutex or JavaLock - checked
300// 2) allocating heap memory - checked
301// 3) executing a VM operation - checked
302// 4) executing a system call (including malloc) that could block or grab a lock
303// 5) invoking GC
304// 6) reaching a safepoint
305// 7) running too long
306// Nor may any method it calls.
307JRTLeafVerifier::JRTLeafVerifier()
308 : NoSafepointVerifier(true, JRTLeafVerifier::should_verify_GC())
309{
310}
311
312JRTLeafVerifier::~JRTLeafVerifier()
313{
314}
315
316bool JRTLeafVerifier::should_verify_GC() {
317 switch (JavaThread::current()->thread_state()) {
318 case _thread_in_Java:
319 // is in a leaf routine, there must be no safepoint.
320 return true;
321 case _thread_in_native:
322 // A native thread is not subject to safepoints.
323 // Even while it is in a leaf routine, GC is ok
324 return false;
325 default:
326 // Leaf routines cannot be called from other contexts.
327 ShouldNotReachHere();
328 return false;
329 }
330}
331#endif // ASSERT
332