Skip to content

Instantly share code, notes, and snippets.

@AlexTalker
Last active March 11, 2021 12:10
Show Gist options
  • Save AlexTalker/209a0e819a1f17fd97704488e0457e93 to your computer and use it in GitHub Desktop.
Save AlexTalker/209a0e819a1f17fd97704488e0457e93 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
_calc() { awk "BEGIN{print $*}"; }
_call_lspci() {
local pcie="$1"
lspci -s "$pcie" -vvv
}
get_cap_width() {
_call_lspci "$1" | grep -oP "LnkCap.*Width x\K\d+"
}
get_sta_width() {
_call_lspci "$1" | grep -oP "LnkSta.*Width x\K\d+"
}
get_cap_speed() {
_call_lspci "$1" | grep -oP "LnkCap.*Speed \K\d+"
}
get_sta_speed() {
_call_lspci "$1" | grep -oP "LnkSta.*Speed \K\d+"
}
get_cap_max_payload() {
_call_lspci "$1" | grep -A 2 DevCap: | grep -oP "MaxPayload \K\d+"
}
get_ctl_max_payload() {
_call_lspci "$1" | grep -A 2 DevCtl: | grep -oP "MaxPayload \K\d+"
}
get_ctl_max_read_request() {
_call_lspci "$1" | grep -A 2 DevCtl: | grep -oP "MaxReadReq \K\d+"
}
get_pcie_gen() {
_call_lspci "$1" | grep -oP "PCIeGen\K\d+"
}
get_encoding() {
local pcie_gen="$1"
if [[ $pcie_gen == 1 ]]; then
echo "1/5";
elif [[ $pcie_gen == 2 ]]; then
echo "1/5";
elif [[ $pcie_gen == 3 ]]; then
echo "2/130";
else
echo "Unknown PCIe Gen: $pcie_gen" >&2
exit 1;
fi
}
for pcie in $(lspci | grep Mellanox | awk '{ print $1; }'); do
echo "For the PCIe port:"
lspci -s $pcie
cap_width=$(get_cap_width "$pcie")
cap_speed=$(get_cap_speed "$pcie")
sta_width=$(get_sta_width "$pcie")
sta_speed=$(get_sta_speed "$pcie")
pcie_gen=$(get_pcie_gen "$pcie")
encoding=$(get_encoding "$pcie_gen")
cap_max_payload=$(get_cap_max_payload "$pcie")
ctl_max_payload=$(get_ctl_max_payload "$pcie")
ctl_max_read_request=$(get_ctl_max_read_request "$pcie")
# Taken from Mellanox page
current_speed=$(_calc "$sta_speed * $sta_width * (1 - $encoding) - 1")
max_speed=$(_calc "$cap_speed * $cap_width * (1 - $encoding) - 1")
echo "PCIe generation: $pcie_gen"
echo "Encoding loss: $encoding"
echo "Max Width: x$cap_width (current: x$sta_width)"
echo "Max Speed: $cap_speed GT/s (current: $sta_speed GT/s)"
echo "Max Payload: $cap_max_payload bytes (current: $ctl_max_payload bytes)"
echo "Max Read Request: $ctl_max_read_request bytes"
echo "Max PCIe Bandwidth: $max_speed Gb/s (current: $current_speed Gb/s)"
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment