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