Monitors CPU usage level

This commit is contained in:
Raymundo Cassani 2020-05-26 12:31:31 -04:00
parent e94f511892
commit c4ac200ed3

View File

@ -3,6 +3,8 @@ const Settings = imports.ui.settings;
const Mainloop = imports.mainloop; const Mainloop = imports.mainloop;
const Lang = imports.lang; const Lang = imports.lang;
const Clutter = imports.gi.Clutter; const Clutter = imports.gi.Clutter;
const Cinnamon = imports.gi.Cinnamon;
const Gio = imports.gi.Gio;
@ -36,16 +38,6 @@ MyDesklet.prototype = {
setupUI: function(){ setupUI: function(){
// initialize size // initialize size
// create the clutter elements
// creates container for one child
// this.window = new St.Bin();
// creates a label to present text
// this.text = new St.Label();
// adds label to container
// this.window.add_actor(this.text);
// Sets the container as content actor of the desklet
// this.setContent(this.window);
this.canvas = new Clutter.Actor(); this.canvas = new Clutter.Actor();
this.canvas.remove_all_children(); this.canvas.remove_all_children();
this.text1 = new St.Label(); this.text1 = new St.Label();
@ -58,30 +50,60 @@ MyDesklet.prototype = {
// set decoration settings // set decoration settings
// set initial values // flag to indicate the first loop of the Desklet
this.first_run = true;
// update loop for Desklet
this.update(); this.update();
}, },
update: function() { update: function() {
// console log
global.log("update SMG");
// do the graph
// type of system variable to graph // type of system variable to graph
var type = this.type; var type = this.type;
// add text to label var value = 0.0;
this.text1.set_text("Hello Desktop2 ".concat(type)); // do the graph
this.text2.set_text(type);
// positions text // text positions
this.text1.set_position(null, null); this.text1.set_position(null, null);
this.text2.set_position(null, 50); this.text2.set_position(null, 50);
// call this.update() in 5 seconds // current values
this.timeout = Mainloop.timeout_add_seconds(5, Lang.bind(this, this.update)); switch (type) {
case "cpu":
// CPU usage https://rosettacode.org/wiki/Linux_CPU_utilization
var cpu_values = this.get_cpu_times();
var cpu_tot = cpu_values[0];
var cpu_idl = cpu_values[1];
if (this.first_run){
this.first_run = false;
this.cpu_tot = cpu_tot;
this.cpu_idl = cpu_idl;
}
else {
var cpu_use = 100 * (1 - (cpu_idl - this.cpu_idl) / (cpu_tot - this.cpu_tot));
this.cpu_tot = cpu_tot;
this.cpu_idl = cpu_idl;
this.text1.set_text(type);
this.text2.set_text(Math.round(cpu_use).toString());
// update in graph
}
break;
case "ram":
value = 12;
this.text1.set_text(type);
this.text2.set_text(value.toString());
global.log("update ram");
break;
}
// call this.update() every in refresh_interval seconds
this.timeout = Mainloop.timeout_add_seconds(this.refresh_interval, Lang.bind(this, this.update));
}, },
on_setting_changed: function() { on_setting_changed: function() {
// update decoration settings
// this.refreshDecoration();
// settings changed; instant refresh // settings changed; instant refresh
Mainloop.source_remove(this.timeout); Mainloop.source_remove(this.timeout);
this.update(); this.update();
@ -89,6 +111,24 @@ MyDesklet.prototype = {
on_desklet_removed: function() { on_desklet_removed: function() {
Mainloop.source_remove(this.timeout); Mainloop.source_remove(this.timeout);
} },
get_cpu_times: function(){
// launching sequential processes
// https://stackoverflow.com/questions/61147229/multiple-arguments-in-gio-subprocess
let subprocess = new Gio.Subprocess({
argv: ['/bin/sh', '-c', 'cat /proc/stat | grep cpu -w'],
flags: Gio.SubprocessFlags.STDOUT_PIPE,
});
subprocess.init(null);
let [, out] = subprocess.communicate_utf8(null, null); // get full output from stdout
let cpu_line = out.split(/\r?\n/)[0]; // get only one line
let cpu_values = cpu_line.split(/\s+/); // split by space
let cpu_idl = parseFloat(cpu_values[4]);
let cpu_tot = 0;
for (let i = 1; i<10; i++){
cpu_tot += parseFloat(cpu_values[i])
}
return [cpu_tot, cpu_idl];
}
}; };