This commit is contained in:
devdatt 2026-01-19 17:17:20 +05:30
parent 232a325ada
commit 262dbf710d
1 changed files with 36 additions and 29 deletions

View File

@ -27,20 +27,18 @@ function saveCoreState(array $state): void
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
NUMA + SMT TOPOLOGY (SOURCE OF TRUTH) NUMA TOPOLOGY
--------------------------------------------------------- */ --------------------------------------------------------- */
function getNumaTopology(): array function getNumaTopology(): array
{ {
$nodes = []; $nodes = [];
/* discover NUMA nodes */
foreach (glob('/sys/devices/system/node/node*') as $nodePath) { foreach (glob('/sys/devices/system/node/node*') as $nodePath) {
$node = (int)str_replace('node', '', basename($nodePath)); $node = (int)str_replace('node', '', basename($nodePath));
$nodes[$node] = []; $nodes[$node] = [];
} }
/* map cpu -> node -> core_id */
foreach (glob('/sys/devices/system/cpu/cpu[0-9]*') as $cpuPath) { foreach (glob('/sys/devices/system/cpu/cpu[0-9]*') as $cpuPath) {
$cpu = (int)str_replace('cpu', '', basename($cpuPath)); $cpu = (int)str_replace('cpu', '', basename($cpuPath));
$topo = "$cpuPath/topology"; $topo = "$cpuPath/topology";
@ -64,12 +62,11 @@ function getNumaTopology(): array
$nodes[$node][$coreId][] = $cpu; $nodes[$node][$coreId][] = $cpu;
} }
/* normalize ordering */
ksort($nodes); ksort($nodes);
foreach ($nodes as &$cores) { foreach ($nodes as &$cores) {
ksort($cores); ksort($cores);
foreach ($cores as &$threads) { foreach ($cores as &$threads) {
sort($threads); // primary thread first sort($threads);
} }
} }
@ -77,37 +74,50 @@ function getNumaTopology(): array
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
BUILD GLOBAL ROUND-ROBIN PLAN NUMA-AWARE ROUND-ROBIN PLAN
EVEN CPUs FIRST ODD CPUs
--------------------------------------------------------- */ --------------------------------------------------------- */
function buildAllocationPlan(array $nodes): array function buildAllocationPlan(array $nodes): array
{ {
$even = []; $perNode = [];
$odd = [];
/* build per-node even → odd cpu lists */
foreach ($nodes as $node => $cores) { foreach ($nodes as $node => $cores) {
foreach ($cores as $coreId => $threads) { $even = [];
foreach ($threads as $cpu) { $odd = [];
$entry = [
"node" => $node,
"cpu" => $cpu
];
foreach ($cores as $threads) {
foreach ($threads as $cpu) {
if (($cpu % 2) === 0) { if (($cpu % 2) === 0) {
$even[] = $entry; $even[] = $cpu;
} else { } else {
$odd[] = $entry; $odd[] = $cpu;
} }
} }
} }
sort($even);
sort($odd);
$perNode[$node] = array_merge($even, $odd);
}
/* interleave nodes (true NUMA rotation) */
$plan = [];
$max = max(array_map('count', $perNode));
for ($i = 0; $i < $max; $i++) {
foreach ($perNode as $node => $cpus) {
if (isset($cpus[$i])) {
$plan[] = [
"node" => $node,
"cpu" => $cpus[$i]
];
}
}
} }
/* deterministic ordering */ return $plan;
usort($even, fn($a, $b) => $a["cpu"] <=> $b["cpu"]);
usort($odd, fn($a, $b) => $a["cpu"] <=> $b["cpu"]);
return array_merge($even, $odd);
} }
/* --------------------------------------------------------- /* ---------------------------------------------------------
@ -141,11 +151,8 @@ function allocateCore(int $serviceId): array
function freeCore(int $serviceId): void function freeCore(int $serviceId): void
{ {
$state = loadCoreState(); $state = loadCoreState();
unset($state["allocations"][$serviceId]);
if (isset($state["allocations"][$serviceId])) { saveCoreState($state);
unset($state["allocations"][$serviceId]);
saveCoreState($state);
}
} }
function getServiceCore(int $serviceId): ?array function getServiceCore(int $serviceId): ?array
@ -270,9 +277,9 @@ if ($_SERVER["REQUEST_METHOD"] === "POST" && $_POST["action"] === "edit") {
]; ];
$new = $row; $new = $row;
$alloc = getServiceCore($id); $alloc = getServiceCore($new["id"]);
if ($alloc === null) { if ($alloc === null) {
$alloc = allocateCore($id); $alloc = allocateCore($new["id"]);
} }
$core = (int)$alloc["cpu"]; $core = (int)$alloc["cpu"];