Skip to content

Commit 95c6327

Browse files
committed
add unittest for the resource module
- test that the module imports - limited test for getrusage
1 parent 8531b13 commit 95c6327

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
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/modules/ResourceModuleBuiltins.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ abstract static class GetRuUsageNode extends PythonBuiltinNode {
8080
PTuple getruusageThread(@SuppressWarnings("unused") int who) {
8181
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
8282
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)
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)
8585

8686
long ru_maxrss; // maximum resident set size
8787
if (threadMXBean instanceof com.sun.management.ThreadMXBean) {
@@ -92,7 +92,13 @@ PTuple getruusageThread(@SuppressWarnings("unused") int who) {
9292
ru_maxrss = runtime.maxMemory();
9393
}
9494

95-
long ru_ixrss = -1; // shared memory size
95+
String osName = System.getProperty("os.name");
96+
if (osName.contains("Linux")) {
97+
// peak memory usage (kilobytes on Linux
98+
ru_maxrss /= 1024;
99+
}
100+
101+
long ru_ixrss = -1; // shared memory size
96102
long ru_idrss = -1; // unshared memory size
97103
long ru_isrss = -1; // unshared stack size
98104
long ru_minflt = -1; // page faults not requiring I/O
@@ -117,19 +123,22 @@ PTuple getruusageSelf(@SuppressWarnings("unused") int who) {
117123
double ru_utime = 0; // time in user mode (float)
118124
double ru_stime = 0; // time in system mode (float)
119125
for (long thId : threadMXBean.getAllThreadIds()) {
120-
ru_utime += threadMXBean.getThreadUserTime(thId);
121-
ru_stime += threadMXBean.getThreadCpuTime(thId);
126+
ru_utime += threadMXBean.getThreadUserTime(thId) / 1000000000.0;
127+
ru_stime += threadMXBean.getThreadCpuTime(thId) / 1000000000.0;
122128
}
123129

124130
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
125131
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
126132
MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
127-
long ru_maxrss = heapMemoryUsage.getCommitted() + nonHeapMemoryUsage.getCommitted(); // maximum
128-
// resident
129-
// set
130-
// size
133+
long ru_maxrss = heapMemoryUsage.getCommitted() + nonHeapMemoryUsage.getCommitted();
134+
135+
String osName = System.getProperty("os.name");
136+
if (osName.contains("Linux")) {
137+
// peak memory usage (kilobytes on Linux
138+
ru_maxrss /= 1024;
139+
}
131140

132-
long ru_ixrss = -1; // shared memory size
141+
long ru_ixrss = -1; // shared memory size
133142
long ru_idrss = -1; // unshared memory size
134143
long ru_isrss = -1; // unshared stack size
135144
long ru_minflt = -1; // page faults not requiring I/O

0 commit comments

Comments
 (0)