Add a trace malloc test

This commit is contained in:
nicolargo 2022-02-20 09:38:53 +01:00
parent f7971620ee
commit 74a2e6bbe8
4 changed files with 50 additions and 8 deletions

View File

@ -75,6 +75,10 @@ show-issue: venv
profiling: venv venv-dev profiling: venv venv-dev
@echo "Please complete and run: sudo ./venv/bin/py-spy record -o ./docs/_static/glances-flame.svg -d 60 -s --pid <GLANCES PID>" @echo "Please complete and run: sudo ./venv/bin/py-spy record -o ./docs/_static/glances-flame.svg -d 60 -s --pid <GLANCES PID>"
trace-malloc: venv
@echo "Test is running, please wait ~ 30 secondes..."
./venv/bin/python -m glances -C ./conf/glances.conf --trace-malloc --stop-after 15 --quiet
release-note: release-note:
git --no-pager log $(LASTTAG)..HEAD --first-parent --pretty=format:"* %s" git --no-pager log $(LASTTAG)..HEAD --first-parent --pretty=format:"* %s"
@echo "\n" @echo "\n"

View File

@ -42,6 +42,7 @@ except ImportError:
# Import Glances libs # Import Glances libs
# Note: others Glances libs will be imported optionally # Note: others Glances libs will be imported optionally
from glances.compat import PY3
from glances.logger import logger from glances.logger import logger
from glances.main import GlancesMain from glances.main import GlancesMain
from glances.timer import Counter from glances.timer import Counter
@ -64,6 +65,9 @@ if psutil_version_info < psutil_min_version:
print('psutil 5.3.0 or higher is needed. Glances cannot start.') print('psutil 5.3.0 or higher is needed. Glances cannot start.')
sys.exit(1) sys.exit(1)
# Trac malloc is only available on Python 3.4 or higher
if PY3:
import tracemalloc
def __signal_handler(signal, frame): def __signal_handler(signal, frame):
"""Callback for CTRL-C.""" """Callback for CTRL-C."""
@ -81,6 +85,8 @@ def end():
logger.info("Glances stopped (key pressed: CTRL-C)") logger.info("Glances stopped (key pressed: CTRL-C)")
# The end... # The end...
sys.exit(0) sys.exit(0)
@ -91,6 +97,9 @@ def start(config, args):
# Load mode # Load mode
global mode global mode
if args.trace_malloc:
tracemalloc.start()
start_duration = Counter() start_duration = Counter()
if core.is_standalone(): if core.is_standalone():
@ -118,6 +127,14 @@ def start(config, args):
# Serve forever # Serve forever
mode.serve_forever() mode.serve_forever()
if args.trace_malloc:
# See more options here: https://docs.python.org/3/library/tracemalloc.html
snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics("lineno")
print("[ Trace malloc - Top 10 ]")
for stat in top_stats[:10]:
print(stat)
# Shutdown # Shutdown
mode.end() mode.end()

View File

@ -24,7 +24,7 @@ import sys
import tempfile import tempfile
from glances import __version__, psutil_version from glances import __version__, psutil_version
from glances.compat import input, disable, enable from glances.compat import input, disable, enable, PY3
from glances.config import Config from glances.config import Config
from glances.globals import WINDOWS from glances.globals import WINDOWS
from glances.processes import sort_processes_key_list from glances.processes import sort_processes_key_list
@ -358,6 +358,13 @@ Examples of use:
dest='cached_time', dest='cached_time',
help='set the server cache time [default: {} sec]'.format(self.cached_time), help='set the server cache time [default: {} sec]'.format(self.cached_time),
) )
parser.add_argument(
'--stop-after',
default=None,
type=int,
dest='stop_after',
help='stop Glances after n refresh',
)
parser.add_argument( parser.add_argument(
'--open-web-browser', '--open-web-browser',
action='store_true', action='store_true',
@ -415,6 +422,13 @@ Examples of use:
dest='stdout_issue', dest='stdout_issue',
help='test all plugins and exit (please copy/paste the output if you open an issue)', help='test all plugins and exit (please copy/paste the output if you open an issue)',
) )
parser.add_argument(
'--trace-malloc',
default=False,
action='store_true',
dest='trace_malloc',
help='test memory leak with tracemalloc (python 3.4 or higher needed)',
)
parser.add_argument( parser.add_argument(
'--api-doc', default=None, action='store_true', dest='stdout_apidoc', help='display fields descriptions' '--api-doc', default=None, action='store_true', dest='stdout_apidoc', help='display fields descriptions'
) )
@ -667,10 +681,15 @@ Examples of use:
sys.exit(2) sys.exit(2)
# Disable HDDTemp if sensors are disabled # Disable HDDTemp if sensors are disabled
if getattr(args, 'disable_sensors', False): if getattr(self.args, 'disable_sensors', False):
disable(args, 'hddtemp') disable(self.args, 'hddtemp')
logger.debug("Sensors and HDDTemp are disabled") logger.debug("Sensors and HDDTemp are disabled")
if getattr(self.args, 'trace_malloc', True) and not PY3:
self.args.trace_malloc = False
logger.critical("trace_malloc is only available with Python 3")
sys.exit(2)
# Let the plugins known the Glances mode # Let the plugins known the Glances mode
self.args.is_standalone = self.is_standalone() self.args.is_standalone = self.is_standalone()
self.args.is_client = self.is_client() self.args.is_client = self.is_client()

View File

@ -165,9 +165,11 @@ class GlancesStandalone(object):
def serve_forever(self): def serve_forever(self):
"""Wrapper to the serve_forever function.""" """Wrapper to the serve_forever function."""
# loop = True if self.args.stop_after:
# while loop: for _ in range(self.args.stop_after):
# loop = self.__serve_once() if not self.__serve_once():
break
else:
while self.__serve_once(): while self.__serve_once():
pass pass
self.end() self.end()