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 8 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
13 changes: 11 additions & 2 deletions clients/drcachesim/simulator/cache_fifo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ cache_fifo_t::init(int associativity, int block_size, int total_size,
void
cache_fifo_t::access_update(int block_idx, int way)
{
// Since the FIFO replacement policy is independent of cache hit,
// we do not need to do anything here.
return;
}

Expand All @@ -86,3 +84,14 @@ cache_fifo_t::replace_which_way(int block_idx)
}
return -1;
}

int
cache_fifo_t::get_next_way_to_replace(const int block_idx)
{
for (int i = 0; i < associativity_; i++) {
if (get_caching_device_block(block_idx, i).counter_ == 1) {
return i;
}
}
return -1;
}
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) override;
};

#endif /* _CACHE_FIFO_H_ */
6 changes: 6 additions & 0 deletions clients/drcachesim/simulator/cache_lru.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ cache_lru_t::replace_which_way(int line_idx)
}
return max_way;
}

int
cache_lru_t::get_next_way_to_replace(const int block_idx)
{
return replace_which_way(block_idx);
bete0 marked this conversation as resolved.
Show resolved Hide resolved
}
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) override;
};

#endif /* _CACHE_LRU_H_ */
21 changes: 21 additions & 0 deletions clients/drcachesim/simulator/caching_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,27 @@ caching_device_t::replace_which_way(int block_idx)
return min_way;
}

int
caching_device_t::get_next_way_to_replace(const int block_idx)
{
// 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) {
if (get_caching_device_block(block_idx, way).tag_ == TAG_INVALID) {
min_way = way;
break;
}
if (way == 0 || get_caching_device_block(block_idx, way).counter_ < min_counter) {
min_counter = get_caching_device_block(block_idx, way).counter_;
min_way = way;
}
}
return min_way;
}

void
caching_device_t::invalidate(addr_t tag, invalidation_type_t invalidation_type)
{
Expand Down
10 changes: 10 additions & 0 deletions 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);
bete0 marked this conversation as resolved.
Show resolved Hide resolved
bete0 marked this conversation as resolved.
Show resolved Hide resolved
virtual void
record_access_stats(const memref_t &memref, bool hit,
caching_device_block_t *cache_block);
Expand All @@ -135,6 +144,7 @@ class caching_device_t {
{
return (tag & blocks_per_set_mask_) << assoc_bits_;
}

bete0 marked this conversation as resolved.
Show resolved Hide resolved
inline caching_device_block_t &
get_caching_device_block(int block_idx, int way)
{
Expand Down
133 changes: 125 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,42 @@ 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);
}
};

class cache_fifo_test_t : public cache_fifo_t {
bete0 marked this conversation as resolved.
Show resolved Hide resolved
public:
void
access_and_check_lru(const addr_t addr,
const int expected_replacement_way_after_access)
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 +124,107 @@ 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_fifo_test_t cache_fifo_test;
cache_fifo_test.initialize_cache(/*associativity=*/4, /*line_size=*/32,
/*total_size=*/256);
const addr_t ADDRESS_A = 0;
const addr_t ADDRESS_B = 64;
const addr_t ADDRESS_C = 128;
const addr_t ADDRESS_D = 192;
const addr_t ADDRESS_E = 72;
const addr_t ADDRESS_F = 130;

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));

// Lower-case letter shows the way that is to be replaced after the access.
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 1); // A x X X
cache_fifo_test.access_and_check_fifo(ADDRESS_B, 2); // A B x X
cache_fifo_test.access_and_check_fifo(ADDRESS_C, 3); // A B C x
cache_fifo_test.access_and_check_fifo(ADDRESS_D, 0); // a B C D
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_fifo(ADDRESS_E, 0); // a B C D
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 0); // a B C D
cache_fifo_test.access_and_check_fifo(ADDRESS_F, 0); // a B C D
}

void
unit_test_cache_fifo_eight_way()
{
cache_fifo_test_t cache_fifo_test;
cache_fifo_test.initialize_cache(/*associativity=*/8, /*line_size=*/32,
/*total_size=*/1024);
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 = 144;
const addr_t ADDRESS_J = 150;
const addr_t ADDRESS_K = 900;
const addr_t ADDRESS_L = 780;

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));

// Lower-case letter shows the way that is to be replaced after the access
// (aka 'first way').
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 1); // A x X X X X X X
cache_fifo_test.access_and_check_fifo(ADDRESS_B, 2); // A B x X X X X X
cache_fifo_test.access_and_check_fifo(ADDRESS_C, 3); // A B C x X X X X
cache_fifo_test.access_and_check_fifo(ADDRESS_D, 4); // A B C D x X X X
cache_fifo_test.access_and_check_fifo(ADDRESS_E, 5); // A B C D E x X X
cache_fifo_test.access_and_check_fifo(ADDRESS_F, 6); // A B C D E F x X
cache_fifo_test.access_and_check_fifo(ADDRESS_G, 7); // A B C D F F G x
cache_fifo_test.access_and_check_fifo(ADDRESS_H, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_E, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_E, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_E, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_A, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_I, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_J, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_K, 0); // a B C D E F G H
cache_fifo_test.access_and_check_fifo(ADDRESS_L, 0); // a B C D 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.
}