Getting RAM size (from C++): howto?

I’m working on porting my own little archiving program to Haiku (zpaqfranz).

I’m having some problems, but I think it can be solved :slight_smile:

I would need help with a specific function though, which basically returns the current free RAM (or an estimate of it)
The environment is normal g++

Basically it is used to allocate (almost) all the available RAM to extract deduplicated files and recalculate the relative hashes (short version), instead of a filesystem temporary path

	This function is rather tricky: report the "just about free" memory of the system.
	It depends on both the operating system and the compiler (and architecture)
	It is needed for the -ramdisk switch
	If you have compiling problems, for example because you are using a "strange" system, 
	just change the source code to return 0;
	As today tested on (64 bit)
	- Windows
	- FreeBSD
	- OpenBSD 6.6/7.1
	- Debian
	- Solaris 11
	- OmniOS
	- MacOS 11
#ifdef __HAIKU__
int64_t internal_getramdisksize()
	return 0;

I suppose there’s documentation for it somewhere, but it’s not that easy to find (at least for a newbie)
Thanks for every answer!

PS does some kind of “pkg install zpaqfranz” esists in Haiku?

1 Like

The command used to manage packages is pkgman. See “pkgman help” for more info.

For the RAM available size you can compute it from get_system_info: OS.h\kernel\os\headers - haiku - Haiku's main repository

It is counted in “pages” there so you have to multiply by B_PAGE_SIZE. Or maybe you can simply use “free_memory”, depending on exactly what you need.

1 Like

Thank for the answer, I am getting closer

typedef struct {
	bigtime_t		boot_time;			/* time of boot (usecs since 1/1/1970) */

	uint32			cpu_count;			/* number of cpus */

	uint64			max_pages;			/* total # of accessible pages */
	uint64			used_pages;			/* # of accessible pages in use */
	uint64			cached_pages;
	uint64			block_cache_pages;
	uint64			ignored_pages;		/* # of ignored/inaccessible pages */

	uint64			needed_memory;
	uint64			free_memory;

	uint64			max_swap_pages;
	uint64			free_swap_pages;

It is not very clear (to me) how to computer the “real free RAM”
what is “free_memory”?


Also please be aware that in most modern OSs free memory can change from one moment to the next one. There’s more memory available to a process than there is physical memory! This is called virtual memory.

For example if you have two large chunks of memory used by your program but they are not used simultanously, you (or the OS) could swap them.

Thanks for the explanation, in fact the point is related to the physical RAM, not the virtual memory

I think I succeeded (actually I had to fiddle a bit with array allocation, which seems to be quite limited in size)

#ifdef __HAIKU__
	const int BUFSIZE	=16384;
#ifdef ANCIENT
	const int BUFSIZE	=16384;
	const int BUFSIZE	=65536*8;

I’m not sure it’s a program of interest for Haiku, it’s more for server backup

However I add to the supported platforms, with Windows, FreeBSD, OpenBSD, Solaris, OmniOS, MacOS, ESXi, QNAP

Can I mention PulkoMandy in the greetings?

1 Like

Yes, if you want to :slight_smile:

I’m not sure about the exact meaning of the various fields in get_system_info, probably some of them include only physical memory and others include also physical memory. There are also pages used by the block cache and file cache (cached_pages and block_cache_pages), which may be freed if an application needs them, but that will slow down disk operations since all the cache content has to be flushed to disk (if modified) and when there is no cache available, all data will be pushed to disk almost immediately, instead of staying in RAM and being flushed later.

So it can be a bit complicated to find the “right” value with all these parameters involved.

1 Like

Of course this is an estimate, not a precise value
For now I am using… free_memory :slight_smile:

If you are curious it is a fork of an archiver that is already in Haiku (zpaq 7.15), you are… number 16 :smiley:

But the “Haiku-compatible” is on the nigthly build

To compile just
g++ -O3 -Dunix zpaqfranz.cpp -o zpaqfranz -pthread -static

The “ramdisk” of course is a fake ramdisk, it is used to extract archive in RAM (via standard malloc) for the “nonstreamed” ones

zpaqfranz w /tmp/thearchive.zpaq -ramdisk -test -checksum

-pthread is there even if you don’t ask for it. It was added to the linker includes as a do-nothing option for compatibility with Linux ports. (Or so I’ve been told.)

1 Like