Closed am11 closed 4 years ago
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
There is an exact description of the meaning of the returned structure members on Windows: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-memorystatusex It says:
ullTotalPageFile The current committed memory limit for the system or the current process, whichever is smaller, in bytes. To get the system-wide committed memory limit, call GetPerformanceInfo.
ullAvailPageFile The maximum amount of memory the current process can commit, in bytes. This value is equal to or smaller than the system-wide available commit value. To calculate the system-wide available commit value, call GetPerformanceInfo and subtract the value of CommitTotal from the value of CommitLimit.
I am not sure how that maps to the values you got since I don't know what they really mean.
It looks like we do not need kstat
for this, but instead swapctl(2)
. The following program:
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/swap.h>
int main(int argc, char **argv)
{
struct anoninfo ai;
if (swapctl(SC_AINFO, &ai) != -1)
{
int pagesize = getpagesize();
printf("free:\t%" PRIu64 "\ntotal:\t%" PRIu64 "\n", ai.ani_free * pagesize, ai.ani_max * pagesize);
}
return 0;
}
on SmartOS x64 prints:
free: 1833984000
total: 1911136256
Btw, is our usage of sysinfo.totalswap
and sysinfo.freeswap
correct in case of Linux? I ran the following program on Ubuntu 18.04 x64:
#include <inttypes.h>
#include <stdio.h>
#include <sys/sysinfo.h>
int main(int argc, char **argv)
{
struct sysinfo info;
if (sysinfo(&info) == 0)
{
printf("total %" PRIu64 "\nfree: %" PRIu64 "\n", info.totalswap, info.freeswap);
}
return 0;
}
and it prints 0:
total 0
free: 0
Do you have swap enabled on your Ubuntu? The zeros seems to indicate you don't.
Thank you, that was the reason. After creating 1Gb swapfile and enabling it on Ubuntu, I get:
$ free -m
total used free shared buff/cache available
Mem: 7953 2340 280 85 5332 5220
Swap: 1023 0 1023
$ ./a.out
total 1073737728
free: 1073737728
I will use swapctl(2)
for SunOS and continue the port.
Information about swap memory is not available using
sysinfo(2)
, although it exists on Solaris to provide other information. Therefore, to implementGlobalMemoryStatusEx
, it seems likekstat_open(3)
and friends are most suitable options.As per the discussion in this thread, the system counters provided by
kstat
are mainly "sum total of the value sampled every second since the system was booted", so it requires a bit of computation. Here is a self-contained repro: https://pastebin.com/raw/VxVpwJhC. The result on SmartOS x64 looks like this:At this point,
df -b
reports:There is also this blog article which sheds light on kstat and sample program iterates over the linked list to gather all the raw data:
The interesting part for global memory status is (under
ks_name=vminfo ks_class=vm
):which I am having trouble mapping to
ullTotalPageFile
andullAvailPageFile
. Would it be correct to compute as:cc @janvorli