Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/addr2line.hpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/addr2line.hpp	(revision 2212)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/addr2line.hpp	(revision 2212)
@@ -0,0 +1,109 @@
+#ifdef XIOS_MEMTRACK
+
+#ifndef __ADDR2LINE_HPP__
+#define __ADDR2LINE_HPP__
+
+#include <sys/types.h>
+#include<sys/wait.h>
+#include<sys/prctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <string>
+#include <iostream>
+#include <fstream>
+
+namespace xios
+{
+  class CAddr2line
+  {
+    public:
+
+      CAddr2line(void) ;
+      ~CAddr2line() ;
+
+      void write(const std::string& str) ;
+      void read(std::string& str) ;
+   
+    private:
+      pid_t child_pid;
+      int   from_child, to_child;
+  } ;
+
+
+  CAddr2line::CAddr2line(void)
+  {
+    pid_t p;
+    int pipe_stdin[2], pipe_stdout[2];
+    char execfile[1000] ;
+    ssize_t size=readlink("/proc/self/exe",execfile,1000) ;
+    execfile[size]='\0' ;
+    std::string cmdline = "addr2line -e ";
+    cmdline=cmdline+execfile ;
+    
+    int err ;
+    err=pipe(pipe_stdin) ;
+    // if (err) return -1; // make an exception//
+    err=pipe(pipe_stdout) ;
+     // if (err) return -1; // make an exception//
+
+    p = fork();
+
+    if(p < 0) return ; // make an exception//
+    if(p == 0) 
+    { /* child */
+        close(pipe_stdin[1]);
+        dup2(pipe_stdin[0], 0);
+        close(pipe_stdout[0]);
+        dup2(pipe_stdout[1], 1);
+        execl("/bin/sh", "sh", "-c", cmdline.c_str(), NULL);
+        std::cout<<"child exit !!!"<<std::endl ;
+        perror("execl"); exit(99);
+    }
+    child_pid = p;
+    to_child = pipe_stdin[1];
+    from_child = pipe_stdout[0];
+    close(pipe_stdin[0]);
+    close(pipe_stdout[1]);
+  }
+  
+  void CAddr2line::write(const std::string& str)
+  {
+    std::string strTmp=str+'\n' ;
+    ::write(to_child, strTmp.data(), strTmp.size()) ;
+  }
+
+  void CAddr2line::read(std::string& str)
+  {
+    const int maxSize=10000 ;
+    char buffer[maxSize] ;
+    
+    int a ;
+    for(int i=0;i<maxSize;i++)
+    {
+      a = ::read(from_child,&buffer[i],1) ;
+      if (a==-1) std::cout<<::strerror(errno)<<std::endl ;
+      if(buffer[i]=='\n')
+      {
+        buffer[i]='\0' ;
+        break ;
+      }
+    } 
+    str=buffer ;
+  }
+  
+  CAddr2line::~CAddr2line()
+  {
+
+    ::close(from_child) ;
+    ::close(to_child) ;
+    waitpid(child_pid, NULL, 0) ;
+  }
+
+}
+
+#endif
+
+#endif
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/client.hpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/client.hpp	(revision 2211)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/client.hpp	(revision 2212)
@@ -46,4 +46,5 @@
         static CPoolRessource* getPoolRessource(void) { return poolRessource_ ; }
 
+        static void openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb);
       protected:
         static int rank_;                 //!< Rank in model intraComm
@@ -51,5 +52,4 @@
         static StdOFStream m_errorStream;
 
-        static void openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb);
         static CPoolRessource* poolRessource_ ;
     };
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/cxios.cpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/cxios.cpp	(revision 2211)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/cxios.cpp	(revision 2212)
@@ -14,4 +14,5 @@
 #include "services_manager.hpp"
 #include "servers_ressource.hpp"
+#include "mem_checker.hpp"
 
 namespace xios
@@ -155,6 +156,18 @@
 
 #ifdef XIOS_MEMTRACK_FULL
-     MemTrack::TrackListMemoryUsage() ;
-     MemTrack::TrackDumpBlocks();
+      report(0) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
+      report(0) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
+      
+      ofstream memReport ;
+      std::filebuf* fb = memReport.rdbuf();
+      CClient::openStream(clientFile, ".mem", fb);
+      
+      MemTrack::TrackListMemoryUsage() ;
+      size_t memtrack_blocks=0 ;
+      memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ;
+      size_t memtrack_size=0 ;
+      memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ;
+      MemTrack::TrackDumpBlocks(memReport, memtrack_blocks,memtrack_size);
+      memReport.close();
 #endif
 
