#!/usr/bin/env bash
set -euo pipefail

# shellcheck source=../lib/common.sh
source "$(dirname "$0")/../lib/common.sh"
# shellcheck source=../lib/docker.sh
source "$(dirname "$0")/../lib/docker.sh"

NAME="${1:-$LM_CONTAINER}"
require_container "$NAME"

echo "=== docker stats ==="
"${DOCKER[@]}" stats --no-stream "$NAME"
echo
echo "=== limits ==="
print_container_limits "$NAME"

CID="$("${DOCKER[@]}" inspect -f '{{.Id}}' "$NAME")"
CG="/sys/fs/cgroup"
echo
echo "=== cgroup (host) ==="
printf 'memory.usage = '
cat "$CG/memory/docker/$CID/memory.usage_in_bytes" 2>/dev/null \
  || cat "$CG/memory/system.slice/docker-$CID.scope/memory.usage_in_bytes" 2>/dev/null \
  || echo "n/a"
printf ' bytes\n'
echo
echo "=== processes ==="
"${DOCKER[@]}" top "$NAME"
