Merge pull request #776 from rcassani/wait-asynch
system-monitor-graph@rcassani - Use asynchronous wait when taking measurements
This commit is contained in:
commit
c19f44b4a9
@ -7,7 +7,6 @@ const Cinnamon = imports.gi.Cinnamon;
|
|||||||
const Gio = imports.gi.Gio;
|
const Gio = imports.gi.Gio;
|
||||||
const Cairo = imports.cairo;
|
const Cairo = imports.cairo;
|
||||||
const St = imports.gi.St;
|
const St = imports.gi.St;
|
||||||
//const Util = imports.misc.util;
|
|
||||||
const GLib = imports.gi.GLib;
|
const GLib = imports.gi.GLib;
|
||||||
const Gettext = imports.gettext;
|
const Gettext = imports.gettext;
|
||||||
|
|
||||||
@ -98,6 +97,14 @@ SystemMonitorGraph.prototype = {
|
|||||||
this.cpu_cpu_idl = 0;
|
this.cpu_cpu_idl = 0;
|
||||||
this.hdd_cpu_tot = 0;
|
this.hdd_cpu_tot = 0;
|
||||||
this.hdd_hdd_tot = 0;
|
this.hdd_hdd_tot = 0;
|
||||||
|
// values to graph
|
||||||
|
this.cpu_use = 0;
|
||||||
|
this.ram_values = new Array(2).fill(0.0);
|
||||||
|
this.swap_values = new Array(2).fill(0.0);
|
||||||
|
this.hdd_values = new Array(4).fill(0.0);
|
||||||
|
this.gpu_use = 0;
|
||||||
|
this.gpu_mem = new Array(2).fill(0.0);
|
||||||
|
|
||||||
// set colors
|
// set colors
|
||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
case "cpu":
|
case "cpu":
|
||||||
@ -148,43 +155,43 @@ SystemMonitorGraph.prototype = {
|
|||||||
// current values
|
// current values
|
||||||
switch (this.type) {
|
switch (this.type) {
|
||||||
case "cpu":
|
case "cpu":
|
||||||
let cpu_use = this.get_cpu_use();
|
this.get_cpu_use();
|
||||||
value = cpu_use / 100;
|
value = this.cpu_use / 100;
|
||||||
text1 = _("CPU");
|
text1 = _("CPU");
|
||||||
text2 = Math.round(cpu_use).toString() + "%";
|
text2 = Math.round(this.cpu_use).toString() + "%";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "ram":
|
case "ram":
|
||||||
let ram_values = this.get_ram_values();
|
this.get_ram_values();
|
||||||
let ram_use = 100 * ram_values[1] / ram_values[0];
|
let ram_use = 100 * this.ram_values[1] / this.ram_values[0];
|
||||||
value = ram_use / 100;
|
value = ram_use / 100;
|
||||||
text1 = _("RAM");
|
text1 = _("RAM");
|
||||||
text2 = Math.round(ram_use).toString() + "%"
|
text2 = Math.round(ram_use).toString() + "%"
|
||||||
text3 = ram_values[1].toFixed(1) + " / "
|
text3 = this.ram_values[1].toFixed(1) + " / "
|
||||||
+ ram_values[0].toFixed(1) + " " + _("GiB");
|
+ this.ram_values[0].toFixed(1) + " " + _("GiB");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "swap":
|
case "swap":
|
||||||
let swap_values = this.get_swap_values();
|
this.get_swap_values();
|
||||||
let swap_use = 100 * swap_values[1] / swap_values[0];
|
let swap_use = 100 * this.swap_values[1] / this.swap_values[0];
|
||||||
value = swap_use / 100;
|
value = swap_use / 100;
|
||||||
text1 = _("Swap");
|
text1 = _("Swap");
|
||||||
text2 = Math.round(swap_use).toString() + "%"
|
text2 = Math.round(swap_use).toString() + "%"
|
||||||
text3 = swap_values[1].toFixed(1) + " / "
|
text3 = this.swap_values[1].toFixed(1) + " / "
|
||||||
+ swap_values[0].toFixed(1) + " " + _("GiB");
|
+ this.swap_values[0].toFixed(1) + " " + _("GiB");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "hdd":
|
case "hdd":
|
||||||
let dir_path = decodeURIComponent(this.filesystem.replace("file://", "").trim());
|
let dir_path = decodeURIComponent(this.filesystem.replace("file://", "").trim());
|
||||||
if(dir_path == null || dir_path == "") dir_path = "/";
|
if(dir_path == null || dir_path == "") dir_path = "/";
|
||||||
let hdd_values = this.get_hdd_values(dir_path);
|
this.get_hdd_values(dir_path);
|
||||||
let hdd_use = Math.min(hdd_values[1], 100); //already in %
|
let hdd_use = Math.min(this.hdd_values[1], 100); //already in %
|
||||||
value = hdd_use / 100;
|
value = hdd_use / 100;
|
||||||
text1 = this.filesystem_label;
|
text1 = this.filesystem_label;
|
||||||
if (text1 == "") text1 = hdd_values[0];
|
if (text1 == "") text1 = this.hdd_values[0];
|
||||||
text2 = Math.round(hdd_use).toString() + "%"
|
text2 = Math.round(hdd_use).toString() + "%"
|
||||||
text3 = hdd_values[3].toFixed(0) + " " + _("GB free of") + " "
|
text3 = this.hdd_values[3].toFixed(0) + " " + _("GB free of") + " "
|
||||||
+ hdd_values[2].toFixed(0) + " " + _("GB");
|
+ this.hdd_values[2].toFixed(0) + " " + _("GB");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "gpu":
|
case "gpu":
|
||||||
@ -192,19 +199,19 @@ SystemMonitorGraph.prototype = {
|
|||||||
case "nvidia":
|
case "nvidia":
|
||||||
switch (this.gpu_variable) {
|
switch (this.gpu_variable) {
|
||||||
case "usage":
|
case "usage":
|
||||||
let gpu_use = this.get_nvidia_gpu_use();
|
this.get_nvidia_gpu_use();
|
||||||
value = gpu_use / 100;
|
value = this.gpu_use / 100;
|
||||||
text1 = _("GPU Usage");
|
text1 = _("GPU Usage");
|
||||||
text2 = Math.round(gpu_use).toString() + "%";
|
text2 = Math.round(this.gpu_use).toString() + "%";
|
||||||
break;
|
break;
|
||||||
case "memory":
|
case "memory":
|
||||||
let gpu_mem_values = this.get_nvidia_gpu_mem();
|
this.get_nvidia_gpu_mem();
|
||||||
let gpu_mem_use = 100 * gpu_mem_values[1] / gpu_mem_values[0];
|
let gpu_mem_use = 100 * this.gpu_mem[1] / this.gpu_mem[0];
|
||||||
value = gpu_mem_use / 100;
|
value = gpu_mem_use / 100;
|
||||||
text1 = _("GPU Memory");
|
text1 = _("GPU Memory");
|
||||||
text2 = Math.round(gpu_mem_use).toString() + "%"
|
text2 = Math.round(gpu_mem_use).toString() + "%"
|
||||||
text3 = gpu_mem_values[1].toFixed(1) + " / "
|
text3 = this.gpu_mem[1].toFixed(1) + " / "
|
||||||
+ gpu_mem_values[0].toFixed(1) + " " + _("GiB");
|
+ this.gpu_mem[0].toFixed(1) + " " + _("GiB");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -325,49 +332,68 @@ SystemMonitorGraph.prototype = {
|
|||||||
|
|
||||||
get_cpu_use: function() {
|
get_cpu_use: function() {
|
||||||
// https://rosettacode.org/wiki/Linux_CPU_utilization
|
// https://rosettacode.org/wiki/Linux_CPU_utilization
|
||||||
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: ['head', '-n', '1', '/proc/stat'],
|
||||||
|
flags: Gio.SubprocessFlags.STDOUT_PIPE|Gio.SubprocessFlags.STDERR_PIPE,
|
||||||
|
});
|
||||||
|
subprocess.init(null);
|
||||||
|
subprocess.wait_async(null, (sourceObject, res) => {
|
||||||
|
let [, stdout, stderr] = sourceObject.communicate_utf8(null, null);
|
||||||
|
let cpu_values = stdout.split(/\s+/);
|
||||||
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])
|
||||||
}
|
}
|
||||||
let cpu_use = 100 * (1 - (cpu_idl - this.cpu_cpu_idl) / (cpu_tot - this.cpu_cpu_tot));
|
this.cpu_use = 100 * (1 - (cpu_idl - this.cpu_cpu_idl) / (cpu_tot - this.cpu_cpu_tot));
|
||||||
this.cpu_cpu_tot = cpu_tot;
|
this.cpu_cpu_tot = cpu_tot;
|
||||||
this.cpu_cpu_idl = cpu_idl;
|
this.cpu_cpu_idl = cpu_idl;
|
||||||
return cpu_use;
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_ram_values: function() {
|
get_ram_values: function() {
|
||||||
// used = total - available
|
// used = total - available
|
||||||
let mem_out = Cinnamon.get_file_contents_utf8_sync("/proc/meminfo");
|
let subprocess = new Gio.Subprocess({
|
||||||
let mem_tot = parseInt(mem_out.match(/(MemTotal):\D+(\d+)/)[2]);
|
argv: ['cat', '/proc/meminfo'],
|
||||||
let mem_usd = mem_tot - parseInt(mem_out.match(/(MemAvailable):\D+(\d+)/)[2]);
|
flags: Gio.SubprocessFlags.STDOUT_PIPE|Gio.SubprocessFlags.STDERR_PIPE,
|
||||||
|
});
|
||||||
|
subprocess.init(null);
|
||||||
|
subprocess.wait_async(null, (sourceObject, res) => {
|
||||||
|
let [, stdout, stderr] = sourceObject.communicate_utf8(null, null);
|
||||||
|
let mem_tot = parseInt(stdout.match(/(MemTotal):\D+(\d+)/)[2]);
|
||||||
|
let mem_usd = mem_tot - parseInt(stdout.match(/(MemAvailable):\D+(\d+)/)[2]);
|
||||||
let ram_tot = mem_tot / GIB_TO_KIB;
|
let ram_tot = mem_tot / GIB_TO_KIB;
|
||||||
let ram_usd = mem_usd / GIB_TO_KIB;
|
let ram_usd = mem_usd / GIB_TO_KIB;
|
||||||
return [ram_tot, ram_usd];
|
this.ram_values = [ram_tot, ram_usd];
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_swap_values: function() {
|
get_swap_values: function() {
|
||||||
// used = total - available
|
// used = total - available
|
||||||
let mem_out = Cinnamon.get_file_contents_utf8_sync("/proc/meminfo");
|
let subprocess = new Gio.Subprocess({
|
||||||
let mem_tot = parseInt(mem_out.match(/(SwapTotal):\D+(\d+)/)[2]);
|
argv: ['cat', '/proc/meminfo'],
|
||||||
let mem_usd = mem_tot - parseInt(mem_out.match(/(SwapFree):\D+(\d+)/)[2]);
|
flags: Gio.SubprocessFlags.STDOUT_PIPE|Gio.SubprocessFlags.STDERR_PIPE,
|
||||||
|
});
|
||||||
|
subprocess.init(null);
|
||||||
|
subprocess.wait_async(null, (sourceObject, res) => {
|
||||||
|
let [, stdout, stderr] = sourceObject.communicate_utf8(null, null);
|
||||||
|
let mem_tot = parseInt(stdout.match(/(SwapTotal):\D+(\d+)/)[2]);
|
||||||
|
let mem_usd = mem_tot - parseInt(stdout.match(/(SwapFree):\D+(\d+)/)[2]);
|
||||||
let swap_tot = mem_tot / GIB_TO_KIB;
|
let swap_tot = mem_tot / GIB_TO_KIB;
|
||||||
let swap_usd = mem_usd / GIB_TO_KIB;
|
let swap_usd = mem_usd / GIB_TO_KIB;
|
||||||
return [swap_tot, swap_usd];
|
this.swap_values = [swap_tot, swap_usd];
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_hdd_values: function(dir_path) {
|
get_hdd_values: function(dir_path) {
|
||||||
let subprocess = new Gio.Subprocess({
|
let subprocess = new Gio.Subprocess({
|
||||||
argv: ['/bin/df', dir_path],
|
argv: ['/bin/df', dir_path],
|
||||||
flags: Gio.SubprocessFlags.STDOUT_PIPE,
|
flags: Gio.SubprocessFlags.STDOUT_PIPE|Gio.SubprocessFlags.STDERR_PIPE,
|
||||||
});
|
});
|
||||||
subprocess.init(null);
|
subprocess.init(null);
|
||||||
let [, out] = subprocess.communicate_utf8(null, null); // get full output from stdout
|
subprocess.wait_async(null, (sourceObject, res) => {
|
||||||
let df_line = out.match(/.+/g)[1];
|
let [, stdout, stderr] = sourceObject.communicate_utf8(null, null);
|
||||||
|
let df_line = stdout.match(/.+/g)[1];
|
||||||
let df_values = df_line.split(/\s+/); // split by space
|
let df_values = df_line.split(/\s+/); // split by space
|
||||||
// values for partition space
|
// values for partition space
|
||||||
let hdd_tot = parseFloat(df_values[1]) * 1024 / GB_TO_B;
|
let hdd_tot = parseFloat(df_values[1]) * 1024 / GB_TO_B;
|
||||||
@ -376,7 +402,8 @@ SystemMonitorGraph.prototype = {
|
|||||||
let dev_fs = df_values[0];
|
let dev_fs = df_values[0];
|
||||||
let fs = dev_fs.split(/\/+/)[2];
|
let fs = dev_fs.split(/\/+/)[2];
|
||||||
let hdd_use = this.get_hdd_use(fs);
|
let hdd_use = this.get_hdd_use(fs);
|
||||||
return [fs, hdd_use, hdd_tot, hdd_fre];
|
this.hdd_values = [fs, hdd_use, hdd_tot, hdd_fre];
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_hdd_use: function(fs) {
|
get_hdd_use: function(fs) {
|
||||||
@ -412,19 +439,32 @@ SystemMonitorGraph.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
get_nvidia_gpu_use: function() {
|
get_nvidia_gpu_use: function() {
|
||||||
let [result, stdout, stderr] = GLib.spawn_command_line_sync("nvidia-smi --query-gpu=utilization.gpu --format=csv --id=" + this.gpu_id);
|
let subprocess = new Gio.Subprocess({
|
||||||
let gpu_use = parseInt(stdout.toString().match(/[^\r\n]+/g)[1]); // parse integer in second line
|
argv: ['nvidia-smi', '--query-gpu=utilization.gpu', '--format=csv', '--id='+ this.gpu_id],
|
||||||
return gpu_use;
|
flags: Gio.SubprocessFlags.STDOUT_PIPE|Gio.SubprocessFlags.STDERR_PIPE,
|
||||||
|
});
|
||||||
|
subprocess.init(null);
|
||||||
|
subprocess.wait_async(null, (sourceObject, res) => {
|
||||||
|
let [, stdout, stderr] = sourceObject.communicate_utf8(null, null);
|
||||||
|
this.gpu_use = parseInt(stdout.toString().match(/[^\r\n]+/g)[1]); // parse integer in second line
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
get_nvidia_gpu_mem: function() {
|
get_nvidia_gpu_mem: function() {
|
||||||
let [result, stdout, stderr] = GLib.spawn_command_line_sync("nvidia-smi --query-gpu=memory.total --format=csv --id=" + this.gpu_id);
|
let subprocess = new Gio.Subprocess({
|
||||||
let mem_tot = parseInt(stdout.toString().match(/[^\r\n]+/g)[1]); // parse integer in second line
|
argv: ['nvidia-smi', '--query-gpu=memory.total,memory.used', '--format=csv', '--id='+ this.gpu_id],
|
||||||
[result, stdout, stderr] = GLib.spawn_command_line_sync("nvidia-smi --query-gpu=memory.used --format=csv --id=" + this.gpu_id);
|
flags: Gio.SubprocessFlags.STDOUT_PIPE|Gio.SubprocessFlags.STDERR_PIPE,
|
||||||
let mem_usd = parseInt(stdout.toString().match(/[^\r\n]+/g)[1]); // parse integer in second line
|
});
|
||||||
let gpu_mem_tot = mem_tot / GIB_TO_MIB;
|
subprocess.init(null);
|
||||||
let gpu_mem_usd = mem_usd / GIB_TO_MIB;
|
subprocess.wait_async(null, (sourceObject, res) => {
|
||||||
return [gpu_mem_tot, gpu_mem_usd];
|
let [, stdout, stderr] = sourceObject.communicate_utf8(null, null);
|
||||||
|
let fslines = stdout.split(/\r?\n/); // Line0:Headers Line1:Values
|
||||||
|
let items = fslines[1].split(','); // Values are comma-separated
|
||||||
|
let mem_tot = parseInt(items[0]);
|
||||||
|
let mem_usd = parseInt(items[1]);
|
||||||
|
this.gpu_mem[0] = mem_tot / GIB_TO_MIB;
|
||||||
|
this.gpu_mem[1] = mem_usd / GIB_TO_MIB;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,6 +3,6 @@
|
|||||||
"uuid": "system-monitor-graph@rcassani",
|
"uuid": "system-monitor-graph@rcassani",
|
||||||
"name": "System monitor graph",
|
"name": "System monitor graph",
|
||||||
"description": "Creates graphs for system variables.",
|
"description": "Creates graphs for system variables.",
|
||||||
"version": "1.3",
|
"version": "1.4",
|
||||||
"prevent-decorations": true
|
"prevent-decorations": true
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user