// Runtime.java // native代码 // 返回JAVA进程可用核数 public native intavailableProcessors();
JDK 8u191之前的代码:
1 2 3 4 5 6 7 8
// os_linux.cpp intos::active_processor_count() { // Linux doesn't yet have a (official) notion of processor sets, // so just return the number of online processors. int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN); assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check"); return online_cpus; }
// os_linux.cpp // 如果指定了JVM参数-XX:ActiveProcessorCount, 直接返回-XX:ActiveProcessorCount的值 // 如果在容器里面,调用OSContainer::active_processor_count // 否则,调用Linux::active_processor_count( intos::active_processor_count() { // User has overridden the number of active processors if (ActiveProcessorCount > 0) { log_trace(os)("active_processor_count: " "active processor count set by user : %d", ActiveProcessorCount); return ActiveProcessorCount; }
int active_cpus; if (OSContainer::is_containerized()) { active_cpus = OSContainer::active_processor_count(); log_trace(os)("active_processor_count: determined by OSContainer: %d", active_cpus); } else { // 返回当前进程的可用核数,较之前版本增加了cpu亲缘性处理 active_cpus = os::Linux::active_processor_count(); }