1f4cf1aa39
* Srnagara/tool counters collect (#331) * Adding counter collection capability to tools * Adding counter collection feature to tools * Adding counter collection capability to tools * Fixing merge down issues * Small tool fixes for build + prevent profile realloc * Reproducing the counter name query issue in buffered callback * Minor fix for init order + sample that directly uses sdk-tool for debug purposes * Adding a temporary fix to print the counter names * Fixing the output file name and reverting the changes of caching the profile config * Fixing SGPR_Count value * cleaning up debug prints * Adding header to counter collection file * Adding kernel filtering support * Remove threading * Cleaning up the code * Removing redundant prints * Revert "Remove threading" This reverts commit 05c58fb9de826e92cf8d2e3d1c31d5578525dcb4. * Revert "Cleaning up the code" This reverts commit 1d964882bf2396dee8ad020cbb6c83b36e0674e9. * Changing the tools code to align with init-order fix * cmake formatting (cmake-format) (#335) Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> * source formatting (clang-format v11) (#336) Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> * Adding support for async memory copy * source formatting (clang-format v11) (#391) Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> * Fixing header typo * Fixing tool_fini * Replaceing the direction and kind fields values with description * Update lib/rocprofiler-sdk-tool/helper.cpp - Remove use of VLA * Update lib/rocprofiler-sdk-tool/tool.cpp - Formatting * Migrate common/config.* to rocprofiler-sdk-tool * Update lib/rocprofiler-sdk-tool/tool.cpp - fix clang-tidy issues * source formatting (clang-format v11) (#392) Co-authored-by: jrmadsen <jrmadsen@users.noreply.github.com> * Update lib/common/mpl.hpp - is_string_type / is_string_type_impl for deducing if type is a string type * Update include/rocprofiler-sdk/fwd.h - ROCPROFILER_BUFFER_TRACING_MEMORY_COPY_NONE starts at zero * Update lib/rocprofiler-sdk/hsa/async_copy.* - functions for operation ids and names * Update lib/rocprofiler-sdk/buffer_tracing.cpp - support iterating and getting names for ROCPROFILER_BUFFER_TRACING_MEMORY_COPY * Update lib/rocprofiler-sdk-tool/config.* - env ROCPROFILER_ prefix -> ROCPROF_ prefix - add support for memory copy tracing, counter collection, etc. * Update lib/rocprofiler-sdk-tool/helper.* - removed TracerFlushRecord - removed cxa_demangle (use one in common library) - removed GetCounterNames (handled in config) - removed GetKernelNames (handled in config) * Add lib/rocprofiler-sdk-tool/output_file.* - separate out get_output_stream function and output_file struct from tool.cpp * Add lib/rocprofiler-sdk-tool/csv.hpp - write_csv_entry automatically quotes strings - csv_encoder struct enforces correct number of columns * Update lib/rocprofiler-sdk-tool/CMakeLists.txt - add new files * Update lib/rocprofiler-sdk-tool/tool.cpp - update construction of output_file class - add kernel_symbol_data for serializing kernel trace data - use config instead of env lookups - optimize counter collection profile config lookup/creation * Update bin/rocprofv3 - rocprofv3 --help exits with 0 (as it should) - command-line arg for memory copy tracing - command-line arg for mangled kernels - command-line arg for truncated kernels - env ROCPROFILER_ prefix -> env ROCPROF_ prefix * Update tests/async-copy-tracing/validate.py - update test_async_copy_direction to new enum values * Update tests/kernel-tracing/validate.py - update test_async_copy_direction to new enum values * Update tests/tools/json-tool.cpp - add ROCPROFILER_BUFFER_TRACING_MEMORY_COPY to supported buffer_name_info * Update samples/counter_collection/{CMakeLists.txt,main.cpp} - remove counter-collection-sdk-tool * Update .github/workflows/docs.yml - fix paths triggering running the workflow --------- Co-authored-by: Benjamin Welton <bewelton@amd.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com> Co-authored-by: jrmadsen <jrmadsen@users.noreply.github.com> * adding counter collection support * Adding counter collection test * changing directory structure of counter collection tests * Fixing test path for rocprofv3 * Adding hsa-tracing basic test * cmake formatting (cmake-format) (#362) Co-authored-by: bgopesh <bgopesh@users.noreply.github.com> * counter collection tests drop2 * fixing hsa-trace test for rocprofv3 path * python formatting (black) (#371) Co-authored-by: bgopesh <bgopesh@users.noreply.github.com> * both counter colleciton and tracing should work together * Fixing rocprofv3 path * Attempt to fix Segfault with AddressSanitizer * fixing sanitizer segfault * Update rocprofv3 * Update lib/rocprofiler-sdk-tool/README.md - update env variables * Update lib/rocprofiler-sdk/buffer_tracing.cpp - return ROCPROFILER_STATUS_BUFFER_NOT_FOUND if buffer tracing service is configured with invalid buffer * Update lib/rocprofiler-sdk-tool/tool.cpp - designated hsa API trace buffer * Update tests/hsa-tracing/CMakeLists.txt - Fix environment * Update rocprofv3 - do not override HSA_TOOLS_LIB - support ROCPROF_PRELOAD - LD_PRELOAD librocprofiler-sdk.so * Restructure tests directory - move all rocprofv3 integration tests into subfolder * Update cmake/Templates/rocprofiler-sdk/config.cmake.in - create rocprofiler-sdk::rocprofv3 cmake target * Update tests/rocprofv3/hsa-tracing - improve validate.py - convert input to dict via csv.DictReader * Update tests/apps/CMakeLists.txt - fix build rpath for simple-transpose * Update cmake/rocprofiler_memcheck.cmake - prefer libtsan.so.0 * Update tests/rocprofv3/hsa-tracing - move to tests/rocprofv3/tracing - include kernel tracing and memory copy tracing * Update lib/rocprofiler-sdk-tool/tool.cpp - normalize "_ID" vs. "_Id" in CSV column names (use "_Id") * Update lib/rocprofiler-sdk/buffer.{hpp,cpp} - change signature of buffer::get_buffers() - buffer::get_buffers() uses static_object * Update lib/rocprofiler-sdk/context/context.cpp - update usage of buffer::get_buffers() - now returns pointer * Update lib/rocprofiler-sdk/tests/buffer.cpp - update to change for signature of buffer::get_buffers() * Update tests/rocprofv3/tracing/CMakeLists.txt - use %argt% with -d argument * Update lib/rocprofiler-sdk-tool/tool.cpp - use atexit for finalization * Update tests/rocprofv3/tracing/CMakeLists.txt - tweaked name of tests * Update lib/rocprofiler-sdk/hsa/async_copy.* - async_copy_fini + reference counting signals * Update lib/rocprofiler-sdk/registration.cpp - invoke hsa::async_copy_fini() to prevent data race on signals --------- Co-authored-by: SrirakshaNag <104580803+SrirakshaNag@users.noreply.github.com> Co-authored-by: Benjamin Welton <bewelton@amd.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: SrirakshaNag <SrirakshaNag@users.noreply.github.com> Co-authored-by: gobhardw <gopesh.bhardwaj@amd.com> Co-authored-by: bgopesh <bgopesh@users.noreply.github.com>
286 строки
8.9 KiB
C++
286 строки
8.9 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/buffer.hpp"
|
|
|
|
#include <glog/logging.h>
|
|
#include <rocprofiler-sdk/rocprofiler.h>
|
|
|
|
#include "lib/common/container/stable_vector.hpp"
|
|
#include "lib/common/static_object.hpp"
|
|
#include "lib/common/utility.hpp"
|
|
#include "lib/rocprofiler-sdk/context/context.hpp"
|
|
#include "lib/rocprofiler-sdk/context/domain.hpp"
|
|
#include "lib/rocprofiler-sdk/hsa/hsa.hpp"
|
|
#include "lib/rocprofiler-sdk/internal_threading.hpp"
|
|
#include "lib/rocprofiler-sdk/registration.hpp"
|
|
|
|
#include <atomic>
|
|
#include <exception>
|
|
#include <mutex>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
namespace rocprofiler
|
|
{
|
|
namespace buffer
|
|
{
|
|
namespace
|
|
{
|
|
using reserve_size_t = common::container::reserve_size;
|
|
|
|
auto&
|
|
get_buffers_mutex()
|
|
{
|
|
static auto _v = std::mutex{};
|
|
return _v;
|
|
}
|
|
|
|
uint64_t
|
|
get_buffer_offset()
|
|
{
|
|
static uint64_t _v = []() {
|
|
auto gen = std::mt19937{std::random_device{}()};
|
|
auto rng = std::uniform_int_distribution<uint64_t>{std::numeric_limits<uint8_t>::max(),
|
|
std::numeric_limits<uint16_t>::max()};
|
|
return rng(gen);
|
|
}();
|
|
return _v;
|
|
}
|
|
} // namespace
|
|
|
|
bool
|
|
is_valid_buffer_id(rocprofiler_buffer_id_t id)
|
|
{
|
|
if(!get_buffers()) return false;
|
|
auto nbuffers = get_buffers()->size();
|
|
auto offset = get_buffer_offset();
|
|
return (id.handle >= offset && id.handle < (offset + nbuffers));
|
|
}
|
|
|
|
unique_buffer_vec_t*
|
|
get_buffers()
|
|
{
|
|
static auto*& _v = common::static_object<unique_buffer_vec_t>::construct(
|
|
reserve_size_t{unique_buffer_vec_t::chunk_size});
|
|
return _v;
|
|
}
|
|
|
|
instance*
|
|
get_buffer(rocprofiler_buffer_id_t buffer_id)
|
|
{
|
|
if(is_valid_buffer_id(buffer_id) && get_buffers())
|
|
{
|
|
for(auto& itr : *get_buffers())
|
|
{
|
|
if(itr && itr->buffer_id == buffer_id.handle)
|
|
{
|
|
return itr.get();
|
|
}
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::optional<rocprofiler_buffer_id_t>
|
|
allocate_buffer()
|
|
{
|
|
if(registration::get_fini_status() > 0) return std::nullopt;
|
|
|
|
// ensure buffer has thread to handle flushing it
|
|
static auto _init_threads_once = std::once_flag{};
|
|
std::call_once(_init_threads_once, []() { internal_threading::initialize(); });
|
|
|
|
// ... allocate any internal space needed to handle another context ...
|
|
auto _lk = std::unique_lock<std::mutex>{get_buffers_mutex()};
|
|
|
|
// initial context identifier number
|
|
auto _idx = get_buffer_offset() + CHECK_NOTNULL(get_buffers())->size();
|
|
|
|
// make space in registered
|
|
CHECK_NOTNULL(get_buffers())->emplace_back(nullptr);
|
|
|
|
// create an entry in the registered
|
|
auto& _cfg_v = CHECK_NOTNULL(get_buffers())->back();
|
|
_cfg_v = allocator::make_unique_static<buffer::instance>();
|
|
auto* _cfg = _cfg_v.get();
|
|
|
|
if(!_cfg) return std::nullopt;
|
|
|
|
// set the buffer id value
|
|
_cfg_v->buffer_id = _idx;
|
|
|
|
return rocprofiler_buffer_id_t{_idx};
|
|
}
|
|
|
|
rocprofiler_status_t
|
|
flush(rocprofiler_buffer_id_t buffer_id, bool wait)
|
|
{
|
|
if(registration::get_fini_status() > 0) return ROCPROFILER_STATUS_SUCCESS;
|
|
|
|
auto offset = get_buffer_offset();
|
|
|
|
if(!is_valid_buffer_id(buffer_id)) return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
|
|
|
|
auto* buff = get_buffer(buffer_id);
|
|
|
|
auto* task_group =
|
|
internal_threading::get_task_group(rocprofiler_callback_thread_t{buff->task_group_id});
|
|
|
|
if(wait && task_group) task_group->wait();
|
|
|
|
// buffer is currently being flushed or destroyed
|
|
if(buff->syncer.test_and_set())
|
|
{
|
|
if(!wait) return ROCPROFILER_STATUS_ERROR_BUFFER_BUSY;
|
|
while(buff->syncer.test_and_set())
|
|
{
|
|
std::this_thread::yield();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
|
}
|
|
}
|
|
|
|
auto idx = buff->buffer_idx++;
|
|
|
|
auto _task = [buffer_id, idx, offset]() {
|
|
auto& buff_v = CHECK_NOTNULL(get_buffers())->at(buffer_id.handle - offset);
|
|
auto& buff_internal_v = buff_v->get_internal_buffer(idx);
|
|
|
|
if(!buff_internal_v.is_empty())
|
|
{
|
|
// get the array of record headers
|
|
auto buff_data = buff_internal_v.get_record_headers();
|
|
|
|
// invoke buffer callback
|
|
try
|
|
{
|
|
if(buff_v->callback)
|
|
{
|
|
buff_v->callback(rocprofiler_context_id_t{buff_v->context_id},
|
|
rocprofiler_buffer_id_t{buff_v->buffer_id},
|
|
buff_data.data(),
|
|
buff_data.size(),
|
|
buff_v->callback_data,
|
|
buff_v->drop_count);
|
|
}
|
|
} catch(std::exception& e)
|
|
{
|
|
LOG(ERROR) << "buffer callback threw an exception: " << e.what();
|
|
}
|
|
// clear the buffer
|
|
buff_internal_v.clear();
|
|
}
|
|
else
|
|
{
|
|
LOG(INFO) << "buffer at " << buffer_id.handle << " is empty...";
|
|
}
|
|
|
|
buff_v->syncer.clear();
|
|
};
|
|
|
|
if(task_group)
|
|
{
|
|
task_group->exec(_task);
|
|
if(wait) task_group->join();
|
|
}
|
|
else
|
|
{
|
|
_task();
|
|
}
|
|
|
|
return ROCPROFILER_STATUS_SUCCESS;
|
|
}
|
|
} // namespace buffer
|
|
} // namespace rocprofiler
|
|
|
|
extern "C" {
|
|
rocprofiler_status_t
|
|
rocprofiler_create_buffer(rocprofiler_context_id_t context,
|
|
size_t size,
|
|
size_t watermark,
|
|
rocprofiler_buffer_policy_t action,
|
|
rocprofiler_buffer_tracing_cb_t callback,
|
|
void* callback_data,
|
|
rocprofiler_buffer_id_t* buffer_id)
|
|
{
|
|
if(rocprofiler::registration::get_init_status() > -1)
|
|
return ROCPROFILER_STATUS_ERROR_CONFIGURATION_LOCKED;
|
|
|
|
auto* existing_buff = rocprofiler::buffer::get_buffer(*buffer_id);
|
|
if(existing_buff)
|
|
{
|
|
LOG(ERROR) << "buffer (handle=" << buffer_id->handle
|
|
<< ") already allocated: handle=" << existing_buff->buffer_id;
|
|
return ROCPROFILER_STATUS_ERROR_SERVICE_ALREADY_CONFIGURED;
|
|
}
|
|
|
|
auto opt_buff_id = rocprofiler::buffer::allocate_buffer();
|
|
if(!opt_buff_id) return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
|
|
buffer_id->handle = opt_buff_id->handle;
|
|
|
|
auto& buff = CHECK_NOTNULL(rocprofiler::buffer::get_buffers())
|
|
->at(opt_buff_id->handle - rocprofiler::buffer::get_buffer_offset());
|
|
|
|
// allocate the buffers. if it is lossless, we allocate a second buffer to store data while
|
|
// other buffer is being flushed
|
|
buff->buffers.front().allocate(size);
|
|
if(action == ROCPROFILER_BUFFER_POLICY_LOSSLESS) buff->buffers.back().allocate(size);
|
|
|
|
buff->watermark = watermark;
|
|
buff->policy = action;
|
|
buff->callback = callback;
|
|
buff->callback_data = callback_data;
|
|
buff->context_id = context.handle;
|
|
buff->buffer_id = buffer_id->handle;
|
|
buff->buffer_idx = 0;
|
|
|
|
return ROCPROFILER_STATUS_SUCCESS;
|
|
}
|
|
|
|
rocprofiler_status_t
|
|
rocprofiler_flush_buffer(rocprofiler_buffer_id_t buffer_id)
|
|
{
|
|
return rocprofiler::buffer::flush(buffer_id, true);
|
|
}
|
|
|
|
rocprofiler_status_t
|
|
rocprofiler_destroy_buffer(rocprofiler_buffer_id_t buffer_id)
|
|
{
|
|
if(!rocprofiler::buffer::is_valid_buffer_id(buffer_id))
|
|
return ROCPROFILER_STATUS_ERROR_BUFFER_NOT_FOUND;
|
|
|
|
auto offset = rocprofiler::buffer::get_buffer_offset();
|
|
auto* buffers = CHECK_NOTNULL(rocprofiler::buffer::get_buffers());
|
|
auto& buff = buffers->at(buffer_id.handle - offset);
|
|
|
|
// buffer is currently being flushed or destroyed
|
|
if(buff->syncer.test_and_set()) return ROCPROFILER_STATUS_ERROR_BUFFER_BUSY;
|
|
|
|
for(auto& itr : buff->buffers)
|
|
itr.reset();
|
|
|
|
buff->syncer.clear();
|
|
|
|
return ROCPROFILER_STATUS_SUCCESS;
|
|
}
|
|
}
|