Fix RAM usage calculation error (#6412)

This commit is contained in:
Vaalyn 2023-07-10 15:56:48 +02:00 committed by GitHub
parent 0bf399d5b4
commit 7be5a94b05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 13 deletions

View File

@ -150,14 +150,20 @@ final class ServerStatsAction implements SingleActionInterface
'bytes' => [
'total' => $memoryStats->memTotal,
'free' => $memoryStats->memFree,
'cached' => $memoryStats->cached,
'buffers' => $memoryStats->buffers,
'cached' => $memoryStats->getCachedMemory(),
'sReclaimable' => $memoryStats->sReclaimable,
'shmem' => $memoryStats->shmem,
'used' => $memoryStats->getUsedMemory(),
],
'readable' => [
'total' => Quota::getReadableSize($memoryStats->memTotal),
'free' => Quota::getReadableSize($memoryStats->memFree),
'cached' => Quota::getReadableSize($memoryStats->cached),
'used' => Quota::getReadableSize($memoryStats->getUsedMemory()),
'total' => Quota::getReadableSize($memoryStats->memTotal, 2),
'free' => Quota::getReadableSize($memoryStats->memFree, 2),
'buffers' => Quota::getReadableSize($memoryStats->buffers, 2),
'cached' => Quota::getReadableSize($memoryStats->getCachedMemory(), 2),
'sReclaimable' => Quota::getReadableSize($memoryStats->sReclaimable, 2),
'shmem' => Quota::getReadableSize($memoryStats->shmem, 2),
'used' => Quota::getReadableSize($memoryStats->getUsedMemory(), 2),
],
],
'swap' => [
@ -167,9 +173,9 @@ final class ServerStatsAction implements SingleActionInterface
'used' => $memoryStats->getUsedSwap(),
],
'readable' => [
'total' => Quota::getReadableSize($memoryStats->swapTotal),
'free' => Quota::getReadableSize($memoryStats->swapFree),
'used' => Quota::getReadableSize($memoryStats->getUsedSwap()),
'total' => Quota::getReadableSize($memoryStats->swapTotal, 2),
'free' => Quota::getReadableSize($memoryStats->swapFree, 2),
'used' => Quota::getReadableSize($memoryStats->getUsedSwap(), 2),
],
],
'disk' => [

View File

@ -31,7 +31,7 @@ final class Quota
$factor = (int)floor((strlen($bytesStr) - 1) / 3);
if (isset($size[$factor])) {
$byteDivisor = Math\BigInteger::of(1000)->power($factor);
$byteDivisor = Math\BigInteger::of(1024)->power($factor);
$sizeString = $bytes->toBigDecimal()
->dividedBy($byteDivisor, $decimals, Math\RoundingMode::HALF_DOWN);
@ -66,7 +66,7 @@ final class Quota
haystack: 'bkmgtpezy',
needle: $unit[0]
) ?: 0;
$byteMultiplier = Math\BigInteger::of(1000)->power($bytePower);
$byteMultiplier = Math\BigInteger::of(1024)->power($bytePower);
return Math\BigDecimal::of($size)
->multipliedBy($byteMultiplier)

View File

@ -12,7 +12,10 @@ final class MemoryData
public function __construct(
public readonly BigInteger $memTotal,
public readonly BigInteger $memFree,
public readonly BigInteger $buffers,
public readonly BigInteger $cached,
public readonly BigInteger $sReclaimable,
public readonly BigInteger $shmem,
public readonly BigInteger $swapTotal,
public readonly BigInteger $swapFree,
) {
@ -22,14 +25,20 @@ final class MemoryData
{
$memTotal = Quota::convertFromReadableSize($meminfo['MemTotal']) ?? BigInteger::zero();
$memFree = Quota::convertFromReadableSize($meminfo['MemFree']) ?? BigInteger::zero();
$buffers = Quota::convertFromReadableSize($meminfo['Buffers']) ?? BigInteger::zero();
$cached = Quota::convertFromReadableSize($meminfo['Cached']) ?? BigInteger::zero();
$sReclaimable = Quota::convertFromReadableSize($meminfo['SReclaimable']) ?? BigInteger::zero();
$shmem = Quota::convertFromReadableSize($meminfo['Shmem']) ?? BigInteger::zero();
$swapTotal = Quota::convertFromReadableSize($meminfo['SwapTotal']) ?? BigInteger::zero();
$swapFree = Quota::convertFromReadableSize($meminfo['SwapFree']) ?? BigInteger::zero();
return new self(
$memTotal,
$memFree,
$buffers,
$cached,
$sReclaimable,
$shmem,
$swapTotal,
$swapFree
);
@ -37,9 +46,20 @@ final class MemoryData
public function getUsedMemory(): BigInteger
{
return $this->memTotal
->minus($this->memFree)
->minus($this->cached);
$usedDiff = $this->memFree
->plus($this->cached)
->plus($this->sReclaimable)
->minus($this->shmem)
->plus($this->buffers);
return $this->memTotal->isGreaterThanOrEqualTo($usedDiff)
? $this->memTotal->minus($usedDiff)
: $this->memTotal->minus($this->memFree);
}
public function getCachedMemory(): BigInteger
{
return $this->cached->plus($this->buffers);
}
public function getUsedSwap(): BigInteger