Files
systemd/shell-completion/bash/systemd-cgtop
T
Eric BoucherandZbigniew Jędrzejewski-Szmek 34127b7c4c cgtop: make root row default to cgroup with '/' to toggle
The root row in systemd-cgtop previously displayed whole-system RAM
usage read from /proc/meminfo (MemTotal - MemAvailable), not a cgroup
sum. This is a pragmatic fallback — the kernel intentionally does not
expose memory.current on the v2 root cgroup (see the existing
is_root_cgroup() comment about avoiding 'multiple truth about system
state'), so cgtop picked /proc/meminfo to fill the gap.

Nevertheless, that caued the '/' row to provide different information
than every other row in the table:
- All child rows: 'what is this cgroup charged for?' (anon + file
  + sock + shmem attributable to the cgroup).
- Root row: 'what is the whole system using?' (includes kernel
  slab, vmalloc, pagetables, zram mem_used, and anything not
  attributable to any cgroup).

On a typical desktop the root row can read ~10 GB while the sum of
direct children is ~5 GB, with no explanation visible in the tool
itself.

This commit:
  1. Introduces --root-memory= command line option.
  2. Flips the default for the root row to the sum of memory.current
     over its direct children.
  3. Adds a '/' interactive key that toggles between the two modes.
  4. Updates the interactive help string ('?' / 'h'), the man page,
     and the bash completion for the new option.

Inside a container (detect_container() > 0), is_root_cgroup() already
returns false, so none of this applies — the container's '/' still
reads memory.current from the namespaced cgroup as before.

Co-authored-by: Zbigniew Jędrzejewski-Szmek <zbyszek@amutable.com>
2026-10-09 11:19:05 +02:00

71 lines
2.2 KiB
Bash

# shellcheck shell=bash
# systemd-cgtop(1) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# systemd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <https://www.gnu.org/licenses/>.
__contains_word() {
local w word=$1; shift
for w in "$@"; do
[[ $w = "$word" ]] && return
done
}
__get_machines() {
local a b
{ machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null; echo ".host"; } |
{ while read -r a b; do echo " $a"; done; } |
sort -u
}
_systemd_cgtop() {
local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} words cword
local comps
local -A OPTS=(
[STANDALONE]='-h --help --version -p -t -c -m -i -b --batch -r --raw -k -P'
[ARG]='--cpu --depth -M --machine --recursive -n --iterations -d --delay --order --root-memory'
)
_init_completion || return
if __contains_word "$prev" ${OPTS[ARG]}; then
case $prev in
--machine|-M)
comps=$( __get_machines )
;;
--recursive)
comps='yes no'
;;
--order)
comps='path tasks cpu memory io'
;;
--root-memory)
comps='cgroup system'
;;
esac
COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
return 0
fi
COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
if [ -d /sys/fs/cgroup/ ]; then
COMPREPLY+=( $(cd /sys/fs/cgroup/ && compgen -o nospace -o dirnames "$cur") )
fi
}
complete -F _systemd_cgtop systemd-cgtop