-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·74 lines (55 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
const exec = require('child_process').execSync;
function getMethods (platform) {
function darwinGetMemoryUsage (memType) {
let pos = 3;
if (memType.split(' ').length > 1) {
pos = 4;
}
const cmd = `vm_stat | grep 'Pages ${memType}:' | awk '{print $${pos}}'`;
return exec(cmd, {'encoding' : 'utf8'});
}
function darwinGetTotalMemory () {
return parseInt(exec('sysctl -n hw.memsize', {'encoding' : 'utf8'}), 10);
}
function darwinGetUsedMemory () {
const memTypes = ['active', 'wired down', 'inactive'];
const pageSize = darwinPageSize();
let totalUsed = 0;
memTypes.forEach(function (type) {
totalUsed += parseInt(darwinGetMemoryUsage(type), 10);
});
return (totalUsed * pageSize);
}
function darwinPageSize () {
return parseInt(exec('sysctl -n hw.pagesize', {'encoding' : 'utf8'}), 10);
}
function linuxGetTotalMemory () {
return parseInt(exec('free -b | grep "Mem:" | awk \'{print $2}\'', {'encoding' : 'utf8'}), 10);
}
function linuxGetUsedMemory () {
return parseInt(exec('free -b | grep "Mem:" | awk \'{print $7}\'', {'encoding' : 'utf8'}), 10);
}
const methods = {
'linux' : {
'getTotalMemory' : linuxGetTotalMemory,
'getUsedMemory' : linuxGetUsedMemory
},
'darwin' : {
'getTotalMemory' : darwinGetTotalMemory,
'getUsedMemory' : darwinGetUsedMemory
}
};
return methods[process.platform];
}
const methods = getMethods();
function total (format) {
return methods.getTotalMemory();
}
function used (format) {
return methods.getUsedMemory();
}
function free (format) {
return methods.getTotalMemory() - methods.getUsedMemory();
}
module.exports = {total, used, free};