@@ -177,4 +190,5 @@
   void CXios::initServerSide(void)
   {
+    CMemChecker::get("xios").resume() ;
     initServer();
     isClient = false;
@@ -193,8 +207,21 @@
 
 #ifdef XIOS_MEMTRACK_FULL
-     MemTrack::TrackListMemoryUsage() ;
-     MemTrack::TrackDumpBlocks();
-#endif
-#endif
+      report(0) << " Memory report : current memory used by XIOS : "<<  MemTrack::getCurrentMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
+      report(0) << " Memory report : maximum memory used by XIOS : "<<  MemTrack::getMaxMemorySize()*1.0/(1024*1024)<<" Mbyte" << endl ;
+      ofstream memReport ;
+      std::filebuf* fb = memReport.rdbuf();
+      CClient::openStream(serverFile, ".mem", fb);
+      
+      MemTrack::TrackListMemoryUsage() ;
+      size_t memtrack_blocks=0 ;
+      memtrack_blocks=xios::CXios::getin("memtrack_blocks",memtrack_blocks) ;
+      size_t memtrack_size=0 ;
+      memtrack_size=xios::CXios::getin("memtrack_size",memtrack_size) ;
+      MemTrack::TrackDumpBlocks(memReport,memtrack_blocks,memtrack_size);
+      memReport.close() ;
+#endif
+#endif
+    CMemChecker::get("xios").suspend() ;
+    report(0)<<CMemChecker::getAllCumulatedMem() ;
     CServer::closeInfoStream();
   }
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/mem_checker.cpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/mem_checker.cpp	(revision 2212)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/mem_checker.cpp	(revision 2212)
@@ -0,0 +1,105 @@
+#include "mem_checker.hpp"
+#include "mpi.hpp"
+#include <string>
+#include <map>
+#include <iostream>
+#include <sstream>
+#include <fstream>
+
+namespace xios
+{
+  CMemChecker CMemChecker::dummy_("") ;
+  std::map<std::string,CMemChecker> CMemChecker::allMemChecker_;
+  bool CMemChecker::enabled_=true;
+  bool CMemChecker::first_=true;
+
+  CMemChecker::CMemChecker(const std::string& name) : name_(name) 
+  { 
+    if (first_) check() ;
+    reset();
+  }
+
+  void CMemChecker::check(void)
+  {
+    std::ifstream statStream("/proc/self/stat",std::ios_base::in);
+    enabled_ &= statStream.good() ;
+    first_=false ;
+  }
+  double CMemChecker::getMem(void)
+  {
+    if (first_) check() ;
+    if (!enabled_) return 0;
+    std::ifstream statStream("/proc/self/stat",std::ios_base::in);
+    std::string dummy ;
+    for(int i=1;i<=22;i++) statStream>>dummy ;
+    unsigned long vsize; 
+    statStream>>vsize ;
+    return vsize ;
+  }
+  
+  void CMemChecker::suspend(void)
+  {
+    if (first_) check() ;
+    if (!enabled_) return ;
+    if (!suspended_) cumulatedMem_ += getMem() - lastMem_;
+    suspended_ = true;
+  }
+  
+  void CMemChecker::resume(void)
+  {
+    if (first_) check() ;
+    if (!enabled_) return ;
+    if (suspended_) lastMem_ = getMem();
+    suspended_ = false;
+  }
+  
+  void CMemChecker::reset(void)
+  {
+    if (first_) check() ;
+    if (!enabled_) return ;
+    cumulatedMem_ = 0.;
+    suspended_ = true;
+  }
+  
+  double CMemChecker::getCumulatedMem(void)
+  {
+    if (first_) check() ;
+    if (!enabled_) return 0;
+    return cumulatedMem_;
+  }
+  
+  CMemChecker& CMemChecker::get(const std::string name)
+  {
+    if (first_) check() ;
+    if (!enabled_) return dummy_ ;
+    else
+    {
+      std::map<std::string,CMemChecker>::iterator it = allMemChecker_.find(name);
+      if (it == allMemChecker_.end())
+        it = allMemChecker_.insert(std::make_pair(name, CMemChecker(name))).first;
+      return it->second;
+    }
+  }
+
+  std::string CMemChecker::getAllCumulatedMem(void)
+  {
+    if (first_) check() ;
+    if (!enabled_) return std::string(" MemChecker : memory consumption report not available") ; 
+    std::ostringstream strOut ;
+    const double Kb=1024 ;
+    const double Mb=Kb*1024 ;
+    const double Gb=Mb*1024 ;
+    const double Tb=Mb*1024 ;
+    for(std::map<std::string,CMemChecker>::iterator it=allMemChecker_.begin();it!=allMemChecker_.end();++it)
+    {  
+      strOut<<"MemChecker : "<<it->first<<"    -->   consumed memory : " ;
+      double mem=it->second.getCumulatedMem() ;
+      if (mem>=Tb) strOut<< mem / Tb<<" Tb"<<std::endl ;
+      else if (mem>=Gb) strOut<< mem / Gb<<" Gb"<<std::endl ;
+      else if (mem>=Mb) strOut<< mem / Mb<<" Mb"<<std::endl ;
+      else if (mem>=Kb) strOut<< mem / Kb<<" Kb"<<std::endl ;
+      else strOut<< mem <<" bytes"<<std::endl ;
+    }
+    return strOut.str() ;
+  }
+}
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/mem_checker.hpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/mem_checker.hpp	(revision 2212)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/mem_checker.hpp	(revision 2212)
@@ -0,0 +1,38 @@
+#ifndef __XIOS_MEM_CHECKER_HPP__
+#define __XIOS_MEM_CHECKER_HPP__
+
+#include <string>
+#include <map>
+
+namespace xios
+{
+  class CMemChecker
+  {
+    public:
+      CMemChecker(const std::string& name);
+      void suspend(void);
+      void resume(void);
+      void reset(void);
+      double getCumulatedMem(void);
+      static double getMem(void);
+      static CMemChecker& get(std::string name);
+      static std::string getAllCumulatedMem(void) ;
+      static void disable(void) { enabled_=false ;}
+      static void enable(void) {enabled_=true ;}
+    private:
+      static void check(void) ;
+      double cumulatedMem_;
+      double lastMem_;
+      bool suspended_;
+      std::string name_;
+
+      static std::map<std::string,CMemChecker> allMemChecker_;
+      static CMemChecker dummy_ ;
+      static bool first_ ;
+      static bool enabled_ ;
+  };
+}
+
+
+
+#endif
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/memtrack.cpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/memtrack.cpp	(revision 2211)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/memtrack.cpp	(revision 2212)
@@ -45,15 +45,14 @@
 #include <string>
 #include <execinfo.h>
