SWDEV-489158: Adding consumer+producer model to AST evaluation (#13)

* Rebased optizations for rocprofv3 tool

* Fixing merge conflicts

* Formatting

* Open from within mutex

* Small name changes

* Added operator

* removed some parameters

* Optimizing counter collection

* Re-arrange code

* Adding back dimension query

* Formatting

* Update source/lib/rocprofiler-sdk/thread_trace/att_core.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Formatting 2

* Fix for test compilation

* Fix for yield

* Adding back check for zero

* Improved thread handling

* Formatting

* Remove automatic start

* Adding test

* Small fixes

* Adding lock for buffer callbacks

* Fix for race condition in AST

* Adding check for ptr

---------

Co-authored-by: Giovanni Baraldi <gbaraldi@amd.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Baraldi, Giovanni
2024-12-05 05:33:53 +01:00
committed by GitHub
parent c42bdc3128
commit b7661bccfd
15 changed files with 577 additions and 153 deletions
@@ -84,3 +84,21 @@ gtest_add_tests(
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_tests_properties(${counter-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests")
set(ROCPROFILER_LIB_CONSUMER_TEST_SOURCES consumer_test.cpp)
add_executable(consumer-test)
target_sources(consumer-test PRIVATE ${ROCPROFILER_LIB_CONSUMER_TEST_SOURCES})
target_link_libraries(
consumer-test rocprofiler-sdk::rocprofiler-sdk-hsa-runtime
rocprofiler-sdk::rocprofiler-sdk-hip rocprofiler-sdk::rocprofiler-sdk-common-library
rocprofiler-sdk::rocprofiler-sdk-static-library GTest::gtest GTest::gtest_main)
gtest_add_tests(
TARGET consumer-test
SOURCES ${ROCPROFILER_LIB_CONSUMER_TEST_SOURCES}
TEST_LIST consumer-tests_TESTS
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set_tests_properties(${consumer-tests_TESTS} PROPERTIES TIMEOUT 45 LABELS "unittests")
@@ -0,0 +1,116 @@
// 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 <algorithm>
#include <array>
#include <cstdint>
#include <future>
#include <mutex>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include <fmt/core.h>
#include <gtest/gtest.h>
#include "lib/rocprofiler-sdk/counters/sample_consumer.hpp"
namespace rocprofiler
{
namespace counters
{
constexpr size_t NUM_THREADS = 5;
constexpr size_t NUM_ELEMENTS = 1ul << 17;
using result_array_t = std::array<std::atomic<size_t>, NUM_ELEMENTS>;
using result_array_ptr_t = std::shared_ptr<result_array_t>;
struct DummyData
{
size_t index;
size_t increment;
result_array_ptr_t array;
};
using consumer_t = consumer_thread_t<DummyData>;
void
consume_fn(DummyData&& data)
{
data.array->at(data.index).fetch_add(data.increment);
}
TEST(consumer, nothread)
{
auto array = std::make_shared<result_array_t>();
consumer_t consumer(consume_fn);
consumer.add(DummyData{1, 1, array});
EXPECT_EQ(array->at(0).load(), 0);
EXPECT_EQ(array->at(1).load(), 1);
}
TEST(consumer, singlethread)
{
auto array = std::make_shared<result_array_t>();
{
consumer_t consumer(consume_fn);
consumer.start();
for(size_t i = 0; i < NUM_ELEMENTS; i++)
consumer.add(DummyData{i, 1, array});
}
for(auto& var : *array)
EXPECT_EQ(var.load(), 1);
}
TEST(consumer, multithreaded)
{
auto array = std::make_shared<result_array_t>();
consumer_t consumer(consume_fn);
auto produce_fn = [&](size_t tid) {
for(size_t i = 0; i < NUM_ELEMENTS; i++)
consumer.add(DummyData{i, tid, array});
};
{
std::vector<std::future<void>> threads{};
for(size_t i = 0; i < NUM_THREADS; i++)
threads.push_back(std::async(std::launch::async, produce_fn, i + 1));
consumer.start();
}
consumer.exit();
size_t expected = NUM_THREADS * (NUM_THREADS + 1) / 2;
for(auto& var : *array)
EXPECT_EQ(var.load(), expected);
}
} // namespace counters
} // namespace rocprofiler
@@ -459,14 +459,16 @@ TEST(core, check_callbacks)
&opt_buff_id),
"Could not create buffer");
cb_info->buffer = opt_buff_id;
// hsa::Queue::queue_info_session_t sess = {.queue = fq, .correlation_id = &corr_id};
hsa::Queue::queue_info_session_t sess = hsa::Queue::queue_info_session_t{.queue = fq};
sess.correlation_id = &corr_id;
auto _sess = hsa::Queue::queue_info_session_t{.queue = fq};
_sess.correlation_id = &corr_id;
auto sess = std::make_shared<hsa::Queue::queue_info_session_t>(std::move(_sess));
counters::inst_pkt_t pkts;
pkts.emplace_back(
std::make_pair(std::move(ret_pkt), static_cast<counters::ClientID>(0)));
completed_cb(&ctx, cb_info, fq, pkt, sess, pkts, kernel_dispatch::profiling_time{});
completed_cb(&ctx, cb_info, sess, pkts, kernel_dispatch::profiling_time{});
rocprofiler_flush_buffer(opt_buff_id);
rocprofiler_destroy_buffer(opt_buff_id);
}