Skip to content

Commit

Permalink
Update gen_sai_dbg_dump scripts and remove gen_sai_dbg_dump_lib.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
aviramd committed Nov 11, 2024
1 parent 3e858d3 commit 9893ebf
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 135 deletions.
148 changes: 128 additions & 20 deletions scripts/gen_sai_dbg_dump.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/bin/bash

. /usr/local/bin/gen_sai_dbg_dump_lib.sh

###############################################################################
# Prints the usage information.
# Globals:
Expand Down Expand Up @@ -58,12 +56,128 @@ copy_from_docker() {
fi
}

###############################################################################
# generate_sai_dump
# Description:
# This function triggers the generation of a SAI debug dump file in the
# `syncd` Docker container through Redis and waits for the file to be ready.
# Arguments:
# $1 - Filename for the SAI debug dump file.
# $2 - Optional timeout for file readiness (default: 10 seconds).
# Returns:
# 0 - On success
# 1 - On failure
###############################################################################
generate_sai_dump() {
local DB=0
local KEY="DBG_GEN_DUMP_TABLE:DUMP"
local STATUS_KEY="DBG_GEN_DUMP_STATUS_TABLE:DUMP"
local FIELD="file"
local STATUS_FIELD="status"
local STATUS="1"

local SYNCD_DUMP_FILE="$1"
local TIMEOUT_FOR_GEN_DBG_DUMP_FILE_READYNESS="${2:-60}"
local INTERVAL=1
local TIME_PASSED=0

if [ -z "$SYNCD_DUMP_FILE" ]; then
echo "Error: No filename provided for the SAI debug dump file."
return 1
fi

# Ensure the syncd container is running
if [[ "$( docker container inspect -f '{{.State.Running}}' syncd )" != "true" ]]; then
echo "Error: syncd container is not running."
return 1
fi

# Extract the directory from the SYNCD_DUMP_FILE path
local SYNCD_DUMP_DIR
SYNCD_DUMP_DIR=$(dirname "$SYNCD_DUMP_FILE")

# Ensure the directory exists in the syncd container; if not, create it
if ! docker exec syncd test -d "$SYNCD_DUMP_DIR"; then
#echo "Creating directory '$SYNCD_DUMP_DIR' inside the syncd container..."
if ! docker exec syncd mkdir -p "$SYNCD_DUMP_DIR"; then
echo "Error: Failed to create directory inside the syncd container."
return 1
fi
fi

# Delete the tables from STATE_DB before triggering the dump file
redis-cli -n $DB DEL $KEY > /dev/null 2>&1
redis-cli -n $DB DEL $STATUS_KEY > /dev/null 2>&1

# Set the DBG_GEN_DUMP in the Redis DB to trigger the dump generation
if ! redis-cli SADD "DBG_GEN_DUMP_TABLE_KEY_SET" "DUMP" > /dev/null 2>&1; then
echo "Error: Failed to publish message to Redis DBG_GEN_DUMP_TABLE_CHANNEL."
return 1
fi

if ! redis-cli -n $DB HSET "_$KEY" $FIELD $SYNCD_DUMP_FILE > /dev/null 2>&1; then
echo "Error: Failed to set Redis key."
return 1
fi

if ! redis-cli PUBLISH "DBG_GEN_DUMP_TABLE_CHANNEL@0" "G" > /dev/null 2>&1; then
echo "Error: Failed to publish message to Redis DBG_GEN_DUMP_TABLE_CHANNEL."
return 1
fi

# Timeout and interval for checking status of file readiness
while [ $TIME_PASSED -lt $TIMEOUT_FOR_GEN_DBG_DUMP_FILE_READYNESS ]; do
# Get the status field value
STATUS=$(redis-cli -n $DB HGET "$STATUS_KEY" "$STATUS_FIELD" 2>/dev/null | grep -o '^[0-9]*$')

# Check if STATUS is non-empty
if [ -n "$STATUS" ]; then
# STATUS field exists; you can use it as needed
break
fi
sleep $INTERVAL
TIME_PASSED=$((TIME_PASSED + INTERVAL))
done

# Delete the tables from STATE_DB after triggering the dump file
redis-cli -n $DB DEL $KEY > /dev/null 2>&1
redis-cli -n $DB DEL $STATUS_KEY > /dev/null 2>&1

if [ -n "$STATUS" ] && [ "$STATUS" -ne 0 ]; then
echo "Error: dump file operation failed, Status $STATUS"
return 1
fi

if [ $TIME_PASSED -ge $TIMEOUT_FOR_GEN_DBG_DUMP_FILE_READYNESS ]; then
echo "Timeout reached. Status was not ready in time."
return 1
fi

# Poll for file existence in the syncd container with a timeout
TIME_PASSED=0
while [ $TIME_PASSED -lt $TIMEOUT_FOR_GEN_DBG_DUMP_FILE_READYNESS ]; do
if docker exec syncd test -f "$SYNCD_DUMP_FILE"; then
#echo "SAI dump file successfully generated in the syncd container."
break
fi
sleep $INTERVAL
TIME_PASSED=$((TIME_PASSED + INTERVAL))
done

if [ $TIME_PASSED -ge $TIMEOUT_FOR_GEN_DBG_DUMP_FILE_READYNESS ]; then
echo "Error: SAI dump file does not exist in the syncd container after waiting ${TIME_PASSED} seconds."
return 1
fi

return 0
}

