Changed: Parse nvidia-smi output to determine video memory

This commit is contained in:
kervala 2016-01-01 16:04:06 +01:00
parent f793077ac0
commit 51dba01e7d

View file

@ -601,6 +601,83 @@ sint CSystemUtils::getTotalVideoMemory()
#elif defined(NL_OS_MAC)
// the right method is using OpenGL
#else
if (res == -1)
{
// use nvidia-smi
std::string command = "nvidia-smi -q -d MEMORY";
std::string out = getCommandOutput(command);
if (out.empty())
{
nlwarning("Unable to launch %s", command.c_str());
}
else
{
std::vector<std::string> lines;
explode(out, std::string("\n"), lines, true);
// process each line
for(uint i = 0; i < lines.size(); ++i)
{
// Total : 62 MB
std::string line = lines[i];
// find Total line
std::string::size_type pos = line.find("Total");
if (pos == std::string::npos) continue;
pos += 6;
// find separator
pos = line.find(':', pos);
if (pos == std::string::npos) continue;
pos += 2;
// find units
std::string::size_type posUnits = line.find(' ', pos);
if (posUnits == std::string::npos) continue;
++posUnits;
// found device ID
std::string memory = line.substr(pos, posUnits-pos-1);
std::string units = line.substr(posUnits);
// convert video memory to sint
if (NLMISC::fromString(memory, res))
{
if (units == "MB")
{
res *= 1024;
}
else if (units == "GB")
{
res *= 1024 * 1024;
}
else
{
// reset to use other methods
res = -1;
nlwarning("nvidia-smi reported %d %s as wrong video memory units", res, units.c_str());
break;
}
nlinfo("nvidia-smi reported %d KiB of video memory", res);
}
else
{
// reset to use other methods
res = -1;
}
break;
}
}
}
if (res == -1)
{
// under Linux, no method is really reliable...
NLMISC::CIFile file;
@ -625,6 +702,7 @@ sint CSystemUtils::getTotalVideoMemory()
if (pos != std::string::npos)
{
// [ 20.883] (--) NVIDIA(0): Memory: 2097152 kBytes
// [ 28.515] (--) NVIDIA(0): Memory: 262144 kBytes
pos = line.find("Memory: ", pos);
// found memory line
@ -669,11 +747,12 @@ sint CSystemUtils::getTotalVideoMemory()
break;
}
// TODO: other drivers: nv, fglrx (ATI), radeon (ATI)
// TODO: other drivers: fglrx (ATI), radeon (ATI)
}
file.close();
}
}
if (res == -1)
{