Skip to content

Commit 39947fb

Browse files
committed
add get_process_priority()
1 parent 6defce0 commit 39947fb

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function i = get_process_priority()
2+
3+
p = System.Diagnostics.Process.GetCurrentProcess();
4+
i = p.PriorityClass;
5+
6+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function i = get_process_priority()
2+
3+
if ispc()
4+
i = [];
5+
else
6+
pid = py.os.getpid();
7+
i = double(py.os.getpriority(py.os.PRIO_PROCESS, pid));
8+
end
9+
10+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function [i, cmd] = get_process_priority()
2+
3+
pid = stdlib.get_pid();
4+
5+
if ispc()
6+
cmd = sprintf('pwsh -c "(Get-Process -Id %d).PriorityClass"', pid);
7+
else
8+
cmd = sprintf('ps -o ni= -p %d', pid);
9+
end
10+
11+
[s, m] = system(cmd);
12+
if s == 0
13+
if ispc()
14+
i = strip(m);
15+
else
16+
i = str2double(m);
17+
end
18+
else
19+
i = [];
20+
end
21+
22+
end

+stdlib/Backend.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
switch functionName
8585
case {'filesystem_type', 'is_removable', 'ram_total', 'ram_free'}
8686
if ~stdlib.python.has_psutil(); continue, end
87-
case {'cpu_load', 'get_owner', 'get_uid'}
87+
case {'cpu_load', 'get_owner', 'get_process_priority', 'get_uid'}
8888
if ispc(), continue, end
8989
case 'is_admin'
9090
if ispc() || stdlib.matlabOlderThan('R2024a'), continue, end

+stdlib/get_process_priority.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% GET_PROCESS_PRIORITY get priority of the current Matlab session
2+
% This is an integer value like "nice" on Unix-like systems
3+
% On Windows systems the char value is like 'Normal'
4+
5+
function [i, b] = get_process_priority(backend)
6+
arguments
7+
backend (1,:) string = ["dotnet", "python", "sys"]
8+
end
9+
10+
o = stdlib.Backend(mfilename(), backend);
11+
i = o.func();
12+
13+
b = o.backend;
14+
15+
end

test/TestSys.m

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
B_hostname
1414
B_username
1515
B_get_uid
16+
B_get_process_priority
1617
end
1718

1819
methods (TestParameterDefinition, Static)
19-
function [B_cpu_arch, B_cpu_load, B_ram_free, B_ram_total, B_is_admin, B_os_version, B_hostname, B_username, B_get_uid] = setupBackends()
20+
function [B_cpu_arch, B_cpu_load, B_ram_free, B_ram_total, B_is_admin, B_os_version, B_hostname, B_username, B_get_uid, B_get_process_priority] = setupBackends()
2021
B_cpu_arch = init_backend("cpu_arch");
2122
B_cpu_load = init_backend("cpu_load");
2223
B_ram_free = init_backend("ram_free");
@@ -26,6 +27,7 @@
2627
B_hostname = init_backend("hostname");
2728
B_username = init_backend("get_username");
2829
B_get_uid = init_backend('get_uid');
30+
B_get_process_priority = init_backend('get_process_priority');
2931
end
3032
end
3133

@@ -49,6 +51,12 @@ function test_cpu_load(tc, B_cpu_load)
4951
end
5052

5153

54+
function test_process_priority(tc, B_get_process_priority)
55+
r = stdlib.get_process_priority(B_get_process_priority);
56+
tc.verifyNotEmpty(r)
57+
end
58+
59+
5260
function test_os_version(tc, B_os_version)
5361
[os, ver] = stdlib.os_version(B_os_version);
5462

0 commit comments

Comments
 (0)