Skip to content

Commit ccefd45

Browse files
vadymhlushko-mlnxisabelmsft
authored andcommitted
Optimize the execution time of the 'show techsupport' script to 5-10%, (sonic-net#2504)
- 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 4f825d9 commit ccefd45

File tree

1 file changed

+48
-70
lines changed

1 file changed

+48
-70
lines changed

scripts/generate_dump

+48-70
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:
@@ -770,8 +786,8 @@ save_proc() {
770786
( [ -e $f ] && $CP $V -r $f $TARDIR/proc ) || echo "$f not found" > $TARDIR/$f
771787
fi
772788
done
773-
$TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+rw $BASE/proc
774-
$RM $V -rf $TARDIR/proc
789+
790+
chmod ugo+rw -R $DUMPDIR/$BASE/proc
775791
}
776792

777793
###############################################################################
@@ -822,9 +838,7 @@ save_proc_stats() {
822838
( $CP $V -r $stats_file $TARDIR/proc_stats ) || echo "$stats_file error" > $TARDIR/$stats_file
823839
fi
824840

825-
$TAR $V -rhf $TARFILE -C $DUMPDIR --mode=+rw $BASE/proc_stats
826-
$RM $V -rf $TARDIR/proc_stats
827-
$RM -rf $stats_file
841+
chmod ugo+rw -R $DUMPDIR/$BASE/proc_stats
828842
}
829843

830844
###############################################################################
@@ -916,16 +930,13 @@ save_file() {
916930
local orig_path=$1
917931
local supp_dir=$2
918932
local gz_path="$TARDIR/$supp_dir/$(basename $orig_path)"
919-
local tar_path="${BASE}/$supp_dir/$(basename $orig_path)"
920933
local do_gzip=${3:-true}
921-
local do_tar_append=${4:-true}
922934
if [ ! -d "$TARDIR/$supp_dir" ]; then
923935
$MKDIR $V -p "$TARDIR/$supp_dir"
924936
fi
925937

926938
if $do_gzip; then
927939
gz_path="${gz_path}.gz"
928-
tar_path="${tar_path}.gz"
929940
if $NOOP; then
930941
echo "gzip -c $orig_path > $gz_path"
931942
else
@@ -939,11 +950,6 @@ save_file() {
939950
fi
940951
fi
941952

942-
if $do_tar_append; then
943-
($TAR $V -rhf $TARFILE -C $DUMPDIR "$tar_path" \
944-
|| abort "${EXT_PROCFS_SAVE_FAILED}" "tar append operation failed. Aborting to prevent data loss.") \
945-
&& $RM $V -f "$gz_path"
946-
fi
947953
end_t=$(date +%s%3N)
948954
echo "[ save_file:$orig_path] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
949955
}
@@ -1290,7 +1296,7 @@ collect_barefoot() {
12901296
done
12911297

12921298
for file in $(find /tmp/bf_logs -type f); do
1293-
save_file "${file}" log true true
1299+
save_file "${file}" log true
12941300
done
12951301
}
12961302

@@ -1346,16 +1352,12 @@ save_log_files() {
13461352
# don't gzip already-gzipped log files :)
13471353
# do not append the individual files to the main tarball
13481354
if [ -z "${file##*.gz}" ]; then
1349-
save_file $file log false false
1355+
save_file $file log false
13501356
else
1351-
save_file $file log true false
1357+
save_file $file log true
13521358
fi
13531359
done
13541360

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

@@ -1380,11 +1382,7 @@ save_warmboot_files() {
13801382
else
13811383
mkdir -p $TARDIR
13821384
$CP $V -rf /host/warmboot $TARDIR
1383-
1384-
($TAR $V --warning=no-file-removed -rhf $TARFILE -C $DUMPDIR --mode=+rw \
1385-
$BASE/warmboot \
1386-
|| abort "${EXT_TAR_FAILED}" "Tar append operation failed. Aborting for safety.") \
1387-
&& $RM $V -rf $TARDIR
1385+
chmod ugo+rw -R $DUMPDIR/$BASE/warmboot
13881386
fi
13891387
end_t=$(date +%s%3N)
13901388
echo "[ Warm-boot Files ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
@@ -1546,8 +1544,7 @@ main() {
15461544
/proc/pagetypeinfo /proc/partitions /proc/sched_debug /proc/slabinfo \
15471545
/proc/softirqs /proc/stat /proc/swaps /proc/sysvipc /proc/timer_list \
15481546
/proc/uptime /proc/version /proc/vmallocinfo /proc/vmstat \
1549-
/proc/zoneinfo \
1550-
|| abort "${EXT_PROCFS_SAVE_FAILED}" "Proc saving operation failed. Aborting for safety."
1547+
/proc/zoneinfo
15511548
save_proc_stats
15521549
end_t=$(date +%s%3N)
15531550
echo "[ Capture Proc State ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
@@ -1567,7 +1564,6 @@ main() {
15671564
save_cmd "systemd-analyze plot" "systemd.analyze.plot.svg"
15681565

15691566
save_platform_info
1570-
15711567
save_cmd "show vlan brief" "vlan.summary"
15721568
save_cmd "show version" "version"
15731569
save_cmd "show platform summary" "platform.summary"
@@ -1582,8 +1578,8 @@ main() {
15821578

15831579
save_ip_info
15841580
save_bridge_info
1585-
15861581
save_frr_info
1582+
15871583
save_bgp_info
15881584
save_evpn_info
15891585

@@ -1663,9 +1659,6 @@ main() {
16631659
# 2nd counter snapshot late. Need 2 snapshots to make sense of counters trend.
16641660
save_counter_snapshot $asic 2
16651661

1666-
$RM $V -rf $TARDIR
1667-
$MKDIR $V -p $TARDIR
1668-
$MKDIR $V -p $LOGDIR
16691662
# Copying the /etc files to a directory and then tar it
16701663
$CP -r /etc $TARDIR/etc
16711664
rm_list=$(find -L $TARDIR/etc -maxdepth 5 -type l)
@@ -1677,30 +1670,13 @@ main() {
16771670
# Remove secret from /etc files before tar
16781671
remove_secret_from_etc_files $TARDIR
16791672

1680-
start_t=$(date +%s%3N)
1681-
($TAR $V --warning=no-file-removed -rhf $TARFILE -C $DUMPDIR --mode=+rw \
1682-
--exclude="etc/alternatives" \
1683-
--exclude="*/etc/passwd*" \
1684-
--exclude="*/etc/shadow*" \
1685-
--exclude="*/etc/group*" \
1686-
--exclude="*/etc/gshadow*" \
1687-
--exclude="*/etc/ssh*" \
1688-
--exclude="*get_creds*" \
1689-
--exclude="*snmpd.conf*" \
1690-
--exclude="*/etc/mlnx" \
1691-
--exclude="*/etc/mft" \
1692-
--exclude="*/etc/sonic/*.cer" \
1693-
--exclude="*/etc/sonic/*.crt" \
1694-
--exclude="*/etc/sonic/*.pem" \
1695-
--exclude="*/etc/sonic/*.key" \
1696-
--exclude="*/etc/ssl/*.pem" \
1697-
--exclude="*/etc/ssl/certs/*" \
1698-
--exclude="*/etc/ssl/private/*" \
1699-
$BASE/etc \
1700-
|| abort "${EXT_TAR_FAILED}" "Tar append operation failed. Aborting for safety.") \
1701-
&& $RM $V -rf $TARDIR
1702-
end_t=$(date +%s%3N)
1703-
echo "[ TAR /etc Files ] : $(($end_t-$start_t)) msec" >> $TECHSUPPORT_TIME_INFO
1673+
# Remove unecessary files
1674+
$RM $V -rf $TARDIR/etc/alternatives $TARDIR/etc/passwd* \
1675+
$TARDIR/etc/shadow* $TARDIR/etc/group* $TARDIR/etc/gshadow* \
1676+
$TARDIR/etc/ssh* $TARDIR/get_creds* $TARDIR/snmpd.conf* \
1677+
$TARDIR/etc/mlnx $TARDIR/etc/mft $TARDIR/etc/sonic/*.cer \
1678+
$TARDIR/etc/sonic/*.crt $TARDIR/etc/sonic/*.pem $TARDIR/etc/sonic/*.key \
1679+
$TARDIR/etc/ssl/*.pem $TARDIR/etc/ssl/certs/ $TARDIR/etc/ssl/private/*
17041680

17051681
save_log_files
17061682
save_crash_files
@@ -1720,6 +1696,8 @@ finalize() {
17201696
# Save techsupport timing profile info
17211697
save_file $TECHSUPPORT_TIME_INFO log false
17221698

1699+
save_to_tar
1700+
17231701
if $DO_COMPRESS; then
17241702
RC=0
17251703
$GZIP $V $TARFILE || RC=$?

0 commit comments

Comments
 (0)