1 /*
2  * Copyright (c) 2020, 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 "logging/log.hpp"
27 #include "runtime/atomic.hpp"
28 #include "runtime/frame.inline.hpp"
29 #include "runtime/safepoint.hpp"
30 #include "runtime/stackWatermark.inline.hpp"
31 #include "runtime/thread.hpp"
32 #include "utilities/debug.hpp"
33 #include "utilities/globalDefinitions.hpp"
34 #include "utilities/macros.hpp"
35 #include "utilities/preserveException.hpp"
36 
37 class StackWatermarkFramesIterator : public CHeapObj<mtInternal> {
38   JavaThread* _jt;
39   uintptr_t _caller;
40   uintptr_t _callee;
41   StackFrameStream _frame_stream;
42   StackWatermark& _owner;
43   bool _is_done;
44 
45   void set_watermark(uintptr_t sp);
46   RegisterMap& register_map();
47   frame& current();
48   void next();
49 
50 public:
51   StackWatermarkFramesIterator(StackWatermark& owner);
caller() const52   uintptr_t caller() const { return _caller; }
callee() const53   uintptr_t callee() const { return _callee; }
54   void process_one(void* context);
55   void process_all(void* context);
56   bool has_next() const;
57 };
58 
set_watermark(uintptr_t sp)59 void StackWatermarkFramesIterator::set_watermark(uintptr_t sp) {
60   if (!has_next()) {
61     return;
62   }
63 
64   if (_callee == 0) {
65     _callee = sp;
66   } else if (_caller == 0) {
67     _caller = sp;
68   } else {
69     _callee = _caller;
70     _caller = sp;
71   }
72 }
73 
74 // This class encapsulates various marks we need to deal with calling the
75 // frame processing code from arbitrary points in the runtime. It is mostly
76 // due to problems that we might want to eventually clean up inside of the
77 // frame processing code, such as creating random handles even though there
78 // is no safepoint to protect against, and fiddling around with exceptions.
79 class StackWatermarkProcessingMark {
80   ResetNoHandleMark _rnhm;
81   HandleMark _hm;
82   PreserveExceptionMark _pem;
83   ResourceMark _rm;
84 
85 public:
StackWatermarkProcessingMark(Thread * thread)86   StackWatermarkProcessingMark(Thread* thread) :
87       _rnhm(),
88       _hm(thread),
89       _pem(thread),
90       _rm(thread) { }
91 };
92 
process_one(void * context)93 void StackWatermarkFramesIterator::process_one(void* context) {
94   StackWatermarkProcessingMark swpm(Thread::current());
95   while (has_next()) {
96     frame f = current();
97     uintptr_t sp = reinterpret_cast<uintptr_t>(f.sp());
98     bool frame_has_barrier = StackWatermark::has_barrier(f);
99     _owner.process(f, register_map(), context);
100     next();
101     if (frame_has_barrier) {
102       set_watermark(sp);
103       break;
104     }
105   }
106 }
107 
process_all(void * context)108 void StackWatermarkFramesIterator::process_all(void* context) {
109   const uintptr_t frames_per_poll_gc = 5;
110 
111   ResourceMark rm;
112   log_info(stackbarrier)("Processing whole stack for tid %d",
113                          _jt->osthread()->thread_id());
114   uint i = 0;
115   while (has_next()) {
116     frame f = current();
117     uintptr_t sp = reinterpret_cast<uintptr_t>(f.sp());
118     assert(sp >= _caller, "invariant");
119     bool frame_has_barrier = StackWatermark::has_barrier(f);
120     _owner.process(f, register_map(), context);
121     next();
122     if (frame_has_barrier) {
123       set_watermark(sp);
124       if (++i == frames_per_poll_gc) {
125         // Yield every N frames so mutator can progress faster.
126         i = 0;
127         _owner.yield_processing();
128       }
129     }
130   }
131 }
132 
StackWatermarkFramesIterator(StackWatermark & owner)133 StackWatermarkFramesIterator::StackWatermarkFramesIterator(StackWatermark& owner) :
134     _jt(owner._jt),
135     _caller(0),
136     _callee(0),
137     _frame_stream(owner._jt, true /* update_registers */, false /* process_frames */),
138     _owner(owner),
139     _is_done(_frame_stream.is_done()) {
140 }
141 
current()142 frame& StackWatermarkFramesIterator::current() {
143   return *_frame_stream.current();
144 }
145 
register_map()146 RegisterMap& StackWatermarkFramesIterator::register_map() {
147   return *_frame_stream.register_map();
148 }
149 
has_next() const150 bool StackWatermarkFramesIterator::has_next() const {
151   return !_is_done;
152 }
153 
next()154 void StackWatermarkFramesIterator::next() {
155   _frame_stream.next();
156   _is_done = _frame_stream.is_done();
157 }
158 
StackWatermark(JavaThread * jt,StackWatermarkKind kind,uint32_t epoch)159 StackWatermark::StackWatermark(JavaThread* jt, StackWatermarkKind kind, uint32_t epoch) :
160     _state(StackWatermarkState::create(epoch, true /* is_done */)),
161     _watermark(0),
162     _next(NULL),
163     _jt(jt),
164     _iterator(NULL),
165     _lock(Mutex::tty - 1, "stack_watermark_lock", true, Mutex::_safepoint_check_never),
166     _kind(kind),
167     _linked_watermark(NULL) {
168 }
169 
~StackWatermark()170 StackWatermark::~StackWatermark() {
171   delete _iterator;
172 }
173 
174 #ifdef ASSERT
assert_is_frame_safe(const frame & f)175 void StackWatermark::assert_is_frame_safe(const frame& f) {
176   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
177   assert(is_frame_safe(f), "Frame must be safe");
178 }
179 #endif
180 
181 // A frame is "safe" if it *and* its caller have been processed. This is the invariant
182 // that allows exposing a frame, and for that frame to directly access its caller frame
183 // without going through any hooks.
is_frame_safe(const frame & f)184 bool StackWatermark::is_frame_safe(const frame& f) {
185   assert(_lock.owned_by_self(), "Must be locked");
186   uint32_t state = Atomic::load(&_state);
187   if (!processing_started(state)) {
188     return false;
189   }
190   if (processing_completed(state)) {
191     return true;
192   }
193   return reinterpret_cast<uintptr_t>(f.sp()) < _iterator->caller();
194 }
195 
start_processing_impl(void * context)196 void StackWatermark::start_processing_impl(void* context) {
197   log_info(stackbarrier)("Starting stack processing for tid %d",
198                          _jt->osthread()->thread_id());
199   delete _iterator;
200   if (_jt->has_last_Java_frame()) {
201     _iterator = new StackWatermarkFramesIterator(*this);
202     // Always process three frames when starting an iteration.
203     //
204     // The three frames corresponds to:
205     // 1) The callee frame
206     // 2) The caller frame
207     // This allows a callee to always be able to read state from its caller
208     // without needing any special barriers.
209     //
210     // 3) An extra frame to deal with unwinding safepointing on the way out.
211     // Sometimes, we also call into the runtime to on_unwind(), but then
212     // hit a safepoint poll on the way out from the runtime.
213     _iterator->process_one(context);
214     _iterator->process_one(context);
215     _iterator->process_one(context);
216   } else {
217     _iterator = NULL;
218   }
219   update_watermark();
220 }
221 
yield_processing()222 void StackWatermark::yield_processing() {
223   update_watermark();
224   MutexUnlocker mul(&_lock, Mutex::_no_safepoint_check_flag);
225 }
226 
update_watermark()227 void StackWatermark::update_watermark() {
228   assert(_lock.owned_by_self(), "invariant");
229   if (_iterator != NULL && _iterator->has_next()) {
230     assert(_iterator->callee() != 0, "sanity");
231     Atomic::release_store(&_watermark, _iterator->callee());
232     Atomic::release_store(&_state, StackWatermarkState::create(epoch_id(), false /* is_done */)); // release watermark w.r.t. epoch
233   } else {
234     Atomic::release_store(&_watermark, uintptr_t(0)); // Release stack data modifications w.r.t. watermark
235     Atomic::release_store(&_state, StackWatermarkState::create(epoch_id(), true /* is_done */)); // release watermark w.r.t. epoch
236     log_info(stackbarrier)("Finished stack processing iteration for tid %d",
237                            _jt->osthread()->thread_id());
238   }
239 }
240 
process_one()241 void StackWatermark::process_one() {
242   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
243   if (!processing_started()) {
244     start_processing_impl(NULL /* context */);
245   } else if (!processing_completed()) {
246     _iterator->process_one(NULL /* context */);
247     update_watermark();
248   }
249 }
250 
link_watermark(StackWatermark * watermark)251 void StackWatermark::link_watermark(StackWatermark* watermark) {
252   assert(watermark == NULL || _linked_watermark == NULL, "nesting not supported");
253   _linked_watermark = watermark;
254 }
255 
watermark()256 uintptr_t StackWatermark::watermark() {
257   return Atomic::load_acquire(&_watermark);
258 }
259 
last_processed()260 uintptr_t StackWatermark::last_processed() {
261   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
262   if (!processing_started()) {
263     // Stale state; no last processed
264     return 0;
265   }
266   if (processing_completed()) {
267     // Already processed all; no last processed
268     return 0;
269   }
270   return _iterator->caller();
271 }
272 
processing_started() const273 bool StackWatermark::processing_started() const {
274   return processing_started(Atomic::load(&_state));
275 }
276 
processing_started_acquire() const277 bool StackWatermark::processing_started_acquire() const {
278   return processing_started(Atomic::load_acquire(&_state));
279 }
280 
processing_completed() const281 bool StackWatermark::processing_completed() const {
282   return processing_completed(Atomic::load(&_state));
283 }
284 
processing_completed_acquire() const285 bool StackWatermark::processing_completed_acquire() const {
286   return processing_completed(Atomic::load_acquire(&_state));
287 }
288 
on_safepoint()289 void StackWatermark::on_safepoint() {
290   start_processing();
291   StackWatermark* linked_watermark = _linked_watermark;
292   if (linked_watermark != NULL) {
293     linked_watermark->finish_processing(NULL /* context */);
294   }
295 }
296 
start_processing()297 void StackWatermark::start_processing() {
298   if (!processing_started_acquire()) {
299     MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
300     if (!processing_started()) {
301       start_processing_impl(NULL /* context */);
302     }
303   }
304 }
305 
finish_processing(void * context)306 void StackWatermark::finish_processing(void* context) {
307   MutexLocker ml(&_lock, Mutex::_no_safepoint_check_flag);
308   if (!processing_started()) {
309     start_processing_impl(context);
310   }
311   if (!processing_completed()) {
312     _iterator->process_all(context);
313     update_watermark();
314   }
315 }
316