• Alexis SALZMAN's avatar
    [xTool] add xDeltaMemory a basic memory profiler · 0628714a
    Alexis SALZMAN authored
    xDeltaMemory class is built in the same spirit as xDeltaTime.
    A "start" method capture a state and a "end" method do a comparison with
    previous captured state. This comparison give a measure of memory
    consumption between those two events.
    To be as precise as possible the usual string used to flag measure are
    now forcedly used by initAccu method and user must use startAccu,
    endAccu with the integer returned by initAccu.
    
    It give result like xDeltaTime as table with parallel statistic and Peak
    memory usage. This last information comes from getrusage (like
    xMemoryMonitor) and correspond to maximum resident set size. All other
    information are related to user request and do not represent the
    application true current memory consumption: malloc may have already
    reserve 2GB and user vector may use only 1.5GB. This is this last
    information that is given by xDeltaMemory.
    
    Only Heap is analyzed.
    
    Behind the curtain:
    ===================
    xDeltaMemory used deprecated GNU extension called hooks. malloc,realloc
    and free hooks give a way to set your own feature related to those
    function. In our case we want to count what user did ask for allocation.
    A ideal solution would be to use the size argument to count. But when
    freeing memory we just have a pointer in hand so its not easy to decrease
    our counter. Instead xDeltaMemory use malloc_usable_size which demangle the
    malloc information related to the accessible memory associated to
    pointer given by malloc.  xDeltaMemory over estimate then the real size
    asked by the user as  malloc_usable_size return the asked size plus the
    padding eventually added by malloc. But this is already a pretty
    accurate measure. Compare to xMemoryMonitor or the use of mallinfo here
    we place the measure in client application side. The others approaches
    can be qualified as system measuring snapshot. And thus they are harder
    to use for precise measure of user allocation.
    
    The deprecated aspect seem to last from a long time ... With gcc 8 on
    liger it is still available ....
    
    The GNU extension aspect is clearly a limitation. This is not portable !
    But all GNU specific aspect has been guarded by __GNUC__ macro so on
    other compiler xDeltaMemory will return 0 for all measure.
    
    To measure third party library calls, it will be possible only if
    the library is also compiled with a GNU compiler and malloc is in use.
    Somme test on liger with mumps show that xDeltaMemory provided almost
    the same information as MUMPS itself.
    
    Note that interleaving measure is possible. A specific counter track the
    start/end to deactivate hooks outside measuring sequence to avoid any
    extra computational cost.
    
    The implementation use static variable ! It is then not currently thread safe.
    
    New atomic test case:
    =====================
    A small test show how  to use xDeltaMemory and is for now the only
    documentation.
    
    TODO
    ====
    doc+xNoDeltaMemory class
    0628714a