< prev index next >

src/hotspot/share/runtime/vmOperations.cpp

Print this page

 26 #include "classfile/classLoaderDataGraph.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "code/codeCache.hpp"
 29 #include "compiler/compileBroker.hpp"
 30 #include "gc/shared/collectedHeap.hpp"
 31 #include "gc/shared/isGCActiveMark.hpp"
 32 #include "logging/log.hpp"
 33 #include "logging/logStream.hpp"
 34 #include "logging/logConfiguration.hpp"
 35 #include "memory/heapInspection.hpp"
 36 #include "memory/metaspace/metaspaceReporter.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "memory/universe.hpp"
 39 #include "oops/symbol.hpp"
 40 #include "runtime/arguments.hpp"
 41 #include "runtime/deoptimization.hpp"
 42 #include "runtime/frame.inline.hpp"
 43 #include "runtime/interfaceSupport.inline.hpp"
 44 #include "runtime/javaThread.inline.hpp"
 45 #include "runtime/jniHandles.hpp"

 46 #include "runtime/stackFrameStream.inline.hpp"
 47 #include "runtime/synchronizer.hpp"
 48 #include "runtime/threads.hpp"
 49 #include "runtime/threadSMR.inline.hpp"
 50 #include "runtime/vmOperations.hpp"
 51 #include "services/threadService.hpp"

 52 
 53 #define VM_OP_NAME_INITIALIZE(name) #name,
 54 
 55 const char* VM_Operation::_names[VM_Operation::VMOp_Terminating] = \
 56   { VM_OPS_DO(VM_OP_NAME_INITIALIZE) };
 57 
 58 void VM_Operation::set_calling_thread(Thread* thread) {
 59   _calling_thread = thread;
 60 }
 61 
 62 void VM_Operation::evaluate() {
 63   ResourceMark rm;
 64   LogTarget(Debug, vmoperation) lt;
 65   if (lt.is_enabled()) {
 66     LogStream ls(lt);
 67     ls.print("begin ");
 68     print_on_error(&ls);
 69     ls.cr();
 70   }
 71   doit();

248   _with_locked_monitors = with_locked_monitors;
249   _with_locked_synchronizers = with_locked_synchronizers;
250 }
251 
252 bool VM_ThreadDump::doit_prologue() {
253   if (_with_locked_synchronizers) {
254     // Acquire Heap_lock to dump concurrent locks
255     Heap_lock->lock();
256   }
257 
258   return true;
259 }
260 
261 void VM_ThreadDump::doit_epilogue() {
262   if (_with_locked_synchronizers) {
263     // Release Heap_lock
264     Heap_lock->unlock();
265   }
266 }
267 









































































































