mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-03 21:44:33 +00:00 
			
		
		
		
	global memory debug through command line flag
This commit is contained in:
		@@ -216,14 +216,21 @@ void Environment::createDerivedObject(const std::string name,
 | 
			
		||||
    if (!object_[address].data)
 | 
			
		||||
    {
 | 
			
		||||
        MemoryStats memStats;
 | 
			
		||||
 | 
			
		||||
        MemoryProfiler::stats    = &memStats;
 | 
			
		||||
    
 | 
			
		||||
        if (!MemoryProfiler::stats)
 | 
			
		||||
        {
 | 
			
		||||
            MemoryProfiler::stats = &memStats;
 | 
			
		||||
        }
 | 
			
		||||
        size_t initMem           = MemoryProfiler::stats->currentlyAllocated;
 | 
			
		||||
        object_[address].storage = storage;
 | 
			
		||||
        object_[address].Ls      = Ls;
 | 
			
		||||
        object_[address].data.reset(new Holder<B>(new T(std::forward<Ts>(args)...)));
 | 
			
		||||
        object_[address].size    = memStats.maxAllocated;
 | 
			
		||||
        object_[address].size    = MemoryProfiler::stats->maxAllocated - initMem;
 | 
			
		||||
        object_[address].type    = &typeid(T);
 | 
			
		||||
        MemoryProfiler::stats    = nullptr;
 | 
			
		||||
        if (MemoryProfiler::stats == &memStats)
 | 
			
		||||
        {
 | 
			
		||||
            MemoryProfiler::stats = nullptr;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -39,33 +39,6 @@ HadronsLogger Hadrons::HadronsLogMessage(1,"Message");
 | 
			
		||||
HadronsLogger Hadrons::HadronsLogIterative(1,"Iterative");
 | 
			
		||||
HadronsLogger Hadrons::HadronsLogDebug(1,"Debug");
 | 
			
		||||
 | 
			
		||||
// pretty size formatting //////////////////////////////////////////////////////
 | 
			
		||||
std::string Hadrons::sizeString(long unsigned int bytes)
 | 
			
		||||
 | 
			
		||||
{
 | 
			
		||||
    constexpr unsigned int bufSize = 256;
 | 
			
		||||
    const char             *suffixes[7] = {"", "K", "M", "G", "T", "P", "E"};
 | 
			
		||||
    char                   buf[256];
 | 
			
		||||
    long unsigned int      s     = 0;
 | 
			
		||||
    double                 count = bytes;
 | 
			
		||||
    
 | 
			
		||||
    while (count >= 1024 && s < 7)
 | 
			
		||||
    {
 | 
			
		||||
        s++;
 | 
			
		||||
        count /= 1024;
 | 
			
		||||
    }
 | 
			
		||||
    if (count - floor(count) == 0.0)
 | 
			
		||||
    {
 | 
			
		||||
        snprintf(buf, bufSize, "%d %sB", (int)count, suffixes[s]);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        snprintf(buf, bufSize, "%.1f %sB", count, suffixes[s]);
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    return std::string(buf);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// type utilities //////////////////////////////////////////////////////////////
 | 
			
		||||
constexpr unsigned int maxNameSize = 1024u;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -138,9 +138,6 @@ public:\
 | 
			
		||||
private:\
 | 
			
		||||
    name(void) = default;
 | 
			
		||||
 | 
			
		||||
// pretty size formating
 | 
			
		||||
std::string sizeString(long unsigned int bytes);
 | 
			
		||||
 | 
			
		||||
// type utilities
 | 
			
		||||
template <typename T>
 | 
			
		||||
const std::type_info * typeIdPt(const T &x)
 | 
			
		||||
 
 | 
			
		||||
@@ -97,4 +97,29 @@ void check_huge_pages(void *Buf,uint64_t BYTES)
 | 
			
		||||
#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)
 | 
			
		||||
  {
 | 
			
		||||
      s++;
 | 
			
		||||
      count /= 1024;
 | 
			
		||||
  }
 | 
			
		||||
  if (count - floor(count) == 0.0)
 | 
			
		||||
  {
 | 
			
		||||
      snprintf(buf, bufSize, "%d %sB", (int)count, suffixes[s]);
 | 
			
		||||
  }
 | 
			
		||||
  else
 | 
			
		||||
  {
 | 
			
		||||
      snprintf(buf, bufSize, "%.1f %sB", count, suffixes[s]);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  return std::string(buf);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -64,6 +64,8 @@ namespace Grid {
 | 
			
		||||
 | 
			
		||||
  };
 | 
			
		||||
  
 | 
			
		||||
  std::string sizeString(size_t bytes);
 | 
			
		||||
 | 
			
		||||
  struct MemoryStats
 | 
			
		||||
  {
 | 
			
		||||
    size_t totalAllocated{0}, maxAllocated{0}, 
 | 
			
		||||
@@ -77,15 +79,20 @@ namespace Grid {
 | 
			
		||||
    static bool        debug;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  #define memString(bytes) std::to_string(bytes) + " (" + sizeString(bytes) + ")"
 | 
			
		||||
  #define profilerDebugPrint \
 | 
			
		||||
  if (MemoryProfiler::stats)\
 | 
			
		||||
  {\
 | 
			
		||||
    auto s = MemoryProfiler::stats;\
 | 
			
		||||
    std::cout << "[Memory debug] Stats " << MemoryProfiler::stats << std::endl;\
 | 
			
		||||
    std::cout << "[Memory debug] Total  : " << s->totalAllocated << "B" << std::endl;\
 | 
			
		||||
    std::cout << "[Memory debug] Max    : " << s->maxAllocated << "B" << std::endl;\
 | 
			
		||||
    std::cout << "[Memory debug] Current: " << s->totalAllocated << "B" << std::endl;\
 | 
			
		||||
    std::cout << "[Memory debug] Freed  : " << s->totalFreed << "B" << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] Stats " << MemoryProfiler::stats << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] total  : " << memString(s->totalAllocated) \
 | 
			
		||||
              << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] max    : " << memString(s->maxAllocated) \
 | 
			
		||||
              << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] current: " << memString(s->currentlyAllocated) \
 | 
			
		||||
              << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] freed  : " << memString(s->totalFreed) \
 | 
			
		||||
              << std::endl;\
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  #define profilerAllocate(bytes)\
 | 
			
		||||
@@ -98,7 +105,7 @@ namespace Grid {
 | 
			
		||||
  }\
 | 
			
		||||
  if (MemoryProfiler::debug)\
 | 
			
		||||
  {\
 | 
			
		||||
    std::cout << "[Memory debug] allocating " << bytes << "B" << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] allocating " << memString(bytes) << std::endl;\
 | 
			
		||||
    profilerDebugPrint;\
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -111,7 +118,7 @@ namespace Grid {
 | 
			
		||||
  }\
 | 
			
		||||
  if (MemoryProfiler::debug)\
 | 
			
		||||
  {\
 | 
			
		||||
    std::cout << "[Memory debug] freeing " << bytes << "B" << std::endl;\
 | 
			
		||||
    std::cout << GridLogDebug << "[Memory debug] freeing " << memString(bytes) << std::endl;\
 | 
			
		||||
    profilerDebugPrint;\
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -204,7 +204,7 @@ std::string GridCmdVectorIntToString(const std::vector<int> & vec){
 | 
			
		||||
// Reinit guard
 | 
			
		||||
/////////////////////////////////////////////////////////
 | 
			
		||||
static int Grid_is_initialised = 0;
 | 
			
		||||
 | 
			
		||||
static MemoryStats dbgMemStats;
 | 
			
		||||
 | 
			
		||||
void Grid_init(int *argc,char ***argv)
 | 
			
		||||
{
 | 
			
		||||
@@ -251,6 +251,11 @@ void Grid_init(int *argc,char ***argv)
 | 
			
		||||
    assert(fp!=(FILE *)NULL);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if( GridCmdOptionExists(*argv,*argv+*argc,"--debug-mem") ){
 | 
			
		||||
    MemoryProfiler::debug = true;
 | 
			
		||||
    MemoryProfiler::stats = &dbgMemStats;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ////////////////////////////////////
 | 
			
		||||
  // Banner
 | 
			
		||||
  ////////////////////////////////////
 | 
			
		||||
@@ -324,6 +329,7 @@ void Grid_init(int *argc,char ***argv)
 | 
			
		||||
    std::cout<<GridLogMessage<<"  --decomposition : report on default omp,mpi and simd decomposition"<<std::endl;    
 | 
			
		||||
    std::cout<<GridLogMessage<<"  --debug-signals : catch sigsegv and print a blame report"<<std::endl;
 | 
			
		||||
    std::cout<<GridLogMessage<<"  --debug-stdout  : print stdout from EVERY node"<<std::endl;
 | 
			
		||||
    std::cout<<GridLogMessage<<"  --debug-mem     : print Grid allocator activity"<<std::endl;
 | 
			
		||||
    std::cout<<GridLogMessage<<"  --notimestamp   : suppress millisecond resolution stamps"<<std::endl;
 | 
			
		||||
    std::cout<<GridLogMessage<<std::endl;
 | 
			
		||||
    std::cout<<GridLogMessage<<"Performance:"<<std::endl;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user