From 9cb1c4df7b9ce1a9ad1312621b0f2b16a94fba3a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 3 Jun 2024 15:54:25 +0200 Subject: Adding upstream version 060+5. Signed-off-by: Daniel Baumann --- modules.d/99memstrack/memstrack-report.sh | 17 ++++++++ modules.d/99memstrack/memstrack-start.sh | 65 +++++++++++++++++++++++++++++++ modules.d/99memstrack/memstrack.service | 15 +++++++ modules.d/99memstrack/module-setup.sh | 28 +++++++++++++ 4 files changed, 125 insertions(+) create mode 100755 modules.d/99memstrack/memstrack-report.sh create mode 100755 modules.d/99memstrack/memstrack-start.sh create mode 100644 modules.d/99memstrack/memstrack.service create mode 100755 modules.d/99memstrack/module-setup.sh (limited to 'modules.d/99memstrack') diff --git a/modules.d/99memstrack/memstrack-report.sh b/modules.d/99memstrack/memstrack-report.sh new file mode 100755 index 0000000..0ee7f2f --- /dev/null +++ b/modules.d/99memstrack/memstrack-report.sh @@ -0,0 +1,17 @@ +#!/bin/sh +. /lib/dracut-lib.sh + +if ! [ "$DEBUG_MEM_LEVEL" -ge 4 ]; then + return 0 +fi + +if command -v systemctl > /dev/null; then + systemctl stop memstrack.service +else + pkill --signal INT '[m]emstrack' + while pgrep -c '[m]emstrack' > /dev/null; do + sleep 1 + done +fi + +cat /.memstrack diff --git a/modules.d/99memstrack/memstrack-start.sh b/modules.d/99memstrack/memstrack-start.sh new file mode 100755 index 0000000..45f65c2 --- /dev/null +++ b/modules.d/99memstrack/memstrack-start.sh @@ -0,0 +1,65 @@ +#!/bin/sh +# Mount kernel debug fs so debug tools can work. +# memdebug=4 and memdebug=5 requires debug fs to be mounted. +# And there is no need to umount it. + +type getargnum > /dev/null 2>&1 || . /lib/dracut-lib.sh + +# "sys/kernel/tracing" has the priority if exists. +get_trace_base() { + # trace access through debugfs would be obsolete if "/sys/kernel/tracing" is available. + if [ -d "/sys/kernel/tracing" ]; then + echo "/sys/kernel" + else + echo "/sys/kernel/debug" + fi +} + +is_debugfs_ready() { + [ -f "$(get_trace_base)/tracing/trace" ] +} + +prepare_debugfs() { + trace_base=$(get_trace_base) + # old debugfs interface case. + if ! [ -d "$trace_base/tracing" ]; then + mount none -t debugfs "$trace_base" + # new tracefs interface case. + elif ! [ -f "$trace_base/tracing/trace" ]; then + mount none -t tracefs "$trace_base/tracing" + fi + + if ! [ -f "$trace_base/tracing/trace" ]; then + echo "WARN: failed to mount debugfs" + return 1 + fi +} + +if ! is_debugfs_ready; then + prepare_debugfs +fi + +if [ -n "$DEBUG_MEM_LEVEL" ]; then + if [ "$DEBUG_MEM_LEVEL" -ge 5 ]; then + echo "memstrack - will report kernel module memory usage summary and top allocation stack" + nohup memstrack --report module_summary,module_top --notui --throttle 80 -o /.memstrack > /dev/null & + elif [ "$DEBUG_MEM_LEVEL" -ge 4 ]; then + echo "memstrack - will report memory usage summary" + nohup memstrack --report module_summary --notui --throttle 80 -o /.memstrack > /dev/null & + else + exit 0 + fi +fi + +PID=$! +RET=$? + +if [ $RET -ne 0 ]; then + echo "Failed to start memstrack, exit status: $RET" + exit $RET +fi + +echo $PID > /run/memstrack.pid + +# Wait a second for memstrack to setup everything, avoid missing any event +sleep 1 diff --git a/modules.d/99memstrack/memstrack.service b/modules.d/99memstrack/memstrack.service new file mode 100644 index 0000000..3717c73 --- /dev/null +++ b/modules.d/99memstrack/memstrack.service @@ -0,0 +1,15 @@ +[Unit] +Description=Memstrack Anylazing Service +DefaultDependencies=no +Before=dracut-cmdline.service systemd-udevd.service local-fs-pre.target +IgnoreOnIsolate=true +ConditionKernelCommandLine=|rd.memdebug=4 +ConditionKernelCommandLine=|rd.memdebug=5 + +[Service] +Type=forking +ExecStart=/bin/memstrack-start +PIDFile=/run/memstrack.pid +StandardInput=null +StandardOutput=journal+console +StandardError=journal+console diff --git a/modules.d/99memstrack/module-setup.sh b/modules.d/99memstrack/module-setup.sh new file mode 100755 index 0000000..27563eb --- /dev/null +++ b/modules.d/99memstrack/module-setup.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +check() { + if ! require_binaries pgrep pkill memstrack; then + dinfo "memstrack is not available" + dinfo "If you need to use rd.memdebug>=4, please install memstrack and procps-ng" + return 1 + fi + + return 0 +} + +depends() { + echo systemd + return 0 +} + +install() { + inst_multiple pgrep pkill nohup + inst "/bin/memstrack" "/bin/memstrack" + + inst "$moddir/memstrack-start.sh" "/bin/memstrack-start" + inst_hook cleanup 99 "$moddir/memstrack-report.sh" + + inst "$moddir/memstrack.service" "$systemdsystemunitdir/memstrack.service" + + $SYSTEMCTL -q --root "$initdir" add-wants initrd.target memstrack.service +} -- cgit v1.2.3