Skip to content

Commit

Permalink
i#4842 drcachesim unit tests: Add cache_lru unit test
Browse files Browse the repository at this point in the history
This change adds unit tests for clients/drcachesim/simulators/cache_lru.cpp.
These unit tests catch issues reported in i#4881 which was tested by
reverting the fix for that issue (#4883).

Issue: #4842
  • Loading branch information
bete0 committed Mar 21, 2022
1 parent 5031bcf commit 74a300c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 45 deletions.
59 changes: 17 additions & 42 deletions clients/drcachesim/tests/cache_replacement_policy_unit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,21 @@
#include "cache_replacement_policy_unit_test.h"
#include "simulator/cache_lru.h"

struct cache_config_t {
int associativity = 4;
int line_size = 32;
int total_size = 256;
caching_device_t *parent = nullptr;
caching_device_stats_t *stats = new cache_stats_t(line_size, "", true);
prefetcher_t *prefetcher = nullptr;
bool inclusive = true;
bool coherent_cache = true;
int id = -1;
snoop_filter_t *snoop_filter = nullptr;
};

static cache_config_t
make_test_config(int associativity, int line_size, int total_size)
{
cache_config_t config;
config.associativity = associativity;
config.line_size = line_size;
config.total_size = total_size;
return config;
}

class cache_lru_test_t : public cache_lru_t {
public:
void
initialize_cache(int way, int line_size, int total_size)
{
caching_device_t *parent = nullptr;
prefetcher_t *prefetcher = nullptr;
caching_device_stats_t *stats = new cache_stats_t(line_size, "", true);
if (!init(way, line_size, total_size, parent, stats, prefetcher)) {
std::cerr << "LRU cache failed to initialize"
<< "\n";
exit(1);
}
}

void
unit_test_replace_which_way()
{
Expand All @@ -70,15 +60,7 @@ class cache_lru_test_t : public cache_lru_t {
// ----------------------------------------------
// | way 0 | way 1 | way 2 | way 3 |
// -----------------------------------------------
cache_config_t config_1 = make_test_config(4, 32, 256);
if (!init(config_1.associativity, config_1.line_size, config_1.total_size,
config_1.parent, config_1.stats, config_1.prefetcher,
config_1.inclusive, config_1.coherent_cache, config_1.id,
config_1.snoop_filter)) {
std::cerr << "LRU cache failed to initialize"
<< "\n";
exit(1);
}
initialize_cache(4, 32, 256);

memref_t ref_1;
ref_1.data.type = TRACE_TYPE_READ;
Expand All @@ -89,7 +71,7 @@ class cache_lru_test_t : public cache_lru_t {
}
// Access the ways in the following fashion. This sequence follows the sequence
// shown in https://github.com/DynamoRIO/dynamorio/issues/4881.
// Access the ways in 3, 0, 1, 2 order.
// Access the ways in 3("a"), 0("b"), 1("c"), 2("b") order.
ref_1.data.addr = 192; // Access way 3 ("a" in issue 4881).
request(ref_1);

Expand All @@ -102,19 +84,12 @@ class cache_lru_test_t : public cache_lru_t {
ref_1.data.addr = 128; // Access way 2 ("d" in issue 4881).
request(ref_1);

// Way 3 ("a") should be replaced.
assert(replace_which_way(0) == 3);

// Create and initialize an 8-way set associative cache with line size of 32 and
// total size of 256 bytes.
cache_config_t config_2 = make_test_config(8, 32, 256);
if (!init(config_2.associativity, config_2.line_size, config_2.total_size,
config_2.parent, config_2.stats, config_2.prefetcher,
config_2.inclusive, config_2.coherent_cache, config_2.id,
config_2.snoop_filter)) {
std::cerr << "LRU cache failed to initialize"
<< "\n";
exit(1);
}
initialize_cache(8, 32, 256);

memref_t ref_2;
ref_2.data.type = TRACE_TYPE_READ;
Expand Down
6 changes: 3 additions & 3 deletions clients/drcachesim/tests/cache_replacement_policy_unit_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
* DAMAGE.
*/

#ifndef _CACHE_REPL_POLICY_UNIT_TESTS_
#define _CACHE_REPL_POLICY_UNIT_TESTS_ 1
#ifndef _CACHE_REPLACEMENT_POLICY_UNIT_TESTS_
#define _CACHE_REPLACEMENT_POLICY_UNIT_TESTS_ 1

#include "simulator/cache_lru.h"

void
unit_test_cache_replacement_policy();

#endif /* _CACHE_REPL_POLICY_UNIT_TESTS_ */
#endif /* _CACHE_REPLACEMENT_POLICY_UNIT_TESTS_ */

0 comments on commit 74a300c

Please sign in to comment.