Grid 0.7.0
MemoryStats.cc
Go to the documentation of this file.
1#include <Grid/GridCore.h>
2#include <fcntl.h>
3
5
7bool MemoryProfiler::debug = false;
8
9void check_huge_pages(void *Buf,uint64_t BYTES)
10{
11#ifdef __linux__
12 int fd = open("/proc/self/pagemap", O_RDONLY);
13 assert(fd >= 0);
14 const int page_size = 4096;
15 uint64_t virt_pfn = (uint64_t)Buf / page_size;
16 off_t offset = sizeof(uint64_t) * virt_pfn;
17 uint64_t npages = (BYTES + page_size-1) / page_size;
18 std::vector<uint64_t> pagedata(npages);
19 uint64_t ret = lseek(fd, offset, SEEK_SET);
20 assert(ret == offset);
21 ret = ::read(fd, &pagedata[0], sizeof(uint64_t)*npages);
22 assert(ret == sizeof(uint64_t) * npages);
23 int nhugepages = npages / 512;
24 int n4ktotal, nnothuge;
25 n4ktotal = 0;
26 nnothuge = 0;
27 for (int i = 0; i < nhugepages; ++i) {
28 uint64_t baseaddr = (pagedata[i*512] & 0x7fffffffffffffULL) * page_size;
29 for (int j = 0; j < 512; ++j) {
30 uint64_t pageaddr = (pagedata[i*512+j] & 0x7fffffffffffffULL) * page_size;
31 ++n4ktotal;
32 if (pageaddr != baseaddr + j * page_size)
33 ++nnothuge;
34 }
35 }
37 printf("rank %d Allocated %d 4k pages, %d not in huge pages\n", rank, n4ktotal, nnothuge);
38#endif
39}
40
41std::string sizeString(const size_t bytes)
42{
43 constexpr unsigned int bufSize = 256;
44 const char *suffixes[7] = {"", "K", "M", "G", "T", "P", "E"};
45 char buf[256];
46 size_t s = 0;
47 double count = bytes;
48
49 while (count >= 1024 && s < 7)
50 {
51 s++;
52 count /= 1024;
53 }
54 if (count - floor(count) == 0.0)
55 {
56 snprintf(buf, bufSize, "%d %sB", (int)count, suffixes[s]);
57 }
58 else
59 {
60 snprintf(buf, bufSize, "%.1f %sB", count, suffixes[s]);
61 }
62
63 return std::string(buf);
64}
65
67
void check_huge_pages(void *Buf, uint64_t BYTES)
Definition MemoryStats.cc:9
std::string sizeString(const size_t bytes)
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
static MemoryStats * stats
Definition MemoryStats.h:45
static bool debug
Definition MemoryStats.h:46