Skip to content

Commit 4764aa8

Browse files
committed
Factor out common peak statistic
1 parent ff431c3 commit 4764aa8

File tree

5 files changed

+77
-21
lines changed

5 files changed

+77
-21
lines changed

src/snmalloc/backend_helpers/statsrange.h

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ namespace snmalloc
1717
{
1818
using ContainsParent<ParentRange>::parent;
1919

20-
static inline std::atomic<size_t> current_usage{};
21-
static inline std::atomic<size_t> peak_usage{};
20+
static inline Stat usage{};
2221

2322
public:
2423
static constexpr bool Aligned = ParentRange::Aligned;
@@ -29,36 +28,28 @@ namespace snmalloc
2928

3029
constexpr Type() = default;
3130

32-
CapPtr<void, ChunkBounds> alloc_range(size_t size)
31+
capptr::Arena<void> alloc_range(size_t size)
3332
{
34-
auto result = parent.alloc_range(size);
35-
if (result != nullptr)
36-
{
37-
auto prev = current_usage.fetch_add(size);
38-
auto curr = peak_usage.load();
39-
while (curr < prev + size)
40-
{
41-
if (peak_usage.compare_exchange_weak(curr, prev + size))
42-
break;
43-
}
44-
}
45-
return result;
33+
auto r = parent.alloc_range(size);
34+
if (r != nullptr)
35+
usage += size;
36+
return r;
4637
}
4738

48-
void dealloc_range(CapPtr<void, ChunkBounds> base, size_t size)
39+
void dealloc_range(capptr::Arena<void> base, size_t size)
4940
{
50-
current_usage -= size;
41+
usage -= size;
5142
parent.dealloc_range(base, size);
5243
}
5344

5445
size_t get_current_usage()
5546
{
56-
return current_usage.load();
47+
return usage.get_curr();
5748
}
5849

5950
size_t get_peak_usage()
6051
{
61-
return peak_usage.load();
52+
return usage.get_peak();
6253
}
6354
};
6455
};

src/snmalloc/ds_core/ds_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
#include "ptrwrap.h"
1616
#include "redblacktree.h"
1717
#include "seqset.h"
18+
#include "stats.h"

src/snmalloc/ds_core/stats.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <atomic>
2+
#include <cstddef>
3+
#include "defines.h"
4+
5+
namespace snmalloc
6+
{
7+
/**
8+
* Very basic statistic that tracks current and peak values.
9+
*/
10+
class Stat
11+
{
12+
private:
13+
std::atomic<size_t> curr{0};
14+
std::atomic<size_t> peak{0};
15+
16+
public:
17+
void increase(size_t amount)
18+
{
19+
size_t c = (curr += amount);
20+
size_t p = peak.load(std::memory_order_relaxed);
21+
while (c > p)
22+
{
23+
if (peak.compare_exchange_strong(p, c))
24+
break;
25+
}
26+
}
27+
28+
void decrease(size_t amount)
29+
{
30+
size_t prev = curr.fetch_sub(amount);
31+
SNMALLOC_ASSERT(prev >= amount);
32+
}
33+
34+
size_t get_curr()
35+
{
36+
return curr.load(std::memory_order_relaxed);
37+
}
38+
39+
size_t get_peak()
40+
{
41+
return peak.load(std::memory_order_relaxed);
42+
}
43+
44+
void operator+=(size_t amount)
45+
{
46+
increase(amount);
47+
}
48+
49+
void operator-=(size_t amount)
50+
{
51+
decrease(amount);
52+
}
53+
54+
void operator++()
55+
{
56+
increase(1);
57+
}
58+
59+
void operator--()
60+
{
61+
decrease(1);
62+
}
63+
};
64+
} // namespace snmalloc

src/snmalloc/mem/globalalloc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace snmalloc
8787
}
8888
}
8989

90-
if (result == nullptr && RemoteDeallocCache::remote_inflight.load() != 0)
90+
if (result == nullptr && RemoteDeallocCache::remote_inflight.get_curr() != 0)
9191
error("ERROR: RemoteDeallocCache::remote_inflight != 0");
9292

9393
if (result != nullptr)

src/snmalloc/mem/remotecache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace snmalloc
1919
{
2020
std::array<freelist::Builder<false>, REMOTE_SLOTS> list;
2121

22-
static inline std::atomic<size_t> remote_inflight{0};
22+
static inline Stat remote_inflight;
2323

2424
/**
2525
* The total amount of bytes of memory in the cache.

0 commit comments

Comments
 (0)