Merged
refactor(machine): Remove 1G block in memoryPool. #5106
chaizhengtongAdd on Jul 27
refactor(machine): Remove 1G block in memoryPool. #5106
Merged
chaizhengtongAdd on Jul 27
Total: 3 files changed+24-196
@@ -21,7 +21,6 @@
21namespace npu::tile_fwk {21namespace npu::tile_fwk {
22namespace {22namespace {
23inline constexpr int RTMALLOC_SUCCESS = 0;23inline constexpr int RTMALLOC_SUCCESS = 0;
24-inline constexpr size_t ONT_GB_SIZE = 1024 * 1024 * 1024;
25inline constexpr uint64_t SENTINEL_VALUE = 0xDEADBEEFDEADBEEF;24inline constexpr uint64_t SENTINEL_VALUE = 0xDEADBEEFDEADBEEF;
26inline constexpr uint32_t SENTINEL_NUM = 64;25inline constexpr uint32_t SENTINEL_NUM = 64;
27inline constexpr uint32_t SENTINEL_MEM_SIZE = 512;26inline constexpr uint32_t SENTINEL_MEM_SIZE = 512;
@@ -41,86 +40,17 @@ RtError NormalizedRtMemcpy(void* dst, uint64_t destMax, const void* src, uint64_
41 return RuntimeMemcpyDirect(dst, destMax, src, cnt, kind);40 return RuntimeMemcpyDirect(dst, destMax, src, cnt, kind);
42}41}
43 42 
44-MemoryBlock::MemoryBlock(void* addr, size_t size, bool isHuge)43+MemoryBlock::MemoryBlock(void* addr, size_t size) : baseAddr(addr), blockSize(size), usedSize(0) {}
45- : baseAddr(addr), blockSize(size), usedSize(0), isHuge1G(isHuge)
46-{
47- Init();
48-}
49- 
50-void MemoryBlock::Init()
51-{
52- if (isHuge1G) {
53- freeMap[reinterpret_cast<uintptr_t>(baseAddr)] = blockSize;
54- } else {
55- freeMap.clear();
56- }
57-}
58 44 
59void* MemoryBlock::Allocate(uint64_t alignSize)45void* MemoryBlock::Allocate(uint64_t alignSize)
60{46{
61- if (!isHuge1G) {47+ if (usedSize == 0 && blockSize >= alignSize) {
62- if (usedSize == 0 && blockSize >= alignSize) {48+ usedSize = blockSize;
63- usedSize = blockSize;49+ return baseAddr;
64- return baseAddr;
65- }
66- return nullptr;
67- }
68- 
69- for (auto it = freeMap.begin(); it != freeMap.end(); ++it) {
70- uintptr_t chunkAddr = it->first;
71- size_t chunkSize = it->second;
72- 
73- if (chunkSize >= alignSize) {
74- void* usePtr = reinterpret_cast<void*>(chunkAddr);
75- size_t remaining = chunkSize - alignSize;
76- 
77- freeMap.erase(it);
78- 
79- if (remaining > 0) {
80- freeMap[chunkAddr + alignSize] = remaining;
81- }
82- 
83- usedSize += alignSize;
84- MACHINE_LOGI("Allocate in 1GB block: ptr=%p, chunkSize=%zu, alignSize=%lu.", usePtr, chunkSize, alignSize);
85- return usePtr;
86- }
87 }50 }
88 return nullptr;51 return nullptr;
89}52}
90 53 
91-void MemoryBlock::Free(void* ptr, size_t size)
92-{
93- if (!isHuge1G) {
94- MACHINE_LOGE(DevCommonErr::FREE_FAILED, "Logic Error: 2MB block should not call Free()");
95- return;
96- }
97- 
98- uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
99- 
100- freeMap[addr] = size;
101- usedSize -= size;
102- 
103- auto it = freeMap.find(addr);
104- if (it == freeMap.end())
105- return;
106- 
107- auto nextIt = std::next(it);
108- if (nextIt != freeMap.end()) {
109- if (it->first + it->second == nextIt->first) {
110- it->second += nextIt->second;
111- freeMap.erase(nextIt);
112- }
113- }
114- 
115- if (it != freeMap.begin()) {
116- auto prevIt = std::prev(it);
117- if (prevIt->first + prevIt->second == it->first) {
118- prevIt->second += it->second;
119- freeMap.erase(it);
120- }
121- }
122-}
123- 
124DevMemoryPool& DevMemoryPool::Instance()54DevMemoryPool& DevMemoryPool::Instance()
125{55{
126 static DevMemoryPool memoryPool;56 static DevMemoryPool memoryPool;
@@ -165,7 +95,7 @@ bool DevMemoryPool::AllocDevAddrInPool(uint8_t** devAddr, const uint64_t size)
165 void* ptr = block->Allocate(alignSize);95 void* ptr = block->Allocate(alignSize);
166 if (ptr != nullptr) {96 if (ptr != nullptr) {
167 *devAddr = static_cast<uint8_t*>(ptr);97 *devAddr = static_cast<uint8_t*>(ptr);
168- RecordAllocation(ptr, block, alignSize);98+ RecordAllocation(ptr, block);
169 PutSentinelAddr(*devAddr, size);99 PutSentinelAddr(*devAddr, size);
170 return true;100 return true;
171 }101 }
@@ -176,7 +106,7 @@ bool DevMemoryPool::AllocDevAddrInPool(uint8_t** devAddr, const uint64_t size)
176 void* ptr = newBlock->Allocate(alignSize);106 void* ptr = newBlock->Allocate(alignSize);
177 if (ptr != nullptr) {107 if (ptr != nullptr) {
178 *devAddr = static_cast<uint8_t*>(ptr);108 *devAddr = static_cast<uint8_t*>(ptr);
179- RecordAllocation(ptr, newBlock, alignSize);109+ RecordAllocation(ptr, newBlock);
180 PutSentinelAddr(*devAddr, size);110 PutSentinelAddr(*devAddr, size);
181 return true;111 return true;
182 }112 }
@@ -201,23 +131,17 @@ void DevMemoryPool::FreeDevAddr(void* ptr)
201 }131 }
202 132 
203 MemoryBlock* block = it->second;133 MemoryBlock* block = it->second;
204- size_t size = allocSizes_[ptr];
205 134 
206- if (block->isHuge1G) {135+ MACHINE_LOGI("Directly freeing 2MB block: addr=%p.", block->baseAddr);
207- block->Free(ptr, size);136+ FreeMemBlock(block);
208- } else {137+ for (auto vecIt = memoryBlocks_.begin(); vecIt != memoryBlocks_.end(); ++vecIt) {
209- MACHINE_LOGI("Directly freeing 2MB block: addr=%p.", block->baseAddr);138+ if (*vecIt == block) {
210- FreeMemBlock(block);139+ memoryBlocks_.erase(vecIt);
211- for (auto vecIt = memoryBlocks_.begin(); vecIt != memoryBlocks_.end(); ++vecIt) {140+ break;
212- if (*vecIt == block) {
213- memoryBlocks_.erase(vecIt);
214- break;
215- }
216 }141 }
217 }142 }
218 143 
219 addrToBlock_.erase(it);144 addrToBlock_.erase(it);
220- allocSizes_.erase(ptr);
221}145}
222 146 
223void DevMemoryPool::PutSentinelAddr(uint8_t* baseAddr, uint64_t baseSize)147void DevMemoryPool::PutSentinelAddr(uint8_t* baseAddr, uint64_t baseSize)
@@ -335,31 +259,23 @@ void DevMemoryPool::DestroyPool()
335 }259 }
336 memoryBlocks_.clear();260 memoryBlocks_.clear();
337 addrToBlock_.clear();261 addrToBlock_.clear();
338- allocSizes_.clear();
339 MACHINE_LOGI("MemPool destroyed, all memory freed");262 MACHINE_LOGI("MemPool destroyed, all memory freed");
340}263}
341 264 
342void DevMemoryPool::PrintPoolStatus() const265void DevMemoryPool::PrintPoolStatus() const
343{266{
344- size_t cnt1G = 0;
345- size_t cnt2M = 0;
346 size_t total = 0;267 size_t total = 0;
347 size_t used = 0;268 size_t used = 0;
348 MACHINE_LOGI("========== [Memory Pool Status] ==========");269 MACHINE_LOGI("========== [Memory Pool Status] ==========");
349 for (size_t i = 0; i < memoryBlocks_.size(); ++i) {270 for (size_t i = 0; i < memoryBlocks_.size(); ++i) {
350 auto* blk = memoryBlocks_[i];271 auto* blk = memoryBlocks_[i];
351- if (blk->isHuge1G)
352- cnt1G++;
353- else
354- cnt2M++;
355 total += blk->blockSize;272 total += blk->blockSize;
356 used += blk->usedSize;273 used += blk->usedSize;
357 274 
358 double rate = blk->blockSize ? (double)blk->usedSize * 100.0 / blk->blockSize : 0;275 double rate = blk->blockSize ? (double)blk->usedSize * 100.0 / blk->blockSize : 0;
359- MACHINE_LOGI("Block[%lu] %s | Addr: %p | Used: %.1f%% | Fragments: %lu", i, blk->isHuge1G ? "1G" : "2M",276+ MACHINE_LOGI("Block[%lu] | Addr: %p | Used: %.1f%%", i, blk->baseAddr, rate);
360- blk->baseAddr, rate, blk->freeMap.size());
361 }277 }
362- MACHINE_LOGI("Summary: 1G x %lu, 2M x %lu | Used/Total: %lu/%lu MB", cnt1G, cnt2M, used >> 20, total >> 20);278+ MACHINE_LOGI("Summary: Blocks: %lu | Used/Total: %lu/%lu MB", memoryBlocks_.size(), used >> 20, total >> 20);
363}279}
364 280 
365void DevMemoryPool::FreeMemBlock(MemoryBlock* block)281void DevMemoryPool::FreeMemBlock(MemoryBlock* block)
@@ -377,25 +293,14 @@ void DevMemoryPool::FreeMemBlock(MemoryBlock* block)
377 block = nullptr;293 block = nullptr;
378}294}
379 295 
380-void DevMemoryPool::RecordAllocation(void* ptr, MemoryBlock* block, size_t size)296+void DevMemoryPool::RecordAllocation(void* ptr, MemoryBlock* block) { addrToBlock_[ptr] = block; }
381-{
382- addrToBlock_[ptr] = block;
383- allocSizes_[ptr] = size;
384-}
385 297 
386MemoryBlock* DevMemoryPool::CreateNewBlock(uint64_t alignSize)298MemoryBlock* DevMemoryPool::CreateNewBlock(uint64_t alignSize)
387{299{
388 uint8_t* devAddr = nullptr;300 uint8_t* devAddr = nullptr;
389- size_t size1G = ((alignSize - 1) / ONT_GB_SIZE + 1) * ONT_GB_SIZE;
390- 
391- if (RuntimeMalloc((void**)&devAddr, size1G, ONG_GB_HUGE_PAGE_FLAGS, 0) == RTMALLOC_SUCCESS) {
392- MemoryBlock* block = new MemoryBlock(devAddr, size1G, true);
393- memoryBlocks_.push_back(block);
394- return block;
395- }
396 301 
397 if (RuntimeMalloc((void**)&devAddr, alignSize, TWO_MB_HUGE_PAGE_FLAGS, 0) == RTMALLOC_SUCCESS) {302 if (RuntimeMalloc((void**)&devAddr, alignSize, TWO_MB_HUGE_PAGE_FLAGS, 0) == RTMALLOC_SUCCESS) {
398- MemoryBlock* block = new MemoryBlock(devAddr, alignSize, false);303+ MemoryBlock* block = new MemoryBlock(devAddr, alignSize);
399 memoryBlocks_.push_back(block);304 memoryBlocks_.push_back(block);
400 return block;305 return block;
401 }306 }
@@ -15,27 +15,20 @@
15 15 
16#pragma once16#pragma once
17 17 
18-#include <map>
19#include <unordered_map>18#include <unordered_map>
20#include <vector>19#include <vector>
21#include "adapter/api/runtime_define.h"20#include "adapter/api/runtime_define.h"
22 21 
23namespace npu::tile_fwk {22namespace npu::tile_fwk {
24-inline constexpr uint32_t ONG_GB_HUGE_PAGE_FLAGS = RT_MEMORY_HBM | RT_MEMORY_POLICY_HUGE1G_PAGE_ONLY;
25inline constexpr uint32_t TWO_MB_HUGE_PAGE_FLAGS = RT_MEMORY_HBM | RT_MEMORY_POLICY_HUGE_PAGE_FIRST;23inline constexpr uint32_t TWO_MB_HUGE_PAGE_FLAGS = RT_MEMORY_HBM | RT_MEMORY_POLICY_HUGE_PAGE_FIRST;
26 24 
27struct MemoryBlock {25struct MemoryBlock {
28 void* baseAddr;26 void* baseAddr;
29 size_t blockSize;27 size_t blockSize;
30 size_t usedSize;28 size_t usedSize;
31- bool isHuge1G;
32 29 
33- std::map<uintptr_t, size_t> freeMap;30+ MemoryBlock(void* addr, size_t size);
34- 
35- MemoryBlock(void* addr, size_t size, bool isHuge);
36- void Init();
37 void* Allocate(uint64_t alignSize);31 void* Allocate(uint64_t alignSize);
38- void Free(void* ptr, size_t size);
39};32};
40 33 
41RtError NormalizedRtMemcpy(void* dst, uint64_t destMax, const void* src, uint64_t cnt, RtMemcpyKind kind);34RtError NormalizedRtMemcpy(void* dst, uint64_t destMax, const void* src, uint64_t cnt, RtMemcpyKind kind);
@@ -59,14 +52,13 @@ private:
59 static void PrintSentinelVal(std::vector<uint64_t>& sentinelVal, uint8_t* sentinelAddr);52 static void PrintSentinelVal(std::vector<uint64_t>& sentinelVal, uint8_t* sentinelAddr);
60 void PutSentinelAddr(uint8_t* baseAddr, uint64_t baseSize);53 void PutSentinelAddr(uint8_t* baseAddr, uint64_t baseSize);
61 bool CheckSentinel(uint8_t* baseAddr, bool remove = true);54 bool CheckSentinel(uint8_t* baseAddr, bool remove = true);
62- void RecordAllocation(void* ptr, MemoryBlock* block, size_t size);55+ void RecordAllocation(void* ptr, MemoryBlock* block);
63 MemoryBlock* CreateNewBlock(uint64_t alignSize);56 MemoryBlock* CreateNewBlock(uint64_t alignSize);
64 void DynamicRecycle();57 void DynamicRecycle();
65 void PrintPoolStatus() const;58 void PrintPoolStatus() const;
66 59 
67 std::vector<MemoryBlock*> memoryBlocks_;60 std::vector<MemoryBlock*> memoryBlocks_;
68 std::unordered_map<void*, MemoryBlock*> addrToBlock_;61 std::unordered_map<void*, MemoryBlock*> addrToBlock_;
69- std::unordered_map<void*, size_t> allocSizes_;
70 62 
71 bool needMemCheck_{false};63 bool needMemCheck_{false};
72 std::vector<uint64_t> sentinelVec_;64 std::vector<uint64_t> sentinelVec_;
@@ -18,101 +18,32 @@
18 18 
19using namespace npu::tile_fwk;19using namespace npu::tile_fwk;
20 20 
21-TEST(MemoryBlockTest, NonHuge_FirstAllocation_ReturnsBaseAddr)21+TEST(MemoryBlockTest, FirstAllocation_ReturnsBaseAddr)
22{22{
23 uint8_t buf[1024];23 uint8_t buf[1024];
24- MemoryBlock block(buf, 1024, false);24+ MemoryBlock block(buf, 1024);
25 void* ptr = block.Allocate(512);25 void* ptr = block.Allocate(512);
26 EXPECT_EQ(ptr, static_cast<void*>(buf));26 EXPECT_EQ(ptr, static_cast<void*>(buf));
27 EXPECT_EQ(block.usedSize, 1024u);27 EXPECT_EQ(block.usedSize, 1024u);
28}28}
29 29 
30-TEST(MemoryBlockTest, NonHuge_SecondAllocation_ReturnsNull)30+TEST(MemoryBlockTest, SecondAllocation_ReturnsNull)
31{31{
32 uint8_t buf[1024];32 uint8_t buf[1024];
33- MemoryBlock block(buf, 1024, false);33+ MemoryBlock block(buf, 1024);
34 block.Allocate(512);34 block.Allocate(512);
35 void* ptr = block.Allocate(512);35 void* ptr = block.Allocate(512);
36 EXPECT_EQ(ptr, nullptr);36 EXPECT_EQ(ptr, nullptr);
37}37}
38 38 
39-TEST(MemoryBlockTest, NonHuge_AlignExceedsBlockSize_ReturnsNull)39+TEST(MemoryBlockTest, AlignExceedsBlockSize_ReturnsNull)
40{40{
41 uint8_t buf[256];41 uint8_t buf[256];
42- MemoryBlock block(buf, 256, false);42+ MemoryBlock block(buf, 256);
43 void* ptr = block.Allocate(512);43 void* ptr = block.Allocate(512);
44 EXPECT_EQ(ptr, nullptr);44 EXPECT_EQ(ptr, nullptr);
45}45}
46 46 
47-TEST(MemoryBlockTest, NonHuge_Free_ThrowsError)
48-{
49- uint8_t buf[1024];
50- MemoryBlock block(buf, 1024, false);
51- EXPECT_THROW(block.Free(buf, 512), std::exception);
52-}
53- 
54-TEST(MemoryBlockTest, Huge_FirstAllocation_ReturnsBaseAddr)
55-{
56- uintptr_t base = 0x1000;
57- MemoryBlock block(reinterpret_cast<void*>(base), 4096, true);
58- void* ptr = block.Allocate(512);
59- EXPECT_EQ(ptr, reinterpret_cast<void*>(base));
60-}
61- 
62-TEST(MemoryBlockTest, Huge_SecondAllocation_ReturnsNextChunk)
63-{
64- uintptr_t base = 0x1000;
65- MemoryBlock block(reinterpret_cast<void*>(base), 4096, true);
66- void* ptr1 = block.Allocate(512);
67- void* ptr2 = block.Allocate(512);
68- EXPECT_EQ(ptr1, reinterpret_cast<void*>(base));
69- EXPECT_EQ(ptr2, reinterpret_cast<void*>(base + 512));
70-}
71- 
72-TEST(MemoryBlockTest, Huge_AllocationExceedsFreeSpace_ReturnsNull)
73-{
74- uintptr_t base = 0x1000;
75- MemoryBlock block(reinterpret_cast<void*>(base), 512, true);
76- block.Allocate(512);
77- void* ptr = block.Allocate(512);
78- EXPECT_EQ(ptr, nullptr);
79-}
80- 
81-TEST(MemoryBlockTest, Huge_FreeAndReallocate)
82-{
83- uintptr_t base = 0x1000;
84- MemoryBlock block(reinterpret_cast<void*>(base), 4096, true);
85- void* ptr1 = block.Allocate(1024);
86- block.Free(ptr1, 1024);
87- void* ptr2 = block.Allocate(1024);
88- EXPECT_EQ(ptr2, reinterpret_cast<void*>(base));
89-}
90- 
91-TEST(MemoryBlockTest, Huge_FreeMergesAdjacentChunks)
92-{
93- uintptr_t base = 0x1000;
94- MemoryBlock block(reinterpret_cast<void*>(base), 4096, true);
95- void* ptr1 = block.Allocate(1024);
96- void* ptr2 = block.Allocate(1024);
97- block.Free(ptr1, 1024);
98- block.Free(ptr2, 1024);
99- void* ptr3 = block.Allocate(4096);
100- EXPECT_NE(ptr3, nullptr);
101-}
102- 
103-TEST(MemoryBlockTest, Huge_FreeNonAdjacentChunks_NoMerge)
104-{
105- uintptr_t base = 0x1000;
106- MemoryBlock block(reinterpret_cast<void*>(base), 4096, true);
107- void* ptr1 = block.Allocate(1024);
108- void* ptr2 = block.Allocate(1024);
109- void* ptr3 = block.Allocate(1024);
110- (void)ptr2;
111- block.Free(ptr1, 1024);
112- block.Free(ptr3, 1024);
113- EXPECT_EQ(block.freeMap.size(), 2u);
114-}
115- 
116// ===== DevMemoryPool tests =====47// ===== DevMemoryPool tests =====
117// DevMemoryPool is a singleton backed by RuntimeMalloc (stub->malloc in UT). Tests below exercise48// DevMemoryPool is a singleton backed by RuntimeMalloc (stub->malloc in UT). Tests below exercise
118// FreeDevAddr/CheckAllSentinels/DynamicRecycle/PrintPoolStatus/DestroyPool/DevAlloc/CopyDataToDevice.49// FreeDevAddr/CheckAllSentinels/DynamicRecycle/PrintPoolStatus/DestroyPool/DevAlloc/CopyDataToDevice.