[make-initrd] [PATCH v1 08/11] feature/procacct: Add accounting report

Alexey Gladkov gladkov.alexey at gmail.com
Thu Jun 15 20:59:17 MSK 2023


Process the raw report and display it in a more user-friendly way just
before system init execution. It is possible to control which values to
include in the report sing the boot cmdline parameter rdacct="..." .

Signed-off-by: Alexey Gladkov <gladkov.alexey at gmail.com>
---
 data/etc/rc.d/rc.sysexec                      |  12 ++
 data/etc/rc.d/rc.sysinit                      |  23 +++-
 features/debug-procacct/config.mk             |   2 +-
 .../debug-procacct/data/bin/procacct-report   | 117 ++++++++++++++++++
 .../data/etc/initrd/cmdline.d/procacct        |   1 +
 features/debug-procacct/rules.mk              |   2 +-
 6 files changed, 149 insertions(+), 8 deletions(-)
 create mode 100755 features/debug-procacct/data/bin/procacct-report
 create mode 100644 features/debug-procacct/data/etc/initrd/cmdline.d/procacct

diff --git a/data/etc/rc.d/rc.sysexec b/data/etc/rc.d/rc.sysexec
index eaa859a4..af2549fe 100755
--- a/data/etc/rc.d/rc.sysexec
+++ b/data/etc/rc.d/rc.sysexec
@@ -3,6 +3,18 @@
 . /.initrd/initenv
 . /etc/init.d/functions
 
+if [ -s /tmp/procacct.stats ]; then
+	printf '### Accounting Results ###\n'
+	/bin/procacct-report /tmp/procacct.stats
+	printf '### Accounting Done ###\n'
+fi
+
+if [ -s /tmp/procacct.err ]; then
+	printf '### Accounting Error ###\n'
+	cat /tmp/procacct.err
+	printf '### Accounting FAILES ###\n'
+fi
+
 ! mountpoint -q /proc ||
 	umount -fl /proc
 
diff --git a/data/etc/rc.d/rc.sysinit b/data/etc/rc.d/rc.sysinit
index 8e0ee7fd..de63bfa5 100755
--- a/data/etc/rc.d/rc.sysinit
+++ b/data/etc/rc.d/rc.sysinit
@@ -11,17 +11,23 @@ dmesg -n "${LOGLEVEL:-1}"
 
 mount -n -t proc -o nodev,noexec,nosuid proc /proc
 
+have_acct=
 quiet=n
-__set_quiet()
+__handler()
 {
-	[ "$1" = 'quiet' ] ||
-		return 0
-	quiet=y
-	[ -z "$2" ] || ! shell_var_is_no "$2" || quiet=n
+	case "${1,,}" in
+		quiet)
+			quiet=y
+			[ -z "$2" ] || ! shell_var_is_no "$2" || quiet=n
+			;;
+		rdacct)
+			have_acct=1
+			;;
+	esac
 }
 
 read -r CMDLINE < /proc/cmdline
-cmdline_foreach "$CMDLINE" __set_quiet
+cmdline_foreach "$CMDLINE" __handler
 
 echo "QUIET=$quiet" >>/etc/sysconfig/init
 
@@ -34,5 +40,10 @@ if shell_var_is_no "$quiet"; then
 	}
 fi
 
+if [ -n "$have_acct" ]; then
+	mount -n -t sysfs sysfs /sys
+	/bin/procacct -o /tmp/procacct.stats 2>/tmp/procacct.err &
+fi
+
 # Alt-Uparrow
 spawn-shell ${CONSOLE_KEYSTROKE:-alt keycode 103} &
