-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdrawRamOnChart.js
200 lines (184 loc) · 4.61 KB
/
drawRamOnChart.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Builds Chart in png file with RAM consumption of one or several processes.
// To filter process you can pass a fake argument as a CLI argument e.g. --tagprocess and then run it as:
//
// node drawRamOnChart.js 'tagprocess'
//
// Or you can use process name directly:
//
// node drawRamOnChart.js 'chrome'
//
// If there are multiple processes with specified tag, then the number of such processes will be counted (red line) and chart will show common RAM (blue)
//
// You can also use OR to filter by multiple criteria:
//
// # draw summary RAM of all processes which have tagprocess or chrome in ps aux output
// node drawRamOnChart.js 'tagprocess|chrome'
const vega = require('vega');
const sharp = require('sharp');
const tags = process.argv[2] ? process.argv[2].split('|') : ['tagprocess'];
const { exec } = require('child_process');
function os_func() {
this.execCommand = function (cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
};
}
var os = new os_func();
const TITLE_FONT_SIZE = 28;
const LABEL_FONT_SIZE = 24;
const data = {
$schema: 'https://vega.github.io/schema/vega/v5.json',
description: 'Line chart',
width: 1920,
height: 1080,
padding: 20,
background: 'white',
data: [
{
name: 'table',
values: [],
},
],
scales: [
{
name: 'x',
type: 'linear',
range: 'width',
round: true,
domain: { data: 'table', field: 'x' },
},
{
name: 'y',
type: 'linear',
range: 'height',
nice: true,
zero: false,
domain: { data: 'table', field: 'y' },
},
{
name: 'p',
type: 'linear',
range: 'height',
nice: true,
zero: false,
domain: { data: 'table', field: 'p' },
},
],
axes: [
{
orient: 'bottom',
scale: 'x',
title: 'Time (seconds)',
titleFontSize: TITLE_FONT_SIZE,
labelFontSize: LABEL_FONT_SIZE,
},
{
orient: 'left',
scale: 'y',
labelColor: 'blue',
tickColor: 'blue',
title: 'Occupied RAM (MiB)',
titleColor: 'blue',
grid: true,
titleFontSize: TITLE_FONT_SIZE,
labelFontSize: LABEL_FONT_SIZE,
},
{
orient: 'right',
scale: 'p',
labelColor: 'red',
tickColor: 'red',
title: 'Processes count',
titleColor: 'red',
tickMinStep: 1,
titleFontSize: TITLE_FONT_SIZE,
labelFontSize: LABEL_FONT_SIZE,
},
],
marks: [
{
fontSize: 25,
type: 'line',
from: { data: 'table' },
encode: {
enter: {
x: { scale: 'x', field: 'x' },
y: { scale: 'y', field: 'y' },
stroke: { value: 'blue' },
strokeWidth: { value: 2 },
},
update: {
strokeOpacity: { value: 1 },
},
hover: {
strokeOpacity: { value: 0.5 },
},
},
},
{
type: 'line',
from: { data: 'table' },
encode: {
enter: {
x: { scale: 'x', field: 'x' },
y: { scale: 'p', field: 'p' },
stroke: { value: 'red' },
strokeWidth: { value: 2 },
},
update: {
strokeOpacity: { value: 1 },
},
hover: {
strokeOpacity: { value: 0.5 },
},
},
},
],
};
function doPlot() {
const view = new vega.View(vega.parse(data)).renderer('none').initialize();
view
.toSVG()
.then(async function (svg) {
await sharp(Buffer.from(svg))
.toFormat('png')
.toFile(`RAMChart_${tags.join('_or_')}.png`);
})
.catch(function (err) {
console.error(err);
});
}
const start = new Date();
async function run() {
while (1) {
await new Promise((r) => setTimeout(r, 1000));
const secondsPassed = Math.round(new Date() - start) / 1000;
const rss = await os.execCommand(
`ps aux | grep ${tags.reduce(
(a, t) => a + ' -e ' + t,
''
)} | grep -v grep | grep -v drawRamOnChart | awk '{print $6}'`
);
// rss might be string separated by \n if there are multiple processes with tag (e.g. when child process spawned)
// we want to see total value so we summ them
// filter out empty strings
const rssForMultipleProcesses = rss.split('\n').filter((v) => !!v);
const sumRSS = rssForMultipleProcesses.reduce((summ, value) => {
return summ + +value;
}, 0);
data.data[0].values.push({
x: secondsPassed,
y: +sumRSS / 1024,
p: rssForMultipleProcesses.length,
});
doPlot();
}
}
run();