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

graphics: optimize getNvidiaSmi() #927

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
29 changes: 17 additions & 12 deletions lib/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,24 @@ function graphics(callback) {
if (_windows) {
try {
const basePath = util.WINDIR + '\\System32\\DriverStore\\FileRepository';
// find all directories that have an nvidia-smi.exe file
const candidateDirs = fs.readdirSync(basePath).filter(dir => {
return fs.readdirSync([basePath, dir].join('/')).includes('nvidia-smi.exe');
});
// use the directory with the most recently created nvidia-smi.exe file
const targetDir = candidateDirs.reduce((prevDir, currentDir) => {
const previousNvidiaSmi = fs.statSync([basePath, prevDir, 'nvidia-smi.exe'].join('/'));
const currentNvidiaSmi = fs.statSync([basePath, currentDir, 'nvidia-smi.exe'].join('/'));
return (previousNvidiaSmi.ctimeMs > currentNvidiaSmi.ctimeMs) ? prevDir : currentDir;
});
let newestNvidiaSmi = null;
for (const dir of fs.readdirSync(basePath)) {
// nvidia-smi will only be located in directories starting with `nvdm`.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this may or may not be true. we may want to broaden this to just look inside all directories that start with nv (case insensitive.)

if (!dir.startsWith('nvdm')) continue;
const path = [basePath, dir, 'nvidia-smi.exe'].join('/');
const stats = fs.statSync(path, {
throwIfNoEntry: false
});
if (!stats) continue;
if (!newestNvidiaSmi) {
newestNvidiaSmi = { stats, path };
} else if (stats.ctimeMs > newestNvidiaSmi.stats.ctimeMs) {
newestNvidiaSmi = { stats, path };
}
}

if (targetDir) {
_nvidiaSmiPath = [basePath, targetDir, 'nvidia-smi.exe'].join('/');
if (newestNvidiaSmi) {
_nvidiaSmiPath = newestNvidiaSmi.path;
}
} catch (e) {
util.noop();
Expand Down