Skip to content

Commit ea72413

Browse files
committed
[GR-16679] add simple impl for missing resource.getrusage builtin
PullRequest: graalpython/562
2 parents ab87b0a + 7e31acd commit ea72413

File tree

4 files changed

+321
-0
lines changed

4 files changed

+321
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
41+
def assert_raises(err, fn, *args, **kwargs):
42+
raised = False
43+
try:
44+
fn(*args, **kwargs)
45+
except err:
46+
raised = True
47+
assert raised
48+
49+
50+
def test_import():
51+
imported = True
52+
try:
53+
import resource
54+
except ImportError:
55+
imported = False
56+
assert imported
57+
58+
59+
def test_gerusage():
60+
from resource import getrusage, RUSAGE_SELF, RUSAGE_THREAD
61+
for who in [RUSAGE_SELF, RUSAGE_THREAD]:
62+
ru = getrusage(who)
63+
attrs = [
64+
"ru_utime", "ru_stime", "ru_maxrss", "ru_ixrss", "ru_idrss", "ru_isrss",
65+
"ru_minflt", "ru_majflt", "ru_nswap", "ru_inblock", "ru_oublock", "ru_msgsnd", "ru_msgrcv", "ru_nsignals",
66+
"ru_nvcsw", "ru_nivcsw"
67+
]
68+
assert ru.ru_utime >= 0
69+
assert ru.ru_stime >= 0
70+
assert ru.ru_maxrss > 0

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,192 @@
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.ValueError;
44+
45+
import java.lang.management.ManagementFactory;
46+
import java.lang.management.MemoryMXBean;
47+
import java.lang.management.MemoryUsage;
48+
import java.lang.management.ThreadMXBean;
49+
import java.util.List;
50+
51+
import com.oracle.graal.python.builtins.Builtin;
52+
import com.oracle.graal.python.builtins.CoreFunctions;
53+
import com.oracle.graal.python.builtins.PythonBuiltins;
54+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
55+
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
56+
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
57+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
58+
import com.oracle.truffle.api.TruffleOptions;
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+
long id = Thread.currentThread().getId();
82+
Runtime runtime = Runtime.getRuntime();
83+
84+
double ru_utime = 0; // time in user mode (float)
85+
double ru_stime = 0; // time in system mode (float)
86+
long ru_maxrss; // maximum resident set size
87+
88+
if (!TruffleOptions.AOT) {
89+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
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+
95+
if (threadMXBean instanceof com.sun.management.ThreadMXBean) {
96+
com.sun.management.ThreadMXBean thMxBean = (com.sun.management.ThreadMXBean) threadMXBean;
97+
ru_maxrss = thMxBean.getThreadAllocatedBytes(id);
98+
} else {
99+
ru_maxrss = runtime.maxMemory();
100+
}
101+
} else {
102+
ru_maxrss = runtime.maxMemory();
103+
}
104+
105+
String osName = System.getProperty("os.name");
106+
if (osName.contains("Linux")) {
107+
// peak memory usage (kilobytes on Linux
108+
ru_maxrss /= 1024;
109+
}
110+
111+
long ru_ixrss = -1; // shared memory size
112+
long ru_idrss = -1; // unshared memory size
113+
long ru_isrss = -1; // unshared stack size
114+
long ru_minflt = -1; // page faults not requiring I/O
115+
long ru_majflt = -1; // page faults requiring I/O
116+
long ru_nswap = -1; // number of swap outs
117+
long ru_inblock = -1; // block input operations
118+
long ru_oublock = -1; // block output operations
119+
long ru_msgsnd = -1; // messages sent
120+
long ru_msgrcv = -1; // messages received
121+
long ru_nsignals = -1; // signals received
122+
long ru_nvcsw = -1; // voluntary context switches
123+
long ru_nivcsw = -1; // nvoluntary context switches
124+
return factory().createTuple(new Object[]{ru_utime, ru_stime, ru_maxrss, ru_ixrss, ru_idrss, ru_isrss,
125+
ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals,
126+
ru_nvcsw, ru_nivcsw});
127+
}
128+
129+
@Specialization(guards = {"who == RUSAGE_SELF"})
130+
@TruffleBoundary
131+
PTuple getruusageSelf(@SuppressWarnings("unused") int who) {
132+
Runtime runtime = Runtime.getRuntime();
133+
134+
double ru_utime = 0; // time in user mode (float)
135+
double ru_stime = 0; // time in system mode (float)
136+
long ru_maxrss;
137+
138+
if (!TruffleOptions.AOT) {
139+
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
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+
}
153+
}
154+
155+
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
156+
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
157+
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
158+
ru_maxrss = heapMemoryUsage.getCommitted() + nonHeapMemoryUsage.getCommitted();
159+
} else {
160+
ru_maxrss = runtime.maxMemory();
161+
}
162+
163+
String osName = System.getProperty("os.name");
164+
if (osName.contains("Linux")) {
165+
// peak memory usage (kilobytes on Linux
166+
ru_maxrss /= 1024;
167+
}
168+
169+
long ru_ixrss = -1; // shared memory size
170+
long ru_idrss = -1; // unshared memory size
171+
long ru_isrss = -1; // unshared stack size
172+
long ru_minflt = -1; // page faults not requiring I/O
173+
long ru_majflt = -1; // page faults requiring I/O
174+
long ru_nswap = -1; // number of swap outs
175+
long ru_inblock = -1; // block input operations
176+
long ru_oublock = -1; // block output operations
177+
long ru_msgsnd = -1; // messages sent
178+
long ru_msgrcv = -1; // messages received
179+
long ru_nsignals = -1; // signals received
180+
long ru_nvcsw = -1; // voluntary context switches
181+
long ru_nivcsw = -1; // nvoluntary context switches
182+
return factory().createTuple(new Object[]{ru_utime, ru_stime, ru_maxrss, ru_ixrss, ru_idrss, ru_isrss,
183+
ru_minflt, ru_majflt, ru_nswap, ru_inblock, ru_oublock, ru_msgsnd, ru_msgrcv, ru_nsignals,
184+
ru_nvcsw, ru_nivcsw});
185+
}
186+
187+
@Fallback
188+
PTuple getruusage(@SuppressWarnings("unused") Object who) {
189+
throw raise(ValueError, "ruusage usage not yet implemented for specified arg.");
190+
}
191+
}
192+
}
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)