Skip to content

Commit

Permalink
get cpu count using /proc/cpuinfo on linux
Browse files Browse the repository at this point in the history
Signed-off-by: Malex14 <39774812+Malex14@users.noreply.github.com>
  • Loading branch information
Malex14 authored and kesselb committed Dec 12, 2024
1 parent 89683e4 commit b5b1c69
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
7 changes: 3 additions & 4 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@
e.preventDefault();
})

if (cpuload === 'N/A') {
if (cpuload === 'N/A' || numCpus === -1) {
$cpuFooterInfo.text(t('serverinfo', 'CPU info not available'));
$cpuLoadCanvas.addClass('hidden');
return;

} else if ($cpuLoadCanvas.hasClass('hidden')) {
$cpuLoadCanvas.removeClass('hidden');
}
Expand All @@ -120,7 +119,7 @@
});
}

$cpuFooterInfo.text(t('serverinfo', 'Load average: {percentage} % ({cpu}) last minute', { percentage: cpuloadPercentageFixed[0], cpu: cpuloadFixed[0] }));
$cpuFooterInfo.text(t('serverinfo', 'Load average: {percentage} % ({load}) last minute', { percentage: cpuloadPercentageFixed[0], load: cpuloadFixed[0] }));
$cpuFooterInfo[0].title = t(
'serverinfo',
'{lastMinutePercentage} % ({lastMinute}) last Minute\n{last5MinutesPercentage} % ({last5Minutes}) last 5 Minutes\n{last15MinutesPercentage} % ({last15Minutes}) last 15 Minutes',
Expand All @@ -134,7 +133,7 @@
}
);

cpuLoadLine.append(new Date().getTime(), cpuload[0]);
cpuLoadLine.append(new Date().getTime(), cpuload[0] / numCpus * 100);
}

function isMemoryStat(memTotal, memFree) {
Expand Down
17 changes: 9 additions & 8 deletions lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ public function getCpuName(): string {

$model = $matches[1][0];

$pattern = '/processor\s+:\s(.+)/';

preg_match_all($pattern, $cpuinfo, $matches);
$threads = count($matches[1]);
$threads = $this->getCpuCount();

if ($threads === 1) {
$data = $model . ' (1 thread)';
Expand All @@ -107,14 +104,18 @@ public function getCpuName(): string {
}

public function getCpuCount(): int {
$numCpu = 1; // this should be a save default
$numCpu = -1;

try {
$numCpu = intval($this->executeCommand('nproc --all'));
} catch (RuntimeException) {
$cpuinfo = $this->readContent('/proc/cpuinfo');
} catch (RuntimeException $e) {
return $numCpu;
}

return $numCpu;
$pattern = '/processor\s+:\s(.+)/';

preg_match_all($pattern, $cpuinfo, $matches);
return count($matches[1]);
}

public function getTime(): string {
Expand Down

0 comments on commit b5b1c69

Please sign in to comment.