Skip to content

Commit 4fbcbfb

Browse files
committed
core: Address feedback
Signed-off-by: Abhijat Malviya <abhijat@dragonflydb.io>
1 parent 815b25a commit 4fbcbfb

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

src/core/page_usage_stats.cc

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "core/page_usage_stats.h"
66

77
#include <absl/container/flat_hash_set.h>
8+
#include <absl/strings/ascii.h>
89
#include <absl/strings/str_join.h>
910
#include <glog/logging.h>
1011
#include <hdr/hdr_histogram.h>
@@ -23,28 +24,31 @@ mi_page_usage_stats_t mi_heap_page_is_underutilized(mi_heap_t* heap, void* p, fl
2324

2425
namespace dfly {
2526

27+
using absl::StrAppend;
28+
using absl::StrFormat;
29+
using absl::StripTrailingAsciiWhitespace;
30+
2631
namespace {
2732
constexpr auto kUsageHistPoints = std::array{50, 90, 99};
2833
constexpr auto kInitialSBFCap = 1000;
2934
constexpr auto kFProb = 0.001;
3035
constexpr auto kGrowthFactor = 2;
3136
constexpr auto kHistSignificantFigures = 3;
3237

33-
FilterWithSize MakeSBF() {
34-
return {.sbf = SBF{kInitialSBFCap, kFProb, kGrowthFactor, PMR_NS::get_default_resource()},
35-
.size = 0};
36-
}
37-
3838
} // namespace
3939

40+
FilterWithSize::FilterWithSize()
41+
: sbf{SBF{kInitialSBFCap, kFProb, kGrowthFactor, PMR_NS::get_default_resource()}}, size{0} {
42+
}
43+
4044
void FilterWithSize::Add(uintptr_t address) {
4145
const auto s = std::to_string(address);
4246
if (sbf.Add(s)) {
4347
size += 1;
4448
}
4549
}
4650

47-
void CollectedPageStats::Merge(CollectedPageStats&& other, ShardId shard_id) {
51+
void CollectedPageStats::Merge(CollectedPageStats&& other, uint16_t shard_id) {
4852
this->pages_scanned += other.pages_scanned;
4953
this->pages_marked_for_realloc += other.pages_marked_for_realloc;
5054
this->pages_full += other.pages_full;
@@ -54,54 +58,50 @@ void CollectedPageStats::Merge(CollectedPageStats&& other, ShardId shard_id) {
5458
shard_wide_summary.emplace(std::make_pair(shard_id, std::move(other.page_usage_hist)));
5559
}
5660

57-
CollectedPageStats CollectedPageStats::Merge(CollectedPageStats* stats, const size_t size,
61+
CollectedPageStats CollectedPageStats::Merge(absl::Span<CollectedPageStats> stats,
5862
const float threshold) {
5963
CollectedPageStats result;
6064
result.threshold = threshold;
61-
for (size_t i = 0; i < size; ++i) {
62-
result.Merge(std::move(*stats), i);
63-
stats++;
65+
66+
size_t shard_index = 0;
67+
for (CollectedPageStats& stat : stats) {
68+
result.Merge(std::move(stat), shard_index++);
6469
}
6570
return result;
6671
}
6772

6873
std::string CollectedPageStats::ToString() const {
69-
std::vector<std::string> rows;
70-
rows.push_back(absl::StrFormat("Page usage threshold: %f", threshold * 100));
71-
rows.push_back(absl::StrFormat("Pages scanned: %d", pages_scanned));
72-
rows.push_back(absl::StrFormat("Pages marked for reallocation: %d", pages_marked_for_realloc));
73-
rows.push_back(absl::StrFormat("Pages full: %d", pages_full));
74-
rows.push_back(absl::StrFormat("Pages reserved for malloc: %d", pages_reserved_for_malloc));
75-
rows.push_back(
76-
absl::StrFormat("Pages skipped due to heap mismatch: %d", pages_with_heap_mismatch));
77-
rows.push_back(absl::StrFormat("Pages with usage above threshold: %d", pages_above_threshold));
74+
std::string response;
75+
StrAppend(&response, "Page usage threshold: ", threshold * 100, "\n");
76+
StrAppend(&response, "Pages scanned: ", pages_scanned, "\n");
77+
StrAppend(&response, "Pages marked for reallocation: ", pages_marked_for_realloc, "\n");
78+
StrAppend(&response, "Pages full: ", pages_full, "\n");
79+
StrAppend(&response, "Pages reserved for malloc: ", pages_reserved_for_malloc, "\n");
80+
StrAppend(&response, "Pages skipped due to heap mismatch: ", pages_with_heap_mismatch, "\n");
81+
StrAppend(&response, "Pages with usage above threshold: ", pages_above_threshold, "\n");
7882
for (const auto& [shard_id, usage] : shard_wide_summary) {
79-
rows.push_back(absl::StrFormat("[Shard %d]", shard_id));
83+
StrAppend(&response, "[Shard ", shard_id, "]\n");
8084
for (const auto& [percentage, count] : usage) {
81-
rows.push_back(absl::StrFormat(" %d%% pages are below %d%% block usage", percentage, count));
85+
StrAppend(&response,
86+
StrFormat(" %d%% pages are below %d%% block usage\n", percentage, count));
8287
}
8388
}
84-
return absl::StrJoin(rows, "\n");
89+
StripTrailingAsciiWhitespace(&response);
90+
return response;
8591
}
8692

87-
UniquePages::UniquePages()
88-
: pages_scanned{MakeSBF()},
89-
pages_marked_for_realloc{MakeSBF()},
90-
pages_full{MakeSBF()},
91-
pages_reserved_for_malloc{MakeSBF()},
92-
pages_with_heap_mismatch{MakeSBF()},
93-
pages_above_threshold{MakeSBF()} {
93+
PageUsage::UniquePages::UniquePages() {
9494
hdr_histogram* h = nullptr;
9595
const auto init_result = hdr_init(1, 100, kHistSignificantFigures, &h);
9696
CHECK_EQ(0, init_result) << "failed to initialize histogram";
9797
page_usage_hist = h;
9898
}
9999

100-
UniquePages::~UniquePages() {
100+
PageUsage::UniquePages::~UniquePages() {
101101
hdr_close(page_usage_hist);
102102
}
103103

104-
void UniquePages::AddStat(mi_page_usage_stats_t stat) {
104+
void PageUsage::UniquePages::AddStat(mi_page_usage_stats_t stat) {
105105
const auto address = stat.page_address;
106106
pages_scanned.Add(address);
107107
if (stat.flags == MI_DFLY_PAGE_BELOW_THRESHOLD) {
@@ -124,7 +124,7 @@ void UniquePages::AddStat(mi_page_usage_stats_t stat) {
124124
}
125125
}
126126

127-
CollectedPageStats UniquePages::CollectedStats() const {
127+
CollectedPageStats PageUsage::UniquePages::CollectedStats() const {
128128
CollectedPageStats::ShardUsageSummary usage;
129129
for (const auto p : kUsageHistPoints) {
130130
usage[p] = hdr_value_at_percentile(page_usage_hist, p);

src/core/page_usage_stats.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <mimalloc/types.h>
99

1010
#include "core/bloom.h"
11-
#include "server/tx_base.h"
1211

1312
struct hdr_histogram;
1413

@@ -17,6 +16,7 @@ namespace dfly {
1716
enum class CollectPageStats : uint8_t { YES, NO };
1817

1918
struct FilterWithSize {
19+
FilterWithSize();
2020
SBF sbf;
2121
size_t size;
2222
void Add(uintptr_t);
@@ -33,30 +33,14 @@ struct CollectedPageStats {
3333

3434
using ShardUsageSummary = absl::btree_map<uint8_t, uint64_t>;
3535
ShardUsageSummary page_usage_hist;
36-
absl::btree_map<ShardId, ShardUsageSummary> shard_wide_summary;
36+
absl::btree_map<uint16_t, ShardUsageSummary> shard_wide_summary;
3737

38-
void Merge(CollectedPageStats&& other, ShardId shard_id);
39-
static CollectedPageStats Merge(CollectedPageStats* stats, size_t size, float threshold);
38+
void Merge(CollectedPageStats&& other, uint16_t shard_id);
39+
static CollectedPageStats Merge(absl::Span<CollectedPageStats> stats, float threshold);
4040

4141
std::string ToString() const;
4242
};
4343

44-
struct UniquePages {
45-
FilterWithSize pages_scanned;
46-
FilterWithSize pages_marked_for_realloc;
47-
FilterWithSize pages_full;
48-
FilterWithSize pages_reserved_for_malloc;
49-
FilterWithSize pages_with_heap_mismatch;
50-
FilterWithSize pages_above_threshold;
51-
hdr_histogram* page_usage_hist;
52-
53-
explicit UniquePages();
54-
~UniquePages();
55-
56-
void AddStat(mi_page_usage_stats_t stat);
57-
CollectedPageStats CollectedStats() const;
58-
};
59-
6044
class PageUsage {
6145
public:
6246
PageUsage(CollectPageStats collect_stats, float threshold);
@@ -75,6 +59,22 @@ class PageUsage {
7559
CollectPageStats collect_stats_{CollectPageStats::NO};
7660
float threshold_;
7761

62+
struct UniquePages {
63+
FilterWithSize pages_scanned{};
64+
FilterWithSize pages_marked_for_realloc{};
65+
FilterWithSize pages_full{};
66+
FilterWithSize pages_reserved_for_malloc{};
67+
FilterWithSize pages_with_heap_mismatch{};
68+
FilterWithSize pages_above_threshold{};
69+
hdr_histogram* page_usage_hist{};
70+
71+
explicit UniquePages();
72+
~UniquePages();
73+
74+
void AddStat(mi_page_usage_stats_t stat);
75+
CollectedPageStats CollectedStats() const;
76+
};
77+
7878
UniquePages unique_pages_;
7979
};
8080

src/server/memory_cmd.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ void MemoryCmd::Run(CmdArgList args) {
188188
}
189189
});
190190

191-
const auto merged = CollectedPageStats::Merge(results, shard_set->size(), threshold);
191+
const CollectedPageStats merged =
192+
CollectedPageStats::Merge(absl::Span(results, shard_set->size()), threshold);
192193
auto* rb = static_cast<RedisReplyBuilder*>(builder_);
193194
return rb->SendVerbatimString(merged.ToString());
194195
}

0 commit comments

Comments
 (0)