Skip to content

Commit bbe4edd

Browse files
Make KmdNotifyHelper thread safe
Change-Id: Iace168eb849e0d7090d17d63d7b47b057ff2385d
1 parent fa2416b commit bbe4edd

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

runtime/helpers/kmd_notify_properties.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ bool KmdNotifyHelper::obtainTimeoutParams(int64_t &timeoutValueOutput,
5050

5151
bool KmdNotifyHelper::applyQuickKmdSleepForSporadicWait() const {
5252
if (properties->enableQuickKmdSleepForSporadicWaits) {
53-
auto now = std::chrono::high_resolution_clock::now();
54-
auto timeDiff = std::chrono::duration_cast<std::chrono::microseconds>(now - lastWaitForCompletionTimestamp).count();
53+
auto timeDiff = getMicrosecondsSinceEpoch() - lastWaitForCompletionTimestampUs.load();
5554
if (timeDiff > properties->delayQuickKmdSleepForSporadicWaitsMicroseconds) {
5655
return true;
5756
}
@@ -60,7 +59,12 @@ bool KmdNotifyHelper::applyQuickKmdSleepForSporadicWait() const {
6059
}
6160

6261
void KmdNotifyHelper::updateLastWaitForCompletionTimestamp() {
63-
lastWaitForCompletionTimestamp = std::chrono::high_resolution_clock::now();
62+
lastWaitForCompletionTimestampUs = getMicrosecondsSinceEpoch();
63+
}
64+
65+
int64_t KmdNotifyHelper::getMicrosecondsSinceEpoch() const {
66+
auto now = std::chrono::high_resolution_clock::now().time_since_epoch();
67+
return std::chrono::duration_cast<std::chrono::microseconds>(now).count();
6468
}
6569

6670
void KmdNotifyHelper::overrideFromDebugVariable(int32_t debugVariableValue, int64_t &destination) {

runtime/helpers/kmd_notify_properties.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <cstdint>
2727
#include <chrono>
28+
#include <atomic>
2829

2930
namespace OCLRT {
3031
struct KmdNotifyProperties {
@@ -66,9 +67,10 @@ class KmdNotifyHelper {
6667
protected:
6768
bool applyQuickKmdSleepForSporadicWait() const;
6869
int64_t getBaseTimeout(const int64_t &multiplier) const;
70+
int64_t getMicrosecondsSinceEpoch() const;
6971

7072
const KmdNotifyProperties *properties = nullptr;
71-
std::chrono::high_resolution_clock::time_point lastWaitForCompletionTimestamp;
72-
bool acLineConnected = true;
73+
std::atomic<int64_t> lastWaitForCompletionTimestampUs{0};
74+
std::atomic<bool> acLineConnected{true};
7375
};
7476
} // namespace OCLRT

unit_tests/helpers/kmd_notify_tests.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ struct KmdNotifyTests : public ::testing::Test {
5757
class MockKmdNotifyHelper : public KmdNotifyHelper {
5858
public:
5959
using KmdNotifyHelper::acLineConnected;
60-
using KmdNotifyHelper::lastWaitForCompletionTimestamp;
60+
using KmdNotifyHelper::getMicrosecondsSinceEpoch;
61+
using KmdNotifyHelper::lastWaitForCompletionTimestampUs;
62+
using KmdNotifyHelper::properties;
6163

6264
MockKmdNotifyHelper() = delete;
6365
MockKmdNotifyHelper(const KmdNotifyProperties *newProperties) : KmdNotifyHelper(newProperties){};
@@ -190,14 +192,15 @@ HWTEST_F(KmdNotifyTests, givenZeroFlushStampWhenWaitIsCalledThenDisableTimeout)
190192
}
191193

192194
HWTEST_F(KmdNotifyTests, givenNonQuickSleepRequestWhenItsSporadicWaitThenOverrideQuickSleepRequest) {
193-
overrideKmdNotifyParams(true, 3, true, 2, true, 0);
195+
overrideKmdNotifyParams(true, 3, true, 2, true, 1);
194196
auto csr = createMockCsr<FamilyType>();
195197

196198
auto expectedDelay = device->getHardwareInfo().capabilityTable.kmdNotifyProperties.delayQuickKmdSleepMicroseconds;
197199
EXPECT_CALL(*csr, waitForCompletionWithTimeout(::testing::_, expectedDelay, ::testing::_)).Times(1).WillOnce(::testing::Return(true));
198200

199-
auto now = std::chrono::high_resolution_clock::now();
200-
mockKmdNotifyHelper->lastWaitForCompletionTimestamp = now - std::chrono::hours(24);
201+
int64_t timeSinceLastWait = mockKmdNotifyHelper->properties->delayQuickKmdSleepForSporadicWaitsMicroseconds + 1;
202+
203+
mockKmdNotifyHelper->lastWaitForCompletionTimestampUs = mockKmdNotifyHelper->getMicrosecondsSinceEpoch() - timeSinceLastWait;
201204
csr->waitForTaskCountWithKmdNotifyFallback(taskCountToWait, 1, false);
202205
}
203206

@@ -247,7 +250,7 @@ HWTEST_F(KmdNotifyTests, givenDefaultCommandStreamReceiverWhenWaitCalledThenUpda
247250
overrideKmdNotifyParams(true, 3, true, 2, true, 1);
248251

249252
auto csr = createMockCsr<FamilyType>();
250-
EXPECT_NE(0, mockKmdNotifyHelper->lastWaitForCompletionTimestamp.time_since_epoch().count());
253+
EXPECT_NE(0, mockKmdNotifyHelper->lastWaitForCompletionTimestampUs.load());
251254

252255
EXPECT_EQ(1u, mockKmdNotifyHelper->updateLastWaitForCompletionTimestampCalled);
253256
csr->waitForTaskCountWithKmdNotifyFallback(0, 0, false);
@@ -258,7 +261,7 @@ HWTEST_F(KmdNotifyTests, givenDefaultCommandStreamReceiverWithDisabledSporadicWa
258261
overrideKmdNotifyParams(true, 3, true, 2, false, 0);
259262

260263
auto csr = createMockCsr<FamilyType>();
261-
EXPECT_EQ(0, mockKmdNotifyHelper->lastWaitForCompletionTimestamp.time_since_epoch().count());
264+
EXPECT_EQ(0, mockKmdNotifyHelper->lastWaitForCompletionTimestampUs.load());
262265

263266
csr->waitForTaskCountWithKmdNotifyFallback(0, 0, false);
264267
EXPECT_EQ(0u, mockKmdNotifyHelper->updateLastWaitForCompletionTimestampCalled);

0 commit comments

Comments
 (0)