Skip to content

Commit f026a86

Browse files
committed
add get_max_open_files
1 parent 3c1225a commit f026a86

4 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
%% python.get_max_open_files Get process open-file soft limit via Python
2+
function omax = get_max_open_files()
3+
4+
omax = [];
5+
6+
if ispc()
7+
omax = uint64(omax);
8+
return
9+
end
10+
11+
try
12+
% RLIMIT_NOFILE soft limit is the active per-process descriptor cap.
13+
lim = py.resource.getrlimit(py.resource.RLIMIT_NOFILE);
14+
omax = lim(1);
15+
catch e
16+
pythonException(e)
17+
omax = [];
18+
end
19+
20+
omax = uint64(omax);
21+
22+
end

+stdlib/+sys/get_max_open_files.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
%% sys.get_max_open_files Get process open-file soft limit via shell
2+
function omax = get_max_open_files()
3+
4+
omax = [];
5+
6+
if ispc()
7+
omax = uint64(omax);
8+
return
9+
end
10+
11+
[s, m] = system('ulimit -n');
12+
if s == 0
13+
omax = str2double(strtrim(m));
14+
15+
if ~isfinite(omax) || omax <= 0
16+
omax = [];
17+
end
18+
end
19+
20+
omax = uint64(omax);
21+
22+
end

+stdlib/get_max_open_files.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
%% GET_MAX_OPEN_FILES Get process open-file soft limit
2+
% These limits can be important if using
3+
% <https://www.mathworks.com/help/parallel-computing/recommended-system-limits-for-macintosh-and-linux.html Parallel Computing>
4+
5+
function [omax, b] = get_max_open_files(backend)
6+
7+
if nargin < 1
8+
backend = {'python', 'sys'};
9+
else
10+
backend = cellstr(backend);
11+
end
12+
13+
omax = uint64([]);
14+
15+
for j = 1:numel(backend)
16+
b = backend{j};
17+
18+
switch b
19+
case 'python'
20+
if stdlib.has_python()
21+
omax = stdlib.python.get_max_open_files();
22+
end
23+
case 'sys'
24+
omax = stdlib.sys.get_max_open_files();
25+
otherwise
26+
error('stdlib:get_max_open_files:ValueError', 'Unknown backend: %s', b)
27+
end
28+
29+
if ~isempty(omax)
30+
return
31+
end
32+
end
33+
34+
end
35+
36+
%!test
37+
%! n = stdlib.get_max_open_files();
38+
%! assert(isscalar(n));
39+
%! assert(n > 0);

test/TestDisk.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ function test_disk_capacity(tc, Ps, B_jdps)
5959
end
6060

6161

62+
function test_ulimit(tc, B_ps)
63+
[i, b] = stdlib.get_max_open_files(B_ps);
64+
tc.assertEqual(char(b), B_ps)
65+
tc.verifyClass(i, 'uint64')
66+
if ispc()
67+
tc.verifyEmpty(i)
68+
else
69+
tc.verifyGreaterThan(i, 0)
70+
end
71+
end
72+
73+
6274
function test_is_removable(tc, B_ps)
6375
[y, b] = stdlib.is_removable(pwd(), B_ps);
6476
tc.assertEqual(char(b), B_ps)

0 commit comments

Comments
 (0)