forked from alexge233/tmux-gpu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpu.js
executable file
·126 lines (109 loc) · 3.55 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
///
/// 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');
const os = require('os');
const fs = require('fs');
const colors = require('tmux-colors');
const path = require('path');
// width of GPU usage
const gpu_width = 5;
const pci_width = 3;
const pwr_width = 2;
const ram_width = 3;
const filename = path.join(os.homedir(), '.gpu.tmp');
// get the nvidia gpu ids installed
var gpus = sp.spawnSync("nvidia-smi", ["--query-gpu=utilization.memory,utilization.gpu,power.draw,power.limit,memory.total,memory.used", "--format=csv,noheader,nounits"]);
var output = gpus.stdout.toString();
var lines = output.split("\n");
while(lines.length > 0 && lines[lines.length-1] === "") {
lines.pop();
}
var samples = new Array(lines.length);
var devices = 0;
// for each gpu installed we'll query usage, temp and memory usage
lines.forEach(function (str) {
// [0] = ram util, [1] gpu util, [2] pwr draw, [3] pwr limit, [4] mem total [5] mem used
var values = str.split(",");
if (values.length != 6) return
samples[devices] = [0, 0, 0, 0]
samples[devices][0] = parseFloat(values[1]);
samples[devices][1] = parseFloat(values[0]);
samples[devices][2] = parseFloat(values[2]) / parseFloat(values[3]) * 100;
samples[devices][3] = parseFloat(values[5]) / parseFloat(values[4]) * 100;
devices++;
});
// 8ths
var bars = ['\u2581', '\u2582', '\u2583', '\u2584',
'\u2585', '\u2586', '\u2587', '\u2588', '\u2588'];
// load previous samples from file
fs.access(filename, fs.F_OK, function (err) {
var contents = null
if (!err) {
contents = fs.readFileSync(filename, 'utf8')
if (contents) {
try {
contents = JSON.parse(contents);
} catch {
contents = null
}
}
}
if (!Array.isArray(contents) || contents.length === 0) {
contents = null;
}
if (!contents) {
contents = [];
for (var i = 0; i < devices; ++i) {
contents[i] = {}
}
for (var d = 0; d < devices; ++d) {
history = []
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 < pwr_width; i++)
history[2][i] = parseFloat(0);
for (var i = 0; i < ram_width; i++)
history[3][i] = parseFloat(0);
contents[d] = history;
}
}
update_graph(contents)
});
function update_graph(history_records) {
for (var i = 0; i < devices; ++i) {
print_graphs(i, history_records);
}
// write computed samples to file
fs.writeFile(filename, JSON.stringify(history_records), function (err) {
if (err) {console.error('cannot write tmp file');}
});
}
function print_graphs(gpu_id, history_records) {
var text = ['', '', '', ''];
history = history_records[gpu_id]
s = samples[gpu_id]
//console.log(samples)
//console.log(history)
for (var i = 0; i < history.length; i++) {
history[i].shift();
history[i].push(s[i]);
for (var k = 0; k < history[i].length; k++) {
var x = parseInt((((history[i][k] - 0) * (8 - 1)) / (100 - 0)) + 1);
text[i] += bars[x];
}
}
process.stdout.write(" #[fg=red,bold,bg=white]GPU" + gpu_id + " " + s[0].toFixed() + "% " + s[1].toFixed() + "% ")
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]'));
}
//console.log(samples);