Skip to content

Commit

Permalink
Merge pull request mtwebster#1 from autarkper/pull
Browse files Browse the repository at this point in the history
Add CPU monitoring, plus some other changes
  • Loading branch information
mtwebster committed Jan 6, 2013
2 parents 594c8b8 + 0b699e0 commit 1eded09
Showing 1 changed file with 61 additions and 18 deletions.
79 changes: 61 additions & 18 deletions applet.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@ MyApplet.prototype = {
_init: function(orientation) {
Applet.TextIconApplet.prototype._init.call(this, orientation);

try {
this.set_applet_icon_symbolic_name('utilities-system-monitor-symbolic');
this._orientation = orientation;
this.cinnamonMem = new CinnamonMemMonitor();
this.initialTime = new Date();

this._pulse();
}
catch (e) {
global.logError(e);
}
// Unless we hard-code a width, the applet will change its width dynamically,
// as the width of the displayed data changes.
this.actor.width = 100; // heuristically determined value
// Make label less prominent.
this._applet_label.set_style("font-weight: normal;");

this._orientation = orientation;
this.cinnamonMem = new CinnamonMemMonitor();
this.initialTime = new Date();
},

_pulse: function() {
this.cinnamonMem.updateMem();
if (this.stopped) return;

this.cinnamonMem.update();
let now = new Date();
let elapsed = (now.getTime() - this.initialTime.getTime()) / MINUTE; // get elapsed minutes
let delta = this.cinnamonMem.getDiffKb() / elapsed;
Expand All @@ -57,14 +57,30 @@ MyApplet.prototype = {
ttip += "Diff: " + this.cinnamonMem.getDiffMb().toFixed(2) + "m\n";
let time = secondsToTime(elapsed * 60);
ttip += "Elapsed: " + time.h + ":" + time.m + ":" + time.s + "\n";
ttip += "Acc. Cinnamon 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";
let label = " " + this.cinnamonMem.getCurMb().toFixed(2) + "m";

let curMb = this.cinnamonMem.getCurMb().toFixed(2);
let cpuUsage = (this.cinnamonMem.getCpuUsage()*100).toPrecision(2);

let label = " " + curMb + "m, " + cpuUsage + "%";
this.set_applet_label(label);

this.set_applet_tooltip(ttip);
Mainloop.timeout_add(REFRESH_RATE, Lang.bind(this, this._pulse));
},

on_applet_added_to_panel: function() {
this.stopped = false;
this._pulse();
},

on_applet_removed_from_panel: function(event) {
this.stopped = true;
},

on_applet_clicked: function(event) {
this.cinnamonMem.resetStats();
this.initialTime = new Date();
Expand All @@ -86,15 +102,39 @@ CinnamonMemMonitor.prototype = {
try {
this.pid = global.get_pid();
this.procMem = new GTop.glibtop_proc_mem();
GTop.glibtop.get_proc_mem(this.procMem, this.pid);
this.startMem = this.procMem.resident;
this.procTime = new GTop.glibtop_proc_time();
this.gtop = new GTop.glibtop_cpu();

this.resetStats();
} catch (e) {
global.logError(e);
}
},

updateMem: function() {
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);
},

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() {
Expand All @@ -114,8 +154,11 @@ CinnamonMemMonitor.prototype = {
},

resetStats: function() {
this.updateMem();
this.update();
this.startMem = this.procMem.resident;
this.startRtime = this.procTime.rtime;
this.startTicks = this.gtop.total;
this.startIdle = this.gtop.idle;
}
};

Expand All @@ -139,4 +182,4 @@ function secondsToTime(secs)
"s": seconds < 10 ? "0" + seconds: seconds
};
return obj;
}
}

0 comments on commit 1eded09

Please sign in to comment.