Skip to content

Commit

Permalink
Normalize memory to MB
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Sep 5, 2020
1 parent 7788a4a commit da649ab
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
8 changes: 2 additions & 6 deletions lib/OperatingSystems/DefaultOs.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ public function getMemory(): Memory {
}

foreach ($matches['Key'] as $i => $key) {
$value = (int)$matches['Value'][$i];
$unit = $matches['Unit'][$i];

if ($unit === 'kB') {
$value *= 1024;
}
// Value is always in KB: https://github.com/torvalds/linux/blob/c70672d8d316ebd46ea447effadfe57ab7a30a50/fs/proc/meminfo.c#L58-L60
$value = (int)$matches['Value'][$i] / 1024;

switch ($key) {
case 'MemTotal':
Expand Down
8 changes: 4 additions & 4 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function getMemory(): Memory {

$result = preg_match_all($pattern, $swapinfo, $matches);
if ($result === 1) {
$data->setSwapTotal((int)$matches['Avail'][0]);
$data->setSwapFree($data->getSwapTotal() - (int)$matches['Used'][0]);
$data->setSwapTotal((int)$matches['Avail'][0] / 1024);
$data->setSwapFree(($data->getSwapTotal() - (int)$matches['Used'][0]) / 1024);
}

unset($matches, $result);
Expand All @@ -67,8 +67,8 @@ public function getMemory(): Memory {

$lines = explode("\n", $meminfo);
if (count($lines) > 4) {
$data->setMemTotal((int)$lines[0]);
$data->setMemAvailable((int)$lines[1] * ((int)$lines[2] + (int)$lines[3] + (int)$lines[4]));
$data->setMemTotal((int)$lines[0] / 1024 / 1024);
$data->setMemAvailable(((int)$lines[1] * ((int)$lines[2] + (int)$lines[3] + (int)$lines[4])) / 1024 / 1024);
}

unset($lines);
Expand Down
32 changes: 31 additions & 1 deletion lib/Resources/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,72 @@ class Memory {
private $swapTotal = -1;
private $swapFree = -1;

/**
* @return int in MB
*/
public function getMemTotal(): int {
return $this->memTotal;
}

/**
* @param int $memTotal in MB
*/
public function setMemTotal(int $memTotal): void {
$this->memTotal = $memTotal;
}

/**
* @return int in MB
*/
public function getMemFree(): int {
return $this->memFree;
}

/**
* @param int $memFree in MB
*/
public function setMemFree(int $memFree): void {
$this->memFree = $memFree;
}

/**
* @return int in MB
*/
public function getMemAvailable(): int {
return $this->memAvailable;
}

/**
* @param int $memAvailable in MB
*/
public function setMemAvailable(int $memAvailable): void {
$this->memAvailable = $memAvailable;
}

/**
* @return int in MB
*/
public function getSwapTotal(): int {
return $this->swapTotal;
}

/**
* @param int $swapTotal in MB
*/
public function setSwapTotal(int $swapTotal): void {
$this->swapTotal = $swapTotal;
}

/**
* @return int in MB
*/
public function getSwapFree(): int {
return $this->swapFree;
}


/**
* @param int $swapFree in MB
*/
public function setSwapFree(int $swapFree): void {
$this->swapFree = $swapFree;
}
Expand Down
10 changes: 5 additions & 5 deletions tests/lib/DefaultOsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public function testGetMemory(): void {

$memory = $this->os->getMemory();

$this->assertEquals(16330252 * 1024, $memory->getMemTotal());
$this->assertEquals(2443908 * 1024, $memory->getMemFree());
$this->assertEquals(7675276 * 1024, $memory->getMemAvailable());
$this->assertEquals(999420 * 1024, $memory->getSwapTotal());
$this->assertEquals(917756 * 1024, $memory->getSwapFree());
$this->assertEquals(15947, $memory->getMemTotal());
$this->assertEquals(2386, $memory->getMemFree());
$this->assertEquals(7495, $memory->getMemAvailable());
$this->assertEquals(975, $memory->getSwapTotal());
$this->assertEquals(896, $memory->getSwapFree());
}

public function testGetMemoryNoData(): void {
Expand Down
12 changes: 6 additions & 6 deletions tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function testGetMemory(): void {

$memory = $this->os->getMemory();

$this->assertEquals(68569628672, $memory->getMemTotal());
$this->assertEquals(65393, $memory->getMemTotal());
$this->assertEquals(-1, $memory->getMemFree());
$this->assertEquals(15809376256, $memory->getMemAvailable());
$this->assertEquals(3744300, $memory->getSwapTotal());
$this->assertEquals(3744300, $memory->getSwapFree());
$this->assertEquals(15076, $memory->getMemAvailable());
$this->assertEquals(3656, $memory->getSwapTotal());
$this->assertEquals(3656, $memory->getSwapFree());
}

public function testGetMemoryNoSwapinfo(): void {
Expand All @@ -79,9 +79,9 @@ public function testGetMemoryNoSwapinfo(): void {

$memory = $this->os->getMemory();

$this->assertEquals(68569628672, $memory->getMemTotal());
$this->assertEquals(65393, $memory->getMemTotal());
$this->assertEquals(-1, $memory->getMemFree());
$this->assertEquals(15809376256, $memory->getMemAvailable());
$this->assertEquals(15076, $memory->getMemAvailable());
$this->assertEquals(-1, $memory->getSwapTotal());
$this->assertEquals(-1, $memory->getSwapFree());
}
Expand Down

0 comments on commit da649ab

Please sign in to comment.