Skip to content

Commit a359b61

Browse files
committed
core,server: Add print for result of defrag
1 parent 17dcdf5 commit a359b61

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/core/page_usage_stats.cc

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

77
#include <absl/container/flat_hash_set.h>
8-
#include <cmake-build-debug/third_party/libs/hdr_histogram/include/hdr/hdr_histogram.h>
8+
#include <absl/strings/str_join.h>
99
#include <glog/logging.h>
1010
#include <hdr/hdr_histogram.h>
1111

@@ -54,13 +54,43 @@ void CollectedPageStats::Merge(CollectedPageStats&& other, ShardId shard_id) {
5454
shard_wide_summary.emplace(std::make_pair(shard_id, std::move(other.page_usage_hist)));
5555
}
5656

57-
UniquePages::UniquePages(float threshold)
58-
: pages_scanned{MakeBloomFilter()},
59-
pages_marked_for_realloc{MakeBloomFilter()},
60-
pages_full{MakeBloomFilter()},
61-
pages_reserved_for_malloc{MakeBloomFilter()},
62-
pages_with_heap_mismatch{MakeBloomFilter()},
63-
pages_above_threshold{MakeBloomFilter()} {
57+
CollectedPageStats CollectedPageStats::Merge(CollectedPageStats* stats, const size_t size,
58+
const float threshold) {
59+
CollectedPageStats result;
60+
result.threshold = threshold;
61+
for (size_t i = 0; i < size; ++i) {
62+
result.Merge(std::move(*stats), i);
63+
stats++;
64+
}
65+
return result;
66+
}
67+
68+
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));
78+
for (const auto& [shard_id, usage] : shard_wide_summary) {
79+
rows.push_back(absl::StrFormat("[Shard %d]", shard_id));
80+
for (const auto& [percentage, count] : usage) {
81+
rows.push_back(absl::StrFormat(" %d%% pages are below %d%% block usage", percentage, count));
82+
}
83+
}
84+
return absl::StrJoin(rows, "\n");
85+
}
86+
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()} {
6494
hdr_histogram* h = nullptr;
6595
const auto init_result = hdr_init(1, 100, kHistSignificantFigures, &h);
6696
CHECK_EQ(0, init_result) << "failed to initialize histogram";

src/core/page_usage_stats.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ struct CollectedPageStats {
3636
absl::btree_map<ShardId, ShardUsageSummary> shard_wide_summary;
3737

3838
void Merge(CollectedPageStats&& other, ShardId shard_id);
39+
static CollectedPageStats Merge(CollectedPageStats* stats, size_t size, float threshold);
40+
41+
std::string ToString() const;
3942
};
4043

4144
struct UniquePages {

src/server/memory_cmd.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ void MemoryCmd::Run(CmdArgList args) {
187187
}
188188
}
189189
});
190-
return builder_->SendSimpleString("OK");
190+
191+
const auto merged = CollectedPageStats::Merge(results, shard_set->size(), threshold);
192+
auto* rb = static_cast<RedisReplyBuilder*>(builder_);
193+
return rb->SendVerbatimString(merged.ToString());
191194
}
192195

193196
string err = UnknownSubCmd(parser.Next(), "MEMORY");

0 commit comments

Comments
 (0)