21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "code/codeCache.hpp"
27 #include "code/compiledIC.hpp"
28 #include "code/icBuffer.hpp"
29 #include "code/nmethod.hpp"
30 #include "compiler/compileBroker.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/method.hpp"
33 #include "runtime/atomic.hpp"
34 #include "runtime/compilationPolicy.hpp"
35 #include "runtime/mutexLocker.hpp"
36 #include "runtime/os.hpp"
37 #include "runtime/sweeper.hpp"
38 #include "runtime/vm_operations.hpp"
39 #include "trace/tracing.hpp"
40 #include "utilities/events.hpp"
41 #include "utilities/xmlstream.hpp"
42
43 #ifdef ASSERT
44
45 #define SWEEP(nm) record_sweep(nm, __LINE__)
46 // Sweeper logging code
47 class SweeperRecord {
48 public:
49 int traversal;
50 int invocation;
51 int compile_id;
52 long traversal_mark;
53 int state;
54 const char* kind;
55 address vep;
56 address uep;
57 int line;
58
59 void print() {
60 tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
125 }
126 #else
127 #define SWEEP(nm)
128 #endif
129
130 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
131 long NMethodSweeper::_traversals = 0; // Nof. stack traversals performed
132 int NMethodSweeper::_seen = 0; // Nof. nmethods we have currently processed in current pass of CodeCache
133 int NMethodSweeper::_flushed_count = 0; // Nof. nmethods flushed in current sweep
134 int NMethodSweeper::_zombified_count = 0; // Nof. nmethods made zombie in current sweep
135 int NMethodSweeper::_marked_count = 0; // Nof. nmethods marked for reclaim in current sweep
136
137 volatile int NMethodSweeper::_invocations = 0; // Nof. invocations left until we are completed with this pass
138 volatile int NMethodSweeper::_sweep_started = 0; // Whether a sweep is in progress.
139
140 jint NMethodSweeper::_locked_seen = 0;
141 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
142 bool NMethodSweeper::_request_mark_phase = false;
143
144 int NMethodSweeper::_total_nof_methods_reclaimed = 0;
145 jlong NMethodSweeper::_total_time_sweeping = 0;
146 jlong NMethodSweeper::_total_time_this_sweep = 0;
147 jlong NMethodSweeper::_peak_sweep_time = 0;
148 jlong NMethodSweeper::_peak_sweep_fraction_time = 0;
149 int NMethodSweeper::_hotness_counter_reset_val = 0;
150
151
152 class MarkActivationClosure: public CodeBlobClosure {
153 public:
154 virtual void do_code_blob(CodeBlob* cb) {
155 if (cb->is_nmethod()) {
156 nmethod* nm = (nmethod*)cb;
157 nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
158 // If we see an activation belonging to a non_entrant nmethod, we mark it.
159 if (nm->is_not_entrant()) {
160 nm->mark_as_seen_on_stack();
161 }
162 }
163 }
164 };
165 static MarkActivationClosure mark_activation_closure;
166
167 class SetHotnessClosure: public CodeBlobClosure {
168 public:
187 }
188
189 // Scans the stacks of all Java threads and marks activations of not-entrant methods.
190 // No need to synchronize access, since 'mark_active_nmethods' is always executed at a
191 // safepoint.
192 void NMethodSweeper::mark_active_nmethods() {
193 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
194 // If we do not want to reclaim not-entrant or zombie methods there is no need
195 // to scan stacks
196 if (!MethodFlushing) {
197 return;
198 }
199
200 // Check for restart
201 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
202 if (!sweep_in_progress() && need_marking_phase()) {
203 _seen = 0;
204 _invocations = NmethodSweepFraction;
205 _current = CodeCache::first_nmethod();
206 _traversals += 1;
207 _total_time_this_sweep = 0;
208
209 if (PrintMethodFlushing) {
210 tty->print_cr("### Sweep: stack traversal %d", _traversals);
211 }
212 Threads::nmethods_do(&mark_activation_closure);
213
214 // reset the flags since we started a scan from the beginning.
215 reset_nmethod_marking();
216 _locked_seen = 0;
217 _not_entrant_seen_on_stack = 0;
218 } else {
219 // Only set hotness counter
220 Threads::nmethods_do(&set_hotness_closure);
221 }
222
223 OrderAccess::storestore();
224 }
225
226 void NMethodSweeper::possibly_sweep() {
227 assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
235 if (old != 0) {
236 return;
237 }
238 #ifdef ASSERT
239 if (LogSweeper && _records == NULL) {
240 // Create the ring buffer for the logging code
241 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
242 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
243 }
244 #endif
245 if (_invocations > 0) {
246 sweep_code_cache();
247 _invocations--;
248 }
249 _sweep_started = 0;
250 }
251 }
252
253 void NMethodSweeper::sweep_code_cache() {
254
255 jlong sweep_start_counter = os::elapsed_counter();
256
257 _flushed_count = 0;
258 _zombified_count = 0;
259 _marked_count = 0;
260
261 if (PrintMethodFlushing && Verbose) {
262 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
263 }
264
265 if (!CompileBroker::should_compile_new_jobs()) {
266 // If we have turned off compilations we might as well do full sweeps
267 // in order to reach the clean state faster. Otherwise the sleeping compiler
268 // threads will slow down sweeping.
269 _invocations = 1;
270 }
271
272 // We want to visit all nmethods after NmethodSweepFraction
273 // invocations so divide the remaining number of nmethods by the
274 // remaining number of invocations. This is only an estimate since
275 // the number of nmethods changes during the sweep so the final
311 }
312 _seen++;
313 _current = next;
314 }
315 }
316
317 assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache");
318
319 if (!sweep_in_progress() && !need_marking_phase() && (_locked_seen || _not_entrant_seen_on_stack)) {
320 // we've completed a scan without making progress but there were
321 // nmethods we were unable to process either because they were
322 // locked or were still on stack. We don't have to aggressively
323 // clean them up so just stop scanning. We could scan once more
324 // but that complicates the control logic and it's unlikely to
325 // matter much.
326 if (PrintMethodFlushing) {
327 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
328 }
329 }
330
331 jlong sweep_end_counter = os::elapsed_counter();
332 jlong sweep_time = sweep_end_counter - sweep_start_counter;
333 _total_time_sweeping += sweep_time;
334 _total_time_this_sweep += sweep_time;
335 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
336 _total_nof_methods_reclaimed += _flushed_count;
337
338 EventSweepCodeCache event(UNTIMED);
339 if (event.should_commit()) {
340 event.set_starttime(sweep_start_counter);
341 event.set_endtime(sweep_end_counter);
342 event.set_sweepIndex(_traversals);
343 event.set_sweepFractionIndex(NmethodSweepFraction - _invocations + 1);
344 event.set_sweptCount(swept_count);
345 event.set_flushedCount(_flushed_count);
346 event.set_markedCount(_marked_count);
347 event.set_zombifiedCount(_zombified_count);
348 event.commit();
349 }
350
351 #ifdef ASSERT
352 if(PrintMethodFlushing) {
353 tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, (jlong)sweep_time);
354 }
355 #endif
356
357 if (_invocations == 1) {
358 _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
359 log_sweep("finished");
360 }
361
362 // Sweeper is the only case where memory is released, check here if it
363 // is time to restart the compiler. Only checking if there is a certain
364 // amount of free memory in the code cache might lead to re-enabling
365 // compilation although no memory has been released. For example, there are
366 // cases when compilation was disabled although there is 4MB (or more) free
367 // memory in the code cache. The reason is code cache fragmentation. Therefore,
368 // it only makes sense to re-enable compilation if we have actually freed memory.
369 // Note that typically several kB are released for sweeping 16MB of the code
370 // cache. As a result, 'freed_memory' > 0 to restart the compiler.
371 if (UseCodeCacheFlushing && (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0))) {
372 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
373 log_sweep("restart_compiler");
|
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "code/codeCache.hpp"
27 #include "code/compiledIC.hpp"
28 #include "code/icBuffer.hpp"
29 #include "code/nmethod.hpp"
30 #include "compiler/compileBroker.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/method.hpp"
33 #include "runtime/atomic.hpp"
34 #include "runtime/compilationPolicy.hpp"
35 #include "runtime/mutexLocker.hpp"
36 #include "runtime/os.hpp"
37 #include "runtime/sweeper.hpp"
38 #include "runtime/vm_operations.hpp"
39 #include "trace/tracing.hpp"
40 #include "utilities/events.hpp"
41 #include "utilities/ticks.inline.hpp"
42 #include "utilities/xmlstream.hpp"
43
44 #ifdef ASSERT
45
46 #define SWEEP(nm) record_sweep(nm, __LINE__)
47 // Sweeper logging code
48 class SweeperRecord {
49 public:
50 int traversal;
51 int invocation;
52 int compile_id;
53 long traversal_mark;
54 int state;
55 const char* kind;
56 address vep;
57 address uep;
58 int line;
59
60 void print() {
61 tty->print_cr("traversal = %d invocation = %d compile_id = %d %s uep = " PTR_FORMAT " vep = "
126 }
127 #else
128 #define SWEEP(nm)
129 #endif
130
131 nmethod* NMethodSweeper::_current = NULL; // Current nmethod
132 long NMethodSweeper::_traversals = 0; // Nof. stack traversals performed
133 int NMethodSweeper::_seen = 0; // Nof. nmethods we have currently processed in current pass of CodeCache
134 int NMethodSweeper::_flushed_count = 0; // Nof. nmethods flushed in current sweep
135 int NMethodSweeper::_zombified_count = 0; // Nof. nmethods made zombie in current sweep
136 int NMethodSweeper::_marked_count = 0; // Nof. nmethods marked for reclaim in current sweep
137
138 volatile int NMethodSweeper::_invocations = 0; // Nof. invocations left until we are completed with this pass
139 volatile int NMethodSweeper::_sweep_started = 0; // Whether a sweep is in progress.
140
141 jint NMethodSweeper::_locked_seen = 0;
142 jint NMethodSweeper::_not_entrant_seen_on_stack = 0;
143 bool NMethodSweeper::_request_mark_phase = false;
144
145 int NMethodSweeper::_total_nof_methods_reclaimed = 0;
146 Tickspan NMethodSweeper::_total_time_sweeping;
147 Tickspan NMethodSweeper::_total_time_this_sweep;
148 Tickspan NMethodSweeper::_peak_sweep_time;
149 Tickspan NMethodSweeper::_peak_sweep_fraction_time;
150 int NMethodSweeper::_hotness_counter_reset_val = 0;
151
152
153 class MarkActivationClosure: public CodeBlobClosure {
154 public:
155 virtual void do_code_blob(CodeBlob* cb) {
156 if (cb->is_nmethod()) {
157 nmethod* nm = (nmethod*)cb;
158 nm->set_hotness_counter(NMethodSweeper::hotness_counter_reset_val());
159 // If we see an activation belonging to a non_entrant nmethod, we mark it.
160 if (nm->is_not_entrant()) {
161 nm->mark_as_seen_on_stack();
162 }
163 }
164 }
165 };
166 static MarkActivationClosure mark_activation_closure;
167
168 class SetHotnessClosure: public CodeBlobClosure {
169 public:
188 }
189
190 // Scans the stacks of all Java threads and marks activations of not-entrant methods.
191 // No need to synchronize access, since 'mark_active_nmethods' is always executed at a
192 // safepoint.
193 void NMethodSweeper::mark_active_nmethods() {
194 assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
195 // If we do not want to reclaim not-entrant or zombie methods there is no need
196 // to scan stacks
197 if (!MethodFlushing) {
198 return;
199 }
200
201 // Check for restart
202 assert(CodeCache::find_blob_unsafe(_current) == _current, "Sweeper nmethod cached state invalid");
203 if (!sweep_in_progress() && need_marking_phase()) {
204 _seen = 0;
205 _invocations = NmethodSweepFraction;
206 _current = CodeCache::first_nmethod();
207 _traversals += 1;
208 _total_time_this_sweep = Tickspan();
209
210 if (PrintMethodFlushing) {
211 tty->print_cr("### Sweep: stack traversal %d", _traversals);
212 }
213 Threads::nmethods_do(&mark_activation_closure);
214
215 // reset the flags since we started a scan from the beginning.
216 reset_nmethod_marking();
217 _locked_seen = 0;
218 _not_entrant_seen_on_stack = 0;
219 } else {
220 // Only set hotness counter
221 Threads::nmethods_do(&set_hotness_closure);
222 }
223
224 OrderAccess::storestore();
225 }
226
227 void NMethodSweeper::possibly_sweep() {
228 assert(JavaThread::current()->thread_state() == _thread_in_vm, "must run in vm mode");
236 if (old != 0) {
237 return;
238 }
239 #ifdef ASSERT
240 if (LogSweeper && _records == NULL) {
241 // Create the ring buffer for the logging code
242 _records = NEW_C_HEAP_ARRAY(SweeperRecord, SweeperLogEntries, mtGC);
243 memset(_records, 0, sizeof(SweeperRecord) * SweeperLogEntries);
244 }
245 #endif
246 if (_invocations > 0) {
247 sweep_code_cache();
248 _invocations--;
249 }
250 _sweep_started = 0;
251 }
252 }
253
254 void NMethodSweeper::sweep_code_cache() {
255
256 const Ticks sweep_start_counter = Ticks::now();
257
258 _flushed_count = 0;
259 _zombified_count = 0;
260 _marked_count = 0;
261
262 if (PrintMethodFlushing && Verbose) {
263 tty->print_cr("### Sweep at %d out of %d. Invocations left: %d", _seen, CodeCache::nof_nmethods(), _invocations);
264 }
265
266 if (!CompileBroker::should_compile_new_jobs()) {
267 // If we have turned off compilations we might as well do full sweeps
268 // in order to reach the clean state faster. Otherwise the sleeping compiler
269 // threads will slow down sweeping.
270 _invocations = 1;
271 }
272
273 // We want to visit all nmethods after NmethodSweepFraction
274 // invocations so divide the remaining number of nmethods by the
275 // remaining number of invocations. This is only an estimate since
276 // the number of nmethods changes during the sweep so the final
312 }
313 _seen++;
314 _current = next;
315 }
316 }
317
318 assert(_invocations > 1 || _current == NULL, "must have scanned the whole cache");
319
320 if (!sweep_in_progress() && !need_marking_phase() && (_locked_seen || _not_entrant_seen_on_stack)) {
321 // we've completed a scan without making progress but there were
322 // nmethods we were unable to process either because they were
323 // locked or were still on stack. We don't have to aggressively
324 // clean them up so just stop scanning. We could scan once more
325 // but that complicates the control logic and it's unlikely to
326 // matter much.
327 if (PrintMethodFlushing) {
328 tty->print_cr("### Couldn't make progress on some nmethods so stopping sweep");
329 }
330 }
331
332 const Ticks sweep_end_counter = Ticks::now();
333 const Tickspan sweep_time = sweep_end_counter - sweep_start_counter;
334 _total_time_sweeping += sweep_time;
335 _total_time_this_sweep += sweep_time;
336 _peak_sweep_fraction_time = MAX2(sweep_time, _peak_sweep_fraction_time);
337 _total_nof_methods_reclaimed += _flushed_count;
338
339 EventSweepCodeCache event(UNTIMED);
340 if (event.should_commit()) {
341 event.set_starttime(sweep_start_counter);
342 event.set_endtime(sweep_end_counter);
343 event.set_sweepIndex(_traversals);
344 event.set_sweepFractionIndex(NmethodSweepFraction - _invocations + 1);
345 event.set_sweptCount(swept_count);
346 event.set_flushedCount(_flushed_count);
347 event.set_markedCount(_marked_count);
348 event.set_zombifiedCount(_zombified_count);
349 event.commit();
350 }
351
352 #ifdef ASSERT
353 if(PrintMethodFlushing) {
354 tty->print_cr("### sweeper: sweep time(%d): " INT64_FORMAT, _invocations, sweep_time.value());
355 }
356 #endif
357
358 if (_invocations == 1) {
359 _peak_sweep_time = MAX2(_peak_sweep_time, _total_time_this_sweep);
360 log_sweep("finished");
361 }
362
363 // Sweeper is the only case where memory is released, check here if it
364 // is time to restart the compiler. Only checking if there is a certain
365 // amount of free memory in the code cache might lead to re-enabling
366 // compilation although no memory has been released. For example, there are
367 // cases when compilation was disabled although there is 4MB (or more) free
368 // memory in the code cache. The reason is code cache fragmentation. Therefore,
369 // it only makes sense to re-enable compilation if we have actually freed memory.
370 // Note that typically several kB are released for sweeping 16MB of the code
371 // cache. As a result, 'freed_memory' > 0 to restart the compiler.
372 if (UseCodeCacheFlushing && (!CompileBroker::should_compile_new_jobs() && (freed_memory > 0))) {
373 CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
374 log_sweep("restart_compiler");
|