4754 // initialize thread priority policy 4755 prio_init(); 4756 4757 return JNI_OK; 4758 } 4759 4760 // Mark the polling page as unreadable 4761 void os::make_polling_page_unreadable(void) { 4762 if (!guard_memory((char*)_polling_page, Linux::page_size())) { 4763 fatal("Could not disable polling page"); 4764 } 4765 } 4766 4767 // Mark the polling page as readable 4768 void os::make_polling_page_readable(void) { 4769 if (!linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) { 4770 fatal("Could not enable polling page"); 4771 } 4772 } 4773 4774 // Get the current number of available processors for this process. 4775 // This value can change at any time during a process's lifetime. 4776 // sched_getaffinity gives an accurate answer as it accounts for cpusets. 4777 // If it appears there may be more than 1024 processors then we do a 4778 // dynamic check - see 6515172 for details. 4779 // If anything goes wrong we fallback to returning the number of online 4780 // processors - which can be greater than the number available to the process. 4781 int os::active_processor_count() { 4782 cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors 4783 cpu_set_t* cpus_p = &cpus; 4784 int cpus_size = sizeof(cpu_set_t); 4785 4786 int configured_cpus = processor_count(); // upper bound on available cpus 4787 int cpu_count = 0; 4788 4789 // To enable easy testing of the dynamic path on different platforms we 4790 // introduce a diagnostic flag: UseCpuAllocPath 4791 if (configured_cpus >= CPU_SETSIZE || UseCpuAllocPath) { 4792 // kernel may use a mask bigger than cpu_set_t 4793 log_trace(os)("active_processor_count: using dynamic path %s" 4794 "- configured processors: %d", 4795 UseCpuAllocPath ? "(forced) " : "", 4796 configured_cpus); 4797 cpus_p = CPU_ALLOC(configured_cpus); 4798 if (cpus_p != NULL) { 4799 cpus_size = CPU_ALLOC_SIZE(configured_cpus); 4800 // zero it just to be safe 4801 CPU_ZERO_S(cpus_size, cpus_p); 4802 } 4803 else { 4804 // failed to allocate so fallback to online cpus 4805 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); 4806 log_trace(os)("active_processor_count: " 4807 "CPU_ALLOC failed (%s) - using " 4808 "online processor count: %d", 4809 strerror(errno), online_cpus); 4810 return online_cpus; 4811 } 4812 } 4813 else { 4814 log_trace(os)("active_processor_count: using static path - configured processors: %d", 4815 configured_cpus); 4816 } 4817 4818 // pid 0 means the current thread - which we have to assume represents the process 4819 if (sched_getaffinity(0, cpus_size, cpus_p) == 0) { 4820 if (cpus_p != &cpus) { 4821 cpu_count = CPU_COUNT_S(cpus_size, cpus_p); 4822 } 4823 else { 4824 cpu_count = CPU_COUNT(cpus_p); 4825 } 4826 log_trace(os)("active_processor_count: sched_getaffinity processor count: %d", cpu_count); 4827 } 4828 else { 4829 cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN); 4830 warning("sched_getaffinity failed (%s)- using online processor count (%d) " 4831 "which may exceed available processors", strerror(errno), cpu_count); 4832 } 4833 4834 if (cpus_p != &cpus) { 4835 CPU_FREE(cpus_p); 4836 } | 4754 // initialize thread priority policy 4755 prio_init(); 4756 4757 return JNI_OK; 4758 } 4759 4760 // Mark the polling page as unreadable 4761 void os::make_polling_page_unreadable(void) { 4762 if (!guard_memory((char*)_polling_page, Linux::page_size())) { 4763 fatal("Could not disable polling page"); 4764 } 4765 } 4766 4767 // Mark the polling page as readable 4768 void os::make_polling_page_readable(void) { 4769 if (!linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) { 4770 fatal("Could not enable polling page"); 4771 } 4772 } 4773 4774 // older glibc versions don't have this macro (which expands to 4775 // an optimized bit-counting function) so we have to roll our own 4776 #ifndef CPU_COUNT 4777 4778 static int _cpu_count(const cpu_set_t* cpus) { 4779 int count = 0; 4780 // only look up to the number of configured processors 4781 for (int i = 0; i < os::processor_count(); i++) { 4782 if (CPU_ISSET(i, cpus)) { 4783 count++; 4784 } 4785 } 4786 return count; 4787 } 4788 4789 #define CPU_COUNT(cpus) _cpu_count(cpus) 4790 4791 #endif // CPU_COUNT 4792 4793 // Get the current number of available processors for this process. 4794 // This value can change at any time during a process's lifetime. 4795 // sched_getaffinity gives an accurate answer as it accounts for cpusets. 4796 // If it appears there may be more than 1024 processors then we do a 4797 // dynamic check - see 6515172 for details. 4798 // If anything goes wrong we fallback to returning the number of online 4799 // processors - which can be greater than the number available to the process. 4800 int os::active_processor_count() { 4801 cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors 4802 cpu_set_t* cpus_p = &cpus; 4803 int cpus_size = sizeof(cpu_set_t); 4804 4805 int configured_cpus = processor_count(); // upper bound on available cpus 4806 int cpu_count = 0; 4807 4808 // old build platforms may not support dynamic cpu sets 4809 #ifdef CPU_ALLOC 4810 4811 // To enable easy testing of the dynamic path on different platforms we 4812 // introduce a diagnostic flag: UseCpuAllocPath 4813 if (configured_cpus >= CPU_SETSIZE || UseCpuAllocPath) { 4814 // kernel may use a mask bigger than cpu_set_t 4815 log_trace(os)("active_processor_count: using dynamic path %s" 4816 "- configured processors: %d", 4817 UseCpuAllocPath ? "(forced) " : "", 4818 configured_cpus); 4819 cpus_p = CPU_ALLOC(configured_cpus); 4820 if (cpus_p != NULL) { 4821 cpus_size = CPU_ALLOC_SIZE(configured_cpus); 4822 // zero it just to be safe 4823 CPU_ZERO_S(cpus_size, cpus_p); 4824 } 4825 else { 4826 // failed to allocate so fallback to online cpus 4827 int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); 4828 log_trace(os)("active_processor_count: " 4829 "CPU_ALLOC failed (%s) - using " 4830 "online processor count: %d", 4831 strerror(errno), online_cpus); 4832 return online_cpus; 4833 } 4834 } 4835 else { 4836 log_trace(os)("active_processor_count: using static path - configured processors: %d", 4837 configured_cpus); 4838 } 4839 #else // CPU_ALLOC 4840 4841 #define CPU_COUNT_S(size, cpus) -1 4842 #define CPU_FREE(cpus) 4843 4844 log_trace(os)("active_processor_count: only static path available - configured processors: %d", 4845 configured_cpus); 4846 #endif // CPU_ALLOC 4847 4848 // pid 0 means the current thread - which we have to assume represents the process 4849 if (sched_getaffinity(0, cpus_size, cpus_p) == 0) { 4850 if (cpus_p != &cpus) { 4851 cpu_count = CPU_COUNT_S(cpus_size, cpus_p); 4852 } 4853 else { 4854 cpu_count = CPU_COUNT(cpus_p); 4855 } 4856 log_trace(os)("active_processor_count: sched_getaffinity processor count: %d", cpu_count); 4857 } 4858 else { 4859 cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN); 4860 warning("sched_getaffinity failed (%s)- using online processor count (%d) " 4861 "which may exceed available processors", strerror(errno), cpu_count); 4862 } 4863 4864 if (cpus_p != &cpus) { 4865 CPU_FREE(cpus_p); 4866 } |