FMemStats.h 3.53 KB
Newer Older
1
// ===================================================================================
2
3
4
5
6
7
8
9
// Logiciel initial: ScalFmm Version 0.5
// Co-auteurs : Olivier Coulaud, Bérenger Bramas.
// Propriétaires : INRIA.
// Copyright © 2011-2012, diffusé sous les termes et conditions d’une licence propriétaire.
// Initial software: ScalFmm Version 0.5
// Co-authors: Olivier Coulaud, Bérenger Bramas.
// Owners: INRIA.
// Copyright © 2011-2012, spread under the terms and conditions of a proprietary license.
10
// ===================================================================================
11
12
13
#ifndef FMEMSTATS_H
#define FMEMSTATS_H

14
#include "FGlobal.hpp"
15

16
#include <cstdlib>
17
#include <cstring>
18

19
20
21
22
23
24

/** 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
  */

25
#ifdef SCALFMM_USE_MEM_STATS
26
27
#include <new>
#include <stdexcept>
28
#warning You are using meme stats
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    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

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;
    }

62
#ifdef SCALFMM_USE_MEM_STATS
63
64
65
66
67
68
69
70
    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();
71
#endif
72
73

public:
74
    /** Singleton */
75
76
    static FMemStats controler;

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

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

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

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

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

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

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


#endif // FMEMSTATS_H