Skip to content

Commit b8fae74

Browse files
committed
ram_{free,total}: choose_method
1 parent c0b31da commit b8fae74

File tree

5 files changed

+40
-42
lines changed

5 files changed

+40
-42
lines changed

+stdlib/+sys/filesystem_type.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
function t = filesystem_type(p)
22

3-
if ~stdlib.exists(p)
4-
t = string.empty;
5-
return
6-
end
3+
t = string.empty;
4+
5+
if ~stdlib.exists(p), return, end
76

87
if ispc()
98
dl = extractBefore(stdlib.absolute(p), 2);
@@ -17,8 +16,6 @@
1716
[s, t] = system(cmd);
1817
if s == 0
1918
t = string(strip(t));
20-
else
21-
t = string.empty;
2219
end
2320

2421
end

+stdlib/private/choose_method.m

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313

1414
for m = method
1515
switch m
16+
case "dotnet"
17+
if endsWith(name, "ram_total")
18+
if stdlib.dotnet_api() < 6, continue, end
19+
end
1620
case "legacy", has = true;
1721
case "native", has = stdlib.strempty(minVersion) || ~isMATLABReleaseOlderThan(minVersion);
18-
otherwise, has = str2func("stdlib.has_" + m);
22+
case "sys"
23+
has = true;
24+
otherwise
25+
has = str2func("stdlib.has_" + m);
26+
if endsWith(name, ["filesystem_type", "ram_total", "ram_free"])
27+
if ~stdlib.python.has_psutil(); continue, end
28+
end
1929
end
2030

2131
if has()

+stdlib/ram_free.m

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@
88
%%% Outputs
99
% * freebytes: free physical RAM [bytes]
1010
%
11-
% This is done using Java on non-Windows platforms.
12-
%
13-
% VisualBasic (needs Windows) is needed to do this with .NET,
14-
%
15-
% builtin memory() on Windows includes swap. The user could do that themselves.
16-
%
17-
% we installed use Java or Python psutil, which are consistent with each other.
11+
% Java or Python psutil are consistent with each other.
1812
%
1913
% Fallback is to shell commands.
2014

21-
function bytes = ram_free()
22-
23-
if stdlib.has_java()
24-
bytes = stdlib.java.ram_free();
25-
elseif stdlib.python.has_psutil()
26-
bytes = stdlib.python.ram_free();
27-
else
28-
bytes = stdlib.sys.ram_free();
15+
function bytes = ram_free(method)
16+
arguments
17+
method (1,:) string = ["java", "python", "sys"]
2918
end
3019

20+
fun = choose_method(method, "ram_free");
21+
22+
bytes = fun();
23+
24+
% * VisualBasic (needs Windows) is needed to do this with .NET.
25+
% * builtin memory() on Windows includes swap. The user could do that themselves.
26+
3127
end

+stdlib/ram_total.m

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66
%%% Outputs
77
% * bytes: total physical RAM [bytes]
88

9-
function bytes = ram_total()
10-
11-
if stdlib.dotnet_api() >= 6
12-
bytes = stdlib.dotnet.ram_total();
13-
elseif stdlib.has_java()
14-
bytes = stdlib.java.ram_total();
15-
elseif stdlib.python.has_psutil()
16-
bytes = stdlib.python.ram_total();
17-
else
18-
bytes = stdlib.sys.ram_total();
9+
function bytes = ram_total(method)
10+
arguments
11+
method (1,:) string = ["java", "dotnet", "python", "sys"]
1912
end
2013

21-
end
14+
fun = choose_method(method, "ram_total");
2215

23-
%!assert(ram_total()>0)
16+
bytes = fun();
17+
18+
end

test/TestSys.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
host_fun = {@stdlib.hostname, @stdlib.sys.get_hostname, @stdlib.dotnet.get_hostname, @stdlib.java.get_hostname, @stdlib.python.get_hostname}
1212
user_fun = {@stdlib.get_username, @stdlib.sys.get_username, @stdlib.dotnet.get_username, @stdlib.java.get_username, @stdlib.python.get_username}
1313
is_admin_fun = {@stdlib.is_admin, @stdlib.sys.is_admin, @stdlib.dotnet.is_admin, @stdlib.java.is_admin, @stdlib.python.is_admin}
14-
ram_free_fun = {@stdlib.ram_free, @stdlib.sys.ram_free, @stdlib.java.ram_free, @stdlib.python.ram_free}
15-
ram_total_fun = {@stdlib.ram_total, @stdlib.sys.ram_total, @stdlib.dotnet.ram_total, @stdlib.java.ram_total, @stdlib.python.ram_total}
14+
ram_free_method = {'sys', 'java', 'python'}
15+
ram_total_method = {'sys', 'java', 'python'}
1616
end
1717

1818
methods(TestClassSetup)
@@ -144,20 +144,20 @@ function test_cpu_arch(tc, cpu_arch_fun)
144144
tc.verifyGreaterThan(strlength(arch), 0, "CPU architecture should not be empty")
145145
end
146146

147-
function test_ram_total(tc, ram_total_fun)
148-
is_capable(tc, ram_total_fun)
147+
function test_ram_total(tc, ram_total_method)
148+
is_capable(tc, str2func("stdlib." + ram_total_method + ".ram_total"))
149149

150-
t = ram_total_fun();
150+
t = stdlib.ram_total(ram_total_method);
151151
tc.verifyGreaterThan(t, 0)
152152
tc.verifyClass(t, 'uint64')
153153
end
154154

155155

156-
function test_ram_free(tc, ram_free_fun)
156+
function test_ram_free(tc, ram_free_method)
157157
% don't verify less than or equal total due to shaky system measurements
158-
is_capable(tc, ram_free_fun)
158+
is_capable(tc, str2func("stdlib." + ram_free_method + ".ram_free"))
159159

160-
f = ram_free_fun();
160+
f = stdlib.ram_free(ram_free_method);
161161
tc.verifyGreaterThan(f, 0)
162162
tc.verifyClass(f, 'uint64')
163163
end

0 commit comments

Comments
 (0)