]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
lsinitrd: optimize performance when handling multiple files
authorKairui Song <kasong@redhat.com>
Wed, 25 Jul 2018 08:47:37 +0000 (16:47 +0800)
committerHarald Hoyer <harald@hoyer.xyz>
Thu, 26 Jul 2018 07:57:55 +0000 (09:57 +0200)
Currently, when trying to unpack or print the content of multiple
files, lsinitrd will decompress the image and pipe the decompressed
content to cpio to retrive each file if the image is compressed.
Which mean if we want to extract 10 files the image will be decompressed
10 times, which is a waste of time.

This patch will let lsinitrd decompress the image file to a temp file
first if multiple file names are given, then cpio will read from the
decompressed temp file, which will speed up a lot.

Time consumption test for command:
`lsinitrd initramfs-4.16.15-300.fc28.x86_64.img \
    usr/lib/dracut/build-parameter.txt \
    usr/lib/dracut/modules.txt \
    etc/machine-id \
    etc/hostname \
    usr/lib/udev/rules.d/99-systemd.rules`

Before the patch:
2.37user 0.33system 0:02.12elapsed

After the patch:
0.50user 0.42system 0:00.72elapsed

There would be a more significant time difference if we try to
extract more files.

lsinitrd.sh

index 1b9a93b60203285d08560e88d1749a2294b73503..0cb89e9c12b4b23cb9d6d3154a6d883e7bb13ca6 100755 (executable)
@@ -251,6 +251,17 @@ if [[ $SKIP ]]; then
     CAT=skipcpio
 fi
 
+if (( ${#filenames[@]} > 1 )); then
+    TMPFILE="$(mktemp -t --suffix=.cpio lsinitrd.XXXXXX)"
+    $CAT "$image" 2>/dev/null > $TMPFILE
+    trap "rm -f '$TMPFILE'" EXIT
+    pre_decompress()
+    {
+        cat $TMPFILE
+    }
+    CAT=pre_decompress
+fi
+
 ret=0
 
 if [[ -n "$unpack" ]]; then