Skip to content

Commit 8dbec05

Browse files
authored
Code optimizations to reduce disk writes on SONiC switches (#20640)
Why I did it This PR contains various optimizations to reduce disk writes on SONiC switches as mentioned in the High-level Design. This contributes to the overall reduction in data written to the disk over time, which can help extend the life of the disk and reduce the risk of disk failure. Work item tracking Microsoft ADO (number only): 28300356 How I did it Moved supervisord logs within swss to tmpfs Optimized away repeated disk writes by vtysh in the bgp container Moved monit and logrotate writers to tmpfs How to verify it Flash image with these optimizations onto a sonic switch and check the following: Within swss container, supervisord logs are written to /dev/shm/supervisor/ Within bgp container, verify that ~/.history_frr file is not written into repeatedly with show bgp summary json Verify that monit state file is in /dev/shm/monit Verify that logrotate status file is in /dev/shm/logrotate
1 parent f7014a3 commit 8dbec05

File tree

9 files changed

+21
-8
lines changed

9 files changed

+21
-8
lines changed

dockers/docker-orchagent/docker-init.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mkdir -p /etc/swss/config.d/
44
mkdir -p /etc/supervisor/
55
mkdir -p /etc/supervisor/conf.d/
6-
6+
mkdir -p /dev/shm/supervisor/
77

88
CFGGEN_PARAMS=" \
99
-d \

files/build_templates/docker_image_ctl.j2

+1
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ start() {
633633
{%- endif %}
634634
-e RUNTIME_OWNER=local \
635635
--uts=host \{# W/A: this should be set per-docker, for those dockers which really need host's UTS namespace #}
636+
--tmpfs /var/log/supervisor:rw \
636637
{%- if install_debug_image == "y" %}
637638
-v /src:/src:ro -v /debug:/debug:rw \
638639
{%- endif %}

files/build_templates/sonic_debian_extension.j2

+5
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/sonic-nettools_*.deb || \
104104
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f
105105
sudo setcap 'cap_net_raw=+ep' $FILESYSTEM_ROOT/usr/bin/wol
106106

107+
# This is needed for moving monit logs, state and logrotate status to tmpfs
108+
sudo bash -c "echo \"d /dev/shm/monit/ 0755 root root\" > $FILESYSTEM_ROOT/etc/tmpfiles.d/tmpfs-monit.conf"
109+
sudo bash -c "echo \"d /dev/shm/logrotate/ 0755 root root\" > $FILESYSTEM_ROOT/etc/tmpfiles.d/tmpfs-logrotate.conf"
110+
111+
107112
# Install a patched version of ifupdown2 (and its dependencies via 'apt-get -y install -f')
108113
sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/ifupdown2_*.deb || \
109114
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
[Unit]
2-
Requires=logrotate-config.service
2+
Requires=logrotate-config.service
3+
4+
[Service]
5+
ExecStart=
6+
ExecStart=/usr/sbin/logrotate --state /dev/shm/logrotate/status /etc/logrotate.conf

files/image_config/monit/monitrc

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
## default the file is placed in $HOME/.monit.id.
3939
#
4040
# set idfile /var/.monit.id
41-
set idfile /var/lib/monit/id
41+
set idfile /dev/shm/monit/id
4242
#
4343
## Set the location of the Monit state file which saves monitoring states
4444
## on each cycle. By default the file is placed in $HOME/.monit.state. If
4545
## the state file is stored on a persistent filesystem, Monit will recover
4646
## the monitoring state across reboots. If it is on temporary filesystem, the
4747
## state will be lost on reboot which may be convenient in some situations.
4848
#
49-
set statefile /var/lib/monit/state
49+
set statefile /dev/shm/monit/state
5050
#
5151
#
5252

@@ -91,7 +91,7 @@
9191
## available in the back end filesystem).
9292
#
9393
set eventqueue
94-
basedir /var/lib/monit/events # set the base directory where events will be stored
94+
basedir /dev/shm/monit/events # set the base directory where events will be stored
9595
slots 100 # optionally limit the queue size
9696
#
9797
#

files/initramfs-tools/union-mount.j2

+3
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ mkdir -p ${rootmnt}/boot
212212
mkdir -p ${rootmnt}/host/$image_dir/boot
213213
mount --bind ${rootmnt}/host/$image_dir/boot ${rootmnt}/boot
214214

215+
## Mount the /tmp directory as tmpfs
216+
mount -t tmpfs -o rw,nosuid,nodev,size=25% tmpfs ${rootmnt}/tmp
217+
215218
## Mount loop device or tmpfs for /var/log
216219
if $logs_inram; then
217220
# NOTE: some platforms, when reaching initramfs stage, have a small

src/sonic-bgpcfgd/bgpcfgd/managers_bgp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ def load_peers():
387387
Load peers from FRR.
388388
:return: set of peers, which are already installed in FRR
389389
"""
390-
command = ["vtysh", "-c", "show bgp vrfs json"]
390+
command = ["vtysh", "-H", "/dev/null", "-c", "show bgp vrfs json"]
391391
ret_code, out, err = run_command(command)
392392
if ret_code == 0:
393393
js_vrf = json.loads(out)

src/sonic-bgpcfgd/bgpmon/bgpmon.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def update_new_peer_states(self, peer_dict):
7777

7878
# Get a new snapshot of BGP neighbors and store them in the "new" location
7979
def get_all_neigh_states(self):
80-
cmd = ["vtysh", "-c", 'show bgp summary json']
80+
cmd = ["vtysh", "-H", "/dev/null", "-c", 'show bgp summary json']
8181
retry_attempt = 0
8282

8383
while retry_attempt < self.MAX_RETRY_ATTEMPTS:

src/sonic-bgpcfgd/tests/test_bgp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def constructor(constants_path, bgp_router_id="", peer_type="general", with_lo0_
3232
}
3333

3434
return_value_map = {
35-
"['vtysh', '-c', 'show bgp vrfs json']": (0, "{\"vrfs\": {\"default\": {}}}", ""),
35+
"['vtysh', '-H', '/dev/null', '-c', 'show bgp vrfs json']": (0, "{\"vrfs\": {\"default\": {}}}", ""),
3636
"['vtysh', '-c', 'show bgp vrf default neighbors json']": (0, "{\"10.10.10.1\": {}, \"20.20.20.1\": {}, \"fc00:10::1\": {}}", "")
3737
}
3838

0 commit comments

Comments
 (0)