Add device mutual exclusion tests and related fixes
* Added a new test to verify mutual exclusion of access to device
resources
* Added some missing acquiring of mutexes to some RSMI calls, as
well as try-catch blocks.
Change-Id: I87aac009878a0b2d1f975e1d5b794d887bb23ff9
[ROCm/amdsmi commit: f8b57c3b16]
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
#include <sys/utsname.h>
|
||||
#include <pthread.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
@@ -75,7 +76,6 @@ static rsmi_status_t handleException() {
|
||||
try {
|
||||
throw;
|
||||
} catch (const std::bad_alloc& e) {
|
||||
debug_print("RSMI exception: BadAlloc\n");
|
||||
return RSMI_STATUS_OUT_OF_RESOURCES;
|
||||
} catch (const amd::smi::rsmi_exception& e) {
|
||||
debug_print("Exception caught: %s.\n", e.what());
|
||||
@@ -123,7 +123,12 @@ static rsmi_status_t handleException() {
|
||||
|
||||
#define DEVICE_MUTEX \
|
||||
amd::smi::pthread_wrap _pw(*get_mutex(dv_ind)); \
|
||||
amd::smi::ScopedPthread _lock(_pw);
|
||||
amd::smi::RocmSMI& smi_ = amd::smi::RocmSMI::getInstance(); \
|
||||
bool blocking_ = !(smi_.init_options() && RSMI_INIT_FLAG_RESRV_TEST1); \
|
||||
amd::smi::ScopedPthread _lock(_pw, blocking_); \
|
||||
if (!blocking_ && _lock.mutex_not_acquired()) { \
|
||||
return RSMI_STATUS_BUSY; \
|
||||
}
|
||||
|
||||
/* This group of macros is used to facilitate checking of support for rsmi_dev*
|
||||
* "getter" functions. When the return buffer is set to nullptr, the macro will
|
||||
@@ -1638,10 +1643,13 @@ rsmi_dev_name_get(uint32_t dv_ind, char *name, size_t len) {
|
||||
|
||||
rsmi_status_t
|
||||
rsmi_dev_brand_get(uint32_t dv_ind, char *brand, uint32_t len) {
|
||||
TRY
|
||||
CHK_SUPPORT_NAME_ONLY(brand)
|
||||
if (len == 0) {
|
||||
return RSMI_STATUS_INVALID_ARGS;
|
||||
}
|
||||
DEVICE_MUTEX
|
||||
|
||||
std::map<std::string, std::string> brand_names = {
|
||||
{"D05121", "mi25"},
|
||||
{"D05131", "mi25"},
|
||||
@@ -1676,6 +1684,7 @@ rsmi_dev_brand_get(uint32_t dv_ind, char *brand, uint32_t len) {
|
||||
// If there is no SKU match, return marketing name instead
|
||||
rsmi_dev_name_get(dv_ind, brand, len);
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
CATCH
|
||||
}
|
||||
|
||||
rsmi_status_t
|
||||
@@ -2501,6 +2510,7 @@ rsmi_status_t rsmi_dev_serial_number_get(uint32_t dv_ind,
|
||||
}
|
||||
|
||||
TRY
|
||||
DEVICE_MUTEX
|
||||
|
||||
std::string val_str;
|
||||
rsmi_status_t ret = get_dev_value_str(amd::smi::kDevSerialNumber,
|
||||
@@ -3146,3 +3156,23 @@ rsmi_func_iter_next(rsmi_func_id_iter_handle_t handle) {
|
||||
|
||||
CATCH
|
||||
}
|
||||
|
||||
// UNDOCUMENTED FUNCTIONS
|
||||
// This functions are not declared in rocm_smi.h. They are either not fully
|
||||
// supported, or to be used for test purposes.
|
||||
|
||||
// This function acquires a mutex and waits for a number of seconds
|
||||
rsmi_status_t
|
||||
rsmi_test_sleep(uint32_t dv_ind, uint32_t seconds) {
|
||||
// DEVICE_MUTEX
|
||||
amd::smi::pthread_wrap _pw(*get_mutex(dv_ind));
|
||||
amd::smi::RocmSMI& smi_ = amd::smi::RocmSMI::getInstance();
|
||||
bool blocking_ = !(smi_.init_options() && RSMI_INIT_FLAG_RESRV_TEST1);
|
||||
amd::smi::ScopedPthread _lock(_pw, blocking_);
|
||||
if (!blocking_ && _lock.mutex_not_acquired()) {
|
||||
return RSMI_STATUS_BUSY;
|
||||
}
|
||||
|
||||
sleep(seconds);
|
||||
return RSMI_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,26 @@
|
||||
// NOLINT(legal/copyright)
|
||||
/*
|
||||
Modifications Copyright © 2019 – 2020 Advanced Micro Devices, Inc. All Rights
|
||||
Reserved.
|
||||
Copyright (c) 2018 Oleg Yamnikov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in 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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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
|
||||
AUTHORS 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 IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
#include "shared_mutex.h" // NOLINT(build/include)
|
||||
#include <errno.h> // errno, ENOENT
|
||||
#include <fcntl.h> // O_RDWR, O_CREATE
|
||||
@@ -59,7 +81,7 @@ shared_mutex_t shared_mutex_init(const char *name, mode_t mode) {
|
||||
pthread_mutex_t *mutex_ptr = reinterpret_cast<pthread_mutex_t *>(addr);
|
||||
|
||||
// Make sure the mutex wasn't left in a locked state. If we can't
|
||||
// acquire it in 3 sec., re-do everything.
|
||||
// acquire it in 5 sec., re-do everything.
|
||||
struct timespec expireTime;
|
||||
clock_gettime(CLOCK_REALTIME, &expireTime);
|
||||
expireTime.tv_sec += 5;
|
||||
@@ -75,7 +97,7 @@ shared_mutex_t shared_mutex_init(const char *name, mode_t mode) {
|
||||
" /dev/shm.");
|
||||
free(mutex.name);
|
||||
|
||||
throw amd::smi::rsmi_exception(RSMI_STATUS_RESOURCE_BUSY, __FUNCTION__);
|
||||
throw amd::smi::rsmi_exception(RSMI_STATUS_BUSY, __FUNCTION__);
|
||||
return mutex;
|
||||
} else {
|
||||
if (pthread_mutex_unlock(mutex_ptr)) {
|
||||
|
||||
@@ -1,5 +1,26 @@
|
||||
// NOLINT(legal/copyright)
|
||||
// See LICENSE file
|
||||
/*
|
||||
Modifications Copyright © 2019 – 2020 Advanced Micro Devices, Inc. All Rights
|
||||
Reserved.
|
||||
Copyright (c) 2018 Oleg Yamnikov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in 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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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
|
||||
AUTHORS 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 IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_SHARED_MUTEX_SHARED_MUTEX_H_
|
||||
#define SRC_SHARED_MUTEX_SHARED_MUTEX_H_
|
||||
|
||||
Reference in New Issue
Block a user