4040 */
4141package com .oracle .graal .python .builtins .modules ;
4242
43- import static com .oracle .graal .python .runtime .exception .PythonErrorType .NotImplementedError ;
4443import static com .oracle .graal .python .runtime .exception .PythonErrorType .ValueError ;
4544
4645import java .lang .management .ManagementFactory ;
5655import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
5756import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
5857import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
58+ import com .oracle .truffle .api .TruffleOptions ;
5959import com .oracle .truffle .api .dsl .Fallback ;
6060import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
6161import com .oracle .truffle .api .dsl .NodeFactory ;
@@ -78,17 +78,24 @@ abstract static class GetRuUsageNode extends PythonBuiltinNode {
7878 @ Specialization (guards = {"who == RUSAGE_THREAD" })
7979 @ TruffleBoundary
8080 PTuple getruusageThread (@ SuppressWarnings ("unused" ) int who ) {
81- ThreadMXBean threadMXBean = ManagementFactory .getThreadMXBean ();
8281 long id = Thread .currentThread ().getId ();
83- double ru_utime = threadMXBean .getThreadUserTime (id ) / 1000000000.0 ; // time in user mode (float)
84- double ru_stime = threadMXBean .getThreadCpuTime (id ) / 1000000000.0 ; // time in system mode (float)
82+ Runtime runtime = Runtime .getRuntime ();
8583
84+ double ru_utime = -1 ; // time in user mode (float)
85+ double ru_stime = -1 ; // time in system mode (float)
8686 long ru_maxrss ; // maximum resident set size
87- if (threadMXBean instanceof com .sun .management .ThreadMXBean ) {
88- com .sun .management .ThreadMXBean thMxBean = (com .sun .management .ThreadMXBean ) threadMXBean ;
89- ru_maxrss = thMxBean .getThreadAllocatedBytes (id );
87+
88+ if (!TruffleOptions .AOT ) {
89+ ThreadMXBean threadMXBean = ManagementFactory .getThreadMXBean ();
90+ ru_utime = threadMXBean .getThreadUserTime (id ) / 1000000000.0 ;
91+ ru_stime = threadMXBean .getThreadCpuTime (id ) / 1000000000.0 ;
92+ if (threadMXBean instanceof com .sun .management .ThreadMXBean ) {
93+ com .sun .management .ThreadMXBean thMxBean = (com .sun .management .ThreadMXBean ) threadMXBean ;
94+ ru_maxrss = thMxBean .getThreadAllocatedBytes (id );
95+ } else {
96+ ru_maxrss = runtime .maxMemory ();
97+ }
9098 } else {
91- Runtime runtime = Runtime .getRuntime ();
9299 ru_maxrss = runtime .maxMemory ();
93100 }
94101
@@ -119,18 +126,28 @@ PTuple getruusageThread(@SuppressWarnings("unused") int who) {
119126 @ Specialization (guards = {"who == RUSAGE_SELF" })
120127 @ TruffleBoundary
121128 PTuple getruusageSelf (@ SuppressWarnings ("unused" ) int who ) {
122- ThreadMXBean threadMXBean = ManagementFactory .getThreadMXBean ();
123- double ru_utime = 0 ; // time in user mode (float)
124- double ru_stime = 0 ; // time in system mode (float)
125- for (long thId : threadMXBean .getAllThreadIds ()) {
126- ru_utime += threadMXBean .getThreadUserTime (thId ) / 1000000000.0 ;
127- ru_stime += threadMXBean .getThreadCpuTime (thId ) / 1000000000.0 ;
128- }
129+ Runtime runtime = Runtime .getRuntime ();
130+
131+ double ru_utime = -1 ; // time in user mode (float)
132+ double ru_stime = -1 ; // time in system mode (float)
133+ long ru_maxrss ;
134+
135+ if (!TruffleOptions .AOT ) {
136+ ru_utime = 0 ;
137+ ru_stime = 0 ;
138+ 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 ;
142+ }
129143
130- MemoryMXBean memoryMXBean = ManagementFactory .getMemoryMXBean ();
131- MemoryUsage heapMemoryUsage = memoryMXBean .getHeapMemoryUsage ();
132- MemoryUsage nonHeapMemoryUsage = memoryMXBean .getNonHeapMemoryUsage ();
133- long ru_maxrss = heapMemoryUsage .getCommitted () + nonHeapMemoryUsage .getCommitted ();
144+ MemoryMXBean memoryMXBean = ManagementFactory .getMemoryMXBean ();
145+ MemoryUsage heapMemoryUsage = memoryMXBean .getHeapMemoryUsage ();
146+ MemoryUsage nonHeapMemoryUsage = memoryMXBean .getNonHeapMemoryUsage ();
147+ ru_maxrss = heapMemoryUsage .getCommitted () + nonHeapMemoryUsage .getCommitted ();
148+ } else {
149+ ru_maxrss = runtime .maxMemory ();
150+ }
134151
135152 String osName = System .getProperty ("os.name" );
136153 if (osName .contains ("Linux" )) {
0 commit comments