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