Skip to content

Commit 8531b13

Browse files
committed
GR-16679: add simple impl for missing resource.getrusage builtin
1 parent ab87b0a commit 8531b13

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.function.Supplier;
4141
import java.util.logging.Level;
4242

43+
import com.oracle.graal.python.builtins.modules.ResourceModuleBuiltins;
4344
import org.graalvm.nativeimage.ImageInfo;
4445

4546
import com.oracle.graal.python.PythonLanguage;
@@ -233,6 +234,7 @@ private static final String[] initializeCoreFiles() {
233234
"java",
234235
"pyio_patches",
235236
"pwd",
237+
"resource",
236238
"_contextvars"));
237239
// must be last
238240
coreFiles.add("final_patches");
@@ -348,6 +350,7 @@ private static final PythonBuiltins[] initializeBuiltins() {
348350
new LockBuiltins(),
349351
new RLockBuiltins(),
350352
new PwdModuleBuiltins(),
353+
new ResourceModuleBuiltins(),
351354
new ContextvarsModuleBuiltins()));
352355
if (!TruffleOptions.AOT) {
353356
ServiceLoader<PythonBuiltins> providers = ServiceLoader.load(PythonBuiltins.class, Python3Core.class.getClassLoader());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules;
42+
43+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.NotImplementedError;
44+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
45+
46+
import java.lang.management.ManagementFactory;
47+
import java.lang.management.MemoryMXBean;
48+
import java.lang.management.MemoryUsage;
49+
import java.lang.management.ThreadMXBean;
50+
import java.util.List;
51+
52+
import com.oracle.graal.python.builtins.Builtin;
53+
import com.oracle.graal.python.builtins.CoreFunctions;
54+
import com.oracle.graal.python.builtins.PythonBuiltins;
55+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
56+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
57+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
58+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
59+
import com.oracle.truffle.api.dsl.Fallback;
60+
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
61+
import com.oracle.truffle.api.dsl.NodeFactory;
62+
import com.oracle.truffle.api.dsl.Specialization;
63+
64+
@CoreFunctions(defineModule = "resource")
65+
public class ResourceModuleBuiltins extends PythonBuiltins {
66+
@Override
67+
protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() {
68+
return ResourceModuleBuiltinsFactory.getFactories();
69+
}
70+
71+
@Builtin(name = "getrusage", minNumOfPositionalArgs = 1)
72+
@GenerateNodeFactory
73+
abstract static class GetRuUsageNode extends PythonBuiltinNode {
74+
protected static int RUSAGE_CHILDREN = -1;
75+
protected static int RUSAGE_SELF = 0;
76+
protected static int RUSAGE_THREAD = 1;
77+
78+
@Specialization(guards = {"who == RUSAGE_THREAD"})
79+
@TruffleBoundary
80+
PTuple getruusageThread(@SuppressWarnings("unused") int who) {
81+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
82+
long id = Thread.currentThread().getId();
83+
double ru_utime = threadMXBean.getThreadUserTime(id); // time in user mode (float)
84+
double ru_stime = threadMXBean.getThreadCpuTime(id); // time in system mode (float)
85+
86+
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);
90+
} else {
91+
Runtime runtime = Runtime.getRuntime();
92+
ru_maxrss = runtime.maxMemory();
93+
}
94+
95+
long ru_ixrss = -1; // shared memory size
96+
long ru_idrss = -1; // unshared memory size
97+
long ru_isrss = -1; // unshared stack size
98+
long ru_minflt = -1; // page faults not requiring I/O
99+
long ru_majflt = -1; // page faults requiring I/O
100+
long ru_nswap = -1; // number of swap outs
101+
long ru_inblock = -1; // block input operations
102+
long ru_oublock = -1; // block output operations
103+
long ru_msgsnd = -1; // messages sent
104+
long ru_msgrcv = -1; // messages received
105+
long ru_nsignals = -1; // signals received
106+
long ru_nvcsw = -1; // voluntary context switches
107+
long ru_nivcsw = -1; // nvoluntary context switches
108+
return factory().createTuple(new Object[]{ru_utime, ru_stime, ru_maxrss, ru_ixrss, ru_idrss, ru_isrss,
109+
ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals,
110+
ru_nvcsw, ru_nivcsw});
111+
}
112+
113+
@Specialization(guards = {"who == RUSAGE_SELF"})
114+
@TruffleBoundary
115+
PTuple getruusageSelf(@SuppressWarnings("unused") int who) {
116+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
117+
double ru_utime = 0; // time in user mode (float)
118+
double ru_stime = 0; // time in system mode (float)
119+
for (long thId : threadMXBean.getAllThreadIds()) {
120+
ru_utime += threadMXBean.getThreadUserTime(thId);
121+
ru_stime += threadMXBean.getThreadCpuTime(thId);
122+
}
123+
124+
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
125+
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
126+
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
127+
long ru_maxrss = heapMemoryUsage.getCommitted() + nonHeapMemoryUsage.getCommitted(); // maximum
128+
// resident
129+
// set
130+
// size
131+
132+
long ru_ixrss = -1; // shared memory size
133+
long ru_idrss = -1; // unshared memory size
134+
long ru_isrss = -1; // unshared stack size
135+
long ru_minflt = -1; // page faults not requiring I/O
136+
long ru_majflt = -1; // page faults requiring I/O
137+
long ru_nswap = -1; // number of swap outs
138+
long ru_inblock = -1; // block input operations
139+
long ru_oublock = -1; // block output operations
140+
long ru_msgsnd = -1; // messages sent
141+
long ru_msgrcv = -1; // messages received
142+
long ru_nsignals = -1; // signals received
143+
long ru_nvcsw = -1; // voluntary context switches
144+
long ru_nivcsw = -1; // nvoluntary context switches
145+
return factory().createTuple(new Object[]{ru_utime, ru_stime, ru_maxrss, ru_ixrss, ru_idrss, ru_isrss,
146+
ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals,
147+
ru_nvcsw, ru_nivcsw});
148+
}
149+
150+
@Fallback
151+
PTuple getruusage(@SuppressWarnings("unused") Object who) {
152+
throw raise(ValueError, "ruusage usage not yet implemented for specified arg.");
153+
}
154+
}
155+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
RUSAGE_CHILDREN = -1
41+
RUSAGE_SELF = 0
42+
RUSAGE_THREAD = 1
43+
44+
from _descriptor import make_named_tuple_class
45+
46+
ru_result = make_named_tuple_class("ru_result", [
47+
"ru_utime", "ru_stime", "ru_maxrss", "ru_ixrss", "ru_idrss", "ru_isrss",
48+
"ru_minflt", "ru_majflt", "ru_nswap", "ru_inblock", "ru_oublock", "ru_msgsnd", "ru_msgrcv", "ru_nsignals",
49+
"ru_nvcsw", "ru_nivcsw"
50+
])
51+
52+
old_getrusage = getrusage
53+
54+
@__builtin__
55+
def getrusage(who):
56+
return ru_result(old_getrusage(who))

0 commit comments

Comments
 (0)