Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capture and Replay: Add support for xrt::runlist APIs #8514

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/runtime_src/core/tools/xbreplay/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(xbreplay_objects OBJECT
src/replay_xrt/replay_xrt_kernel.cpp
src/replay_xrt/replay_xrt_run.cpp
src/replay_xrt/replay_xrt_xclbin.cpp
src/replay_xrt/replay_xrt_runlist.cpp
)

target_include_directories(xbreplay_objects PRIVATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "experimental/xrt_device.h"
#include "experimental/xrt_xclbin.h"
#include "experimental/xrt_ext.h"
#include "experimental/xrt_kernel.h"
#include "xrt/xrt_kernel.h"
#include "xrt.h"
#include "xclbin.h"
Expand Down Expand Up @@ -61,6 +62,9 @@ class replay_xrt
/*Map between handle from log and bo */
std::unordered_map<uint64_t, std::shared_ptr<xrt::xclbin>> m_xclbin_hndle_map;

/*Map between handle from log and runlist */
std::unordered_map<uint64_t, std::shared_ptr<xrt::runlist>> m_runlist_hndle_map;

/*Map between group id */
std::unordered_map<uint64_t, xrt::memory_group> m_kernel_grp_id;

Expand All @@ -87,6 +91,9 @@ class replay_xrt
/*Register hw_ctx class API's*/
void register_hwctxt_class_func();

/* Registers runlist class API's */
void register_runlist_class_func();

/* Validate's file path */
bool is_file_exist(const std::string& fileName)
{
Expand Down Expand Up @@ -124,6 +131,9 @@ class replay_xrt

/* Register HW CTX Class APIs*/
register_hwctxt_class_func();

/*Register Runlist class API's */
register_runlist_class_func();
}

/*
Expand Down Expand Up @@ -159,6 +169,7 @@ class replay_xrt
m_xclbin_hndle_map.clear();
m_hwctx_hndle_map.clear();
m_device_hndle_map.clear();
m_runlist_hndle_map.clear();
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.

#include "replay_xrt.hpp"

namespace xrt_core::tools::xbreplay {

/**
* Replay maintains a map where each member function of the XRT classes is associated
* with a corresponding lambda function. This API adds a lambda function entry for each
* corresponding member of the xrt::runlist class.
*/
void replay_xrt::register_runlist_class_func()
{
m_api_map["xrt::runlist::runlist(const xrt::hw_context&)"] =
[this] (std::shared_ptr<utils::message> msg)
{
const std::vector<std::pair<std::string, std::string>>& args = msg->m_args;
auto hwctx_handle = std::stoull(args[0].second, nullptr, utils::base_hex);
auto phwctx = m_hwctx_hndle_map[hwctx_handle];

if (phwctx)
{
const xrt::hw_context& hwctx = *phwctx;
auto runlist_ptr = std::make_shared<xrt::runlist>(hwctx);
m_runlist_hndle_map[msg->m_ret_val] = runlist_ptr;
}
else
throw std::runtime_error("Failed to get hardware context handle");
};

m_api_map["xrt::runlist::add(const xrt::run&)"] =
[this] (std::shared_ptr<utils::message> msg)
{
const std::vector<std::pair<std::string, std::string>>& args = msg->m_args;
auto runlist_handle = std::stoull(args[0].second, nullptr, utils::base_hex);
auto run_handle = std::stoull(args[1].second, nullptr, utils::base_hex);

auto runlist_ptr = m_runlist_hndle_map[runlist_handle];
auto run_ptr = m_run_hndle_map[run_handle];

if (runlist_ptr && run_ptr)
{
const xrt::run& run = *run_ptr;
runlist_ptr->add(run);
}
else
throw std::runtime_error("Failed to get runlist or run handle");
};

m_api_map["xrt::runlist::execute()"] =
[this] (std::shared_ptr<utils::message> msg)
{
auto runlist_ptr = m_runlist_hndle_map[msg->m_handle];

if (runlist_ptr)
runlist_ptr->execute();
else
throw std::runtime_error("Failed to get runlist handle");
};

m_api_map["xrt::runlist::wait(const std::chrono::milliseconds&)"] =
[this] (std::shared_ptr<utils::message> msg)
{
const std::vector<std::pair<std::string, std::string>>& args = msg->m_args;
auto runlist_ptr = m_runlist_hndle_map[msg->m_handle];
auto time = std::stoll(args[0].second);
const std::chrono::milliseconds timeout(time);

if (runlist_ptr)
runlist_ptr->wait(timeout);
else
throw std::runtime_error("Failed to get runlist handle");
};

m_api_map["xrt::runlist::reset()"] =
[this] (std::shared_ptr<utils::message> msg)
{
auto runlist_ptr = m_runlist_hndle_map[msg->m_handle];

if (runlist_ptr)
runlist_ptr->reset();
else
throw std::runtime_error("Failed to get runlist handle");
};
}
}
7 changes: 7 additions & 0 deletions src/runtime_src/core/tools/xbtracer/src/lib/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ const static std::unordered_map < std::string, void **> fname2fptr_map = {
{"xrt::run::set_arg_at_index(int, xrt::bo const&)", (void **) &dtbl.run.set_arg2},
{"xrt::run::update_arg_at_index(int, void const*, unsigned long)", (void **) &dtbl.run.update_arg3},
{"xrt::run::update_arg_at_index(int, xrt::bo const&)", (void **) &dtbl.run.update_arg2},

/* runlist class maps */
{"xrt::runlist::runlist(xrt::hw_context const&)", (void **) &dtbl.runlist.ctor},
{"xrt::runlist::add(xrt::run const&)", (void **) &dtbl.runlist.add},
{"xrt::runlist::execute()", (void **) &dtbl.runlist.execute},
{"xrt::runlist::wait(std::chrono::duration<long, std::ratio<1l, 1000l> > const&) const", (void **) &dtbl.runlist.wait},
{"xrt::runlist::reset()", (void **) &dtbl.runlist.reset},

/* kernel class maps */
{"xrt::kernel::kernel(xrt::device const&, xrt::uuid const&, std::string const&, xrt::kernel::cu_access_mode)", (void **) &dtbl.kernel.ctor},
Expand Down
1 change: 1 addition & 0 deletions src/runtime_src/core/tools/xbtracer/src/lib/capture.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class xrt_ftbl
xrt_bo_ftbl bo;
xrt_kernel_ftbl kernel;
xrt_run_ftbl run;
xrt_runlist_ftbl runlist;
xrt_xclbin_ftbl xclbin;
xrt_hw_context_ftbl hw_context;
xrt_ext_ftbl ext;
Expand Down
1 change: 1 addition & 0 deletions src/runtime_src/core/tools/xbtracer/src/lib/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ void logger::synth_dtor_trace_fn()
run |= check_ref_count(m_krnl_ref_tracker);
run |= check_ref_count(m_bo_ref_tracker);
run |= check_ref_count(m_hw_cnxt_ref_tracker);
run |= check_ref_count(m_runlist_ref_tracker);

if (m_is_destructing == false)
run = true;
Expand Down
9 changes: 9 additions & 0 deletions src/runtime_src/core/tools/xbtracer/src/lib/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "xrt/xrt_bo.h"
#include "xrt/xrt_device.h"
#include "xrt/xrt_kernel.h"
#include "experimental/xrt_kernel.h"

#ifdef _WIN32
# include <windows.h>
Expand Down Expand Up @@ -103,6 +104,8 @@ class logger
std::string>> m_krnl_ref_tracker;
std::vector<std::tuple<std::shared_ptr<run_impl>, std::thread::id,
std::string>> m_run_ref_tracker;
std::vector<std::tuple<std::shared_ptr<runlist_impl>, std::thread::id,
std::string>> m_runlist_ref_tracker;
std::vector<std::tuple<std::shared_ptr<bo_impl>, std::thread::id,
std::string>> m_bo_ref_tracker;
std::vector<std::tuple<std::shared_ptr<hw_context_impl>, std::thread::id,
Expand Down Expand Up @@ -178,6 +181,12 @@ class logger
std::this_thread::get_id(), "xrt::run::~run()"));
}

void set_pimpl(std::shared_ptr<runlist_impl> hpimpl)
{
m_runlist_ref_tracker.emplace_back(std::make_tuple(hpimpl,
std::this_thread::get_id(), "xrt::runlist::~runlist()"));
}

void set_pimpl(std::shared_ptr<hw_context_impl> hpimpl)
{
m_hw_cnxt_ref_tracker.emplace_back(std::make_tuple(hpimpl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>

#define XCL_DRIVER_DLL_EXPORT
#define XRT_API_SOURCE
#include "capture.h"
#include "logger.h"

Expand All @@ -13,7 +14,7 @@ namespace xtx = xrt::tools::xbtracer;
const xtx::xrt_ftbl& dtbl = xtx::xrt_ftbl::get_instance();

/*
* kernel/run class instrumented methods
* kernel/run/runlist class instrumented methods
* */
namespace xrt {
XCL_DRIVER_DLLESPEC
Expand Down Expand Up @@ -254,4 +255,52 @@ xrt::xclbin kernel::get_xclbin() const
return xclbin;
}

XRT_API_EXPORT
runlist::runlist(const xrt::hw_context& hwctx)
{
auto func = "xrt::runlist::runlist(const xrt::hw_context&)";
XRT_TOOLS_XBT_CALL_CTOR(dtbl.runlist.ctor, this, hwctx);
/* As pimpl will be updated only after ctor call */
XRT_TOOLS_XBT_FUNC_ENTRY(func, hwctx.get_handle().get());
XRT_TOOLS_XBT_FUNC_EXIT(func);
}

XRT_API_EXPORT
void runlist::add(const xrt::run& run)
{
auto func = "xrt::runlist::add(const xrt::run&)";
XRT_TOOLS_XBT_FUNC_ENTRY(func, run.get_handle().get());
XRT_TOOLS_XBT_CALL_METD(dtbl.runlist.add, run);
XRT_TOOLS_XBT_FUNC_EXIT(func);
}

XRT_API_EXPORT
void runlist::execute()
{
auto func = "xrt::runlist::execute()";
XRT_TOOLS_XBT_FUNC_ENTRY(func, this);
XRT_TOOLS_XBT_CALL_METD(dtbl.runlist.execute);
XRT_TOOLS_XBT_FUNC_EXIT(func);
}

XRT_API_EXPORT
std::cv_status runlist::wait(const std::chrono::milliseconds& timeout) const
{
auto func = "xrt::runlist::wait(const std::chrono::milliseconds&)";
XRT_TOOLS_XBT_FUNC_ENTRY(func, timeout.count());
std::cv_status status = std::cv_status::no_timeout;
XRT_TOOLS_XBT_CALL_METD_RET(dtbl.runlist.wait, status, timeout);
XRT_TOOLS_XBT_FUNC_EXIT_RET(func, (int)status);
return status;
}

XRT_API_EXPORT
void runlist::reset()
{
auto func = "xrt::runlist::reset()";
XRT_TOOLS_XBT_FUNC_ENTRY(func, this);
XRT_TOOLS_XBT_CALL_METD(dtbl.runlist.reset);
XRT_TOOLS_XBT_FUNC_EXIT(func);
}

} // namespace xrt
25 changes: 24 additions & 1 deletion src/runtime_src/core/tools/xbtracer/src/lib/xrt_kernel_inst.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include "xrt/xrt_kernel.h"
#include "experimental/xrt_kernel.h"

/*
* Run class method aliases.
Expand Down Expand Up @@ -63,7 +64,7 @@ using xrt_kernel_ctor = xrt::kernel* (*)(void*, const xrt::device&,\
const xrt::uuid&, const std::string&, xrt::kernel::cu_access_mode);
using xrt_kernel_ctor2 = xrt::kernel* (*)(void*, const xrt::hw_context&,\
const std::string&);
/*TODO: Marked obselete - should be keep this ?*/
/*TODO: Marked obselete - should we keep this ?*/
using xrt_kernel_ctor_obs = xrt::kernel* (*)(void* cxt, xclDeviceHandle,\
const xrt::uuid&, const std::string&, xrt::kernel::cu_access_mode);
using xrt_kernel_group_id = int (xrt::kernel::*)(int) const;
Expand All @@ -90,3 +91,25 @@ struct xrt_kernel_ftbl
xrt_kernel_get_xclbin get_xclbin;
xrt_kernel_dtor dtor;
};

/*
* Runlist class method aliases.
*/
using xrt_runlist_ctor = xrt::runlist (*)(void*, const xrt::hw_context&);
using xrt_runlist_add = void (xrt::runlist::*)(const xrt::run&);
using xrt_runlist_execute = void (xrt::runlist::*)(void);
using xrt_runlist_wait = std::cv_status (xrt::runlist::*)\
(const std::chrono::milliseconds&) const;
using xrt_runlist_reset = void (xrt::runlist::*)(void);

/*
* struct xrt_runlist_ftbl definition.
*/
struct xrt_runlist_ftbl
{
xrt_runlist_ctor ctor;
xrt_runlist_add add;
xrt_runlist_execute execute;
xrt_runlist_wait wait;
xrt_runlist_reset reset;
};
Loading