mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-09-07 00:16:57 +08:00
refactor(runtime): lazily activate host modules (#6331)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env bash
|
||||
# 读取采样开始时已经存在的容器进程,避免把采样器自身计入结果。
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
self_pid="$$"
|
||||
process_dirs=(/proc/[0-9]*)
|
||||
|
||||
printf 'pid\tppid\tthreads\trss_kib\tpss_kib\tuss_kib\tcomm\texe\tcmdline\n'
|
||||
|
||||
for process_dir in "${process_dirs[@]}"; do
|
||||
pid="${process_dir##*/}"
|
||||
if [[ "${pid}" == "${self_pid}" ]] || [[ ! -r "${process_dir}/status" ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
status_values="$(awk '
|
||||
/^PPid:/ { ppid=$2 }
|
||||
/^Threads:/ { threads=$2 }
|
||||
END { printf "%d %d", ppid + 0, threads + 0 }
|
||||
' "${process_dir}/status" 2>/dev/null || true)"
|
||||
read -r ppid threads <<< "${status_values:-0 0}"
|
||||
|
||||
rss_kib=0
|
||||
pss_kib=0
|
||||
uss_kib=0
|
||||
if [[ -r "${process_dir}/smaps_rollup" ]]; then
|
||||
memory_values="$(awk '
|
||||
/^Rss:/ { rss=$2 }
|
||||
/^Pss:/ { pss=$2 }
|
||||
/^Private_Clean:/ { uss += $2 }
|
||||
/^Private_Dirty:/ { uss += $2 }
|
||||
/^Private_Hugetlb:/ { uss += $2 }
|
||||
END { printf "%d %d %d", rss + 0, pss + 0, uss + 0 }
|
||||
' "${process_dir}/smaps_rollup" 2>/dev/null || true)"
|
||||
read -r rss_kib pss_kib uss_kib <<< "${memory_values:-0 0 0}"
|
||||
fi
|
||||
|
||||
comm=""
|
||||
if [[ -r "${process_dir}/comm" ]]; then
|
||||
IFS= read -r comm < "${process_dir}/comm" || true
|
||||
fi
|
||||
executable="$(readlink "${process_dir}/exe" 2>/dev/null || true)"
|
||||
command_line="$(tr '\000\t' ' ' < "${process_dir}/cmdline" 2>/dev/null || true)"
|
||||
comm="${comm//$'\t'/ }"
|
||||
executable="${executable//$'\t'/ }"
|
||||
command_line="${command_line//$'\t'/ }"
|
||||
|
||||
printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
|
||||
"${pid}" "${ppid:-0}" "${threads:-0}" \
|
||||
"${rss_kib:-0}" "${pss_kib:-0}" "${uss_kib:-0}" \
|
||||
"${comm}" "${executable}" "${command_line}"
|
||||
done
|
||||
@@ -0,0 +1,35 @@
|
||||
"""MoviePilot Docker A/B 测量时使用的最小 ``sys.modules`` 快照探针。"""
|
||||
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
|
||||
|
||||
_OUTPUT_DIR = os.environ.get("MP_PERF_OUTPUT_DIR")
|
||||
_snapshot_index = 0
|
||||
|
||||
|
||||
def _dump_modules(_signum, _frame) -> None:
|
||||
"""收到 SIGUSR1 时原子写出当前解释器已经导入的模块名称。"""
|
||||
global _snapshot_index
|
||||
if not _OUTPUT_DIR:
|
||||
return
|
||||
|
||||
_snapshot_index += 1
|
||||
os.makedirs(_OUTPUT_DIR, exist_ok=True)
|
||||
final_path = os.path.join(
|
||||
_OUTPUT_DIR,
|
||||
f"modules-{os.getpid()}-{_snapshot_index}.txt",
|
||||
)
|
||||
temporary_path = f"{final_path}.tmp"
|
||||
with open(temporary_path, "w", encoding="utf-8") as output:
|
||||
for module_name in sorted(sys.modules):
|
||||
output.write(module_name)
|
||||
output.write("\n")
|
||||
output.flush()
|
||||
os.fsync(output.fileno())
|
||||
os.replace(temporary_path, final_path)
|
||||
|
||||
|
||||
if _OUTPUT_DIR and hasattr(signal, "SIGUSR1"):
|
||||
signal.signal(signal.SIGUSR1, _dump_modules)
|
||||
Reference in New Issue
Block a user