Skip to content

Commit a04a39d

Browse files
authored
i#7324: Add set_parent to caching_device_t (#7325)
- Added a function that allows setting the parent of a caching device. It handles the parent's children vector as well. - Added `get_children` to allow testing - Added a test. Fixes #7324
1 parent 6d04dd1 commit a04a39d

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

clients/drcachesim/simulator/caching_device.h

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#ifndef _CACHING_DEVICE_H_
3737
#define _CACHING_DEVICE_H_ 1
3838

39+
#include <algorithm>
3940
#include <cstddef>
4041
#include <cstdint>
4142
#include <functional>
@@ -113,6 +114,24 @@ class caching_device_t {
113114
{
114115
return parent_;
115116
}
117+
void
118+
set_parent(caching_device_t *parent)
119+
{
120+
if (parent_ != nullptr) {
121+
parent_->children_.erase(
122+
std::remove(parent_->children_.begin(), parent_->children_.end(), this),
123+
parent_->children_.end());
124+
}
125+
if (parent != nullptr) {
126+
parent->children_.push_back(this);
127+
}
128+
parent_ = parent;
129+
}
130+
const std::vector<caching_device_t *> &
131+
get_children() const
132+
{
133+
return children_;
134+
}
116135
inline double
117136
get_loaded_fraction() const
118137
{

clients/drcachesim/tests/drcachesim_unit_tests.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,42 @@ class test_cache_simulator_t : public cache_simulator_t {
457457
}
458458
};
459459

460+
void
461+
unit_test_set_parent()
462+
{
463+
cache_t child_1;
464+
cache_t child_2;
465+
cache_t parent;
466+
assert(child_1.init(1, 64, 1024, nullptr, new cache_stats_t(64, "", false, false)));
467+
assert(child_2.init(1, 64, 1024, nullptr, new cache_stats_t(64, "", false, false)));
468+
assert(parent.init(1, 64, 1024, nullptr, new cache_stats_t(64, "", false, false)));
469+
// Test setting parent.
470+
child_1.set_parent(&parent);
471+
assert(child_1.get_parent() == &parent);
472+
assert(parent.get_parent() == nullptr);
473+
assert(parent.get_children() == std::vector<caching_device_t *> { &child_1 });
474+
assert(child_1.get_children().empty());
475+
// Test removing parent.
476+
child_1.set_parent(nullptr);
477+
assert(parent.get_parent() == nullptr);
478+
assert(child_1.get_parent() == nullptr);
479+
assert(parent.get_children().empty());
480+
assert(child_1.get_children().empty());
481+
// Test multiple children.
482+
child_1.set_parent(&parent);
483+
child_2.set_parent(&parent);
484+
assert(child_1.get_parent() == &parent);
485+
assert(child_2.get_parent() == &parent);
486+
assert((parent.get_children() ==
487+
std::vector<caching_device_t *> { &child_1, &child_2 }));
488+
// Test existing child.
489+
child_2.set_parent(&parent);
490+
assert(child_1.get_parent() == &parent);
491+
assert(child_2.get_parent() == &parent);
492+
assert((parent.get_children() ==
493+
std::vector<caching_device_t *> { &child_1, &child_2 }));
494+
}
495+
460496
void
461497
unit_test_exclusive_cache()
462498
{
@@ -960,6 +996,7 @@ test_main(int argc, const char *argv[])
960996
unit_test_core_sharded();
961997
unit_test_nextline_prefetcher();
962998
unit_test_custom_prefetcher();
999+
unit_test_set_parent();
9631000
return 0;
9641001
}
9651002

0 commit comments

Comments
 (0)