Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Debug]
build_type: [Release]
c_compiler: [gcc, clang, cl]
include:
- os: windows-latest
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_subdirectory(main)
add_subdirectory(LRU)
add_subdirectory(LFU)
add_subdirectory(perfect)
add_subdirectory(benchmark)


include(CTest)
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
# cpp_proj
# HWC Caches. LRU, LFU, Belady's optimal cache

How to run
## How to run

```bash
./build/main/main <<< "2 6 3 3 1 2 1 2" # cache_size Nelem elem1 elem2 ...
```
output is
```bash
Cache type N hits
Belady 3
LRU 3
LFU 1
```

### Benchmark
input : uniform random integers [0: 100) with input size 100'000

![statistics](./benchmark/cache_hit_statistics.png)

# Tips
To reproduce benchmarks one needs to build the project and run
```bash
./build/benchmark/benchmark
cd benchmark
gnuplot script.gp
open cache_hit_statistics.png
```

## CMAKE


## How to build: CMAKE
```bash
cmake -S . -B build -G Ninja \
-DCMAKE_C_COMPILER=gcc-14 \
Expand Down
18 changes: 18 additions & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
add_executable(benchmark)
target_sources(benchmark
PRIVATE
benchmark.cpp
)

target_compile_options(benchmark
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/W4 /WX>
$<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra -Werror>
)

target_link_libraries(benchmark
PRIVATE
LRU
LFU
perfect
)
68 changes: 68 additions & 0 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "LFU.hpp"
#include "LRU.hpp"
#include "perfect.hpp"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <utility>

int slow_get_page(int key) { return key; }

std::vector<int> generate_rnd_keys(size_t n, int mod = 100) {
std::vector<int> keys(n);
std::generate(keys.begin(), keys.end(),
[&]() { return static_cast<int>(rand() % mod); });
return keys;
}

int main() {
size_t input_sz = 100'000;
int rnd_mod = 100;
std::vector<int> keys = generate_rnd_keys(input_sz, rnd_mod);

std::map<size_t, std::map<std::string, int>> cache_sizes_vs_strategy_hits;
std::cout << "Cache type\tN hits\n";
for (size_t cache_sz = 10; cache_sz < 101; cache_sz += 10) {
std::map<std::string, int> cache_hits;
cache::perfect_t<int> perfect_cache(cache_sz, keys);
cache::LRU_t<int> lru{cache_sz};
cache::LFU_t<int> lfu{cache_sz};

for (const auto key : keys) {
if (perfect_cache.lookup_update(key, slow_get_page))
cache_hits["Belady"] += 1;

if (lru.lookup_update(key, slow_get_page))
cache_hits["LRU"] += 1;

if (lfu.lookup_update(key, slow_get_page))
cache_hits["LFU"] += 1;
}
cache_sizes_vs_strategy_hits[cache_sz] = cache_hits;

std::cout << "cache size: " << cache_sz << '\n';
for (auto [cache_type, nhits] : cache_hits) {
std::cout << cache_type << "\t" << nhits << '\n';
}
std::cout << std::endl;
}

// output to a file
std::ofstream file("benchmark/cache_hits.dat");
if (file.is_open()) {
file << "#Number of keys = " << input_sz << "\n";
file << "#input keys: uniform distribution [0:" << rnd_mod << ")\n";
file << "#Cache_size\tLRU\tLFU\tBelady\n";
for (auto &[sz, cache_stats] : cache_sizes_vs_strategy_hits) {
file << sz << '\t' << cache_stats["LRU"] << '\t' << cache_stats["LFU"]
<< '\t' << cache_stats["Belady"] << '\n';
}
file.close();
}

return 0;
}
Binary file added benchmark/cache_hit_statistics.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions benchmark/cache_hits.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#Number of keys = 100000
#input keys: uniform distribution [0:100)
#Cache_size LRU LFU Belady
10 10015 9828 35205
20 19947 19849 51756
30 29887 29907 63218
40 39921 39898 72054
50 49944 49914 79207
60 59924 59805 85109
70 69960 69747 90046
80 79974 79703 94157
90 89939 89828 97468
100 99900 99900 99900
11 changes: 11 additions & 0 deletions benchmark/script.gp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set terminal pngcairo size 800,600 enhanced font "Helvetica,12"
set output 'cache_hit_statistics.png'

set title "Cache statistics for uniform random integers [0:100)"
set xlabel "Cache size"
set ylabel "Cache hits"
set key bottom right

plot "cache_hits.dat" u 1:2 title "LRU",\
"" u 1:3 title "LFU",\
"" u 1:4 title "Belady"\
2 changes: 1 addition & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ int main() {
std::map<std::string, int> cache_hits;
for (const auto key : keys) {
if (perfect_cache.lookup_update(key, slow_get_page))
cache_hits["perfect_cache"] += 1;
cache_hits["Belady"] += 1;

if (lru.lookup_update(key, slow_get_page))
cache_hits["LRU"] += 1;
Expand Down
2 changes: 0 additions & 2 deletions tests/LFU_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ TEST(LFU, eviction_LRU) {

size_t nhits(const std::vector<int> &input) {
size_t cache_sz = input[0];
size_t nelem = input[1];
std::vector<int> page_ids(input.begin() + 2, input.end());
assert(page_ids.size() == nelem);

cache::LFU_t<page_t> lfu{cache_sz};
size_t hits{};
Expand Down
2 changes: 0 additions & 2 deletions tests/LRU_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ TEST(LRU, eviction) {

size_t nhits(const std::vector<int> &input) {
size_t cache_sz = input[0];
size_t nelem = input[1];
std::vector<int> page_ids(input.begin() + 2, input.end());
assert(page_ids.size() == nelem);

cache::LRU_t<page_t> lru{cache_sz};
size_t hits{};
Expand Down
2 changes: 0 additions & 2 deletions tests/perfect_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ TEST(Perfect_cache, eviction_2) {

size_t nhits(const std::vector<int> &input) {
size_t cache_sz = input[0];
size_t nelem = input[1];
std::vector<int> page_ids(input.begin() + 2, input.end());
assert(page_ids.size() == nelem);

cache::perfect_t<page_t> perf_cache{cache_sz, page_ids};
size_t hits{};
Expand Down
Loading