3553 } 3554 3555 //////////////////////////////////////////////////////////////////////////////// 3556 // thread priority support 3557 3558 // Note: Normal Bsd applications are run with SCHED_OTHER policy. SCHED_OTHER 3559 // only supports dynamic priority, static priority must be zero. For real-time 3560 // applications, Bsd supports SCHED_RR which allows static priority (1-99). 3561 // However, for large multi-threaded applications, SCHED_RR is not only slower 3562 // than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out 3563 // of 5 runs - Sep 2005). 3564 // 3565 // The following code actually changes the niceness of kernel-thread/LWP. It 3566 // has an assumption that setpriority() only modifies one kernel-thread/LWP, 3567 // not the entire user process, and user level threads are 1:1 mapped to kernel 3568 // threads. It has always been the case, but could change in the future. For 3569 // this reason, the code should not be used as default (ThreadPriorityPolicy=0). 3570 // It is only used when ThreadPriorityPolicy=1 and requires root privilege. 3571 3572 #if defined(_ALLBSD_SOURCE) && !defined(__APPLE__) 3573 int os::java_to_os_priority[MaxPriority + 1] = { 3574 19, // 0 Entry should never be used 3575 3576 0, // 1 MinPriority 3577 3, // 2 3578 6, // 3 3579 3580 10, // 4 3581 15, // 5 NormPriority 3582 18, // 6 3583 3584 21, // 7 3585 25, // 8 3586 28, // 9 NearMaxPriority 3587 3588 31 // 10 MaxPriority 3589 }; 3590 #elif defined(__APPLE__) 3591 /* Using Mach high-level priority assignments */ 3592 int os::java_to_os_priority[MaxPriority + 1] = { 3593 0, // 0 Entry should never be used (MINPRI_USER) 3594 3595 27, // 1 MinPriority 3596 28, // 2 3597 29, // 3 3598 3599 30, // 4 3600 31, // 5 NormPriority (BASEPRI_DEFAULT) 3601 32, // 6 3602 3603 33, // 7 3604 34, // 8 3605 35, // 9 NearMaxPriority 3606 3607 36 // 10 MaxPriority 3608 }; 3609 #else 3610 int os::java_to_os_priority[MaxPriority + 1] = { 3611 19, // 0 Entry should never be used 3612 3613 4, // 1 MinPriority 3614 3, // 2 3615 2, // 3 3616 3617 1, // 4 3618 0, // 5 NormPriority 3619 -1, // 6 3620 3621 -2, // 7 3622 -3, // 8 3623 -4, // 9 NearMaxPriority 3624 3625 -5 // 10 MaxPriority 3626 }; 3627 #endif 3628 3629 static int prio_init() { 3630 if (ThreadPriorityPolicy == 1) { 3631 // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1 3632 // if effective uid is not root. Perhaps, a more elegant way of doing 3633 // this is to test CAP_SYS_NICE capability, but that will require libcap.so 3634 if (geteuid() != 0) { 3635 if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) { 3636 warning("-XX:ThreadPriorityPolicy requires root privilege on Bsd"); 3637 } 3638 ThreadPriorityPolicy = 0; 3639 } 3640 } 3641 return 0; 3642 } 3643 3644 OSReturn os::set_native_priority(Thread* thread, int newpri) { 3645 if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK; | 3553 } 3554 3555 //////////////////////////////////////////////////////////////////////////////// 3556 // thread priority support 3557 3558 // Note: Normal Bsd applications are run with SCHED_OTHER policy. SCHED_OTHER 3559 // only supports dynamic priority, static priority must be zero. For real-time 3560 // applications, Bsd supports SCHED_RR which allows static priority (1-99). 3561 // However, for large multi-threaded applications, SCHED_RR is not only slower 3562 // than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out 3563 // of 5 runs - Sep 2005). 3564 // 3565 // The following code actually changes the niceness of kernel-thread/LWP. It 3566 // has an assumption that setpriority() only modifies one kernel-thread/LWP, 3567 // not the entire user process, and user level threads are 1:1 mapped to kernel 3568 // threads. It has always been the case, but could change in the future. For 3569 // this reason, the code should not be used as default (ThreadPriorityPolicy=0). 3570 // It is only used when ThreadPriorityPolicy=1 and requires root privilege. 3571 3572 #if defined(_ALLBSD_SOURCE) && !defined(__APPLE__) 3573 int os::java_to_os_priority[CriticalPriority + 1] = { 3574 19, // 0 Entry should never be used 3575 3576 0, // 1 MinPriority 3577 3, // 2 3578 6, // 3 3579 3580 10, // 4 3581 15, // 5 NormPriority 3582 18, // 6 3583 3584 21, // 7 3585 25, // 8 3586 28, // 9 NearMaxPriority 3587 3588 31, // 10 MaxPriority 3589 3590 31 // 11 CriticalPriority 3591 }; 3592 #elif defined(__APPLE__) 3593 /* Using Mach high-level priority assignments */ 3594 int os::java_to_os_priority[CriticalPriority + 1] = { 3595 0, // 0 Entry should never be used (MINPRI_USER) 3596 3597 27, // 1 MinPriority 3598 28, // 2 3599 29, // 3 3600 3601 30, // 4 3602 31, // 5 NormPriority (BASEPRI_DEFAULT) 3603 32, // 6 3604 3605 33, // 7 3606 34, // 8 3607 35, // 9 NearMaxPriority 3608 3609 36, // 10 MaxPriority 3610 3611 36 // 11 CriticalPriority 3612 }; 3613 #else 3614 int os::java_to_os_priority[CriticalPriority + 1] = { 3615 19, // 0 Entry should never be used 3616 3617 4, // 1 MinPriority 3618 3, // 2 3619 2, // 3 3620 3621 1, // 4 3622 0, // 5 NormPriority 3623 -1, // 6 3624 3625 -2, // 7 3626 -3, // 8 3627 -4, // 9 NearMaxPriority 3628 3629 -5, // 10 MaxPriority 3630 3631 -5 // 11 CriticalPriority 3632 }; 3633 #endif 3634 3635 static int prio_init() { 3636 if (ThreadPriorityPolicy == 1) { 3637 // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1 3638 // if effective uid is not root. Perhaps, a more elegant way of doing 3639 // this is to test CAP_SYS_NICE capability, but that will require libcap.so 3640 if (geteuid() != 0) { 3641 if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) { 3642 warning("-XX:ThreadPriorityPolicy requires root privilege on Bsd"); 3643 } 3644 ThreadPriorityPolicy = 0; 3645 } 3646 } 3647 return 0; 3648 } 3649 3650 OSReturn os::set_native_priority(Thread* thread, int newpri) { 3651 if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK; |