Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show nvidia gpu utilization when available - closes 39 #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 105 additions & 80 deletions lib/gtop.js
Original file line number Diff line number Diff line change
@@ -1,97 +1,122 @@
var blessed = require('blessed'),
contrib = require('blessed-contrib'),
monitor = require('./monitor');


var screen = blessed.screen()
var grid = new contrib.grid({
rows: 12,
cols: 12,
screen: screen
})

var cpuLine = grid.set(0, 0, 4, 12, contrib.line, {
showNthLabel: 5,
maxY: 100,
label: 'CPU History',
showLegend: true,
})

var memLine = grid.set(4, 0, 4, 8, contrib.line, {
showNthLabel: 5,
maxY: 100,
label: 'Memory and Swap History',
showLegend: true,
legend: {
width: 10
}
})

var memDonut = grid.set(4, 8, 2, 4, contrib.donut, {
radius: 8,
arcWidth: 3,
yPadding: 2,
remainColor: 'black',
label: 'Memory',
});
monitor = require('./monitor'),
exec = require('child_process').exec;

var swapDonut = grid.set(6, 8, 2, 4, contrib.donut, {
radius: 8,
arcWidth: 3,
yPadding: 2,
remainColor: 'black',
label: 'Swap',
});
function init() {
let supportsNvidiaSmi = false;

exec('nvidia-smi -L | wc -l', (error, stdout) => {
if (!error && parseInt(stdout, 10) > 0) {
supportsNvidiaSmi = true;
}
render({ supportsNvidiaSmi });
});
}

var netSpark = grid.set(8, 0, 2, 6, contrib.sparkline, {
label: 'Network History',
tags: true,
style: {
fg: 'blue'
function render({ supportsNvidiaSmi }) {
var screen = blessed.screen()
var grid = new contrib.grid({
rows: supportsNvidiaSmi ? 16 : 12,
cols: 12,
screen: screen
})

var cpuLine = grid.set(0, 0, 4, 12, contrib.line, {
showNthLabel: 5,
maxY: 100,
label: 'CPU History',
showLegend: true,
})

var memLine = grid.set(4, 0, 4, 8, contrib.line, {
showNthLabel: 5,
maxY: 100,
label: 'Memory and Swap History',
showLegend: true,
legend: {
width: 10
}
})

var memDonut = grid.set(4, 8, 2, 4, contrib.donut, {
radius: 8,
arcWidth: 3,
yPadding: 2,
remainColor: 'black',
label: 'Memory',
});

var swapDonut = grid.set(6, 8, 2, 4, contrib.donut, {
radius: 8,
arcWidth: 3,
yPadding: 2,
remainColor: 'black',
label: 'Swap',
});

var netSpark = grid.set(8, 0, 2, 6, contrib.sparkline, {
label: 'Network History',
tags: true,
style: {
fg: 'blue'
}
})

var diskDonut = grid.set(10, 0, 2, 6, contrib.donut, {
radius: 8,
arcWidth: 3,
yPadding: 2,
remainColor: 'black',
label: 'Disk usage',
})

var procTable = grid.set(8, 6, 4, 6, contrib.table, {
keys: true,
label: 'Processes',
columnSpacing: 1,
columnWidth: [7, 24, 7, 7]
})

if (supportsNvidiaSmi) {
var gpuLine = grid.set(12, 0, 4, 12, contrib.line, {
showNthLabel: 5,
maxY: 100,
label: 'GPU History',
showLegend: true,
})
}
})

var diskDonut = grid.set(10, 0, 2, 6, contrib.donut, {
radius: 8,
arcWidth: 3,
yPadding: 2,
remainColor: 'black',
label: 'Disk usage',
})

var procTable = grid.set(8, 6, 4, 6, contrib.table, {
keys: true,
label: 'Processes',
columnSpacing: 1,
columnWidth: [7, 24, 7, 7]
})

procTable.focus()

screen.render();
screen.on('resize', function(a) {
cpuLine.emit('attach');
memLine.emit('attach');
memDonut.emit('attach');
swapDonut.emit('attach');
netSpark.emit('attach');
diskDonut.emit('attach');
procTable.emit('attach');
});

screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
procTable.focus()

screen.render();
screen.on('resize', function(a) {
cpuLine.emit('attach');
memLine.emit('attach');
memDonut.emit('attach');
swapDonut.emit('attach');
netSpark.emit('attach');
diskDonut.emit('attach');
procTable.emit('attach');
if (supportsNvidiaSmi) {
gpuLine.emit('attach');
}
});

screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});

function init() {
new monitor.Cpu(cpuLine); //no Windows support
new monitor.Mem(memLine, memDonut, swapDonut);
new monitor.Net(netSpark);
new monitor.Disk(diskDonut);
new monitor.Proc(procTable); // no Windows support
if (supportsNvidiaSmi) {
new monitor.Gpu(gpuLine);
}
}


process.on('uncaughtException', function(err) {
// avoid exiting due to unsupported system resources in Windows
});
Expand Down
64 changes: 64 additions & 0 deletions lib/monitor/gpu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { exec } = require('child_process');
const { colors } = require('../utils');

function gpuInfoCmd (gpuId) {
return `nvidia-smi --id=${gpuId} --query-gpu=utilization.gpu,utilization.memory --format=csv`;
}

function getNvidiaData (gpuId) {
return new Promise(resolve => {
const cmd = gpuInfoCmd(gpuId);
exec(cmd, (error, stdout) => {
if (error) {
return reject(error);
}
const utilization = parseUtilization(stdout);
resolve(utilization);
});
});
}

function parseUtilization (data) {
const lines = data.split('\n');
const values = lines[1].split(',');

return {
load: parseInt(values[0], 10),
memory: parseInt(values[1], 10)
};
}

class Gpu {
constructor(line) {
this.line = line;
this.gpus = [{}];
this.gpuData = this.gpus.map((_, i) => ({
title: `GPU ${i+1}`,
style: {
line: colors[i % colors.length],
},
x: Array(61).fill().map((_, i) => 60 - i),
y: Array(61).fill(0),
}));
this.render();
this.interval = setInterval(() => this.render(), 1000);
}

render() {
this.gpus.forEach((gpu, i) => {
getNvidiaData(i)
.then((data) => this.updateGpuData(data, i))
.catch(err => console.error(err));
});
this.line.setData(this.gpuData);
this.line.screen.render();
}

updateGpuData(data, i) {
this.gpuData[i].title = `GPU${i+1} ${data.load}%`;
this.gpuData[i].y.shift();
this.gpuData[i].y.push(data.load);
}
}

module.exports = Gpu;
1 change: 1 addition & 0 deletions lib/monitor/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
Cpu: require('./cpu'),
Gpu: require('./gpu'),
Mem: require('./mem'),
Net: require('./net'),
Disk: require('./disk'),
Expand Down