-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Expand file tree
/
Copy pathvmOperations.cpp
More file actions
607 lines (515 loc) · 19.1 KB
/
Copy pathvmOperations.cpp
File metadata and controls
607 lines (515 loc) · 19.1 KB
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright (c) 1997, 2026, 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.
*
*/
#include "classfile/classLoaderDataGraph.hpp"
#include "classfile/stringTable.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/vmSymbols.hpp"
#include "code/codeCache.hpp"
#include "compiler/compileBroker.hpp"
#include "gc/shared/collectedHeap.hpp"
#include "gc/shared/isGCActiveMark.hpp"
#include "logging/log.hpp"
#include "logging/logConfiguration.hpp"
#include "logging/logStream.hpp"
#include "memory/heapInspection.hpp"
#include "memory/metaspace/metaspaceReporter.hpp"
#include "memory/resourceArea.hpp"
#include "memory/universe.hpp"
#include "oops/symbol.hpp"
#include "runtime/arguments.hpp"
#include "runtime/deoptimization.hpp"
#include "runtime/frame.inline.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/javaThread.inline.hpp"
#include "runtime/jniHandles.hpp"
#include "runtime/objectMonitor.inline.hpp"
#include "runtime/stackFrameStream.inline.hpp"
#include "runtime/synchronizer.hpp"
#include "runtime/threads.hpp"
#include "runtime/threadSMR.inline.hpp"
#include "runtime/vmOperations.hpp"
#include "runtime/vmThread.hpp"
#include "services/threadService.hpp"
#include "utilities/growableArray.hpp"
#include "utilities/ticks.hpp"
#define VM_OP_NAME_INITIALIZE(name) #name,
const char* VM_Operation::_names[VM_Operation::VMOp_Terminating] = \
{ VM_OPS_DO(VM_OP_NAME_INITIALIZE) };
void VM_Operation::set_calling_thread(Thread* thread) {
_calling_thread = thread;
}
void VM_Operation::evaluate() {
ResourceMark rm;
LogTarget(Debug, vmoperation) lt;
if (lt.is_enabled()) {
LogStream ls(lt);
ls.print("begin ");
print_on_error(&ls);
ls.cr();
}
doit();
if (lt.is_enabled()) {
LogStream ls(lt);
ls.print("end ");
print_on_error(&ls);
ls.cr();
}
}
// Called by fatal error handler.
void VM_Operation::print_on_error(outputStream* st) const {
st->print("VM_Operation (" PTR_FORMAT "): ", p2i(this));
st->print("%s", name());
st->print(", mode: %s", evaluate_at_safepoint() ? "safepoint" : "no safepoint");
if (calling_thread()) {
st->print(", requested by thread " PTR_FORMAT, p2i(calling_thread()));
}
}
void VM_ClearICs::doit() {
if (_preserve_static_stubs) {
CodeCache::cleanup_inline_caches_whitebox();
} else {
CodeCache::clear_inline_caches();
}
}
void VM_CleanClassLoaderDataMetaspaces::doit() {
ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces();
}
void VM_RehashStringTable::doit() {
StringTable::rehash_table();
}
void VM_RehashSymbolTable::doit() {
SymbolTable::rehash_table();
}
VM_DeoptimizeFrame::VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id, int reason) {
_thread = thread;
_id = id;
_reason = reason;
}
void VM_DeoptimizeFrame::doit() {
assert(_reason > Deoptimization::Reason_none && _reason < Deoptimization::Reason_LIMIT, "invalid deopt reason");
Deoptimization::deoptimize_frame_internal(_thread, _id, (Deoptimization::DeoptReason)_reason);
}
#ifndef PRODUCT
void VM_DeoptimizeAll::doit() {
JavaThreadIteratorWithHandle jtiwh;
// deoptimize all java threads in the system
if (DeoptimizeALot) {
for (; JavaThread *thread = jtiwh.next(); ) {
if (thread->has_last_Java_frame()) {
thread->deoptimize();
}
}
} else if (DeoptimizeRandom) {
// Deoptimize some selected threads and frames
int tnum = os::random() & 0x3;
int fnum = os::random() & 0x3;
int tcount = 0;
for (; JavaThread *thread = jtiwh.next(); ) {
if (thread->has_last_Java_frame()) {
if (tcount++ == tnum) {
tcount = 0;
int fcount = 0;
// Deoptimize some selected frames.
for(StackFrameStream fst(thread, false /* update */, true /* process_frames */); !fst.is_done(); fst.next()) {
if (fst.current()->can_be_deoptimized()) {
if (fcount++ == fnum) {
fcount = 0;
Deoptimization::deoptimize(thread, *fst.current());
}
}
}
}
}
}
}
}
void VM_ZombieAll::doit() {
JavaThread::cast(calling_thread())->make_zombies();
}
#endif // !PRODUCT
bool VM_PrintThreads::doit_prologue() {
// Get Heap_lock if concurrent locks will be dumped
if (_print_concurrent_locks) {
Heap_lock->lock();
}
return true;
}
void VM_PrintThreads::doit() {
Threads::print_on(_out, true, false, _print_concurrent_locks, _print_extended_info);
if (_print_jni_handle_info) {
JNIHandles::print_on(_out);
}
}
void VM_PrintThreads::doit_epilogue() {
if (_print_concurrent_locks) {
// Release Heap_lock
Heap_lock->unlock();
}
}
void VM_PrintMetadata::doit() {
metaspace::MetaspaceReporter::print_report(_out, _scale, _flags);
}
VM_FindDeadlocks::~VM_FindDeadlocks() {
if (_deadlocks != nullptr) {
DeadlockCycle* cycle = _deadlocks;
while (cycle != nullptr) {
DeadlockCycle* d = cycle;
cycle = cycle->next();
delete d;
}
}
}
void VM_FindDeadlocks::doit() {
// Update the hazard ptr in the originating thread to the current
// list of threads. This VM operation needs the current list of
// threads for proper deadlock detection and those are the
// JavaThreads we need to be protected when we return info to the
// originating thread.
_setter.set();
_deadlocks = ThreadService::find_deadlocks_at_safepoint(_setter.list(), _concurrent_locks);
if (_out != nullptr) {
int num_deadlocks = 0;
for (DeadlockCycle* cycle = _deadlocks; cycle != nullptr; cycle = cycle->next()) {
num_deadlocks++;
cycle->print_on_with(_setter.list(), _out);
}
if (num_deadlocks == 1) {
_out->print_cr("\nFound 1 deadlock.\n");
_out->flush();
} else if (num_deadlocks > 1) {
_out->print_cr("\nFound %d deadlocks.\n", num_deadlocks);
_out->flush();
}
}
}
VM_ThreadDump::VM_ThreadDump(ThreadDumpResult* result,
int max_depth,
bool with_locked_monitors,
bool with_locked_synchronizers) {
_result = result;
_num_threads = 0; // 0 indicates all threads
_threads = nullptr;
_max_depth = max_depth;
_with_locked_monitors = with_locked_monitors;
_with_locked_synchronizers = with_locked_synchronizers;
}
VM_ThreadDump::VM_ThreadDump(ThreadDumpResult* result,
GrowableArray<instanceHandle>* threads,
int num_threads,
int max_depth,
bool with_locked_monitors,
bool with_locked_synchronizers) {
_result = result;
_num_threads = num_threads;
_threads = threads;
_max_depth = max_depth;
_with_locked_monitors = with_locked_monitors;
_with_locked_synchronizers = with_locked_synchronizers;
}
bool VM_ThreadDump::doit_prologue() {
if (_with_locked_synchronizers) {
// Acquire Heap_lock to dump concurrent locks
Heap_lock->lock();
}
return true;
}
void VM_ThreadDump::doit_epilogue() {
if (_with_locked_synchronizers) {
// Release Heap_lock
Heap_lock->unlock();
}
}
// Hash table of int64_t to a list of ObjectMonitor* owned by the JavaThread.
class ObjectMonitorsDump : public MonitorClosure, public ObjectMonitorsView {
private:
static unsigned int ptr_hash(int64_t const& s1) {
// 2654435761 = 2^32 * Phi (golden ratio)
return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
}
private:
using ObjectMonitorList = GrowableArrayCHeap<ObjectMonitor*, mtThread>;
// HashTable SIZE is specified at compile time so we
// use 1031 which is the first prime after 1024.
typedef HashTable<int64_t, ObjectMonitorList, 1031, AnyObj::C_HEAP, mtThread,
&ObjectMonitorsDump::ptr_hash> PtrTable;
PtrTable* _ptrs;
size_t _key_count;
size_t _om_count;
void add(ObjectMonitor* monitor) {
int64_t key = monitor->owner();
bool created = false;
ObjectMonitorList* list = _ptrs->put_if_absent(key, &created);
if (created) {
_key_count++;
}
assert(list->find(monitor) == -1, "Should not contain duplicates");
list->push(monitor); // Add the ObjectMonitor to the list.
_om_count++;
}
public:
// HashTable is passed to various functions and populated in
// different places so we allocate it using C_HEAP to make it immune
// from any ResourceMarks that happen to be in the code paths.
ObjectMonitorsDump() : _ptrs(new (mtThread) PtrTable), _key_count(0), _om_count(0) {}
~ObjectMonitorsDump() {
delete _ptrs;
}
// Implements MonitorClosure used to collect all owned monitors in the system
void do_monitor(ObjectMonitor* monitor) override {
assert(monitor->has_owner(), "Expects only owned monitors");
if (monitor->has_anonymous_owner()) {
// There's no need to collect anonymous owned monitors
// because the caller of this code is only interested
// in JNI owned monitors.
return;
}
if (monitor->object_peek() == nullptr) {
// JNI code doesn't necessarily keep the monitor object
// alive. Filter out monitors with dead objects.
return;
}
add(monitor);
}
// Implements the ObjectMonitorsView interface
void visit(MonitorClosure* closure, JavaThread* thread) override {
int64_t key = ObjectMonitor::owner_id_from(thread);
ObjectMonitorList* list = _ptrs->get(key);
if (list == nullptr) {
return;
}
for (int i = 0; i < list->length(); i++) {
closure->do_monitor(list->at(i));
}
}
size_t key_count() { return _key_count; }
size_t om_count() { return _om_count; }
};
void VM_ThreadDump::doit() {
ResourceMark rm;
// Set the hazard ptr in the originating thread to protect the
// current list of threads. This VM operation needs the current list
// of threads for a proper dump and those are the JavaThreads we need
// to be protected when we return info to the originating thread.
_result->set_t_list();
ConcurrentLocksDump concurrent_locks(true);
if (_with_locked_synchronizers) {
concurrent_locks.dump_at_safepoint();
}
ObjectMonitorsDump object_monitors;
if (_with_locked_monitors) {
// Gather information about owned monitors.
ObjectSynchronizer::owned_monitors_iterate(&object_monitors);
// If there are many object monitors in the system then the above iteration
// can start to take time. Be friendly to following thread dumps by telling
// the MonitorDeflationThread to deflate monitors.
//
// This is trying to be somewhat backwards compatible with the previous
// implementation, which performed monitor deflation right here. We might
// want to reconsider the need to trigger monitor deflation from the thread
// dumping and instead maybe tweak the deflation heuristics.
ObjectSynchronizer::request_deflate_idle_monitors();
}
if (_num_threads == 0) {
// Snapshot all live threads
for (uint i = 0; i < _result->t_list()->length(); i++) {
JavaThread* jt = _result->t_list()->thread_at(i);
if (jt->is_exiting() ||
jt->is_hidden_from_external_view()) {
// skip terminating threads and hidden threads
continue;
}
ThreadConcurrentLocks* tcl = nullptr;
if (_with_locked_synchronizers) {
tcl = concurrent_locks.thread_concurrent_locks(jt);
}
snapshot_thread(jt, tcl, &object_monitors);
}
} else {
// Snapshot threads in the given _threads array
// A dummy snapshot is created if a thread doesn't exist
for (int i = 0; i < _num_threads; i++) {
instanceHandle th = _threads->at(i);
if (th() == nullptr) {
// skip if the thread doesn't exist
// Add a dummy snapshot
_result->add_thread_snapshot();
continue;
}
// Dump thread stack only if the thread is alive and not exiting
// and not VM internal thread.
JavaThread* jt = java_lang_Thread::thread(th());
if (jt != nullptr && !_result->t_list()->includes(jt)) {
// _threads[i] doesn't refer to a valid JavaThread; this check
// is primarily for JVM_DumpThreads() which doesn't have a good
// way to validate the _threads array.
jt = nullptr;
}
if (jt == nullptr || /* thread not alive */
jt->is_exiting() ||
jt->is_hidden_from_external_view()) {
// add a null snapshot if skipped
_result->add_thread_snapshot();
continue;
}
ThreadConcurrentLocks* tcl = nullptr;
if (_with_locked_synchronizers) {
tcl = concurrent_locks.thread_concurrent_locks(jt);
}
snapshot_thread(jt, tcl, &object_monitors);
}
}
}
void VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl,
ObjectMonitorsView* monitors) {
ThreadSnapshot* snapshot = _result->add_thread_snapshot(java_thread);
snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors, monitors, false);
snapshot->set_concurrent_locks(tcl);
}
volatile bool VM_Exit::_vm_exited = false;
Thread * volatile VM_Exit::_shutdown_thread = nullptr;
int VM_Exit::set_vm_exited() {
Thread * thr_cur = Thread::current();
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
int num_active = 0;
_shutdown_thread = thr_cur;
_vm_exited = true; // global flag
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
if (thr != thr_cur && thr->thread_state() == _thread_in_native) {
++num_active;
thr->set_terminated(JavaThread::_vm_exited); // per-thread flag
}
}
return num_active;
}
int VM_Exit::wait_for_threads_in_native_to_block() {
// VM exits at safepoint. This function must be called at the final safepoint
// to wait for threads in _thread_in_native state to be quiescent.
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
Thread * thr_cur = Thread::current();
// Compiler threads need longer wait because they can access VM data directly
// while in native. If they are active and some structures being used are
// deleted by the shutdown sequence, they will crash. On the other hand, user
// threads must go through native=>Java/VM transitions first to access VM
// data, and they will be stopped during state transition. In theory, we
// don't have to wait for user threads to be quiescent, but it's always
// better to terminate VM when current thread is the only active thread, so
// wait for user threads too.
// Time per attempt. It is practical to start waiting with 10us delays
// (around scheduling delay / timer slack), and exponentially ramp up
// to 10ms if compiler threads are not responding.
jlong max_wait_time = millis_to_nanos(10);
jlong wait_time = 10000;
jlong start_time = os::javaTimeNanos();
// Deadline for user threads in native code.
// User-settable flag counts "attempts" in 10ms units, to a maximum of 10s.
jlong user_threads_deadline = start_time + (UserThreadWaitAttemptsAtExit * millis_to_nanos(10));
// Deadline for compiler threads: at least 10 seconds.
jlong compiler_threads_deadline = start_time + millis_to_nanos(10000);
JavaThreadIteratorWithHandle jtiwh;
while (true) {
int num_active = 0;
int num_active_compiler_thread = 0;
jtiwh.rewind();
for (; JavaThread *thr = jtiwh.next(); ) {
if (thr!=thr_cur && thr->thread_state() == _thread_in_native) {
num_active++;
if (thr->is_Compiler_thread()) {
num_active_compiler_thread++;
}
}
}
jlong time = os::javaTimeNanos();
if (num_active == 0) {
return 0;
}
if (time >= compiler_threads_deadline) {
return num_active;
}
if ((num_active_compiler_thread == 0) && (time >= user_threads_deadline)) {
return num_active;
}
os::naked_short_nanosleep(wait_time);
wait_time = MIN2(max_wait_time, wait_time * 2);
}
}
void VM_Exit::doit() {
if (VerifyBeforeExit) {
HandleMark hm(VMThread::vm_thread());
// Among other things, this ensures that Eden top is correct.
Universe::heap()->prepare_for_verify();
// Silent verification so as not to pollute normal output,
// unless we really asked for it.
Universe::verify();
}
CompileBroker::set_should_block();
// Wait for a short period for threads in native to block. Any thread
// still executing native code after the wait will be stopped at
// native==>Java/VM barriers.
// Among 16276 JCK tests, 94% of them come here without any threads still
// running in native; the other 6% are quiescent within 250ms (Ultra 80).
wait_for_threads_in_native_to_block();
set_vm_exited();
// The ObjectMonitor subsystem uses perf counters so do this before
// we call exit_globals() so we don't run afoul of perfMemory_exit().
ObjectSynchronizer::do_final_audit_and_print_stats();
// We'd like to call IdealGraphPrinter::clean_up() to finalize the
// XML logging, but we can't safely do that here. The logic to make
// XML termination logging safe is tied to the termination of the
// VMThread, and it doesn't terminate on this exit path. See 8222534.
// cleanup globals resources before exiting. exit_globals() currently
// cleans up outputStream resources and PerfMemory resources.
exit_globals();
LogConfiguration::finalize();
// Check for exit hook
exit_hook_t exit_hook = Arguments::exit_hook();
if (exit_hook != nullptr) {
// exit hook should exit.
exit_hook(_exit_code);
// ... but if it didn't, we must do it here
vm_direct_exit(_exit_code);
} else {
vm_direct_exit(_exit_code);
}
}
void VM_Exit::wait_if_vm_exited() {
if (_vm_exited) {
// Need to check for an unattached thread as only attached threads
// can acquire the lock.
Thread* current = Thread::current_or_null();
if (current != nullptr && current != _shutdown_thread) {
// _vm_exited is set at safepoint, and the Threads_lock is never released
// so we will block here until the process dies.
Threads_lock->lock();
ShouldNotReachHere();
}
}
}
void VM_PrintCompileQueue::doit() {
CompileBroker::print_compile_queues(_out);
}
#if INCLUDE_SERVICES
void VM_PrintClassHierarchy::doit() {
KlassHierarchy::print_class_hierarchy(_out, _print_interfaces, _print_subclasses, _classname);
}
#endif