config

OpenBSD system configuration
git clone git://jacobedwards.org/config
Log | Files | Refs | README

commit 95d6556c5dda4d8a777d924dfd6d354ae2a4ab8c
parent 130a17c65f313487c9479b5b1e85636aa9550fcf
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 17 Feb 2021 19:58:37 -0800

Add mem, A memory usage printing script

Also add a helper script, human.awk, to downscale numerical input
using unit prefixes, e.g. Ki or Mi.

Diffstat:
Amodules/scripts/.local/bin/human.awk | 43+++++++++++++++++++++++++++++++++++++++++++
Amodules/scripts/.local/bin/mem | 22++++++++++++++++++++++
2 files changed, 65 insertions(+), 0 deletions(-)

diff --git a/modules/scripts/.local/bin/human.awk b/modules/scripts/.local/bin/human.awk @@ -0,0 +1,43 @@ +#!/usr/bin/awk -f +# Copyright 2021 Jacob R. Edwards +# License: GPLv3 +# +# human.awk -- Scale numbers using unit prefixes +# +# This awk(1) script tries to keep numeric input short by using +# unit prefixes such as `Ki', `Mi', `Gi', etc. Whitespace in numeric +# input is omitted from the result, non-numeric input is passed +# through unchanged. Note that "numeric" is defined as `[0-9]+' +# rendering negitive numbers "non-numeric". +# +# METRIC Metric `decimal' or `binary' (default `binary') +# START Input size (1 for `K', 2 for `M', etc.) (default 0) + +function tohuman(n) +{ + for (i = START; n > MULT && i < MAX; ++i) + n /= MULT; + return i ? sprintf("%zu%c%c", n, SIZES[i], SUFX) : n; +} + +BEGIN { + MAX = split("KMGTPEZY", SIZES, ""); + if (!METRIC || METRIC == "binary") { + MULT = 1024; + SUFX = "i"; + } else if (METRIC == "decimal") { + MULT = 1000; + SUFX = "b"; + } else { + printf("%s: '%s': Invalid metric.\n", ARGV[0], METRIC) \ + > "/dev/stderr"; + exit(1); + } +} + +/^[ \t]*[0-9]+[ \t]*$/ { + print(tohuman($0)); + next; +} + +1 diff --git a/modules/scripts/.local/bin/mem b/modules/scripts/.local/bin/mem @@ -0,0 +1,22 @@ +#!/bin/sh +# Copyright 2021 Jacob R. Edwards +# License: GPLv3 +# +# mem -- Display memory usage +# +# t[otal] Display the total memory usage +# a[ll] Display per process memory usage in `mem,pid,program' format + +human() human.awk -vSTART=1 "$@" +total() awk "$@" '{ t += $0 } END { print t }' + +case ${1:-all} in +(a|all) + ps -Acorss,pid,command | sed -Ee 1d -e 's/^ *([^ ]+) /\1\ +/' | human | paste -d' ' - - | sed -E 's/ +/ /g' | sort -h;; +(t|total) + ps -Aorss | total | human;; +(*) + printf 'usage: %s [all|total]\n' `basename $0` 1>&2 + exit 1 +esac