Files
rocm-systems/tests/amd_smi_test/functional/init_shutdown_refcount.cc
T
Dalibor Stanisavljevic b4b761d02f SWDEV-370223 - Change the name of the header to amdsmi.h
Change dev to device_handle throughout the file
Change the pcie_info pcie_speed field type to uint32_t
Add AMDSMI prefix before amdsmi_mm_ip enum

Change-Id: I242145389ddc3f2ad05dfd6ca371640f4d118fc4
Signed-off-by: Dalibor Stanisavljevic <Dalibor.Stanisavljevic@amd.com>
2022-12-08 13:34:34 +01:00

241 行
7.6 KiB
C++
可執行檔

/*
* =============================================================================
* ROC Runtime Conformance Release License
* =============================================================================
* The University of Illinois/NCSA
* Open Source License (NCSA)
*
* Copyright (c) 2022, 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 <pthread.h>
#include <algorithm>
#include <iostream>
#include <thread> // NOLINT
#include <random>
#include <chrono> // NOLINT
#include "init_shutdown_refcount.h"
#include <gtest/gtest.h>
#include "amd_smi/amdsmi.h"
#include "../test_common.h"
extern int32_t
rsmi_test_refcount(uint64_t refcnt_type);
static void rand_sleep_mod(int msec) {
assert(msec > 10);
unsigned int seed = time(NULL);
std::mt19937_64 eng{seed};
std::uniform_int_distribution<> dist{10, msec};
std::this_thread::sleep_for(std::chrono::milliseconds{dist(eng)});
}
static void* AMDSMIInitFunction(void* args) {
amdsmi_status_t status;
(void)args;
rand_sleep_mod(100);
status = amdsmi_init(AMDSMI_INIT_AMD_GPUS);
EXPECT_EQ(AMDSMI_STATUS_SUCCESS, status);
pthread_exit(nullptr);
return nullptr;
}
static void* AMDSMIShutDownFunction(void* args) {
amdsmi_status_t status;
(void)args;
rand_sleep_mod(100);
status = amdsmi_shut_down();
EXPECT_EQ(AMDSMI_STATUS_SUCCESS, status);
pthread_exit(nullptr);
return nullptr;
}
static void *AMDSMIInitShutDownFunction(void* args) {
amdsmi_status_t status;
(void)args;
rand_sleep_mod(100);
status = amdsmi_init(AMDSMI_INIT_AMD_GPUS);
EXPECT_EQ(AMDSMI_STATUS_SUCCESS, status);
rand_sleep_mod(100);
status = amdsmi_shut_down();
EXPECT_EQ(AMDSMI_STATUS_SUCCESS, status);
pthread_exit(nullptr);
return nullptr;
}
static const int NumOfThreads = 100;
TestConcurrentInit::TestConcurrentInit(void) : TestBase() {
set_title("AMDSMI Concurrent Init Test");
set_description("This test initializes AMDSMI concurrently to verify "
"reference counting functionality.");
}
TestConcurrentInit::~TestConcurrentInit(void) {
}
void TestConcurrentInit::SetUp(void) {
// TestBase::SetUp(); // Skip usual SetUp to avoid doing the usual amdsmi_init
return;
}
// Compare required profile for this test case with what we're actually
// running on
void TestConcurrentInit::DisplayTestInfo(void) {
IF_VERB(STANDARD) {
TestBase::DisplayTestInfo();
}
return;
}
void TestConcurrentInit::DisplayResults(void) const {
IF_VERB(STANDARD) {
TestBase::DisplayResults();
}
return;
}
void TestConcurrentInit::Close() {
// This will close handles opened within rsmitst utility calls and call
// amdsmi_shut_down(), so it should be done after other hsa cleanup
TestBase::Close();
}
// Compare required profile for this test case with what we're actually
// running on
void TestConcurrentInit::Run(void) {
if (setup_failed_) {
IF_VERB(STANDARD) {
std::cout << "** SetUp Failed for this test. Skipping.**" << std::endl;
}
return;
}
pthread_t ThreadId[NumOfThreads];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
IF_VERB(STANDARD) {
std::cout << "Testing concurrent amdsmi_init()..." << std::endl;
}
for (int Id = 0; Id < NumOfThreads; ++Id) {
int ThreadStatus = pthread_create(&ThreadId[Id], &attr,
AMDSMIInitFunction, nullptr);
ASSERT_EQ(0, ThreadStatus) << "pthead_create failed.";
}
for (int Id = 0; Id < NumOfThreads; ++Id) {
int err = pthread_join(ThreadId[Id], nullptr);
ASSERT_EQ(0, err) << "pthread_join failed.";
}
// Invoke hsa_shut_down and verify that all the hsa_init's were counted.
// HSA should be exactly closed after NumOfThreads calls.
for (int Id = 0; Id < NumOfThreads; ++Id) {
amdsmi_status_t err = amdsmi_shut_down();
ASSERT_EQ(AMDSMI_STATUS_SUCCESS, err) << "An amdsmi_init was missed.";
}
amdsmi_status_t err = amdsmi_shut_down();
ASSERT_EQ(AMDSMI_STATUS_INIT_ERROR, err) <<
"amdsmi_init reference count was too high.";
int32_t refcnt = rsmi_test_refcount(0);
ASSERT_EQ(0, refcnt);
IF_VERB(STANDARD) {
std::cout << "Concurrent amdsmi_init() test passed." <<
std::endl << std::endl;
std::cout << "Testing concurrent amdsmi_shut_down()..." << std::endl;
}
// Invoke hsa_shut_down and verify that all the hsa_init's were counted.
// HSA should be exactly closed after NumOfThreads calls.
for (int Id = 0; Id < NumOfThreads; ++Id) {
amdsmi_status_t err = amdsmi_init(AMDSMI_INIT_AMD_GPUS);
ASSERT_EQ(AMDSMI_STATUS_SUCCESS, err);
}
for (int Id = 0; Id < NumOfThreads; ++Id) {
int ThreadStatus =
pthread_create(&ThreadId[Id], &attr, AMDSMIShutDownFunction, nullptr);
ASSERT_EQ(0, ThreadStatus) << "pthead_create failed.";
}
for (int Id = 0; Id < NumOfThreads; ++Id) {
int err = pthread_join(ThreadId[Id], nullptr);
ASSERT_EQ(0, err) << "pthread_join failed.";
}
refcnt = rsmi_test_refcount(0);
ASSERT_EQ(0, refcnt);
IF_VERB(STANDARD) {
std::cout << "Concurrent amdsmi_shut_down() passed." << std::endl;
std::cout <<
"Testing concurrent amdsmi_init() followed by amdsmi_shut_down()..." <<
std::endl;
}
for (int Id = 0; Id < NumOfThreads; ++Id) {
int ThreadStatus =
pthread_create(&ThreadId[Id], &attr, AMDSMIInitShutDownFunction, nullptr);
ASSERT_EQ(0, ThreadStatus) << "pthead_create failed.";
}
for (int Id = 0; Id < NumOfThreads; ++Id) {
int err = pthread_join(ThreadId[Id], nullptr);
ASSERT_EQ(0, err) << "pthread_join failed.";
}
refcnt = rsmi_test_refcount(0);
ASSERT_EQ(0, refcnt);
IF_VERB(STANDARD) {
std::cout <<
"Concurrent amdsmi_init() followed by amdsmi_shut_down() passed." <<
std::endl;
}
}