From 81f1bf4a7fed131d872938cf04c7bed3522b4986 Mon Sep 17 00:00:00 2001 From: Vivek Date: Thu, 3 Oct 2024 17:25:39 -0700 Subject: [PATCH] Replace straightforward sonic-cfggen calls with sonic-db-cli (#20232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Why I did it Fix https://github.com/sonic-net/sonic-buildimage/issues/20048 It is explained in ticket above, how sonic-cfggen calls in the hostname.sh, pcie-check.sh and banner-config and hogging the CPU and slightly delaying the start of swss **pcie-check.sh** redis-cli call is also replaced with sonic-db-cli since redis-cli is a wrapper around the actual redis-cli under database container image **swss start** If the SKU has create_only_config_db_buffers.json set to true, the only config that's updated today is. ``` { "DEVICE_METADATA": { "localhost": { "create_only_config_db_buffers": "true" } } } ``` We use sonic-cfggen which cause 1.5 sec delay in the start of swss container. Thus replace with sonic-db-cli. If a complex use case arises in future, this can be updated. Screenshot 2024-09-16 at 12 45 05 PM ### How I did it #### How to verify it **Note: Everything was tested on MSN2700 device, Intel Celeron CPU with 2 cores** - 1.5 sec saved in the swss container start image - 1.6 sec saved in the start of swss service. Previously, it took almost 1.8 sec after config-setup is finished for the swss to start. After replacing the calls, it start almost 0.2 sec after config-setup is finished In total, anywhere between 2.5 - 3.5 sec is saved --- files/build_templates/docker_image_ctl.j2 | 2 +- files/image_config/bannerconfig/banner-config.sh | 8 ++++---- files/image_config/hostname/hostname-config.sh | 4 ++-- files/image_config/pcie-check/pcie-check.sh | 6 +++--- files/image_config/rsyslog/rsyslog-config.sh | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index 87f801c1549c..732681818824 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -433,7 +433,7 @@ start() { if [ -d "$HWSKU_FOLDER" ]; then CREATE_ONLY_CONFIG_DB_BUFFERS_JSON="$HWSKU_FOLDER/create_only_config_db_buffers.json" if [ -f "$CREATE_ONLY_CONFIG_DB_BUFFERS_JSON" ]; then - $SONIC_CFGGEN -j $CREATE_ONLY_CONFIG_DB_BUFFERS_JSON --write-to-db + $SONIC_DB_CLI CONFIG_DB HSET 'DEVICE_METADATA|localhost' create_only_config_db_buffers true fi fi {%- endif %} diff --git a/files/image_config/bannerconfig/banner-config.sh b/files/image_config/bannerconfig/banner-config.sh index b81b342bf52f..5c605a034ea0 100755 --- a/files/image_config/bannerconfig/banner-config.sh +++ b/files/image_config/bannerconfig/banner-config.sh @@ -1,14 +1,14 @@ #!/bin/bash -e -STATE=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["state"]') +STATE=$(sonic-db-cli CONFIG_DB HGET 'BANNER_MESSAGE|global' state) LOGIN= MOTD= LOGOUT= if [[ $STATE == "enabled" ]]; then - LOGIN=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["login"]') - MOTD=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["motd"]') - LOGOUT=$(sonic-cfggen -d -v 'BANNER_MESSAGE["global"]["logout"]') + LOGIN=$(sonic-db-cli CONFIG_DB HGET 'BANNER_MESSAGE|global' login) + MOTD=$(sonic-db-cli CONFIG_DB HGET 'BANNER_MESSAGE|global' motd) + LOGOUT=$(sonic-db-cli CONFIG_DB HGET 'BANNER_MESSAGE|global' logout) echo -e "$LOGIN" > /etc/issue.net echo -e "$LOGIN" > /etc/issue diff --git a/files/image_config/hostname/hostname-config.sh b/files/image_config/hostname/hostname-config.sh index 5f4bfede2097..91b0b7b32f91 100755 --- a/files/image_config/hostname/hostname-config.sh +++ b/files/image_config/hostname/hostname-config.sh @@ -1,7 +1,7 @@ #!/bin/bash -e -CURRENT_HOSTNAME=`hostname` -HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']` +CURRENT_HOSTNAME=$(hostname) +HOSTNAME=$(sonic-db-cli CONFIG_DB HGET 'DEVICE_METADATA|localhost' hostname) if [ -z "$HOSTNAME" ] ; then echo "Missing hostname in the config file, setting to default 'sonic'" diff --git a/files/image_config/pcie-check/pcie-check.sh b/files/image_config/pcie-check/pcie-check.sh index 3d4184c8684c..d8558da82701 100755 --- a/files/image_config/pcie-check/pcie-check.sh +++ b/files/image_config/pcie-check/pcie-check.sh @@ -18,7 +18,7 @@ function debug() function check_and_rescan_pcie_devices() { PCIE_CHK_CMD='sudo pcieutil check | grep "$RESULTS"' - PLATFORM=$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform) + PLATFORM=$(sonic-db-cli CONFIG_DB HGET 'DEVICE_METADATA|localhost' platform) if [ ! -f /usr/share/sonic/device/$PLATFORM/pcie*.yaml ]; then debug "pcie.yaml does not exist! Can't check PCIe status!" @@ -38,7 +38,7 @@ function check_and_rescan_pcie_devices() fi if [ "$(eval $PCIE_CHK_CMD)" = "$EXPECTED" ]; then - redis-cli -n 6 HSET $PCIE_STATUS_TABLE "status" "PASSED" + sonic-db-cli STATE_DB HSET $PCIE_STATUS_TABLE "status" "PASSED" debug "PCIe check passed" exit else @@ -54,7 +54,7 @@ function check_and_rescan_pcie_devices() done debug "PCIe check failed" - redis-cli -n 6 HSET $PCIE_STATUS_TABLE "status" "FAILED" + sonic-db-cli STATE_DB HSET $PCIE_STATUS_TABLE "status" "FAILED" } check_and_rescan_pcie_devices diff --git a/files/image_config/rsyslog/rsyslog-config.sh b/files/image_config/rsyslog/rsyslog-config.sh index 5f28708216ed..9c2da9780112 100755 --- a/files/image_config/rsyslog/rsyslog-config.sh +++ b/files/image_config/rsyslog/rsyslog-config.sh @@ -1,6 +1,6 @@ #!/bin/bash -PLATFORM=`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform` +PLATFORM=$(sonic-db-cli CONFIG_DB HGET 'DEVICE_METADATA|localhost' platform) # Parse the device specific asic conf file, if it exists ASIC_CONF=/usr/share/sonic/device/$PLATFORM/asic.conf