Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glances: refactor start() #3131

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions glances/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,52 +74,36 @@ 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()
else:
# 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')
Expand All @@ -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()

Expand Down