-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpu.js
executable file
·139 lines (123 loc) · 4.31 KB
/
gpu.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
#!/usr/bin/node
///
/// Node.js script which gets the nvidia GPU usage, free memory and temp
/// and displays it as in your tmux status bar.
///
const sp = require('child_process');
// width of GPU usage
const gpu_width = 10;
const pci_width = 3;
const temp_width = 1;
const ram_width = 3;
// get the nvidia gpu ids installed
var gpus = sp.spawnSync("/usr/bin/nvidia-settings", ["-t", "-q", "gpus"]);
var output = gpus.stdout.toString();
output = output.replace(/^\s+|\s+$/g, '');
var lines = output.split("\n");
// find lines which contain the pattern [digit]
var search = [];
lines.forEach(function(str){
if (/\[[0-9]+\]/.test(str))
search.push(str);
});
// search for the actual gpu ids, e.g.: `[gpu:0]`
var gpu_ids = [];
search.forEach(function(str){
var id = str.substring(str.lastIndexOf("["),str.lastIndexOf("]")+1);
gpu_ids.push(id);
});
// [0] = gpu_use, [1] PCIe bandwidth, [2] Core temp, [3] ram use
var averages = [0, 0, 0, 0];
var devices = 0;
// for each gpu installed we'll query usage, temp and memory usage
gpu_ids.forEach(function(id){
// query gpu usage
var query = sp.spawnSync("/usr/bin/nvidia-settings", ["-t", "-q", id+"/GPUUtilization"]);
var out = query.stdout.toString();
out = out.replace(/^\s+|\s+$/g, '');
var gpu_use = (out.match(/graphics=\d?\d/g)[0]).replace('graphics=', '');
var ram_use = (out.match(/memory=\d?\d/g)[0]).replace('memory=', '');
var pci_use = (out.match(/PCIe=\d?\d/g)[0]).replace('PCIe=', '');
averages[0] += parseFloat(gpu_use);
averages[1] += parseFloat(pci_use);
// query gpu temp
var query = sp.spawnSync("/usr/bin/nvidia-settings", ["-t", "-q", id+"/GPUCoreTemp"]);
out = query.stdout.toString();
out = out.replace(/^\s+|\s+$/g, '');
averages[2] += parseFloat(out);
// query gpu used memory
var query = sp.spawnSync("/usr/bin/nvidia-settings", ["-t", "-q", id+"/UsedDedicatedGPUMemory"]);
out = query.stdout.toString();
var used_mem = out.replace(/^\s+|\s+$/g, '');
// query gpu total memory
var query = sp.spawnSync("/usr/bin/nvidia-settings", ["-t", "-q", id+"/TotalDedicatedGPUMemory"]);
out = query.stdout.toString();
var total_mem = out.replace(/^\s+|\s+$/g, '');
// min-max norm ram usage
var norm = (used_mem - 0) / (total_mem - 0);
averages[3] += parseFloat(norm)*100;
devices++;
});
// Average all values
for (var i=0; i< averages.length; i++){
averages[i] = averages[i]/devices;
}
// 8ths
var bars = [ '\u2581','\u2582','\u2583','\u2584',
'\u2585','\u2586','\u2587','\u2588'];
// all previous history
var history = {};
const fs = require('fs');
var contents;
// load previous averages from file
fs.access('.gpu.tmp', fs.F_OK, function(err) {
if (!err) {
if (contents = fs.readFileSync('.gpu.tmp', 'utf8')){
history = JSON.parse(contents);
print_graphs();
}
}
else {
history[0] = [];
history[1] = [];
history[2] = [];
history[3] = [];
for (var i=0; i<gpu_width; i++)
history[0][i] = parseFloat(0);
for (var i=0; i<pci_width; i++)
history[1][i] = parseFloat(0);
for (var i=0; i<temp_width; i++)
history[2][i] = parseFloat(0);
for (var i=0; i<ram_width; i++)
history[3][i] = parseFloat(0);
//
print_graphs();
}
});
function print_graphs()
{
var text = ['','','',''];
for (var i=0; i<averages.length; i++){
history[i].shift();
history[i].push(averages[i]);
for (var k=0; k<history[i].length; k++){
if (i !== 2){
var x = parseInt((((history[i][k] - 0) * (8 - 1)) / (100 - 0)) + 1);
text[i] += bars[x];
}
else {
var x = parseInt((((history[i][k] - 30) * (8 - 1)) / (100 - 30)) + 1);
text[i] += bars[x];
}
}
}
var colors = require('tmux-colors');
process.stdout.write(colors('#[fg=green,bold]'+text[0]+'#[fg=cyan,bold]'+text[1]
+'#[fg=red,bold]'+text[2]+'#[fg=yellow,bold]'+text[3]
+'#[default]'));
// write computed averages to file
fs.writeFile('.gpu.tmp', JSON.stringify(history), function (err){
if (err){ console.error('cannot write tmp file');}
});
}
//console.log(averages);