2022-02-08 17:42:17 -06:00
|
|
|
// MIT License
|
|
|
|
|
//
|
2025-01-15 13:06:12 -05:00
|
|
|
// Copyright (c) 2022-2025 Advanced Micro Devices, Inc. All Rights Reserved.
|
2022-02-08 17:42:17 -06:00
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2023-02-04 10:59:50 -06:00
|
|
|
#include "state.hpp"
|
2024-12-13 18:48:39 -05:00
|
|
|
#include "common/static_object.hpp"
|
2023-02-04 10:59:50 -06:00
|
|
|
#include "config.hpp"
|
|
|
|
|
#include "utility.hpp"
|
2022-02-08 17:42:17 -06:00
|
|
|
|
2026-01-14 21:27:51 +01:00
|
|
|
#include "logger/debug.hpp"
|
|
|
|
|
|
2023-02-27 12:09:03 -06:00
|
|
|
#include <atomic>
|
2022-02-08 17:42:17 -06:00
|
|
|
#include <string>
|
|
|
|
|
|
2024-10-15 11:20:40 -04:00
|
|
|
namespace rocprofsys
|
2022-11-11 07:31:14 -06:00
|
|
|
{
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
auto&
|
|
|
|
|
get_state_value()
|
|
|
|
|
{
|
2024-12-13 18:48:39 -05:00
|
|
|
static auto*& _v = common::static_object<std::atomic<State>>::construct(
|
|
|
|
|
common::do_not_destroy{}, State::PreInit);
|
|
|
|
|
return *_v;
|
2022-11-11 07:31:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadState&
|
|
|
|
|
get_thread_state_value()
|
|
|
|
|
{
|
2023-02-27 12:09:03 -06:00
|
|
|
static thread_local auto _v = ThreadState{ ThreadState::Enabled };
|
2022-11-11 07:31:14 -06:00
|
|
|
return _v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto&
|
|
|
|
|
get_thread_state_history(int64_t _idx = utility::get_thread_index())
|
|
|
|
|
{
|
2024-10-15 11:20:40 -04:00
|
|
|
static auto _v = utility::get_filled_array<ROCPROFSYS_MAX_THREADS>(
|
2022-11-11 07:31:14 -06:00
|
|
|
[]() { return utility::get_reserved_vector<ThreadState>(32); });
|
|
|
|
|
|
2024-10-15 11:20:40 -04:00
|
|
|
if(_idx >= ROCPROFSYS_MAX_THREADS)
|
2023-01-24 18:53:23 -06:00
|
|
|
{
|
|
|
|
|
static thread_local auto _tl_v = utility::get_reserved_vector<ThreadState>(32);
|
|
|
|
|
return _tl_v;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-11 07:31:14 -06:00
|
|
|
return _v.at(_idx);
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
State
|
|
|
|
|
get_state()
|
|
|
|
|
{
|
2023-02-27 12:09:03 -06:00
|
|
|
return get_state_value().load(std::memory_order_relaxed);
|
2022-11-11 07:31:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadState
|
|
|
|
|
get_thread_state()
|
|
|
|
|
{
|
|
|
|
|
return get_thread_state_value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
State
|
|
|
|
|
set_state(State _n)
|
|
|
|
|
{
|
2026-01-14 21:27:51 +01:00
|
|
|
if(get_debug_init())
|
|
|
|
|
{
|
|
|
|
|
LOG_DEBUG("Setting state :: {} -> {}", std::to_string(get_state()),
|
|
|
|
|
std::to_string(_n));
|
|
|
|
|
}
|
2022-11-11 07:31:14 -06:00
|
|
|
// state should always be increased, not decreased
|
2026-01-14 21:27:51 +01:00
|
|
|
if(get_is_continuous_integration() && _n < get_state())
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
fmt::format("State is being assigned to a lesser value :: {} -> {}",
|
|
|
|
|
std::to_string(get_state()), std::to_string(_n)));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 12:09:03 -06:00
|
|
|
auto _v = get_state();
|
|
|
|
|
get_state_value().store(_n, std::memory_order_relaxed);
|
|
|
|
|
// std::swap(get_state_value(), _n);
|
|
|
|
|
return _v;
|
2022-11-11 07:31:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadState
|
|
|
|
|
set_thread_state(ThreadState _n)
|
|
|
|
|
{
|
|
|
|
|
std::swap(get_thread_state_value(), _n);
|
|
|
|
|
return _n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadState
|
|
|
|
|
push_thread_state(ThreadState _v)
|
|
|
|
|
{
|
|
|
|
|
if(get_thread_state() >= ThreadState::Completed) return get_thread_state();
|
|
|
|
|
|
|
|
|
|
return get_thread_state_history().emplace_back(set_thread_state(_v));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadState
|
|
|
|
|
pop_thread_state()
|
|
|
|
|
{
|
|
|
|
|
if(get_thread_state() >= ThreadState::Completed) return get_thread_state();
|
|
|
|
|
|
|
|
|
|
auto& _hist = get_thread_state_history();
|
|
|
|
|
if(!_hist.empty())
|
|
|
|
|
{
|
|
|
|
|
set_thread_state(_hist.back());
|
|
|
|
|
_hist.pop_back();
|
|
|
|
|
}
|
|
|
|
|
return get_thread_state();
|
|
|
|
|
}
|
2024-10-15 11:20:40 -04:00
|
|
|
} // namespace rocprofsys
|
2022-11-11 07:31:14 -06:00
|
|
|
|
2022-02-08 17:42:17 -06:00
|
|
|
namespace std
|
|
|
|
|
{
|
|
|
|
|
std::string
|
2024-10-15 11:20:40 -04:00
|
|
|
to_string(rocprofsys::State _v)
|
2022-02-08 17:42:17 -06:00
|
|
|
{
|
|
|
|
|
switch(_v)
|
|
|
|
|
{
|
2024-10-15 11:20:40 -04:00
|
|
|
case rocprofsys::State::PreInit: return "PreInit";
|
|
|
|
|
case rocprofsys::State::Init: return "Init";
|
|
|
|
|
case rocprofsys::State::Active: return "Active";
|
|
|
|
|
case rocprofsys::State::Disabled: return "Disabled";
|
|
|
|
|
case rocprofsys::State::Finalized: return "Finalized";
|
2022-02-08 17:42:17 -06:00
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-08 04:40:10 -05:00
|
|
|
std::string
|
2024-10-15 11:20:40 -04:00
|
|
|
to_string(rocprofsys::ThreadState _v)
|
2022-05-08 04:40:10 -05:00
|
|
|
{
|
|
|
|
|
switch(_v)
|
|
|
|
|
{
|
2024-10-15 11:20:40 -04:00
|
|
|
case rocprofsys::ThreadState::Enabled: return "Enabled";
|
|
|
|
|
case rocprofsys::ThreadState::Internal: return "Internal";
|
|
|
|
|
case rocprofsys::ThreadState::Completed: return "Completed";
|
|
|
|
|
case rocprofsys::ThreadState::Disabled: return "Disabled";
|
2022-05-08 04:40:10 -05:00
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-08 17:42:17 -06:00
|
|
|
std::string
|
2024-10-15 11:20:40 -04:00
|
|
|
to_string(rocprofsys::Mode _v)
|
2022-02-08 17:42:17 -06:00
|
|
|
{
|
|
|
|
|
switch(_v)
|
|
|
|
|
{
|
2024-10-15 11:20:40 -04:00
|
|
|
case rocprofsys::Mode::Trace: return "Trace";
|
|
|
|
|
case rocprofsys::Mode::Sampling: return "Sampling";
|
|
|
|
|
case rocprofsys::Mode::Causal: return "Causal";
|
|
|
|
|
case rocprofsys::Mode::Coverage: return "Coverage";
|
2022-02-08 17:42:17 -06:00
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-01-24 18:53:23 -06:00
|
|
|
|
|
|
|
|
std::string
|
2024-10-15 11:20:40 -04:00
|
|
|
to_string(rocprofsys::CausalMode _v)
|
2023-01-24 18:53:23 -06:00
|
|
|
{
|
|
|
|
|
switch(_v)
|
|
|
|
|
{
|
2024-10-15 11:20:40 -04:00
|
|
|
case rocprofsys::CausalMode::Line: return "Line";
|
|
|
|
|
case rocprofsys::CausalMode::Function: return "Function";
|
2023-01-24 18:53:23 -06:00
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2022-02-08 17:42:17 -06:00
|
|
|
} // namespace std
|