-
+#include <cxxabi.h>
+#include <dlfcn.h>
+#include <map>
+#include <fstream>
 #include "memtrack.hpp"
+#include "log.hpp"
+#include "addr2line.hpp"
 #undef new    // IMPORTANT!
 
-extern "C"
-{
-  void addr2line(const char *file_name, char** addr, int naddr) ;
-#ifdef XIOS_MEMTRACK_LIGHT
-  void addr2line(const char *file_name, char** addr, int naddr) {} 
-#endif
-}
+
 /* ------------------------------------------------------------ */
 /* -------------------- namespace MemTrack -------------------- */
@@ -98,6 +97,7 @@
         
             void Stamp(char const *filename, int lineNum, char const *typeName);
-            void backTrace(void) ;
-        
+            void backTrace(std::ofstream& memReport, xios::CAddr2line& addr2line) ;
+            void backTrace(std::ofstream& memReport) ;
+
             static void AddNode(BlockHeader *node);
             static void RemoveNode(BlockHeader *node);
@@ -139,18 +139,49 @@
     }
 
-    void BlockHeader::backTrace(void)
+
+    void BlockHeader::backTrace(std::ofstream& memReport)
+    {
+        char** strings ;
+        strings = backtrace_symbols(stackArray, this->stackSize);
+        int stackSize=this->stackSize-3 ;
+        char* buffer ;
+        char* outputBuffer ;
+        size_t length=10 ;
+        outputBuffer = (char*) malloc(length) ;
+        int status ;
+        Dl_info info;
+        for(int i=0;i<stackSize;i++) 
+        {
+            if (dladdr(stackArray[i+3], &info))
+            {
+              buffer=abi::__cxa_demangle(info.dli_sname,outputBuffer,&length,&status );
+              if (buffer!=nullptr) 
+              {
+                memReport<<buffer<<std::endl ;
+                outputBuffer=buffer ;
+              }
+              else if (info.dli_sname!=nullptr) memReport<<info.dli_sname<<std::endl ;
+              else memReport<<strings[i+3]<<std::endl ;
+            }
+            else memReport<<strings[i+3]<<std::endl ;
+        }
+        free(buffer) ;
+        free(strings) ;
+    }
+
+    void BlockHeader::backTrace(std::ofstream& memReport, xios::CAddr2line& addr2line)
     {   
         
-//        oss<<"addr2line -C -f -i -s -e ../bin/test_client.exe " ;
-        char *addr ;
-        char buffer[20] ;
-        addr=buffer ;
-        for(int i=0;i<stackSize;i++) 
-        {
-          std::ostringstream oss ;
-          oss<<stackArray[i] ;
-          strcpy(addr,oss.str().c_str()) ;
-          addr2line("/proc/self/exe",&addr,1) ;
-        }
+      int stackSize=this->stackSize-3 ;
+      
+      for(int i=0;i<stackSize;i++)
+      {
+        std::ostringstream address;
+        address << stackArray[i+3];
+        addr2line.write(address.str()) ;
+        std::string out ;
+        addr2line.read(out) ;
+        memReport<<out<<std::endl ;
+      }
     }
     /* ---------------------------------------- BlockHeader AddNode */
