Fri, 03 Feb 2012 12:08:55 -0800
Merge
1 /*
2 * Copyright (c) 1999, 2012, 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,
102 bool is_osr_method = false, int osr_bci = -1, bool is_blocking = false,
103 const char* msg = NULL, bool short_form = false);
105 public:
106 void print_compilation(outputStream* st = tty, bool short_form = false);
107 static void print_compilation(outputStream* st, const nmethod* nm, const char* msg = NULL) {
108 print_compilation_impl(st, nm->method(), nm->compile_id(), nm->comp_level(),
109 nm->is_osr_method(), nm->is_osr_method() ? nm->osr_entry_bci() : -1, /*is_blocking*/ false,
110 msg);
111 }
113 static void print_inlining(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg = NULL);
114 static void print_inlining(ciMethod* method, int inline_level, int bci, const char* msg = NULL) {
115 print_inlining(tty, method, inline_level, bci, msg);
116 }
118 static void print_inline_indent(int inline_level, outputStream* st = tty);
120 void print();
121 void print_line();
122 void print_line_on_error(outputStream* st, char* buf, int buflen);
124 void log_task(xmlStream* log);
125 void log_task_queued();
126 void log_task_start(CompileLog* log);
127 void log_task_done(CompileLog* log);
128 };
130 // CompilerCounters
131 //
132 // Per Compiler Performance Counters.
133 //
134 class CompilerCounters : public CHeapObj {
136 public:
137 enum {
138 cmname_buffer_length = 160
139 };
141 private:
143 char _current_method[cmname_buffer_length];
144 PerfStringVariable* _perf_current_method;
146 int _compile_type;
147 PerfVariable* _perf_compile_type;
149 PerfCounter* _perf_time;
150 PerfCounter* _perf_compiles;
152 public:
153 CompilerCounters(const char* name, int instance, TRAPS);
155 // these methods should be called in a thread safe context
157 void set_current_method(const char* method) {
158 strncpy(_current_method, method, (size_t)cmname_buffer_length);
159 if (UsePerfData) _perf_current_method->set_value(method);
160 }
162 char* current_method() { return _current_method; }
164 void set_compile_type(int compile_type) {
165 _compile_type = compile_type;
166 if (UsePerfData) _perf_compile_type->set_value((jlong)compile_type);
167 }
169 int compile_type() { return _compile_type; }
171 PerfCounter* time_counter() { return _perf_time; }
172 PerfCounter* compile_counter() { return _perf_compiles; }
173 };
175 // CompileQueue
176 //
177 // A list of CompileTasks.
178 class CompileQueue : public CHeapObj {
179 private:
180 const char* _name;
181 Monitor* _lock;
183 CompileTask* _first;
184 CompileTask* _last;
186 int _size;
187 public:
188 CompileQueue(const char* name, Monitor* lock) {
189 _name = name;
190 _lock = lock;
191 _first = NULL;
192 _last = NULL;
193 _size = 0;
194 }
196 const char* name() const { return _name; }
197 Monitor* lock() const { return _lock; }
199 void add(CompileTask* task);
200 void remove(CompileTask* task);
201 CompileTask* first() { return _first; }
202 CompileTask* last() { return _last; }
204 CompileTask* get();
206 bool is_empty() const { return _first == NULL; }
207 int size() const { return _size; }
209 void print();
210 };
212 // CompileTaskWrapper
213 //
214 // Assign this task to the current thread. Deallocate the task
215 // when the compilation is complete.
216 class CompileTaskWrapper : StackObj {
217 public:
218 CompileTaskWrapper(CompileTask* task);
219 ~CompileTaskWrapper();
220 };
223 // Compilation
224 //
225 // The broker for all compilation requests.
226 class CompileBroker: AllStatic {
227 friend class Threads;
228 friend class CompileTaskWrapper;
230 public:
231 enum {
232 name_buffer_length = 100
233 };
235 // Compile type Information for print_last_compile() and CompilerCounters
236 enum { no_compile, normal_compile, osr_compile, native_compile };
238 private:
239 static bool _initialized;
240 static volatile bool _should_block;
242 // This flag can be used to stop compilation or turn it back on
243 static volatile jint _should_compile_new_jobs;
245 // The installed compiler(s)
246 static AbstractCompiler* _compilers[2];
248 // These counters are used for assigning id's to each compilation
249 static uint _compilation_id;
250 static uint _osr_compilation_id;
251 static uint _native_compilation_id;
253 static int _last_compile_type;
254 static int _last_compile_level;
255 static char _last_method_compiled[name_buffer_length];
257 static CompileQueue* _c2_method_queue;
258 static CompileQueue* _c1_method_queue;
259 static CompileTask* _task_free_list;
261 static GrowableArray<CompilerThread*>* _method_threads;
263 // performance counters
264 static PerfCounter* _perf_total_compilation;
265 static PerfCounter* _perf_native_compilation;
266 static PerfCounter* _perf_osr_compilation;
267 static PerfCounter* _perf_standard_compilation;
269 static PerfCounter* _perf_total_bailout_count;
270 static PerfCounter* _perf_total_invalidated_count;
271 static PerfCounter* _perf_total_compile_count;
272 static PerfCounter* _perf_total_native_compile_count;
273 static PerfCounter* _perf_total_osr_compile_count;
274 static PerfCounter* _perf_total_standard_compile_count;
276 static PerfCounter* _perf_sum_osr_bytes_compiled;
277 static PerfCounter* _perf_sum_standard_bytes_compiled;
278 static PerfCounter* _perf_sum_nmethod_size;
279 static PerfCounter* _perf_sum_nmethod_code_size;
281 static PerfStringVariable* _perf_last_method;
282 static PerfStringVariable* _perf_last_failed_method;
283 static PerfStringVariable* _perf_last_invalidated_method;
284 static PerfVariable* _perf_last_compile_type;
285 static PerfVariable* _perf_last_compile_size;
286 static PerfVariable* _perf_last_failed_type;
287 static PerfVariable* _perf_last_invalidated_type;
289 // Timers and counters for generating statistics
290 static elapsedTimer _t_total_compilation;
291 static elapsedTimer _t_osr_compilation;
292 static elapsedTimer _t_standard_compilation;
294 static int _total_bailout_count;
295 static int _total_invalidated_count;
296 static int _total_compile_count;
297 static int _total_native_compile_count;
298 static int _total_osr_compile_count;
299 static int _total_standard_compile_count;
301 static int _sum_osr_bytes_compiled;
302 static int _sum_standard_bytes_compiled;
303 static int _sum_nmethod_size;
304 static int _sum_nmethod_code_size;
306 static CompilerThread* make_compiler_thread(const char* name, CompileQueue* queue, CompilerCounters* counters, TRAPS);
307 static void init_compiler_threads(int c1_compiler_count, int c2_compiler_count);
308 static bool compilation_is_complete (methodHandle method, int osr_bci, int comp_level);
309 static bool compilation_is_prohibited(methodHandle method, int osr_bci, int comp_level);
310 static uint assign_compile_id (methodHandle method, int osr_bci);
311 static bool is_compile_blocking (methodHandle method, int osr_bci);
312 static void preload_classes (methodHandle method, TRAPS);
314 static CompileTask* create_compile_task(CompileQueue* queue,
315 int compile_id,
316 methodHandle method,
317 int osr_bci,
318 int comp_level,
319 methodHandle hot_method,
320 int hot_count,
321 const char* comment,
322 bool blocking);
323 static CompileTask* allocate_task();
324 static void free_task(CompileTask* task);
325 static void wait_for_completion(CompileTask* task);
327 static void invoke_compiler_on_method(CompileTask* task);
328 static void set_last_compile(CompilerThread *thread, methodHandle method, bool is_osr, int comp_level);
329 static void push_jni_handle_block();
330 static void pop_jni_handle_block();
331 static bool check_break_at(methodHandle method, int compile_id, bool is_osr);
332 static void collect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
334 static void compile_method_base(methodHandle method,
335 int osr_bci,
336 int comp_level,
337 methodHandle hot_method,
338 int hot_count,
339 const char* comment,
340 Thread* thread);
341 static CompileQueue* compile_queue(int comp_level) {
342 if (is_c2_compile(comp_level)) return _c2_method_queue;
343 if (is_c1_compile(comp_level)) return _c1_method_queue;
344 return NULL;
345 }
346 public:
347 enum {
348 // The entry bci used for non-OSR compilations.
349 standard_entry_bci = InvocationEntryBci
350 };
352 static AbstractCompiler* compiler(int comp_level) {
353 if (is_c2_compile(comp_level)) return _compilers[1]; // C2
354 if (is_c1_compile(comp_level)) return _compilers[0]; // C1
355 return NULL;
356 }
358 static bool compilation_is_in_queue(methodHandle method, int osr_bci);
359 static int queue_size(int comp_level) {
360 CompileQueue *q = compile_queue(comp_level);
361 return q != NULL ? q->size() : 0;
362 }
363 static void compilation_init();
364 static void init_compiler_thread_log();
365 static nmethod* compile_method(methodHandle method,
366 int osr_bci,
367 int comp_level,
368 methodHandle hot_method,
369 int hot_count,
370 const char* comment, Thread* thread);
372 static void compiler_thread_loop();
374 static uint get_compilation_id() { return _compilation_id; }
375 static bool is_idle();
377 // Set _should_block.
378 // Call this from the VM, with Threads_lock held and a safepoint requested.
379 static void set_should_block();
381 // Call this from the compiler at convenient points, to poll for _should_block.
382 static void maybe_block();
384 enum {
385 // Flags for toggling compiler activity
386 stop_compilation = 0,
387 run_compilation = 1
388 };
390 static bool should_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
391 static bool set_should_compile_new_jobs(jint new_state) {
392 // Return success if the current caller set it
393 jint old = Atomic::cmpxchg(new_state, &_should_compile_new_jobs, 1-new_state);
394 return (old == (1-new_state));
395 }
396 static void handle_full_code_cache();
398 // Return total compilation ticks
399 static jlong total_compilation_ticks() {
400 return _perf_total_compilation != NULL ? _perf_total_compilation->get_value() : 0;
401 }
403 // Print a detailed accounting of compilation time
404 static void print_times();
406 // Debugging output for failure
407 static void print_last_compile();
409 static void print_compiler_threads_on(outputStream* st);
410 };
412 #endif // SHARE_VM_COMPILER_COMPILEBROKER_HPP