@@ -496,11 +496,59 @@ _metrics_read_cpu() {
496496 echo $(( (dtotal - didle) * 1000 * ncpus / dtotal ))
497497}
498498
499- # Returns "<used_bytes> <total_bytes>" from /proc/meminfo.
500- # Prefers MemAvailable (kernel's own estimate of reclaimable memory) so that
501- # page-cache is not counted as "used", matching what tools like `free` report.
499+ # Returns "<used_bytes> <total_bytes>" scoped to this container.
500+ # Reads cgroup memory files (accurate inside Docker/Pterodactyl containers).
501+ # Falls back to /proc/meminfo only when no cgroup files are present.
502+ #
503+ # Cgroup v2: memory.current / memory.max (values already in bytes)
504+ # Cgroup v1: memory.usage_in_bytes / memory.limit_in_bytes minus total_cache
505+ # /proc/meminfo fallback: host-level, used as last resort only.
502506_metrics_read_memory () {
503- local total=0 available=0 free=0 buffers=0 cached=0 sreclaimable=0 key val
507+ local used=0 total=0
508+
509+ # ── cgroup v2 ────────────────────────────────────────────────────────────
510+ if [ -f /sys/fs/cgroup/memory.current ]; then
511+ used=" $( cat /sys/fs/cgroup/memory.current 2> /dev/null) " || used=0
512+ local _max_raw
513+ _max_raw=" $( cat /sys/fs/cgroup/memory.max 2> /dev/null) " || _max_raw=" max"
514+ # "max" means unlimited; treat as 0 (caller overrides with SERVER_MEMORY)
515+ if [ " $_max_raw " = " max" ] || [ -z " $_max_raw " ]; then
516+ total=0
517+ else
518+ total=" $_max_raw "
519+ fi
520+ # Subtract inactive file cache — kernel considers it reclaimable
521+ if [ -f /sys/fs/cgroup/memory.stat ]; then
522+ local _inactive_file
523+ _inactive_file=" $( grep ' ^inactive_file ' /sys/fs/cgroup/memory.stat 2> /dev/null | awk ' {print $2}' ) " || _inactive_file=0
524+ used=$(( used - ${_inactive_file:- 0} ))
525+ [ " $used " -lt 0 ] && used=0
526+ fi
527+ echo " ${used:- 0} ${total:- 0} "
528+ return
529+ fi
530+
531+ # ── cgroup v1 ────────────────────────────────────────────────────────────
532+ if [ -f /sys/fs/cgroup/memory/memory.usage_in_bytes ]; then
533+ used=" $( cat /sys/fs/cgroup/memory/memory.usage_in_bytes 2> /dev/null) " || used=0
534+ total=" $( cat /sys/fs/cgroup/memory/memory.limit_in_bytes 2> /dev/null) " || total=0
535+ # Large sentinel value (≥ 1 TiB) means unlimited
536+ if [ " ${total:- 0} " -gt $(( 1024 * 1024 * 1024 * 1024 )) ] 2> /dev/null; then
537+ total=0
538+ fi
539+ # Subtract page cache (total_cache) — not actual RSS
540+ if [ -f /sys/fs/cgroup/memory/memory.stat ]; then
541+ local _cache
542+ _cache=" $( grep ' ^total_cache ' /sys/fs/cgroup/memory/memory.stat 2> /dev/null | awk ' {print $2}' ) " || _cache=0
543+ used=$(( used - ${_cache:- 0} ))
544+ [ " $used " -lt 0 ] && used=0
545+ fi
546+ echo " ${used:- 0} ${total:- 0} "
547+ return
548+ fi
549+
550+ # ── /proc/meminfo fallback (host-scoped; inaccurate inside containers) ───
551+ local available=0 free=0 buffers=0 cached=0 sreclaimable=0 key val
504552 while IFS=: read -r key val; do
505553 key=" ${key// / } " ; val=" ${val// [^0-9]/ } "
506554 case " $key " in
@@ -512,13 +560,13 @@ _metrics_read_memory() {
512560 SReclaimable) sreclaimable=" $val " ;;
513561 esac
514562 done < /proc/meminfo
515- local used
516563 if [ " ${available:- 0} " -gt 0 ] 2> /dev/null; then
517564 used=$(( total - available ))
518565 else
519566 used=$(( total - free - buffers - cached - sreclaimable ))
520567 fi
521568 [ " ${used:- 0} " -lt 0 ] && used=0
569+ # /proc/meminfo values are in kB; convert to bytes
522570 echo " $(( used * 1024 )) $(( total * 1024 )) "
523571}
524572
@@ -718,9 +766,9 @@ start_metrics_loop() {
718766 " $ingest_url " > /dev/null 2>&1 && _post_ok=1 || true
719767 fi
720768 if [ " $_post_ok " -eq 1 ]; then
721- log_info " [egg-metrics] cpu=${cpu} % mem=${mem_used_mb} MB net_rx=${net_rx} B/s players=${player_count} "
769+ # log_info "[egg-metrics] cpu=${cpu}% mem=${mem_used_mb}MB net_rx=${net_rx}B/s players=${player_count}"
722770 else
723- log_warn " [egg-metrics] failed to post metrics to ${ingest_url} "
771+ # log_warn "[egg-metrics] failed to post metrics to ${ingest_url}"
724772 fi
725773 done
726774 ) &
0 commit comments