Skip to content

Commit 5c76545

Browse files
committed
cpu_load: add python and sys methods
1 parent 7fa3a83 commit 5c76545

File tree

7 files changed

+76
-22
lines changed

7 files changed

+76
-22
lines changed

+stdlib/+java/cpu_load.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function L = cpu_load()
2+
3+
4+
b = javaMethod("getOperatingSystemMXBean", "java.lang.management.ManagementFactory");
5+
6+
L = b.getSystemLoadAverage();
7+
if L < 0
8+
if stdlib.java_api() < 14
9+
L = b.getSystemCpuLoad();
10+
else
11+
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getCpuLoad()
12+
L = b.getCpuLoad();
13+
end
14+
end
15+
16+
end

+stdlib/+python/cpu_load.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function L = cpu_load()
2+
3+
Lavg = py.os.getloadavg();
4+
L = double(Lavg(1));
5+
6+
end

+stdlib/+sys/cpu_load.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function L = cpu_load()
2+
3+
L = NaN;
4+
5+
if ispc()
6+
cmd = 'pwsh -c "Get-CimInstance -ClassName Win32_Processor | Measure-Object -Property LoadPercentage -Average | Select-Object -ExpandProperty Average"';
7+
elseif ismac()
8+
cmd = "sysctl -n vm.loadavg | awk '{print $2}'";
9+
else
10+
cmd = "cat /proc/loadavg | awk '{print $1}'";
11+
end
12+
13+
[status, m] = system(cmd);
14+
if status ~= 0
15+
warning("stdlib:cpu_load:OSError", "Failed to get CPU load");
16+
return
17+
end
18+
19+
L = str2double(strtrim(m));
20+
21+
if ispc()
22+
L = L / 100.;
23+
end
24+
25+
end

+stdlib/cpu_load.m

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
%% CPU_LOAD get total physical CPU load
2-
% requires: java
32
%
43
% Returns the "recent cpu usage" for the whole system.
54
%
6-
% This value is a double in the [0.0,1.0] interval.
7-
% A value of 0.0 means that all CPUs were idle during the recent period of time observed, while a value of 1.0 means that all CPUs were actively running 100% of the time during the recent period being observed.
8-
% All values betweens 0.0 and 1.0 are possible depending of the activities going on in the system.
9-
% If the system recent cpu usage is not available, the method returns a negative value.
5+
% This value is a double greater than 0.
6+
% If the system recent cpu usage is not available, the method returns a negative or NaN value.
107

11-
function L = cpu_load()
8+
function L = cpu_load(method)
9+
arguments
10+
method (1,:) string = ["java", "python", "sys"]
11+
end
1212

13-
b = javaMethod("getOperatingSystemMXBean", "java.lang.management.ManagementFactory");
13+
fun = choose_method(method, "cpu_load");
1414

15-
if stdlib.java_api() < 14
16-
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getSystemCpuLoad()
17-
L = b.getSystemCpuLoad();
18-
else
19-
% https://docs.oracle.com/en/java/javase/21/docs/api/jdk.management/com/sun/management/OperatingSystemMXBean.html#getCpuLoad()
20-
L = b.getCpuLoad();
21-
end
15+
L = fun();
2216

23-
end
2417

25-
%!assert(cpu_load()>=0)
26-
%!assert(cpu_load()<=1)
18+
end

+stdlib/private/choose_method.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
if ~isunix(), continue, end
2525
end
2626
case "python"
27+
2728
has = @stdlib.has_python;
2829

2930
if endsWith(name, ["filesystem_type", "ram_total", "ram_free"])
3031
if ~stdlib.python.has_psutil(); continue, end
3132
end
33+
34+
if endsWith(name, "cpu_load")
35+
if ~isunix(), continue, end
36+
end
37+
3238
case {"legacy", "sys"}, has = true;
3339
case "native", has = stdlib.strempty(minVersion) || ~isMATLABReleaseOlderThan(minVersion);
3440
otherwise

test/TestJava.m

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ function test_java_api(tc)
2727
end
2828

2929

30-
function test_cpu_load(tc)
31-
tc.verifyGreaterThanOrEqual(stdlib.cpu_load(), 0)
32-
end
33-
34-
3530
function test_is_regular_file(tc)
3631
tc.verifyFalse(stdlib.is_regular_file(stdlib.null_file()), "null file is not a regular file")
3732
end

test/TestSys.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
is_admin_fun = {@stdlib.is_admin, @stdlib.sys.is_admin, @stdlib.dotnet.is_admin, @stdlib.java.is_admin, @stdlib.python.is_admin}
1414
ram_free_method = {'sys', 'java', 'python'}
1515
ram_total_method = {'sys', 'dotnet', 'java', 'python'}
16+
cpu_load_method = {"java", "python", "sys"}
1617
end
1718

1819
methods(TestClassSetup)
@@ -52,6 +53,19 @@ function test_get_pid(tc)
5253
end
5354

5455

56+
function test_cpu_load(tc, cpu_load_method)
57+
n = "stdlib." + cpu_load_method + ".cpu_load";
58+
h = @stdlib.cpu_load;
59+
tc.assertNotEmpty(which(n))
60+
try
61+
r = h(cpu_load_method);
62+
tc.verifyGreaterThan(r, 0.)
63+
catch e
64+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
65+
end
66+
end
67+
68+
5569
function test_get_shell(tc)
5670
tc.assumeFalse(tc.CI, "get_shell is not tested in CI due to platform differences")
5771
tc.verifyNotEmpty(stdlib.get_shell())

0 commit comments

Comments
 (0)