1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-14 01:35:36 +00:00
Grid/lib/allocator/AlignedAllocator.cc

127 lines
2.8 KiB
C++
Raw Normal View History

2017-02-22 18:09:33 +00:00
#include <Grid/GridCore.h>
2017-08-26 11:38:04 +01:00
#include <fcntl.h>
2018-01-13 00:19:19 +00:00
NAMESPACE_BEGIN(Grid);
2017-12-06 15:50:25 +00:00
MemoryStats *MemoryProfiler::stats = nullptr;
bool MemoryProfiler::debug = false;
2017-09-22 14:21:18 +01:00
int PointerCache::victim;
2017-09-22 14:21:18 +01:00
PointerCache::PointerCacheEntry PointerCache::Entries[PointerCache::Ncache];
void *PointerCache::Insert(void *ptr,size_t bytes) {
2017-08-16 09:42:57 +01:00
if (bytes < 4096 ) return ptr;
2017-02-07 06:17:16 +00:00
#ifdef GRID_OMP
assert(omp_in_parallel()==0);
#endif
2017-02-07 06:17:16 +00:00
void * ret = NULL;
int v = -1;
for(int e=0;e<Ncache;e++) {
if ( Entries[e].valid==0 ) {
v=e;
break;
}
}
if ( v==-1 ) {
v=victim;
victim = (victim+1)%Ncache;
}
if ( Entries[v].valid ) {
ret = Entries[v].address;
Entries[v].valid = 0;
Entries[v].address = NULL;
Entries[v].bytes = 0;
}
Entries[v].address=ptr;
Entries[v].bytes =bytes;
Entries[v].valid =1;
return ret;
}
void *PointerCache::Lookup(size_t bytes) {
2018-01-13 00:19:19 +00:00
if (bytes < 4096 ) return NULL;
#ifdef _OPENMP
assert(omp_in_parallel()==0);
#endif
for(int e=0;e<Ncache;e++){
if ( Entries[e].valid && ( Entries[e].bytes == bytes ) ) {
Entries[e].valid = 0;
return Entries[e].address;
}
}
return NULL;
}
void check_huge_pages(void *Buf,uint64_t BYTES)
{
#ifdef __linux__
int fd = open("/proc/self/pagemap", O_RDONLY);
assert(fd >= 0);
const int page_size = 4096;
uint64_t virt_pfn = (uint64_t)Buf / page_size;
off_t offset = sizeof(uint64_t) * virt_pfn;
uint64_t npages = (BYTES + page_size-1) / page_size;
uint64_t pagedata[npages];
uint64_t ret = lseek(fd, offset, SEEK_SET);
assert(ret == offset);
ret = ::read(fd, pagedata, sizeof(uint64_t)*npages);
assert(ret == sizeof(uint64_t) * npages);
int nhugepages = npages / 512;
int n4ktotal, nnothuge;
n4ktotal = 0;
nnothuge = 0;
for (int i = 0; i < nhugepages; ++i) {
uint64_t baseaddr = (pagedata[i*512] & 0x7fffffffffffffULL) * page_size;
for (int j = 0; j < 512; ++j) {
uint64_t pageaddr = (pagedata[i*512+j] & 0x7fffffffffffffULL) * page_size;
++n4ktotal;
if (pageaddr != baseaddr + j * page_size)
++nnothuge;
2018-01-13 00:19:19 +00:00
}
}
int rank = CartesianCommunicator::RankWorld();
printf("rank %d Allocated %d 4k pages, %d not in huge pages\n", rank, n4ktotal, nnothuge);
#endif
}
std::string sizeString(const size_t bytes)
{
constexpr unsigned int bufSize = 256;
const char *suffixes[7] = {"", "K", "M", "G", "T", "P", "E"};
char buf[256];
size_t s = 0;
double count = bytes;
while (count >= 1024 && s < 7)
2018-01-13 00:19:19 +00:00
{
s++;
count /= 1024;
2018-01-13 00:19:19 +00:00
}
if (count - floor(count) == 0.0)
2018-01-13 00:19:19 +00:00
{
snprintf(buf, bufSize, "%d %sB", (int)count, suffixes[s]);
2018-01-13 00:19:19 +00:00
}
else
2018-01-13 00:19:19 +00:00
{
snprintf(buf, bufSize, "%.1f %sB", count, suffixes[s]);
2018-01-13 00:19:19 +00:00
}
return std::string(buf);
}
2018-01-13 00:19:19 +00:00
NAMESPACE_END(Grid);