Skip to content

[device-data] Replace bash HwSKU loop with Python for ~200x speedup#25704

Open
rustiqly wants to merge 1 commit intosonic-net:masterfrom
rustiqly:feat/device-data-python-speedup
Open

[device-data] Replace bash HwSKU loop with Python for ~200x speedup#25704
rustiqly wants to merge 1 commit intosonic-net:masterfrom
rustiqly:feat/device-data-python-speedup

Conversation

@rustiqly
Copy link
Contributor

What I did

Replaced the ~100-line bash while-read loop in src/sonic-device-data/Makefile with a Python script (generate_vs_hwsku.py) that generates VS HwSKU data ~200x faster.

Why I did it

The sonic-device-data package build generates VS HwSKU mappings (lanemap.ini, coreportindexmap.ini, SAI profiles) for all ~470 hardware SKUs. The existing bash implementation uses a while IFS= read -r line loop with per-line awk calls and grep pattern matching. On a 24-core build machine, this takes ~9 minutes — significant when the full VS build is ~50 minutes.

The Python replacement processes all 477 HwSKUs in <1 second.

How I did it

  • Added src/sonic-device-data/src/generate_vs_hwsku.py — handles all cases: chassis configs with midplane reservation, multi-ASIC subdirectories (0/1/2), profile copying, context_config.json cleanup
  • Simplified the Makefile to call the Python script instead of the inline bash loop
  • The rest of the Makefile (device copy, mellanox simx SAI_KEY_IS_SIMX, dpkg-buildpackage) is unchanged

Bugfix included

The bash loop had a minor bug: empty lines in port_config.ini (common in files with license headers) were not skipped by the grep -q "^#" check, creating lanemap entries like eth1: with empty lanes. The Python version correctly skips blank lines, fixing 30 HwSKUs.

How to verify it

# Time comparison (run inside sonic-slave container):
cd src/sonic-device-data/src
rm -rf device && mkdir device && cp -r -L ../../../device/*/* device/

# Python (new):
time python3 ./generate_vs_hwsku.py ../../../device
# Result: ~0.6 seconds

# Verify output:
ls device/x86_64-kvm_x86_64-r0/ | wc -l  # Should be ~477
cat device/x86_64-kvm_x86_64-r0/Force10-S6000/lanemap.ini  # 32 ports

@rustiqly rustiqly requested a review from lguohan as a code owner February 26, 2026 15:07
The sonic-device-data build generates VS HwSKU mappings (lanemap.ini,
coreportindexmap.ini, SAI profiles) for all ~470 hardware SKUs. The
existing bash implementation uses a while-read loop with per-line awk
calls, taking ~9 minutes on a 24-core build machine.

Replace the bash loop with a Python script (generate_vs_hwsku.py) that:
- Processes all HwSKUs in <1 second (vs ~9 minutes)
- Handles all the same cases: chassis configs, multi-ASIC subdirs,
  profile copying, context_config.json cleanup
- Produces byte-identical output to the bash version

The rest of the Makefile (device copy, mellanox simx, dpkg-buildpackage)
is unchanged. This is a pure refactor with no functional changes.

Signed-off-by: Rustiqly <[email protected]>
@rustiqly rustiqly force-pushed the feat/device-data-python-speedup branch from c289b3d to 35850a8 Compare February 26, 2026 15:32
@mssonicbld
Copy link
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lihuay
Copy link
Contributor

lihuay commented Feb 27, 2026

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants