- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathcompileBroker.hpp
454 lines (370 loc) · 16.4 KB
/
compileBroker.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#ifndef SHARE_COMPILER_COMPILEBROKER_HPP
#defineSHARE_COMPILER_COMPILEBROKER_HPP
#include"ci/compilerInterface.hpp"
#include"compiler/abstractCompiler.hpp"
#include"compiler/compilerDirectives.hpp"
#include"compiler/compilerThread.hpp"
#include"compiler/compileTask.hpp"
#include"runtime/atomic.hpp"
#include"runtime/perfDataTypes.hpp"
#include"utilities/stack.hpp"
#if INCLUDE_JVMCI
#include"jvmci/jvmciCompiler.hpp"
#endif
classnmethod;
#if defined(ASSERT) && COMPILER2_OR_JVMCI
// Stress testing. Dedicated threads revert optimizations based on escape analysis concurrently to
// the running java application. Configured with vm options DeoptimizeObjectsALot*.
classDeoptimizeObjectsALotThread : publicJavaThread {
staticvoiddeopt_objs_alot_thread_entry(JavaThread* thread, TRAPS);
voiddeoptimize_objects_alot_loop_single();
voiddeoptimize_objects_alot_loop_all();
public:
DeoptimizeObjectsALotThread() : JavaThread(&deopt_objs_alot_thread_entry) { }
boolis_hidden_from_external_view() const { returntrue; }
};
#endif
// CompilerCounters
//
// Per Compiler Performance Counters.
//
classCompilerCounters : publicCHeapObj<mtCompiler> {
public:
enum {
cmname_buffer_length = 160
};
private:
char _current_method[cmname_buffer_length];
int _compile_type;
public:
CompilerCounters();
// these methods should be called in a thread safe context
voidset_current_method(constchar* method) {
strncpy(_current_method, method, (size_t)cmname_buffer_length-1);
_current_method[cmname_buffer_length-1] = '\0';
}
char* current_method() { return _current_method; }
voidset_compile_type(int compile_type) {
_compile_type = compile_type;
}
intcompile_type() { return _compile_type; }
};
// CompileQueue
//
// A list of CompileTasks.
classCompileQueue : publicCHeapObj<mtCompiler> {
private:
constchar* _name;
CompileTask* _first;
CompileTask* _last;
CompileTask* _first_stale;
volatileint _size;
int _peak_size;
uint _total_added;
uint _total_removed;
voidpurge_stale_tasks();
public:
CompileQueue(constchar* name) {
_name = name;
_first = nullptr;
_last = nullptr;
_size = 0;
_total_added = 0;
_total_removed = 0;
_peak_size = 0;
_first_stale = nullptr;
}
constchar* name() const { return _name; }
voidadd(CompileTask* task);
voidremove(CompileTask* task);
voidremove_and_mark_stale(CompileTask* task);
CompileTask* first() { return _first; }
CompileTask* last() { return _last; }
CompileTask* get(CompilerThread* thread);
boolis_empty() const { return _first == nullptr; }
intsize() const { return _size; }
intget_peak_size() const { return _peak_size; }
uintget_total_added() const { return _total_added; }
uintget_total_removed() const { return _total_removed; }
// Redefine Classes support
voidmark_on_stack();
voidfree_all();
voidprint_tty();
voidprint(outputStream* st = tty);
~CompileQueue() {
assert (is_empty(), " Compile Queue must be empty");
}
};
// CompileTaskWrapper
//
// Assign this task to the current thread. Deallocate the task
// when the compilation is complete.
classCompileTaskWrapper : StackObj {
public:
CompileTaskWrapper(CompileTask* task);
~CompileTaskWrapper();
};
// Compilation
//
// The broker for all compilation requests.
classCompileBroker: AllStatic {
friendclassThreads;
friendclassCompileTaskWrapper;
public:
enum {
name_buffer_length = 100
};
// Compile type Information for print_last_compile() and CompilerCounters
enum { no_compile, normal_compile, osr_compile, native_compile };
staticintassign_compile_id (const methodHandle& method, int osr_bci);
private:
staticbool _initialized;
staticvolatilebool _should_block;
// This flag can be used to stop compilation or turn it back on
staticvolatile jint _should_compile_new_jobs;
// The installed compiler(s)
static AbstractCompiler* _compilers[2];
// The maximum numbers of compiler threads to be determined during startup.
staticint _c1_count, _c2_count;
// An array of compiler thread Java objects
static jobject *_compiler1_objects, *_compiler2_objects;
// An array of compiler logs
static CompileLog **_compiler1_logs, **_compiler2_logs;
// These counters are used for assigning id's to each compilation
staticvolatile jint _compilation_id;
staticvolatile jint _osr_compilation_id;
staticvolatile jint _native_compilation_id;
static CompileQueue* _c2_compile_queue;
static CompileQueue* _c1_compile_queue;
// performance counters
static PerfCounter* _perf_total_compilation;
static PerfCounter* _perf_osr_compilation;
static PerfCounter* _perf_standard_compilation;
static PerfCounter* _perf_total_bailout_count;
static PerfCounter* _perf_total_invalidated_count;
static PerfCounter* _perf_total_compile_count;
static PerfCounter* _perf_total_osr_compile_count;
static PerfCounter* _perf_total_standard_compile_count;
static PerfCounter* _perf_sum_osr_bytes_compiled;
static PerfCounter* _perf_sum_standard_bytes_compiled;
static PerfCounter* _perf_sum_nmethod_size;
static PerfCounter* _perf_sum_nmethod_code_size;
static PerfStringVariable* _perf_last_method;
static PerfStringVariable* _perf_last_failed_method;
static PerfStringVariable* _perf_last_invalidated_method;
static PerfVariable* _perf_last_compile_type;
static PerfVariable* _perf_last_compile_size;
static PerfVariable* _perf_last_failed_type;
static PerfVariable* _perf_last_invalidated_type;
// Timers and counters for generating statistics
static elapsedTimer _t_total_compilation;
static elapsedTimer _t_osr_compilation;
static elapsedTimer _t_standard_compilation;
static elapsedTimer _t_invalidated_compilation;
static elapsedTimer _t_bailedout_compilation;
staticuint _total_compile_count;
staticuint _total_bailout_count;
staticuint _total_invalidated_count;
staticuint _total_native_compile_count;
staticuint _total_osr_compile_count;
staticuint _total_standard_compile_count;
staticuint _total_compiler_stopped_count;
staticuint _total_compiler_restarted_count;
staticuint _sum_osr_bytes_compiled;
staticuint _sum_standard_bytes_compiled;
staticuint _sum_nmethod_size;
staticuint _sum_nmethod_code_size;
static jlong _peak_compilation_time;
static CompilerStatistics _stats_per_level[];
staticvolatileint _print_compilation_warning;
enum ThreadType {
compiler_t,
deoptimizer_t
};
static JavaThread* make_thread(ThreadType type, jobject thread_oop, CompileQueue* queue, AbstractCompiler* comp, JavaThread* THREAD);
staticvoidinit_compiler_threads();
staticvoidpossibly_add_compiler_threads(JavaThread* THREAD);
staticboolcompilation_is_prohibited(const methodHandle& method, int osr_bci, int comp_level, bool excluded);
static CompileTask* create_compile_task(CompileQueue* queue,
int compile_id,
const methodHandle& method,
int osr_bci,
int comp_level,
const methodHandle& hot_method,
int hot_count,
CompileTask::CompileReason compile_reason,
bool blocking);
staticvoidwait_for_completion(CompileTask* task);
#if INCLUDE_JVMCI
staticboolwait_for_jvmci_completion(JVMCICompiler* comp, CompileTask* task, JavaThread* thread);
#endif
staticvoidfree_buffer_blob_if_allocated(CompilerThread* thread);
staticvoidinvoke_compiler_on_method(CompileTask* task);
staticvoidhandle_compile_error(CompilerThread* thread, CompileTask* task, ciEnv* ci_env,
int compilable, constchar* failure_reason);
staticvoidupdate_compile_perf_data(CompilerThread *thread, const methodHandle& method, bool is_osr);
staticvoidcollect_statistics(CompilerThread* thread, elapsedTimer time, CompileTask* task);
staticvoidcompile_method_base(const methodHandle& method,
int osr_bci,
int comp_level,
const methodHandle& hot_method,
int hot_count,
CompileTask::CompileReason compile_reason,
bool blocking,
Thread* thread);
static CompileQueue* compile_queue(int comp_level);
staticboolinit_compiler_runtime();
staticvoidshutdown_compiler_runtime(AbstractCompiler* comp, CompilerThread* thread);
public:
enum {
// The entry bci used for non-OSR compilations.
standard_entry_bci = InvocationEntryBci
};
static AbstractCompiler* compiler(int comp_level) {
if (is_c2_compile(comp_level)) return _compilers[1]; // C2
if (is_c1_compile(comp_level)) return _compilers[0]; // C1
returnnullptr;
}
staticboolcompilation_is_complete(const methodHandle& method, int osr_bci, int comp_level);
staticboolcompilation_is_in_queue(const methodHandle& method);
staticvoidprint_compile_queues(outputStream* st);
staticintqueue_size(int comp_level) {
CompileQueue *q = compile_queue(comp_level);
return q != nullptr ? q->size() : 0;
}
staticvoidcompilation_init(JavaThread* THREAD);
staticvoidinit_compiler_thread_log();
static nmethod* compile_method(const methodHandle& method,
int osr_bci,
int comp_level,
const methodHandle& hot_method,
int hot_count,
CompileTask::CompileReason compile_reason,
TRAPS);
static CompileQueue* c1_compile_queue();
static CompileQueue* c2_compile_queue();
private:
static nmethod* compile_method(const methodHandle& method,
int osr_bci,
int comp_level,
const methodHandle& hot_method,
int hot_count,
CompileTask::CompileReason compile_reason,
DirectiveSet* directive,
TRAPS);
public:
// Acquire any needed locks and assign a compile id
staticintassign_compile_id_unlocked(Thread* thread, const methodHandle& method, int osr_bci);
staticvoidcompiler_thread_loop();
staticintget_compilation_id() { return _compilation_id; }
// Set _should_block.
// Call this from the VM, with Threads_lock held and a safepoint requested.
staticvoidset_should_block();
// Call this from the compiler at convenient points, to poll for _should_block.
staticvoidmaybe_block();
enum CompilerActivity {
// Flags for toggling compiler activity
stop_compilation = 0,
run_compilation = 1,
shutdown_compilation = 2
};
staticinline jint get_compilation_activity_mode() { return _should_compile_new_jobs; }
staticinlineboolshould_compile_new_jobs() { return UseCompiler && (_should_compile_new_jobs == run_compilation); }
staticboolset_should_compile_new_jobs(jint new_state) {
// Return success if the current caller set it
jint old = Atomic::cmpxchg(&_should_compile_new_jobs, 1-new_state, new_state);
bool success = (old == (1-new_state));
if (success) {
if (new_state == run_compilation) {
_total_compiler_restarted_count++;
} else {
_total_compiler_stopped_count++;
}
}
return success;
}
staticvoiddisable_compilation_forever() {
UseCompiler = false;
AlwaysCompileLoopMethods = false;
Atomic::xchg(&_should_compile_new_jobs, jint(shutdown_compilation));
}
staticboolis_compilation_disabled_forever() {
return _should_compile_new_jobs == shutdown_compilation;
}
staticvoidhandle_full_code_cache(CodeBlobType code_blob_type);
// Ensures that warning is only printed once.
staticboolshould_print_compiler_warning() {
jint old = Atomic::cmpxchg(&_print_compilation_warning, 0, 1);
return old == 0;
}
// Return total compilation ticks
static jlong total_compilation_ticks();
// Redefine Classes support
staticvoidmark_on_stack();
// Print current compilation time stats for a given compiler
staticvoidprint_times(constchar* name, CompilerStatistics* stats);
// Print a detailed accounting of compilation time
staticvoidprint_times(bool per_compiler = true, bool aggregate = true);
// compiler name for debugging
staticconstchar* compiler_name(int comp_level);
// Provide access to compiler thread Java objects
static jobject compiler1_object(int idx) {
assert(_compiler1_objects != nullptr, "must be initialized");
assert(idx < _c1_count, "oob");
return _compiler1_objects[idx];
}
static jobject compiler2_object(int idx) {
assert(_compiler2_objects != nullptr, "must be initialized");
assert(idx < _c2_count, "oob");
return _compiler2_objects[idx];
}
static AbstractCompiler* compiler1() { return _compilers[0]; }
static AbstractCompiler* compiler2() { return _compilers[1]; }
staticboolcan_remove(CompilerThread *ct, bool do_it);
static CompileLog* get_log(CompilerThread* ct);
staticintget_c1_thread_count() { return _compilers[0]->num_compiler_threads(); }
staticintget_c2_thread_count() { return _compilers[1]->num_compiler_threads(); }
staticintget_total_compile_count() { return _total_compile_count; }
staticintget_total_bailout_count() { return _total_bailout_count; }
staticintget_total_invalidated_count() { return _total_invalidated_count; }
staticintget_total_native_compile_count() { return _total_native_compile_count; }
staticintget_total_osr_compile_count() { return _total_osr_compile_count; }
staticintget_total_standard_compile_count() { return _total_standard_compile_count; }
staticintget_total_compiler_stopped_count() { return _total_compiler_stopped_count; }
staticintget_total_compiler_restarted_count() { return _total_compiler_restarted_count; }
staticintget_sum_osr_bytes_compiled() { return _sum_osr_bytes_compiled; }
staticintget_sum_standard_bytes_compiled() { return _sum_standard_bytes_compiled; }
staticintget_sum_nmethod_size() { return _sum_nmethod_size;}
staticintget_sum_nmethod_code_size() { return _sum_nmethod_code_size; }
static jlong get_peak_compilation_time() { return _peak_compilation_time; }
static jlong get_total_compilation_time() { return _t_total_compilation.milliseconds(); }
// Log that compilation profiling is skipped because metaspace is full.
staticvoidlog_metaspace_failure();
// CodeHeap State Analytics.
staticvoidprint_info(outputStream *out);
staticvoidprint_heapinfo(outputStream *out, constchar* function, size_t granularity);
};
#endif// SHARE_COMPILER_COMPILEBROKER_HPP