Skip to content

Commit 584fbba

Browse files
authored
Merge pull request #3131 from ariel-anieli/glances-start-refactor
glances: refactor start()
2 parents b7cdf66 + fc4dc38 commit 584fbba

File tree

1 file changed

+46
-29
lines changed

1 file changed

+46
-29
lines changed

glances/__init__.py

+46-29
Original file line numberDiff line numberDiff line change
@@ -74,52 +74,36 @@ def end():
7474
sys.exit(0)
7575

7676

77-
def start(config, args):
78-
"""Start Glances."""
79-
80-
# Load mode
81-
global mode
82-
83-
if args.trace_malloc or args.memory_leak:
84-
tracemalloc.start()
85-
86-
start_duration = Counter()
87-
88-
if core.is_standalone():
89-
from glances.standalone import GlancesStandalone as GlancesMode
90-
elif core.is_client():
91-
if core.is_client_browser():
92-
from glances.client_browser import GlancesClientBrowser as GlancesMode
93-
else:
94-
from glances.client import GlancesClient as GlancesMode
95-
elif core.is_server():
96-
from glances.server import GlancesServer as GlancesMode
97-
elif core.is_webserver():
98-
from glances.webserver import GlancesWebServer as GlancesMode
99-
100-
# Init the mode
101-
logger.info(f"Start {GlancesMode.__name__} mode")
102-
mode = GlancesMode(config=config, args=args)
103-
104-
# Start the main loop
77+
def start_main_loop(args, start_duration):
10578
logger.debug(f"Glances started in {start_duration.get()} seconds")
10679
if args.stop_after:
10780
logger.info(f'Glances will be stopped in ~{args.stop_after * args.time} seconds')
10881

82+
83+
def check_memleak(args, mode):
10984
if args.memory_leak:
110-
print(f'Memory leak detection, please wait ~{args.stop_after * args.time * args.memory_leak * 2} seconds...')
85+
wait = args.stop_after * args.time * args.memory_leak * 2
86+
print(f'Memory leak detection, please wait ~{wait} seconds...')
11187
# First run without dump to fill the memory
11288
mode.serve_n(args.stop_after)
11389
# Then start the memory-leak loop
11490
snapshot_begin = tracemalloc.take_snapshot()
91+
else:
92+
snapshot_begin = None
93+
94+
return snapshot_begin
95+
11596

97+
def setup_server_mode(args, mode):
11698
if args.stdout_issue or args.stdout_apidoc:
11799
# Serve once for issue/test mode
118100
mode.serve_issue()
119101
else:
120102
# Serve forever
121103
mode.serve_forever()
122104

105+
106+
def maybe_trace_memleak(args, snapshot_begin):
123107
if args.memory_leak:
124108
snapshot_end = tracemalloc.take_snapshot()
125109
snapshot_diff = snapshot_end.compare_to(snapshot_begin, 'filename')
@@ -136,6 +120,39 @@ def start(config, args):
136120
for stat in top_stats[:10]:
137121
print(stat)
138122

123+
124+
def start(config, args):
125+
"""Start Glances."""
126+
127+
# Load mode
128+
global mode
129+
130+
if args.trace_malloc or args.memory_leak:
131+
tracemalloc.start()
132+
133+
start_duration = Counter()
134+
135+
if core.is_standalone():
136+
from glances.standalone import GlancesStandalone as GlancesMode
137+
elif core.is_client():
138+
if core.is_client_browser():
139+
from glances.client_browser import GlancesClientBrowser as GlancesMode
140+
else:
141+
from glances.client import GlancesClient as GlancesMode
142+
elif core.is_server():
143+
from glances.server import GlancesServer as GlancesMode
144+
elif core.is_webserver():
145+
from glances.webserver import GlancesWebServer as GlancesMode
146+
147+
# Init the mode
148+
logger.info(f"Start {GlancesMode.__name__} mode")
149+
mode = GlancesMode(config=config, args=args)
150+
151+
start_main_loop(args, start_duration)
152+
snapshot_begin = check_memleak(args, mode)
153+
setup_server_mode(args, mode)
154+
maybe_trace_memleak(args, snapshot_begin)
155+
139156
# Shutdown
140157
mode.end()
141158

0 commit comments

Comments
 (0)