From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on sa.local.altlinux.org X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686851985; x=1689443985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=9wsuwkOxNAcgZsk3Cgcjuo0mp+yN3qdhxgKS0STYv1Q=; b=g46IzbMWInT/QCFyLjf/IFEHuzi0kOOvXdHihiw1pCvb5lTJYLrCi7QI65eCcub0QU 49f9MaOYq14hF4qNeb/w3mUDr0xH+qXrYaXoXDwVxDAOrf/3mMnxKE/6dzI4UrofZMMX HMH0PW8UakHNy7obQ4FDHvOv3qo7AAVLE8/niI2KqJMqlsTL3pQP4l/l3xmC6oQGGbc3 Ae8GinmHPAA2wQ91y6tcyEnzuhXf/wuoHAxllpB51SiBcyo5qGr13bgmOmZuoUFLD+uT oYU4t4mVE+5WbAb2P4OJqi7bLx4LZ3/iaqGLlEWD1RMA2FX0W0G8pn9Lz2PBOCdY56Ya e26Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686851985; x=1689443985; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9wsuwkOxNAcgZsk3Cgcjuo0mp+yN3qdhxgKS0STYv1Q=; b=dM1uVWetYGAc6BGkHAMlzIxwQeTKL0h/Sr0/DIp2ZyUq+R14Vr6kiv8M6geEEUUPl5 q6hxuv8nj5r9SsgTtEjKaZy6Xma08GzuhOrbXIpZvoEUI1RAF+tLz6QbNk0wD+/2+YL6 EJqU1UIFIPpANqE1ZGIMXwo6gSYEVj6SHTxtjsTdk/a2Cq5U73slf+wQLCEEHmt9zEUv WZ2DXfCdcL7toa634u3Z4MiMRhVCNormnoJKhE2RIr83DnBGBRz17nDnA2F/nlAfzyR9 spDnkkpTmkk3XrnVwq6Zbs4v3BR+zPoqYbMFECwR8yfISMAqsus1tBpiniErGCDuWCQo eD5Q== X-Gm-Message-State: AC+VfDxJ6QB6BDCAf94e1OVDLAQRBUM9iIr0VrC4ZmkKiNrRnolFLJdH OWzWkgEPoxkyglLwrKf0Y4GVsmMAWEQ= X-Google-Smtp-Source: ACHHUZ4suXxFZenyBUoGjzOIY+X9V6eQwcD2zhrmlTyo5Q/ek0CywlWEH+8kQOmylNWrSyKYCkOdpA== X-Received: by 2002:a05:600c:2213:b0:3f7:81e9:2f02 with SMTP id z19-20020a05600c221300b003f781e92f02mr48868wml.4.1686851984986; Thu, 15 Jun 2023 10:59:44 -0700 (PDT) From: Alexey Gladkov To: make-initrd@lists.altlinux.org Date: Thu, 15 Jun 2023 19:59:17 +0200 Message-Id: X-Mailer: git-send-email 2.33.8 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [make-initrd] [PATCH v1 08/11] feature/procacct: Add accounting report X-BeenThere: make-initrd@lists.altlinux.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: make-initrd@lists.altlinux.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Jun 2023 17:59:51 -0000 Archived-At: List-Archive: 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 --- 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