diff --git a/scripts/gen_sai_dbg_dump.sh b/scripts/gen_sai_dbg_dump.sh index 5985eb0b27..2c36be48a3 100644 --- a/scripts/gen_sai_dbg_dump.sh +++ b/scripts/gen_sai_dbg_dump.sh @@ -1,7 +1,5 @@ #!/bin/bash -. /usr/local/bin/gen_sai_dbg_dump_lib.sh - ############################################################################### # Prints the usage information. # Globals: @@ -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. # @@ -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 @@ -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 @@ -142,4 +247,7 @@ main() { exit 0 } -main "$@" +# Only call main if script is executed directly +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi diff --git a/scripts/gen_sai_dbg_dump_lib.sh b/scripts/gen_sai_dbg_dump_lib.sh deleted file mode 100644 index 44f77bf68f..0000000000 --- a/scripts/gen_sai_dbg_dump_lib.sh +++ /dev/null @@ -1,112 +0,0 @@ -#!/bin/bash - -############################################################################### -# 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. -# it ensures that the `syncd` container is running before initiating the dump. -# -# 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:-10}" - 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 "Directory '$SYNCD_DUMP_DIR' does not exist in the syncd container. Creating it..." - if ! docker exec syncd mkdir -p "$SYNCD_DUMP_DIR"; then - echo "Error: Failed to create directory '$SYNCD_DUMP_DIR' 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 - - # Ensure the file exists in the Docker container - if ! docker exec syncd test -f $SYNCD_DUMP_FILE; then - echo "Error: SAI dump file does not exist in the syncd container." - return 1 - fi - - return 0 -} diff --git a/scripts/generate_dump b/scripts/generate_dump index 9e9b78fdd9..7dadcc0de1 100755 --- a/scripts/generate_dump +++ b/scripts/generate_dump @@ -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 @@ -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 diff --git a/setup.py b/setup.py index 3fcb7d9519..c25cf9bd68 100644 --- a/setup.py +++ b/setup.py @@ -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': [