comparison src/share/vm/runtime/compilationPolicy.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children ddb7834449d0
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 2000-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 # include "incls/_precompiled.incl"
26 # include "incls/_compilationPolicy.cpp.incl"
27
28 CompilationPolicy* CompilationPolicy::_policy;
29 elapsedTimer CompilationPolicy::_accumulated_time;
30 bool CompilationPolicy::_in_vm_startup;
31
32 // Determine compilation policy based on command line argument
33 void compilationPolicy_init() {
34 CompilationPolicy::set_in_vm_startup(DelayCompilationDuringStartup);
35
36 switch(CompilationPolicyChoice) {
37 case 0:
38 CompilationPolicy::set_policy(new SimpleCompPolicy());
39 break;
40
41 case 1:
42 #ifdef COMPILER2
43 CompilationPolicy::set_policy(new StackWalkCompPolicy());
44 #else
45 Unimplemented();
46 #endif
47 break;
48
49 default:
50 fatal("CompilationPolicyChoice must be in the range: [0-1]");
51 }
52 }
53
54 void CompilationPolicy::completed_vm_startup() {
55 if (TraceCompilationPolicy) {
56 tty->print("CompilationPolicy: completed vm startup.\n");
57 }
58 _in_vm_startup = false;
59 }
60
61 // Returns true if m must be compiled before executing it
62 // This is intended to force compiles for methods (usually for
63 // debugging) that would otherwise be interpreted for some reason.
64 bool CompilationPolicy::mustBeCompiled(methodHandle m) {
65 if (m->has_compiled_code()) return false; // already compiled
66 if (!canBeCompiled(m)) return false;
67
68 return !UseInterpreter || // must compile all methods
69 (UseCompiler && AlwaysCompileLoopMethods && m->has_loops()); // eagerly compile loop methods
70 }
71
72 // Returns true if m is allowed to be compiled
73 bool CompilationPolicy::canBeCompiled(methodHandle m) {
74 if (m->is_abstract()) return false;
75 if (DontCompileHugeMethods && m->code_size() > HugeMethodLimit) return false;
76
77 return !m->is_not_compilable();
78 }
79
80 #ifndef PRODUCT
81 void CompilationPolicy::print_time() {
82 tty->print_cr ("Accumulated compilationPolicy times:");
83 tty->print_cr ("---------------------------");
84 tty->print_cr (" Total: %3.3f sec.", _accumulated_time.seconds());
85 }
86
87 static void trace_osr_completion(nmethod* osr_nm) {
88 if (TraceOnStackReplacement) {
89 if (osr_nm == NULL) tty->print_cr("compilation failed");
90 else tty->print_cr("nmethod " INTPTR_FORMAT, osr_nm);
91 }
92 }
93 #endif // !PRODUCT
94
95 void CompilationPolicy::reset_counter_for_invocation_event(methodHandle m) {
96 // Make sure invocation and backedge counter doesn't overflow again right away
97 // as would be the case for native methods.
98
99 // BUT also make sure the method doesn't look like it was never executed.
100 // Set carry bit and reduce counter's value to min(count, CompileThreshold/2).
101 m->invocation_counter()->set_carry();
102 m->backedge_counter()->set_carry();
103
104 assert(!m->was_never_executed(), "don't reset to 0 -- could be mistaken for never-executed");
105 }
106
107 void CompilationPolicy::reset_counter_for_back_branch_event(methodHandle m) {
108 // Delay next back-branch event but pump up invocation counter to triger
109 // whole method compilation.
110 InvocationCounter* i = m->invocation_counter();
111 InvocationCounter* b = m->backedge_counter();
112
113 // Don't set invocation_counter's value too low otherwise the method will
114 // look like immature (ic < ~5300) which prevents the inlining based on
115 // the type profiling.
116 i->set(i->state(), CompileThreshold);
117 // Don't reset counter too low - it is used to check if OSR method is ready.
118 b->set(b->state(), CompileThreshold / 2);
119 }
120
121 // SimpleCompPolicy - compile current method
122
123 void SimpleCompPolicy::method_invocation_event( methodHandle m, TRAPS) {
124 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
125
126 int hot_count = m->invocation_count();
127 reset_counter_for_invocation_event(m);
128 const char* comment = "count";
129
130 if (!delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) {
131 nmethod* nm = m->code();
132 if (nm == NULL ) {
133 const char* comment = "count";
134 CompileBroker::compile_method(m, InvocationEntryBci,
135 m, hot_count, comment, CHECK);
136 } else {
137 #ifdef TIERED
138
139 if (nm->is_compiled_by_c1()) {
140 const char* comment = "tier1 overflow";
141 CompileBroker::compile_method(m, InvocationEntryBci,
142 m, hot_count, comment, CHECK);
143 }
144 #endif // TIERED
145 }
146 }
147 }
148
149 void SimpleCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
150 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
151
152 int hot_count = m->backedge_count();
153 const char* comment = "backedge_count";
154
155 if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) {
156 CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
157
158 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
159 }
160 }
161
162 int SimpleCompPolicy::compilation_level(methodHandle m, int branch_bci)
163 {
164 #ifdef TIERED
165 if (!TieredCompilation) {
166 return CompLevel_highest_tier;
167 }
168 if (/* m()->tier1_compile_done() && */
169 // QQQ HACK FIX ME set tier1_compile_done!!
170 !m()->is_native()) {
171 // Grab the nmethod so it doesn't go away while it's being queried
172 nmethod* code = m()->code();
173 if (code != NULL && code->is_compiled_by_c1()) {
174 return CompLevel_highest_tier;
175 }
176 }
177 return CompLevel_fast_compile;
178 #else
179 return CompLevel_highest_tier;
180 #endif // TIERED
181 }
182
183 // StackWalkCompPolicy - walk up stack to find a suitable method to compile
184
185 #ifdef COMPILER2
186 const char* StackWalkCompPolicy::_msg = NULL;
187
188
189 // Consider m for compilation
190 void StackWalkCompPolicy::method_invocation_event(methodHandle m, TRAPS) {
191 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
192
193 int hot_count = m->invocation_count();
194 reset_counter_for_invocation_event(m);
195 const char* comment = "count";
196
197 if (m->code() == NULL && !delayCompilationDuringStartup() && canBeCompiled(m) && UseCompiler) {
198 ResourceMark rm(THREAD);
199 JavaThread *thread = (JavaThread*)THREAD;
200 frame fr = thread->last_frame();
201 assert(fr.is_interpreted_frame(), "must be interpreted");
202 assert(fr.interpreter_frame_method() == m(), "bad method");
203
204 if (TraceCompilationPolicy) {
205 tty->print("method invocation trigger: ");
206 m->print_short_name(tty);
207 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)m(), m->code_size());
208 }
209 RegisterMap reg_map(thread, false);
210 javaVFrame* triggerVF = thread->last_java_vframe(&reg_map);
211 // triggerVF is the frame that triggered its counter
212 RFrame* first = new InterpretedRFrame(triggerVF->fr(), thread, m);
213
214 if (first->top_method()->code() != NULL) {
215 // called obsolete method/nmethod -- no need to recompile
216 if (TraceCompilationPolicy) tty->print_cr(" --> " INTPTR_FORMAT, first->top_method()->code());
217 } else if (compilation_level(m, InvocationEntryBci) == CompLevel_fast_compile) {
218 // Tier1 compilation policy avaoids stack walking.
219 CompileBroker::compile_method(m, InvocationEntryBci,
220 m, hot_count, comment, CHECK);
221 } else {
222 if (TimeCompilationPolicy) accumulated_time()->start();
223 GrowableArray<RFrame*>* stack = new GrowableArray<RFrame*>(50);
224 stack->push(first);
225 RFrame* top = findTopInlinableFrame(stack);
226 if (TimeCompilationPolicy) accumulated_time()->stop();
227 assert(top != NULL, "findTopInlinableFrame returned null");
228 if (TraceCompilationPolicy) top->print();
229 CompileBroker::compile_method(top->top_method(), InvocationEntryBci,
230 m, hot_count, comment, CHECK);
231 }
232 }
233 }
234
235 void StackWalkCompPolicy::method_back_branch_event(methodHandle m, int branch_bci, int loop_top_bci, TRAPS) {
236 assert(UseCompiler || CompileTheWorld, "UseCompiler should be set by now.");
237
238 int hot_count = m->backedge_count();
239 const char* comment = "backedge_count";
240
241 if (!m->is_not_osr_compilable() && !delayCompilationDuringStartup() && canBeCompiled(m)) {
242 CompileBroker::compile_method(m, loop_top_bci, m, hot_count, comment, CHECK);
243
244 NOT_PRODUCT(trace_osr_completion(m->lookup_osr_nmethod_for(loop_top_bci));)
245 }
246 }
247
248 int StackWalkCompPolicy::compilation_level(methodHandle m, int osr_bci)
249 {
250 int comp_level = CompLevel_full_optimization;
251 if (TieredCompilation && osr_bci == InvocationEntryBci) {
252 if (CompileTheWorld) {
253 // Under CTW, the first compile is tier1, the second tier2
254 if (m->highest_tier_compile() == CompLevel_none) {
255 comp_level = CompLevel_fast_compile;
256 }
257 } else if (!m->has_osr_nmethod()) {
258 // Before tier1 is done, use invocation_count + backedge_count to
259 // compare against the threshold. After that, the counters may/will
260 // be reset, so rely on the straight interpreter_invocation_count.
261 if (m->highest_tier_compile() == CompLevel_initial_compile) {
262 if (m->interpreter_invocation_count() < Tier2CompileThreshold) {
263 comp_level = CompLevel_fast_compile;
264 }
265 } else if (m->invocation_count() + m->backedge_count() <
266 Tier2CompileThreshold) {
267 comp_level = CompLevel_fast_compile;
268 }
269 }
270
271 }
272 return comp_level;
273 }
274
275
276 RFrame* StackWalkCompPolicy::findTopInlinableFrame(GrowableArray<RFrame*>* stack) {
277 // go up the stack until finding a frame that (probably) won't be inlined
278 // into its caller
279 RFrame* current = stack->at(0); // current choice for stopping
280 assert( current && !current->is_compiled(), "" );
281 const char* msg = NULL;
282
283 while (1) {
284
285 // before going up the stack further, check if doing so would get us into
286 // compiled code
287 RFrame* next = senderOf(current, stack);
288 if( !next ) // No next frame up the stack?
289 break; // Then compile with current frame
290
291 methodHandle m = current->top_method();
292 methodHandle next_m = next->top_method();
293
294 if (TraceCompilationPolicy && Verbose) {
295 tty->print("[caller: ");
296 next_m->print_short_name(tty);
297 tty->print("] ");
298 }
299
300 if( !Inline ) { // Inlining turned off
301 msg = "Inlining turned off";
302 break;
303 }
304 if (next_m->is_not_compilable()) { // Did fail to compile this before/
305 msg = "caller not compilable";
306 break;
307 }
308 if (next->num() > MaxRecompilationSearchLength) {
309 // don't go up too high when searching for recompilees
310 msg = "don't go up any further: > MaxRecompilationSearchLength";
311 break;
312 }
313 if (next->distance() > MaxInterpretedSearchLength) {
314 // don't go up too high when searching for recompilees
315 msg = "don't go up any further: next > MaxInterpretedSearchLength";
316 break;
317 }
318 // Compiled frame above already decided not to inline;
319 // do not recompile him.
320 if (next->is_compiled()) {
321 msg = "not going up into optimized code";
322 break;
323 }
324
325 // Interpreted frame above us was already compiled. Do not force
326 // a recompile, although if the frame above us runs long enough an
327 // OSR might still happen.
328 if( current->is_interpreted() && next_m->has_compiled_code() ) {
329 msg = "not going up -- already compiled caller";
330 break;
331 }
332
333 // Compute how frequent this call site is. We have current method 'm'.
334 // We know next method 'next_m' is interpreted. Find the call site and
335 // check the various invocation counts.
336 int invcnt = 0; // Caller counts
337 if (ProfileInterpreter) {
338 invcnt = next_m->interpreter_invocation_count();
339 }
340 int cnt = 0; // Call site counts
341 if (ProfileInterpreter && next_m->method_data() != NULL) {
342 ResourceMark rm;
343 int bci = next->top_vframe()->bci();
344 ProfileData* data = next_m->method_data()->bci_to_data(bci);
345 if (data != NULL && data->is_CounterData())
346 cnt = data->as_CounterData()->count();
347 }
348
349 // Caller counts / call-site counts; i.e. is this call site
350 // a hot call site for method next_m?
351 int freq = (invcnt) ? cnt/invcnt : cnt;
352
353 // Check size and frequency limits
354 if ((msg = shouldInline(m, freq, cnt)) != NULL) {
355 break;
356 }
357 // Check inlining negative tests
358 if ((msg = shouldNotInline(m)) != NULL) {
359 break;
360 }
361
362
363 // If the caller method is too big or something then we do not want to
364 // compile it just to inline a method
365 if (!canBeCompiled(next_m)) {
366 msg = "caller cannot be compiled";
367 break;
368 }
369
370 if( next_m->name() == vmSymbols::class_initializer_name() ) {
371 msg = "do not compile class initializer (OSR ok)";
372 break;
373 }
374
375 if (TraceCompilationPolicy && Verbose) {
376 tty->print("\n\t check caller: ");
377 next_m->print_short_name(tty);
378 tty->print(" ( interpreted " INTPTR_FORMAT ", size=%d ) ", (address)next_m(), next_m->code_size());
379 }
380
381 current = next;
382 }
383
384 assert( !current || !current->is_compiled(), "" );
385
386 if (TraceCompilationPolicy && msg) tty->print("(%s)\n", msg);
387
388 return current;
389 }
390
391 RFrame* StackWalkCompPolicy::senderOf(RFrame* rf, GrowableArray<RFrame*>* stack) {
392 RFrame* sender = rf->caller();
393 if (sender && sender->num() == stack->length()) stack->push(sender);
394 return sender;
395 }
396
397
398 const char* StackWalkCompPolicy::shouldInline(methodHandle m, float freq, int cnt) {
399 // Allows targeted inlining
400 // positive filter: should send be inlined? returns NULL (--> yes)
401 // or rejection msg
402 int max_size = MaxInlineSize;
403 int cost = m->code_size();
404
405 // Check for too many throws (and not too huge)
406 if (m->interpreter_throwout_count() > InlineThrowCount && cost < InlineThrowMaxSize ) {
407 return NULL;
408 }
409
410 // bump the max size if the call is frequent
411 if ((freq >= InlineFrequencyRatio) || (cnt >= InlineFrequencyCount)) {
412 if (TraceFrequencyInlining) {
413 tty->print("(Inlined frequent method)\n");
414 m->print();
415 }
416 max_size = FreqInlineSize;
417 }
418 if (cost > max_size) {
419 return (_msg = "too big");
420 }
421 return NULL;
422 }
423
424
425 const char* StackWalkCompPolicy::shouldNotInline(methodHandle m) {
426 // negative filter: should send NOT be inlined? returns NULL (--> inline) or rejection msg
427 if (m->is_abstract()) return (_msg = "abstract method");
428 // note: we allow ik->is_abstract()
429 if (!instanceKlass::cast(m->method_holder())->is_initialized()) return (_msg = "method holder not initialized");
430 if (m->is_native()) return (_msg = "native method");
431 nmethod* m_code = m->code();
432 if( m_code != NULL && m_code->instructions_size() > InlineSmallCode )
433 return (_msg = "already compiled into a big method");
434
435 // use frequency-based objections only for non-trivial methods
436 if (m->code_size() <= MaxTrivialSize) return NULL;
437 if (UseInterpreter) { // don't use counts with -Xcomp
438 if ((m->code() == NULL) && m->was_never_executed()) return (_msg = "never executed");
439 if (!m->was_executed_more_than(MIN2(MinInliningThreshold, CompileThreshold >> 1))) return (_msg = "executed < MinInliningThreshold times");
440 }
441 if (methodOopDesc::has_unloaded_classes_in_signature(m, JavaThread::current())) return (_msg = "unloaded signature classes");
442
443 return NULL;
444 }
445
446
447
448 #endif // COMPILER2