Skip to content

Commit 7e31acd

Browse files
committed
fix calculation of user and system time using the thread mx bean
1 parent 872631a commit 7e31acd

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ def test_gerusage():
6565
"ru_minflt", "ru_majflt", "ru_nswap", "ru_inblock", "ru_oublock", "ru_msgsnd", "ru_msgrcv", "ru_nsignals",
6666
"ru_nvcsw", "ru_nivcsw"
6767
]
68-
assert ru.ru_utime > 0
68+
assert ru.ru_utime >= 0
6969
assert ru.ru_stime >= 0
7070
assert ru.ru_maxrss > 0

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ResourceModuleBuiltins.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ PTuple getruusageThread(@SuppressWarnings("unused") int who) {
8181
long id = Thread.currentThread().getId();
8282
Runtime runtime = Runtime.getRuntime();
8383

84-
double ru_utime = -1; // time in user mode (float)
85-
double ru_stime = -1; // time in system mode (float)
84+
double ru_utime = 0; // time in user mode (float)
85+
double ru_stime = 0; // time in system mode (float)
8686
long ru_maxrss; // maximum resident set size
8787

8888
if (!TruffleOptions.AOT) {
8989
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
90-
ru_utime = threadMXBean.getThreadUserTime(id) / 1000000000.0;
91-
ru_stime = threadMXBean.getThreadCpuTime(id) / 1000000000.0;
90+
if (threadMXBean.isCurrentThreadCpuTimeSupported()) {
91+
ru_utime = threadMXBean.getThreadUserTime(id) / 1000000000.0;
92+
ru_stime = (threadMXBean.getThreadCpuTime(id) - threadMXBean.getThreadUserTime(id)) / 1000000000.0;
93+
}
94+
9295
if (threadMXBean instanceof com.sun.management.ThreadMXBean) {
9396
com.sun.management.ThreadMXBean thMxBean = (com.sun.management.ThreadMXBean) threadMXBean;
9497
ru_maxrss = thMxBean.getThreadAllocatedBytes(id);
@@ -128,17 +131,25 @@ PTuple getruusageThread(@SuppressWarnings("unused") int who) {
128131
PTuple getruusageSelf(@SuppressWarnings("unused") int who) {
129132
Runtime runtime = Runtime.getRuntime();
130133

131-
double ru_utime = -1; // time in user mode (float)
132-
double ru_stime = -1; // time in system mode (float)
134+
double ru_utime = 0; // time in user mode (float)
135+
double ru_stime = 0; // time in system mode (float)
133136
long ru_maxrss;
134137

135138
if (!TruffleOptions.AOT) {
136-
ru_utime = 0;
137-
ru_stime = 0;
138139
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
139-
for (long thId : threadMXBean.getAllThreadIds()) {
140-
ru_utime += threadMXBean.getThreadUserTime(thId) / 1000000000.0;
141-
ru_stime += threadMXBean.getThreadCpuTime(thId) / 1000000000.0;
140+
if (threadMXBean.isThreadCpuTimeSupported()) {
141+
for (long thId : threadMXBean.getAllThreadIds()) {
142+
long tu = threadMXBean.getThreadUserTime(thId);
143+
long tc = threadMXBean.getThreadCpuTime(thId);
144+
145+
if (tu != -1) {
146+
ru_utime += tu / 1000000000.0;
147+
}
148+
149+
if (tu != -1 && tc != -1) {
150+
ru_stime += (tc - tu) / 1000000000.0;
151+
}
152+
}
142153
}
143154

144155
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();

0 commit comments

Comments
 (0)