Skip to content

[SYCL] Profile host events only if the submitted queue has profiling enabled #18982

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

Open
wants to merge 9 commits into
base: sycl
Choose a base branch
from
Open
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
46 changes: 18 additions & 28 deletions sycl/source/detail/event_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,9 @@ event_impl::event_impl(queue_impl &Queue, private_tag)
}

event_impl::event_impl(HostEventState State, private_tag) : MState(State) {
switch (State) {
case HES_Discarded:
case HES_Complete: {
MIsHostEvent = true;
if (State == HES_Discarded || State == HES_Complete)
MIsFlushed = true;
MIsHostEvent = true;
break;
}
case HES_NotComplete: {
MIsProfilingEnabled = true;
MHostProfilingInfo.reset(new HostProfilingInfo());
if (!MHostProfilingInfo)
throw sycl::exception(
sycl::make_error_code(sycl::errc::runtime),
"Out of host memory " +
codeToString(UR_RESULT_ERROR_OUT_OF_HOST_MEMORY));
}
}
}

void event_impl::setQueue(queue_impl &Queue) {
Expand All @@ -215,14 +201,23 @@ void event_impl::setQueue(queue_impl &Queue) {
MIsDefaultConstructed = false;
}

void event_impl::initHostProfilingInfo() {
assert(isHost() && "This must be a host event");
assert(MState == HES_NotComplete &&
"Host event must be incomplete to initialize profiling info");

std::shared_ptr<queue_impl> QueuePtr = MSubmittedQueue.lock();
assert(QueuePtr && "Queue must be valid to initialize host profiling info");
assert(QueuePtr->MIsProfilingEnabled && "Queue must have profiling enabled");

MIsProfilingEnabled = true;
MHostProfilingInfo = std::make_unique<HostProfilingInfo>();
device_impl &Device = QueuePtr->getDeviceImpl();
MHostProfilingInfo->setDevice(&Device);
}

void event_impl::setSubmittedQueue(std::weak_ptr<queue_impl> SubmittedQueue) {
MSubmittedQueue = std::move(SubmittedQueue);
if (MHostProfilingInfo) {
if (std::shared_ptr<queue_impl> QueuePtr = MSubmittedQueue.lock()) {
device_impl &Device = QueuePtr->getDeviceImpl();
MHostProfilingInfo->setDevice(&Device);
}
}
}

void *event_impl::instrumentationProlog(std::string &Name, int32_t StreamID,
Expand Down Expand Up @@ -621,12 +616,7 @@ bool event_impl::isCompleted() {
info::event_command_status::complete;
}

void event_impl::setCommand(void *Cmd) {
MCommand = Cmd;
auto TypedCommand = static_cast<Command *>(Cmd);
if (TypedCommand)
MIsHostEvent = TypedCommand->getWorkerContext() == nullptr;
}
void event_impl::setCommand(void *Cmd) { MCommand = Cmd; }

} // namespace detail
} // namespace _V1
Expand Down
3 changes: 3 additions & 0 deletions sycl/source/detail/event_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ class event_impl : public std::enable_shared_from_this<event_impl> {
return MEvent && MQueue.expired() && !MIsEnqueued && !MCommand;
}

// Initializes the host profiling info for the event.
void initHostProfilingInfo();

protected:
// When instrumentation is enabled emits trace event for event wait begin and
// returns the telemetry event generated for the wait
Expand Down
11 changes: 9 additions & 2 deletions sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,8 +1954,15 @@ ExecCGCommand::ExecCGCommand(
: Command(CommandType::RUN_CG, Queue, CommandBuffer, Dependencies),
MEventNeeded(EventNeeded), MCommandGroup(std::move(CommandGroup)) {
if (MCommandGroup->getType() == detail::CGType::CodeplayHostTask) {
MEvent->setSubmittedQueue(
static_cast<detail::CGHostTask *>(MCommandGroup.get())->MQueue);
queue_impl *SubmitQueue =
static_cast<detail::CGHostTask *>(MCommandGroup.get())->MQueue.get();
assert(SubmitQueue &&
"Host task command group must have a valid submit queue");

MEvent->setSubmittedQueue(SubmitQueue->weak_from_this());
// Initialize host profiling info if the queue has profiling enabled.
if (SubmitQueue->MIsProfilingEnabled)
MEvent->initHostProfilingInfo();
}
if (MCommandGroup->getType() == detail::CGType::ProfilingTag)
MEvent->markAsProfilingTagEvent();
Expand Down
31 changes: 31 additions & 0 deletions sycl/unittests/queue/GetProfilingInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,34 @@ TEST(GetProfilingInfo, check_command_submission_time_with_host_accessor) {

EXPECT_TRUE(DeviceTimerCalled);
}

// Check that query fails for host task if queue doesn't have profiling
// enabled.
TEST(GetProfilingInfo, check_host_task_profiling_info) {
using namespace sycl;
[[maybe_unused]] unittest::UrMock<> Mock;
queue Queue;
event E = Queue.submit([&](handler &cgh) { cgh.host_task([]() {}); });

auto expect_profiling_exception = [&](auto profiling_query) {
try {
std::ignore = profiling_query();
FAIL();
} catch (sycl::exception const &e) {
EXPECT_STREQ(
e.what(),
"Profiling information is unavailable as the queue associated "
"with the event does not have the 'enable_profiling' property.");
}
};

expect_profiling_exception([&] {
return E.get_profiling_info<info::event_profiling::command_submit>();
});
expect_profiling_exception([&] {
return E.get_profiling_info<info::event_profiling::command_start>();
});
expect_profiling_exception([&] {
return E.get_profiling_info<info::event_profiling::command_end>();
});
}
Loading