Skip to contentSkip to conversation

kfdtest: Also detect under-reporting of available memory

PATCH New Daniel Phillips 1 participant 1 message Oct 31, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kfdtest: Also detect under-reporting of available memory

Detect under-reporting of available memory by initially attempting to allocate substantially more than reported available memory, and ensure that the allocation fails. Continue shrinking the attempted allocation until it succeeds, then fail the test if the successful allocation is either too much more than or too much less than reported available.

Change-Id Ib418f0aa26e8db80590a6c5f2578da56a4b60f2b
---
 tests/kfdtest/src/KFDMemoryTest.cpp | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tests/kfdtest/src/KFDMemoryTest.cpp +13 -4
index b4cb37a..f6affec 100644
@@ -252,23 +252,32 @@ TEST_F(KFDMemoryTest, MemoryAlloc) {
 // Basic test for hsaKmtAllocMemory
 TEST_F(KFDMemoryTest, MemoryAllocAll) {
     TEST_START(TESTPROFILE_RUNALL)
-
     int defaultGPUNode = m_NodeInfo.HsaDefaultGPUNode();
     HsaMemFlags memFlags = {0};
     memFlags.ui32.NonPaged = 1; // sys mem vs vram
     HSAuint64 available;
     void *object = NULL;
     int shrink = 21, success = HSAKMT_STATUS_NO_MEMORY;
-
     EXPECT_SUCCESS(hsaKmtAvailableMemory(defaultGPUNode, &available));
     LOG() << "Available: " << available << " bytes" << std::endl;
+    HSAuint64 leeway = (10 << shrink), size = available + leeway;
     for (int i = 0; i < available >> shrink; i++) {
-        HSAuint64 size = available - ((HSAuint64)i << shrink);
         if (hsaKmtAllocMemory(defaultGPUNode, size, memFlags, &object) == HSAKMT_STATUS_SUCCESS) {
-            LOG() << "Allocated: " << size << " bytes" << std::endl;
             success = hsaKmtFreeMemory(object, available);
             break;
         }
+        size -= (1 << shrink);
+    }
+    if (success == HSAKMT_STATUS_SUCCESS) {
+        LOG() << "Allocated: " << size << " bytes" << std::endl;
+        if (size > available + leeway) {
+            LOG() << "Under-reported available memory!" << std::endl;
+            success = HSAKMT_STATUS_ERROR;
+        }
+        if (size < available - leeway) {
+            LOG() << "Over-reported available memory!" << std::endl;
+            success = HSAKMT_STATUS_NO_MEMORY;
+        }
     }
     EXPECT_SUCCESS(success);
     TEST_END
Signature · 1 line
2.35.1