所谓“池化技术”,就是程序先向系统申请过量的资源,然后自己管理以备不时之需。之所以要申请过量的资源,是因为每次申请该资源都有较大的开销,不如提前申请好,这样使用时就会变得非常快捷,大大提高程序运行效率。在计算机中,有很多使用“池”这种技术的地方,除了内存池,还有连接池、线程池、对象池等。
以服务器上的线程池为例,它的主要思想是:先启动若干数量的线程,让它们处于睡眠状态,当接收到客户端的请求时,唤醒池中某个睡眠的线程,让它来处理客户端的请求,当处理完这个请求,线程又进入睡眠状态。
内存池是指程序预先从操作系统申请一块足够大内存,此后,当程序中需要申请内存的时候,不是直接向操作系统申请,而是直接从内存池中获取;同理,当程序释放内存的时候,并不真正将内存返回给操作系统,而是返回内存池。当程序退出(或者特定时间)时,内存池才将之前申请的内存真正释放。
内存池主要解决的当然是效率的问题,其次,作为系统的内存分配器的角度,还需要解决一下内存碎片的问题。
内存碎片分为外碎片和内碎片
C/C++中我们要动态申请内存都是通过malloc去申请内存,实际我们不是直接去堆获取内存的。
而malloc就是一个内存池。malloc() 相当于向操作系统申请了一块较大的内存空间。当内存用完或程序有大量的内存需求时,再根据实际需求向操作系统“申请。
申请内存使用的是malloc,什么场景下都可以用,但是意味着什么场景下都不会有很高的性能,下面我们就先来设计一个定长内存池
ObjectPool.h
#pragma once
#include
#include
#include
using std::cout;
using std::endl;
//定长内存池
//template
//class ObjectPool
//{};
#ifdef _WIN32
#include
#else
#endif
inline static void* SystemAlloc(size_t kpage)//直接去堆上按页申请内存
{
#ifdef _WIN32
void* ptr = VirtualAlloc(0, kpage<<13, MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
#else
// linux下brk mmap等
#endif
if (ptr == nullptr)
throw std::bad_alloc();
return ptr;
}
template
class ObjectPool
{
public:
T* New()
{
T* obj = nullptr;
if (_freeList)
{
//优先把还回来的内存块再次重复利用
void* next = (*(void**)_freeList);
obj = (T*)_freeList;
_freeList = next;
}
else
{
//剩余内存不够一个对象大小时,重新开大块空间
if (remainBytes < sizeof(T))
{
remainBytes = 128 * 1024 ;
//_memory = (char*)malloc(remainBytes);
_memory = (char*)SystemAlloc(remainBytes >> 13);
if (_memory == nullptr)
{
throw std::bad_alloc();
}
}
obj = (T*)_memory;
size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
_memory += objSize;
remainBytes -= objSize;
}
//定位new,显示调用T的构造函数初始化,对已有的空间初始化
new(obj)T;
return obj;
}
void Delete(T* obj)
{
//还回来
//显示调用析构函数清理对象
obj->~T();
if (_freeList == nullptr)
{
_freeList = obj;
//*(int*)obj = nullptr;//前四个字节用来保存下一个内存的地址 把obj强转成int* 再解引用->int 获得此地址 64位下跑不了
*(void**)obj = nullptr;//64位下解引用是void *,*(int**)也可以
}
else
{
//头插
*(void**)obj = _freeList;
_freeList = obj;
}
}
private:
char* _memory = nullptr;//指向大块内存,char是一个字节,好切分内存
size_t remainBytes = 0;//大块内存中剩余数
void* _freeList = nullptr;//管理换回来的内存(链表)的头指针
};
现代很多的开发环境都是多核多线程,在申请内存的场景下,必然存在激烈的锁竞争问题。
内存池需要考虑以下几方面的问题。
concurrent memory pool:
thread cache是哈希桶结构,每个桶是一个按桶位置映射大小的内存块对象的自由链表。每个线程都会有一个thread cache对象,这样每个线程在这里获取对象和释放对象时是无锁的。
自由链表的哈希桶跟对象大小的映射关系
class SizeClass//计算对象大小的对齐映射规则
{
public:
// 整体控制在最多10%左右的内碎片浪费
// [1,128] 8byte对齐 freelist[0,16)
// [128+1,1024] 16byte对齐 freelist[16,72)
// [1024+1,8*1024] 128byte对齐 freelist[72,128)
// [8*1024+1,64*1024] 1024byte对齐 freelist[128,184)
// [64*1024+1,256*1024] 8*1024byte对齐 freelist[184,208)
static inline size_t _RoundUp(size_t bytes, size_t alignNum)//计算对齐数
{
//size_t alignSize ;//对齐
//if (size % 8 != 0)
//{
// alignSize = (size / alignNum + 1) * alignNum;
//}
//else
//{
// alignSize = size;
//}
//return alignSize;
return ((bytes + alignNum - 1) & ~(alignNum - 1));
}
static inline size_t RoundUp(size_t size)
{
if (size <= 128)
{
return _RoundUp(size, 8);
}
else if (size <= 1024)
{
return _RoundUp(size, 16);
}
else if (size <= 8 * 1024)
{
return _RoundUp(size, 128);
}
else if (size <= 64 * 1024)
{
return _RoundUp(size, 1024);
}
else if (size <= 256 * 1024)
{
return _RoundUp(size, 8 * 1024);
}
else
{
return _RoundUp(size, 1 << PAGE_SHIFT);
}
}
映射哪一个自由链表桶
static inline size_t _Index(size_t bytes, size_t alignNum)
{
/*if (bytes % alignNum == 0)
{
return bytes / alignNum - 1;
}
else
{
return bytes / alignNum;
}*/
return ((bytes + (1 << alignNum) - 1) >> alignNum) - 1;
}
// 计算映射的哪一个自由链表桶
static inline size_t Index(size_t bytes)
{
assert(bytes <= MAX_BYTES);
// 每个区间有多少个链
static int group_array[4] = { 16, 56, 56, 56 };
if (bytes <= 128) {
return _Index(bytes, 3);//8 2^3
}
else if (bytes <= 1024) {
return _Index(bytes - 128, 4) + group_array[0];//把前面128减掉,再加上前一个桶的数量
}
else if (bytes <= 8 * 1024) {
return _Index(bytes - 1024, 7) + group_array[1] + group_array[0];
}
else if (bytes <= 64 * 1024) {
return _Index(bytes - 8 * 1024, 10) + group_array[2] + group_array[1] + group_array[0];
}
else if (bytes <= 256 * 1024) {
return _Index(bytes - 64 * 1024, 13) + group_array[3] + group_array[2] + group_array[1] + group_array[0];
}
else {
assert(false);
}
return -1;
}
申请内存:
释放内存
#pragma once
#include "Common.h"
class ThreadCache
{
public:
//申请和释放对象
void* Allocate(size_t size);
void Deallocate(void* ptr, size_t size);
//从中心缓存获取对象
void* FetchFromCentralCache(size_t index, size_t size);
void ListTooLong(FreeList& list, size_t size);//释放对象时,链表过长 ,回收内存到centrral cache
private:
FreeList _freeLists[NFREELISTS];//哈希表,每个位置挂的都是_freeList
};
// TLS:在线程内全局可访问,但不能被其他线程访问到->保持数据的独立性,不需要锁控制,减少成本
static _declspec(thread) ThreadCache * pTLSThreadCache = nullptr;
central cache也是一个哈希桶结构(t桶锁),他的哈希桶的映射关系跟thread cache是一样的。不同的是他的每个哈希桶位置挂是SpanList链表结构,不过每个映射桶下面的span中的大内存块被按映射关系切成了一个个小内存块对象挂在span的自由链表中。
申请内存:
释放内存
以页为单位的大内存管理span的定义及spanlist定义
struct Span//管理多个连续大块内存跨度结构
{
PAGE_ID _pageId = 0;//大块内存起始页号
size_t n = 0;//页的数量
Span* _next = nullptr;//双向链表
Span* _prev = nullptr;//双向链表
size_t _objSize = 0;//切好的小对象的大小
size_t _useCount = 0;//切好的小块内存,被分给thread cache计数
void* _freeList = nullptr;//切好的小块内存自由链表
bool _isUse = false;//是否被使用
};
class SpanList//带头双向链表
{
public:
SpanList()
{
_head = new Span;
_head->_next = _head;
_head->_prev = _head;
}
Span* Begin()
{
return _head->_next;
}
Span* End()
{
return _head;
}
bool Empty()
{
//cout << "heool spanlist empty" << endl;
return _head->_next == _head;
}
void PushFront(Span* span)
{
//cout << "hello common pushfront" << endl;
Insert(Begin(), span);
}
Span* PopFront()
{
//cout << "hello commom popfront" << endl;
Span* front = _head->_next;
Erase(front);
return front;
}
void Insert(Span* pos, Span* newSpan)
{
//cout << "hello commom insert" << endl;
assert(pos);
assert(newSpan);
Span* prev = pos->_prev;
prev->_next = newSpan;
newSpan->_prev = prev;
newSpan->_next = pos;
pos->_prev = newSpan;
}
void Erase(Span* pos)
{
assert(pos);
assert(pos != _head);
Span* prev = pos->_prev;
Span* next = pos->_next;
prev->_next = next;
next->_prev = prev;
}
private:
Span* _head;
public:
std::mutex _mtx;//桶锁
};
#pragma once
#include "Common.h"
//单例模式
class CentralCache
{
public:
static CentralCache* GetInstance()
{
return &_sInst;
}
// 获取一个非空的span
Span* GetOneSpan(SpanList& list, size_t byte_size);
// 从中心缓存获取一定数量的对象给thread cache
size_t FetchRangeObj(void*& start, void*& end, size_t batchNum, size_t size);
// 将一定数量的对象释放到span跨度
void ReleaseListToSpans(void* start, size_t byte_size);
private:
SpanList _spanLists[NFREELISTS];//在ThreadCache是几号桶,在CentralCache就是几号桶
private:
CentralCache() //把构造函数放在私有:别人不能创建对象
{}
CentralCache(const CentralCache&) = delete;
static CentralCache _sInst;
};
申请内存:
释放内存:
整体设计
#pragma once
#include "Common.h"
#include "ObjectPool.h"
class PageCache
{
public:
static PageCache* GetInstance()
{
//cout << "hello Page cache getinstance" << endl;
return &_sInst;
}
Span* MapObjectToSpan(void* obj);//获取对象到span的映射
Span* NewSpan(size_t k);//获取一个k页的span
void ReleaseSpanToPageCache(Span* span);//释放空闲span,合并相邻的span
std::mutex _pageMtx;
private:
SpanList _spanList[NPAGES];
ObjectPool spanPool;
std::unordered_map _idSpanMap;//页号跟span的映射
PageCache() {}
PageCache(const PageCache&) = delete;
static PageCache _sInst;
};
ObjectPool.h
#pragma once
#pragma once
#include
#include
#include
#include "Common.h"
using std::cout;
using std::endl;
//定长内存池
//template
//class ObjectPool
//{};
/*
#ifdef _WIN32
#include
#else
#endif
inline static void* SystemAlloc(size_t kpage)//直接去堆上按页申请内存
{
#ifdef _WIN32
void* ptr = VirtualAlloc(0, kpage << 13, MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
#else
// linux下brk mmap等
#endif
if (ptr == nullptr)
throw std::bad_alloc();
return ptr;
}
*/
template
class ObjectPool
{
public:
T* New()
{
T* obj = nullptr;
if (_freeList)
{
//优先把还回来的内存块再次重复利用
void* next = (*(void**)_freeList);
obj = (T*)_freeList;
_freeList = next;
}
else
{
//剩余内存不够一个对象大小时,重新开大块空间
if (remainBytes < sizeof(T))
{
remainBytes = 128 * 1024;
//_memory = (char*)malloc(remainBytes);
_memory = (char*)SystemAlloc(remainBytes >> 13);
if (_memory == nullptr)
{
throw std::bad_alloc();
}
}
obj = (T*)_memory;
size_t objSize = sizeof(T) < sizeof(void*) ? sizeof(void*) : sizeof(T);
_memory += objSize;
remainBytes -= objSize;
}
//定位new,显示调用T的构造函数初始化,对已有的空间初始化
new(obj)T;
return obj;
}
void Delete(T* obj)
{
//还回来
//显示调用析构函数清理对象
obj->~T();
if (_freeList == nullptr)
{
_freeList = obj;
//*(int*)obj = nullptr;//前四个字节用来保存下一个内存的地址 把obj强转成int* 再解引用->int 获得此地址 64位下跑不了
*(void**)obj = nullptr;//64位下解引用是void *,*(int**)也可以
}
else
{
//头插
*(void**)obj = _freeList;
_freeList = obj;
}
}
private:
char* _memory = nullptr;//指向大块内存,char是一个字节,好切分内存
size_t remainBytes = 0;//大块内存中剩余数
void* _freeList = nullptr;//管理换回来的内存(链表)的头指针
};
Common.h
#pragma once
//公共文件
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
ConcurrentAlloc.h
#pragma once
#include "Common.h"
#include "ThreadCache.h"
#include "PageCache.h"
#include "ObjectPool.h"
static void* ConcurrentAlloc(size_t size)//线程调用申请内存
{
//通过TLS 每个线程无锁的获取自己的专属ThreadCache对象
if (size > MAX_BYTES)
{
size_t alignSize = SizeClass::RoundUp(size);//对齐
size_t kpage = alignSize >> PAGE_SHIFT;//获取页数
PageCache::GetInstance()->_pageMtx.lock();
Span* span = PageCache::GetInstance()->NewSpan(kpage);
//span->_objSize = size;
PageCache::GetInstance()->_pageMtx.unlock();
void* ptr = (void*)(span->_pageId << PAGE_SHIFT);
return ptr;
}
else
{
if (pTLSThreadCache == nullptr)
{
//pTLSThreadCache = new ThreadCache;
static ObjectPool tcPool;
pTLSThreadCache = tcPool.New();
}
cout << std::this_thread::get_id() << ":" << pTLSThreadCache << endl;
return pTLSThreadCache->Allocate(size);
}
}
static void ConcurrentFree(void* ptr)
{
//size:不给大小不知道要还给桶的哪个位置
Span* span = PageCache::GetInstance()->MapObjectToSpan(ptr);
size_t size = span->_objSize;//对齐以后的大小
if (size > MAX_BYTES)
{
PageCache::GetInstance()->_pageMtx.lock();
PageCache::GetInstance()->ReleaseSpanToPageCache(span);
PageCache::GetInstance()->_pageMtx.unlock();
}
else
{
assert(pTLSThreadCache);
pTLSThreadCache->Deallocate(ptr, size);
}
}
ThreadCache.h
#pragma once
#include "Common.h"
class ThreadCache
{
public:
//申请和释放对象
void* Allocate(size_t size);
void Deallocate(void* ptr, size_t size);
//从中心缓存获取对象
void* FetchFromCentralCache(size_t index, size_t size);
void ListTooLong(FreeList& list, size_t size);//释放对象时,链表过长 ,回收内存到centrral cache
private:
FreeList _freeLists[NFREELISTS];//哈希表,每个位置挂的都是_freeList
};
// TLS:在线程内全局可访问,但不能被其他线程访问到->保持数据的独立性,不需要锁控制,减少成本
static _declspec(thread) ThreadCache * pTLSThreadCache = nullptr;
ThreadCache.cpp
#include "ThreadCache.h"
#include "CentralCache.h"
#include "Common.h"
void* ThreadCache::FetchFromCentralCache(size_t index, size_t size)
{
cout << "hello common fecthcenrercache" << endl;
//慢开始反馈调节算法
//最开始不会向 central cache要太多因为可能用不完,如果不要size大小需求batchNum会不断增长直到上限;
//size越大一次向central cache要的越小,size越小一次向central cache要的越大
size_t batchNum = min(_freeLists[index].MaxSize(), SizeClass::NumMoveSize(size));
if (_freeLists[index].MaxSize() == batchNum)
{
_freeLists[index].MaxSize() += 1;
}
void* start = nullptr;
void* end = nullptr;
size_t actualNum = CentralCache::GetInstance()->FetchRangeObj(start, end, batchNum,size);
assert(actualNum > 0);
if (actualNum == 1)
{
assert(start == end);
return start;
}
else
{
_freeLists[index].PushRange(NextObj(start), end, actualNum - 1);
return start;
}
return nullptr;
}
void* ThreadCache::Allocate(size_t size)//申请对象
{
//
assert(size <= MAX_BYTES);
size_t alignSize = SizeClass::RoundUp(size);//获取对其数
size_t index = SizeClass::Index(size);//在哪一个桶-》获取桶的位置
if (!_freeLists[index].Empty())
{
return _freeLists[index].Pop();
}
else
{
return FetchFromCentralCache(index,alignSize);//从中心缓存获取对象
}
}
void ThreadCache::Deallocate(void* ptr, size_t size)//释放对象
{
assert(size <= MAX_BYTES);
assert(ptr);
//找出自由链表映射的桶,对象插入
size_t index = SizeClass::Index(size);//属于哪个桶
_freeLists[index].Push(ptr);
//当链表长度大于等于一次批量申请的内存时就开始还一段内存给central cache
if (_freeLists[index].Size() >= _freeLists[index].MaxSize())
{ ListTooLong(_freeLists[index], size);
}
}
void ThreadCache::ListTooLong(FreeList& list, size_t size)
{
void* start = nullptr;
void* end = nullptr;
list.PopRange(start,end,list.MaxSize());//取出内存
//把内存还给下一层:central cache
CentralCache::GetInstance()->ReleaseListToSpans(start,size);
}
CentralCache.h
#include "CentralCache.h"
#include "PageCache.h"
CentralCache CentralCache::_sInst;
Span* CentralCache::GetOneSpan(SpanList& list, size_t size)
{
//cout << "hello central getonspan" << endl;
// 从SpanLists或者Page cache 获取一个非空的span
//查看当前spanlist中是否还有非空的/还未分配对象的
Span* it = list.Begin();
while (it != list.End())
{
if (it->_freeList != nullptr)
{
//挂着对象
return it;
}
else
{
it = it->_next;
}
}
//先把central cache的桶锁解掉,这样如果其他线程释放内存对象回来不会阻塞
list._mtx.unlock();
//没有空闲span,找 page cache要
PageCache::GetInstance()->_pageMtx.lock();
Span* span = PageCache::GetInstance()->NewSpan(SizeClass::NumMovePage(size));
span->_isUse = true;
span->_objSize = size;
PageCache::GetInstance()->_pageMtx.unlock();
//对获取的span进行切分吧、,不需要加锁,因为其他线程访问不到这个span
//通过页号计算起始地址: 页号<_pageId << PAGE_SHIFT);
size_t bytes = span->n << PAGE_SHIFT;//计算span的大块起始地址和大块内存的大小(字节数)
char* end = start + bytes;
//把大块内存切成自由链表连接起来
//先切一块下来去做头,方便尾插
span->_freeList = start;
start += size;
int i = 1;
void* tail = span->_freeList;
while (start < end)
{
i++;
NextObj(tail) = start;
tail = NextObj(tail);//tail = start
start += size;
}
NextObj(tail) = nullptr;//尾插最后一位需要置空
list._mtx.lock();//切好span以后需要把span挂到桶里去再加锁
list.PushFront(span);
return span;
}
size_t CentralCache::FetchRangeObj(void*& start, void*& end, size_t batchNum, size_t size)// 从中心缓存获取一定数量的对象给thread cache
{
//cout << "hello hello central getonspan" << endl;
size_t index = SizeClass::Index(size);//先查看是哪个桶的
_spanLists[index]._mtx.lock();
Span* span = GetOneSpan(_spanLists[index], size);//先去找一个非空的span
assert(span);
assert(span->_freeList);
//从span中获取batchNum个对象
//如果不够batchNum,有多少拿多少
start = span->_freeList;
end = start;
size_t i = 0;
size_t actualNum = 1;
while (i < batchNum - 1 && NextObj(end) != nullptr)//end 往后走batchNum -1个
{
end = NextObj(end);
++i;
++actualNum;
}
span->_freeList = NextObj(end);
NextObj(end) = nullptr;
span->_useCount += actualNum;//被使用的个数
_spanLists[index]._mtx.unlock();
return actualNum;
}
void CentralCache::ReleaseListToSpans(void* start, size_t size)
{
size_t index = SizeClass::Index(size);//属于哪一个桶
_spanLists[index]._mtx.lock();
while (start)
{
void* next = NextObj(start);
Span* span = PageCache::GetInstance()->MapObjectToSpan(start);//找出对应的span
NextObj(start) = span->_freeList;
span->_freeList = start;
span->_useCount--;
if (span->_useCount == 0)//说明span切分出去的的所有小内存都回来了,
{//该span可以归还给page cache,page cache再可以去做前后页的合并
_spanLists[index].Erase(span);
span->_freeList = nullptr;
span->_next = nullptr;
span->_prev = nullptr;
//span还给下一层
_spanLists[index]._mtx.unlock();
//释放span给Page cache时,使用page cache锁
//这时把桶锁解掉
PageCache::GetInstance()->_pageMtx.lock();
PageCache::GetInstance()->ReleaseSpanToPageCache(span);
PageCache::GetInstance()->_pageMtx.unlock();
_spanLists[index]._mtx.lock();
}
start = next;
}
_spanLists[index]._mtx.unlock();
}
Pagecache.h
#pragma once
#include "Common.h"
#include "ObjectPool.h"
class PageCache
{
public:
static PageCache* GetInstance()
{
//cout << "hello Page cache getinstance" << endl;
return &_sInst;
}
Span* MapObjectToSpan(void* obj);//获取对象到span的映射
Span* NewSpan(size_t k);//获取一个k页的span
void ReleaseSpanToPageCache(Span* span);//释放空闲span,合并相邻的span
std::mutex _pageMtx;
private:
SpanList _spanList[NPAGES];
ObjectPool spanPool;
std::map _idSpanMap;//页号跟span的映射
//std::unordered_map _idSpanMap;//页号跟span的映射
PageCache() {}
PageCache(const PageCache&) = delete;
static PageCache _sInst;
};
PageCache.cpp
#include "PageCache.h"
PageCache PageCache::_sInst;
Span* PageCache::NewSpan(size_t k)//获取k页的span
{
//eg:只有一个128页的,需要两页的-》128分为2span和126span,2返回给central cache,126挂道对应的桶上
//如果central cache中的span usecount=0,说明切分给thread cache小块内存都还回来了,
//则central cache把span还给page cache,page cache通过页号查看相邻页是否空闲,是就合并出更大的page,解决内存碎片问题
assert(k > 0 && k < NPAGES);
if (k > NPAGES - 1)
{
void* ptr = SystemAlloc(k);//大于最大页数直接找堆要
//Span* span = new Span;
Span* span = spanPool.New();
span->_pageId = (PAGE_ID)ptr >> PAGE_SHIFT;
span->n = k;
_idSpanMap[span->_pageId] = span;
return span;
}
if (!_spanList[k].Empty())//第k个桶里面有没有span
{
Span* KSpan = _spanList[k].PopFront();
//建立id和span的映射关系方便centralcache回收小块内存时查找对应的span
for (PAGE_ID i = 0; i < KSpan->n; i++)
{
_idSpanMap[KSpan->_pageId + i] = KSpan;
}
return KSpan;
}
//第k个桶里是空的,检测后面的桶里有没有span,如果有进行切分
//切分成一个k页的span和一个 n-k 页的span
//k页的span返回给central cache,n-k 页的span挂到第 n-k 号桶中去
for (size_t i = k ; i < NPAGES; i++)
{
if (!_spanList[i].Empty())
{
Span* nSpan = _spanList[i].PopFront();
//Span* KSpan = new Span;
Span* KSpan = spanPool.New();
//在nSpan的头部切下K页
//k页span返回,nSpan再挂到对应映射
KSpan->_pageId = nSpan->_pageId;
KSpan->n = k;//kSpan页数变为k
nSpan->_pageId += k;//nSpan 页号变为 += k
nSpan->n -= k;
_spanList[nSpan->n].PushFront(nSpan);//把剩余的页挂到对应的位置
//存储nSpan的首尾页号跟span映射,方便page cache回收内存时进行合并查找
_idSpanMap[nSpan->_pageId] = nSpan;
_idSpanMap[nSpan->_pageId + nSpan->n - 1] = nSpan;
//建立id和span的映射,方便central cache回收查找对应的span
for (PAGE_ID i = 0; i < KSpan->n; i++)
{
_idSpanMap[KSpan->_pageId + i] = KSpan;
}
return KSpan;
}
}
//没有大页span
//找堆要128页的span
Span* bigSpan = spanPool.New();
//Span* bigSpan = new Span;
void* ptr = SystemAlloc(NPAGES - 1);
bigSpan->_pageId = (PAGE_ID)ptr >> PAGE_SHIFT;
bigSpan->n = NPAGES - 1;
_spanList[bigSpan->n].PushFront(bigSpan);
return NewSpan(k);
}
Span* PageCache::MapObjectToSpan(void* obj)
{
PAGE_ID id = ((PAGE_ID)obj >> PAGE_SHIFT);//找出页号
std::unique_lock lock(_pageMtx);
auto ret = _idSpanMap.find(id);
if (ret != _idSpanMap.end())
{
return ret->second;//返回span的指针
}
else
{
assert(false);
return nullptr;
}
}
void PageCache::ReleaseSpanToPageCache(Span* span)
{
//对span前后的页进行合并,解决内存碎片问题
if (span->n > NPAGES - 1)
{
//大于128页的直接还给堆
void* ptr = (void*)(span->_pageId << PAGE_SHIFT);
SystemFree(ptr);
//delete span;
spanPool.Delete(span);
return;
}
//向前合并
while (1)
{
PAGE_ID prevId = span->_pageId - 1;
auto ret = _idSpanMap.find(prevId);
if (ret == _idSpanMap.end())
{//前面的页号没有,不合并
break;
}
Span* prevspan = ret->second;
if (prevspan->_isUse == true)
{//前面相邻页的span在使用
break;
}
if (prevspan->n + span->n > NPAGES - 1)
{
//合并数超过128,没办法管理
break;
}
//合并
span->_pageId = prevspan->_pageId;
span->n += prevspan->n;
_spanList[prevspan->n].Erase(prevspan);
//delete prevspan;
spanPool.Delete(prevspan);
}
//向后合并
while (1)
{
PAGE_ID nextId = span->_pageId + span->n;
auto ret = _idSpanMap.find(nextId);
if (ret == _idSpanMap.end())
{
break;
}
Span* nextspan = ret->second;
if (nextspan->_isUse == true)
{
break;
}
if (nextspan->n + span->n > NPAGES - 1)
{
break;
}
span->n += nextspan->n;
_spanList[nextspan->n].Erase(nextspan);
//delete(nextspan);
spanPool.Delete(nextspan);
}
//前后都合并过了
_spanList[span->n].PushFront(span);
span->_isUse = false;
//方便其他把此span合并
_idSpanMap[span->_pageId] = span;
_idSpanMap[span->_pageId + span->n - 1] = span;
}
调试-》性能与诊断-》开始-》检测
实际采用的就是直接定址法,每一个页号对应span的地址就存储数组中在以该页号为下标的位置
这里还是以32位平台下,一页的大小为8K为例来说明,此时存储页号最多需要19个比特位。而二层基数树实际上就是把这19个比特位分为两次进行映射。
上面一层基数树和二层基数树都适用于32位平台,而对于64位的平台就需要用三层基数树了。三层基数树与二层基数树类似,三层基数树实际上就是把存储页号的若干比特位分为三次进行映射。
PageMap.h
#pragma once
#include "Common.h"
#include "ObjectPool.h"
template
class TCMalloc_PageMap1 {
private:
static const int LENGTH = 1 << BITS;
void** array_;
public:
typedef uintptr_t Number;
explicit TCMalloc_PageMap1(void* (*allocator)(size_t)) {
//array_ = reinterpret_cast((*allocator)(sizeof(void*) << BITS));
size_t size = sizeof(void*) << BITS;
size_t alignSize = SizeClass::_RoundUp(size,1 << PAGE_SHIFT);
array_ = SystemAlloc(alignSize >> PAGE_SHIFT);
memset(array_, 0, sizeof(void*) << BITS);
}
// Return the current value for KEY. Returns NULL if not yet set,
// or if k is out of range.
void* get(Number k) const {
if ((k >> BITS) > 0) {
return NULL;
}
return array_[k];
}
// REQUIRES "k" is in range "[0,2^BITS-1]".
// REQUIRES "k" has been ensured before.
//
// Sets the value 'v' for key 'k'.
void set(Number k, void* v) {
array_[k] = v;
}
};
// Two-level radix tree
template
class TCMalloc_PageMap2 {
private:
// Put 32 entries in the root and (2^BITS)/32 entries in each leaf.
static const int ROOT_BITS = 5;
static const int ROOT_LENGTH = 1 << ROOT_BITS;
static const int LEAF_BITS = BITS - ROOT_BITS;
static const int LEAF_LENGTH = 1 << LEAF_BITS;
// Leaf node
struct Leaf {
void* values[LEAF_LENGTH];
};
Leaf* root_[ROOT_LENGTH]; // Pointers to 32 child nodes
void* (*allocator_)(size_t); // Memory allocator
public:
typedef uintptr_t Number;
//explicit TCMalloc_PageMap2(void* (*allocator)(size_t))
explicit TCMalloc_PageMap2() {
//allocator_ = allocator;
memset(root_, 0, sizeof(root_));
}
void* get(Number k) const {
const Number i1 = k >> LEAF_BITS;
const Number i2 = k & (LEAF_LENGTH - 1);
if ((k >> BITS) > 0 || root_[i1] == NULL) {
return NULL;
}
return root_[i1]->values[i2];
}
void set(Number k, void* v) {
const Number i1 = k >> LEAF_BITS;
const Number i2 = k & (LEAF_LENGTH - 1);
ASSERT(i1 < ROOT_LENGTH);
root_[i1]->values[i2] = v;
}
bool Ensure(Number start, size_t n) {
for (Number key = start; key <= start + n - 1;) {
const Number i1 = key >> LEAF_BITS;
// Check for overflow
if (i1 >= ROOT_LENGTH)
return false;
// Make 2nd level node if necessary
if (root_[i1] == NULL) {
//Leaf* leaf = reinterpret_cast((*allocator_)(sizeof(Leaf)));
//if (leaf == NULL) return false;
static ObjectPool leafpool;
//Leaf* leaf = reinterpret_cast((*allocator_)
Leaf* leaf = (Leaf*)leafpool.New();
memset(leaf, 0, sizeof(*leaf));
root_[i1] = leaf;
}
// Advance key past whatever is covered by this leaf node
key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
}
return true;
}
void PreallocateMoreMemory() {
// Allocate enough to keep track of all possible pages
Ensure(0, 1 << BITS);
}
};
// Three-level radix tree
template
class TCMalloc_PageMap3 {
private:
// How many bits should we consume at each interior level
static const int INTERIOR_BITS = (BITS + 2) / 3; // Round-up
static const int INTERIOR_LENGTH = 1 << INTERIOR_BITS;
// How many bits should we consume at leaf level
static const int LEAF_BITS = BITS - 2 * INTERIOR_BITS;
static const int LEAF_LENGTH = 1 << LEAF_BITS;
// Interior node
struct Node {
Node* ptrs[INTERIOR_LENGTH];
};
// Leaf node
struct Leaf {
void* values[LEAF_LENGTH];
};
Node* root_; // Root of radix tree
void* (*allocator_)(size_t); // Memory allocator
Node* NewNode() {
Node* result = reinterpret_cast((*allocator_)(sizeof(Node)));
if (result != NULL) {
memset(result, 0, sizeof(*result));
}
return result;
}
public:
typedef uintptr_t Number;
explicit TCMalloc_PageMap3(void* (*allocator)(size_t)) {
allocator_ = allocator;
root_ = NewNode();
}
void* get(Number k) const {
const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
const Number i3 = k & (LEAF_LENGTH - 1);
if ((k >> BITS) > 0 ||
root_->ptrs[i1] == NULL || root_->ptrs[i1]->ptrs[i2] == NULL) {
return NULL;
}
return reinterpret_cast(root_->ptrs[i1]->ptrs[i2])->values[i3];
}
void set(Number k, void* v) {
ASSERT(k >> BITS == 0);
const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
const Number i3 = k & (LEAF_LENGTH - 1);
reinterpret_cast(root_->ptrs[i1]->ptrs[i2])->values[i3] = v;
}
bool Ensure(Number start, size_t n) {
for (Number key = start; key <= start + n - 1;) {
const Number i1 = key >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (key >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
// Check for overflow
if (i1 >= INTERIOR_LENGTH || i2 >= INTERIOR_LENGTH)
return false;
// Make 2nd level node if necessary
if (root_->ptrs[i1] == NULL) {
Node* n = NewNode();
if (n == NULL) return false;
root_->ptrs[i1] = n;
}
// Make leaf node if necessary
if (root_->ptrs[i1]->ptrs[i2] == NULL) {
Leaf* leaf = reinterpret_cast((*allocator_)
(sizeof(Leaf)));
if (leaf == NULL) return false;
memset(leaf, 0, sizeof(*leaf));
root_->ptrs[i1]->ptrs[i2] = reinterpret_cast(leaf);
}
// Advance key past whatever is covered by this leaf node
key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
}
return true;
}
void PreallocateMoreMemory() {
}
};
PageCache.h
#pragma once
#include "Common.h"
#include "ObjectPool.h"
template
class TCMalloc_PageMap1 {
private:
static const int LENGTH = 1 << BITS;
void** array_;
public:
typedef uintptr_t Number;
explicit TCMalloc_PageMap1(void* (*allocator)(size_t)) {
//array_ = reinterpret_cast((*allocator)(sizeof(void*) << BITS));
size_t size = sizeof(void*) << BITS;
size_t alignSize = SizeClass::_RoundUp(size,1 << PAGE_SHIFT);
array_ = SystemAlloc(alignSize >> PAGE_SHIFT);
memset(array_, 0, sizeof(void*) << BITS);
}
// Return the current value for KEY. Returns NULL if not yet set,
// or if k is out of range.
void* get(Number k) const {
if ((k >> BITS) > 0) {
return NULL;
}
return array_[k];
}
// REQUIRES "k" is in range "[0,2^BITS-1]".
// REQUIRES "k" has been ensured before.
//
// Sets the value 'v' for key 'k'.
void set(Number k, void* v) {
array_[k] = v;
}
};
// Two-level radix tree
template
class TCMalloc_PageMap2 {
private:
// Put 32 entries in the root and (2^BITS)/32 entries in each leaf.
static const int ROOT_BITS = 5;
static const int ROOT_LENGTH = 1 << ROOT_BITS;
static const int LEAF_BITS = BITS - ROOT_BITS;
static const int LEAF_LENGTH = 1 << LEAF_BITS;
比特就业课
// Leaf node
struct Leaf {
void* values[LEAF_LENGTH];
};
Leaf* root_[ROOT_LENGTH]; // Pointers to 32 child nodes
void* (*allocator_)(size_t); // Memory allocator
public:
typedef uintptr_t Number;
//explicit TCMalloc_PageMap2(void* (*allocator)(size_t))
explicit TCMalloc_PageMap2() {
//allocator_ = allocator;
memset(root_, 0, sizeof(root_));
}
void* get(Number k) const {
const Number i1 = k >> LEAF_BITS;
const Number i2 = k & (LEAF_LENGTH - 1);
if ((k >> BITS) > 0 || root_[i1] == NULL) {
return NULL;
}
return root_[i1]->values[i2];
}
void set(Number k, void* v) {
const Number i1 = k >> LEAF_BITS;
const Number i2 = k & (LEAF_LENGTH - 1);
ASSERT(i1 < ROOT_LENGTH);
root_[i1]->values[i2] = v;
}
bool Ensure(Number start, size_t n) {
for (Number key = start; key <= start + n - 1;) {
const Number i1 = key >> LEAF_BITS;
// Check for overflow
if (i1 >= ROOT_LENGTH)
return false;
// Make 2nd level node if necessary
if (root_[i1] == NULL) {
//Leaf* leaf = reinterpret_cast((*allocator_)(sizeof(Leaf)));
//if (leaf == NULL) return false;
static ObjectPool leafpool;
//Leaf* leaf = reinterpret_cast((*allocator_)
Leaf* leaf = (Leaf*)leafpool.New();
memset(leaf, 0, sizeof(*leaf));
root_[i1] = leaf;
}
// Advance key past whatever is covered by this leaf node
key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
}
return true;
}
void PreallocateMoreMemory() {
// Allocate enough to keep track of all possible pages
Ensure(0, 1 << BITS);
}
};
// Three-level radix tree
template
class TCMalloc_PageMap3 {
private:
// How many bits should we consume at each interior level
static const int INTERIOR_BITS = (BITS + 2) / 3; // Round-up
static const int INTERIOR_LENGTH = 1 << INTERIOR_BITS;
// How many bits should we consume at leaf level
static const int LEAF_BITS = BITS - 2 * INTERIOR_BITS;
static const int LEAF_LENGTH = 1 << LEAF_BITS;
// Interior node
struct Node {
Node* ptrs[INTERIOR_LENGTH];
};
// Leaf node
struct Leaf {
void* values[LEAF_LENGTH];
};
Node* root_; // Root of radix tree
void* (*allocator_)(size_t); // Memory allocator
Node* NewNode() {
Node* result = reinterpret_cast((*allocator_)(sizeof(Node)));
if (result != NULL) {
memset(result, 0, sizeof(*result));
}
return result;
}
public:
typedef uintptr_t Number;
explicit TCMalloc_PageMap3(void* (*allocator)(size_t)) {
allocator_ = allocator;
root_ = NewNode();
}
void* get(Number k) const {
const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
const Number i3 = k & (LEAF_LENGTH - 1);
if ((k >> BITS) > 0 ||
root_->ptrs[i1] == NULL || root_->ptrs[i1]->ptrs[i2] == NULL) {
return NULL;
}
return reinterpret_cast(root_->ptrs[i1]->ptrs[i2])->values[i3];
}
void set(Number k, void* v) {
ASSERT(k >> BITS == 0);
const Number i1 = k >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
const Number i3 = k & (LEAF_LENGTH - 1);
reinterpret_cast(root_->ptrs[i1]->ptrs[i2])->values[i3] = v;
}
bool Ensure(Number start, size_t n) {
for (Number key = start; key <= start + n - 1;) {
const Number i1 = key >> (LEAF_BITS + INTERIOR_BITS);
const Number i2 = (key >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
// Check for overflow
if (i1 >= INTERIOR_LENGTH || i2 >= INTERIOR_LENGTH)
return false;
// Make 2nd level node if necessary
if (root_->ptrs[i1] == NULL) {
Node* n = NewNode();
if (n == NULL) return false;
root_->ptrs[i1] = n;
}
// Make leaf node if necessary
if (root_->ptrs[i1]->ptrs[i2] == NULL) {
Leaf* leaf = reinterpret_cast((*allocator_)
(sizeof(Leaf)));
if (leaf == NULL) return false;
memset(leaf, 0, sizeof(*leaf));
root_->ptrs[i1]->ptrs[i2] = reinterpret_cast(leaf);
}
// Advance key past whatever is covered by this leaf node
key = ((key >> LEAF_BITS) + 1) << LEAF_BITS;
}
return true;
}
void PreallocateMoreMemory() {
}
};
PageCache.cpp
#include "PageCache.h"
PageCache PageCache::_sInst;
Span* PageCache::NewSpan(size_t k)//获取k页的span
{
//eg:只有一个128页的,需要两页的-》128分为2span和126span,2返回给central cache,126挂道对应的桶上
//如果central cache中的span usecount=0,说明切分给thread cache小块内存都还回来了,
//则central cache把span还给page cache,page cache通过页号查看相邻页是否空闲,是就合并出更大的page,解决内存碎片问题
assert(k > 0 && k < NPAGES);
if (k > NPAGES - 1)
{
void* ptr = SystemAlloc(k);//大于最大页数直接找堆要
//Span* span = new Span;
Span* span = spanPool.New();
span->_pageId = (PAGE_ID)ptr >> PAGE_SHIFT;
span->n = k;
//_idSpanMap[span->_pageId] = span;
_idSpanMap.set(span->_pageId,span);
return span;
}
if (!_spanList[k].Empty())//第k个桶里面有没有span
{
Span* KSpan = _spanList[k].PopFront();
//建立id和span的映射关系方便centralcache回收小块内存时查找对应的span
for (PAGE_ID i = 0; i < KSpan->n; i++)
{
//_idSpanMap[KSpan->_pageId + i] = KSpan;
_idSpanMap.set(KSpan->_pageId + i, KSpan);
}
return KSpan;
}
//第k个桶里是空的,检测后面的桶里有没有span,如果有进行切分
//切分成一个k页的span和一个 n-k 页的span
//k页的span返回给central cache,n-k 页的span挂到第 n-k 号桶中去
for (size_t i = k ; i < NPAGES; i++)
{
if (!_spanList[i].Empty())
{
Span* nSpan = _spanList[i].PopFront();
//Span* KSpan = new Span;
Span* KSpan = spanPool.New();
//在nSpan的头部切下K页
//k页span返回,nSpan再挂到对应映射
KSpan->_pageId = nSpan->_pageId;
KSpan->n = k;//kSpan页数变为k
nSpan->_pageId += k;//nSpan 页号变为 += k
nSpan->n -= k;
_spanList[nSpan->n].PushFront(nSpan);//把剩余的页挂到对应的位置
//存储nSpan的首尾页号跟span映射,方便page cache回收内存时进行合并查找
//_idSpanMap[nSpan->_pageId] = nSpan;
//_idSpanMap[nSpan->_pageId + nSpan->n - 1] = nSpan
_idSpanMap.set(nSpan->_pageId, nSpan);
_idSpanMap.set(nSpan->_pageId + nSpan->n - 1, nSpan);
//建立id和span的映射,方便central cache回收查找对应的span
for (PAGE_ID i = 0; i < KSpan->n; i++)
{
//_idSpanMap[KSpan->_pageId + i] = KSpan;
_idSpanMap.set(KSpan->_pageId + i, KSpan);
}
return KSpan;
}
}
//没有大页span
//找堆要128页的span
Span* bigSpan = spanPool.New();
//Span* bigSpan = new Span;
void* ptr = SystemAlloc(NPAGES - 1);
bigSpan->_pageId = (PAGE_ID)ptr >> PAGE_SHIFT;
bigSpan->n = NPAGES - 1;
_spanList[bigSpan->n].PushFront(bigSpan);
return NewSpan(k);
}
Span* PageCache::MapObjectToSpan(void* obj)
{
PAGE_ID id = ((PAGE_ID)obj >> PAGE_SHIFT);//找出页号
auto ret = (Span*)_idSpanMap.get(id);
assert(ret != nullptr);
return ret;
}
void PageCache::ReleaseSpanToPageCache(Span* span)
{
//对span前后的页进行合并,解决内存碎片问题
if (span->n > NPAGES - 1)
{
//大于128页的直接还给堆
void* ptr = (void*)(span->_pageId << PAGE_SHIFT);
SystemFree(ptr);
//delete span;
spanPool.Delete(span);
return;
}
//向前合并
while (1)
{
PAGE_ID prevId = span->_pageId - 1;
auto ret = (Span*)_idSpanMap.get(prevId);
if (ret == nullptr)
{
break;
}
Span* prevspan = ret;
if (prevspan->_isUse == true)
{//前面相邻页的span在使用
break;
}
if (prevspan->n + span->n > NPAGES - 1)
{
//合并数超过128,没办法管理
break;
}
//合并
span->_pageId = prevspan->_pageId;
span->n += prevspan->n;
_spanList[prevspan->n].Erase(prevspan);
//delete prevspan;
spanPool.Delete(prevspan);
}
//向后合并
while (1)
{
PAGE_ID nextId = span->_pageId + span->n;
auto ret = (Span*)_idSpanMap.get(nextId);
if (ret == nullptr)
{
break;
}
Span* nextspan = ret;
if (nextspan->_isUse == true)
{
break;
}
if (nextspan->n + span->n > NPAGES - 1)
{
break;
}
span->n += nextspan->n;
_spanList[nextspan->n].Erase(nextspan);
//delete(nextspan);
spanPool.Delete(nextspan);
}
//前后都合并过了
_spanList[span->n].PushFront(span);
span->_isUse = false;
//方便其他把此span合并
//_idSpanMap[span->_pageId] = span;
//_idSpanMap[span->_pageId + span->n - 1] = span;
_idSpanMap.set(span->_pageId, span);
_idSpanMap.set(span->_pageId + span->n - 1, span);
}
//只有Span* NewSpan(size_t k) void ReleaseSpanToPageCache(Span* span)
//这两个函数中去建立id和span的映射(回去写)
//基数树,写之前会提前开好空间,写数据过程中,不会动数据结构
//读写是分离的。线程1对一个位置读写的时候,线程2不可以对这个位置读写
转载至:https://zhuanlan.zhihu.com/p/582514123