430 lines
23 KiB
PowerShell
430 lines
23 KiB
PowerShell
# Warteschlange der Engine-Messlaeufe (alle Tracks), siehe CLAUDE.md "Messlaeufe".
|
|
#
|
|
# Liest bei jedem Durchgang messung_engine/warteschlange.txt (Zeilen: id|exe|argumente|env|arbeitsordner,
|
|
# env als K=V;K=V, leere Felder erlaubt, '#' = Kommentar) und startet Laeufe parallel, solange das
|
|
# THREAD-BUDGET reicht. Budget in GRENZE.txt (jederzeit aenderbar, wirkt beim naechsten Start):
|
|
# threads=16 Summe der Mess-Threads aller laufenden Laeufe (2, wenn der Client spielt)
|
|
# je_lauf=8 Threads pro Lauf (POKERBENCH_THREADS)
|
|
# Prioritaet Idle. Fertige ids stehen in erledigt.txt, Ausgabe in logs/<id>.log.
|
|
# Eigene PID steht in STARTER.pid. ANHALTEN: zuerst diesen Starter beenden, dann die Laeufe
|
|
# (Get-Process pokerbench-*). Datei STOP im Ordner = keine neuen Laeufe mehr starten, sauber auslaufen.
|
|
# FRUEH-STOPP (Plan M2): steht im env-Feld FRUEHSTOPP=<basisordner>, prueft der Starter den Lauf alle
|
|
# 5 Minuten mit fruehstopp.py gegen die Basis; ist ein Gegner sicher Verlierer (zeitgleichmaessiges
|
|
# Intervall ganz unter 0), wird der Lauf beendet ("<id> exit=fruehstopp-VERLIERER" in erledigt.txt).
|
|
# Ebenso GEWINNER (16.11., Exit 4 von fruehstopp.py: Alpha-Spending an festen Zellblicken, aufs Tor von
|
|
# ab.py abgestimmt; LBR: NICHT-SCHLECHTER): "<id> exit=fruehstopp-GEWINNER", Bericht in logs/<id>.fruehstopp.
|
|
# Die fertigen Zellen bleiben liegen; ab.py auf dem Teil-Ordner ist dann das Urteil. FRUEHSTOPP_GEWINNER=0
|
|
# (im env-Feld des Laufs oder fuer den Starter) schaltet nur den Gewinner-Stopp ab.
|
|
# Ein Neustart des Starters uebernimmt bereits laufende Laeufe (an ihrem Ausgabeordner erkannt), samt
|
|
# ihrem FRUEHSTOPP.
|
|
# WACHE (16.11.): alle 10 Minuten die Top-CPU-Prozesse (Name, Prioritaet, CPU-Anteil am ganzen Rechner im
|
|
# Fenster) nach cpu.log. Ist 60 Minuten lang bei keinem laufenden Lauf etwas fertig geworden (keine
|
|
# Ausgabe-Datei, kein Log gewachsen, kein Lauf beendet), steht "WARNUNG ..." in starter.log und cpu.log
|
|
# (stuendlich wiederholt, solange es anhaelt).
|
|
# SPIELBETRIEB (16.11.): alle 10 s wird geprueft, ob gespielt wird: der echte Client (CoinPoker.exe) laeuft
|
|
# UND entweder die Laeufer-Sperre scratch\laeufer.pid (betrieb/sperre.py) im Live-Baum existiert und ihre PID
|
|
# lebt, oder ein python-Prozess mit runner.py --act laeuft, dessen Kommandozeile im Live-Baum liegt
|
|
# (Kettenspecs anderer Worktrees gegen den Klon zaehlen nicht: Fehlalarm 16.11. 13:46). Dann gilt 2/2 (threads=2, je_lauf=2), egal was in GRENZE.txt steht: Laeufe ueber dem Budget werden
|
|
# angehalten (NtSuspendProcess, nicht beendet), neue starten mit 2 Threads; nach Spielende laufen sie weiter
|
|
# (NtResumeProcess) und GRENZE.txt gilt wieder. Wechsel stehen in starter.log, der wirksame Zustand in
|
|
# BUDGET_JETZT.txt (fuer Laeufe ausserhalb der Schlange: dort nachsehen). Beim Start und bei jedem Blick
|
|
# ohne Spiel wird JEDER pokerbench-*-Prozess so oft fortgesetzt, bis kein Thread mehr angehalten ist (auch
|
|
# nicht uebernommene, auch mehrfach angehaltene); bei Spiel haelt das Spielbudget sie neu an.
|
|
# Test ohne echten Laeufer: POKERBENCH_SPIELSPERRE=<Pfad einer Test-Sperre> (Datei mit einer PID),
|
|
# POKERBENCH_SPIEL_ACT=0 schaltet die Suche nach runner.py --act ab, POKERBENCH_LIVEBAUM=<Pfad> setzt den
|
|
# Live-Baum, POKERBENCH_SPIEL_CLIENT=<Prozessname ohne .exe> den Client (Standard CoinPoker).
|
|
$ErrorActionPreference = 'Continue'
|
|
$D = Split-Path -Parent $MyInvocation.MyCommand.Path
|
|
(Get-Process -Id $PID).PriorityClass = 'Idle'
|
|
$PID | Set-Content "$D\STARTER.pid"
|
|
New-Item -ItemType Directory -Force "$D\logs" | Out-Null
|
|
if (-not (Test-Path "$D\erledigt.txt")) { New-Item -ItemType File "$D\erledigt.txt" | Out-Null }
|
|
|
|
function GrenzeDatei {
|
|
$g = @{ threads = 16; je_lauf = 8 }
|
|
if (Test-Path "$D\GRENZE.txt") {
|
|
Get-Content "$D\GRENZE.txt" | ForEach-Object {
|
|
if ($_ -match '^\s*(\w+)\s*=\s*(\d+)') { $g[$Matches[1]] = [int]$Matches[2] }
|
|
}
|
|
}
|
|
$g
|
|
}
|
|
|
|
# Wirksames Budget: bei Spielbetrieb 2/2, sonst GRENZE.txt.
|
|
function Grenze {
|
|
if ($script:spiel) { return @{ threads = 2; je_lauf = 2 } }
|
|
GrenzeDatei
|
|
}
|
|
|
|
if (-not ('PbProz' -as [type])) {
|
|
Add-Type -TypeDefinition @'
|
|
using System;
|
|
using System.Runtime.InteropServices;
|
|
public static class PbProz {
|
|
[DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr OpenProcess(uint zugriff, bool erben, int pid);
|
|
[DllImport("kernel32.dll")] static extern bool CloseHandle(IntPtr h);
|
|
[DllImport("kernel32.dll")] static extern bool GetExitCodeProcess(IntPtr h, out uint code);
|
|
[DllImport("ntdll.dll")] static extern int NtSuspendProcess(IntPtr h);
|
|
[DllImport("ntdll.dll")] static extern int NtResumeProcess(IntPtr h);
|
|
// wie betrieb/sperre._lebt_windows: Zugriff verweigert = es gibt ihn
|
|
public static bool Lebt(int pid) {
|
|
if (pid <= 0) return false;
|
|
IntPtr h = OpenProcess(0x1000, false, pid);
|
|
if (h == IntPtr.Zero) return Marshal.GetLastWin32Error() == 5;
|
|
try { uint c; return GetExitCodeProcess(h, out c) && c == 259; } finally { CloseHandle(h); }
|
|
}
|
|
static int Mach(int pid, bool anhalten) {
|
|
IntPtr h = OpenProcess(0x0800, false, pid); // PROCESS_SUSPEND_RESUME
|
|
if (h == IntPtr.Zero) return -1;
|
|
try { return anhalten ? NtSuspendProcess(h) : NtResumeProcess(h); } finally { CloseHandle(h); }
|
|
}
|
|
public static int Anhalten(int pid) { return Mach(pid, true); }
|
|
public static int Fortsetzen(int pid) { return Mach(pid, false); }
|
|
}
|
|
'@
|
|
}
|
|
|
|
$LIVEBAUM = if ($env:POKERBENCH_LIVEBAUM) { $env:POKERBENCH_LIVEBAUM } else {
|
|
'D:\Projects\pokerfreak\.claude\worktrees\engine-lecks-angreifen-de7b88' }
|
|
$SPIELSPERRE = if ($env:POKERBENCH_SPIELSPERRE) { $env:POKERBENCH_SPIELSPERRE } else { Join-Path $LIVEBAUM 'scratch\laeufer.pid' }
|
|
$SPIELCLIENT = if ($env:POKERBENCH_SPIEL_CLIENT) { $env:POKERBENCH_SPIEL_CLIENT } else { 'CoinPoker' }
|
|
$spiel = $null; $budget_zeile = $null
|
|
|
|
# Grund, warum gespielt wird ("sperre pid=..." / "runner --act pid=..."), sonst $null.
|
|
function Spiel {
|
|
# ohne echten Client spielt niemand
|
|
if (-not (Get-Process -Name $SPIELCLIENT -ErrorAction SilentlyContinue)) { return $null }
|
|
if (Test-Path -LiteralPath $SPIELSPERRE) {
|
|
$n = 0
|
|
$txt = Get-Content -LiteralPath $SPIELSPERRE -Raw -ErrorAction SilentlyContinue
|
|
if ($txt -and [int]::TryParse($txt.Trim(), [ref]$n) -and [PbProz]::Lebt($n)) { return "sperre pid=$n" }
|
|
}
|
|
if ($env:POKERBENCH_SPIEL_ACT -ne '0') {
|
|
$r = Get-CimInstance Win32_Process -Filter "Name = 'python.exe' OR Name = 'pythonw.exe' OR Name = 'python3.exe'" -ErrorAction SilentlyContinue |
|
|
Where-Object { $_.CommandLine -and $_.CommandLine -match 'runner\.py' -and $_.CommandLine -match '--act\b' -and
|
|
$_.CommandLine.IndexOf($LIVEBAUM, [StringComparison]::OrdinalIgnoreCase) -ge 0 } |
|
|
Select-Object -First 1
|
|
if ($r) { return "runner --act pid=$($r.ProcessId)" }
|
|
}
|
|
$null
|
|
}
|
|
|
|
# Bei Spiel: Laeufe ueber 2 Threads anhalten (laufende behalten Vorrang, dann die kleinsten), passende
|
|
# angehaltene fortsetzen. Nach Spielende alle fortsetzen.
|
|
# Jeden pokerbench-*-Prozess mit angehaltenen Threads so oft fortsetzen, bis keiner mehr angehalten ist,
|
|
# ob uebernommen oder nicht (16.11.: ein beendeter Starter liess 48572/62864 angehalten zurueck, 62864
|
|
# mehrfach). Laeuft beim Start und bei jedem Blick ohne Spiel.
|
|
function AlleFortsetzen($anlass) {
|
|
foreach ($p in @(Get-Process -Name 'pokerbench-*' -ErrorAction SilentlyContinue)) {
|
|
$n = 0
|
|
try {
|
|
while ($n -lt 64) {
|
|
$p.Refresh()
|
|
$halt = @($p.Threads | Where-Object { $_.ThreadState -eq 'Wait' -and $_.WaitReason -eq 'Suspended' }).Count
|
|
if ($halt -eq 0) { break }
|
|
$null = [PbProz]::Fortsetzen($p.Id); $n++
|
|
}
|
|
} catch {}
|
|
if ($n -gt 0) {
|
|
"$($p.ProcessName) pid=$($p.Id) fortgesetzt ${n}x ($anlass) $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
}
|
|
}
|
|
|
|
function Spielbudget {
|
|
$grund = Spiel
|
|
if ($grund -and -not $script:spiel) {
|
|
"spiel an ($grund): Budget 2/2 $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
if (-not $grund -and $script:spiel) {
|
|
foreach ($id in @($laufend.Keys)) {
|
|
$l = $laufend[$id]
|
|
if ($l.angehalten) {
|
|
$r = [PbProz]::Fortsetzen($l.p.Id); $l.angehalten = $false
|
|
"$id fortgesetzt pid=$($l.p.Id) status=$r $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
}
|
|
"spiel aus: Budget aus GRENZE.txt $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
if (-not $grund) { AlleFortsetzen 'kein spiel' }
|
|
$script:spiel = $grund
|
|
if ($grund) {
|
|
$platz = 2
|
|
$reihe = @($laufend.Keys | Sort-Object { [int][bool]$laufend[$_].angehalten }, { $laufend[$_].t })
|
|
foreach ($id in $reihe) {
|
|
$l = $laufend[$id]
|
|
if ($l.p.HasExited) { continue }
|
|
if ($l.t -le $platz) {
|
|
$platz -= $l.t
|
|
if ($l.angehalten) {
|
|
$r = [PbProz]::Fortsetzen($l.p.Id); $l.angehalten = $false
|
|
"$id fortgesetzt (passt ins Spielbudget) pid=$($l.p.Id) status=$r $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
} elseif (-not $l.angehalten) {
|
|
$r = [PbProz]::Anhalten($l.p.Id); $l.angehalten = $true
|
|
"$id angehalten (spiel, $($l.t) Threads) pid=$($l.p.Id) status=$r $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
}
|
|
}
|
|
$g = Grenze
|
|
$halt = @($laufend.Keys | Where-Object { $laufend[$_].angehalten }) -join ','
|
|
$zeile = "spiel=$(if ($grund) { 1 } else { 0 }) threads=$($g.threads) je_lauf=$($g.je_lauf)" +
|
|
$(if ($grund) { " grund=$($grund -replace ' ', '_')" } else { '' }) + $(if ($halt) { " angehalten=$halt" } else { '' })
|
|
if ($zeile -ne $script:budget_zeile) {
|
|
$script:budget_zeile = $zeile
|
|
"$zeile seit=$(Get-Date -Format s)" | Set-Content "$D\BUDGET_JETZT.txt" -Encoding ascii
|
|
}
|
|
}
|
|
|
|
function Jobs {
|
|
if (-not (Test-Path "$D\warteschlange.txt")) { return @() }
|
|
@(Get-Content "$D\warteschlange.txt" | Where-Object { $_ -and -not $_.StartsWith('#') })
|
|
}
|
|
|
|
# Ausgabe eines Laufs: der Wert nach --out, sonst das erste Argument (Ordner/Datei bei ab_zelle, LBR).
|
|
function Ausgabe($args_) {
|
|
$teile = @($args_ -split ' ' | Where-Object { $_ })
|
|
for ($i = 0; $i -lt $teile.Count - 1; $i++) { if ($teile[$i] -eq '--out') { return $teile[$i + 1] } }
|
|
if ($teile.Count) { $teile[0] } else { $null }
|
|
}
|
|
|
|
# Wert eines Schluessels aus dem env-Feld einer Zeile (K=V;K=V), sonst $null.
|
|
function EnvWert($env_, $schluessel) {
|
|
foreach ($kv in ($env_ -split ';' | Where-Object { $_ })) {
|
|
$k, $v = $kv -split '=', 2
|
|
if ($k -eq $schluessel) { return $v }
|
|
}
|
|
$null
|
|
}
|
|
|
|
function FsPfad($env_) {
|
|
$v = EnvWert $env_ 'FRUEHSTOPP'
|
|
if (-not $v) { return $null }
|
|
if ([IO.Path]::IsPathRooted($v)) { $v } else { Join-Path $D $v }
|
|
}
|
|
|
|
# WACHE: CPU-Zeit je Prozess beim letzten Blick (pid -> Sekunden) und Zeitpunkt; letzter Fortschritt.
|
|
$cpu_alt = @{}; $cpu_zeit = Get-Date
|
|
$kerne = [Environment]::ProcessorCount
|
|
$letzter_fortschritt = Get-Date; $letzte_warnung = [datetime]::MinValue; $letzte_wache = Get-Date
|
|
|
|
function CpuStand {
|
|
$s = @{}
|
|
foreach ($p in Get-Process) {
|
|
try { $s[$p.Id] = @{ c = $p.TotalProcessorTime.TotalSeconds; n = $p.ProcessName; p = $p } } catch {}
|
|
}
|
|
$s
|
|
}
|
|
|
|
function CpuLog {
|
|
# Achtung: PowerShell-Namen ohne Gross/Klein, ein lokales $d wuerde $D (Messordner) verdecken.
|
|
$jetzt = Get-Date
|
|
$neu = CpuStand
|
|
$fenster = ($jetzt - $script:cpu_zeit).TotalSeconds
|
|
if ($fenster -lt 1) { return }
|
|
$zeilen = New-Object System.Collections.Generic.List[object]
|
|
$summe = 0.0
|
|
foreach ($id in @($neu.Keys)) {
|
|
$alt = $script:cpu_alt[$id]
|
|
$delta = $neu[$id].c
|
|
if ($null -ne $alt) { $delta = $delta - $alt }
|
|
if ($delta -gt 0) {
|
|
$summe += $delta
|
|
$zeilen.Add([pscustomobject]@{ id = $id; n = $neu[$id].n; p = $neu[$id].p; d = $delta })
|
|
}
|
|
}
|
|
$nenner = $fenster * $kerne
|
|
$text = New-Object System.Collections.Generic.List[string]
|
|
$text.Add(('{0} CPU gesamt {1:N1} % ({2} Kerne, Fenster {3:N0} s), Top:' -f $jetzt.ToString('s'), (100 * $summe / $nenner), $kerne, $fenster))
|
|
foreach ($z in @($zeilen | Sort-Object -Property d -Descending | Select-Object -First 12)) {
|
|
$prio = '?'
|
|
try { if ($z.p.PriorityClass) { $prio = [string]$z.p.PriorityClass } } catch {}
|
|
$text.Add((' {0,5:N1} % {1,-12} {2} pid={3}' -f (100 * $z.d / $nenner), $prio, $z.n, $z.id))
|
|
}
|
|
[IO.File]::AppendAllText("$D\cpu.log", ($text -join "`r`n") + "`r`n")
|
|
$script:cpu_alt = @{}
|
|
foreach ($id in @($neu.Keys)) { $script:cpu_alt[$id] = $neu[$id].c }
|
|
$script:cpu_zeit = $jetzt
|
|
}
|
|
|
|
# Juengste Aenderung an den Ausgaben eines laufenden Laufs (Ausgabe-Datei oder -Ordner, logs/<id>.*).
|
|
function Juengste($id, $l) {
|
|
$pfade = @("$D\logs\$id.log", "$D\logs\$id.err")
|
|
if ($l.out) { $pfade += $(if ([IO.Path]::IsPathRooted($l.out)) { $l.out } else { Join-Path $l.wd $l.out }) }
|
|
$t = [datetime]::MinValue
|
|
foreach ($x in $pfade) {
|
|
if (Test-Path -LiteralPath $x -PathType Container) {
|
|
$f = Get-ChildItem -LiteralPath $x -File -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
|
|
if ($f -and $f.LastWriteTime -gt $t) { $t = $f.LastWriteTime }
|
|
} elseif (Test-Path -LiteralPath $x) {
|
|
$w = (Get-Item -LiteralPath $x).LastWriteTime
|
|
if ($w -gt $t) { $t = $w }
|
|
}
|
|
}
|
|
$t
|
|
}
|
|
|
|
function Wache {
|
|
CpuLog
|
|
$aktiv = @($laufend.Keys | Where-Object { -not $laufend[$_].angehalten })
|
|
if ($aktiv.Count -eq 0) { $script:letzter_fortschritt = Get-Date } # nichts laeuft oder alles angehalten
|
|
foreach ($id in $aktiv) {
|
|
$t = Juengste $id $laufend[$id]
|
|
if ($t -gt $script:letzter_fortschritt) { $script:letzter_fortschritt = $t }
|
|
}
|
|
$still = ((Get-Date) - $script:letzter_fortschritt).TotalMinutes
|
|
if ($aktiv.Count -gt 0 -and $still -ge 60 -and ((Get-Date) - $script:letzte_warnung).TotalMinutes -ge 60) {
|
|
$script:letzte_warnung = Get-Date
|
|
$w = "WARNUNG $(Get-Date -Format s): seit $([int]$still) min nichts fertig geworden " +
|
|
"(letzter Fortschritt $($script:letzter_fortschritt.ToString('s')), laufend: $($aktiv -join ', '))"
|
|
$w | Add-Content "$D\starter.log"
|
|
$w | Add-Content "$D\cpu.log"
|
|
}
|
|
}
|
|
|
|
AlleFortsetzen 'starterstart'
|
|
$laufend = @{} # id -> @{ p = Process; t = threads; fs = Basis fuer den Fruehstopp; out; wd; gw }
|
|
# Bereits laufende Laeufe uebernehmen. Sie zaehlen mit je_lauf Threads (so hat der Starter sie
|
|
# gestartet); frueher zaehlte jeder mit dem vollen Budget, dann blockierte ein langer Lauf alles.
|
|
# Abgleich (16.11. abends): gleiche exe (voller Pfad) UND die Kommandozeile endet genau auf die Argumente
|
|
# der Zeile; ids aus erledigt.txt nie; je PID hoechstens eine id. Vorher passte "multi-deep --mikro" (kein
|
|
# Ausgabeordner vorn) auf jeden chartgen-Prozess, dieselbe PID wurde dreimal uebernommen, auch unter
|
|
# laengst erledigten ids.
|
|
$prozesse = @(Get-CimInstance Win32_Process -Filter "Name LIKE 'pokerbench-%'")
|
|
$schon_erledigt = @{}
|
|
if (Test-Path "$D\erledigt.txt") { Get-Content "$D\erledigt.txt" | ForEach-Object { $schon_erledigt[($_ -split ' ')[0]] = 1 } }
|
|
$vergeben = @{} # pid -> id
|
|
foreach ($j in Jobs) {
|
|
$f = $j -split '\|'
|
|
if ($f.Count -lt 3 -or $schon_erledigt.ContainsKey($f[0]) -or $laufend.ContainsKey($f[0])) { continue }
|
|
$out = Ausgabe $f[2]
|
|
$argumente = $f[2].Trim()
|
|
$treffer = $prozesse | Where-Object {
|
|
-not $vergeben.ContainsKey([int]$_.ProcessId) -and $_.CommandLine -and
|
|
$_.ExecutablePath -and ($_.ExecutablePath -ieq $f[1]) -and
|
|
($argumente -eq '' -or $_.CommandLine.TrimEnd().EndsWith(' ' + $argumente))
|
|
} | Select-Object -First 1
|
|
if ($treffer) {
|
|
$vergeben[[int]$treffer.ProcessId] = $f[0]
|
|
$wd = if ($f.Count -gt 4 -and $f[4]) { $f[4] } else { $D }
|
|
$laufend[$f[0]] = @{ p = (Get-Process -Id $treffer.ProcessId); t = (GrenzeDatei).je_lauf; exe = $f[1]; fs = (FsPfad $f[3]); out = $out
|
|
wd = $wd; gw = (EnvWert $f[3] 'FRUEHSTOPP_GEWINNER'); angehalten = $false }
|
|
# ein frueherer Starter kann ihn bei Spiel angehalten haben: fortsetzen, Spielbudget haelt ggf. neu an
|
|
$null = [PbProz]::Fortsetzen($treffer.ProcessId)
|
|
"$($f[0]) uebernommen pid=$($treffer.ProcessId) $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
}
|
|
|
|
# README-HAKEN: ist ein fertiger Job ein Referenz-Arm (sein Ausgabepfad steht in REFERENZ.txt), schreibt
|
|
# werkzeuge/readme_staerken.py die Zahlen-Bloecke der README neu. Abgekoppelt, Idle, Zeitlimit im Skript;
|
|
# ob der Job zaehlt, entscheidet das Skript. Fehler nur ins Log, nie in die Schlange. README_NACH_LAUF=0 = aus.
|
|
function Readme-Haken($id) {
|
|
if ($env:README_NACH_LAUF -eq '0') { return }
|
|
try {
|
|
$baum = Split-Path -Parent $D
|
|
$rs = Join-Path $baum 'werkzeuge\readme_staerken.py'
|
|
if (-not (Test-Path $rs)) { return }
|
|
$q = Start-Process -FilePath 'python' -ArgumentList "`"$rs`" --nach-job $id --messordner `"$D`"" `
|
|
-WorkingDirectory $baum -WindowStyle Hidden -PassThru
|
|
try { $q.PriorityClass = 'Idle' } catch {}
|
|
} catch {
|
|
"$id readme-haken fehler $_ $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
}
|
|
}
|
|
|
|
# FAIRE VERTEILUNG (17.11.): frueher startete immer $offen[0]; wer einen grossen Stapel einreihte, belegte
|
|
# den ganzen Rechner, alle anderen Sessions warteten (Owner: "immer nur eine macht Fortschritte").
|
|
# Jetzt bekommt der Track mit den wenigsten laufenden Threads den naechsten Platz, innerhalb eines Tracks
|
|
# bleibt die Reihenfolge der Schlange. Track = Worktree der exe (...\worktrees\<name>\...), sonst ihr Ordner.
|
|
# Ist nur ein Track offen, bekommt er alles (nichts bleibt ungenutzt).
|
|
function Track($exe) {
|
|
if ($exe -match '(?i)\\worktrees\\([^\\]+)\\') { return $Matches[1].ToLower() }
|
|
try { return (Split-Path -Parent $exe).ToLower() } catch { return '' }
|
|
}
|
|
function NaechsterJob($offen_, $laufend_) {
|
|
$last = @{}
|
|
foreach ($l in $laufend_.Values) { if (-not $l.angehalten) { $k = $l.k; if (-not $last.ContainsKey($k)) { $last[$k] = 0 }; $last[$k] += $l.t } }
|
|
$best = $null; $bestlast = [int]::MaxValue
|
|
foreach ($z in $offen_) {
|
|
$k = Track (($z -split '\|')[1])
|
|
$b = if ($last.ContainsKey($k)) { $last[$k] } else { 0 }
|
|
if ($b -lt $bestlast) { $best = $z; $bestlast = $b }
|
|
}
|
|
return $best
|
|
}
|
|
foreach ($id in @($laufend.Keys)) { $laufend[$id].k = Track $laufend[$id].exe } # uebernommene Laeufe
|
|
|
|
$s0 = CpuStand; foreach ($i in $s0.Keys) { $cpu_alt[$i] = $s0[$i].c }; $cpu_zeit = Get-Date
|
|
$letzte_pruefung = Get-Date
|
|
while ($true) {
|
|
try { Spielbudget } catch { "spielbudget fehler $_ $(Get-Date -Format s)" | Add-Content "$D\starter.log" }
|
|
if (((Get-Date) - $letzte_wache).TotalMinutes -ge 10) {
|
|
$letzte_wache = Get-Date
|
|
try { Wache } catch { "wache fehler $_ $(Get-Date -Format s)" | Add-Content "$D\starter.log" }
|
|
}
|
|
if (((Get-Date) - $letzte_pruefung).TotalMinutes -ge 5) {
|
|
$letzte_pruefung = Get-Date
|
|
foreach ($id in @($laufend.Keys)) {
|
|
$l = $laufend[$id]
|
|
if (-not $l.fs -or $l.p.HasExited) { continue }
|
|
$gw_vorher = $env:FRUEHSTOPP_GEWINNER
|
|
if ($l.gw) { $env:FRUEHSTOPP_GEWINNER = $l.gw }
|
|
$bericht = & python "$D\fruehstopp.py" $l.fs $l.out 2>&1
|
|
$code = $LASTEXITCODE
|
|
if ($null -eq $gw_vorher) { Remove-Item Env:FRUEHSTOPP_GEWINNER -ErrorAction SilentlyContinue } else { $env:FRUEHSTOPP_GEWINNER = $gw_vorher }
|
|
$urteil = if ($code -eq 3) { 'VERLIERER' } elseif ($code -eq 4) { 'GEWINNER' } else { $null }
|
|
if ($urteil) {
|
|
try { Stop-Process -Id $l.p.Id -Force } catch {}
|
|
$bericht | Set-Content "$D\logs\$id.fruehstopp"
|
|
"$id exit=fruehstopp-$urteil $(Get-Date -Format s)" | Add-Content "$D\erledigt.txt"
|
|
"$id fruehstopp $urteil $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
$laufend.Remove($id)
|
|
$letzter_fortschritt = Get-Date
|
|
}
|
|
}
|
|
}
|
|
foreach ($id in @($laufend.Keys)) {
|
|
$p = $laufend[$id].p
|
|
if ($p.HasExited) {
|
|
"$id exit=$($p.ExitCode) $(Get-Date -Format s)" | Add-Content "$D\erledigt.txt"
|
|
Readme-Haken $id
|
|
$laufend.Remove($id)
|
|
$letzter_fortschritt = Get-Date
|
|
}
|
|
}
|
|
$fertig = @{}
|
|
Get-Content "$D\erledigt.txt" | ForEach-Object { $fertig[($_ -split ' ')[0]] = 1 }
|
|
$offen = @(Jobs | Where-Object { $i = ($_ -split '\|')[0]; -not $fertig.ContainsKey($i) -and -not $laufend.ContainsKey($i) })
|
|
$g = Grenze
|
|
$belegt = 0; foreach ($l in $laufend.Values) { if (-not $l.angehalten) { $belegt += $l.t } }
|
|
$frei = $g.threads - $belegt
|
|
if ((Test-Path "$D\STOP") -or $offen.Count -eq 0) {
|
|
if ($laufend.Count -eq 0) { break }
|
|
} elseif ($frei -ge 1 -and ($frei -ge $g.je_lauf -or $laufend.Count -eq 0)) {
|
|
$t = [Math]::Max(1, [Math]::Min($g.je_lauf, $frei))
|
|
$f = (NaechsterJob $offen $laufend) -split '\|'
|
|
$id = $f[0]; $exe = $f[1]; $args_ = $f[2]; $env_ = $f[3]; $wd = $f[4]
|
|
if (-not $wd) { $wd = $D }
|
|
$env:POKERBENCH_THREADS = "$t"
|
|
foreach ($kv in ($env_ -split ';' | Where-Object { $_ })) {
|
|
$k, $v = $kv -split '=', 2; Set-Item "Env:$k" $v
|
|
}
|
|
$p = Start-Process -FilePath $exe -ArgumentList $args_ -WorkingDirectory $wd -NoNewWindow -PassThru `
|
|
-RedirectStandardOutput "$D\logs\$id.log" -RedirectStandardError "$D\logs\$id.err"
|
|
$null = $p.Handle # Handle halten, sonst ist ExitCode spaeter leer ("exit=" in erledigt.txt)
|
|
# Owner 18.11.: gebremst wird nur im Spielbetrieb (dann haelt der Starter die Laeufe ohnehin an bzw. 2/2);
|
|
# sonst BelowNormal. Idle gab je Prozess nur ~0,4 Kerne bei 60 % Maschinenlast.
|
|
try { $p.PriorityClass = 'BelowNormal' } catch {}
|
|
foreach ($kv in ($env_ -split ';' | Where-Object { $_ })) { Remove-Item "Env:$(($kv -split '=')[0])" -ErrorAction SilentlyContinue }
|
|
Remove-Item Env:POKERBENCH_THREADS -ErrorAction SilentlyContinue
|
|
$laufend[$id] = @{ p = $p; t = $t; k = (Track $exe); fs = (FsPfad $env_); out = (Ausgabe $args_); wd = $wd
|
|
gw = (EnvWert $env_ 'FRUEHSTOPP_GEWINNER'); angehalten = $false }
|
|
$letzter_fortschritt = Get-Date
|
|
"$id gestartet pid=$($p.Id) threads=$t $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|
|
continue
|
|
}
|
|
Start-Sleep -Seconds 10
|
|
}
|
|
Remove-Item "$D\STARTER.pid" -ErrorAction SilentlyContinue
|
|
"leer $(Get-Date -Format s)" | Add-Content "$D\starter.log"
|