268 void VM_ThreadDump::doit() {
269   ResourceMark rm;
270 
271   // Set the hazard ptr in the originating thread to protect the
272   // current list of threads. This VM operation needs the current list
273   // of threads for a proper dump and those are the JavaThreads we need
274   // to be protected when we return info to the originating thread.
275   _result->set_t_list();
276 
277   ConcurrentLocksDump concurrent_locks(true);
278   if (_with_locked_synchronizers) {
279     concurrent_locks.dump_at_safepoint();
280   }
281 
282   ObjectMonitorsHashtable table;
283   ObjectMonitorsHashtable* tablep = nullptr;
284   if (_with_locked_monitors) {
285     // The caller wants locked monitor information and that's expensive to gather
286     // when there are a lot of inflated monitors. So we deflate idle monitors and
287     // gather information about owned monitors at the same time.
288     tablep = &table;
289     while (ObjectSynchronizer::deflate_idle_monitors(tablep) > 0) {
290       ; /* empty */
291     }





292   }
293 
294   if (_num_threads == 0) {
295     // Snapshot all live threads
296 
297     for (uint i = 0; i < _result->t_list()->length(); i++) {
298       JavaThread* jt = _result->t_list()->thread_at(i);
299       if (jt->is_exiting() ||
300           jt->is_hidden_from_external_view())  {
301         // skip terminating threads and hidden threads
302         continue;
303       }
304       ThreadConcurrentLocks* tcl = nullptr;
305       if (_with_locked_synchronizers) {
306         tcl = concurrent_locks.thread_concurrent_locks(jt);
307       }
308       snapshot_thread(jt, tcl, tablep);
309     }
310   } else {
311     // Snapshot threads in the given _threads array
312     // A dummy snapshot is created if a thread doesn't exist
313 
314     for (int i = 0; i < _num_threads; i++) {
315       instanceHandle th = _threads->at(i);
316       if (th() == nullptr) {
317         // skip if the thread doesn't exist
318         // Add a dummy snapshot
319         _result->add_thread_snapshot();
320         continue;
321       }
322 
323       // Dump thread stack only if the thread is alive and not exiting
324       // and not VM internal thread.
325       JavaThread* jt = java_lang_Thread::thread(th());
326       if (jt != nullptr && !_result->t_list()->includes(jt)) {
327         // _threads[i] doesn't refer to a valid JavaThread; this check
328         // is primarily for JVM_DumpThreads() which doesn't have a good
329         // way to validate the _threads array.
330         jt = nullptr;
331       }
332       if (jt == nullptr || /* thread not alive */
333           jt->is_exiting() ||
334           jt->is_hidden_from_external_view())  {
335         // add a null snapshot if skipped
336         _result->add_thread_snapshot();
337         continue;
338       }
339       ThreadConcurrentLocks* tcl = nullptr;
340       if (_with_locked_synchronizers) {
341         tcl = concurrent_locks.thread_concurrent_locks(jt);
342       }
343       snapshot_thread(jt, tcl, tablep);
344     }
345   }
346 }
347 
348 void VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl,
349                                     ObjectMonitorsHashtable* table) {
350   ThreadSnapshot* snapshot = _result->add_thread_snapshot(java_thread);
351   snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors, table, false);
352   snapshot->set_concurrent_locks(tcl);
353 }
354 
355 volatile bool VM_Exit::_vm_exited = false;
356 Thread * volatile VM_Exit::_shutdown_thread = nullptr;
357 
358 int VM_Exit::set_vm_exited() {
359 
360   Thread * thr_cur = Thread::current();
361 
362   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
363 
364   int num_active = 0;
365 
366   _shutdown_thread = thr_cur;
367   _vm_exited = true;                                // global flag
368   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
369     if (thr != thr_cur && thr->thread_state() == _thread_in_native) {
370       ++num_active;
371       thr->set_terminated(JavaThread::_vm_exited);  // per-thread flag

 26 #include "classfile/classLoaderDataGraph.hpp"
 27 #include "classfile/vmSymbols.hpp"
 28 #include "code/codeCache.hpp"
 29 #include "compiler/compileBroker.hpp"
 30 #include "gc/shared/collectedHeap.hpp"
 31 #include "gc/shared/isGCActiveMark.hpp"
 32 #include "logging/log.hpp"
 33 #include "logging/logStream.hpp"
 34 #include "logging/logConfiguration.hpp"
 35 #include "memory/heapInspection.hpp"
 36 #include "memory/metaspace/metaspaceReporter.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "memory/universe.hpp"
 39 #include "oops/symbol.hpp"
 40 #include "runtime/arguments.hpp"
 41 #include "runtime/deoptimization.hpp"
 42 #include "runtime/frame.inline.hpp"
 43 #include "runtime/interfaceSupport.inline.hpp"
 44 #include "runtime/javaThread.inline.hpp"
 45 #include "runtime/jniHandles.hpp"
 46 #include "runtime/objectMonitor.inline.hpp"
 47 #include "runtime/stackFrameStream.inline.hpp"
 48 #include "runtime/synchronizer.hpp"
 49 #include "runtime/threads.hpp"
 50 #include "runtime/threadSMR.inline.hpp"
 51 #include "runtime/vmOperations.hpp"
 52 #include "services/threadService.hpp"
 53 #include "utilities/ticks.hpp"
 54 
 55 #define VM_OP_NAME_INITIALIZE(name) #name,
 56 
 57 const char* VM_Operation::_names[VM_Operation::VMOp_Terminating] = \
 58   { VM_OPS_DO(VM_OP_NAME_INITIALIZE) };
 59 
 60 void VM_Operation::set_calling_thread(Thread* thread) {
 61   _calling_thread = thread;
 62 }
 63 
 64 void VM_Operation::evaluate() {
 65   ResourceMark rm;
 66   LogTarget(Debug, vmoperation) lt;
 67   if (lt.is_enabled()) {
 68     LogStream ls(lt);
 69     ls.print("begin ");
 70     print_on_error(&ls);
 71     ls.cr();
 72   }
 73   doit();

250   _with_locked_monitors = with_locked_monitors;
251   _with_locked_synchronizers = with_locked_synchronizers;
252 }
253 
254 bool VM_ThreadDump::doit_prologue() {
255   if (_with_locked_synchronizers) {
256     // Acquire Heap_lock to dump concurrent locks
257     Heap_lock->lock();
258   }
259 
260   return true;
261 }
262 
263 void VM_ThreadDump::doit_epilogue() {
264   if (_with_locked_synchronizers) {
265     // Release Heap_lock
266     Heap_lock->unlock();
267   }
268 }
269 
270 // Hash table of void* to a list of ObjectMonitor* owned by the JavaThread.
271 // The JavaThread's owner key is either a JavaThread* or a stack lock
272 // address in the JavaThread so we use "void*".
273 //
274 class ObjectMonitorsDump : public MonitorClosure, public ObjectMonitorsView {
275  private:
276   static unsigned int ptr_hash(void* const& s1) {
277     // 2654435761 = 2^32 * Phi (golden ratio)
278     return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
279   }
280 
281  private:
282   class ObjectMonitorLinkedList :
283     public LinkedListImpl<ObjectMonitor*,
284                           AnyObj::C_HEAP, mtThread,
285                           AllocFailStrategy::RETURN_NULL> {};
286 
287   // ResourceHashtable SIZE is specified at compile time so we
288   // use 1031 which is the first prime after 1024.
289   typedef ResourceHashtable<void*, ObjectMonitorLinkedList*, 1031, AnyObj::C_HEAP, mtThread,
290                             &ObjectMonitorsDump::ptr_hash> PtrTable;
291   PtrTable* _ptrs;
292   size_t _key_count;
293   size_t _om_count;
294 
295   void add_list(void* key, ObjectMonitorLinkedList* list) {
296     _ptrs->put(key, list);
297     _key_count++;
298   }
299 
300   ObjectMonitorLinkedList* get_list(void* key) {
301     ObjectMonitorLinkedList** listpp = _ptrs->get(key);
302     return (listpp == nullptr) ? nullptr : *listpp;
303   }
304 
305   void add(ObjectMonitor* monitor) {
306     void* key = monitor->owner();
307 
308     ObjectMonitorLinkedList* list = get_list(key);
309     if (list == nullptr) {
310       // Create new list and add it to the hash table:
311       list = new (mtThread) ObjectMonitorLinkedList;
312       _ptrs->put(key, list);
313       _key_count++;
314     }
315 
316     assert(list->find(monitor) == nullptr, "Should not contain duplicates");
317     list->add(monitor);  // Add the ObjectMonitor to the list.
318     _om_count++;
319   }
320 
321  public:
322   // ResourceHashtable is passed to various functions and populated in
323   // different places so we allocate it using C_HEAP to make it immune
324   // from any ResourceMarks that happen to be in the code paths.
325   ObjectMonitorsDump() : _ptrs(new (mtThread) PtrTable), _key_count(0), _om_count(0) {}
326 
327   ~ObjectMonitorsDump() {
328     class CleanupObjectMonitorsDump: StackObj {
329      public:
330       bool do_entry(void*& key, ObjectMonitorLinkedList*& list) {
331         list->clear();  // clear the LinkListNodes
332         delete list;    // then delete the LinkedList
333         return true;
334       }
335     } cleanup;
336 
337     _ptrs->unlink(&cleanup);  // cleanup the LinkedLists
338     delete _ptrs;             // then delete the hash table
339   }
340 
341   // Implements MonitorClosure used to collect all owned monitors in the system
342   void do_monitor(ObjectMonitor* monitor) override {
343     assert(monitor->has_owner(), "Expects only owned monitors");
344 
345     if (monitor->is_owner_anonymous()) {
346       // There's no need to collect anonymous owned monitors
347       // because the caller of this code is only interested
348       // in JNI owned monitors.
349       return;
350     }
351 
352     if (monitor->object_peek() == nullptr) {
353       // JNI code doesn't necessarily keep the monitor object
354       // alive. Filter out monitors with dead objects.
355       return;
356     }
357 
358     add(monitor);
359   }
360 
361   // Implements the ObjectMonitorsView interface
362   void visit(MonitorClosure* closure, JavaThread* thread) override {
363     ObjectMonitorLinkedList* list = get_list(thread);
364     LinkedListIterator<ObjectMonitor*> iter(list != nullptr ? list->head() : nullptr);
365     while (!iter.is_empty()) {
366       ObjectMonitor* monitor = *iter.next();
367       closure->do_monitor(monitor);
368     }
369   }
370 
371   size_t key_count() { return _key_count; }
372   size_t om_count() { return _om_count; }
373 };
374 
375 void VM_ThreadDump::doit() {
376   ResourceMark rm;
377 
378   // Set the hazard ptr in the originating thread to protect the
379   // current list of threads. This VM operation needs the current list
380   // of threads for a proper dump and those are the JavaThreads we need
381   // to be protected when we return info to the originating thread.
382   _result->set_t_list();
383 
384   ConcurrentLocksDump concurrent_locks(true);
385   if (_with_locked_synchronizers) {
386     concurrent_locks.dump_at_safepoint();
387   }
388 
389   ObjectMonitorsDump object_monitors;

390   if (_with_locked_monitors) {
391     // Gather information about owned monitors.
392     ObjectSynchronizer::owned_monitors_iterate(&object_monitors);
393 
394     // If there are many object monitors in the system then the above iteration
395     // can start to take time. Be friendly to following thread dumps by telling
396     // the MonitorDeflationThread to deflate monitors.
397     //
398     // This is trying to be somewhat backwards compatible with the previous
399     // implementation, which performed monitor deflation right here. We might
400     // want to reconsider the need to trigger monitor deflation from the thread
401     // dumping and instead maybe tweak the deflation heuristics.
402     ObjectSynchronizer::request_deflate_idle_monitors();
403   }
404 
405   if (_num_threads == 0) {
406     // Snapshot all live threads
407 
408     for (uint i = 0; i < _result->t_list()->length(); i++) {
409       JavaThread* jt = _result->t_list()->thread_at(i);
410       if (jt->is_exiting() ||
411           jt->is_hidden_from_external_view())  {
412         // skip terminating threads and hidden threads
413         continue;
414       }
415       ThreadConcurrentLocks* tcl = nullptr;
416       if (_with_locked_synchronizers) {
417         tcl = concurrent_locks.thread_concurrent_locks(jt);
418       }
419       snapshot_thread(jt, tcl, &object_monitors);
420     }
421   } else {
422     // Snapshot threads in the given _threads array
423     // A dummy snapshot is created if a thread doesn't exist
424 
425     for (int i = 0; i < _num_threads; i++) {
426       instanceHandle th = _threads->at(i);
427       if (th() == nullptr) {
428         // skip if the thread doesn't exist
429         // Add a dummy snapshot
430         _result->add_thread_snapshot();
431         continue;
432       }
433 
434       // Dump thread stack only if the thread is alive and not exiting
435       // and not VM internal thread.
436       JavaThread* jt = java_lang_Thread::thread(th());
437       if (jt != nullptr && !_result->t_list()->includes(jt)) {
438         // _threads[i] doesn't refer to a valid JavaThread; this check
439         // is primarily for JVM_DumpThreads() which doesn't have a good
440         // way to validate the _threads array.
441         jt = nullptr;
442       }
443       if (jt == nullptr || /* thread not alive */
444           jt->is_exiting() ||
445           jt->is_hidden_from_external_view())  {
446         // add a null snapshot if skipped
447         _result->add_thread_snapshot();
448         continue;
449       }
450       ThreadConcurrentLocks* tcl = nullptr;
451       if (_with_locked_synchronizers) {
452         tcl = concurrent_locks.thread_concurrent_locks(jt);
453       }
454       snapshot_thread(jt, tcl, &object_monitors);
455     }
456   }
457 }
458 
459 void VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl,
460                                     ObjectMonitorsView* monitors) {
461   ThreadSnapshot* snapshot = _result->add_thread_snapshot(java_thread);
462   snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors, monitors, false);
463   snapshot->set_concurrent_locks(tcl);
464 }
465 
466 volatile bool VM_Exit::_vm_exited = false;
467 Thread * volatile VM_Exit::_shutdown_thread = nullptr;
468 
469 int VM_Exit::set_vm_exited() {
470 
471   Thread * thr_cur = Thread::current();
472 
473   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already");
474 
475   int num_active = 0;
476 
477   _shutdown_thread = thr_cur;
478   _vm_exited = true;                                // global flag
479   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *thr = jtiwh.next(); ) {
480     if (thr != thr_cur && thr->thread_state() == _thread_in_native) {
481       ++num_active;
482       thr->set_terminated(JavaThread::_vm_exited);  // per-thread flag
< prev index next >