Repository: ambari Updated Branches: refs/heads/branch-2.4 d1da45b8c -> 3a942404d refs/heads/trunk 737e60516 -> dfd1ce492
AMBARI-17527. Setup logging to find memory leak rootcause (aonishuk) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/dfd1ce49 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/dfd1ce49 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/dfd1ce49 Branch: refs/heads/trunk Commit: dfd1ce49234b935cfb73f3a7c1b3fd558d2fe5b8 Parents: 737e605 Author: Andrew Onishuk <[email protected]> Authored: Fri Jul 1 19:02:42 2016 +0300 Committer: Andrew Onishuk <[email protected]> Committed: Fri Jul 1 19:02:42 2016 +0300 ---------------------------------------------------------------------- .../src/main/python/ambari_agent/main.py | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/dfd1ce49/ambari-agent/src/main/python/ambari_agent/main.py ---------------------------------------------------------------------- diff --git a/ambari-agent/src/main/python/ambari_agent/main.py b/ambari-agent/src/main/python/ambari_agent/main.py index 201264c..f93722f 100644 --- a/ambari-agent/src/main/python/ambari_agent/main.py +++ b/ambari-agent/src/main/python/ambari_agent/main.py @@ -18,6 +18,50 @@ See the License for the specific language governing permissions and limitations under the License. ''' +import gc +import traceback +import threading +import time + +def setup_memory_leak_debugger(): + gc.disable_old = gc.disable + gc.enable_old = gc.enable + gc.isenabled_old = gc.isenabled + + MEMORY_LEAK_DEBUG_FILEPATH = "/var/log/ambari-agent/memory_leak_debug.out" + def print_debug_info(result): + time_prefix = time.strftime('%X %x') + thread_repr = repr(threading.currentThread()) + stack_trace = ''.join(traceback.format_stack()).strip() + + message = '{0} | {1}\n{2}\n'.format(time_prefix, thread_repr, stack_trace) + if result: + message = '{0}Result = {1}\n'.format(message, result) + + with open(MEMORY_LEAK_DEBUG_FILEPATH, "a") as f: + f.write(message) + + def disable_new(): + result = gc.disable_old() + print_debug_info(result) + return result + + def enable_new(): + result = gc.enable_old() + print_debug_info(result) + return result + + def isenabled_new(): + result = gc.isenabled_old() + print_debug_info(result) + return result + + gc.disable = disable_new + gc.enable = enable_new + gc.isenabled = isenabled_new + +setup_memory_leak_debugger() + import logging.handlers import logging.config import signal
