]> git.ipfire.org Git - thirdparty/squid.git/blob - src/mem/Meter.h
Source Format Enforcement (#1234)
[thirdparty/squid.git] / src / mem / Meter.h
1 /*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_SRC_MEM_METER_H
10 #define SQUID_SRC_MEM_METER_H
11
12 #include "time/gadgets.h"
13
14 namespace Mem
15 {
16
17 /**
18 * object to track per-action memory usage (e.g. #idle objects)
19 */
20 class Meter
21 {
22 public:
23 /// flush the meter level back to 0, but leave peak records
24 void flush() {level=0;}
25
26 ssize_t currentLevel() const {return level;}
27 ssize_t peak() const {return hwater_level;}
28 time_t peakTime() const {return hwater_stamp;}
29
30 Meter &operator ++() {++level; checkHighWater(); return *this;}
31 Meter &operator --() {--level; return *this;}
32
33 Meter &operator +=(ssize_t n) { level += n; checkHighWater(); return *this;}
34 Meter &operator -=(ssize_t n) { level -= n; return *this;}
35
36 private:
37 /// check the high-water level of this meter and raise if necessary
38 /// recording the timestamp of last high-water peak change
39 void checkHighWater() {
40 if (hwater_level < level) {
41 hwater_level = level;
42 hwater_stamp = squid_curtime ? squid_curtime : time(nullptr);
43 }
44 }
45
46 ssize_t level = 0; ///< current level (count or volume)
47 ssize_t hwater_level = 0; ///< high water mark
48 time_t hwater_stamp = 0; ///< timestamp of last high water mark change
49 };
50
51 /**
52 * Object to track per-pool memory usage (alloc = inuse+idle)
53 */
54 class PoolMeter
55 {
56 public:
57 /// Object to track per-pool cumulative counters
58 struct mgb_t {
59 double count = 0.0;
60 double bytes = 0.0;
61 };
62
63 /// flush counters back to 0, but leave historic peak records
64 void flush() {
65 alloc.flush();
66 inuse.flush();
67 idle.flush();
68 gb_allocated = mgb_t();
69 gb_oallocated = mgb_t();
70 gb_saved = mgb_t();
71 gb_freed = mgb_t();
72 }
73
74 Meter alloc;
75 Meter inuse;
76 Meter idle;
77
78 /** history Allocations */
79 mgb_t gb_allocated;
80 mgb_t gb_oallocated;
81
82 /** account Saved Allocations */
83 mgb_t gb_saved;
84
85 /** account Free calls */
86 mgb_t gb_freed;
87 };
88
89 } // namespace Mem
90
91 #endif /* SQUID_SRC_MEM_METER_H */
92