Skip to content

Commit 0a4dbf8

Browse files
loks0nclaude
andcommitted
fix: ignore cpuset when it matches all online CPUs
cgroup v2 always exposes cpuset.cpus.effective listing every CPU even when no user-visible cpuset restriction is configured. Compare the set against /sys/devices/system/cpu/online and return null when it covers all of them, so the v1 fallback remains reachable and "no restriction" is no longer misrepresented as "cpuset = all CPUs". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f573d01 commit 0a4dbf8

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

src/System/System.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,29 +253,50 @@ private static function getCgroupCpusetCount(): ?float
253253
continue;
254254
}
255255

256-
$count = 0;
257-
foreach (explode(',', $contents) as $range) {
258-
if ($range === '') {
259-
continue;
260-
}
261-
if (str_contains($range, '-')) {
262-
[$start, $end] = explode('-', $range, 2);
263-
if (is_numeric($start) && is_numeric($end)) {
264-
$count += ((int) $end - (int) $start) + 1;
265-
}
266-
} elseif (is_numeric($range)) {
267-
$count += 1;
268-
}
256+
$count = self::countCpuList($contents);
257+
if ($count <= 0) {
258+
continue;
269259
}
270260

271-
if ($count > 0) {
272-
return (float) $count;
261+
// If the cpuset matches every online CPU, no user-visible restriction
262+
// is in effect (cgroup v2 always exposes the full set). Treat as null.
263+
$online = @file_get_contents('/sys/devices/system/cpu/online');
264+
if ($online !== false) {
265+
$onlineCount = self::countCpuList(trim($online));
266+
if ($onlineCount > 0 && $count >= $onlineCount) {
267+
return null;
268+
}
273269
}
270+
271+
return (float) $count;
274272
}
275273

276274
return null;
277275
}
278276

277+
/**
278+
* Counts CPUs in a Linux cpu list string like "0-3,5,7-8".
279+
*/
280+
private static function countCpuList(string $list): int
281+
{
282+
$count = 0;
283+
foreach (explode(',', $list) as $range) {
284+
if ($range === '') {
285+
continue;
286+
}
287+
if (str_contains($range, '-')) {
288+
[$start, $end] = explode('-', $range, 2);
289+
if (is_numeric($start) && is_numeric($end)) {
290+
$count += ((int) $end - (int) $start) + 1;
291+
}
292+
} elseif (is_numeric($range)) {
293+
$count += 1;
294+
}
295+
}
296+
297+
return $count;
298+
}
299+
279300
/**
280301
* Helper function to read a Linux System's /proc/stat data and convert it into an array.
281302
*

0 commit comments

Comments
 (0)