|
| 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 | +} |
0 commit comments