Add initial support for getting process information

Added implementation of and tests for
rsmi_dev_compute_process_info_by_pid_get() and
rsmi_dev_compute_process_info_get()

Change-Id: I4c4f5f39fe6701da37916c9ad41449b5d35ac7af
This commit is contained in:
Chris Freehill
2019-07-03 11:29:19 -05:00
parent 1c5e090507
commit 9b93cbe21d
14 changed files with 660 additions and 49 deletions
+50
View File
@@ -62,6 +62,7 @@
#include "rocm_smi/rocm_smi_utils.h"
#include "rocm_smi/rocm_smi_exception.h"
#include "rocm_smi/rocm_smi_counters.h"
#include "rocm_smi/rocm_smi_kfd.h"
#include "rocm_smi/rocm_smi64Config.h"
@@ -130,6 +131,7 @@ static pthread_mutex_t *get_mutex(uint32_t dv_ind) {
static rsmi_status_t errno_to_rsmi_status(uint32_t err) {
switch (err) {
case 0: return RSMI_STATUS_SUCCESS;
case ESRCH: return RSMI_STATUS_NOT_FOUND;
case EACCES: return RSMI_STATUS_PERMISSION;
case EPERM:
case ENOENT: return RSMI_STATUS_NOT_SUPPORTED;
@@ -2299,3 +2301,51 @@ rsmi_dev_counter_group_supported(uint32_t dv_ind, rsmi_event_group_t group) {
CATCH
}
rsmi_status_t
rsmi_compute_process_info_get(rsmi_process_info_t *procs,
uint32_t *num_items) {
TRY
if (num_items == nullptr) {
return RSMI_STATUS_INVALID_ARGS;
}
uint32_t procs_found = 0;
int err = amd::smi:: GetProcessInfo(procs, *num_items, &procs_found);
if (err) {
return errno_to_rsmi_status(err);
}
if (procs && *num_items < procs_found) {
return RSMI_STATUS_INSUFFICIENT_SIZE;
}
if (procs == nullptr || *num_items > procs_found) {
*num_items = procs_found;
}
return RSMI_STATUS_SUCCESS;
CATCH
}
rsmi_status_t
rsmi_compute_process_info_by_pid_get(uint32_t pid,
rsmi_process_info_t *proc) {
TRY
if (proc == nullptr) {
return RSMI_STATUS_INVALID_ARGS;
}
int err = amd::smi::GetProcessInfoForPID(pid, proc);
if (err) {
return errno_to_rsmi_status(err);
}
return RSMI_STATUS_SUCCESS;
CATCH
}
+1 -14
View File
@@ -60,6 +60,7 @@
#include "rocm_smi/rocm_smi_device.h"
#include "rocm_smi/rocm_smi.h"
#include "rocm_smi/rocm_smi_exception.h"
#include "rocm_smi/rocm_smi_utils.h"
extern "C" {
#include "shared_mutex.h" // NOLINT
@@ -159,20 +160,6 @@ static const std::map<rsmi_dev_perf_level, const char *> kDevPerfLvlMap = {
{RSMI_DEV_PERF_LEVEL_UNKNOWN, kDevPerfLevelUnknownStr},
};
static int isRegularFile(std::string fname, bool *is_reg) {
struct stat file_stat;
int ret;
assert(is_reg != nullptr);
ret = stat(fname.c_str(), &file_stat);
if (ret) {
return errno;
}
*is_reg = S_ISREG(file_stat.st_mode);
return 0;
}
#define RET_IF_NONZERO(X) { \
if (X) return X; \
}
+161
View File
@@ -0,0 +1,161 @@
/*
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2019, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Developed by:
*
* AMD Research and AMD ROC Software Development
*
* Advanced Micro Devices, Inc.
*
* www.amd.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal with the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in
* the documentation and/or other materials provided with the distribution.
* - Neither the names of <Name of Development Group, Name of Institution>,
* nor the names of its contributors may be used to endorse or promote
* products derived from this Software without specific prior written
* permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <assert.h>
#include <sys/stat.h>
#include <dirent.h>
#include <algorithm>
#include <string>
#include <map>
#include <fstream>
#include <cstdint>
#include <iostream>
#include <sstream>
#include "rocm_smi/rocm_smi_kfd.h"
#include "rocm_smi/rocm_smi.h"
#include "rocm_smi/rocm_smi_exception.h"
#include "rocm_smi/rocm_smi_utils.h"
namespace amd {
namespace smi {
static const char *kKFDProcPathRoot = "/sys/class/kfd/kfd/proc";
// Sysfs file names
static const char *kKFDPasidFName = "pasid";
static bool is_number(const std::string &s) {
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
}
int GetProcessInfo(rsmi_process_info_t *procs, uint32_t num_allocated,
uint32_t *num_procs_found) {
assert(num_procs_found != nullptr);
*num_procs_found = 0;
errno = 0;
auto proc_dir = opendir(kKFDProcPathRoot);
if (proc_dir == nullptr) {
perror("Unable to open process directory");
return errno;
}
auto dentry = readdir(proc_dir);
std::string prod_id_str;
std::string tmp;
while (dentry != nullptr) {
if (dentry->d_name[0] == '.') {
dentry = readdir(proc_dir);
continue;
}
prod_id_str = dentry->d_name;
assert(is_number(prod_id_str) && "Unexpected file name in kfd/proc dir");
if (!is_number(prod_id_str)) {
continue;
}
if (procs && *num_procs_found < num_allocated) {
int err;
std::string tmp;
procs[*num_procs_found].process_id = std::stoi(prod_id_str);
std::string pasid_str_path = kKFDProcPathRoot;
pasid_str_path += "/";
pasid_str_path += prod_id_str;
pasid_str_path += "/";
pasid_str_path += kKFDPasidFName;
err = ReadSysfsStr(pasid_str_path, &tmp);
if (err) {
return err;
}
assert(is_number(tmp) && "Unexpected value in pasid file");
procs[*num_procs_found].pasid = std::stoi(tmp);
}
++(*num_procs_found);
dentry = readdir(proc_dir);
}
errno = 0;
if (closedir(proc_dir)) {
return errno;
}
return 0;
}
int GetProcessInfoForPID(uint32_t pid, rsmi_process_info_t *proc) {
assert(proc != nullptr);
int err;
std::string tmp;
std::string proc_str_path = kKFDProcPathRoot;
proc_str_path += "/";
proc_str_path += std::to_string(pid);
if (!FileExists(proc_str_path.c_str())) {
return ESRCH;
}
proc->process_id = pid;
std::string pasid_str_path = proc_str_path;
pasid_str_path += "/";
pasid_str_path += kKFDPasidFName;
err = ReadSysfsStr(pasid_str_path, &tmp);
if (err) {
return err;
}
assert(is_number(tmp) && "Unexpected value in pasid file");
proc->pasid = std::stoi(tmp);
return 0;
}
} // namespace smi
} // namespace amd
+1 -32
View File
@@ -39,7 +39,6 @@
* DEALINGS WITH THE SOFTWARE.
*
*/
#include <sys/stat.h>
#include <dirent.h>
#include <assert.h>
#include <string.h>
@@ -61,6 +60,7 @@
#include "rocm_smi/rocm_smi_device.h"
#include "rocm_smi/rocm_smi_main.h"
#include "rocm_smi/rocm_smi_exception.h"
#include "rocm_smi/rocm_smi_utils.h"
static const char *kPathDRMRoot = "/sys/class/drm";
static const char *kPathHWMonRoot = "/sys/class/hwmon";
@@ -73,10 +73,6 @@ static const char *kAMDMonitorTypes[] = {"radeon", "amdgpu", ""};
namespace amd {
namespace smi {
static bool FileExists(char const *filename) {
struct stat buf;
return (stat(filename, &buf) == 0);
}
static uint32_t GetDeviceIndex(const std::string s) {
std::string t = s;
@@ -86,33 +82,6 @@ static uint32_t GetDeviceIndex(const std::string s) {
return stoi(t);
}
// Return 0 if same file, 1 if not, and -1 for error
static int SameFile(const std::string fileA, const std::string fileB) {
struct stat aStat;
struct stat bStat;
int ret;
ret = stat(fileA.c_str(), &aStat);
if (ret) {
return -1;
}
ret = stat(fileB.c_str(), &bStat);
if (ret) {
return -1;
}
if (aStat.st_dev != bStat.st_dev) {
return 1;
}
if (aStat.st_ino != bStat.st_ino) {
return 1;
}
return 0;
}
static int SameDevice(const std::string fileA, const std::string fileB) {
return SameFile(fileA + "/device", fileB + "/device");
}
+47
View File
@@ -42,6 +42,7 @@
*/
#include <assert.h>
#include <errno.h>
#include <sys/stat.h>
#include <fstream>
#include <string>
@@ -53,6 +54,52 @@
namespace amd {
namespace smi {
// Return 0 if same file, 1 if not, and -1 for error
int SameFile(const std::string fileA, const std::string fileB) {
struct stat aStat;
struct stat bStat;
int ret;
ret = stat(fileA.c_str(), &aStat);
if (ret) {
return -1;
}
ret = stat(fileB.c_str(), &bStat);
if (ret) {
return -1;
}
if (aStat.st_dev != bStat.st_dev) {
return 1;
}
if (aStat.st_ino != bStat.st_ino) {
return 1;
}
return 0;
}
bool FileExists(char const *filename) {
struct stat buf;
return (stat(filename, &buf) == 0);
}
int isRegularFile(std::string fname, bool *is_reg) {
struct stat file_stat;
int ret;
assert(is_reg != nullptr);
ret = stat(fname.c_str(), &file_stat);
if (ret) {
return errno;
}
*is_reg = S_ISREG(file_stat.st_mode);
return 0;
}
int WriteSysfsStr(std::string path, std::string val) {
std::ofstream fs;
int ret = 0;