Merge pull request #1487 from balena-io/1471-memory-report

Improve calculation for used system memory
This commit is contained in:
bulldozer-balena[bot] 2020-10-14 17:08:41 +00:00 committed by GitHub
commit 85b9dd573f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -62,7 +62,7 @@ export async function getMemoryInformation(): Promise<{
}> {
const mem = await systeminformation.mem();
return {
used: bytesToMb(mem.used),
used: bytesToMb(mem.used - mem.cached - mem.buffers),
total: bytesToMb(mem.total),
};
}

View File

@ -1,6 +1,17 @@
import { expect } from 'chai';
import * as sysInfo from '../src/lib/system-info';
import { SinonStub, stub } from 'sinon';
import * as systeminformation from 'systeminformation';
function toMb(bytes: number) {
return Math.floor(bytes / 1024 / 1024);
}
function toBytes(kb: number) {
return kb * 1024;
}
describe('System information', () => {
describe('Delta-based filtering', () => {
it('should correctly filter cpu usage', () => {
@ -55,4 +66,40 @@ describe('System information', () => {
).to.deep.equal([]);
});
});
describe('Memory information', function () {
it('should return the correct value for memory usage', async () => {
const [total, free, cached, buffers] = [
763472,
143896,
368360,
16724,
].map(toBytes);
// Stub the output of the systeminformation module
stub(systeminformation, 'mem').resolves({
total,
free,
used: total - free,
cached,
buffers,
slab: 0,
buffcache: 0,
available: 0,
active: 0,
swaptotal: 0,
swapfree: 0,
swapused: 0,
});
const meminfo = await sysInfo.getMemoryInformation();
expect(meminfo.total).to.be.equal(toMb(total));
// used memory = total - free - (cached + buffers)
// this is how `htop` and `free` calculate it
expect(meminfo.used).to.be.equal(toMb(total - free - (cached + buffers)));
(systeminformation.mem as SinonStub).restore();
});
});
});