1 /*
2 * Copyright (c) 1998, 2014, 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 "compiler/compileTask.hpp"
27 #include "compiler/compileLog.hpp"
28 #include "compiler/compileBroker.hpp"
29
30 CompileTask* CompileTask::_task_free_list = NULL;
31 #ifdef ASSERT
32 int CompileTask::_num_allocated_tasks = 0;
33 #endif
34
35 /**
36 * Allocate a CompileTask, from the free list if possible.
37 */
38 CompileTask* CompileTask::allocate() {
39 MutexLocker locker(CompileTaskAlloc_lock);
40 CompileTask* task = NULL;
41
42 if (_task_free_list != NULL) {
43 task = _task_free_list;
44 _task_free_list = task->next();
45 task->set_next(NULL);
46 } else {
47 task = new CompileTask();
48 DEBUG_ONLY(_num_allocated_tasks++;)
49 assert (WhiteBoxAPI || _num_allocated_tasks < 10000, "Leaking compilation tasks?");
50 task->set_next(NULL);
51 task->set_is_free(true);
52 }
53 assert(task->is_free(), "Task must be free.");
54 task->set_is_free(false);
55 return task;
56 }
57
58 /**
59 * Add a task to the free list.
60 */
61
62 void CompileTask::free(CompileTask* task) {
63 MutexLocker locker(CompileTaskAlloc_lock);
64 if (!task->is_free()) {
65 task->set_code(NULL);
66 assert(!task->lock()->is_locked(), "Should not be locked when freed");
67 JNIHandles::destroy_global(task->_method_holder);
68 JNIHandles::destroy_global(task->_hot_method_holder);
69
70 task->set_is_free(true);
71 task->set_next(_task_free_list);
72 _task_free_list = task;
73 }
74 }
75
76
77 void CompileTask::initialize(int compile_id,
78 methodHandle method,
79 int osr_bci,
80 int comp_level,
81 methodHandle hot_method,
82 int hot_count,
83 const char* comment,
84 bool is_blocking) {
85 assert(!_lock->is_locked(), "bad locking");
86
87 _compile_id = compile_id;
88 _method = method();
89 _method_holder = JNIHandles::make_global(method->method_holder()->klass_holder());
90 _osr_bci = osr_bci;
91 _is_blocking = is_blocking;
92 _comp_level = comp_level;
93 _num_inlined_bytecodes = 0;
94
95 _is_complete = false;
96 _is_success = false;
97 _code_handle = NULL;
98
99 _hot_method = NULL;
100 _hot_method_holder = NULL;
101 _hot_count = hot_count;
102 _time_queued = 0; // tidy
103 _comment = comment;
104 _failure_reason = NULL;
105
106 if (LogCompilation) {
107 _time_queued = os::elapsed_counter();
108 if (hot_method.not_null()) {
109 if (hot_method == method) {
110 _hot_method = _method;
111 } else {
112 _hot_method = hot_method();
113 // only add loader or mirror if different from _method_holder
114 _hot_method_holder = JNIHandles::make_global(hot_method->method_holder()->klass_holder());
115 }
116 }
117 }
118
119 _next = NULL;
120 }
121
122 // ------------------------------------------------------------------
123 // CompileTask::code/set_code
124 //
125 nmethod* CompileTask::code() const {
126 if (_code_handle == NULL) return NULL;
127 return _code_handle->code();
128 }
129
130 void CompileTask::set_code(nmethod* nm) {
131 if (_code_handle == NULL && nm == NULL) return;
132 guarantee(_code_handle != NULL, "");
133 _code_handle->set_code(nm);
134 if (nm == NULL) _code_handle = NULL; // drop the handle also
135 }
136
137 void CompileTask::mark_on_stack() {
138 // Mark these methods as something redefine classes cannot remove.
139 _method->set_on_stack(true);
140 if (_hot_method != NULL) {
141 _hot_method->set_on_stack(true);
142 }
143 }
144
145 // RedefineClasses support
146 void CompileTask::metadata_do(void f(Metadata*)) {
147 f(method());
148 if (hot_method() != NULL && hot_method() != method()) {
149 f(hot_method());
150 }
151 }
152
153 // ------------------------------------------------------------------
154 // CompileTask::print_line_on_error
155 //
156 // This function is called by fatal error handler when the thread
157 // causing troubles is a compiler thread.
158 //
159 // Do not grab any lock, do not allocate memory.
160 //
161 // Otherwise it's the same as CompileTask::print_line()
162 //
163 void CompileTask::print_line_on_error(outputStream* st, char* buf, int buflen) {
164 // print compiler name
165 st->print("%s:", CompileBroker::compiler_name(comp_level()));
166 print(st);
167 }
168
169 // ------------------------------------------------------------------
170 // CompileTask::print_tty
171 void CompileTask::print_tty() {
172 ttyLocker ttyl; // keep the following output all in one block
173 // print compiler name if requested
174 if (CIPrintCompilerName) tty->print("%s:", CompileBroker::compiler_name(comp_level()));
175 print(tty);
176 }
177
178 // ------------------------------------------------------------------
179 // CompileTask::print_impl
180 void CompileTask::print_impl(outputStream* st, Method* method, int compile_id, int comp_level,
181 bool is_osr_method, int osr_bci, bool is_blocking,
182 const char* msg, bool short_form, bool cr) {
183 if (!short_form) {
184 st->print("%7d ", (int) st->time_stamp().milliseconds()); // print timestamp
185 }
186 st->print("%4d ", compile_id); // print compilation number
187
188 // For unloaded methods the transition to zombie occurs after the
189 // method is cleared so it's impossible to report accurate
190 // information for that case.
191 bool is_synchronized = false;
192 bool has_exception_handler = false;
193 bool is_native = false;
194 if (method != NULL) {
195 is_synchronized = method->is_synchronized();
196 has_exception_handler = method->has_exception_handler();
197 is_native = method->is_native();
198 }
199 // method attributes
200 const char compile_type = is_osr_method ? '%' : ' ';
201 const char sync_char = is_synchronized ? 's' : ' ';
202 const char exception_char = has_exception_handler ? '!' : ' ';
203 const char blocking_char = is_blocking ? 'b' : ' ';
204 const char native_char = is_native ? 'n' : ' ';
205
206 // print method attributes
207 st->print("%c%c%c%c%c ", compile_type, sync_char, exception_char, blocking_char, native_char);
208
209 if (TieredCompilation) {
210 if (comp_level != -1) st->print("%d ", comp_level);
211 else st->print("- ");
212 }
213 st->print(" "); // more indent
214
215 if (method == NULL) {
216 st->print("(method)");
217 } else {
218 method->print_short_name(st);
219 if (is_osr_method) {
220 st->print(" @ %d", osr_bci);
221 }
222 if (method->is_native())
223 st->print(" (native)");
224 else
225 st->print(" (%d bytes)", method->code_size());
226 }
227
228 if (msg != NULL) {
229 st->print(" %s", msg);
230 }
231 if (cr) {
232 st->cr();
233 }
234 }
235
236 void CompileTask::print_inline_indent(int inline_level, outputStream* st) {
237 // 1234567
238 st->print(" "); // print timestamp
239 // 1234
240 st->print(" "); // print compilation number
241 // %s!bn
242 st->print(" "); // print method attributes
243 if (TieredCompilation) {
244 st->print(" ");
245 }
246 st->print(" "); // more indent
247 st->print(" "); // initial inlining indent
248 for (int i = 0; i < inline_level; i++) st->print(" ");
249 }
250
251 // ------------------------------------------------------------------
252 // CompileTask::print_compilation
253 void CompileTask::print(outputStream* st, const char* msg, bool short_form, bool cr) {
254 bool is_osr_method = osr_bci() != InvocationEntryBci;
255 print_impl(st, method(), compile_id(), comp_level(), is_osr_method, osr_bci(), is_blocking(), msg, short_form, cr);
256 }
257
258 // ------------------------------------------------------------------
259 // CompileTask::log_task
260 void CompileTask::log_task(xmlStream* log) {
261 Thread* thread = Thread::current();
262 methodHandle method(thread, this->method());
263 ResourceMark rm(thread);
264
265 // <task id='9' method='M' osr_bci='X' level='1' blocking='1' stamp='1.234'>
266 log->print(" compile_id='%d'", _compile_id);
267 if (_osr_bci != CompileBroker::standard_entry_bci) {
268 log->print(" compile_kind='osr'"); // same as nmethod::compile_kind
269 } // else compile_kind='c2c'
270 if (!method.is_null()) log->method(method);
271 if (_osr_bci != CompileBroker::standard_entry_bci) {
272 log->print(" osr_bci='%d'", _osr_bci);
273 }
274 if (_comp_level != CompLevel_highest_tier) {
275 log->print(" level='%d'", _comp_level);
276 }
277 if (_is_blocking) {
278 log->print(" blocking='1'");
279 }
280 log->stamp();
281 }
282
283 // ------------------------------------------------------------------
284 // CompileTask::log_task_queued
285 void CompileTask::log_task_queued() {
286 Thread* thread = Thread::current();
287 ttyLocker ttyl;
288 ResourceMark rm(thread);
289
290 xtty->begin_elem("task_queued");
291 log_task(xtty);
292 if (_comment != NULL) {
293 xtty->print(" comment='%s'", _comment);
294 }
295 if (_hot_method != NULL) {
296 methodHandle hot(thread, _hot_method);
297 methodHandle method(thread, _method);
298 if (hot() != method()) {
299 xtty->method(hot);
300 }
301 }
302 if (_hot_count != 0) {
303 xtty->print(" hot_count='%d'", _hot_count);
304 }
305 xtty->end_elem();
306 }
307
308
309 // ------------------------------------------------------------------
310 // CompileTask::log_task_start
311 void CompileTask::log_task_start(CompileLog* log) {
312 log->begin_head("task");
313 log_task(log);
314 log->end_head();
315 }
316
317
318 // ------------------------------------------------------------------
319 // CompileTask::log_task_done
320 void CompileTask::log_task_done(CompileLog* log) {
321 Thread* thread = Thread::current();
322 methodHandle method(thread, this->method());
323 ResourceMark rm(thread);
324
325 if (!_is_success) {
326 const char* reason = _failure_reason != NULL ? _failure_reason : "unknown";
327 log->elem("failure reason='%s'", reason);
328 }
329
330 // <task_done ... stamp='1.234'> </task>
331 nmethod* nm = code();
332 log->begin_elem("task_done success='%d' nmsize='%d' count='%d'",
333 _is_success, nm == NULL ? 0 : nm->content_size(),
334 method->invocation_count());
335 int bec = method->backedge_count();
336 if (bec != 0) log->print(" backedge_count='%d'", bec);
337 // Note: "_is_complete" is about to be set, but is not.
338 if (_num_inlined_bytecodes != 0) {
339 log->print(" inlined_bytes='%d'", _num_inlined_bytecodes);
340 }
341 log->stamp();
342 log->end_elem();
343 log->clear_identities(); // next task will have different CI
344 log->tail("task");
345 if (log->unflushed_count() > 2000) {
346 log->flush();
347 }
348 log->mark_file_end();
349 }
350
351 // ------------------------------------------------------------------
352 // CompileTask::print_inlining
353 void CompileTask::print_inlining_inner(outputStream* st, ciMethod* method, int inline_level, int bci, const char* msg) {
354 // 1234567
355 st->print(" "); // print timestamp
356 // 1234
357 st->print(" "); // print compilation number
358
359 // method attributes
360 if (method->is_loaded()) {
361 const char sync_char = method->is_synchronized() ? 's' : ' ';
362 const char exception_char = method->has_exception_handlers() ? '!' : ' ';
363 const char monitors_char = method->has_monitor_bytecodes() ? 'm' : ' ';
364
365 // print method attributes
366 st->print(" %c%c%c ", sync_char, exception_char, monitors_char);
367 } else {
368 // %s!bn
369 st->print(" "); // print method attributes
370 }
371
372 if (TieredCompilation) {
373 st->print(" ");
374 }
375 st->print(" "); // more indent
376 st->print(" "); // initial inlining indent
377
378 for (int i = 0; i < inline_level; i++) st->print(" ");
379
380 st->print("@ %d ", bci); // print bci
381 method->print_short_name(st);
382 if (method->is_loaded())
383 st->print(" (%d bytes)", method->code_size());
384 else
385 st->print(" (not loaded)");
386
387 if (msg != NULL) {
388 st->print(" %s", msg);
389 }
390 st->cr();
391 }
--- EOF ---