Skip to content

Commit 27a7aa3

Browse files
authored
Adding object memory tracking (#5119)
1 parent d0ec02b commit 27a7aa3

File tree

3 files changed

+204
-173
lines changed

3 files changed

+204
-173
lines changed

src/core/intrusive_string_list.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ class ISLEntry {
196196
return (uptr() & kTtlBit) != 0;
197197
}
198198

199+
size_t Size() {
200+
size_t key_field_size = HasSso() ? 1 : 4;
201+
size_t ttl_field_size = HasTtl() ? 4 : 0;
202+
return (sizeof(char*) + ttl_field_size + key_field_size + GetKeySize());
203+
}
204+
199205
[[nodiscard]] bool UpdateTtl(uint32_t ttl_sec) {
200206
if (HasTtl()) {
201207
auto* ttl_pos = Raw() + sizeof(char*);
@@ -260,22 +266,25 @@ class IntrusiveStringList {
260266
return prev_.Next().ExpiryTime();
261267
}
262268

263-
void SetExpiryTime(uint32_t ttl_sec) {
269+
void SetExpiryTime(uint32_t ttl_sec, size_t* obj_malloc_used) {
264270
auto entry = prev_.Next();
265271

266272
if (!entry.UpdateTtl(ttl_sec)) {
267273
ISLEntry new_entry = ISLEntry::Create(entry.Key(), entry.Next().data_, ttl_sec);
274+
(*obj_malloc_used) += sizeof(new_entry) + new_entry.Size();
275+
(*obj_malloc_used) -= sizeof(entry) + entry.Size();
268276
ISLEntry::Destroy(entry);
269277
prev_.SetNext(new_entry);
270278
}
271279
}
272280

273-
bool ExpireIfNeeded(uint32_t time_now) {
281+
bool ExpireIfNeeded(uint32_t time_now, size_t* obj_malloc_used) {
274282
auto entry = prev_.Next();
275283

276284
if (auto entry_time = entry.ExpiryTime();
277285
entry_time != UINT32_MAX && time_now >= entry_time) {
278286
prev_.SetNext(prev_.Next().Next());
287+
(*obj_malloc_used) -= sizeof(entry) + entry.Size();
279288
ISLEntry::Destroy(entry);
280289
return true;
281290
}
@@ -340,12 +349,14 @@ class IntrusiveStringList {
340349
ISLEntry& Insert(ISLEntry e) {
341350
e.SetNext(start_);
342351
start_ = e;
352+
obj_malloc_used_ += sizeof(e) + e.Size();
343353
return start_;
344354
}
345355

346356
UniqueISLEntry Pop(uint32_t curr_time) {
347357
for (auto it = start_; it && it.ExpiryTime() < curr_time; it = start_) {
348358
start_ = it.Next();
359+
obj_malloc_used_ -= sizeof(it) + it.Size();
349360
ISLEntry::Destroy(it);
350361
}
351362
auto res = start_;
@@ -371,7 +382,7 @@ class IntrusiveStringList {
371382
auto it = begin();
372383

373384
for (; it; ++it) {
374-
if (it.ExpireIfNeeded(time_now)) {
385+
if (it.ExpireIfNeeded(time_now, &obj_malloc_used_)) {
375386
(*expired_fields)++;
376387
continue;
377388
}
@@ -388,7 +399,7 @@ class IntrusiveStringList {
388399
uint32_t* expired_fields, uint32_t time_now = UINT32_MAX) {
389400
auto entry = begin();
390401
for (; entry; ++entry) {
391-
if (entry.ExpireIfNeeded(time_now)) {
402+
if (entry.ExpireIfNeeded(time_now, &obj_malloc_used_)) {
392403
(*expired_fields)++;
393404
continue;
394405
}
@@ -446,7 +457,12 @@ class IntrusiveStringList {
446457
return start_;
447458
}
448459

460+
size_t ObjMallocUsed() const {
461+
return obj_malloc_used_;
462+
};
463+
449464
private:
465+
size_t obj_malloc_used_;
450466
ISLEntry start_;
451467
static ISLEntry end_;
452468
};

src/core/intrusive_string_set.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class IntrusiveStringSet {
4040
// return prev.Next()->ExpiryTime();
4141
// }
4242

43-
void SetExpiryTime(uint32_t ttl_sec) {
44-
entry_.SetExpiryTime(ttl_sec);
43+
void SetExpiryTime(uint32_t ttl_sec, size_t* obj_malloc_used) {
44+
entry_.SetExpiryTime(ttl_sec, obj_malloc_used);
4545
}
4646

4747
// bool HasExpiry() const {
@@ -275,6 +275,18 @@ class IntrusiveStringSet {
275275
return time_now_;
276276
}
277277

278+
size_t ObjMallocUsed() const {
279+
size_t bucket_obj_memory = 0;
280+
for (const auto& bucket : entries_) {
281+
bucket_obj_memory += bucket.ObjMallocUsed();
282+
}
283+
return bucket_obj_memory;
284+
}
285+
286+
size_t SetMallocUsed() const {
287+
return entries_.capacity() * sizeof(IntrusiveStringList);
288+
}
289+
278290
private:
279291
// was Grow in StringSet
280292
void Rehash(size_t prev_size) {
@@ -309,8 +321,9 @@ class IntrusiveStringSet {
309321
std::uint32_t size_ = 0; // number of elements in the set.
310322
std::uint32_t time_now_ = 0;
311323

312-
static_assert(sizeof(IntrusiveStringList) == sizeof(void*),
313-
"IntrusiveStringList should be just a pointer");
324+
// TODO: fix obj memory management
325+
static_assert(sizeof(IntrusiveStringList) == (sizeof(void*) + sizeof(size_t)),
326+
"IntrusiveStringList should be pointer + memory track");
314327
Buckets entries_;
315328
};
316329

0 commit comments

Comments
 (0)