diff --git a/features/debug-procacct/config.mk b/features/debug-procacct/config.mk
index 8b137891..06dcf603 100644
--- a/features/debug-procacct/config.mk
+++ b/features/debug-procacct/config.mk
@@ -1 +1 @@
-
+PROCACCT_DATA = $(FEATURESDIR)/debug-procacct/data
diff --git a/features/debug-procacct/data/bin/procacct-report b/features/debug-procacct/data/bin/procacct-report
new file mode 100755
index 00000000..049c50e8
--- /dev/null
+++ b/features/debug-procacct/data/bin/procacct-report
@@ -0,0 +1,117 @@
+#!/bin/bash -efu
+# SPDX-License-Identifier: GPL-2.0
+
+. shell-error
+. shell-quote
+
+readonly def_type=top
+readonly def_kind=cputime:rssusage:etime
+readonly def_limit=10
+
+readonly procacct_stats_file="$1"
+shift
+
+if [ "$#" -eq 0 ]; then
+	[ ! -f /.initrd/initenv ] || . /.initrd/initenv
+
+	if [ -n "${RDACCT-}" ] && [ "${RDACCT-}" != 1 ]; then
+		quote_shell_variable vars "${RDACCT//,/ }"
+		eval -- "$vars"
+	fi
+else
+	kind="${1//:/ }"
+fi
+
+type="${type:-$def_type}"
+kind="${kind:-$def_kind}"
+limit="${limit:-$def_limit}"
+
+set -- ${kind//:/ }
+
+for (( i=$#; i > 0; i-- )) do
+	case "$1" in
+		iobytes) set -- "$@" iorbytes iowbytes ;;
+		bytes)   set -- "$@" rbytes wbytes     ;;
+		*)       set -- "$@" "$1"              ;;
+	esac
+	shift
+done
+
+i=0; FIELD=(); DESC=(); UNIT=();
+
+F_TYPE=$(( $i + 1 ))     ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+F_PID=$(( $i + 1 ))      ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+F_TGID=$(( $i + 1 ))     ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+F_PPID=$(( $i + 1 ))     ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+F_BTIME=$(( $i + 1 ))    ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+F_ETIME=$(( $i + 1 ))    ; FIELD[$i]="etime"    ; DESC[$i]="Elapsed time"    ; UNIT[$i]="usec"  ; i=$(( $i + 1 ))
+F_CPUTIME=$(( $i + 1 ))  ; FIELD[$i]="cputime"  ; DESC[$i]="CPU time"        ; UNIT[$i]="usec"  ; i=$(( $i + 1 ))
+F_VMUSAGE=$(( $i + 1 ))  ; FIELD[$i]="vmusage"  ; DESC[$i]="VM usage"        ; UNIT[$i]="kb"    ; i=$(( $i + 1 ))
+F_RSSUSAGE=$(( $i + 1 )) ; FIELD[$i]="rssusage" ; DESC[$i]="RSS usage"       ; UNIT[$i]="kb"    ; i=$(( $i + 1 ))
+F_RBYTES=$(( $i + 1 ))   ; FIELD[$i]="rbytes"   ; DESC[$i]="Read bytes"      ; UNIT[$i]="bytes" ; i=$(( $i + 1 ))
+F_WBYTES=$(( $i + 1 ))   ; FIELD[$i]="wbytes"   ; DESC[$i]="Write bytes"     ; UNIT[$i]="bytes" ; i=$(( $i + 1 ))
+F_IORBYTES=$(( $i + 1 )) ; FIELD[$i]="iorbytes" ; DESC[$i]="Read I/O bytes"  ; UNIT[$i]="bytes" ; i=$(( $i + 1 ))
+F_IOWBYTES=$(( $i + 1 )) ; FIELD[$i]="iowbytes" ; DESC[$i]="Write I/O bytes" ; UNIT[$i]="bytes" ; i=$(( $i + 1 ))
+F_COMM=$(( $i + 1 ))     ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+F_CMDLINE=$(( $i + 1 ))  ; FIELD[$i]=""         ; DESC[$i]=""                ; UNIT[$i]=""      ; i=$(( $i + 1 ))
+
+cut_output()
+{
+	case "$limit" in
+		(-[1-9]*) tail -n "${limit#-}" ;;
+		( [1-9]*) head -n "$limit" ;;
+		(   ''|0) cat ;;
+	esac
+}
+
+output_top()
+{
+	printf '%s:\n' "Top $limit of ${DESC[$index]}"
+
+	cut -f "${F_TYPE},${F_PID},${F_TGID},${F_BTIME},${field},${F_COMM}-" \
+		"$1" |
+	while IFS='	' read -r ptype pid tgid btime data comm cmdline; do
+		date="$(date -u +'%Y-%m-%d %H:%M:%S' -d "@$btime" 2>/dev/null)" ||
+			date="@$btime secs"
+
+		case "$ptype" in
+			P) name="Process" ;;
+			T) name="Thread $pid" ;;
+			*) name="Unknown thing id=$pid" ;;
+		esac
+
+		[ "$cmdline" != - ] ||
+			cmdline="$comm"
+
+		printf '  * %s (pid=%s):\n' "$name" "$tgid"
+		printf '    - %-15s: %s\n' "Start time"      "$date"
+		printf '    - %-15s: %s\n' "${DESC[$index]}" "$data ${UNIT[$index]}"
+		printf '    - %-15s: %s\n' "Command"         "$cmdline"
+	done
+	printf '\n'
+}
+
+data_file="/tmp/$PROG.stats"
+
+for by_kind; do
+	by_kind="${by_kind,,}"
+
+	for index in "${!FIELD[@]}" 0; do
+		[ "${FIELD[$index]}" != "$by_kind" ] ||
+			break
+	done
+
+	[ $index -gt 0 ] ||
+		continue
+
+	field=$(( $index + 1 ))
+
+	sort -nrk "$field,$field" "$procacct_stats_file" |
+		cut_output > "$data_file"
+
+	case "$type" in
+		top) output_top "$data_file" ;;
+	esac
+
+	echo rm -f -- "$data_file"
+done
diff --git a/features/debug-procacct/data/etc/initrd/cmdline.d/procacct b/features/debug-procacct/data/etc/initrd/cmdline.d/procacct
new file mode 100644
index 00000000..99474627
--- /dev/null
+++ b/features/debug-procacct/data/etc/initrd/cmdline.d/procacct
@@ -0,0 +1 @@
+register_parameter string RDACCT
diff --git a/features/debug-procacct/rules.mk b/features/debug-procacct/rules.mk
index 8b137891..5aabfbd2 100644
--- a/features/debug-procacct/rules.mk
+++ b/features/debug-procacct/rules.mk
@@ -1 +1 @@
-
+PUT_FEATURE_DIRS += $(PROCACCT_DATA)
-- 
2.33.8



More information about the Make-initrd mailing list