-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i#4842 drcachesim unit tests: Add cache_lru unit test
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
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |