Faster calculation of CPU and RAM use

This commit is contained in:
Raymundo Cassani 2020-05-30 23:06:55 -04:00
parent bded8373fc
commit 61e9f095cf

View File

@ -11,6 +11,8 @@ const Util = imports.misc.util;
const UUID = "system-monitor-graph@rcassani"; const UUID = "system-monitor-graph@rcassani";
const DESKLET_PATH = imports.ui.deskletManager.deskletMeta[UUID].path; const DESKLET_PATH = imports.ui.deskletManager.deskletMeta[UUID].path;
const GB_TO_KB = 1048576; // 1 GB = 1,048,576 kB
function MyDesklet(metadata, desklet_id) { function MyDesklet(metadata, desklet_id) {
this._init(metadata, desklet_id); this._init(metadata, desklet_id);
@ -122,7 +124,6 @@ MyDesklet.prototype = {
// current values // current values
switch (this.type) { switch (this.type) {
case "cpu": case "cpu":
// CPU usage https://rosettacode.org/wiki/Linux_CPU_utilization
let cpu_values = this.get_cpu_times(); let cpu_values = this.get_cpu_times();
let cpu_tot = cpu_values[0]; let cpu_tot = cpu_values[0];
let cpu_idl = cpu_values[1]; let cpu_idl = cpu_values[1];
@ -253,42 +254,28 @@ MyDesklet.prototype = {
}, },
get_cpu_times: function() { get_cpu_times: function() {
// launching sequential processes // https://rosettacode.org/wiki/Linux_CPU_utilization
// https://stackoverflow.com/questions/61147229/multiple-arguments-in-gio-subprocess let cpu_line = Cinnamon.get_file_contents_utf8_sync("/proc/stat").match(/cpu\s.+/)[0];
let subprocess = new Gio.Subprocess({ let cpu_values = cpu_line.split(/\s+/);
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_idl = parseFloat(cpu_values[4]);
let cpu_tot = 0; let cpu_tot = 0;
for (let i = 1; i<10; i++){ for (let i = 1; i<10; i++){
cpu_tot += parseFloat(cpu_values[i]) cpu_tot += parseFloat(cpu_values[i])
} }
return [cpu_tot, cpu_idl]; return [cpu_tot, cpu_idl];
// Util.spawn_async(["/bin/bash", "-c", "timeout -k " + this.timeout + " " + this.timeout + " " + command.command + " || echo \"Timeout or error occured.\""], Lang.bind(this, this._setNewCommandResult, command));
//Util.spawn_async(["/bin/bash", "-c", "timeout -k 2 2 cat /proc/stat | grep cpu -w"], Lang.bind(this, this._setNewCommandResult, command));
return [cpu_tot, cpu_idl];
}, },
get_ram_values: function() { get_ram_values: function() {
let subprocess = new Gio.Subprocess({ // https://www.man7.org/linux/man-pages/man1/free.1.html
argv: ['/bin/sh', '-c', '/usr/bin/free | grep Mem: -w'], let mem_out = Cinnamon.get_file_contents_utf8_sync("/proc/meminfo");
flags: Gio.SubprocessFlags.STDOUT_PIPE, let mem_tot = parseInt(mem_out.match(/(MemTotal):\D+(\d+)/)[2]);
}); let mem_usd = mem_tot - parseInt(mem_out.match(/(MemFree):\D+(\d+)/)[2])
subprocess.init(null); - parseInt(mem_out.match(/(Cached):\D+(\d+)/)[2])
let gb = 1048576; // 1 GB = 1,048,576 kB - parseInt(mem_out.match(/(Buffers):\D+(\d+)/)[2])
let [, out] = subprocess.communicate_utf8(null, null); // get full output from stdout - parseInt(mem_out.match(/(SReclaimable):\D+(\d+)/)[2]);
let ram_line = out.split(/\r?\n/)[0]; // get only one line
let ram_values = ram_line.split(/\s+/); // split by space let ram_tot = mem_tot / GB_TO_KB;
let ram_tot = parseFloat(ram_values[1]) / gb; let ram_usd = mem_usd / GB_TO_KB;
let ram_usd = parseFloat(ram_values[2]) / gb;
return [ram_tot, ram_usd]; return [ram_tot, ram_usd];
}, },