Windows-Standards bleiben; macOS nutzt memory_pressure x hw.memsize und os.nice(10). Rauchprobe: 2 Blaetter poolk, motor + motor~station, 24 Zeilen, mk vorhanden. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""Plattformweichen fuer die Messwerkzeuge (Windows ist der Standard, macOS ist die zweite Heimat).
|
|
|
|
Windows: BelowNormal ueber creationflags, Speicher ueber GlobalMemoryStatusEx, Baum beenden mit taskkill.
|
|
macOS/Linux: os.nice(10) im Kind, eigene Sitzung je Kind (damit der Baum beendet werden kann), Speicher ueber memory_pressure.
|
|
"""
|
|
import os
|
|
import re
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
|
|
WIN = sys.platform == "win32"
|
|
MAC = sys.platform == "darwin"
|
|
BELOW_NORMAL = 0x4000
|
|
|
|
|
|
def _kind_start():
|
|
os.setsid()
|
|
os.nice(10)
|
|
|
|
|
|
def niedrig_args():
|
|
"""Zusatzargumente fuer subprocess.Popen: Prioritaet unter Normal."""
|
|
return {"creationflags": BELOW_NORMAL} if WIN else {"preexec_fn": _kind_start}
|
|
|
|
|
|
def frei_gb():
|
|
"""Freier Arbeitsspeicher in GB."""
|
|
if WIN:
|
|
import ctypes
|
|
|
|
class M(ctypes.Structure):
|
|
_fields_ = [("l", ctypes.c_ulong), ("p", ctypes.c_ulong), ("tot", ctypes.c_ulonglong), ("av", ctypes.c_ulonglong)] + [("x%d" % i, ctypes.c_ulonglong) for i in range(5)]
|
|
m = M()
|
|
m.l = ctypes.sizeof(M)
|
|
ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(m))
|
|
return m.av / 2 ** 30
|
|
if MAC:
|
|
gesamt = int(subprocess.run(["sysctl", "-n", "hw.memsize"], capture_output=True, text=True, check=True).stdout.strip())
|
|
text = subprocess.run(["memory_pressure"], capture_output=True, text=True, check=True).stdout
|
|
z = re.search(r"System-wide memory free percentage:\s*([\d.]+)%", text)
|
|
if not z:
|
|
raise RuntimeError("memory_pressure: keine Zeile 'System-wide memory free percentage'")
|
|
return gesamt * float(z.group(1)) / 100 / 2 ** 30
|
|
seiten = os.sysconf("SC_AVPHYS_PAGES") * os.sysconf("SC_PAGE_SIZE")
|
|
return seiten / 2 ** 30
|
|
|
|
|
|
def beende_baum(p):
|
|
"""Beendet den Prozess samt Kindern."""
|
|
if WIN:
|
|
subprocess.run(["taskkill", "/F", "/T", "/PID", str(p.pid)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
return
|
|
try:
|
|
os.killpg(p.pid, signal.SIGKILL) # Kind ist Leiter der eigenen Sitzung (niedrig_args)
|
|
except (ProcessLookupError, PermissionError):
|
|
try:
|
|
p.kill()
|
|
except OSError:
|
|
pass
|