Skip to content
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
13 changes: 12 additions & 1 deletion src/amd_detail/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace hipFile;

Configuration::Configuration() : m_fastpath(true), m_fallback(true)
Configuration::Configuration() : m_fastpath(true), m_fallback(true), m_statsLevel(0)
{
auto maybe_env_force_compat{Environment::force_compat_mode()};
if (maybe_env_force_compat && maybe_env_force_compat.value()) {
Expand All @@ -24,6 +24,11 @@ Configuration::Configuration() : m_fastpath(true), m_fallback(true)
if (maybe_env_allow_compat && !maybe_env_allow_compat.value()) {
m_fallback = false;
}

auto maybe_stats_level{Environment::stats_level()};
if (maybe_stats_level) {
m_statsLevel = maybe_stats_level.value();
}
}

bool
Expand All @@ -37,3 +42,9 @@ Configuration::fallback() const noexcept
{
return m_fallback;
}

unsigned int
Configuration::statsLevel() const noexcept
{
return m_statsLevel;
}
14 changes: 10 additions & 4 deletions src/amd_detail/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,23 @@ class IConfiguration {
/// @brief Checks if the fallback backend is enabled
/// @return true if the fallback backend is enabled, false otherwise
virtual bool fallback() const noexcept = 0;

/// @brief Shows the level of detail for stats collection
/// @return 0 if stats collection disabled, higher levels of detail as value increases
virtual unsigned int statsLevel() const noexcept = 0;
};

class Configuration : public IConfiguration {
bool m_fastpath;
bool m_fallback;
bool m_fastpath;
bool m_fallback;
unsigned int m_statsLevel;

public:
Configuration();

bool fastpath() const noexcept override;
bool fallback() const noexcept override;
bool fastpath() const noexcept override;
bool fallback() const noexcept override;
unsigned int statsLevel() const noexcept override;
};

}
30 changes: 30 additions & 0 deletions src/amd_detail/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include "environment.h"
#include "sys.h"

#include <cerrno>
#include <cstdlib>
#include <limits>
#include <strings.h>

using namespace hipFile;
Expand All @@ -28,6 +31,27 @@ Environment::get<bool>(const char *key)
return std::nullopt;
}

template <>
std::optional<unsigned int>
Environment::get<unsigned int>(const char *key)
{
const char *value{Context<Sys>::get()->getenv(key)};
if (value == nullptr)
return std::nullopt;
errno = 0;
char *end;
unsigned long x{std::strtoul(value, &end, 10)};
if (errno != 0)
return std::nullopt;
if (end == value)
return std::nullopt;
if (*end != '\0')
return std::nullopt;
if (x > static_cast<unsigned long>(std::numeric_limits<unsigned int>::max()))
return std::numeric_limits<unsigned int>::max();
return static_cast<unsigned int>(x);
}

