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

Updating Stacer to integrate GPU usage for nvidia [Issue #105] #415

Open
wants to merge 6 commits into
base: native
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ before_install:
- sudo apt-get update -qq
install:
- sudo apt-get -y -qq install cmake
- sudo apt-get -y -qq install libgl1-mesa-dev qt59base qt59imageformats qt59svg qt59charts-no-lgpl qt59tools
- sudo apt-get -y -qq install libgl1-mesa-dev qt59base qt59imageformats qt59svg qt59charts-no-lgpl qt59tools libpugixml-dev
- source /opt/qt*/bin/qt*-env.sh
before_script:
- mkdir build && cd build
Expand Down
Binary file added lqt
Binary file not shown.
Empty file modified release.sh
100644 → 100755
Empty file.
263 changes: 263 additions & 0 deletions stacer-core/Info/gpu_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
#include "gpu_info.h"

#include "pugixml.hpp"

#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#include <numeric>
#include <functional>

#include "command_util.h"

GpuInfo::GpuInfo()
{
load_correct_version();
}

std::string exec_gpu(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}

bool find_driver_version_xml(pugi::xml_node node)
{
return strcmp(node.name(), "driver_version") == 0;
}

bool find_cuda_version_xml(pugi::xml_node node)
{
return strcmp(node.name(), "cuda_version") == 0;
}

bool find_gpu_xml(pugi::xml_node node)
{
return strcmp(node.name(), "gpu") == 0;
}

bool find_memory_xml(pugi::xml_node node)
{
return strcmp(node.name(), "fb_memory_usage") == 0;
}

bool find_memory_tol_xml(pugi::xml_node node)
{
return strcmp(node.name(), "total") == 0;
}

bool find_memory_used_xml(pugi::xml_node node)
{
return strcmp(node.name(), "used") == 0;
}

bool find_gpu_utilisation_xml(pugi::xml_node node)
{
return strcmp(node.name(), "utilization") == 0;
}

bool find_gpu_used_xml(pugi::xml_node node)
{
return strcmp(node.name(), "gpu_util") == 0;
}

bool find_gpu_name_xml(pugi::xml_node node)
{
return strcmp(node.name(), "product_name") == 0;
}

bool find_gpu_serial_xml(pugi::xml_node node)
{
return strcmp(node.name(), "serial") == 0;
}

pugi::xml_document GpuInfo::getXMLnvidiasmi() const
// Load the file and return the documents for further processing
{
std::string xml_return = exec_gpu("nvidia-smi -q -x"); // get the xml query from nvidia-smi
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xml_return.c_str());

return doc;
}

struct GpuInfo::getGPUDriverVersion_nvidiasmi{
QString operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

return QString::fromStdString(doc.first_child().find_node(find_driver_version_xml).first_child().value());
}
};

struct GpuInfo::getGPUCudaVersion_nvidiasmi{
QString operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

std::string cuda_str = "CUDA_";

return QString::fromStdString( cuda_str.append(doc.first_child().find_node(find_cuda_version_xml).first_child().value()));
}
};

struct GpuInfo::getGPUPhysicalCoreCount_nvidiasmi{
int operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

int i = 0;

for (pugi::xml_node gpu: doc.first_child().children("gpu"))
{
i+=1;
}

return i;
}
};

struct GpuInfo::getGPUMemoryUsage_nvidiasmi{
std::vector<double> operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

std::string local_string_gpu_memory_used;

std::vector<double> gpu_memory_used;

pugi::xml_node node_inside;

for (pugi::xml_node gpu: doc.first_child().children("gpu"))
{
local_string_gpu_memory_used = gpu.find_node(find_memory_xml).find_node(find_memory_used_xml).first_child().value();
gpu_memory_used.push_back(std::stod(local_string_gpu_memory_used.erase(local_string_gpu_memory_used.find(" MiB"))));
}

//double sum_used_memory = std::accumulate(gpu_memory_used.begin(), gpu_memory_used.end(), decltype(gpu_memory_used)::value_type(0))/1e3;

return gpu_memory_used;

}
};

struct GpuInfo::getGPUMemoryTotal_nvidiasmi{
std::vector<double> operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

std::string local_string_gpu_memory_total;

std::vector<double> gpu_memory_total;

pugi::xml_node node_inside;

for (pugi::xml_node gpu: doc.first_child().children("gpu"))
{
local_string_gpu_memory_total = gpu.find_node(find_memory_xml).find_node(find_memory_tol_xml).first_child().value();
gpu_memory_total.push_back(std::stod(local_string_gpu_memory_total.erase(local_string_gpu_memory_total.find(" MiB"))));
}

//double sum_used_memory = std::accumulate(gpu_memory_used.begin(), gpu_memory_used.end(), decltype(gpu_memory_used)::value_type(0))/1e3;

return gpu_memory_total;

}
};

struct GpuInfo::getGPUUsage_nvidiasmi{
std::vector<double> operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

std::string local_string_gpu_utilisation;

std::vector<double> gpu_utilisation;

pugi::xml_node node_inside;

for (pugi::xml_node gpu: doc.first_child().children("gpu"))
{

//local_string_gpu_memory_total
local_string_gpu_utilisation = gpu.find_node(find_gpu_utilisation_xml).find_node(find_gpu_used_xml).first_child().value();
gpu_utilisation.push_back(std::stod(local_string_gpu_utilisation.erase(local_string_gpu_utilisation.find(" %"))));

}

//double sum_used_memory = std::accumulate(gpu_memory_used.begin(), gpu_memory_used.end(), decltype(gpu_memory_used)::value_type(0))/1e3;

return gpu_utilisation;

}
};

