Files
rocm-systems/source/lib/rocprofiler-sdk/hsa/hsa_barrier.cpp
T
Benjamin Welton 1de44447f4 Deadlock Fix for HSA and Serialization Disable/Enabling support (#582)
* Initial barrier

* Working on profiler serializer extraction

* Current progress

* Serializtion Support

* source formatting (clang-format v11) (#583)

Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>

* cmake formatting (cmake-format) (#584)

Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>

* Minor fix

* Current Progress

* Current progress

* More fixes

* Serialization Fixes

* Bug fix

* source formatting (clang-format v11) (#600)

Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>

* More fixes

* More minor fixes

* source formatting (clang-format v11) (#603)

Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>

* source formatting (clang-format v11) (#604)

Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>

* Lock order inversion false positive

* order fix

* More changes

* source formatting (clang-format v11) (#607)

Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>

* minor test fix

* Minor test changes

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: bwelton <1683479+bwelton@users.noreply.github.com>
2024-03-08 09:02:43 -06:00

112 lines
3.8 KiB
C++

// MIT License
//
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// 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 "lib/rocprofiler-sdk/hsa/hsa_barrier.hpp"
namespace rocprofiler
{
namespace hsa
{
void
hsa_barrier::set_barrier(queue_map_t& q)
{
_core_api.hsa_signal_store_screlease_fn(_barrier_signal, 1);
_queue_waiting.wlock([&](auto& queue_waiting) {
for(auto& [_, queue] : q)
{
queue->lock_queue([ptr = queue.get(), &queue_waiting]() {
if(ptr->active_async_packets() > 0)
{
queue_waiting[ptr->get_id().handle] = ptr->active_async_packets();
}
});
}
if(queue_waiting.empty())
{
_barried_finished();
_core_api.hsa_signal_store_screlease_fn(_barrier_signal, 0);
}
});
}
std::optional<rocprofiler_packet>
hsa_barrier::enqueue_packet(const Queue* queue)
{
if(_complete) return std::nullopt;
bool return_block = false;
_barrier_enqueued.wlock([&](auto& barrier_enqueued) {
if(barrier_enqueued.find(queue->get_id().handle) == barrier_enqueued.end())
{
return_block = true;
barrier_enqueued.insert(queue->get_id().handle);
}
});
if(!return_block) return std::nullopt;
rocprofiler_packet barrier{};
barrier.barrier_and.header = HSA_PACKET_TYPE_BARRIER_AND << HSA_PACKET_HEADER_TYPE;
barrier.barrier_and.dep_signal[0] = _barrier_signal;
LOG(ERROR) << "Barrier Added: " << _barrier_signal.handle;
return barrier;
}
void
hsa_barrier::remove_queue(const Queue* queue)
{
_queue_waiting.wlock([&](auto& queue_waiting) {
if(queue_waiting.find(queue->get_id().handle) == queue_waiting.end()) return;
queue_waiting.erase(queue->get_id().handle);
if(queue_waiting.empty())
{
_barried_finished();
_complete = true;
_core_api.hsa_signal_store_screlease_fn(_barrier_signal, 0);
}
});
}
bool
hsa_barrier::register_completion(const Queue* queue)
{
bool found = false;
_queue_waiting.wlock([&](auto& queue_waiting) {
if(queue_waiting.find(queue->get_id().handle) == queue_waiting.end()) return;
found = true;
queue_waiting[queue->get_id().handle]--;
if(queue_waiting[queue->get_id().handle] == 0)
{
queue_waiting.erase(queue->get_id().handle);
if(queue_waiting.empty())
{
_barried_finished();
// We are done, release the barrier
_complete = true;
_core_api.hsa_signal_store_screlease_fn(_barrier_signal, 0);
}
}
});
return found;
}
} // namespace hsa
} // namespace rocprofiler