views:

124

answers:

3

How would i find out the amount of RAM and details about my system like CPU type, speed, amount of physical memory available. amount of stack and heap memory in RAM, number of processes running.

Also how to determine if there is any way to determin how long it takes your computer to execute an instruction, fetch a word from memory (with and without a cache miss), read consecutive words from disk, and seek to a new location on disk.


Edit: I want to accomplish this on my linux system using g++ compiler. are there any inbulit functions for this..? Also tell me if such things are possible on windows system.

I just got this question out of curiosity when I was learning some memory management stuff in c++. Please guide me through this step by step or may be online tutorials ll do great. Thanks.

+4  A: 

For Windows - GetPhysicallyInstalledSystemMemory for installed RAM, GetSystemInfo for CPUs, Process Status API for process enumeration. Heap and stack usage can be gotten only by the local process for itself. Remember stack usage is per-thread, and in Windows a process can have multiple heaps (use GetProcessHeaps to enumerate them). Memory usage per process in externally visible usage can be retrieved for each process using GetProcessMemoryInfo.

I'm not aware of Win32 APIs for the second paragraph's list. Probably have to do this at the device driver level (kernel mode) I would think, if it's even possible. Instruction fetch and execution depend on the processor, cache size and instruction itself (they are not all the same in complexity). Memory access speed will depend on RAM, CPU and the motherboard FSB speed. Disk access likewise is totally dependent on the system characteristics.

Steve Townsend
are thse functions available on g++ compilers or they are very much OS dependant...?
Anurag
If you are on Windows, they should not be compiler dependent - I would expect g++ will link vs the shipping Windows DLLs and LIBs. No experience myself with anything but Visual C++, though.
Steve Townsend
`GetPhysicallyInstalledSystemMemory` is only supported on Vista+
Axel Gneiting
@Axel - right, prior to that you should use `GlobalMemoryStatusEx`.
Steve Townsend
+2  A: 

With Linux and GCC, you can use the sysconf function included using the <unistd.h> header.

There are various arguments you can pass to get hardware information. For example, to get the amount of physical RAM in your machine you would need to do:

sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);

See the man page for all possible usages.

You can get the maximum stack size of a process using the getrlimit system call along with the RLIMIT_STACK argument, included using the <sys/resource.h> header.

To find out how many processes are running on the current machine you can check the /proc directory. Each running process is represented as a file in this directory named by its process ID number.

Charles Salvia
A: 

On Windows Vista and Windows 7, the Windows System Assessment Tool can provide a lot of info. Supposedly it can be programmatically accessed via the WEI API.

jholl
But there shoud be some OS independent way of doing this using native c++ code, because every time a program executes c++ compiler makes sure if enough memory is available.
Anurag
@Anurag, there is no OS independent, native C++ way to do it. Things like CPU speed or available RAM are not part of the C++ standard.
Charles Salvia