FMemStats.h 3.83 KB
Newer Older
1
// ===================================================================================
2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright ScalFmm 2011 INRIA, Olivier Coulaud, Bérenger Bramas, Matthias Messner
// olivier.coulaud@inria.fr, berenger.bramas@inria.fr
// This software is a computer program whose purpose is to compute the FMM.
//
// This software is governed by the CeCILL-C and LGPL licenses and
// abiding by the rules of distribution of free software.  
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public and CeCILL-C Licenses for more details.
// "http://www.cecill.info". 
// "http://www.gnu.org/licenses".
15
// ===================================================================================
16 17 18
#ifndef FMEMSTATS_H
#define FMEMSTATS_H

19
#include "FGlobal.hpp"
20

21
#include <cstdlib>
22
#include <cstring>
23

24 25 26 27 28 29

/** Memstat has to be enabled in the cmake,
  * then it will use the know each allocate and deallocate
  * and give simple stats like max, total used, current used
  */

30
#ifdef SCALFMM_USE_MEM_STATS
31 32
#include <new>
#include <stdexcept>
33
#warning You are using meme stats
34 35 36 37 38 39 40 41 42 43
    void* operator new(std::size_t n) throw(std::bad_alloc);
    void* operator new(size_t n, std::nothrow_t const&) throw();
    void* operator new[](size_t n) throw(std::bad_alloc);
    void* operator new[](size_t n, std::nothrow_t const&) throw();
    void operator delete(void* p) throw();
    void operator delete(void* p, std::nothrow_t const&) throw();
    void operator delete[](void* p) throw();
    void operator delete[](void* p, std::nothrow_t const&) throw();
#endif

44 45 46
/** Give the memory allocation details
  *
  */
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
class FMemStats {
private:
    unsigned long long maxAllocated;
    unsigned long long totalAllocated;
    std::size_t currentAllocated;

    FMemStats()
        : maxAllocated(0), totalAllocated(0), currentAllocated(0) {
    }

    void allocate(const std::size_t size){
        currentAllocated += size;
        totalAllocated += size;

        if(maxAllocated < currentAllocated){
            maxAllocated = currentAllocated;
        }
    }

    void deallocate(const std::size_t size){
        currentAllocated -= size;
    }

70
#ifdef SCALFMM_USE_MEM_STATS
71 72 73 74 75 76 77 78
    friend void* operator new(std::size_t n) throw(std::bad_alloc);
    friend void* operator new(size_t n, std::nothrow_t const&) throw();
    friend void* operator new[](size_t n) throw(std::bad_alloc);
    friend void* operator new[](size_t n, std::nothrow_t const&) throw();
    friend void operator delete(void* p) throw();
    friend void operator delete(void* p, std::nothrow_t const&) throw();
    friend void operator delete[](void* p) throw();
    friend void operator delete[](void* p, std::nothrow_t const&) throw();
79
#endif
80 81

public:
82
    /** Singleton */
83 84
    static FMemStats controler;

85
    /** return the max that has been allocated */
86 87 88 89
    unsigned long long getMaxAllocated() const{
        return maxAllocated;
    }

90
    /** return the total memory allocated during the running */
91 92 93 94
    unsigned long long getTotalAllocated() const{
        return totalAllocated;
    }

95
    /** return the current size allcoated */
96 97 98
    std::size_t getCurrentAllocated() const{
        return currentAllocated;
    }
99

100
    /** get the max in MB */
101 102 103 104
    float getMaxAllocatedMB() const{
        return float(getMaxAllocated()) / 1024 / 1024;
    }

105
    /** get the total in MB */
106 107 108 109
    float getTotalAllocatedMB() const{
        return float(getTotalAllocated()) / 1024 / 1024;
    }

110
    /** get the current in MB */
111 112 113
    float getCurrentAllocatedMB() const{
        return float(getCurrentAllocated()) / 1024 / 1024;
    }
114

115
    /** To know if mem stat has been enabled */
116 117 118 119 120 121 122
    bool isUsed() const {
#ifdef SCALFMM_USE_MEM_STATS
        return true;
#else
        return false;
#endif
    }
123 124 125 126
};


#endif // FMEMSTATS_H