JDK-4942707 : java.lang.Runtime should have methods to report CPU cache information
  • Type: Enhancement
  • Status: Closed
  • Resolution: Won't Fix
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Affected Version: 1.4.2
  • OS: windows_xp
  • CPU: x86
  • Submit Date: 2003-10-23
  • Updated Date: 2003-11-03
  • Resolved Date: 2003-11-03

Name: rmT116609			Date: 10/23/2003

There should be methods in the java.lang.Runtime class to report the cache characteristics of the CPU(s). The CPU / cache info should be retrieved from java.lang.Runtime similarly like the number of processors and memory size.

At least the following information would need to be provided:
- number of cache levels (e.g. 1, 2, 3)
- size of cache on each cache level, in bytes (e.g. 256 kB)
- cache line size of each cache level (e.g. 32 bytes)
- associativity of the cache (e.g. 2-way, 4-way, 8-way)

A new class would be used for this information (e.g. java.lang.CPUInfo), to avoid cluttering the java.lang.Runtime class. The Runtime class would have one new method:

CPUInfo getCPUInfo(int cpu)

The CPUInfo class would have at least the following methods:

int cacheLevels()
int cacheSize(int level)
int cacheLineSize(int level)
int cacheAssociativity(int level)

This way different numbers could be returned for different CPUs, in case the machine has multiple heterogeneous processors (although I guess this is very rare).

Depending on the hardware platform (Intel x86, Itanium, SPARC, ...) the getCPUInfo() method could return a subclass of CPUInfo that has additional methods specific to the processor architecture (e.g. on the x86 family, if MMX or SSE instructions are supported).

Additionally, it would be useful to have methods to specify for processes and threads, which CPUs they can use (in a multi-CPU machine). This can improve
performance slightly in some cases, as cache thrashing can be minimized. At
least the Win32 API has such functions (SetProcessAffinityMask,
SetThreadAffinityMask). For example, the following methods would be added to
the java.lang.Process and java.lang.Thread classes:

boolean[] getCPUAffinityMask()
void setCPUAffinityMask(boolean[])

I have posted a message to the comp.lang.java.programmer newsgroup on August 16th, 2003 regarding this RFE and received several replies supporting the idea.

Many mathematical and other memory-intensive algorithms benefit greatly from knowing these values accurately, for example matrix multiplication algorithms.

Writing native code for every CPU out there would be very time-consuming. It
would be useful to have this information available via a standard API.

Such methods are available.
Such methods are not available.

Write assembler code to determine the cache sizes for each known CPU type. Use JNI calls to the assembler code.

Another theoretical possibility would be to use some test code to try different algorithms and see, which executes fastest, to probe for likely cache sizes, cache line sizes etc. This would be difficult if not impossible to implement reliably, and complicated, to say the least.
(Incident Review ID: 208090) 

EVALUATION Will not fix. Insufficient motivation. -- ###@###.### 2003/11/3
169-11-11 0