Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which standard C++ features can be used for querying machine/OS architecture?

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.

like image 712
user14717 Avatar asked Jul 07 '14 18:07

user14717


2 Answers

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?

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").

like image 82
Lightness Races in Orbit Avatar answered Oct 11 '22 23:10

Lightness Races in Orbit


"std::thread::hardware_concurrency() gives you the number of threads the machine supports ..."

No, it doesn't. To be precise (citing from here)

std::thread::hardware_concurrency() ... Returns number of concurrent threads supported by the implementation. The value should be considered only a hint. ...
... If the value is not well defined or not computable, returns ​0​.

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.


"how do you detect how much RAM the computer has, or how much RAM the process is using, ..."

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).

"... or how much disk space is available to write to in a certain directory, ..."

C++ standard library also has no notion of such thing like a filesystem or directories ...

"... or how much L2 cache is available"

... 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).

like image 38
πάντα ῥεῖ Avatar answered Oct 11 '22 23:10

πάντα ῥεῖ