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 8, 2022
1 parent 1cd0ba5 commit 352b66e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clients/drcachesim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,17 @@ endif ()
# Be sure to give the targets qualified test names ("tool.drcache*...").

if (BUILD_TESTS)
add_executable(tool.drcachesim.cache_lru_unit_tests tests/cache_lru_unit_tests.cpp)
if (ZLIB_FOUND)
target_link_libraries(tool.drcachesim.cache_lru_unit_tests drmemtrace_simulator
drmemtrace_static drmemtrace_analyzer ${ZLIB_LIBRARIES})
else ()
target_link_libraries(tool.drcachesim.cache_lru_unit_tests drmemtrace_simulator
drmemtrace_static drmemtrace_analyzer)
endif ()
add_win32_flags(tool.drcachesim.cache_lru_unit_tests)
add_test(NAME tool.drcachesim.cache_lru_unit_tests
COMMAND tool.drcachesim.cache_lru_unit_tests)
add_executable(tool.drcachesim.unit_tests tests/drcachesim_unit_tests.cpp)
if (ZLIB_FOUND)
target_link_libraries(tool.drcachesim.unit_tests drmemtrace_simulator
Expand Down
106 changes: 106 additions & 0 deletions clients/drcachesim/tests/cache_lru_unit_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* **********************************************************
* Copyright (c) 2016-2021 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_lru
#include <iostream>
#include <assert.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_configs(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
unit_test_access_update()
{
// Create and initialize an 8-way set associative cache with line size of 32 and
// total size of 1024 bytes.
cache_config_t config_two = make_test_configs(8, 32, 1024);
if (!init(config_two.associativity, config_two.line_size, config_two.total_size,
config_two.parent, config_two.stats, config_two.prefetcher,
config_two.inclusive, config_two.coherent_cache, config_two.id,
config_two.snoop_filter)) {
std::cerr << "LRU cache failed to initialize"
<< "\n";
exit(1);
}

// Access the cacheclines in set 1 in the following sequence and ensure the
// counters are updated properly.
access_update(1, 6);
access_update(1, 1);
access_update(1, 2);
access_update(1, 3);
access_update(1, 4);
access_update(1, 5);
access_update(1, 0);
access_update(1, 7);

assert(get_caching_device_block(1, 0).counter_ == 1);
assert(get_caching_device_block(1, 1).counter_ == 6);
assert(get_caching_device_block(1, 2).counter_ == 5);
assert(get_caching_device_block(1, 3).counter_ == 4);
assert(get_caching_device_block(1, 4).counter_ == 3);
assert(get_caching_device_block(1, 5).counter_ == 2);
assert(get_caching_device_block(1, 6).counter_ == 7);
assert(get_caching_device_block(1, 7).counter_ == 0);
}
};

int
main(int argc, const char *argv[])
{
cache_lru_test_t cache_lru;
cache_lru.unit_test_access_update();
return 0;
}

0 comments on commit 352b66e

Please sign in to comment.