diff --git a/glances/__init__.py b/glances/__init__.py index f7f6e52f3..cf2e1793c 100644 --- a/glances/__init__.py +++ b/glances/__init__.py @@ -74,45 +74,27 @@ def end(): sys.exit(0) -def start(config, args): - """Start Glances.""" - - # Load mode - global mode - - if args.trace_malloc or args.memory_leak: - tracemalloc.start() - - start_duration = Counter() - - if core.is_standalone(): - from glances.standalone import GlancesStandalone as GlancesMode - elif core.is_client(): - if core.is_client_browser(): - from glances.client_browser import GlancesClientBrowser as GlancesMode - else: - from glances.client import GlancesClient as GlancesMode - elif core.is_server(): - from glances.server import GlancesServer as GlancesMode - elif core.is_webserver(): - from glances.webserver import GlancesWebServer as GlancesMode - - # Init the mode - logger.info(f"Start {GlancesMode.__name__} mode") - mode = GlancesMode(config=config, args=args) - - # Start the main loop +def start_main_loop(args, start_duration): logger.debug(f"Glances started in {start_duration.get()} seconds") if args.stop_after: logger.info(f'Glances will be stopped in ~{args.stop_after * args.time} seconds') + +def check_memleak(args, mode): if args.memory_leak: - print(f'Memory leak detection, please wait ~{args.stop_after * args.time * args.memory_leak * 2} seconds...') + wait = args.stop_after * args.time * args.memory_leak * 2 + print(f'Memory leak detection, please wait ~{wait} seconds...') # First run without dump to fill the memory mode.serve_n(args.stop_after) # Then start the memory-leak loop snapshot_begin = tracemalloc.take_snapshot() + else: + snapshot_begin = None + + return snapshot_begin + +def setup_server_mode(args, mode): if args.stdout_issue or args.stdout_apidoc: # Serve once for issue/test mode mode.serve_issue() @@ -120,6 +102,8 @@ def start(config, args): # Serve forever mode.serve_forever() + +def maybe_trace_memleak(args, snapshot_begin): if args.memory_leak: snapshot_end = tracemalloc.take_snapshot() snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename') @@ -136,6 +120,39 @@ def start(config, args): for stat in top_stats[:10]: print(stat) + +def start(config, args): + """Start Glances.""" + + # Load mode + global mode + + if args.trace_malloc or args.memory_leak: + tracemalloc.start() + + start_duration = Counter() + + if core.is_standalone(): + from glances.standalone import GlancesStandalone as GlancesMode + elif core.is_client(): + if core.is_client_browser(): + from glances.client_browser import GlancesClientBrowser as GlancesMode + else: + from glances.client import GlancesClient as GlancesMode + elif core.is_server(): + from glances.server import GlancesServer as GlancesMode + elif core.is_webserver(): + from glances.webserver import GlancesWebServer as GlancesMode + + # Init the mode + logger.info(f"Start {GlancesMode.__name__} mode") + mode = GlancesMode(config=config, args=args) + + start_main_loop(args, start_duration) + snapshot_begin = check_memleak(args, mode) + setup_server_mode(args, mode) + maybe_trace_memleak(args, snapshot_begin) + # Shutdown mode.end()