Skip to content

Commit 45ce253

Browse files
committed
ram_{free,total}: system fallback if other fails
1 parent b514c0a commit 45ce253

File tree

5 files changed

+59
-10
lines changed

5 files changed

+59
-10
lines changed

+stdlib/+sys/ram_free.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function bytes = ram_free()
2+
3+
bytes = 0;
4+
5+
if ispc()
6+
cmd = 'pwsh -c "(Get-CimInstance -ClassName CIM_OperatingSystem).FreePhysicalMemory * 1KB"';
7+
elseif ismac()
8+
cmd = 'sysctl -n hw.memsize';
9+
else
10+
cmd = "free -b | awk '/Mem:/ {print $4}'";
11+
end
12+
13+
[s, m] = system(cmd);
14+
if s == 0
15+
bytes = str2double(m);
16+
end
17+
18+
bytes = uint64(bytes);
19+
20+
end

+stdlib/+sys/ram_total.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function bytes = ram_total()
2+
3+
bytes = 0;
4+
5+
if ispc()
6+
cmd = 'pwsh -c "(Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory"';
7+
elseif ismac()
8+
cmd = 'sysctl -n hw.memsize';
9+
else
10+
cmd = "free -b | awk '/Mem:/ {print $4}'";
11+
end
12+
13+
[s, m] = system(cmd);
14+
if s == 0
15+
bytes = str2double(m);
16+
end
17+
18+
bytes = uint64(bytes);
19+
20+
end

+stdlib/ram_free.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020

2121
function bytes = ram_free()
2222

23+
bytes = 0;
24+
2325
if stdlib.has_java()
2426
bytes = ram_free_java();
2527
elseif stdlib.has_python()
2628
bytes = py_ram_free();
27-
else
28-
bytes = ram_free_system();
29+
end
30+
31+
if bytes <= 0
32+
bytes = stdlib.sys.ram_free();
2933
end
3034

3135
bytes = uint64(bytes);

+stdlib/ram_total.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88

99
function bytes = ram_total()
1010

11+
bytes = 0;
1112

1213
if stdlib.dotnet_api() >= 6
1314
% .NET is 2-3x faster than Java for this
1415
% https://learn.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.totalavailablememorybytes
1516
bytes = System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
1617
elseif stdlib.has_java()
1718
bytes = ram_total_java();
18-
else
19+
end
20+
21+
if bytes <= 0
1922
bytes = ram_total_system();
2023
end
2124

test/TestSys.m

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,21 @@ function test_ram_total(tc)
8080

8181

8282
function test_ram_free(tc)
83-
83+
% don't verify less than or equal total due to shaky system measurements
8484
f = stdlib.ram_free();
8585
tc.verifyGreaterThan(f, 0)
8686
tc.verifyClass(f, 'uint64')
8787
end
8888

89-
function test_ram_free_vs_total(tc)
90-
t = stdlib.ram_total();
91-
tc.assumeGreaterThan(t, 0)
92-
f = stdlib.ram_free();
93-
tc.assumeGreaterThan(f, 0)
89+
end
9490

95-
tc.verifyLessThanOrEqual(f, t, "Free RAM should be less than or equal to total RAM")
91+
92+
methods (Test, TestTags="sys")
93+
94+
function test_ram_free_sys(tc)
95+
f = stdlib.sys.ram_free();
96+
tc.verifyGreaterThan(f, 0)
97+
tc.verifyClass(f, 'uint64')
9698
end
9799

98100
end

0 commit comments

Comments
 (0)