Created
August 29, 2017 14:44
-
-
Save lexeii/8e60e1855aa4b0a16bc7e68302bb1d3d to your computer and use it in GitHub Desktop.
lspci emulation for Busybox lspci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# use option -nn to show both textual and numeric ID's (names & numbers) | |
i=/usr/share/misc/pci.ids.gz | |
if [ ! -s $i ]; then | |
busybox lspci | |
else | |
pciids=$(mktemp) | |
zcat $i | sed '/^#/d; /^\t\t/d' > $pciids | |
classes=$(mktemp) | |
sed -n '/^C /,$p' $pciids > $classes | |
busybox lspci | \ | |
while read a b c id; do | |
echo -n "$a" | |
awk -va=${c:0:2} -vb=${c:2:2} -vo="$1" ' | |
{ | |
if ($1 == "C" && $2 == a) class = substr($0, 5) | |
if (class != "" && $1 == b) { class = substr($0, 5); exit } | |
} | |
END { | |
if (o == "-nn") | |
printf("%s [%s%s]: ", class, a, b) | |
else | |
printf("%s: ", class) | |
}' $classes | |
sed -n "/${id:0:4}/,/^[^\t]/p" $pciids | \ | |
awk -vh=${id:0:4} -vl=${id:5:4} -vo="$1" ' | |
{ | |
if ($1 == h) | |
{ m = substr($0, 7); next } | |
else if ($1 == l) | |
{ d = substr($0, 7); exit } | |
} | |
END { | |
if (o == "-nn") nn = " [" h ":" l "]"; | |
printf("%s%s%s\n", m, d, nn); | |
}' | |
done | |
rm $pciids $classes | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
busybox lspci | while read a b c id ; do | |
echo -n "$a " | |
if [ $b != "Class" ] || [ ! -s /usr/share/misc/pci.ids.gz ]; then | |
echo "$b $c $id" | |
continue | |
fi | |
zcat /usr/share/misc/pci.ids.gz | \ | |
awk -va=${c:0:2} -vb=${c:2:2} -vh=${id:0:4} -vl=${id:5:4} '{ | |
if ($1 == "C" && $2 == a) class=substr($0,5) | |
if (class != "" && $1 == b) { class=substr($0,5); exit } | |
if (substr($0,1,4) == h) m=substr($0,7) | |
else if (m == "") next | |
else if (substr($0,2,4) == l) { name=m substr($0,7); m="" } | |
else if ($1 == h && $2 == l) { name=m substr($0,14); m="" } | |
} END { print class ": " name }' | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lspci --version | |
lspci version 3.2.1 | |
time lspci | |
real 0m 0.03s | |
user 0m 0.02s | |
sys 0m 0.00s | |
time lspci-emul-orig.sh | |
real 0m 3.50s | |
user 0m 3.43s | |
sys 0m 0.17s | |
time lspci-emul-opt.sh | |
real 0m 0.73s | |
user 0m 0.73s | |
sys 0m 0.11s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment