From 30d64979533acd2bb04b9fdeb2aa67f68490ce6f Mon Sep 17 00:00:00 2001 From: Satyanvesh Dittakavi Date: Thu, 27 Oct 2022 18:42:04 +0000 Subject: [PATCH] SWDEV-363369 - Elapsed time needs only stop event timings if both start and stop events are recorded Change-Id: I91c9ce79aacf1014ae77600d1250cfccd9dd6039 --- hipamd/src/hip_event.cpp | 31 ++++++++++++++++++------------- hipamd/src/hip_event.hpp | 14 +++++++------- hipamd/src/hip_event_ipc.cpp | 8 ++++---- hipamd/src/hip_module.cpp | 4 ++-- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/hipamd/src/hip_event.cpp b/hipamd/src/hip_event.cpp index 265bd18ee9..b53a00fe5e 100644 --- a/hipamd/src/hip_event.cpp +++ b/hipamd/src/hip_event.cpp @@ -128,14 +128,14 @@ hipError_t Event::elapsedTime(Event& eStop, float& ms) { return hipErrorNotReady; } - if (event_ == eStop.event_ && recorded_ && eStop.isRecorded()) { + if (event_ == eStop.event_) { // Events are the same, which indicates the stream is empty and likely // eventRecord is called on another stream. For such cases insert and measure a // marker. amd::Command* command = new amd::Marker(*event_->command().queue(), kMarkerDisableFlush); command->enqueue(); command->awaitCompletion(); - ms = static_cast(static_cast(command->event().profilingInfo().end_) - time()) / + ms = static_cast(static_cast(command->event().profilingInfo().end_) - time(false)) / 1000000.f; command->release(); } else { @@ -143,32 +143,37 @@ hipError_t Event::elapsedTime(Event& eStop, float& ms) { // Hence for now make sure CPU status is updated by calling awaitCompletion(); awaitEventCompletion(); eStop.awaitEventCompletion(); - ms = static_cast(eStop.time() - time()) / 1000000.f; + if (unrecorded_ && eStop.isUnRecorded()) { + // Both the events are not recorded, just need the end and start of stop event + ms = static_cast(eStop.time(false) - eStop.time(true)) / 1000000.f; + } else { + ms = static_cast(eStop.time(false) - time(false)) / 1000000.f; + } } return hipSuccess; } -int64_t Event::time() const { +int64_t Event::time(bool getStartTs) const { assert(event_ != nullptr); - if (recorded_) { - return static_cast(event_->profilingInfo().end_); - } else { + if (getStartTs) { return static_cast(event_->profilingInfo().start_); + } else { + return static_cast(event_->profilingInfo().end_); } } -int64_t EventDD::time() const { +int64_t EventDD::time(bool getStartTs) const { uint64_t start = 0, end = 0; assert(event_ != nullptr); g_devices[deviceId()]->devices()[0]->getHwEventTime(*event_, &start, &end); // FIXME: This is only needed if the command had to wait CL_COMPLETE status if (start == 0 || end == 0) { - return Event::time(); + return Event::time(getStartTs); } - if (recorded_) { - return static_cast(end); - } else { + if (getStartTs) { return static_cast(start); + } else { + return static_cast(end); } } @@ -238,7 +243,7 @@ hipError_t Event::enqueueRecordCommand(hipStream_t stream, amd::Command* command event_->release(); } event_ = &command->event(); - recorded_ = record; + unrecorded_ = !record; return hipSuccess; } diff --git a/hipamd/src/hip_event.hpp b/hipamd/src/hip_event.hpp index d6418b1607..bd8e70be1e 100644 --- a/hipamd/src/hip_event.hpp +++ b/hipamd/src/hip_event.hpp @@ -99,7 +99,7 @@ class Event { public: Event(unsigned int flags) : flags(flags), lock_("hipEvent_t", true), - event_(nullptr), recorded_(false), stream_(nullptr) { + event_(nullptr), unrecorded_(false), stream_(nullptr) { // No need to init event_ here as addMarker does that onCapture_ = false; device_id_ = hip::getCurrentDevice()->deviceId(); // Created in current device ctx @@ -131,11 +131,11 @@ class Event { event_->release(); } event_ = &command.event(); - recorded_ = record; + unrecorded_ = !record; command.retain(); } - bool isRecorded() const { return recorded_; } + bool isUnRecorded() const { return unrecorded_; } amd::Monitor& lock() { return lock_; } const int deviceId() const { return device_id_; } void setDeviceId(int id) { device_id_ = id; } @@ -171,17 +171,17 @@ class Event { } virtual bool awaitEventCompletion(); virtual bool ready(); - virtual int64_t time() const; + virtual int64_t time(bool getStartTs) const; protected: amd::Monitor lock_; amd::HostQueue* stream_; amd::Event* event_; int device_id_; - //! Flag to indicate hipEventRecord has been called. This is needed except for + //! Flag to indicate hipEventRecord has not been called. This is needed for //! hip*ModuleLaunchKernel API which takes start and stop events so no //! hipEventRecord is called. Cleanup needed once those APIs are deprecated. - bool recorded_; + bool unrecorded_; }; class EventDD : public Event { @@ -191,7 +191,7 @@ class EventDD : public Event { virtual bool awaitEventCompletion(); virtual bool ready(); - virtual int64_t time() const; + virtual int64_t time(bool getStartTs) const; }; class IPCEvent : public Event { diff --git a/hipamd/src/hip_event_ipc.cpp b/hipamd/src/hip_event_ipc.cpp index 1aa658b185..ca137809aa 100644 --- a/hipamd/src/hip_event_ipc.cpp +++ b/hipamd/src/hip_event_ipc.cpp @@ -141,8 +141,8 @@ hipError_t IPCEvent::streamWait(hipStream_t stream, uint flags) { } hipError_t IPCEvent::recordCommand(amd::Command*& command, amd::HostQueue* queue, uint32_t flags) { - bool recorded = isRecorded(); - if (!recorded) { + bool unrecorded = isUnRecorded(); + if (unrecorded) { command = new amd::Marker(*queue, kMarkerDisableFlush); } else { return Event::recordCommand(command, queue); @@ -152,8 +152,8 @@ hipError_t IPCEvent::recordCommand(amd::Command*& command, amd::HostQueue* queue hipError_t IPCEvent::enqueueRecordCommand(hipStream_t stream, amd::Command* command, bool record) { amd::HostQueue* queue = hip::getQueue(stream); - bool recorded = isRecorded(); - if (!recorded) { + bool unrecorded = isUnRecorded(); + if (unrecorded) { amd::Event& tEvent = command->event(); createIpcEventShmemIfNeeded(); int write_index = ipc_evt_.ipc_shmem_->write_index++; diff --git a/hipamd/src/hip_module.cpp b/hipamd/src/hip_module.cpp index 327fb9e8e7..5deb099e57 100644 --- a/hipamd/src/hip_module.cpp +++ b/hipamd/src/hip_module.cpp @@ -367,7 +367,7 @@ hipError_t ihipModuleLaunchKernel(hipFunction_t f, uint32_t globalWorkSizeX, if (startEvent != nullptr) { hip::Event* eStart = reinterpret_cast(startEvent); - status = eStart->addMarker(hStream, nullptr, true); + status = eStart->addMarker(hStream, nullptr, false); if (status != hipSuccess) { return status; } @@ -377,7 +377,7 @@ hipError_t ihipModuleLaunchKernel(hipFunction_t f, uint32_t globalWorkSizeX, if (stopEvent != nullptr) { hip::Event* eStop = reinterpret_cast(stopEvent); - eStop->BindCommand(*command, true); + eStop->BindCommand(*command, false); } command->release();