@@ -439,30 +470,48 @@
     /* ---------------------------------------- TrackDumpBlocks */
 
-    void TrackDumpBlocks()
+    void TrackDumpBlocks(std::ofstream& memReport, size_t memtrack_blocks, size_t memtrack_size )
     {
         // Get an array of pointers to all extant blocks.
+        std::ostringstream ostr ;
         size_t numBlocks = BlockHeader::CountBlocks();
         BlockHeader **ppBlockHeader =
             (BlockHeader **)calloc(numBlocks, sizeof(*ppBlockHeader));
         BlockHeader::GetBlocks(ppBlockHeader);
-
         // Dump information about the memory blocks.
-        printf("\n");
-        printf("=====================\n");
-        printf("Current Memory Blocks\n");
-        printf("=====================\n");
-        printf("\n");
+        
+        memReport<<std::endl;
+        memReport<<"=====================" <<std::endl ;
+        memReport<<"Current Memory Blocks" <<std::endl ;
+        memReport<<"=====================" <<std::endl ;
+        memReport<<std::endl ;
+        char strbuff[10000] ;
+        std::multimap<size_t,BlockHeader *,std::greater<int>> orderedBlocks ;
         for (size_t i = 0; i < numBlocks; i++)
         {
-            BlockHeader *pBlockHeader = ppBlockHeader[i];
+          BlockHeader *pBlockHeader = ppBlockHeader[i];
+          size_t size = pBlockHeader->GetRequestedSize();
+          orderedBlocks.insert({size,pBlockHeader}) ;
+        }
+        
+        xios::CAddr2line myaddr2line ;
+        size_t i = 0 ;
+        for(auto& it : orderedBlocks)
+        {
+            BlockHeader *pBlockHeader = it.second ;
             char const *typeName = pBlockHeader->GetTypeName();
             size_t size = pBlockHeader->GetRequestedSize();
             char const *fileName = pBlockHeader->GetFilename();
             int lineNum = pBlockHeader->GetLineNum();
-            printf("*** #%-6d %5d bytes %-50s\n", i, size, typeName);
-            printf("... %s:%d\n", fileName, lineNum);
-            pBlockHeader->backTrace();
-        }
-
+            if (memtrack_blocks>0 && i>memtrack_blocks) break ;            
+            if (memtrack_size>0 && size<memtrack_size) break ;            
+            
+            sprintf(strbuff,"*** #%-6d %5d bytes %-50s\n", i, size, typeName);
+            memReport<<strbuff ;
+            sprintf(strbuff,"... %s:%d\n", fileName, lineNum);
+            memReport<<strbuff ;
+            //pBlockHeader->backTrace(memReport, myaddr2line);
+            pBlockHeader->backTrace(memReport);
+            i++ ;
+        }
         // Clean up.
         free(ppBlockHeader);
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/memtrack.hpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/memtrack.hpp	(revision 2211)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/memtrack.hpp	(revision 2212)
@@ -38,4 +38,5 @@
 #include <typeinfo>
 #include <cstring>
+#include <fstream>
 
 namespace MemTrack
@@ -59,5 +60,5 @@
     void TrackFree(void *p);
     void TrackStamp(void *p, const MemStamp &stamp, char const *typeName);
-    void TrackDumpBlocks();
+    void TrackDumpBlocks(std::ofstream& memReport, size_t memtrack_blocks, size_t memtrack_size);
     void TrackListMemoryUsage();
     size_t getCurrentMemorySize(void) ;
Index: XIOS/dev/dev_ym/XIOS_COUPLING/src/server.hpp
===================================================================
--- XIOS/dev/dev_ym/XIOS_COUPLING/src/server.hpp	(revision 2211)
+++ XIOS/dev/dev_ym/XIOS_COUPLING/src/server.hpp	(revision 2212)
@@ -67,4 +67,5 @@
         static CServersRessource* getServersRessource(void) { return serversRessource_;}
         static void launchServersRessource(MPI_Comm commServer) ;
+        static void openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb);
       
       private:
@@ -74,5 +75,4 @@
         static StdOFStream m_infoStream;
         static StdOFStream m_errorStream;
-        static void openStream(const StdString& fileName, const StdString& ext, std::filebuf* fb);
         static CServersRessource* serversRessource_ ;
     };
