Skip to content

Commit 93172c4

Browse files
[202205] [generate_dump] Optimize the execution time of the 'show techsupport' script to 5-10% by reducing calls to the 'tar append' operation (#2562)
- What I did Optimize the execution time of the 'show techsupport' script to 5-10%. - How I did it The show techsupport CLI command calls the generate_dump bash script. In the script, there are a many functions that do the next scenario: 1. Run some CLI command 2. Save output from step 1 to the temporary file 3. Append the temporary file from step 2 to the `/var/dump/sonic_dump_XXXX.tar` file 4. Delete the temporary file from step 2 This PR removes the 3 and 4 step from those functions and creates a new function save_to_tar() which will add to .tar archive the whole directory with temporary files (which means it will not spawn a tar -v -rhf ... process for each temporary file) - How to verify it Run the time show techsupport CLI command and compare the execution time to the original script, the execution time will be decreased by 5-10%. Signed-off-by: Vadym Hlushko <[email protected]>
1 parent f40d82f commit 93172c4

File tree

1 file changed

+46
-65
lines changed

1 file changed

+46
-65
lines changed

scripts/generate_dump

+46-65
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ save_bcmcmd() {
106106
local filename=$2
107107
local filepath="${LOGDIR}/$filename"
108108
local do_gzip=${3:-false}
109-
local tarpath="${BASE}/dump/$filename"
110109
local timeout_cmd="timeout --foreground ${TIMEOUT_MIN}m"
111110
local cmd=$(escape_quotes "$cmd")
112111
if [ ! -d $LOGDIR ]; then
@@ -141,12 +140,9 @@ save_bcmcmd() {
141140
fi
142141
if $do_gzip; then
143142
gzip ${filepath} 2>/dev/null
144-
tarpath="${tarpath}.gz"
145143
filepath="${filepath}.gz"
146144
fi
147-
($TAR $V -rhf $TARFILE -C $DUMPDIR "$tarpath" \
148-
|| abort "${EXT_TAR_FAILED}" "tar append operation failed. Aborting to prevent data loss.") \
149-
&& $RM $V -rf "$filepath"
145+
150146
end_t=$(date +%s%3N)
151147
echo "[ save_bcmcmd:$cmd ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
152148
}
@@ -180,7 +176,7 @@ save_bcmcmd_all_ns() {
180176
}
181177

182178
###############################################################################
183-
# Runs a comamnd and saves its output to the incrementally built tar.
179+
# Runs a comamnd and saves its output to the file.
184180
# Command gets timedout if it runs for more than TIMEOUT_MIN minutes.
185181
# Globals:
186182
# LOGDIR
@@ -208,7 +204,6 @@ save_cmd() {
208204
local filename=$2
209205
local filepath="${LOGDIR}/$filename"
210206
local do_gzip=${3:-false}
211-
local tarpath="${BASE}/dump/$filename"
212207
local timeout_cmd="timeout --foreground ${TIMEOUT_MIN}m"
213208
local cleanup_method=${4:-dummy_cleanup_method}
214209
local redirect='&>'
@@ -230,7 +225,6 @@ save_cmd() {
230225
# as one argument, e.g. vtysh -c "COMMAND HERE" needs to have
231226
# "COMMAND HERE" bunched together as 1 arg to vtysh -c
232227
if $do_gzip; then
233-
tarpath="${tarpath}.gz"
234228
filepath="${filepath}.gz"
235229
# cleanup_method will run in a sub-shell, need declare it first
236230
local cmds="$cleanup_method_declration; $cmd $redirect_eval | $cleanup_method | gzip -c > '${filepath}'"
@@ -260,13 +254,35 @@ save_cmd() {
260254
fi
261255
fi
262256

263-
($TAR $V -rhf $TARFILE -C $DUMPDIR "$tarpath" \
264-
|| abort "${EXT_TAR_FAILED}" "tar append operation failed. Aborting to prevent data loss.") \
265-
&& $RM $V -rf "$filepath"
266257
end_t=$(date +%s%3N)
267258
echo "[ save_cmd:$cmd ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
268259
}
269260

261+
###############################################################################
262+
# Save all collected data to tar archive.
263+
# Globals:
264+
# DUMPDIR
265+
# TAR
266+
# TARFILE
267+
# V
268+
# BASE
269+
# Arguments:
270+
# None
271+
# Returns:
272+
# None
273+
###############################################################################
274+
save_to_tar() {
275+
trap 'handle_error $? $LINENO' ERR
276+
local start_t=$(date +%s%3N)
277+
local end_t=0
278+
279+
cd $DUMPDIR
280+
$TAR $V -rhf $TARFILE "$BASE"
281+
282+
end_t=$(date +%s%3N)
283+
echo "[ save_to_tar ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
284+
}
285+
270286
###############################################################################
271287
# Dummy cleanup method.
272288
# Globals:
@@ -407,7 +423,7 @@ get_vtysh_namespace() {
407423
###############################################################################
408424
# Runs a vtysh command in all namesapces for a multi ASIC platform, and in
409425
# default (host) namespace in single ASIC platforms. Saves its output to the
410-
# incrementally built tar.
426+
# file.
411427
# Globals:
412428
# None
413429
# Arguments:
@@ -437,7 +453,7 @@ save_vtysh() {
437453
}
438454

439455
###############################################################################
440-
# Runs an ip command and saves its output to the incrementally built tar.
456+
# Runs an ip command and saves its output to the file.
441457
# Globals:
442458
# None
443459
# Arguments:
@@ -456,7 +472,7 @@ save_ip() {
456472
}
457473

458474
###############################################################################
459-
# Runs a bridge command and saves its output to the incrementally built tar.
475+
# Runs a bridge command and saves its output to the file.
460476
# Globals:
461477
# None
462478
# Arguments:
@@ -769,8 +785,8 @@ save_proc() {
769785
( [ -e $f ] && $CP $V -r $f $TARDIR/proc ) || echo "$f not found" > $TARDIR/$f
770786
fi
771787
done
772-
$TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+rw $BASE/proc
773-
$RM $V -rf $TARDIR/proc
788+
789+
chmod ugo+rw -R $DUMPDIR/$BASE/proc
774790
}
775791

776792
###############################################################################
@@ -837,7 +853,7 @@ save_platform_info() {
837853
}
838854

839855
###############################################################################
840-
# Runs a comamnd and saves its output to the incrementally built tar.
856+
# Runs a comamnd and saves its output to the file.
841857
# Globals:
842858
# LOGDIR
843859
# BASE
@@ -862,16 +878,13 @@ save_file() {
862878
local orig_path=$1
863879
local supp_dir=$2
864880
local gz_path="$TARDIR/$supp_dir/$(basename $orig_path)"
865-
local tar_path="${BASE}/$supp_dir/$(basename $orig_path)"
866881
local do_gzip=${3:-true}
867-
local do_tar_append=${4:-true}
868882
if [ ! -d "$TARDIR/$supp_dir" ]; then
869883
$MKDIR $V -p "$TARDIR/$supp_dir"
870884
fi
871885

872886
if $do_gzip; then
873887
gz_path="${gz_path}.gz"
874-
tar_path="${tar_path}.gz"
875888
if $NOOP; then
876889
echo "gzip -c $orig_path > $gz_path"
877890
else
@@ -885,11 +898,6 @@ save_file() {
885898
fi
886899
fi
887900

888-
if $do_tar_append; then
889-
($TAR $V -rhf $TARFILE -C $DUMPDIR "$tar_path" \
890-
|| abort "${EXT_PROCFS_SAVE_FAILED}" "tar append operation failed. Aborting to prevent data loss.") \
891-
&& $RM $V -f "$gz_path"
892-
fi
893901
end_t=$(date +%s%3N)
894902
echo "[ save_file:$orig_path] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
895903
}
@@ -1269,16 +1277,12 @@ save_log_files() {
12691277
# don't gzip already-gzipped log files :)
12701278
# do not append the individual files to the main tarball
12711279
if [ -z "${file##*.gz}" ]; then
1272-
save_file $file log false false
1280+
save_file $file log false
12731281
else
1274-
save_file $file log true false
1282+
save_file $file log true
12751283
fi
12761284
done
12771285

1278-
# Append the log folder to the main tarball
1279-
($TAR $V -rhf $TARFILE -C $DUMPDIR ${BASE}/log \
1280-
|| abort "${EXT_TAR_FAILED}" "tar append operation failed. Aborting for safety") \
1281-
&& $RM $V -rf $TARDIR/log
12821286
end_t=$(date +%s%3N)
12831287
echo "[ TAR /var/log Files ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
12841288

@@ -1303,11 +1307,7 @@ save_warmboot_files() {
13031307
else
13041308
mkdir -p $TARDIR
13051309
$CP $V -rf /host/warmboot $TARDIR
1306-
1307-
($TAR $V --warning=no-file-removed -rhf $TARFILE -C $DUMPDIR --mode=+rw \
1308-
$BASE/warmboot \
1309-
|| abort "${EXT_TAR_FAILED}" "Tar append operation failed. Aborting for safety.") \
1310-
&& $RM $V -rf $TARDIR
1310+
chmod ugo+rw -R $DUMPDIR/$BASE/warmboot
13111311
fi
13121312
end_t=$(date +%s%3N)
13131313
echo "[ Warm-boot Files ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
@@ -1469,8 +1469,7 @@ main() {
14691469
/proc/pagetypeinfo /proc/partitions /proc/sched_debug /proc/slabinfo \
14701470
/proc/softirqs /proc/stat /proc/swaps /proc/sysvipc /proc/timer_list \
14711471
/proc/uptime /proc/version /proc/vmallocinfo /proc/vmstat \
1472-
/proc/zoneinfo \
1473-
|| abort "${EXT_PROCFS_SAVE_FAILED}" "Proc saving operation failed. Aborting for safety."
1472+
/proc/zoneinfo
14741473
end_t=$(date +%s%3N)
14751474
echo "[ Capture Proc State ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
14761475

@@ -1574,9 +1573,6 @@ main() {
15741573
# 2nd counter snapshot late. Need 2 snapshots to make sense of counters trend.
15751574
save_counter_snapshot $asic 2
15761575

1577-
$RM $V -rf $TARDIR
1578-
$MKDIR $V -p $TARDIR
1579-
$MKDIR $V -p $LOGDIR
15801576
# Copying the /etc files to a directory and then tar it
15811577
$CP -r /etc $TARDIR/etc
15821578
rm_list=$(find -L $TARDIR/etc -maxdepth 5 -type l)
@@ -1588,30 +1584,13 @@ main() {
15881584
# Remove secret from /etc files before tar
15891585
remove_secret_from_etc_files $TARDIR
15901586

1591-
start_t=$(date +%s%3N)
1592-
($TAR $V --warning=no-file-removed -rhf $TARFILE -C $DUMPDIR --mode=+rw \
1593-
--exclude="etc/alternatives" \
1594-
--exclude="*/etc/passwd*" \
1595-
--exclude="*/etc/shadow*" \
1596-
--exclude="*/etc/group*" \
1597-
--exclude="*/etc/gshadow*" \
1598-
--exclude="*/etc/ssh*" \
1599-
--exclude="*get_creds*" \
1600-
--exclude="*snmpd.conf*" \
1601-
--exclude="*/etc/mlnx" \
1602-
--exclude="*/etc/mft" \
1603-
--exclude="*/etc/sonic/*.cer" \
1604-
--exclude="*/etc/sonic/*.crt" \
1605-
--exclude="*/etc/sonic/*.pem" \
1606-
--exclude="*/etc/sonic/*.key" \
1607-
--exclude="*/etc/ssl/*.pem" \
1608-
--exclude="*/etc/ssl/certs/*" \
1609-
--exclude="*/etc/ssl/private/*" \
1610-
$BASE/etc \
1611-
|| abort "${EXT_TAR_FAILED}" "Tar append operation failed. Aborting for safety.") \
1612-
&& $RM $V -rf $TARDIR
1613-
end_t=$(date +%s%3N)
1614-
echo "[ TAR /etc Files ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
1587+
# Remove unecessary files
1588+
$RM $V -rf $TARDIR/etc/alternatives $TARDIR/etc/passwd* \
1589+
$TARDIR/etc/shadow* $TARDIR/etc/group* $TARDIR/etc/gshadow* \
1590+
$TARDIR/etc/ssh* $TARDIR/get_creds* $TARDIR/snmpd.conf* \
1591+
$TARDIR/etc/mlnx $TARDIR/etc/mft $TARDIR/etc/sonic/*.cer \
1592+
$TARDIR/etc/sonic/*.crt $TARDIR/etc/sonic/*.pem $TARDIR/etc/sonic/*.key \
1593+
$TARDIR/etc/ssl/*.pem $TARDIR/etc/ssl/certs/ $TARDIR/etc/ssl/private/*
16151594

16161595
save_log_files
16171596
save_crash_files
@@ -1631,6 +1610,8 @@ finalize() {
16311610
# Save techsupport timing profile info
16321611
save_file $TECHSUPPORT_TIME_INFO log false
16331612

1613+
save_to_tar
1614+
16341615
if $DO_COMPRESS; then
16351616
RC=0
16361617
$GZIP $V $TARFILE || RC=$?

0 commit comments

Comments
 (0)