Files
rocm-systems/source/lib/rocprofiler/context/context.cpp
T
Jonathan R. Madsen a646c1546c rocprofiler library unit tests (#81)
* Update CI and linting workflows

- delete linting workflow
- compile default CI job with clang-tidy
- split out code coverage matrix entry to separate job
- code coverage job runs code coverage 3x
  - once for total code coverage
  - once for unittests code coverage
  - once for samples code coverage

* Update PTL submodule

- improves handling of when thread pool is destroyed in atexit handler

* Update lib/rocprofiler/buffer

- buffer::instance::get_internal_buffer()
- allocate_buffer invokes internal_threading::initialize() on first entry
- update flush routine
 - if wait is false, does not wait for task group to finish syncing
 - checks for callback pointer

* Update lib/rocprofiler/internal_threading

- modifications to handle destruction of statics before atexit handler is invoked

* Update lib/rocprofiler/registration.cpp

- reorder atexit call in initialize()
- protect finalize from executing more than once

* Add unittests for rocprofiler buffer

* Update CI workflow

- disable fail-fast for sanitizers
- move AddressSanitizer job to top of the list

* Update lib/rocprofiler/tests/buffer/CMakeLists.txt

- do not set memcheck LD_PRELOAD for rocprofiler-lib-buffer-tests

* Update lib/rocprofiler/registration.{hpp,cpp}

- only invoke client finalizers if initialized
- remove invoke_client_initializer
- move invoke_client functions to anonymous namespace (no declaration in header)
- set fini status in finalize

* Update scripts/thread-sanitizer-suppr.txt

- suppress false positive for double mutex lock in external/ptl/source/PTL/TaskGroup.hh

* Restructure lib/rocprofiler/tests

* Update lib/common

- add utility.cpp
- move read_command_line to utility.{hpp,cpp}
  - was formerly in config.cpp

* Update lib/rocprofiler

- checks for init status return configuration locked if status is not greater than -1
  - in other words, this prevents calling these functions directly (which was possible when check was for greater than 0

* Update lib/rocprofiler/context/context.{hpp,cpp}

- provide deactivate_client_contexts and deregister_client_contexts
  - these functions are used when the tool fails to configure

* Update lib/rocprofiler/registration.{hpp,cpp}

- internal "public" get_client_offet()
- client ids are offset by a random value to avoid default values behaving correctly

* Update lib/rocprofiler/tests

- fix rocprofiler_lib.registration_lambda_no_result

* Update lib/rocprofiler/tests

- fix rocprofiler_lib.registration_lambda_with_result

* Update lib/rocprofiler/tests

- remove deep bind from rocprofiler_lib.registration_lambda_with_result

* Update lib/rocprofiler/tests

- use RTLD_NOW when dlopen'ing in rocprofiler_lib.registration_lambda_with_result

* Update rocprofiler registration tests

- split registration tests into separate exe that links to shared library

* Formatting

* Update CI workflow

- always checkout submodules via actions/checkout

* Update lib/rocprofiler/buffer.{hpp,cpp}

- fix issue with buffer flushing not working when only called once

* Update rocprofiler lib registration test

- test for buffered callback

* Update include/rocprofiler/rocprofiler.h

- include internal_threading.h header

* Update rocprofiler lib registration test

- add in internal threading for buffered test
2023-09-26 19:21:31 -05:00

261 řádky
7.6 KiB
C++

// MIT License
//
// Copyright (c) 2023 ROCm Developer Tools
//
// 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 <rocprofiler/fwd.h>
#include <rocprofiler/rocprofiler.h>
#include "lib/common/container/stable_vector.hpp"
#include "lib/rocprofiler/buffer.hpp"
#include "lib/rocprofiler/context/context.hpp"
#include <glog/logging.h>
#include <unistd.h>
#include <atomic>
#include <cstddef>
#include <memory>
#include <mutex>
#include <optional>
namespace rocprofiler
{
namespace context
{
namespace
{
auto&
get_contexts_mutex()
{
static auto _v = std::mutex{};
return _v;
}
constexpr auto invalid_client_idx = std::numeric_limits<uint32_t>::max();
auto&
get_client_index()
{
static auto _v = invalid_client_idx;
return _v;
}
} // namespace
uint64_t
correlation_tracing_service::get_unique_record_id()
{
static auto _v = std::atomic<uint64_t>{};
return _v++;
}
using reserve_size_t = common::container::reserve_size;
unique_context_vec_t&
get_registered_contexts()
{
static auto _v = unique_context_vec_t{reserve_size_t{unique_context_vec_t::chunk_size}};
return _v;
}
active_context_vec_t&
get_active_contexts()
{
static auto* _v = new active_context_vec_t{reserve_size_t{active_context_vec_t::chunk_size}};
static auto _once = std::once_flag{};
std::call_once(_once, std::atexit, []() {
for(auto& itr : *_v)
{
itr.store(nullptr);
}
});
return *_v;
}
// set the client index needs to be called before allocate_context()
void
push_client(uint32_t value)
{
LOG_ASSERT(get_client_index() == invalid_client_idx)
<< " rocprofiler client index is currently " << get_client_index()
<< "... which means that a new client is initializing before the last client finished "
"initializing. This is an internal error, please file a bug report with a reproducer";
get_client_index() = value;
}
// remove the client index
void
pop_client(uint32_t value)
{
LOG_ASSERT(get_client_index() == value)
<< " rocprofiler client index is currently not " << value
<< "... which means that a new client was initialized before this client finished "
"initializing. This is an internal error, please file a bug report with a reproducer";
get_client_index() = invalid_client_idx;
}
std::optional<rocprofiler_context_id_t>
allocate_context()
{
// ... allocate any internal space needed to handle another context ...
auto _lk = std::unique_lock<std::mutex>{get_contexts_mutex()};
// initial context identifier number
auto _idx = get_registered_contexts().size();
// make space in registered
get_registered_contexts().emplace_back(nullptr);
// create an entry in the registered
auto& _cfg_v = get_registered_contexts().back();
_cfg_v = std::make_unique<context>();
auto* _cfg = _cfg_v.get();
// ...
if(!_cfg) return std::nullopt;
_cfg->size = sizeof(context);
_cfg->context_idx = _idx;
_cfg->client_idx = get_client_index();
LOG_ASSERT(_cfg->client_idx != invalid_client_idx)
<< " rocprofiler internal error: a context was allocated without an associated tool client "
"identifier";
return rocprofiler_context_id_t{_idx};
}
rocprofiler_status_t
validate_context(const context* cfg)
{
// if(cfg->buffer == nullptr) return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
// if(cfg->filter == nullptr) return ROCPROFILER_STATUS_ERROR_FILTER_NOT_FOUND;
return (cfg) ? ROCPROFILER_STATUS_SUCCESS : ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
}
rocprofiler_status_t
start_context(rocprofiler_context_id_t context_id)
{
if(context_id.handle >= get_registered_contexts().size())
{
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
}
context* cfg = get_registered_contexts().at(context_id.handle).get();
if(!cfg)
{
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND;
}
if(validate_context(cfg) != ROCPROFILER_STATUS_SUCCESS)
{
return ROCPROFILER_STATUS_ERROR_CONTEXT_INVALID;
}
uint64_t rocp_tot_contexts = get_registered_contexts().size();
auto idx = rocp_tot_contexts;
{
// hold a lock here so prevent multiple threads from finding the same nullptr slot
auto _lk = std::unique_lock<std::mutex>{get_contexts_mutex()};
// try to find a nullptr slot first
for(size_t i = 0; i < get_active_contexts().size(); ++i)
{
auto* itr = get_active_contexts().at(i).load(std::memory_order_relaxed);
if(itr == nullptr)
{
idx = i;
break;
}
else if(context_id.handle == itr->context_idx)
{
return ROCPROFILER_STATUS_SUCCESS;
}
}
// if no nullptr slot was found, then create one while lock is held
if(idx == rocp_tot_contexts)
{
idx = get_active_contexts().size();
get_active_contexts().emplace_back();
}
}
// atomic swap the pointer into the "active" array used internally
context* _expected = nullptr;
bool success = get_active_contexts().at(idx).compare_exchange_strong(
_expected, get_registered_contexts().at(context_id.handle).get());
if(!success) return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_STARTED;
return ROCPROFILER_STATUS_SUCCESS;
}
rocprofiler_status_t
stop_context(rocprofiler_context_id_t idx)
{
// atomically assign the context pointer to NULL so that it is skipped in future
// callbacks
for(auto& itr : get_active_contexts())
{
auto* _expected = itr.load(std::memory_order_relaxed);
if(_expected && _expected->context_idx == idx.handle)
{
bool success = itr.compare_exchange_strong(_expected, nullptr);
if(success) return ROCPROFILER_STATUS_SUCCESS;
}
}
return ROCPROFILER_STATUS_ERROR_CONTEXT_NOT_FOUND; // compare exchange failed
}
void
deactivate_client_contexts(rocprofiler_client_id_t client_id)
{
for(auto& itr : get_active_contexts())
{
auto* itr_v = itr.load();
if(itr_v->client_idx == client_id.handle)
{
itr.store(nullptr);
}
}
}
void
deregister_client_contexts(rocprofiler_client_id_t client_id)
{
for(auto& itr : get_registered_contexts())
{
if(itr->client_idx == client_id.handle)
{
for(auto& bitr : buffer::get_buffers())
{
if(bitr->context_id == itr->context_idx) bitr.reset();
}
itr.reset();
}
}
}
} // namespace context
} // namespace rocprofiler