| 1 | /***************************************************************************** |
| 2 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | you may not use this file except in compliance with the License. |
| 4 | You may obtain a copy of the License at |
| 5 | |
| 6 | http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | |
| 8 | Unless required by applicable law or agreed to in writing, software |
| 9 | distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | See the License for the specific language governing permissions and |
| 12 | limitations under the License. |
| 13 | |
| 14 | See NOTICE file for details. |
| 15 | *****************************************************************************/ |
| 16 | #include <Python.h> |
| 17 | #include "jpype.h" |
| 18 | #include "pyjp.h" |
| 19 | #include "jp_reference_queue.h" |
| 20 | #include "jp_gc.h" |
| 21 | |
| 22 | #ifdef WIN32 |
| 23 | #define USE_PROCESS_INFO |
| 24 | #include <Windows.h> |
| 25 | #include <psapi.h> |
| 26 | |
| 27 | #elif __APPLE__ |
| 28 | #define USE_TASK_INFO |
| 29 | #include <unistd.h> |
| 30 | #include <sys/resource.h> |
| 31 | #include <mach/mach.h> |
| 32 | |
| 33 | #elif __GLIBC__ |
| 34 | // Linux doesn't have an available rss tally so use mallinfo |
| 35 | #define USE_MALLINFO |
| 36 | #include <malloc.h> |
| 37 | |
| 38 | #elif __linux__ |
| 39 | #define USE_PROC_INFO |
| 40 | #include <stdlib.h> |
| 41 | #include <unistd.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <fcntl.h> |
| 44 | static int statm_fd; |
| 45 | static int page_size; |
| 46 | |
| 47 | #else |
| 48 | #define USE_NONE |
| 49 | #endif |
| 50 | #define DELTA_LIMIT 20*1024*1024l |
| 51 | |
| 52 | size_t getWorkingSize() |
| 53 | { |
| 54 | size_t current = 0; |
| 55 | #if defined(USE_PROCESS_INFO) |
| 56 | PROCESS_MEMORY_COUNTERS pmc; |
| 57 | GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof (pmc)); |
| 58 | current = (size_t) pmc.WorkingSetSize; |
| 59 | |
| 60 | #elif defined(USE_TASK_INFO) |
| 61 | struct mach_task_basic_info info; |
| 62 | mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT; |
| 63 | if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t) & info, &count) == KERN_SUCCESS) |
| 64 | current = (size_t) info.resident_size; |
| 65 | |
| 66 | #elif defined(USE_PROC_INFO) |
| 67 | char bytes[32]; |
| 68 | lseek(statm_fd, SEEK_SET, 0); |
| 69 | int len = read(statm_fd, bytes, 32); |
| 70 | long long sz = 0; |
| 71 | int i = 0; |
| 72 | for (; i < len; i++) |
| 73 | { |
| 74 | if (bytes[i] == ' ') |
| 75 | break; |
| 76 | } |
| 77 | i++; |
| 78 | for (; i < len; i++) |
| 79 | { |
| 80 | if (bytes[i] == ' ') |
| 81 | return sz * page_size; |
| 82 | sz *= 10; |
| 83 | sz += bytes[i] - '0'; |
| 84 | } |
| 85 | return sz * page_size; |
| 86 | |
| 87 | #elif defined(USE_MALLINFO) |
| 88 | struct mallinfo mi; |
| 89 | mi = mallinfo(); |
| 90 | current = (size_t) mi.uordblks; |
| 91 | #endif |
| 92 | |
| 93 | return current; |
| 94 | } |
| 95 | |
| 96 | void triggerPythonGC(); |
| 97 | |
| 98 | void JPGarbageCollection::triggered() |
| 99 | { |
| 100 | // If we were triggered from Java call a Python cleanup |
| 101 | if (!in_python_gc) |
| 102 | { |
| 103 | // trigger Python gc |
| 104 | in_python_gc = true; |
| 105 | java_triggered = true; |
| 106 | java_count++; |
| 107 | |
| 108 | // Lock Python so we call trigger a GC |
| 109 | JPPyCallAcquire callback; |
| 110 | PyGC_Collect(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | JPGarbageCollection::JPGarbageCollection() |
| 115 | { |
| 116 | running = false; |
| 117 | in_python_gc = false; |
| 118 | java_triggered = false; |
| 119 | python_gc = nullptr; |
| 120 | _SystemClass = nullptr; |
| 121 | _gcMethodID = nullptr; |
| 122 | |
| 123 | last_python = 0; |
| 124 | last_java = 0; |
| 125 | low_water = 0; |
| 126 | high_water = 0; |
| 127 | limit = 0; |
| 128 | last = 0; |
| 129 | java_count = 0; |
| 130 | python_count = 0; |
| 131 | python_triggered = 0; |
| 132 | } |
| 133 | |
| 134 | void JPGarbageCollection::init(JPJavaFrame& frame) |
| 135 | { |
| 136 | #if defined(USE_PROC_INFO) |
| 137 | statm_fd = open("/proc/self/statm", O_RDONLY); |
| 138 | page_size = getpagesize(); |
| 139 | #endif |
| 140 | // Get the Python garbage collector |
| 141 | JPPyObject gc = JPPyObject::call(PyImport_ImportModule("gc")); |
| 142 | python_gc = gc.keep(); |
| 143 | |
| 144 | // Find the callbacks |
| 145 | JPPyObject callbacks = JPPyObject::call(PyObject_GetAttrString(python_gc, "callbacks")); |
| 146 | |
| 147 | // Hook up our callback |
| 148 | JPPyObject collect = JPPyObject::call(PyObject_GetAttrString(PyJPModule, "_collect")); |
| 149 | PyList_Append(callbacks.get(), collect.get()); |
| 150 | JP_PY_CHECK(); |
| 151 | |
| 152 | // Get the Java System gc so we can trigger |
| 153 | _SystemClass = (jclass) frame.NewGlobalRef(frame.FindClass("java/lang/System")); |
| 154 | _gcMethodID = frame.GetStaticMethodID(_SystemClass, "gc", "()V"); |
| 155 | |
| 156 | jclass ctxt = JPContext_global->m_ContextClass.get(); |
| 157 | _ContextClass = ctxt; |
| 158 | _totalMemoryID = frame.GetStaticMethodID(ctxt, "getTotalMemory", "()J"); |
| 159 | _freeMemoryID = frame.GetStaticMethodID(ctxt, "getFreeMemory", "()J"); |
| 160 | _maxMemoryID = frame.GetStaticMethodID(ctxt, "getMaxMemory", "()J"); |
| 161 | _usedMemoryID = frame.GetStaticMethodID(ctxt, "getUsedMemory", "()J"); |
| 162 | _heapMemoryID = frame.GetStaticMethodID(ctxt, "getHeapMemory", "()J"); |
| 163 | |
| 164 | running = true; |
| 165 | high_water = getWorkingSize(); |
| 166 | limit = high_water + DELTA_LIMIT; |
| 167 | } |
| 168 | |
| 169 | void JPGarbageCollection::shutdown() |
| 170 | { |
| 171 | running = false; |
| 172 | #if defined(USE_PROC_INFO) |
| 173 | close(statm_fd); |
| 174 | #endif |
| 175 | } |
| 176 | |
| 177 | void JPGarbageCollection::onStart() |
| 178 | { |
| 179 | // GCOVR_EXCL_START |
| 180 | // GC is triggered outside of user control. Including it in |
| 181 | // coverage just creates random statistics. |
| 182 | if (!running) |
| 183 | return; |
| 184 | getWorkingSize(); |
| 185 | in_python_gc = true; |
| 186 | // GCOVR_EXCL_STOP |
| 187 | } |
| 188 | |
| 189 | void JPGarbageCollection::onEnd() |
| 190 | { |
| 191 | // GCOVR_EXCL_START |
| 192 | // GC is triggered outside of user control. Including it in |
| 193 | // coverage just creates random statistics. |
| 194 | if (!running) |
| 195 | return; |
| 196 | if (java_triggered) |
| 197 | { |
| 198 | // Remove our lock so that we can watch for triggers |
| 199 | java_triggered = false; |
| 200 | return; |
| 201 | } |
| 202 | if (in_python_gc) |
| 203 | { |
| 204 | in_python_gc = false; |
| 205 | python_count++; |
| 206 | int run_gc = 0; |
| 207 | |
| 208 | size_t current = getWorkingSize(); |
| 209 | if (current > high_water) |
| 210 | high_water = current; |
| 211 | if (current < low_water) |
| 212 | low_water = current; |
| 213 | |
| 214 | if (java_triggered) |
| 215 | last_java = current; |
| 216 | else |
| 217 | last_python = current; |
| 218 | |
| 219 | // Things are getting better so use high water as limit |
| 220 | if (current == low_water) |
| 221 | { |
| 222 | limit = (limit + high_water) / 2; |
| 223 | if ( high_water > low_water + 4 * DELTA_LIMIT) |
| 224 | high_water = low_water + 4 * DELTA_LIMIT; |
| 225 | } |
| 226 | |
| 227 | if (last_python > current) |
| 228 | last_python = current; |
| 229 | |
| 230 | if (current < last) |
| 231 | { |
| 232 | last = current; |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | // Decide the policy |
| 237 | if (current > limit) |
| 238 | { |
| 239 | limit = high_water + DELTA_LIMIT; |
| 240 | run_gc = 1; |
| 241 | } |
| 242 | |
| 243 | // Predict if we will cross the limit soon. |
| 244 | Py_ssize_t pred = current + 2 * (current - last); |
| 245 | last = current; |
| 246 | if ((Py_ssize_t) pred > (Py_ssize_t) limit) |
| 247 | { |
| 248 | run_gc = 2; |
| 249 | limit = high_water + (high_water>>3) + 8 * (current - last); |
| 250 | } |
| 251 | |
| 252 | #if 0 |
| 253 | { |
| 254 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 255 | jlong totalMemory = frame.CallStaticLongMethodA(_ContextClass, _totalMemoryID, nullptr); |
| 256 | jlong freeMemory = frame.CallStaticLongMethodA(_ContextClass, _freeMemoryID, nullptr); |
| 257 | jlong maxMemory = frame.CallStaticLongMethodA(_ContextClass, _maxMemoryID, nullptr); |
| 258 | jlong usedMemory = frame.CallStaticLongMethodA(_ContextClass, _usedMemoryID, nullptr); |
| 259 | jlong heapMemory = frame.CallStaticLongMethodA(_ContextClass, _heapMemoryID, nullptr); |
| 260 | printf("consider gc run=%d (current=%ld, low=%ld, high=%ld, limit=%ld) %ld\n", run_gc, |
| 261 | current, low_water, high_water, limit, limit - pred); |
| 262 | printf(" java total=%ld free=%ld max=%ld used=%ld heap=%ld\n", totalMemory, freeMemory, maxMemory, usedMemory, heapMemory); |
| 263 | } |
| 264 | #endif |
| 265 | |
| 266 | if (run_gc > 0) |
| 267 | { |
| 268 | // Move up the low water |
| 269 | low_water = (low_water + high_water) / 2; |
| 270 | // Don't reset the limit if it was count triggered |
| 271 | JPJavaFrame frame = JPJavaFrame::outer(); |
| 272 | frame.CallStaticVoidMethodA(_SystemClass, _gcMethodID, nullptr); |
| 273 | python_triggered++; |
| 274 | } |
| 275 | } |
| 276 | // GCOVR_EXCL_STOP |
| 277 | } |
| 278 | |
| 279 | void JPGarbageCollection::getStats(JPGCStats& stats) |
| 280 | { |
| 281 | // GCOVR_EXCL_START |
| 282 | stats.current_rss = getWorkingSize(); |
| 283 | stats.min_rss = low_water; |
| 284 | stats.max_rss = high_water; |
| 285 | stats.java_rss = last_java; |
| 286 | stats.python_rss = last_python; |
| 287 | stats.python_triggered = python_triggered; |
| 288 | // GCOVR_EXCL_STOP |
| 289 | } |