struct GpuInfo::getGPUName_nvidiasmi{
QStringList operator()(GpuInfo gi) const
{
pugi::xml_document doc;
doc = gi.getXMLnvidiasmi();

std::string local_string_gpu_name;
std::string local_string_gpu_serial;
std::string space = "__";

QStringList gpu_names;

pugi::xml_node node_inside;

for (pugi::xml_node gpu: doc.first_child().children("gpu"))
{

//local_string_gpu_memory_total
local_string_gpu_name = gpu.find_node(find_gpu_name_xml).first_child().value();
local_string_gpu_serial = gpu.find_node(find_gpu_serial_xml).first_child().value();
gpu_names.push_back(QString::fromStdString(local_string_gpu_name + space + local_string_gpu_serial));

}

//double sum_used_memory = std::accumulate(gpu_memory_used.begin(), gpu_memory_used.end(), decltype(gpu_memory_used)::value_type(0))/1e3;

return gpu_names;

}
};

int GpuInfo::load_correct_version()
{

//std::string string_response;
std::string string_response = exec_gpu("nvidia-smi");
size_t location = string_response.find("NVIDIA-SMI");
bool function_running = location != std::string::npos;
if (function_running)
{ // the nvidia-smi command is available
this->getGPUDriverVersion = getGPUDriverVersion_nvidiasmi();
this->getGPUAcceleratorVersion = getGPUCudaVersion_nvidiasmi();
this->getGPUPhysicalCoreCount = getGPUPhysicalCoreCount_nvidiasmi();
this->getGPUMemoryUsage = getGPUMemoryUsage_nvidiasmi();
this->getGPUMemoryTotal = getGPUMemoryTotal_nvidiasmi();
this->getGPUUsage = getGPUUsage_nvidiasmi();
this->getGPUName = getGPUName_nvidiasmi();
this->finding_GPU_info = true;
return 1;
}

return 0;
}
50 changes: 50 additions & 0 deletions stacer-core/Info/gpu_info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#ifndef GPUINFO_H
#define GPUINFO_H

#include "pugixml.hpp"


#include <functional>
#include <QDebug>
#include <QVector>
#include <QString>
#include <string>

#include "Utils/file_util.h"

#include "stacer-core_global.h"

class STACERCORESHARED_EXPORT GpuInfo
{
public:

GpuInfo();

int load_correct_version();

std::function<QString(GpuInfo)> getGPUDriverVersion;
std::function<QString(GpuInfo)> getGPUAcceleratorVersion;
std::function<int(GpuInfo)> getGPUPhysicalCoreCount;
std::function<QList<int>(GpuInfo)> getGPUPercents;
std::function<std::vector<double>(GpuInfo)> getGPUMemoryUsage;
std::function<std::vector<double>(GpuInfo)> getGPUMemoryTotal;
std::function<std::vector<double>(GpuInfo)> getGPUUsage;
std::function<QStringList(GpuInfo)> getGPUName;
bool finding_GPU_info = false;
// QList<double> getLoadAvgs() const;
// double getAvgClock() const;
// QList<double> getClocks() const;

private:
pugi::xml_document getXMLnvidiasmi() const;
struct getGPUDriverVersion_nvidiasmi;
struct getGPUCudaVersion_nvidiasmi;
struct getGPUPhysicalCoreCount_nvidiasmi;
struct getGPUMemoryUsage_nvidiasmi;
struct getGPUMemoryTotal_nvidiasmi;
struct getGPUUsage_nvidiasmi;
struct getGPUName_nvidiasmi;
//int getCpuPercent(const QList<double> &cpuTimes, const int &processor = 0) const;
};

#endif // CPUINFO_H
43 changes: 43 additions & 0 deletions stacer-core/Info/system_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ SystemInfo::SystemInfo()
CpuInfo ci;
this->cpuCore = QString::number(ci.getCpuPhysicalCoreCount());

GpuInfo gi;
this->sucessGPUInfo = gi.finding_GPU_info;
if(gi.finding_GPU_info){
this->gpuNode = gi.getGPUPhysicalCoreCount(gi);
this->gpuNodeString = QString::number(this->gpuNode);
this->GPUDriverVersion = gi.getGPUDriverVersion(gi);
this->GPUAcceleratorVersion = gi.getGPUAcceleratorVersion(gi);
this->GPUTotalMemory = gi.getGPUMemoryTotal(gi);
this->gpuNames = gi.getGPUName(gi);
}



// get username
QString name = qgetenv("USER");

Expand All @@ -58,6 +71,36 @@ SystemInfo::SystemInfo()
this->username = name;
}

std::vector<double> SystemInfo::getGPUTotalMemory() const
{
return GPUTotalMemory;
}

QString SystemInfo::getGPUCoreString() const
{
return gpuNodeString;
}

QString SystemInfo::getGPUDriverVersion() const
{
return GPUDriverVersion;
}

QStringList SystemInfo::getGPUName() const
{
return gpuNames;
}

QString SystemInfo::getGPUAcceleratorVersion() const
{
return GPUAcceleratorVersion;
}

int SystemInfo::getGPUCore() const
{
return gpuNode;
}

QString SystemInfo::getUsername() const
{
return username;
Expand Down
Loading