Change in vdsm[ovirt-3.5]: WIP profiling: memory: port from dowser to tracemalloc

fromani at redhat.com fromani at redhat.com
Tue Feb 16 10:40:52 UTC 2016


Francesco Romani has uploaded a new change for review.

Change subject: WIP profiling: memory: port from dowser to tracemalloc
......................................................................

WIP profiling: memory: port from dowser to tracemalloc

** WORK IN PROGRESS **

Change-Id: If00a3af13ea48bb75d40ed11e56de04d90452ff7
Signed-off-by: Francesco Romani <fromani at redhat.com>
---
M lib/vdsm/profiling/memory.py
1 file changed, 43 insertions(+), 31 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/95/47595/3

diff --git a/lib/vdsm/profiling/memory.py b/lib/vdsm/profiling/memory.py
index c15d1aa..61ecd92 100755
--- a/lib/vdsm/profiling/memory.py
+++ b/lib/vdsm/profiling/memory.py
@@ -1,5 +1,5 @@
 #
-# Copyright 2014 Red Hat, Inc.
+# Copyright 2015 Red Hat, Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -22,20 +22,27 @@
 This module provides memory profiling.
 """
 
+import gc
 import logging
+import os
 import threading
+import time
 
+from vdsm import constants
+from vdsm.compat import pickle
 from vdsm.config import config
-from vdsm.utils import traceback
 
 from .errors import UsageError
 
-# Import modules lazily when profile is started
-dowser = None
-cherrypy = None
+# Import tracemalloc lazily when profile is started
+tracemalloc = None
+
+# Defaults
+
+_FRAMES = 25
+
 
 _lock = threading.Lock()
-_thread = None
 
 
 def start():
@@ -47,7 +54,7 @@
 def stop():
     """ Stops application memory profiling """
     if is_enabled():
-        _stop_profiling()
+        _stop_profiling(_make_snapshot_name())
 
 
 def is_enabled():
@@ -55,48 +62,53 @@
 
 
 def is_running():
-    return _thread is not None
+    return tracemalloc and tracemalloc.is_tracing()
 
 
- at traceback()
-def _memory_viewer():
-    cherrypy.tree.mount(dowser.Root())
+def snapshot(filename=None):
+    with _lock:
+        if not is_running():
+            raise UsageError('Memory profiler must be running '
+                             'to take snapshots')
+        name = filename or _make_snapshot_name()
+        gc.collect()
+        snap = tracemalloc.take_snapshot()
+        with open(name, 'wb') as fp:
+            # Pickle version 2 can be read by Python 2 and Python 3
+            pickle.dump(snap, fp, 2)
+            snap = None
+        return name
 
-    cherrypy.config.update({
-        'server.socket_host': '0.0.0.0',
-        'server.socket_port': config.getint('vars', 'memory_profile_port')})
 
-    cherrypy.engine.start()
+def _make_snapshot_name():
+    return os.path.join(constants.P_VDSM_RUN,
+                        'vdsm_memory_%s.pickle' % _make_timestamp())
+
+
+def _make_timestamp():
+    return time.strftime('%Y%m%d_%H%M%S')
 
 
 def _start_profiling():
-    global cherrypy
-    global dowser
-    global _thread
+    global tracemalloc
 
     logging.debug("Starting memory profiling")
 
-    import cherrypy
-    import dowser
+    import tracemalloc
     # this nonsense makes pyflakes happy
-    cherrypy
-    dowser
+    tracemalloc
 
     with _lock:
         if is_running():
             raise UsageError('Memory profiler is already running')
-        _thread = threading.Thread(name='memprofile',
-                                   target=_memory_viewer)
-        _thread.daemon = True
-        _thread.start()
+        tracemalloc.start(_FRAMES)
 
 
-def _stop_profiling():
+def _stop_profiling(filename):
     global _thread
     logging.debug("Stopping memory profiling")
     with _lock:
         if is_running():
-            cherrypy.engine.exit()
-            cherrypy.engine.block()
-            _thread.join()
-            _thread = None
+            snapshot(filename)
+            tracemalloc.clear_traces()
+            tracemalloc.stop()


-- 
To view, visit https://gerrit.ovirt.org/47595
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If00a3af13ea48bb75d40ed11e56de04d90452ff7
Gerrit-PatchSet: 3
Gerrit-Project: vdsm
Gerrit-Branch: ovirt-3.5
Gerrit-Owner: Francesco Romani <fromani at redhat.com>
Gerrit-Reviewer: gerrit-hooks <automation at ovirt.org>


More information about the vdsm-patches mailing list