###############################################################################
# Main script logic
# Description:
# This is the main entry point of the script, which handles the generation
# and retrieval of the SAI debug dump file. It parses command-line arguments,
# ensures necessary directories are available, and triggers the SAI debug dump
# ensures necessary directories are available, and triggers the SAI debug dump
# process through Redis. The script waits for the dump file to be generated and
# then copies it from the Docker container to the specified location on the local system.
#
Expand All @@ -86,21 +200,10 @@ main() {
# Parse arguments
while getopts ":f:t:h" opt; do
case $opt in
f)
sai_dump_filename="$OPTARG"
;;
t)
timeout_for_file_readiness="$OPTARG"
;;
h)
usage
exit 0
;;
/?)
echo "Invalid option: -$OPTARG" >&2
usage
exit 1
;;
f) sai_dump_filename="$OPTARG" ;;
t) timeout_for_file_readiness="$OPTARG" ;;
h) usage; exit 0 ;;
?) echo "Invalid option: -$OPTARG" >&2; usage; exit 1 ;;
esac
done

Expand All @@ -117,11 +220,13 @@ main() {
if [ ! -d "$(dirname "$sai_dump_filename")" ]; then
sudo mkdir -p "$(dirname "$sai_dump_filename")"
fi

# Call generate_sai_dump with or without the timeout argument
if [ -n "$timeout_for_file_readiness" ]; then
#echo generate_sai_dump "$syncd_sai_dump_filename" "$timeout_for_file_readiness"
generate_sai_dump "$syncd_sai_dump_filename" "$timeout_for_file_readiness"
else
#echo generate_sai_dump "$syncd_sai_dump_filename"
generate_sai_dump "$syncd_sai_dump_filename"
fi

Expand All @@ -142,4 +247,7 @@ main() {
exit 0
}

main "$@"
# Only call main if script is executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
112 changes: 0 additions & 112 deletions scripts/gen_sai_dbg_dump_lib.sh

This file was deleted.

3 changes: 1 addition & 2 deletions scripts/generate_dump
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ generate_sai_dbg_dump_file() {
fi

# Generate the SAI dump file using the refactored function
source /usr/local/bin/gen_sai_dbg_dump_lib.sh
source /usr/local/bin/gen_sai_dbg_dump.sh
if ! generate_sai_dump "$syncd_sai_dump_filename"; then
echo "Error: Failed to generate SAI debug dump."
return 1
Expand Down Expand Up @@ -2157,7 +2157,6 @@ main() {
collect_marvell
fi


# 2nd counter snapshot late. Need 2 snapshots to make sense of counters trend.
save_counter_snapshot $asic 2

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@
'scripts/check_db_integrity.py',
'scripts/sysreadyshow',
'scripts/gen_sai_dbg_dump.sh',
'scripts/gen_sai_dbg_dump_lib.sh',
],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit 9893ebf

Please sign in to comment.