Skip to content

Implement LRU-K eviction algorithm#311

Open
Copilot wants to merge 2 commits intodevelopfrom
copilot/add-skill-for-libcachesim
Open

Implement LRU-K eviction algorithm#311
Copilot wants to merge 2 commits intodevelopfrom
copilot/add-skill-for-libcachesim

Conversation

Copy link
Contributor

Copilot AI commented Mar 24, 2026

Adds a complete implementation of the LRU-K cache eviction algorithm (O'Neil, O'Neil, Weikum, 1993) — the stub file eviction/cpp/LRU_K.cpp existed but was empty.

Algorithm

LRU-K evicts the object with the largest backward K-distance:

  • Objects with < K accesses → infinite backward K-distance, evicted first in FIFO order
  • Objects with ≥ K accesses → evicted by oldest K-th most recent access time

Default k=2, configurable via parameter string:

./cachesim trace.bin oracleGeneral lru-k 1GiB
./cachesim trace.bin oracleGeneral lru-k 1GiB --eviction-params "k=3"

Changes

  • eviction/cpp/LRU_K.cpp — Full implementation using two pools: std::list FIFO queue for objects with < K accesses, std::set<pair<int64_t, obj_id_t>> sorted by K-th access vtime for objects with ≥ K accesses. Per-object access history stored in std::unordered_map<cache_obj_t*, std::deque<int64_t>> within the eviction params struct.
  • include/libCacheSim/evictionAlgo.h — Added LRU_K_init declaration
  • bin/cachesim/cache_init.h — Registered "lru-k" in the simple_algos dispatch table
  • cache/CMakeLists.txt — Added LRU_K.cpp to eviction_sources_cpp
  • test/common.h — Wired LRU_K_init into create_test_cache; removed dead commented-out stub and pre-existing duplicate LFU branch
  • test/test_evictionAlgo.c — Added LRU_K entry to test_data_truth[] and registered test_LRU_K; shifted subsequent array indices accordingly

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

- Add full LRU-K implementation in LRU_K.cpp
  - Objects with < K accesses are evicted in FIFO order (infinite backward K-distance)
  - Objects with >= K accesses are sorted by K-th most recent access time
  - Default K=2, configurable via "k=N" parameter
- Add LRU_K_init declaration to evictionAlgo.h
- Register "lru-k" in cache_init.h simple_algos array
- Add LRU_K.cpp to CMakeLists.txt build
- Update test/common.h to enable LRU_K_init, remove dead code
- Add test data and test function in test_evictionAlgo.c
- All 28 tests pass including new cacheAlgo_LRU_K test

Co-authored-by: 1a1a11a <8584783+1a1a11a@users.noreply.github.com>
Agent-Logs-Url: https://github.com/1a1a11a/libCacheSim/sessions/ba5ad55c-e5e5-4bc1-a3d0-a355843709d2
Copilot AI changed the title [WIP] Add skill for libcachesim Implement LRU-K eviction algorithm Mar 24, 2026
Copilot AI requested a review from 1a1a11a March 24, 2026 01:20
@1a1a11a 1a1a11a marked this pull request as ready for review March 24, 2026 01:23
@1a1a11a 1a1a11a requested a review from haochengxia as a code owner March 24, 2026 01:23
Copilot AI review requested due to automatic review settings March 24, 2026 01:23
@1a1a11a 1a1a11a requested a review from SeverinaZheng March 24, 2026 01:23
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements the LRU-K eviction policy in libCacheSim and wires it into the build, CLI dispatch, and eviction-algorithm test suite.

Changes:

  • Added a full C++ implementation of LRU-K using a FIFO queue for objects with <K accesses and an ordered set for objects with ≥K accesses.
  • Registered the new algorithm in the public eviction API header, cachesim algorithm dispatch, and CMake source lists.
  • Extended the eviction algorithm tests to construct and validate LRU_K.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
libCacheSim/cache/eviction/cpp/LRU_K.cpp New LRU-K eviction implementation (FIFO + ordered set + per-object access history).
libCacheSim/include/libCacheSim/evictionAlgo.h Exposes LRU_K_init in the public eviction algorithm API.
libCacheSim/bin/cachesim/cache_init.h Registers "lru-k" in the cachesim algorithm dispatch table.
libCacheSim/cache/CMakeLists.txt Adds LRU_K.cpp to the C++ eviction sources list for compilation.
test/common.h Wires LRU_K_init into create_test_cache() for the test harness.
test/test_evictionAlgo.c Adds LRU_K truth data entry and registers a test_LRU_K test case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +94 to +101
const char *k_str = strstr(cache_specific_params, "k=");
if (k_str != nullptr) {
int k_val = atoi(k_str + 2);
if (k_val >= 1) {
lruk->k = k_val;
} else {
WARN("LRU_K: invalid k value %d, using default k=2\n", k_val);
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LRU_K_parse_params() uses atoi() but this file does not include <cstdlib>/<stdlib.h>. In C++ this can fail to compile due to atoi being undeclared (it isn’t provided by headers currently included via abstractRank.hpp). Include the proper header (or switch to std::strtol from <cstdlib> for more robust parsing).

Copilot uses AI. Check for mistakes.
Comment on lines +249 to +253
} else {
/* Objects with < K accesses go to the FIFO queue */
lruk->fifo_queue.push_back(obj);
lruk->fifo_map[obj] = std::prev(lruk->fifo_queue.end());
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LRU_K_insert() uses std::prev(...) but <iterator> is not included. std::prev is declared in <iterator> (not guaranteed to be transitively included by <list>), so this can be a compile failure on some standard libraries/toolchains.

Copilot uses AI. Check for mistakes.
}

lruk->history.erase(obj);
cache_remove_obj_base(cache, obj, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use cache_evict_obj_base here?

cache->remove = LRU_K_remove;

if (ccache_params.consider_obj_metadata) {
cache->obj_md_size = 8;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And for the size of metadata, should we consider the value of K here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants