Skip to content

Commit 3209243

Browse files
Capture and Replay: Add support for xrt::runlist APIs
Signed-off-by: Harsh Wardhan <[email protected]>
1 parent 40ed432 commit 3209243

File tree

9 files changed

+191
-2
lines changed

9 files changed

+191
-2
lines changed

src/runtime_src/core/tools/xbreplay/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_library(xbreplay_objects OBJECT
2222
src/replay_xrt/replay_xrt_kernel.cpp
2323
src/replay_xrt/replay_xrt_run.cpp
2424
src/replay_xrt/replay_xrt_xclbin.cpp
25+
src/replay_xrt/replay_xrt_runlist.cpp
2526
)
2627

2728
target_include_directories(xbreplay_objects PRIVATE

src/runtime_src/core/tools/xbreplay/src/replay_xrt/replay_xrt.hpp

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "experimental/xrt_device.h"
77
#include "experimental/xrt_xclbin.h"
88
#include "experimental/xrt_ext.h"
9+
#include "experimental/xrt_kernel.h"
910
#include "xrt/xrt_kernel.h"
1011
#include "xrt.h"
1112
#include "xclbin.h"
@@ -61,6 +62,9 @@ class replay_xrt
6162
/*Map between handle from log and bo */
6263
std::unordered_map<uint64_t, std::shared_ptr<xrt::xclbin>> m_xclbin_hndle_map;
6364

65+
/*Map between handle from log and runlist */
66+
std::unordered_map<uint64_t, std::shared_ptr<xrt::runlist>> m_runlist_hndle_map;
67+
6468
/*Map between group id */
6569
std::unordered_map<uint64_t, xrt::memory_group> m_kernel_grp_id;
6670

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

94+
/* Registers runlist class API's */
95+
void register_runlist_class_func();
96+
9097
/* Validate's file path */
9198
bool is_file_exist(const std::string& fileName)
9299
{
@@ -124,6 +131,9 @@ class replay_xrt
124131

125132
/* Register HW CTX Class APIs*/
126133
register_hwctxt_class_func();
134+
135+
/*Register Runlist class API's */
136+
register_runlist_class_func();
127137
}
128138

129139
/*
@@ -159,6 +169,7 @@ class replay_xrt
159169
m_xclbin_hndle_map.clear();
160170
m_hwctx_hndle_map.clear();
161171
m_device_hndle_map.clear();
172+
m_runlist_hndle_map.clear();
162173
}
163174

164175
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
3+
4+
#include "replay_xrt.hpp"
5+
6+
namespace xrt_core::tools::xbreplay {
7+
8+
/**
9+
* Replay maintains a map where each member function of the XRT classes is associated
10+
* with a corresponding lambda function. This API adds a lambda function entry for each
11+
* corresponding member of the xrt::runlist class.
12+
*/
13+
void replay_xrt::register_runlist_class_func()
14+
{
15+
m_api_map["xrt::runlist::runlist(const xrt::hw_context&)"] =
16+
[this] (std::shared_ptr<utils::message> msg)
17+
{
18+
const std::vector<std::pair<std::string, std::string>>& args = msg->m_args;
19+
auto hwctx_handle = std::stoull(args[0].second, nullptr, utils::base_hex);
20+
auto phwctx = m_hwctx_hndle_map[hwctx_handle];
21+
22+
if (phwctx)
23+
{
24+
const xrt::hw_context& hwctx = *phwctx;
25+
auto runlist_ptr = std::make_shared<xrt::runlist>(hwctx);
26+
m_runlist_hndle_map[msg->m_ret_val] = runlist_ptr;
27+
}
28+
else
29+
throw std::runtime_error("Failed to get hardware context handle");
30+
};
31+
32+
m_api_map["xrt::runlist::add(const xrt::run&)"] =
33+
[this] (std::shared_ptr<utils::message> msg)
34+
{
35+
const std::vector<std::pair<std::string, std::string>>& args = msg->m_args;
36+
auto runlist_handle = std::stoull(args[0].second, nullptr, utils::base_hex);
37+
auto run_handle = std::stoull(args[1].second, nullptr, utils::base_hex);
38+
39+
auto runlist_ptr = m_runlist_hndle_map[runlist_handle];
40+
auto run_ptr = m_run_hndle_map[run_handle];
41+
42+
if (runlist_ptr && run_ptr)
43+
{
44+
const xrt::run& run = *run_ptr;
45+
runlist_ptr->add(run);
46+
}
47+
else
48+
throw std::runtime_error("Failed to get runlist or run handle");
49+
};
50+
51+
m_api_map["xrt::runlist::execute()"] =
52+
[this] (std::shared_ptr<utils::message> msg)
53+
{
54+
auto runlist_ptr = m_runlist_hndle_map[msg->m_handle];
55+
56+
if (runlist_ptr)
57+
runlist_ptr->execute();
58+
else
59+
throw std::runtime_error("Failed to get runlist handle");
60+
};
61+
62+
m_api_map["xrt::runlist::wait(const std::chrono::milliseconds&)"] =
63+
[this] (std::shared_ptr<utils::message> msg)
64+
{
65+
const std::vector<std::pair<std::string, std::string>>& args = msg->m_args;
66+
auto runlist_ptr = m_runlist_hndle_map[msg->m_handle];
67+
auto time = std::stoll(args[0].second);
68+
const std::chrono::milliseconds timeout(time);
69+
70+
if (runlist_ptr)
71+
runlist_ptr->wait(timeout);
72+
else
73+
throw std::runtime_error("Failed to get runlist handle");
74+
};
75+
76+
m_api_map["xrt::runlist::reset()"] =
77+
[this] (std::shared_ptr<utils::message> msg)
78+
{
79+
auto runlist_ptr = m_runlist_hndle_map[msg->m_handle];
80+
81+
if (runlist_ptr)
82+
runlist_ptr->reset();
83+
else
84+
throw std::runtime_error("Failed to get runlist handle");
85+
};
86+
}
87+
}

