< prev index next >

src/os/windows/vm/os_windows.cpp

Print this page
rev 10257 : 8149036: Add tracing for thread related events at os level
Reviewed-by: coleenp


  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 // Must be at least Windows Vista or Server 2008 to use InitOnceExecuteOnce
  26 #define _WIN32_WINNT 0x0600
  27 
  28 // no precompiled headers
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/disassembler.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "jvm_windows.h"

  38 #include "memory/allocation.inline.hpp"
  39 #include "memory/filemap.hpp"
  40 #include "mutex_windows.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "os_share_windows.hpp"
  43 #include "os_windows.inline.hpp"
  44 #include "prims/jniFastGetField.hpp"
  45 #include "prims/jvm.h"
  46 #include "prims/jvm_misc.hpp"
  47 #include "runtime/arguments.hpp"
  48 #include "runtime/atomic.inline.hpp"
  49 #include "runtime/extendedPC.hpp"
  50 #include "runtime/globals.hpp"
  51 #include "runtime/interfaceSupport.hpp"
  52 #include "runtime/java.hpp"
  53 #include "runtime/javaCalls.hpp"
  54 #include "runtime/mutexLocker.hpp"
  55 #include "runtime/objectMonitor.hpp"
  56 #include "runtime/orderAccess.inline.hpp"
  57 #include "runtime/osThread.hpp"


 419   _alloca(((pid ^ counter++) & 7) * 128);
 420 
 421   thread->initialize_thread_current();
 422 
 423   OSThread* osthr = thread->osthread();
 424   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 425 
 426   if (UseNUMA) {
 427     int lgrp_id = os::numa_get_group_id();
 428     if (lgrp_id != -1) {
 429       thread->set_lgrp_id(lgrp_id);
 430     }
 431   }
 432 
 433   // Diagnostic code to investigate JDK-6573254
 434   int res = 30115;  // non-java thread
 435   if (thread->is_Java_thread()) {
 436     res = 20115;    // java thread
 437   }
 438 


 439   // Install a win32 structured exception handler around every thread created
 440   // by VM, so VM can generate error dump when an exception occurred in non-
 441   // Java thread (e.g. VM thread).
 442   __try {
 443     thread->run();
 444   } __except(topLevelExceptionFilter(
 445                                      (_EXCEPTION_POINTERS*)_exception_info())) {
 446     // Nothing to do.
 447   }
 448 


 449   // One less thread is executing
 450   // When the VMThread gets here, the main thread may have already exited
 451   // which frees the CodeHeap containing the Atomic::add code
 452   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
 453     Atomic::dec_ptr((intptr_t*)&os::win32::_os_thread_count);
 454   }
 455 
 456   // Thread must not return from exit_process_or_thread(), but if it does,
 457   // let it proceed to exit normally
 458   return (unsigned)os::win32::exit_process_or_thread(os::win32::EPT_THREAD, res);
 459 }
 460 
 461 static OSThread* create_os_thread(Thread* thread, HANDLE thread_handle,
 462                                   int thread_id) {
 463   // Allocate the OSThread object
 464   OSThread* osthread = new OSThread(NULL, NULL);
 465   if (osthread == NULL) return NULL;
 466 
 467   // Initialize support for Java interrupts
 468   HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);


 492 
 493 bool os::create_attached_thread(JavaThread* thread) {
 494 #ifdef ASSERT
 495   thread->verify_not_published();
 496 #endif
 497   HANDLE thread_h;
 498   if (!DuplicateHandle(main_process, GetCurrentThread(), GetCurrentProcess(),
 499                        &thread_h, THREAD_ALL_ACCESS, false, 0)) {
 500     fatal("DuplicateHandle failed\n");
 501   }
 502   OSThread* osthread = create_os_thread(thread, thread_h,
 503                                         (int)current_thread_id());
 504   if (osthread == NULL) {
 505     return false;
 506   }
 507 
 508   // Initial thread state is RUNNABLE
 509   osthread->set_state(RUNNABLE);
 510 
 511   thread->set_osthread(osthread);




 512   return true;
 513 }
 514 
 515 bool os::create_main_thread(JavaThread* thread) {
 516 #ifdef ASSERT
 517   thread->verify_not_published();
 518 #endif
 519   if (_starting_thread == NULL) {
 520     _starting_thread = create_os_thread(thread, main_thread, main_thread_id);
 521     if (_starting_thread == NULL) {
 522       return false;
 523     }
 524   }
 525 
 526   // The primordial thread is runnable from the start)
 527   _starting_thread->set_state(RUNNABLE);
 528 
 529   thread->set_osthread(_starting_thread);
 530   return true;
 531 }


 588   // commitment. On the other hand, specifying 'stack_size' larger than
 589   // default value may cause significant increase in memory usage, because
 590   // not only the stack space will be rounded up to MB, but also the
 591   // entire space is committed upfront.
 592   //
 593   // Finally Windows XP added a new flag 'STACK_SIZE_PARAM_IS_A_RESERVATION'
 594   // for CreateThread() that can treat 'stack_size' as stack size. However we
 595   // are not supposed to call CreateThread() directly according to MSDN
 596   // document because JVM uses C runtime library. The good news is that the
 597   // flag appears to work with _beginthredex() as well.
 598 
 599   HANDLE thread_handle =
 600     (HANDLE)_beginthreadex(NULL,
 601                            (unsigned)stack_size,
 602                            (unsigned (__stdcall *)(void*)) java_start,
 603                            thread,
 604                            CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
 605                            &thread_id);
 606 
 607   if (thread_handle == NULL) {



 608     // Need to clean up stuff we've allocated so far
 609     CloseHandle(osthread->interrupt_event());
 610     thread->set_osthread(NULL);
 611     delete osthread;
 612     return NULL;


 613   }
 614 
 615   Atomic::inc_ptr((intptr_t*)&os::win32::_os_thread_count);
 616 
 617   // Store info on the Win32 thread into the OSThread
 618   osthread->set_thread_handle(thread_handle);
 619   osthread->set_thread_id(thread_id);
 620 
 621   // Initial thread state is INITIALIZED, not SUSPENDED
 622   osthread->set_state(INITIALIZED);
 623 
 624   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
 625   return true;
 626 }
 627 
 628 
 629 // Free Win32 resources related to the OSThread
 630 void os::free_thread(OSThread* osthread) {
 631   assert(osthread != NULL, "osthread not set");
 632   CloseHandle(osthread->thread_handle());




  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 // Must be at least Windows Vista or Server 2008 to use InitOnceExecuteOnce
  26 #define _WIN32_WINNT 0x0600
  27 
  28 // no precompiled headers
  29 #include "classfile/classLoader.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "code/icBuffer.hpp"
  33 #include "code/vtableStubs.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/disassembler.hpp"
  36 #include "interpreter/interpreter.hpp"
  37 #include "jvm_windows.h"
  38 #include "logging/log.hpp"
  39 #include "memory/allocation.inline.hpp"
  40 #include "memory/filemap.hpp"
  41 #include "mutex_windows.inline.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "os_share_windows.hpp"
  44 #include "os_windows.inline.hpp"
  45 #include "prims/jniFastGetField.hpp"
  46 #include "prims/jvm.h"
  47 #include "prims/jvm_misc.hpp"
  48 #include "runtime/arguments.hpp"
  49 #include "runtime/atomic.inline.hpp"
  50 #include "runtime/extendedPC.hpp"
  51 #include "runtime/globals.hpp"
  52 #include "runtime/interfaceSupport.hpp"
  53 #include "runtime/java.hpp"
  54 #include "runtime/javaCalls.hpp"
  55 #include "runtime/mutexLocker.hpp"
  56 #include "runtime/objectMonitor.hpp"
  57 #include "runtime/orderAccess.inline.hpp"
  58 #include "runtime/osThread.hpp"


 420   _alloca(((pid ^ counter++) & 7) * 128);
 421 
 422   thread->initialize_thread_current();
 423 
 424   OSThread* osthr = thread->osthread();
 425   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 426 
 427   if (UseNUMA) {
 428     int lgrp_id = os::numa_get_group_id();
 429     if (lgrp_id != -1) {
 430       thread->set_lgrp_id(lgrp_id);
 431     }
 432   }
 433 
 434   // Diagnostic code to investigate JDK-6573254
 435   int res = 30115;  // non-java thread
 436   if (thread->is_Java_thread()) {
 437     res = 20115;    // java thread
 438   }
 439 
 440   log_debug(os)("Thread is alive (tid: " UINTX_FORMAT ").", os::current_thread_id());
 441 
 442   // Install a win32 structured exception handler around every thread created
 443   // by VM, so VM can generate error dump when an exception occurred in non-
 444   // Java thread (e.g. VM thread).
 445   __try {
 446     thread->run();
 447   } __except(topLevelExceptionFilter(
 448                                      (_EXCEPTION_POINTERS*)_exception_info())) {
 449     // Nothing to do.
 450   }
 451 
 452   log_debug(os)("Thread finished (tid: " UINTX_FORMAT ").", os::current_thread_id());
 453 
 454   // One less thread is executing
 455   // When the VMThread gets here, the main thread may have already exited
 456   // which frees the CodeHeap containing the Atomic::add code
 457   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
 458     Atomic::dec_ptr((intptr_t*)&os::win32::_os_thread_count);
 459   }
 460 
 461   // Thread must not return from exit_process_or_thread(), but if it does,
 462   // let it proceed to exit normally
 463   return (unsigned)os::win32::exit_process_or_thread(os::win32::EPT_THREAD, res);
 464 }
 465 
 466 static OSThread* create_os_thread(Thread* thread, HANDLE thread_handle,
 467                                   int thread_id) {
 468   // Allocate the OSThread object
 469   OSThread* osthread = new OSThread(NULL, NULL);
 470   if (osthread == NULL) return NULL;
 471 
 472   // Initialize support for Java interrupts
 473   HANDLE interrupt_event = CreateEvent(NULL, true, false, NULL);


 497 
 498 bool os::create_attached_thread(JavaThread* thread) {
 499 #ifdef ASSERT
 500   thread->verify_not_published();
 501 #endif
 502   HANDLE thread_h;
 503   if (!DuplicateHandle(main_process, GetCurrentThread(), GetCurrentProcess(),
 504                        &thread_h, THREAD_ALL_ACCESS, false, 0)) {
 505     fatal("DuplicateHandle failed\n");
 506   }
 507   OSThread* osthread = create_os_thread(thread, thread_h,
 508                                         (int)current_thread_id());
 509   if (osthread == NULL) {
 510     return false;
 511   }
 512 
 513   // Initial thread state is RUNNABLE
 514   osthread->set_state(RUNNABLE);
 515 
 516   thread->set_osthread(osthread);
 517 
 518   log_debug(os)("Thread attached (tid: " UINTX_FORMAT ").",
 519     os::current_thread_id());
 520 
 521   return true;
 522 }
 523 
 524 bool os::create_main_thread(JavaThread* thread) {
 525 #ifdef ASSERT
 526   thread->verify_not_published();
 527 #endif
 528   if (_starting_thread == NULL) {
 529     _starting_thread = create_os_thread(thread, main_thread, main_thread_id);
 530     if (_starting_thread == NULL) {
 531       return false;
 532     }
 533   }
 534 
 535   // The primordial thread is runnable from the start)
 536   _starting_thread->set_state(RUNNABLE);
 537 
 538   thread->set_osthread(_starting_thread);
 539   return true;
 540 }


 597   // commitment. On the other hand, specifying 'stack_size' larger than
 598   // default value may cause significant increase in memory usage, because
 599   // not only the stack space will be rounded up to MB, but also the
 600   // entire space is committed upfront.
 601   //
 602   // Finally Windows XP added a new flag 'STACK_SIZE_PARAM_IS_A_RESERVATION'
 603   // for CreateThread() that can treat 'stack_size' as stack size. However we
 604   // are not supposed to call CreateThread() directly according to MSDN
 605   // document because JVM uses C runtime library. The good news is that the
 606   // flag appears to work with _beginthredex() as well.
 607 
 608   HANDLE thread_handle =
 609     (HANDLE)_beginthreadex(NULL,
 610                            (unsigned)stack_size,
 611                            (unsigned (__stdcall *)(void*)) java_start,
 612                            thread,
 613                            CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION,
 614                            &thread_id);
 615 
 616   if (thread_handle == NULL) {
 617     log_warning(os)("Failed to start thread - _beginthreadex failed (%s).",
 618       strerror(errno));
 619 
 620     // Need to clean up stuff we've allocated so far
 621     CloseHandle(osthread->interrupt_event());
 622     thread->set_osthread(NULL);
 623     delete osthread;
 624     return NULL;
 625   } else {
 626     log_debug(os)("Thread started (tid: %u)", thread_id);
 627   }
 628 
 629   Atomic::inc_ptr((intptr_t*)&os::win32::_os_thread_count);
 630 
 631   // Store info on the Win32 thread into the OSThread
 632   osthread->set_thread_handle(thread_handle);
 633   osthread->set_thread_id(thread_id);
 634 
 635   // Initial thread state is INITIALIZED, not SUSPENDED
 636   osthread->set_state(INITIALIZED);
 637 
 638   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
 639   return true;
 640 }
 641 
 642 
 643 // Free Win32 resources related to the OSThread
 644 void os::free_thread(OSThread* osthread) {
 645   assert(osthread != NULL, "osthread not set");
 646   CloseHandle(osthread->thread_handle());


< prev index next >