2008-05-12 20:15:08 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
|
|
|
* File : topology.cpp
|
|
|
|
* Project : 0 A.D.
|
|
|
|
* Description : detection of CPU and cache topology
|
|
|
|
* =========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// license: GPL; see lib/license.txt
|
|
|
|
|
|
|
|
#ifndef INCLUDED_TOPOLOGY
|
|
|
|
#define INCLUDED_TOPOLOGY
|
|
|
|
|
2008-05-13 21:43:02 +02:00
|
|
|
// interface rationale:
|
|
|
|
// - explicit initialization avoids the difficulty and overhead of
|
|
|
|
// thread-safe lazy initialization checks.
|
|
|
|
// - requiring an opaque struct to be passed in ensures users call the
|
|
|
|
// init function before using the accessors.
|
|
|
|
// - delegating responsibility for thread-safety to the caller of the
|
|
|
|
// first *_Detect invocation avoids overhead and keeps us independent of
|
|
|
|
// the various threading packages (Boost, OpenMP, POSIX, Win32, ..)
|
|
|
|
|
|
|
|
|
2008-05-13 07:51:25 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-05-13 21:43:02 +02:00
|
|
|
// cpu
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stores CPU topology, i.e. how many packages, cores and SMT units are
|
|
|
|
* actually present and enabled. this is useful for detecting SMP systems,
|
|
|
|
* predicting performance and dimensioning thread pools.
|
|
|
|
*
|
|
|
|
* note: OS abstractions usually only mention "processors", which could be
|
|
|
|
* any mix of the above.
|
|
|
|
**/
|
|
|
|
struct CpuTopology;
|
2008-05-13 07:51:25 +02:00
|
|
|
|
2008-05-13 21:43:02 +02:00
|
|
|
/**
|
|
|
|
* initialize static storage from which topology can be retrieved by
|
|
|
|
* means of the following functions.
|
|
|
|
* @return const pointer to a shared instance.
|
|
|
|
*
|
|
|
|
* WARNING: this function must not be reentered before it has returned once.
|
|
|
|
**/
|
|
|
|
LIB_API const CpuTopology* cpu_topology_Detect();
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return number of *enabled* CPU packages / sockets.
|
|
|
|
**/
|
2008-05-13 21:43:02 +02:00
|
|
|
LIB_API size_t cpu_topology_NumPackages(const CpuTopology*);
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return number of *enabled* CPU cores per package.
|
|
|
|
* (2 on dual-core systems)
|
|
|
|
**/
|
2008-05-13 21:43:02 +02:00
|
|
|
LIB_API size_t cpu_topology_CoresPerPackage(const CpuTopology*);
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return number of *enabled* hyperthreading units per core.
|
|
|
|
* (2 on P4 EE)
|
|
|
|
**/
|
2008-05-13 21:43:02 +02:00
|
|
|
LIB_API size_t cpu_topology_LogicalPerCore(const CpuTopology*);
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// L2 cache
|
|
|
|
|
2008-05-13 21:43:02 +02:00
|
|
|
/**
|
|
|
|
* stores L2 cache topology, i.e. the mapping between processor and caches.
|
|
|
|
* this allows cores sharing a cache to work together on the same dataset,
|
|
|
|
* which may reduce contention and increase effective capacity.
|
|
|
|
*
|
|
|
|
* example: Intel Core2 micro-architectures (e.g. Intel Core2) feature
|
|
|
|
* partitioned L2 caches shared by two cores.
|
|
|
|
**/
|
|
|
|
struct CacheTopology;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* initialize static storage from which topology can be retrieved by
|
|
|
|
* means of the following functions.
|
|
|
|
* @return const pointer to a shared instance.
|
|
|
|
*
|
|
|
|
* WARNING: this function must not be reentered before it has returned once.
|
|
|
|
**/
|
|
|
|
LIB_API const CacheTopology* cache_topology_Detect();
|
2008-05-13 07:51:25 +02:00
|
|
|
|
2008-05-12 20:15:08 +02:00
|
|
|
/**
|
|
|
|
* @return number of distinct L2 caches
|
|
|
|
**/
|
2008-05-13 21:43:02 +02:00
|
|
|
LIB_API size_t cache_topology_NumCaches(const CacheTopology*);
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return L2 cache number (zero-based) to which <processor> belongs.
|
|
|
|
**/
|
2008-05-13 21:43:02 +02:00
|
|
|
LIB_API size_t cache_topology_CacheFromProcessor(const CacheTopology*, size_t processor);
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bit-mask of all processors sharing <cache>.
|
|
|
|
**/
|
2008-05-13 21:43:02 +02:00
|
|
|
LIB_API uintptr_t cache_topology_ProcessorMaskFromCache(const CacheTopology*, size_t cache);
|
2008-05-12 20:15:08 +02:00
|
|
|
|
|
|
|
#endif // #ifndef INCLUDED_TOPOLOGY
|