What are the standard C++ features and utilities for querying the properties of the hardware or operating system capabilities, on which the program is running?
For instance, std::thread::hardware_concurrency()
gives you the number of threads the machine supports.
But how do you detect how much RAM the computer has, or how much RAM the process is using, or how much disk space is available to write to in a certain directory, or how much L2 cache is available?
I would prefer answers by means of c++ (c++14) standards, but TR2 or boost proposals would be good as well.
As others have pointed out, functions for obtaining such properties of the system are typically platform-specific. The STL and boost do not provide platform-independent wrappers, so you'll have to rely on other third party libraries.
I've successfully used SIGAR in the past:
As a side note, Boost Filesystem does actually provide boost::filesystem::space to query "how much disk space is available to write to in a certain directory".
Determining the amount of RAM or hard disk space available is operating-system level functionality. Because there are many different strategies available to tackle those issues, there's no platform independent way to get that information. The APIs for whatever OS you are developing for should provide functionality for determining those values.
For example, the windows API appears to provide this function: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366589%28v=vs.85%29.aspx which can help you to determine how much physical / virtual memory is available.
Determining the amount of cache space available is a different matter, the following answer might help you: https://stackoverflow.com/a/12838695/3798126
You don't. Precisely none of this is the purview of the C++ language, which describes an abstract machine.
The only reason it tells you the number of cores available is because otherwise its multi-threading model would be close to useless, and even then it does so in an abstract way ("hardware concurrency" is not "number of physical CPUs in your desktop PC").
No, it doesn't. To be precise (citing from here)
Best this does is letting you know, how many CPU cores are available for real parallel execution of threads (see @Lightness Races in Orbit's answer here).
You still can have as many thread instances you want, until acquiring one fails.
All of these capabilities like RAM available etc. are highly machine/OS dependent, and aren't queryable with standard c++ functions or classes (at least not I know of).
C++ standard library also has no notion of such thing like a filesystem or directories ...
... and even less notion about such highly MCU specific traits1.
C++ uses a completely abstract, machine architecture and operating system agnostic view of it's world.
1) Thank GOD, Bjarne and the c++ standards committee for this, otherwise I'd have serious problems, to write halfway portable code for the various targets I'm facing. If it fails, and cannot be proven an error from my side violating the standards, it's most probably a bug of the actual compiler implementation. That at least hinders my co-coders squirreling out, for getting onto unnecessary and obscure micro optimization attempts :-D.
All of the above said:
The closest you can get, asking for some machine architecture basic traits and capabilities using the current standards, is what's supported from
<cstddef>
,<limits>
and<type_traits>
IMHO.Though some common 'state of the art' technologies and abstractions were adopted by the latest standard definitions (e.g. like
std::thread
,std::chrono
or filesystem experimental library).