Skip to content
Closed
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
107 changes: 73 additions & 34 deletions src/runtime_src/core/common/api/xrt_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "core/common/trace.h"
#include "core/common/usage_metrics.h"
#include "core/common/xclbin_parser.h"
#include "core/common/xdp/profile.h"

#include <boost/format.hpp>

Expand Down Expand Up @@ -3350,6 +3351,46 @@ class runlist_impl
}
}

void
add_run_helper(xrt::run run)
{
// Get the potentially throwing action out of the way first
auto runidx = m_runlist.size();
m_runlist.reserve(runidx + 1);
m_bos.reserve(runidx + 1);

auto execbuf = get_cmd_chain_for_run_at_index(runidx);
auto [cmd, pkt] = unpack(execbuf);
auto chain_data = get_ert_cmd_chain_data(pkt);

auto run_impl = run.get_handle();
auto run_cmd = run_impl->get_cmd();
auto run_bo = run_cmd->get_exec_bo();
auto run_bo_props = run_bo->get_properties();

auto data_idx = chain_data->command_count;
chain_data->data[data_idx] = run_bo_props.kmhdl;

// Let shim handle binding of run_bo arguments to the command
// that cahins the run_bo. This allows pinning if necessary.
// May throw, but so far no state change, so still safe.
cmd->bind_at(data_idx, run_bo, 0, run_bo_props.size);

// Once a run object is added to a list it will be in a state that
// makes it impossible to add to another list or to same list
// twice. This state is managed by the run object itself by
// recording this runlist with the run object, but it doesn't
// proctect against caller manually controlling the run object,
// which is undefined behavior. No exceptions after this point.
run_impl->set_runlist(this); // throws or changes state of run

// Non throwing state change
chain_data->command_count++;
pkt->count += sizeof(uint64_t) / word_size; // account for added command
m_runlist.push_back(std::move(run)); // move of shared_ptr is noexcept
m_bos.push_back(run_bo); // ptr noexcept
}

// Wait for runlist to complete, then check each chained command
// submitted to determine potential error within chunk. Locate the
// first failing command if any and mark all subsequent commands as
Expand Down Expand Up @@ -3454,41 +3495,18 @@ class runlist_impl
if (m_state != state::idle)
throw xrt_core::error("runlist must be idle before adding run objects, current state: " + state_to_string(m_state));

// Get the potentially throwing action out of the way first
auto runidx = m_runlist.size();
m_runlist.reserve(runidx + 1);
m_bos.reserve(runidx + 1);

auto execbuf = get_cmd_chain_for_run_at_index(runidx);
auto [cmd, pkt] = unpack(execbuf);
auto chain_data = get_ert_cmd_chain_data(pkt);

auto run_impl = run.get_handle();
auto run_cmd = run_impl->get_cmd();
auto run_bo = run_cmd->get_exec_bo();
auto run_bo_props = run_bo->get_properties();

auto data_idx = chain_data->command_count;
chain_data->data[data_idx] = run_bo_props.kmhdl;

// Let shim handle binding of run_bo arguments to the command
// that cahins the run_bo. This allows pinning if necessary.
// May throw, but so far no state change, so still safe.
cmd->bind_at(data_idx, run_bo, 0, run_bo_props.size);

// Once a run object is added to a list it will be in a state that
// makes it impossible to add to another list or to same list
// twice. This state is managed by the run object itself by
// recording this runlist with the run object, but it doesn't
// proctect against caller manually controlling the run object,
// which is undefined behavior. No exceptions after this point.
run_impl->set_runlist(this); // throws or changes state of run
// Get XDP init runs registered with the hwctx and add to runlist
// if not already added.
// These runs initializes AI array for profile/trace data
// The list can be empty if profile/trace is not enabled
if (m_runlist.empty()) {
for (const auto& init_run : xrt_core::xdp::get_init_runs(m_hwctx.get_handle().get())) {
add_run_helper(init_run);
}
}

// Non throwing state change
chain_data->command_count++;
pkt->count += sizeof(uint64_t) / word_size; // account for added command
m_runlist.push_back(std::move(run)); // move of shared_ptr is noexcept
m_bos.push_back(run_bo); // ptr noexcept
// Now add the original run object
add_run_helper(std::move(run));
}

void
Expand All @@ -3500,6 +3518,13 @@ class runlist_impl
if (m_runlist.empty())
return;

// Add XDP exit runs if any at the end of runlist before submitting
// These runs collect profile/trace data
// The list can be empty if profile/trace is not enabled
for (const auto& exit_run : xrt_core::xdp::get_exit_runs(m_hwctx.get_handle().get())) {
add_run_helper(exit_run);
}

// Prep each run object
for (auto& run : m_runlist)
run.get_handle()->prep_start();
Expand Down Expand Up @@ -3539,6 +3564,20 @@ class runlist_impl

// On succesful wait, the runlist becomes idle
m_state = state::idle;

// Remove any XDP exit runs added during execute
// This is done because runlist can be reused
// and XDP exit runs should be added at end
const auto& xdp_exit_runs = xrt_core::xdp::get_exit_runs(m_hwctx.get_handle().get());
if (xdp_exit_runs.size() > 0) {
// remove the exit runs from the runlist
m_runlist.erase(m_runlist.end() - xdp_exit_runs.size(), m_runlist.end());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'size_type' (aka 'unsigned long') to signed type 'difference_type' (aka 'long') is implementation-defined [bugprone-narrowing-conversions]

      m_runlist.erase(m_runlist.end() - xdp_exit_runs.size(), m_runlist.end());
                                        ^


// clear the runlist associated with these exit runs
for (auto& run : xdp_exit_runs)
run.get_handle()->clear_runlist();
}

return std::cv_status::no_timeout;
}

Expand Down
16 changes: 16 additions & 0 deletions src/runtime_src/core/common/xdp/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,20 @@ finish_flush_device(void* handle)
#endif
}

const std::vector<xrt::run>&
get_init_runs(void* /*hwctx_impl*/)
{
// placeholder, TODO : XDP to add implementation
static std::vector<xrt::run> init_runs;
return init_runs;
}

const std::vector<xrt::run>&
get_exit_runs(void* /*hwctx_impl*/)
{
// placeholder, TODO : XDP to add implementation
static std::vector<xrt::run> exit_runs;
return exit_runs;
}

} // end namespace xrt_core::xdp
16 changes: 16 additions & 0 deletions src/runtime_src/core/common/xdp/profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef CORE_COMMON_PROFILE_DOT_H
#define CORE_COMMON_PROFILE_DOT_H

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

// The functions here are the general interfaces for the XDP hooks that are
// called from the common coreutil library and not the specific shims.
namespace xrt_core::xdp {
Expand All @@ -22,6 +24,20 @@ update_device(void* handle, bool hw_context_flow);
void
finish_flush_device(void* handle);

// get_init_runs returns vector of init runs to be prepended to a runlist.
// These runs initialize the AI array for profile/trace data collection.
// handle passed is the hw_context_impl pointer that is used to create runlist
// This call returns empty vector if no profile/trace options are enabled
const std::vector<xrt::run>&
get_init_runs(void* hwctx_impl);

// get_exit_runs returns vector of exit runs to be appended to a runlist.
// These runs are used to flush out the AI array profile/trace data
// handle passed is the hw_context_impl pointer that is used to create runlist.
// This call returns empty vector if no profile/trace options are enabled
const std::vector<xrt::run>&
get_exit_runs(void* hwctx_impl);

} // end namespace xrt_core::xdp

#endif
Loading