Skip to content

Commit d99595a

Browse files
authored
[hostcfgd] Enhance hostcfgd to check feature state and run less system calls (#7987)
Why I did it Currently hostcfgd is implemented in a way each feature which is enabled/disabled triggering execution of systemctl enable/unmask commands which eventually trigger 'systemctl daemon-reload' command. Each call like this cost 0.6s and overall add a overhead of ~12 seconds of CPU time. This change will verify the desired state of a feature and the current state of this feature on systemd and trigger a system call only when must. How I did it Check each feature status on systemd before executing a system call to enable and reload the systemctl daemon. How to verify it Build an image with this change and observe less system calls are executed. Signed-off-by: Shlomi Bitton <shlomibi@nvidia.com>
1 parent 01f51e0 commit d99595a

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

src/sonic-host-services/scripts/hostcfgd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ class FeatureHandler(object):
240240
cmds = []
241241
feature_names, feature_suffixes = self.get_feature_attribute(feature)
242242
for feature_name in feature_names:
243+
# Check if it is already enabled, if yes skip the system call
244+
cmd = "sudo systemctl status {}.{} | grep Loaded".format(feature_name, feature_suffixes[-1])
245+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
246+
(stdout, stderr) = proc.communicate()
247+
if "enabled" in str(stdout):
248+
continue
249+
243250
for suffix in feature_suffixes:
244251
cmds.append("sudo systemctl unmask {}.{}".format(feature_name, suffix))
245252
# If feature has timer associated with it, start/enable corresponding systemd .timer unit
@@ -259,6 +266,13 @@ class FeatureHandler(object):
259266
cmds = []
260267
feature_names, feature_suffixes = self.get_feature_attribute(feature)
261268
for feature_name in feature_names:
269+
# Check if it is already disabled, if yes skip the system call
270+
cmd = "sudo systemctl status {}.{} | grep Loaded".format(feature_name, feature_suffixes[-1])
271+
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
272+
(stdout, stderr) = proc.communicate()
273+
if "disabled" in str(stdout) or "masked" in str(stdout):
274+
continue
275+
262276
for suffix in reversed(feature_suffixes):
263277
cmds.append("sudo systemctl stop {}.{}".format(feature_name, suffix))
264278
cmds.append("sudo systemctl disable {}.{}".format(feature_name, feature_suffixes[-1]))

src/sonic-host-services/tests/hostcfgd/hostcfgd_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ def test_hostcfgd(self, test_name, test_data, fs):
9797
fs.create_dir(hostcfgd.FeatureHandler.SYSTEMD_SYSTEM_DIR)
9898
MockConfigDb.set_config_db(test_data["config_db"])
9999
with mock.patch("hostcfgd.subprocess") as mocked_subprocess:
100+
popen_mock = mock.Mock()
101+
attrs = test_data["popen_attributes"]
102+
popen_mock.configure_mock(**attrs)
103+
mocked_subprocess.Popen.return_value = popen_mock
104+
100105
host_config_daemon = hostcfgd.HostConfigDaemon()
101106
host_config_daemon.feature_handler.update_all_features_config()
102107
assert self.__verify_table(

src/sonic-host-services/tests/hostcfgd/test_vectors.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
call("sudo systemctl enable telemetry.timer", shell=True),
9797
call("sudo systemctl start telemetry.timer", shell=True),
9898
],
99+
"popen_attributes": {
100+
'communicate.return_value': ('output', 'error')
101+
},
99102
},
100103
],
101104
[
@@ -189,6 +192,9 @@
189192
call("sudo systemctl enable telemetry.timer", shell=True),
190193
call("sudo systemctl start telemetry.timer", shell=True),
191194
],
195+
"popen_attributes": {
196+
'communicate.return_value': ('output', 'error')
197+
},
192198
},
193199
],
194200
[
@@ -282,6 +288,96 @@
282288
call("sudo systemctl enable telemetry.timer", shell=True),
283289
call("sudo systemctl start telemetry.timer", shell=True),
284290
],
291+
"popen_attributes": {
292+
'communicate.return_value': ('output', 'error')
293+
},
285294
},
286295
],
296+
[
297+
"DualTorCaseWithNoSystemCalls",
298+
{
299+
"config_db": {
300+
"DEVICE_METADATA": {
301+
"localhost": {
302+
"subtype": "DualToR",
303+
"type": "ToRRouter",
304+
}
305+
},
306+
"KDUMP": {
307+
"config": {
308+
"enabled": "false",
309+
"num_dumps": "3",
310+
"memory": "0M-2G:256M,2G-4G:320M,4G-8G:384M,8G-:448M"
311+
}
312+
},
313+
"FEATURE": {
314+
"dhcp_relay": {
315+
"auto_restart": "enabled",
316+
"has_global_scope": "True",
317+
"has_per_asic_scope": "False",
318+
"has_timer": "False",
319+
"high_mem_alert": "disabled",
320+
"set_owner": "kube",
321+
"state": "enabled"
322+
},
323+
"mux": {
324+
"auto_restart": "enabled",
325+
"has_global_scope": "True",
326+
"has_per_asic_scope": "False",
327+
"has_timer": "False",
328+
"high_mem_alert": "disabled",
329+
"set_owner": "local",
330+
"state": "{% if 'subtype' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['subtype'] == 'DualToR' %}enabled{% else %}always_disabled{% endif %}"
331+
},
332+
"telemetry": {
333+
"auto_restart": "enabled",
334+
"has_global_scope": "True",
335+
"has_per_asic_scope": "False",
336+
"has_timer": "True",
337+
"high_mem_alert": "disabled",
338+
"set_owner": "kube",
339+
"state": "enabled",
340+
"status": "enabled"
341+
},
342+
},
343+
},
344+
"expected_config_db": {
345+
"FEATURE": {
346+
"dhcp_relay": {
347+
"auto_restart": "enabled",
348+
"has_global_scope": "True",
349+
"has_per_asic_scope": "False",
350+
"has_timer": "False",
351+
"high_mem_alert": "disabled",
352+
"set_owner": "kube",
353+
"state": "enabled"
354+
},
355+
"mux": {
356+
"auto_restart": "enabled",
357+
"has_global_scope": "True",
358+
"has_per_asic_scope": "False",
359+
"has_timer": "False",
360+
"high_mem_alert": "disabled",
361+
"set_owner": "local",
362+
"state": "enabled"
363+
},
364+
"telemetry": {
365+
"auto_restart": "enabled",
366+
"has_global_scope": "True",
367+
"has_per_asic_scope": "False",
368+
"has_timer": "True",
369+
"high_mem_alert": "disabled",
370+
"set_owner": "kube",
371+
"state": "enabled",
372+
"status": "enabled"
373+
},
374+
},
375+
},
376+
"expected_subprocess_calls": [
377+
],
378+
"popen_attributes": {
379+
'communicate.return_value': ('enabled', 'error')
380+
},
381+
}
382+
]
287383
]

0 commit comments

Comments
 (0)