Skip to content

Commit

Permalink
i#4842 drcachesim unit tests: Add cache_lru unit test (#5405)
Browse files Browse the repository at this point in the history
Adds a unit test for clients/drcachesim/simulators/cache_lru.cpp.
This unit test catches a bug reported in i#4881 which was tested by
reverting the fix for that issue (PR #4883).

Issue: #4842, #4881
  • Loading branch information
bete0 authored Mar 23, 2022
1 parent 7aa929c commit 5e13602
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clients/drcachesim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ endif ()
# Be sure to give the targets qualified test names ("tool.drcache*...").

if (BUILD_TESTS)
add_executable(tool.drcachesim.unit_tests tests/drcachesim_unit_tests.cpp)
add_executable(tool.drcachesim.unit_tests tests/drcachesim_unit_tests.cpp
tests/cache_replacement_policy_unit_test.cpp)
if (ZLIB_FOUND)
target_link_libraries(tool.drcachesim.unit_tests drmemtrace_simulator
drmemtrace_static drmemtrace_analyzer ${ZLIB_LIBRARIES})
Expand Down
113 changes: 113 additions & 0 deletions clients/drcachesim/tests/cache_replacement_policy_unit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* **********************************************************
* Copyright (c) 2016-2022 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

// Unit tests for cache replacement policies
#include <iostream>
#undef NDEBUG
#include <assert.h>
#include "cache_replacement_policy_unit_test.h"
#include "simulator/cache_lru.h"

class cache_lru_test_t : public cache_lru_t {
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 << "LRU cache failed to initialize\n";
exit(1);
}
}

int
get_block_index(const addr_t addr)
{
addr_t tag = compute_tag(addr);
int block_idx = compute_block_idx(tag);
return block_idx;
}

void
access_and_check_lru(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;
request(ref);
assert(replace_which_way(get_block_index(addr)) ==
expected_replacement_way_after_access);
}
};

void
unit_test_cache_lru_four_way()
{
cache_lru_test_t cache_lru_test;
cache_lru_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;

assert(cache_lru_test.get_block_index(ADDRESS_A) ==
cache_lru_test.get_block_index(ADDRESS_B));
assert(cache_lru_test.get_block_index(ADDRESS_B) ==
cache_lru_test.get_block_index(ADDRESS_C));
assert(cache_lru_test.get_block_index(ADDRESS_C) ==
cache_lru_test.get_block_index(ADDRESS_D));
assert(cache_lru_test.get_block_index(ADDRESS_D) ==
cache_lru_test.get_block_index(ADDRESS_E));

// Access the cache line in the following fashion. This sequence follows the
// sequence shown in i#4881.
// Lower-case letter shows the least recently used way.
cache_lru_test.access_and_check_lru(ADDRESS_A, 1); // A x X X
cache_lru_test.access_and_check_lru(ADDRESS_B, 2); // A B x X
cache_lru_test.access_and_check_lru(ADDRESS_C, 3); // A B C x
cache_lru_test.access_and_check_lru(ADDRESS_D, 0); // a B C D
cache_lru_test.access_and_check_lru(ADDRESS_A, 1); // A b C D
cache_lru_test.access_and_check_lru(ADDRESS_A, 1); // A b C D
cache_lru_test.access_and_check_lru(ADDRESS_A, 1); // A b C D
cache_lru_test.access_and_check_lru(ADDRESS_E, 2); // A E c D
}

void
unit_test_cache_replacement_policy()
{
unit_test_cache_lru_four_way();
// XXX i#4842: Add more test sequences.
}
39 changes: 39 additions & 0 deletions clients/drcachesim/tests/cache_replacement_policy_unit_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* **********************************************************
* Copyright (c) 2016-2022 Google, Inc. All rights reserved.
* **********************************************************/

/*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Google, Inc. nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/

#ifndef _CACHE_REPLACEMENT_POLICY_UNIT_TESTS_
#define _CACHE_REPLACEMENT_POLICY_UNIT_TESTS_ 1

void
unit_test_cache_replacement_policy();

#endif /* _CACHE_REPLACEMENT_POLICY_UNIT_TESTS_ */
3 changes: 3 additions & 0 deletions clients/drcachesim/tests/drcachesim_unit_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
// Unit tests for drcachesim
#include <iostream>
#include <cstdlib>
#undef NDEBUG
#include <assert.h>
#include "cache_replacement_policy_unit_test.h"
#include "simulator/cache_simulator.h"
#include "../common/memref.h"

Expand Down Expand Up @@ -313,5 +315,6 @@ main(int argc, const char *argv[])
unit_test_warmup_refs();
unit_test_sim_refs();
unit_test_child_hits();
unit_test_cache_replacement_policy();
return 0;
}

0 comments on commit 5e13602

Please sign in to comment.