Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

java - Runtime.availableProcessors: what is it going to return?

The javadoc for Runtime.availableProcessors() in Java 1.6 is delightfully unspecific. Is it looking just at the hardware configuration, or also at the load? Is it smart enough to avoid being fooled by hyperthreading? Does it respect a limited set of processors via the linux taskset command?

I can add one datapoint of my own: on a computer here with 12 cores and hyperthreading, Runtime.availableProcessors() indeed returns 24, which is not a good number to use in deciding how many threads to try to run. The machine was clearly not dead-idle, so it also can't have been looking at load in any effective way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On Windows, GetSystemInfo is used and dwNumberOfProcessors from the returned SYSTEM_INFO structure.

This can be seen from void os::win32::initialize_system_info() and int os::active_processor_count() in os_windows.cpp of the OpenJDK source code.

dwNumberOfProcessors, from the MSDN documentation says that it reports 'The number of logical processors in the current group', which means that hyperthreading will increase the number of CPUs reported.

On Linux, os::active_processor_count() uses sysconf:

int os::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;
}

Where _SC_NPROCESSORS_ONLN documentation says 'The number of processors currently online (available).' This is not affected by the affinity of the process, and is also affected by hyperthreading.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...