28ab1c
From f03ee48fab36a9fe55082f15111771b698081598 Mon Sep 17 00:00:00 2001
28ab1c
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
28ab1c
Date: Wed, 5 May 2021 15:46:08 +0200
28ab1c
Subject: [PATCH] fix(dracut-functions): implement a cache for get_maj_min
28ab1c
MIME-Version: 1.0
28ab1c
Content-Type: text/plain; charset=UTF-8
28ab1c
Content-Transfer-Encoding: 8bit
28ab1c
28ab1c
On systems with a large number of devices, usually multipath devices,
28ab1c
dracut can spend a lot of time stat'ing the devices to collect the
28ab1c
major/minor numbers, leading to huge slowness rebuilding the initramfs
28ab1c
when stat'ing devices is slow (seen with oracleasm file systems in
28ab1c
particular).
28ab1c
This commit implements a basic cache stored in a file under
28ab1c
DRACUT_TMPDIR storing the major:minor corresponding to the specified
28ab1c
device.
28ab1c
28ab1c
Reproducer: create N loopback devices used as a LVM extension to volume
28ab1c
group hosting the root file system
28ab1c
28ab1c
  # LVMVG="rhel"
28ab1c
  # NDEVICES=200
28ab1c
  # mkdir devices; for i in $(seq 1 $NDEVICES); do
28ab1c
    truncate -s 10m devices/$i; losetup loop$i devices/$i
28ab1c
  done
28ab1c
  # vgextend $LVMVG $(/bin/ls -1 /dev/loop[0-9]*)
28ab1c
28ab1c
With standard code (tested with RHEL8.3 dracut):
28ab1c
28ab1c
  # dracut -f --debug /tmp/initramfs.img $(uname -r) >/tmp/debug 2>&1
28ab1c
  # grep -c "stat -L -c" /tmp/debug
28ab1c
  2440
28ab1c
28ab1c
With this code:
28ab1c
28ab1c
  # dracut -f --debug /tmp/initramfs.img $(uname -r) >/tmp/debug_optim 2>&1
28ab1c
  # grep -c "stat -L -c" /tmp/debug_optim
28ab1c
  205
28ab1c
28ab1c
Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
28ab1c
(cherry picked from commit c3bb9d18dceed7db6d16f9c2a7f682c5934099d7)
28ab1c
28ab1c
Cherry-picked from: c3bb9d18dceed7db6d16f9c2a7f682c5934099d7
28ab1c
Resolves: #1957622
28ab1c
---
28ab1c
 dracut-functions.sh | 10 +++++++---
28ab1c
 dracut.sh           |  4 ++++
28ab1c
 2 files changed, 11 insertions(+), 3 deletions(-)
28ab1c
28ab1c
diff --git a/dracut-functions.sh b/dracut-functions.sh
28ab1c
index 1431dd18..a221967c 100755
28ab1c
--- a/dracut-functions.sh
28ab1c
+++ b/dracut-functions.sh
28ab1c
@@ -199,12 +199,16 @@ get_fs_env() {
28ab1c
 # $ get_maj_min /dev/sda2
28ab1c
 # 8:2
28ab1c
 get_maj_min() {
28ab1c
-    local _maj _min _majmin
28ab1c
+    local _majmin
28ab1c
+    out="$(grep -m1 -oP "^$1 \K\S+$" "${get_maj_min_cache_file:?}")"
28ab1c
+    if [ -z "$out" ]; then
28ab1c
     _majmin="$(stat -L -c '%t:%T' "$1" 2>/dev/null)"
28ab1c
-    printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
28ab1c
+        out="$(printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))")"
28ab1c
+        echo "$1 $out" >> "${get_maj_min_cache_file:?}"
28ab1c
+    fi
28ab1c
+    echo -n "$out"
28ab1c
 }
28ab1c
 
28ab1c
-
28ab1c
 # get_devpath_block <device>
28ab1c
 # get the DEVPATH in /sys of a block device
28ab1c
 get_devpath_block() {
28ab1c
diff --git a/dracut.sh b/dracut.sh
28ab1c
index 4340e646..f8e68ccb 100755
28ab1c
--- a/dracut.sh
28ab1c
+++ b/dracut.sh
28ab1c
@@ -901,6 +901,10 @@ readonly DRACUT_TMPDIR="$(mktemp -p "$TMPDIR/" -d -t dracut.XXXXXX)"
28ab1c
     exit 1
28ab1c
 }
28ab1c
 
28ab1c
+# Cache file used to optimize get_maj_min()
28ab1c
+declare -x -r get_maj_min_cache_file="${DRACUT_TMPDIR}/majmin_cache"
28ab1c
+: > "$get_maj_min_cache_file"
28ab1c
+
28ab1c
 # clean up after ourselves no matter how we die.
28ab1c
 trap '
28ab1c
     ret=$?;
28ab1c