optional<bool>
Environment::allow_compat_mode()
{
Expand All @@ -39,3 +63,9 @@ Environment::force_compat_mode()
{
return Environment::get<bool>(Environment::FORCE_COMPAT_MODE);
}

optional<unsigned int>
Environment::stats_level()
{
return Environment::get<unsigned int>(Environment::STATS_LEVEL);
}
8 changes: 8 additions & 0 deletions src/amd_detail/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class Environment {
static constexpr const char *const FORCE_COMPAT_MODE{"HIPFILE_FORCE_COMPAT_MODE"};

static std::optional<bool> force_compat_mode();

/// @brief Control how much information is collected to be reported to ais-stats
///
/// 0 indicates no stats should be recorded.
/// >= 1 indicates basic stats will be collected.
static constexpr const char *const STATS_LEVEL{"HIPFILE_STATS_LEVEL"};

static std::optional<unsigned int> stats_level();
};

}
27 changes: 27 additions & 0 deletions test/amd_detail/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct ConfigurationExpectationBuilder {
StrictMock<MHip> &m_mhip;
std::optional<const char *> m_env_force_compat_mode;
std::optional<const char *> m_env_allow_compat_mode;
std::optional<const char *> m_env_stats_level;
void *m_hip_amd_file_read{reinterpret_cast<void *>(0xDEADBEEF)};
void *m_hip_amd_file_write{reinterpret_cast<void *>(0x0BADF00D)};

Expand All @@ -47,6 +48,12 @@ struct ConfigurationExpectationBuilder {
return *this;
}

ConfigurationExpectationBuilder &env_stats_level(const char *value)
{
m_env_stats_level = value;
return *this;
}

ConfigurationExpectationBuilder &hip_amd_file_read(void *value)
{
m_hip_amd_file_read = value;
Expand Down Expand Up @@ -81,6 +88,14 @@ struct ConfigurationExpectation {
EXPECT_CALL(builder.m_msys, getenv(StrEq(hipFile::Environment::ALLOW_COMPAT_MODE)));
}

if (builder.m_env_stats_level) {
EXPECT_CALL(builder.m_msys, getenv(StrEq(hipFile::Environment::STATS_LEVEL)))
.WillOnce(Return(const_cast<char *>(builder.m_env_stats_level.value())));
}
else {
EXPECT_CALL(builder.m_msys, getenv(StrEq(hipFile::Environment::STATS_LEVEL)));
}

EXPECT_CALL(builder.m_mhip, hipRuntimeGetVersion).Times(2);
EXPECT_CALL(builder.m_mhip, hipGetProcAddress(StrEq("hipAmdFileRead"), _, _, _))
.WillOnce(Return(builder.m_hip_amd_file_read));
Expand Down Expand Up @@ -148,4 +163,16 @@ TEST_F(HipFileConfiguration, FallbackDisabledIfAllowCompatModeEnvironmentVariabl
ASSERT_FALSE(Configuration().fallback());
}

TEST_F(HipFileConfiguration, StatsLevelEnvironmentVariableIsNotSetOrInvalid)
{
ConfigurationExpectationBuilder{msys, mhip}.build();
ASSERT_EQ(0, Configuration().statsLevel());
}

TEST_F(HipFileConfiguration, StatsLevelEnvironmentVariableIsSet)
{
ConfigurationExpectationBuilder{msys, mhip}.env_stats_level("1").build();
ASSERT_EQ(1, Configuration().statsLevel());
}

HIPFILE_WARN_NO_GLOBAL_CTOR_ON
28 changes: 28 additions & 0 deletions test/amd_detail/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,32 @@ TEST(HipFileEnvironment, GetBoolReturnsOptionalFalseIfFalse)
ASSERT_EQ(hipFile::Environment::get<bool>(""), make_optional<>(false));
}

TEST(HipFileEnvironment, GetUnsignedIntReturnsNulloptIfNotSet)
{
StrictMock<MSys> msys;

EXPECT_CALL(msys, getenv).WillOnce(Return(nullptr));
ASSERT_EQ(hipFile::Environment::get<unsigned int>(""), nullopt);
}

TEST(HipFileEnvironment, GetUnsignedIntReturnsNulloptIfValueIsInvalid)
{
StrictMock<MSys> msys;

EXPECT_CALL(msys, getenv).WillOnce(Return(const_cast<char *>("blah")));
ASSERT_EQ(hipFile::Environment::get<unsigned int>(""), nullopt);
EXPECT_CALL(msys, getenv).WillOnce(Return(const_cast<char *>("")));
ASSERT_EQ(hipFile::Environment::get<unsigned int>(""), nullopt);
EXPECT_CALL(msys, getenv).WillOnce(Return(const_cast<char *>("1abc")));
ASSERT_EQ(hipFile::Environment::get<unsigned int>(""), nullopt);
}

TEST(HipFileEnvironment, GetUnsignedIntReturnsIntIfValueIsInt)
{
StrictMock<MSys> msys;

EXPECT_CALL(msys, getenv).WillOnce(Return(const_cast<char *>("0")));
ASSERT_EQ(hipFile::Environment::get<unsigned int>(""), make_optional<>(0));
}

HIPFILE_WARN_NO_GLOBAL_CTOR_ON
1 change: 1 addition & 0 deletions test/amd_detail/mconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace rocFile {
struct MConfiguration : IConfiguration {
MOCK_METHOD(bool, fastpath, (), (const, noexcept, override));
MOCK_METHOD(bool, fallback, (), (const, noexcept, override));
MOCK_METHOD(unsigned int, statsLevel, (), (const, noexcept, override));
};

}