src/runtime_src/core/tools/xbtracer/src/lib/capture.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ const static std::unordered_map < std::string, void **> fname2fptr_map = {
105105
{"xrt::run::set_arg_at_index(int, xrt::bo const&)", (void **) &dtbl.run.set_arg2},
106106
{"xrt::run::update_arg_at_index(int, void const*, unsigned long)", (void **) &dtbl.run.update_arg3},
107107
{"xrt::run::update_arg_at_index(int, xrt::bo const&)", (void **) &dtbl.run.update_arg2},
108+
109+
/* runlist class maps */
110+
{"xrt::runlist::runlist(xrt::hw_context const&)", (void **) &dtbl.runlist.ctor},
111+
{"xrt::runlist::add(xrt::run const&)", (void **) &dtbl.runlist.add},
112+
{"xrt::runlist::execute()", (void **) &dtbl.runlist.execute},
113+
{"xrt::runlist::wait(std::chrono::duration<long, std::ratio<1l, 1000l> > const&) const", (void **) &dtbl.runlist.wait},
114+
{"xrt::runlist::state()", (void **) &dtbl.runlist.reset},
108115

109116
/* kernel class maps */
110117
{"xrt::kernel::kernel(xrt::device const&, xrt::uuid const&, std::string const&, xrt::kernel::cu_access_mode)", (void **) &dtbl.kernel.ctor},

src/runtime_src/core/tools/xbtracer/src/lib/capture.h

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class xrt_ftbl
1919
xrt_bo_ftbl bo;
2020
xrt_kernel_ftbl kernel;
2121
xrt_run_ftbl run;
22+
xrt_runlist_ftbl runlist;
2223
xrt_xclbin_ftbl xclbin;
2324
xrt_hw_context_ftbl hw_context;
2425
xrt_ext_ftbl ext;

src/runtime_src/core/tools/xbtracer/src/lib/logger.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ void logger::synth_dtor_trace_fn()
174174
run |= check_ref_count(m_krnl_ref_tracker);
175175
run |= check_ref_count(m_bo_ref_tracker);
176176
run |= check_ref_count(m_hw_cnxt_ref_tracker);
177+
run |= check_ref_count(m_runlist_ref_tracker);
177178

178179
if (m_is_destructing == false)
179180
run = true;

src/runtime_src/core/tools/xbtracer/src/lib/logger.h

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "xrt/xrt_bo.h"
2020
#include "xrt/xrt_device.h"
2121
#include "xrt/xrt_kernel.h"
22+
#include "experimental/xrt_kernel.h"
2223

2324
#ifdef _WIN32
2425
# include <windows.h>
@@ -103,6 +104,8 @@ class logger
103104
std::string>> m_krnl_ref_tracker;
104105
std::vector<std::tuple<std::shared_ptr<run_impl>, std::thread::id,
105106
std::string>> m_run_ref_tracker;
107+
std::vector<std::tuple<std::shared_ptr<runlist_impl>, std::thread::id,
108+
std::string>> m_runlist_ref_tracker;
106109
std::vector<std::tuple<std::shared_ptr<bo_impl>, std::thread::id,
107110
std::string>> m_bo_ref_tracker;
108111
std::vector<std::tuple<std::shared_ptr<hw_context_impl>, std::thread::id,
@@ -178,6 +181,12 @@ class logger
178181
std::this_thread::get_id(), "xrt::run::~run()"));
179182
}
180183

