Make-initrd development discussion
 help / color / mirror / Atom feed
From: Alexey Gladkov <gladkov.alexey@gmail.com>
To: make-initrd@lists.altlinux.org
Subject: [make-initrd] [PATCH v1 08/11] feature/procacct: Add accounting report
Date: Thu, 15 Jun 2023 19:59:17 +0200
Message-ID: <ac871d913e38fdd7c0252a431d5135d12d32f725.1686851829.git.gladkov.alexey@gmail.com> (raw)
In-Reply-To: <cover.1686851829.git.gladkov.alexey@gmail.com>

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@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



  parent reply	other threads:[~2023-06-15 17:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-15 17:59 [make-initrd] [PATCH v1 00/11] Add accounting feature Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 01/11] feature/procacct: New feature to debug initramfs Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 02/11] feature/procacct: Use epoll Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 03/11] feature/procacct: Use default rcvbufsz Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 04/11] feature/procacct: Track more values Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 05/11] feature/procacct: Use msgtemplate instead of custom struct Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 06/11] feature/procacct: Use nonblocking per-call Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 07/11] feature/procacct: Add bpf helper Alexey Gladkov
2023-06-15 17:59 ` Alexey Gladkov [this message]
2023-06-15 17:59 ` [make-initrd] [PATCH v1 09/11] feature/procacct: Wait until procacct is initialized Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 10/11] feature/procacct: Make procacct optional Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 11/11] feature/procacct: Add to testing Alexey Gladkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac871d913e38fdd7c0252a431d5135d12d32f725.1686851829.git.gladkov.alexey@gmail.com \
    --to=gladkov.alexey@gmail.com \
    --cc=make-initrd@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Make-initrd development discussion

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/make-initrd/0 make-initrd/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 make-initrd make-initrd/ http://lore.altlinux.org/make-initrd \
		make-initrd@lists.altlinux.org make-initrd@lists.altlinux.ru make-initrd@lists.altlinux.com
	public-inbox-index make-initrd

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.make-initrd


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git