Cleanup rocm_smi.cc
Change-Id: Ia676c237222b0dd5d9e8a054a93776f3b11e2225 Signed-off-by: Galantsev, Dmitrii <dmitrii.galantsev@amd.com>
This commit is contained in:
کامیت شده توسط
Dmitrii Galantsev
والد
fab0542ab1
کامیت
4aef767596
+53
-46
@@ -40,26 +40,26 @@
|
||||
* DEALINGS WITH THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <glob.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <regex>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "rocm_smi/rocm_smi.h"
|
||||
#include "rocm_smi/rocm_smi_utils.h"
|
||||
@@ -137,7 +137,7 @@ std::vector<std::string> globFilesExist(const std::string& filePattern) {
|
||||
glob_t result_glob;
|
||||
memset(&result_glob, 0, sizeof(result_glob));
|
||||
|
||||
if (glob(filePattern.c_str(), GLOB_TILDE, NULL, &result_glob) != 0) {
|
||||
if (glob(filePattern.c_str(), GLOB_TILDE, nullptr, &result_glob) != 0) {
|
||||
globfree(&result_glob);
|
||||
// Leaving below to help debug issues discovering future glob file searches
|
||||
// debugFilesDiscovered(fileNames);
|
||||
@@ -145,7 +145,7 @@ std::vector<std::string> globFilesExist(const std::string& filePattern) {
|
||||
}
|
||||
|
||||
for(size_t i = 0; i < result_glob.gl_pathc; ++i) {
|
||||
fileNames.push_back(std::string(result_glob.gl_pathv[i]));
|
||||
fileNames.emplace_back(result_glob.gl_pathv[i]);
|
||||
}
|
||||
globfree(&result_glob);
|
||||
|
||||
@@ -367,7 +367,7 @@ std::string removeString(const std::string origStr,
|
||||
// defaults to trim stdOut
|
||||
std::pair<bool, std::string> executeCommand(std::string command, bool stdOut) {
|
||||
char buffer[128];
|
||||
std::string stdoutAndErr = "";
|
||||
std::string stdoutAndErr;
|
||||
bool successfulRun = true;
|
||||
command = "stdbuf -i0 -o0 -e0 " + command; // remove stdOut and err buffering
|
||||
|
||||
@@ -397,14 +397,10 @@ std::pair<bool, std::string> executeCommand(std::string command, bool stdOut) {
|
||||
return std::make_pair(successfulRun, stdoutAndErr);
|
||||
}
|
||||
|
||||
// originalstring - string to search for substring
|
||||
// originalString - string to search for substring
|
||||
// substring - string looking to find
|
||||
bool containsString(std::string originalString, std::string substring) {
|
||||
if (originalString.find(substring) != std::string::npos) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return (originalString.find(substring) != std::string::npos);
|
||||
}
|
||||
|
||||
// Creates and stores supplied data into a temporary file (within /tmp/).
|
||||
@@ -415,9 +411,9 @@ bool containsString(std::string originalString, std::string substring) {
|
||||
// https://man7.org/linux/man-pages/man3/mkstemp.3.html
|
||||
//
|
||||
// Temporary file name format:
|
||||
// <app prefix>_<state name>_<paramenter name>_<device id>
|
||||
// <app prefix>_<state name>_<parameter name>_<device id>
|
||||
// <app prefix> - prefix for our application's identifier (see kTmpFilePrefix)
|
||||
// <paramenter name> - name of parameter being stored
|
||||
// <parameter name> - name of parameter being stored
|
||||
// <state name> - state at which the stored value captures
|
||||
// <device index> - device identifier
|
||||
//
|
||||
@@ -452,9 +448,8 @@ rsmi_status_t storeTmpFile(uint32_t dv_ind, std::string parameterName,
|
||||
close(fd);
|
||||
if (rc_write == -1) {
|
||||
return RSMI_STATUS_FILE_ERROR;
|
||||
} else {
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
}
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
std::vector<std::string> getListOfAppTmpFiles() {
|
||||
@@ -463,16 +458,18 @@ std::vector<std::string> getListOfAppTmpFiles() {
|
||||
struct dirent *ent;
|
||||
std::vector<std::string> tmpFiles;
|
||||
|
||||
if ((dir = opendir(path.c_str())) != nullptr) {
|
||||
// captures all files & directories under specified path
|
||||
while ((ent = readdir(dir)) != nullptr) {
|
||||
std::string fileDirName = ent->d_name;
|
||||
// we only want our app specific files
|
||||
if (containsString(fileDirName, kTmpFilePrefix)) {
|
||||
tmpFiles.emplace_back(path + "/" + fileDirName);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
dir = opendir(path.c_str());
|
||||
if (dir == nullptr) {
|
||||
return tmpFiles;
|
||||
}
|
||||
// captures all files & directories under specified path
|
||||
while ((ent = readdir(dir)) != nullptr) {
|
||||
std::string fileDirName = ent->d_name;
|
||||
// we only want our app specific files
|
||||
if (containsString(fileDirName, kTmpFilePrefix)) {
|
||||
tmpFiles.emplace_back(path + "/" + fileDirName);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return tmpFiles;
|
||||
@@ -501,7 +498,7 @@ std::vector<std::string> readEntireFile(std::string path) {
|
||||
std::string line;
|
||||
while (std::getline(inFileStream, line)) {
|
||||
std::istringstream ss(line);
|
||||
if(line.size() > 0) {
|
||||
if (!line.empty()) {
|
||||
fileContent.push_back(line);
|
||||
}
|
||||
}
|
||||
@@ -513,7 +510,7 @@ std::vector<std::string> readEntireFile(std::string path) {
|
||||
// and their content
|
||||
void displayAppTmpFilesContent() {
|
||||
std::vector<std::string> tmpFiles = getListOfAppTmpFiles();
|
||||
if (tmpFiles.empty() == false) {
|
||||
if (!tmpFiles.empty()) {
|
||||
for (auto &x: tmpFiles) {
|
||||
std::string out = readFile(x);
|
||||
std::cout << __PRETTY_FUNCTION__ << " | Temporary file: " << x
|
||||
@@ -529,7 +526,7 @@ void displayAppTmpFilesContent() {
|
||||
std::string debugVectorContent(std::vector<std::string> v) {
|
||||
std::ostringstream ss;
|
||||
ss << "Vector = {";
|
||||
if (v.size() > 0) {
|
||||
if (!v.empty()) {
|
||||
for (auto it=v.begin(); it < v.end(); it++) {
|
||||
ss << *it;
|
||||
auto temp_it = it;
|
||||
@@ -547,7 +544,7 @@ std::string debugVectorContent(std::vector<std::string> v) {
|
||||
std::string displayAllDevicePaths(std::vector<std::shared_ptr<Device>> v) {
|
||||
std::ostringstream ss;
|
||||
ss << "Vector = {";
|
||||
if (v.size() > 0) {
|
||||
if (!v.empty()) {
|
||||
for (auto it=v.begin(); it < v.end(); it++) {
|
||||
ss << (*it)->path();
|
||||
auto temp_it = it;
|
||||
@@ -562,7 +559,7 @@ std::string displayAllDevicePaths(std::vector<std::shared_ptr<Device>> v) {
|
||||
}
|
||||
|
||||
// Attempts to read application specific temporary file
|
||||
// This method is to be used for reading (or determing if it exists),
|
||||
// This method is to be used for reading (or determining if it exists),
|
||||
// in order to keep file naming scheme consistent.
|
||||
//
|
||||
// dv_ind - device index
|
||||
@@ -580,7 +577,7 @@ std::tuple<bool, std::string> readTmpFile(uint32_t dv_ind,
|
||||
"_" + std::to_string(dv_ind);
|
||||
std::string fileContent;
|
||||
std::vector<std::string> tmpFiles = getListOfAppTmpFiles();
|
||||
if (tmpFiles.empty() == false) {
|
||||
if (!tmpFiles.empty()) {
|
||||
for (auto &x: tmpFiles) {
|
||||
if (containsString(x, tmpFileName)) {
|
||||
fileContent = readFile(x);
|
||||
@@ -620,7 +617,11 @@ std::tuple<bool, std::string, std::string, std::string, std::string,
|
||||
struct utsname buf;
|
||||
bool errorDetected = false;
|
||||
std::string temp_data;
|
||||
std::string sysname, nodename, release, version, machine;
|
||||
std::string sysname;
|
||||
std::string nodename;
|
||||
std::string release;
|
||||
std::string version;
|
||||
std::string machine;
|
||||
std::string domainName = "<undefined>";
|
||||
std::string os_distribution = "<undefined>";
|
||||
std::string endianness = "<undefined>";
|
||||
@@ -640,7 +641,7 @@ std::tuple<bool, std::string, std::string, std::string, std::string,
|
||||
|
||||
std::string filePath = "/etc/os-release";
|
||||
bool fileExists = FileExists(filePath.c_str());
|
||||
if (fileExists == true) {
|
||||
if (fileExists) {
|
||||
std::vector<std::string> fileContent = readEntireFile(filePath);
|
||||
for (auto &line: fileContent) {
|
||||
if (line.find("PRETTY_NAME=") != std::string::npos) {
|
||||
@@ -668,11 +669,17 @@ std::tuple<bool, std::string, std::string, std::string, std::string,
|
||||
void logSystemDetails(void) {
|
||||
std::ostringstream ss;
|
||||
bool errorDetected;
|
||||
std::string sysname, node, release, version, machine, domain, distName,
|
||||
endianness;
|
||||
std::string sysname;
|
||||
std::string node;
|
||||
std::string release;
|
||||
std::string version;
|
||||
std::string machine;
|
||||
std::string domain;
|
||||
std::string distName;
|
||||
std::string endianness;
|
||||
std::tie(errorDetected, sysname, node, release, version, machine, domain,
|
||||
distName, endianness) = getSystemDetails();
|
||||
if (errorDetected == false) {
|
||||
if (!errorDetected) {
|
||||
ss << "====== Gathered system details ============\n"
|
||||
<< "SYSTEM NAME: " << sysname << "\n"
|
||||
<< "OS DISTRIBUTION: " << distName << "\n"
|
||||
@@ -710,7 +717,7 @@ void logHexDump(
|
||||
|
||||
// Output description if given.
|
||||
// if (desc != NULL) printf("%s:\n", desc);
|
||||
if (desc != NULL) ss << "\n" << desc << "\n";
|
||||
if (desc != nullptr) ss << "\n" << desc << "\n";
|
||||
|
||||
// Length checks.
|
||||
if (len == 0) {
|
||||
|
||||
مرجع در شماره جدید
Block a user