184+
void set_pimpl(std::shared_ptr<runlist_impl> hpimpl)
185+
{
186+
m_runlist_ref_tracker.emplace_back(std::make_tuple(hpimpl,
187+
std::this_thread::get_id(), "xrt::runlist::~runlist()"));
188+
}
189+
181190
void set_pimpl(std::shared_ptr<hw_context_impl> hpimpl)
182191
{
183192
m_hw_cnxt_ref_tracker.emplace_back(std::make_tuple(hpimpl,

src/runtime_src/core/tools/xbtracer/src/lib/xrt_kernel_inst.cpp

+50-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <iostream>
55

66
#define XCL_DRIVER_DLL_EXPORT
7+
#define XRT_API_SOURCE
78
#include "capture.h"
89
#include "logger.h"
910

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

1516
/*
16-
* kernel/run class instrumented methods
17+
* kernel/run/runlist class instrumented methods
1718
* */
1819
namespace xrt {
1920
XCL_DRIVER_DLLESPEC
@@ -254,4 +255,52 @@ xrt::xclbin kernel::get_xclbin() const
254255
return xclbin;
255256
}
256257

258+
XRT_API_EXPORT
259+
runlist::runlist(const xrt::hw_context& hwctx)
260+
{
261+
auto func = "xrt::runlist::runlist(const xrt::hw_context&)";
262+
XRT_TOOLS_XBT_CALL_CTOR(dtbl.runlist.ctor, this, hwctx);
263+
/* As pimpl will be updated only after ctor call */
264+
XRT_TOOLS_XBT_FUNC_ENTRY(func, hwctx.get_handle().get());
265+
XRT_TOOLS_XBT_FUNC_EXIT(func);
266+
}
267+
268+
XRT_API_EXPORT
269+
void runlist::add(const xrt::run& run)
270+
{
271+
auto func = "xrt::runlist::add(const xrt::run&)";
272+
XRT_TOOLS_XBT_FUNC_ENTRY(func, run.get_handle().get());
273+
XRT_TOOLS_XBT_CALL_METD(dtbl.runlist.add, run);
274+
XRT_TOOLS_XBT_FUNC_EXIT(func);
275+
}
276+
277+
XRT_API_EXPORT
278+
void runlist::execute()
279+
{
280+
auto func = "xrt::runlist::execute()";
281+
XRT_TOOLS_XBT_FUNC_ENTRY(func, this);
282+
XRT_TOOLS_XBT_CALL_METD(dtbl.runlist.execute);
283+
XRT_TOOLS_XBT_FUNC_EXIT(func);
284+
}
285+
286+
XRT_API_EXPORT
287+
std::cv_status runlist::wait(const std::chrono::milliseconds& timeout) const
288+
{
289+
auto func = "xrt::runlist::wait(const std::chrono::milliseconds&)";
290+
XRT_TOOLS_XBT_FUNC_ENTRY(func, timeout.count());
291+
std::cv_status status = std::cv_status::no_timeout;
292+
XRT_TOOLS_XBT_CALL_METD_RET(dtbl.runlist.wait, status, timeout);
293+
XRT_TOOLS_XBT_FUNC_EXIT_RET(func, (int)status);
294+
return status;
295+
}
296+
297+
XRT_API_EXPORT
298+
void runlist::reset()
299+
{
300+
auto func = "xrt::runlist::reset()";
301+
XRT_TOOLS_XBT_FUNC_ENTRY(func, this);
302+
XRT_TOOLS_XBT_CALL_METD(dtbl.runlist.reset);
303+
XRT_TOOLS_XBT_FUNC_EXIT(func);
304+
}
305+
257306
} // namespace xrt

src/runtime_src/core/tools/xbtracer/src/lib/xrt_kernel_inst.h

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55

66
#include "xrt/xrt_kernel.h"
7+
#include "experimental/xrt_kernel.h"
78

89
/*
910
* Run class method aliases.
@@ -63,7 +64,7 @@ using xrt_kernel_ctor = xrt::kernel* (*)(void*, const xrt::device&,\
6364
const xrt::uuid&, const std::string&, xrt::kernel::cu_access_mode);
6465
using xrt_kernel_ctor2 = xrt::kernel* (*)(void*, const xrt::hw_context&,\
6566
const std::string&);
66-
/*TODO: Marked obselete - should be keep this ?*/
67+
/*TODO: Marked obselete - should we keep this ?*/
6768
using xrt_kernel_ctor_obs = xrt::kernel* (*)(void* cxt, xclDeviceHandle,\
6869
const xrt::uuid&, const std::string&, xrt::kernel::cu_access_mode);
6970
using xrt_kernel_group_id = int (xrt::kernel::*)(int) const;
@@ -90,3 +91,25 @@ struct xrt_kernel_ftbl
9091
xrt_kernel_get_xclbin get_xclbin;
9192
xrt_kernel_dtor dtor;
9293
};
94+
95+
/*
96+
* Runlist class method aliases.
97+
*/
98+
using xrt_runlist_ctor = xrt::runlist (*)(void*, const xrt::hw_context&);
99+
using xrt_runlist_add = void (xrt::runlist::*)(const xrt::run&);
100+
using xrt_runlist_execute = void (xrt::runlist::*)(void);
101+
using xrt_runlist_wait = std::cv_status (xrt::runlist::*)\
102+
(const std::chrono::milliseconds&) const;
103+
using xrt_runlist_reset = void (xrt::runlist::*)(void);
104+
105+
/*
106+
* struct xrt_runlist_ftbl definition.
107+
*/
108+
struct xrt_runlist_ftbl
109+
{
110+
xrt_runlist_ctor ctor;
111+
xrt_runlist_add add;
112+
xrt_runlist_execute execute;
113+
xrt_runlist_wait wait;
114+
xrt_runlist_reset reset;
115+
};

0 commit comments

Comments
 (0)