mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-24 07:46:41 +00:00
Merge pull request #1487 from balena-io/1471-memory-report
Improve calculation for used system memory
This commit is contained in:
commit
85b9dd573f
@ -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),
|
||||
};
|
||||
}
|
||||
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user