Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i#4842 drcachesim unit test: Add cache_fifo unit test #5454

Merged
merged 18 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions clients/drcachesim/simulator/cache_fifo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,32 @@ cache_fifo_t::access_update(int block_idx, int way)
return;
}

// This method replaces the current victim way and returns the next victim way
// to be replaced. As opposed to get_next_way_to_replace() which just returns the
// next way to be replaced without updating the cache state, replace_which_way()
// updates the cache state.
int
cache_fifo_t::replace_which_way(int block_idx)
{
// We replace the block whose counter is 1.
int victim_way = get_next_way_to_replace(block_idx);
if (victim_way != -1) {
bete0 marked this conversation as resolved.
Show resolved Hide resolved
// clear the counter of the victim block
get_caching_device_block(block_idx, victim_way).counter_ = 0;
// set the next block as victim
get_caching_device_block(block_idx, (victim_way + 1) & (associativity_ - 1))
.counter_ = 1;
}
return victim_way;
}

// This method just returns the next way to be replaced without actually
// replacing it, hence doesn't have a side-effect.
int
cache_fifo_t::get_next_way_to_replace(const int block_idx) const
{
for (int i = 0; i < associativity_; i++) {
// We return the block whose counter is 1.
if (get_caching_device_block(block_idx, i).counter_ == 1) {
// clear the counter of the victim block
get_caching_device_block(block_idx, i).counter_ = 0;
// set the next block as victim
get_caching_device_block(block_idx, (i + 1) & (associativity_ - 1)).counter_ =
1;
return i;
}
}
Expand Down
2 changes: 2 additions & 0 deletions clients/drcachesim/simulator/cache_fifo.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class cache_fifo_t : public cache_t {
access_update(int line_idx, int way) override;
int
replace_which_way(int line_idx) override;
int
get_next_way_to_replace(const int block_idx) const override;
};

#endif /* _CACHE_FIFO_H_ */
12 changes: 9 additions & 3 deletions clients/drcachesim/simulator/cache_lru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,23 @@ cache_lru_t::access_update(int line_idx, int way)

int
cache_lru_t::replace_which_way(int line_idx)
{
return get_next_way_to_replace(line_idx);
bete0 marked this conversation as resolved.
Show resolved Hide resolved
}

int
cache_lru_t::get_next_way_to_replace(int block_idx) const
{
// We implement LRU by picking the slot with the largest counter value.
int max_counter = 0;
int max_way = 0;
for (int way = 0; way < associativity_; ++way) {
if (get_caching_device_block(line_idx, way).tag_ == TAG_INVALID) {
if (get_caching_device_block(block_idx, way).tag_ == TAG_INVALID) {
max_way = way;
break;
}
if (get_caching_device_block(line_idx, way).counter_ > max_counter) {
max_counter = get_caching_device_block(line_idx, way).counter_;
if (get_caching_device_block(block_idx, way).counter_ > max_counter) {
max_counter = get_caching_device_block(block_idx, way).counter_;
max_way = way;
}
}
Expand Down
2 changes: 2 additions & 0 deletions clients/drcachesim/simulator/cache_lru.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class cache_lru_t : public cache_t {
access_update(int line_idx, int way) override;
int
replace_which_way(int line_idx) override;
int
get_next_way_to_replace(const int block_idx) const override;
};

#endif /* _CACHE_LRU_H_ */
14 changes: 12 additions & 2 deletions clients/drcachesim/simulator/caching_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,22 @@ caching_device_t::access_update(int block_idx, int way)

int
caching_device_t::replace_which_way(int block_idx)
{
int min_way = get_next_way_to_replace(block_idx);
// Clear the counter for LFU.
get_caching_device_block(block_idx, min_way).counter_ = 0;
return min_way;
}

int
caching_device_t::get_next_way_to_replace(const int block_idx) const
{
// The base caching device class only implements LFU.
// A subclass can override this and access_update() to implement
// some other scheme.
// Return the victim 'way' for the default LFU cache which the base class
// implements. Subclasses override this and return victim 'way' specific to
// their own implementation.
bete0 marked this conversation as resolved.
Show resolved Hide resolved
int min_counter = 0; /* avoid "may be used uninitialized" with GCC 4.4.7 */
int min_way = 0;
for (int way = 0; way < associativity_; ++way) {
Expand All @@ -272,8 +284,6 @@ caching_device_t::replace_which_way(int block_idx)
min_way = way;
}
}
// Clear the counter for LFU.
get_caching_device_block(block_idx, min_way).counter_ = 0;
return min_way;
}

Expand Down
11 changes: 10 additions & 1 deletion clients/drcachesim/simulator/caching_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,21 @@ class caching_device_t {
}
use_tag2block_table_ = use_hashtable;
}
int
get_block_index(const addr_t addr)
bete0 marked this conversation as resolved.
Show resolved Hide resolved
{
addr_t tag = compute_tag(addr);
int block_idx = compute_block_idx(tag);
return block_idx;
}

protected:
virtual void
access_update(int block_idx, int way);
virtual int
replace_which_way(int block_idx);
virtual int
get_next_way_to_replace(const int block_idx) const;
virtual void
record_access_stats(const memref_t &memref, bool hit,
caching_device_block_t *cache_block);
Expand All @@ -136,7 +145,7 @@ class caching_device_t {
return (tag & blocks_per_set_mask_) << assoc_bits_;
}
inline caching_device_block_t &
get_caching_device_block(int block_idx, int way)
get_caching_device_block(int block_idx, int way) const
{
return *(blocks_[block_idx + way]);
}
Expand Down
202 changes: 194 additions & 8 deletions clients/drcachesim/tests/cache_replacement_policy_unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#undef NDEBUG
#include <assert.h>
#include "cache_replacement_policy_unit_test.h"
#include "simulator/cache_fifo.h"
#include "simulator/cache_lru.h"

class cache_lru_test_t : public cache_lru_t {
bete0 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -49,24 +50,90 @@ class cache_lru_test_t : public cache_lru_t {
}
}

int
get_block_index(const addr_t addr)
void
access_and_check_lru(const addr_t addr,
const int expected_replacement_way_after_access)
{
addr_t tag = compute_tag(addr);
int block_idx = compute_block_idx(tag);
return block_idx;
memref_t ref;
ref.data.type = TRACE_TYPE_READ;
ref.data.size = 1;
ref.data.addr = addr;
request(ref);
assert(get_next_way_to_replace(get_block_index(addr)) ==
expected_replacement_way_after_access);
}
};

template <class T> class cache_policy_test_t : public T {
int associativity_;
int line_size_;
int total_size_;

public:
cache_policy_test_t(int associativity, int line_size, int total_size)
{
associativity_ = associativity;
line_size_ = line_size;
total_size_ = total_size;
}
void
access_and_check_lru(const addr_t addr,
const int expected_replacement_way_after_access)
initialize_cache()
{
caching_device_stats_t *stats = new cache_stats_t(line_size_, "", true);
if (!this->init(associativity_, line_size_, total_size_, nullptr, stats,
bete0 marked this conversation as resolved.
Show resolved Hide resolved
nullptr)) {
std::cerr << "FIFO cache failed to initialize\n";
exit(1);
}
}

void
access_and_check_cache(const addr_t addr,
const int expected_replacement_way_after_access)
{
memref_t ref;
ref.data.type = TRACE_TYPE_READ;
ref.data.size = 1;
ref.data.addr = addr;
this->request(ref);
assert(this->get_next_way_to_replace(this->get_block_index(addr)) ==
expected_replacement_way_after_access);
}

bool
tags_are_different(const std::vector<int> &addresses)
{
for (int i = 1; i < addresses.size(); ++i) {
if (this->compute_tag(addresses[i - 1]) == this->compute_tag(addresses[i])) {
bete0 marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
}
return true;
}
};

class cache_fifo_test_t : public cache_fifo_t {
bete0 marked this conversation as resolved.
Show resolved Hide resolved
public:
void
initialize_cache(int associativity, int line_size, int total_size)
{
caching_device_stats_t *stats = new cache_stats_t(line_size, "", true);
if (!init(associativity, line_size, total_size, nullptr, stats, nullptr)) {
std::cerr << "FIFO cache failed to initialize\n";
exit(1);
}
}

void
access_and_check_fifo(const addr_t addr,
bete0 marked this conversation as resolved.
Show resolved Hide resolved
const int expected_replacement_way_after_access)
{
memref_t ref;
ref.data.type = TRACE_TYPE_READ;
ref.data.size = 1;
ref.data.addr = addr;
request(ref);
assert(replace_which_way(get_block_index(addr)) ==
assert(get_next_way_to_replace(get_block_index(addr)) ==
expected_replacement_way_after_access);
}
};
Expand Down Expand Up @@ -105,9 +172,128 @@ unit_test_cache_lru_four_way()
cache_lru_test.access_and_check_lru(ADDRESS_E, 2); // A E c D
}

void
unit_test_cache_fifo_four_way()
{
cache_policy_test_t<cache_fifo_t> cache_fifo_test(/*associativity=*/4,
bete0 marked this conversation as resolved.
Show resolved Hide resolved
/*line_size=*/32,
/*total_size=*/256);
cache_fifo_test.initialize_cache();

const addr_t ADDRESS_A = 0;
const addr_t ADDRESS_B = 64;
const addr_t ADDRESS_C = 128;
const addr_t ADDRESS_D = 256;
const addr_t ADDRESS_E = 320;
const addr_t ADDRESS_F = 384;
const addr_t ADDRESS_G = 448;
const addr_t ADDRESS_H = 512;

assert(cache_fifo_test.get_block_index(ADDRESS_A) ==
cache_fifo_test.get_block_index(ADDRESS_B));
assert(cache_fifo_test.get_block_index(ADDRESS_B) ==
cache_fifo_test.get_block_index(ADDRESS_C));
assert(cache_fifo_test.get_block_index(ADDRESS_C) ==
cache_fifo_test.get_block_index(ADDRESS_D));
assert(cache_fifo_test.get_block_index(ADDRESS_D) ==
cache_fifo_test.get_block_index(ADDRESS_E));
assert(cache_fifo_test.get_block_index(ADDRESS_E) ==
cache_fifo_test.get_block_index(ADDRESS_F));
assert(cache_fifo_test.get_block_index(ADDRESS_F) ==
cache_fifo_test.get_block_index(ADDRESS_G));
assert(cache_fifo_test.get_block_index(ADDRESS_G) ==
bete0 marked this conversation as resolved.
Show resolved Hide resolved
cache_fifo_test.get_block_index(ADDRESS_H));

assert(cache_fifo_test.tags_are_different(
std::vector<int> { ADDRESS_A, ADDRESS_B, ADDRESS_C, ADDRESS_D, ADDRESS_E,
ADDRESS_F, ADDRESS_G, ADDRESS_H }));

// Lower-case letter shows the way that is to be replaced after the access.
cache_fifo_test.access_and_check_cache(ADDRESS_A, 1); // A x X X
cache_fifo_test.access_and_check_cache(ADDRESS_B, 2); // A B x X
cache_fifo_test.access_and_check_cache(ADDRESS_C, 3); // A B C x
cache_fifo_test.access_and_check_cache(ADDRESS_D, 0); // a B C D
cache_fifo_test.access_and_check_cache(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_cache(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_cache(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_cache(ADDRESS_E, 1); // E b C D
cache_fifo_test.access_and_check_cache(ADDRESS_F, 2); // E F c D
cache_fifo_test.access_and_check_cache(ADDRESS_F, 2); // E F c D
cache_fifo_test.access_and_check_cache(ADDRESS_G, 3); // E F G d
cache_fifo_test.access_and_check_cache(ADDRESS_G, 3); // E F G d
cache_fifo_test.access_and_check_cache(ADDRESS_H, 0); // e F G H
cache_fifo_test.access_and_check_cache(ADDRESS_A, 1); // E f G H
bete0 marked this conversation as resolved.
Show resolved Hide resolved
}

void
unit_test_cache_fifo_eight_way()
{
cache_policy_test_t<cache_fifo_t> cache_fifo_test(/*associativity=*/8,
/*line_size=*/64,
/*total_size=*/1024);
cache_fifo_test.initialize_cache();

const addr_t ADDRESS_A = 0;
const addr_t ADDRESS_B = 128;
const addr_t ADDRESS_C = 256;
const addr_t ADDRESS_D = 384;
const addr_t ADDRESS_E = 512;
const addr_t ADDRESS_F = 640;
const addr_t ADDRESS_G = 768;
const addr_t ADDRESS_H = 896;
const addr_t ADDRESS_I = 1024;
const addr_t ADDRESS_J = 1152;
const addr_t ADDRESS_K = 1280;
const addr_t ADDRESS_L = 1408;

assert(cache_fifo_test.get_block_index(ADDRESS_A) ==
cache_fifo_test.get_block_index(ADDRESS_B));
assert(cache_fifo_test.get_block_index(ADDRESS_B) ==
cache_fifo_test.get_block_index(ADDRESS_C));
assert(cache_fifo_test.get_block_index(ADDRESS_C) ==
cache_fifo_test.get_block_index(ADDRESS_D));
assert(cache_fifo_test.get_block_index(ADDRESS_D) ==
cache_fifo_test.get_block_index(ADDRESS_E));
assert(cache_fifo_test.get_block_index(ADDRESS_F) ==
cache_fifo_test.get_block_index(ADDRESS_G));
assert(cache_fifo_test.get_block_index(ADDRESS_H) ==
cache_fifo_test.get_block_index(ADDRESS_I));
assert(cache_fifo_test.get_block_index(ADDRESS_I) ==
cache_fifo_test.get_block_index(ADDRESS_J));
assert(cache_fifo_test.get_block_index(ADDRESS_J) ==
cache_fifo_test.get_block_index(ADDRESS_K));
assert(cache_fifo_test.get_block_index(ADDRESS_K) ==
cache_fifo_test.get_block_index(ADDRESS_L));

assert(cache_fifo_test.tags_are_different(std::vector<int> {
ADDRESS_A, ADDRESS_B, ADDRESS_C, ADDRESS_D, ADDRESS_E, ADDRESS_F, ADDRESS_G,
ADDRESS_H, ADDRESS_I, ADDRESS_J, ADDRESS_K, ADDRESS_L }));

// Lower-case letter shows the way that is to be replaced after the access
// (aka 'first way').
cache_fifo_test.access_and_check_cache(ADDRESS_A, 1); // A x X X X X X X
cache_fifo_test.access_and_check_cache(ADDRESS_B, 2); // A B x X X X X X
cache_fifo_test.access_and_check_cache(ADDRESS_C, 3); // A B C x X X X X
cache_fifo_test.access_and_check_cache(ADDRESS_D, 4); // A B C D x X X X
cache_fifo_test.access_and_check_cache(ADDRESS_E, 5); // A B C D E x X X
cache_fifo_test.access_and_check_cache(ADDRESS_F, 6); // A B C D E F x X
cache_fifo_test.access_and_check_cache(ADDRESS_G, 7); // A B C D F F G x
cache_fifo_test.access_and_check_cache(ADDRESS_H, 0); // a B C D E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_E, 0); // a B C D E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_A, 0); // a B C D E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_A, 0); // a B C D E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_I, 1); // I b C D E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_J, 2); // I J c D E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_K, 3); // I J K d E F G H
cache_fifo_test.access_and_check_cache(ADDRESS_L, 4); // I J K L e F G H
cache_fifo_test.access_and_check_cache(ADDRESS_L, 4); // I J K L e F G H
}

void
unit_test_cache_replacement_policy()
{
unit_test_cache_lru_four_way();
unit_test_cache_fifo_four_way();
unit_test_cache_fifo_eight_way();
// XXX i#4842: Add more test sequences.
}