FMemStats.h 3.07 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 mem stats
BRAMAS Berenger's avatar
BRAMAS Berenger committed
34
void* operator new(std::size_t n);
35 36
void operator delete(void* p);
void operator delete[](void* p);
37 38
#endif

39 40 41
/** Give the memory allocation details
  *
  */
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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;
    }

65
#ifdef ScalFMM_USE_MEM_STATS
BRAMAS Berenger's avatar
BRAMAS Berenger committed
66
    friend void* operator new(std::size_t n);
67 68
    friend void operator delete(void* p);
    friend void operator delete[](void* p);
69
#endif
70 71

public:
72
    /** Singleton */
73 74
    static FMemStats controler;

75
    /** return the max that has been allocated */
76 77 78 79
    unsigned long long getMaxAllocated() const{
        return maxAllocated;
    }

80
    /** return the total memory allocated during the running */
81 82 83 84
    unsigned long long getTotalAllocated() const{
        return totalAllocated;
    }

85
    /** return the current size allcoated */
86 87 88
    std::size_t getCurrentAllocated() const{
        return currentAllocated;
    }
89

90
    /** get the max in MB */
91 92 93 94
    float getMaxAllocatedMB() const{
        return float(getMaxAllocated()) / 1024 / 1024;
    }

95
    /** get the total in MB */
96 97 98 99
    float getTotalAllocatedMB() const{
        return float(getTotalAllocated()) / 1024 / 1024;
    }

100
    /** get the current in MB */
101 102 103
    float getCurrentAllocatedMB() const{
        return float(getCurrentAllocated()) / 1024 / 1024;
    }
104

105
    /** To know if mem stat has been enabled */
106
    bool isUsed() const {
107
#ifdef ScalFMM_USE_MEM_STATS
108 109 110 111 112
        return true;
#else
        return false;
#endif
    }
113 114 115 116
};


#endif // FMEMSTATS_H