&1', $output);
return preg_match('/^\s*\[-n\s/m', implode("\n", $output)) === 1;
}
// Parse intel_gpu_top JSON output
function parseIntelGpuTop() {
$output = [];
$returnCode = 0;
// -J = JSON output, -s 1000 = 1 second sample period
if (intelGpuTopHasSampleCount()) {
// -n 2: an initial sample and one full period, then exit
exec('timeout -s KILL 5 intel_gpu_top -J -s 1000 -n 2 -o - 2>&1', $output, $returnCode);
} else {
// SIGTERM after 2s lets intel_gpu_top flush its output; SIGKILL 3s later
// if the signal was blocked
exec('timeout -k 3 2 intel_gpu_top -J -s 1000 -o - 2>&1', $output, $returnCode);
}
$jsonOutput = implode("\n", $output);
// Check for permission error
if (strpos($jsonOutput, 'Permission denied') !== false || strpos($jsonOutput, 'CAP_PERFMON') !== false) {
return ['error' => 'Permission denied. intel_gpu_top requires CAP_PERFMON capability or root access. See "man 7 capabilities" for details.'];
}
if ($returnCode != 0 && empty($output)) {
return ['error' => 'Failed to run intel_gpu_top. Is it installed? (apt install intel-gpu-tools)'];
}
// intel_gpu_top outputs multiple JSON objects (one per sample), we want the last complete one
// Find the last complete JSON object
$jsonObjects = [];
$depth = 0;
$start = -1;
for ($i = 0; $i < strlen($jsonOutput); $i++) {
$char = $jsonOutput[$i];
if ($char === '{') {
if ($depth === 0) {
$start = $i;
}
$depth++;
} else if ($char === '}') {
$depth--;
if ($depth === 0 && $start >= 0) {
$jsonObjects[] = substr($jsonOutput, $start, $i - $start + 1);
$start = -1;
}
}
}
if (empty($jsonObjects)) {
if ($returnCode == 137) {
// Killed by the timeout backstop. The samples it printed before that are
// still usable, so this only matters when there were none.
return ['error' => 'intel_gpu_top did not stop when asked and had to be killed before it produced any output. This happens under a threaded Apache MPM (event/worker) when /bin/sh is bash. Install an intel-gpu-tools version whose intel_gpu_top supports -n, or run PHP under the prefork MPM or php-fpm.'];
}
return ['error' => 'No valid JSON output from intel_gpu_top. Raw output: ' . substr($jsonOutput, 0, 500)];
}
// Use the last complete JSON object
$lastJson = end($jsonObjects);
$data = json_decode($lastJson, true);
if ($data === null) {
return ['error' => 'Failed to parse JSON from intel_gpu_top: ' . json_last_error_msg()];
}
return $data;
}
// Get list of Intel GPU devices
function getIntelGpuDevices() {
$output = [];
$returnCode = 0;
exec('timeout -s KILL 3 intel_gpu_top -L 2>&1', $output, $returnCode);
if ($returnCode != 0 || empty($output)) {
return [];
}
$devices = [];
foreach ($output as $line) {
$line = trim($line);
if (!empty($line) && strpos($line, 'card') !== false) {
$devices[] = $line;
}
}
return $devices;
}
// Get additional GPU info from sysfs/lspci
function getIntelGpuInfo() {
$info = [
'model' => 'Unknown',
'driver' => 'Unknown',
'pci_id' => 'Unknown',
];
// Try to get GPU model from lspci
$output = [];
exec('lspci -nn 2>/dev/null | grep -i "VGA\|Display\|3D" | grep -i intel', $output);
if (!empty($output)) {
$info['model'] = trim($output[0]);
if (preg_match('/\[([0-9a-f]{4}:[0-9a-f]{4})\]/i', $output[0], $matches)) {
$info['pci_id'] = $matches[1];
}
}
// i915 and xe have no module version (not even when loadable), and nothing
// under /sys/module or modinfo exists when they are built into the kernel.
// Report the driver bound to the Intel DRM device and the kernel it ships in.
foreach (glob('/sys/class/drm/card[0-9]*', GLOB_NOSORT) as $card) {
if (trim(@file_get_contents($card.'/device/vendor')) != '0x8086') continue;
$driver = @readlink($card.'/device/driver');
if ($driver) {
$info['driver'] = basename($driver).' (kernel '.php_uname('r').')';
break;
}
}
return $info;
}
// Format percentage with color coding
function formatPercentage($value, $inverse = false) {
$value = floatval($value);
if ($inverse) {
// For RC6, higher is better (GPU is idle)
if ($value >= 80) {
$class = 'text-success';
} else if ($value >= 50) {
$class = 'text-warning';
} else {
$class = 'text-danger';
}
} else {
// For usage, lower is better (less loaded)
if ($value >= 80) {
$class = 'text-danger';
} else if ($value >= 50) {
$class = 'text-warning';
} else {
$class = 'text-success';
}
}
return '' . number_format($value, 1) . '%';
}
$gpuData = parseIntelGpuTop();
$gpuDevices = getIntelGpuDevices();
$gpuInfo = getIntelGpuInfo();
xhtmlHeaders(__FILE__, 'Intel GPU Status');
getBodyTopHTML();
echo getNavBarHTML();
?>