-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapplet.js
301 lines (242 loc) · 9.11 KB
/
applet.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const Applet = imports.ui.applet;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const GLib = imports.gi.GLib;
const Cinnamon = imports.gi.Cinnamon;
const GTop = imports.gi.GTop;
const Settings = imports.ui.settings;
const Clutter = imports.gi.Clutter;
const ByteArray = imports.byteArray;
const REFRESH_RATE = 1000;
const FLAT_RANGE = 5; // (+/- xx kb/min)
const MB = 1048576;
const KB = 1024;
const MINUTE = 60000;
function MyApplet(orientation, panel_height, instance_id) {
this._init(orientation, panel_height, instance_id);
}
MyApplet.prototype = {
__proto__: Applet.TextIconApplet.prototype,
_init: function(orientation, panel_height, instance_id) {
Applet.TextIconApplet.prototype._init.call(this, orientation, panel_height, instance_id);
this.lastCurMb = 0;
this.maxMb = 0;
this._applet_label.set_style("font-weight: normal;");
this.process_display_name = "Cinnamon";
this.settings = new Settings.AppletSettings(this, "[email protected]", instance_id);
this.settings.bindProperty(Settings.BindingDirection.IN,
"process-name",
"process_name",
this.on_settings_changed,
null);
this.on_settings_changed();
this.pulse_timer_id = 0;
this._orientation = orientation;
this.initialTime = new Date();
},
get_pid_for_process_name: function (name) {
let success, stdout, stderr, code, error;
[success, stdout, stderr, code, error] = GLib.spawn_command_line_sync("ps -eo \"\%p \%c\"");
let pid = global.get_pid();
this.process_display_name = "Cinnamon";
let lines = ByteArray.toString(stdout).split("\n");
for (let line in lines) {
try {
if (lines[line].indexOf("<defunct>") > -1) {
continue;
}
let split_line = lines[line].trim().split(" ");
if (split_line.length == 2 && split_line[1] == name) {
pid = split_line[0];
this.process_display_name = name;
break;
}
} catch (e) {
continue;
}
}
return pid;
},
on_settings_changed: function () {
if (this.process_name == "") {
this.pid = global.get_pid();
}
else
if (!isNaN(parseInt(this.process_name))) {
this.pid = parseInt(this.process_name);
this.process_display_name = `pid(${this.process_name})`;
}
else
{
this.pid = this.get_pid_for_process_name(this.process_name);
}
this.cinnamonMem = new CinnamonMemMonitor(this.pid);
this._applet_label.set_x_align(Clutter.ActorAlign.CENTER);
this._applet_label.set_x_expand(true);
let test_string;
if (this.pid.toString() == global.get_pid().toString()) {
test_string = "cinnamon: 00000.00m, 100.0%";
} else {
test_string = this.process_name + ": 00000.00m, 100.0%";
}
let layout = this._applet_label.create_pango_layout(test_string);
let w, h;
[w, h] = layout.get_pixel_size();
this.actor.natural_width = w;
},
_pulse: function() {
this.cinnamonMem.update();
let now = new Date();
let elapsed = (now.getTime() - this.initialTime.getTime()) / MINUTE; // get elapsed minutes
let delta = this.cinnamonMem.getDiffKb() / elapsed;
let ttip;
if (delta > FLAT_RANGE) {
ttip = "+" + delta.toFixed(2) + " k/min\n";
} else if (delta < -FLAT_RANGE) {
ttip = delta.toFixed(2) + " k/min\n";
} else {
ttip = "flat\n";
}
ttip += "-------\n";
ttip += "Process: " + this.process_display_name + "\n";
ttip += "PID: " + this.pid.toString() + "\n";
let time = secondsToTime(elapsed * 60);
ttip += "Elapsed: " + time.h + ":" + time.m + ":" + time.s + "\n";
ttip += "-------\n";
ttip += "Start: " + this.cinnamonMem.getStartMb().toFixed(2) + " m\n";
ttip += "Diff: " + this.cinnamonMem.getDiffMb().toFixed(2) + " m\n";
ttip += "Max: " + this.cinnamonMem.getMaxMb().toFixed(2) + " m\n";
ttip += "-------\n";
ttip += "Acc. CPU%: " + (this.cinnamonMem.getCinnamonAccumulatedCpuUsage()*100).toPrecision(3) + "\n";
ttip += "Acc. Total CPU%: " + (this.cinnamonMem.getTotalAccumulatedCpuUsage()*100).toPrecision(3) + "\n";
ttip += "-------\n";
ttip += "click to reset or reconnect to the process";
let curMb = this.cinnamonMem.getCurMb().toFixed(2);
let newCurMb = 100 * curMb;
let cpuUsage = (this.cinnamonMem.getCpuUsage()*100).toFixed(1);
let updown = (newCurMb > this.lastCurMb) ? "⇗" : (newCurMb == this.lastCurMb) ? "⇒" : "⇘"; // Symbola fonts
this.lastCurMb = newCurMb;
let label;
if (this.process_display_name == "Cinnamon") {
label = updown + " " + curMb + " m, " + cpuUsage + " %";
} else {
label = this.process_display_name + ": " + updown + " " + curMb + " m, " + cpuUsage + " %";
}
if (this.maxMb < this.lastCurMb) {
this.maxMb = this.lastCurMb;
label = "*"+label;
//~ this.actor.set_style("color: red;");
} else {
//~ this.actor.set_style("color: #b9b9b9;");
}
this.set_applet_label(label);
this.set_applet_tooltip(ttip);
return true;
},
stop_pulse: function() {
if (this.pulse_timer_id > 0) {
Mainloop.source_remove(this.pulse_timer_id);
this.pulse_timer_id = 0;
}
},
on_applet_added_to_panel: function() {
this.stop_pulse();
this.pulse_timer_id = Mainloop.timeout_add(REFRESH_RATE, Lang.bind(this, this._pulse));
},
on_applet_removed_from_panel: function(event) {
this.stop_pulse();
},
on_applet_clicked: function(event) {
this.initialTime = new Date();
this.on_settings_changed();
},
on_orientation_changed: function (orientation) {
this._orientation = orientation;
// this._initContextMenu();
}
};
function CinnamonMemMonitor(pid) {
this._init(pid);
}
CinnamonMemMonitor.prototype = {
_init: function(pid) {
try {
this.pid = pid;
this.procMem = new GTop.glibtop_proc_mem();
this.procTime = new GTop.glibtop_proc_time();
this.gtop = new GTop.glibtop_cpu();
this.maxMem = 0;
this.resetStats();
} catch (e) {
global.logError(e);
}
},
update: function() {
GTop.glibtop_get_proc_mem(this.procMem, this.pid);
this.lastRtime = this.procTime.rtime;
this.lastTick = this.gtop.total;
GTop.glibtop_get_proc_time(this.procTime, this.pid);
GTop.glibtop_get_cpu(this.gtop);
if (this.maxMem < this.procMem.resident) {
this.maxMem = this.procMem.resident;
}
},
getCpuUsage: function() {
let delta = this.procTime.rtime - this.lastRtime;
let tickDelta = this.gtop.total - this.lastTick;
return tickDelta ? delta/tickDelta : 0;
},
getCinnamonAccumulatedCpuUsage: function() {
let delta = this.procTime.rtime - this.startRtime;
let tickDelta = this.gtop.total - this.startTicks;
return tickDelta ? delta/tickDelta : 0;
},
getTotalAccumulatedCpuUsage: function() {
let delta = this.gtop.idle - this.startIdle;
let tickDelta = this.gtop.total - this.startTicks;
return 1 - (tickDelta ? delta/tickDelta : 0);
},
getCurMb: function() {
return this.procMem.resident/MB;
},
getStartMb: function() {
return this.startMem/MB;
},
getMaxMb: function() {
return this.maxMem / MB;
},
getDiffMb: function() {
return (this.procMem.resident - this.startMem)/MB;
},
getDiffKb: function() {
return (this.procMem.resident - this.startMem)/KB;
},
resetStats: function() {
this.update();
this.startMem = this.procMem.resident;
this.startRtime = this.procTime.rtime;
this.startTicks = this.gtop.total;
this.startIdle = this.gtop.idle;
this.maxMem = this.startMem;
}
};
function main(metadata, orientation, panel_height, instance_id) {
let myApplet = new MyApplet(orientation, panel_height, instance_id);
return myApplet;
}
function secondsToTime(secs)
{
let hours = Math.floor(secs / (60 * 60));
let divisor_for_minutes = secs % (60 * 60);
let minutes = Math.floor(divisor_for_minutes / 60);
let divisor_for_seconds = divisor_for_minutes % 60;
let seconds = Math.floor(divisor_for_seconds);
let obj = {
"h": hours,
"m": minutes < 10 ? "0" + minutes: minutes,
"s": seconds < 10 ? "0" + seconds: seconds
};
return obj;
}