Sun, 11 Sep 2011 14:48:24 -0700
7088955: add C2 IR support to the SA
Reviewed-by: kvn
1 /*
2 * Copyright (c) 1999, 2011, 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 */
25 #ifndef SHARE_VM_COMPILER_COMPILEBROKER_HPP
26 #define SHARE_VM_COMPILER_COMPILEBROKER_HPP
28 #include "ci/compilerInterface.hpp"
29 #include "compiler/abstractCompiler.hpp"
30 #include "runtime/perfData.hpp"
32 class nmethod;
33 class nmethodLocker;
35 // CompileTask
36 //
37 // An entry in the compile queue. It represents a pending or current
38 // compilation.
39 class CompileTask : public CHeapObj {
40 friend class VMStructs;
42 private:
43 Monitor* _lock;
44 uint _compile_id;
45 jobject _method;
46 int _osr_bci;
47 bool _is_complete;
48 bool _is_success;
49 bool _is_blocking;
50 int _comp_level;
51 int _num_inlined_bytecodes;
52 nmethodLocker* _code_handle; // holder of eventual result
53 CompileTask* _next, *_prev;
55 // Fields used for logging why the compilation was initiated:
56 jlong _time_queued; // in units of os::elapsed_counter()
57 jobject _hot_method; // which method actually triggered this task
58 int _hot_count; // information about its invocation counter
59 const char* _comment; // more info about the task
61 public:
62 CompileTask() {
63 _lock = new Monitor(Mutex::nonleaf+2, "CompileTaskLock");
64 }
66 void initialize(int compile_id, methodHandle method, int osr_bci, int comp_level,
67 methodHandle hot_method, int hot_count, const char* comment,
68 bool is_blocking);
70 void free();
72 int compile_id() const { return _compile_id; }
73 jobject method_handle() const { return _method; }
74 int osr_bci() const { return _osr_bci; }
75 bool is_complete() const { return _is_complete; }
76 bool is_blocking() const { return _is_blocking; }
77 bool is_success() const { return _is_success; }
79 nmethodLocker* code_handle() const { return _code_handle; }
80 void set_code_handle(nmethodLocker* l) { _code_handle = l; }
81 nmethod* code() const; // _code_handle->code()
82 void set_code(nmethod* nm); // _code_handle->set_code(nm)
84 Monitor* lock() const { return _lock; }
86 void mark_complete() { _is_complete = true; }
87 void mark_success() { _is_success = true; }
89 int comp_level() { return _comp_level;}
90 void set_comp_level(int comp_level) { _comp_level = comp_level;}
92 int num_inlined_bytecodes() const { return _num_inlined_bytecodes; }
93 void set_num_inlined_bytecodes(int n) { _num_inlined_bytecodes = n; }
95 CompileTask* next() const { return _next; }
96 void set_next(CompileTask* next) { _next = next; }
97 CompileTask* prev() const { return _prev; }
98 void set_prev(CompileTask* prev) { _prev = prev; }
100 private:
101 static void print_compilation_impl(outputStream* st, methodOop method, int compile_id, int comp_level, bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false, const char* msg = NULL);
103 public:
104 void print_compilation(outputStream* st = tty);
105 static void print_compilation(outputStream* st, const nmethod* nm, const char* msg = NULL) {
106 print_compilation_impl(st, nm->method(), nm->compile_id(), nm->comp_level(), nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false, msg);
107 }
109 static void print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
110 static void print_inlining(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
111 print_inlining(tty, method, inline_level, bci, msg);
112 }
114 static void print_inline_indent(int inline_level, outputStream* st = tty);
116 void print();
117 void print_line();
118 void print_line_on_error(outputStream* st, char* buf, int buflen);
120 void log_task(xmlStream* log);
121 void log_task_queued();
122 void log_task_start(CompileLog* log);
123 void log_task_done(CompileLog* log);
124 };
126 // CompilerCounters
127 //
128 // Per Compiler Performance Counters.
129 //
130 class CompilerCounters : public CHeapObj {
132 public:
133 enum {
134 cmname_buffer_length = 160
135 };
137 private:
139 char _current_method[cmname_buffer_length];
140 PerfStringVariable* _perf_current_method;
142 int _compile_type;
143 PerfVariable* _perf_compile_type;
145 PerfCounter* _perf_time;
146 PerfCounter* _perf_compiles;
148 public:
149 CompilerCounters(const char* name, int instance, TRAPS);
151 // these methods should be called in a thread safe context
153 void set_current_method(const char* method) {
154 strncpy(_current_method, method, (size_t)cmname_buffer_length);
155 if (UsePerfData) _perf_current_method->set_value(method);
156 }
158 char* current_method() { return _current_method; }
160 void set_compile_type(int compile_type) {
161 _compile_type = compile_type;
162 if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type);
163 }
165 int compile_type() { return _compile_type; }
167 PerfCounter* time_counter() { return _perf_time; }
168 PerfCounter* compile_counter() { return _perf_compiles; }
169 };
171 // CompileQueue
172 //
173 // A list of CompileTasks.
174 class CompileQueue : public CHeapObj {
175 private:
176 const char* _name;
177 Monitor* _lock;
179 CompileTask* _first;
180 CompileTask* _last;
182 int _size;
183 public:
184 CompileQueue(const char* name, Monitor* lock) {
185 _name = name;
186 _lock = lock;
187 _first = NULL;
188 _last = NULL;
189 _size = 0;
190 }
192 const char* name() const { return _name; }
193 Monitor* lock() const { return _lock; }
195 void add(CompileTask* task);
196 void remove(CompileTask* task);
197 CompileTask* first() { return _first; }
198 CompileTask* last() { return _last; }
200 CompileTask* get();
202 bool is_empty() const { return _first == NULL; }
203 int size() const { return _size; }
205 void print();
206 };
208 // CompileTaskWrapper
209 //
210 // Assign this task to the current thread. Deallocate the task
211 // when the compilation is complete.
212 class CompileTaskWrapper : StackObj {
213 public:
214 CompileTaskWrapper(CompileTask* task);
215 ~CompileTaskWrapper();
216 };
219 // Compilation
220 //
221 // The broker for all compilation requests.
222 class CompileBroker: AllStatic {
223 friend class Threads;
224 friend class CompileTaskWrapper;
226 public:
227 enum {
228 name_buffer_length = 100
229 };
231 // Compile type Information for print_last_compile() and CompilerCounters
232 enum { no_compile, normal_compile, osr_compile, native_compile };
234 private:
235 static bool _initialized;
236 static volatile bool _should_block;
238 // This flag can be used to stop compilation or turn it back on
239 static volatile jint _should_compile_new_jobs;
241 // The installed compiler(s)
242 static AbstractCompiler* _compilers[2];
244 // These counters are used for assigning id's to each compilation
245 static uint _compilation_id;
246 static uint _osr_compilation_id;
247 static uint _native_compilation_id;
249 static int _last_compile_type;
250 static int _last_compile_level;
251 static char _last_method_compiled[name_buffer_length];
253 static CompileQueue* _c2_method_queue;
254 static CompileQueue* _c1_method_queue;
255 static CompileTask* _task_free_list;
257 static GrowableArray<CompilerThread*>* _method_threads;
259 // performance counters
260 static PerfCounter* _perf_total_compilation;
261 static PerfCounter* _perf_native_compilation;
262 static PerfCounter* _perf_osr_compilation;
263 static PerfCounter* _perf_standard_compilation;
265 static PerfCounter* _perf_total_bailout_count;
266 static PerfCounter* _perf_total_invalidated_count;
267 static PerfCounter* _perf_total_compile_count;
268 static PerfCounter* _perf_total_native_compile_count;
269 static PerfCounter* _perf_total_osr_compile_count;
270 static PerfCounter* _perf_total_standard_compile_count;
272 static PerfCounter* _perf_sum_osr_bytes_compiled;
273 static PerfCounter* _perf_sum_standard_bytes_compiled;
274 static PerfCounter* _perf_sum_nmethod_size;
275 static PerfCounter* _perf_sum_nmethod_code_size;
277 static PerfStringVariable* _perf_last_method;
278 static PerfStringVariable* _perf_last_failed_method;
279 static PerfStringVariable* _perf_last_invalidated_method;
280 static PerfVariable* _perf_last_compile_type;
281 static PerfVariable* _perf_last_compile_size;
282 static PerfVariable* _perf_last_failed_type;
283 static PerfVariable* _perf_last_invalidated_type;
285 // Timers and counters for generating statistics
286 static elapsedTimer _t_total_compilation;
287 static elapsedTimer _t_osr_compilation;
288 static elapsedTimer _t_standard_compilation;
290 static int _total_bailout_count;
291 static int _total_invalidated_count;
292 static int _total_compile_count;
293 static int _total_native_compile_count;
294 static int _total_osr_compile_count;
295 static int _total_standard_compile_count;
297 static int _sum_osr_bytes_compiled;
298 static int _sum_standard_bytes_compiled;
299 static int _sum_nmethod_size;
300 static int _sum_nmethod_code_size;
302 static CompilerThread* make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS);
303 static void init_compiler_threads(int c1_compiler_count, int c2_compiler_count);
304 static bool compilation_is_complete (methodHandle method, int osr_bci, int comp_level);
305 static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level);
306 static uint assign_compile_id (methodHandle method, int osr_bci);
307 static bool is_compile_blocking (methodHandle method, int osr_bci);
308 static void preload_classes (methodHandle method, TRAPS);
310 static CompileTask* create_compile_task(CompileQueue* queue,
311 int compile_id,
312 methodHandle method,
313 int osr_bci,
314 int comp_level,
315 methodHandle hot_method,
316 int hot_count,
317 const char* comment,
318 bool blocking);
319 static CompileTask* allocate_task();
320 static void free_task(CompileTask* task);
321 static void wait_for_completion(CompileTask* task);
323 static void invoke_compiler_on_method(CompileTask* task);
324 static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level);
325 static void push_jni_handle_block();
326 static void pop_jni_handle_block();
327 static bool check_break_at(methodHandle method, int compile_id, bool is_osr);
328 static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
330 static void compile_method_base(methodHandle method,
331 int osr_bci,
332 int comp_level,
333 methodHandle hot_method,
334 int hot_count,
335 const char* comment,
336 TRAPS);
337 static CompileQueue* compile_queue(int comp_level) {
338 if (is_c2_compile(comp_level)) return _c2_method_queue;
339 if (is_c1_compile(comp_level)) return _c1_method_queue;
340 return NULL;
341 }
342 public:
343 enum {
344 // The entry bci used for non-OSR compilations.
345 standard_entry_bci = InvocationEntryBci
346 };
348 static AbstractCompiler* compiler(int comp_level) {
349 if (is_c2_compile(comp_level)) return _compilers[1]; // C2
350 if (is_c1_compile(comp_level)) return _compilers[0]; // C1
351 return NULL;
352 }
354 static bool compilation_is_in_queue(methodHandle method, int osr_bci);
355 static int queue_size(int comp_level) {
356 CompileQueue *q = compile_queue(comp_level);
357 return q != NULL ? q->size() : 0;
358 }
359 static void compilation_init();
360 static void init_compiler_thread_log();
361 static nmethod* compile_method(methodHandle method,
362 int osr_bci,
363 int comp_level,
364 methodHandle hot_method,
365 int hot_count,
366 const char* comment, TRAPS);
368 static void compiler_thread_loop();
370 static uint get_compilation_id() { return _compilation_id; }
371 static bool is_idle();
373 // Set _should_block.
374 // Call this from the VM, with Threads_lock held and a safepoint requested.
375 static void set_should_block();
377 // Call this from the compiler at convenient points, to poll for _should_block.
378 static void maybe_block();
380 enum {
381 // Flags for toggling compiler activity
382 stop_compilation = 0,
383 run_compilation = 1
384 };
386 static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
387 static bool set_should_compile_new_jobs(jint new_state) {
388 // Return success if the current caller set it
389 jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state);
390 return (old == (1-new_state));
391 }
392 static void handle_full_code_cache();
394 // Return total compilation ticks
395 static jlong total_compilation_ticks() {
396 return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0;
397 }
399 // Print a detailed accounting of compilation time
400 static void print_times();
402 // Debugging output for failure
403 static void print_last_compile();
405 static void print_compiler_threads_on(outputStream* st);
406 };
408 #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP