Skip to content

Commit 73f2a69

Browse files
adds: full code evolve simplified interface
1 parent 6d3d368 commit 73f2a69

7 files changed

Lines changed: 205 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ sftp-config.json
1919
# Clangd cache
2020
*.cache/
2121
.lint-logs/
22+
23+
libCacheSim/cache/eviction/FullCodeEvolve/LLMCode.hpp

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ file(GLOB cache_source
262262

263263
${PROJECT_SOURCE_DIR}/libCacheSim/cache/eviction/LHD/*
264264
${PROJECT_SOURCE_DIR}/libCacheSim/cache/eviction/cpp/*
265+
${PROJECT_SOURCE_DIR}/libCacheSim/cache/eviction/FullCodeEvolve/*.cpp
265266
)
266267

267268
if(EXISTS ${PROJECT_SOURCE_DIR}/libCacheSim/cache/eviction/fifo)

libCacheSim/bin/cachesim/cache_init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static inline cache_t *create_cache(const char *trace_path, const char *eviction
2727

2828
if (strcasecmp(eviction_algo, "lru") == 0) {
2929
cache = LRU_init(cc_params, eviction_params);
30+
} else if (strcasecmp(eviction_algo, "fullcodeevolve") == 0) {
31+
cache = FullCodeEvolve_init(cc_params, eviction_params);
3032
} else if (strcasecmp(eviction_algo, "fifo") == 0) {
3133
cache = FIFO_init(cc_params, eviction_params);
3234
} else if (strcasecmp(eviction_algo, "arc") == 0) {

libCacheSim/cache/eviction/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ set(sourceCPP
8383
cpp/GDSF.cpp
8484
LHD/lhd.cpp
8585
LHD/LHD_Interface.cpp
86+
FullCodeEvolve/FullCodeEvolveInterface.cpp
8687
)
8788

8889
if (ENABLE_LRB)
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include "../../../dataStructure/hashtable/hashtable.h"
2+
#include "../../../include/libCacheSim/evictionAlgo.h"
3+
4+
// Include the generated logic
5+
#include "LLMCode.hpp"
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
typedef struct {
12+
CacheManager* manager;
13+
} FullCodeEvolve_params_t;
14+
15+
// function declarations
16+
static void FullCodeEvolve_free(cache_t *cache);
17+
static bool FullCodeEvolve_get(cache_t *cache, const request_t *req);
18+
static cache_obj_t *FullCodeEvolve_find(cache_t *cache, const request_t *req, const bool update_cache);
19+
static cache_obj_t *FullCodeEvolve_insert(cache_t *cache, const request_t *req);
20+
static cache_obj_t *FullCodeEvolve_to_evict(cache_t *cache, const request_t *req);
21+
static void FullCodeEvolve_evict(cache_t *cache, const request_t *req);
22+
static bool FullCodeEvolve_remove(cache_t *cache, const obj_id_t obj_id);
23+
static void FullCodeEvolve_print_cache(const cache_t *cache);
24+
25+
// ***********************************************************************
26+
// **** end user facing functions ****
27+
// **** init, free, get ****
28+
// ***********************************************************************
29+
/**
30+
* @brief initialize a FullCodeEvolve cache
31+
*
32+
* @param ccache_params some common cache parameters
33+
* @param cache_specific_params FullCodeEvolve specific parameters, should be NULL
34+
*/
35+
cache_t *FullCodeEvolve_init(const common_cache_params_t ccache_params,
36+
const char *cache_specific_params) {
37+
cache_t *cache =
38+
cache_struct_init("FullCodeEvolve", ccache_params, cache_specific_params);
39+
cache->cache_init = FullCodeEvolve_init;
40+
cache->cache_free = FullCodeEvolve_free;
41+
cache->get = cache_get_base;
42+
cache->find = FullCodeEvolve_find;
43+
cache->insert = FullCodeEvolve_insert;
44+
cache->evict = FullCodeEvolve_evict;
45+
cache->remove = FullCodeEvolve_remove;
46+
cache->to_evict = FullCodeEvolve_to_evict;
47+
cache->get_occupied_byte = cache_get_occupied_byte_default;
48+
cache->can_insert = cache_can_insert_default;
49+
cache->get_n_obj = cache_get_n_obj_default;
50+
cache->print_cache = FullCodeEvolve_print_cache;
51+
52+
if (ccache_params.consider_obj_metadata) {
53+
cache->obj_md_size = 8 * 2;
54+
} else {
55+
cache->obj_md_size = 0;
56+
}
57+
58+
FullCodeEvolve_params_t* params = (FullCodeEvolve_params_t*) malloc(sizeof(FullCodeEvolve_params_t));
59+
params->manager = new CacheManager();
60+
cache->eviction_params = params;
61+
return cache;
62+
}
63+
64+
// ***********************************************************************
65+
// **** developer facing APIs (used by cache developer) ****
66+
// ***********************************************************************
67+
68+
/**
69+
* @brief check whether an object is in the cache
70+
*
71+
* @param cache
72+
* @param req
73+
* @param update_cache whether to update the cache,
74+
* if true, the object is promoted
75+
* and if the object is expired, it is removed from the cache
76+
* @return true on hit, false on miss
77+
*/
78+
static cache_obj_t *FullCodeEvolve_find(cache_t *cache, const request_t *req,
79+
const bool update_cache) {
80+
assert(update_cache);
81+
FullCodeEvolve_params_t *params = (FullCodeEvolve_params_t *)cache->eviction_params;
82+
cache_obj_t *cache_obj = cache_find_base(cache, req, update_cache);
83+
84+
bool cache_obj_LLM = params->manager->find(req->obj_id);
85+
86+
// correctness checks: either both reference cache+LLM Cache (a) find object, or (b) miss object.
87+
if(cache_obj) assert(cache_obj_LLM);
88+
else assert(!cache_obj_LLM);
89+
90+
return cache_obj;
91+
}
92+
93+
/**
94+
* @brief insert an object into the cache,
95+
* update the hash table and cache metadata
96+
* this function assumes the cache has enough space
97+
* and eviction is not part of this function
98+
*
99+
* @param cache
100+
* @param req
101+
* @return the inserted object
102+
*/
103+
static cache_obj_t *FullCodeEvolve_insert(cache_t *cache, const request_t *req) {
104+
FullCodeEvolve_params_t *params = (FullCodeEvolve_params_t *)cache->eviction_params;
105+
106+
cache_obj_t *obj = cache_insert_base(cache, req);
107+
params->manager->insert(req->obj_id);
108+
109+
return obj;
110+
}
111+
112+
/**
113+
* @brief evict an object from the cache
114+
* it needs to call cache_evict_base before returning
115+
* which updates some metadata such as n_obj, occupied size, and hash table
116+
*
117+
* @param cache
118+
* @param req not used
119+
*/
120+
static void FullCodeEvolve_evict(cache_t *cache, const request_t *req) {
121+
FullCodeEvolve_params_t *params = (FullCodeEvolve_params_t *)cache->eviction_params;
122+
obj_id_t victim_id = params->manager->evict();
123+
124+
request_t fake_req = {};
125+
fake_req.obj_id = victim_id;
126+
127+
cache_obj_t* victim = cache_find_base(cache, &fake_req, false);
128+
if(victim){
129+
cache_evict_base(cache, victim, true);
130+
}
131+
else assert(false);
132+
}
133+
134+
static void FullCodeEvolve_free(cache_t *cache) {
135+
FullCodeEvolve_params_t *params = (FullCodeEvolve_params_t *)cache->eviction_params;
136+
137+
delete params->manager;
138+
free(params);
139+
cache->eviction_params = nullptr;
140+
141+
cache_struct_free(cache);
142+
}
143+
144+
145+
static void FullCodeEvolve_remove_obj(cache_t *cache, cache_obj_t *obj) {
146+
assert(false);
147+
}
148+
149+
static bool FullCodeEvolve_remove(cache_t *cache, const obj_id_t obj_id) {
150+
assert(false);
151+
}
152+
153+
static void FullCodeEvolve_print_cache(const cache_t *cache) {
154+
assert(false);
155+
}
156+
157+
static cache_obj_t *FullCodeEvolve_to_evict(cache_t *cache, const request_t *req) {
158+
assert(false);
159+
return new cache_obj_t();
160+
}
161+
162+
#ifdef __cplusplus
163+
}
164+
#endif
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <bits/stdc++.h>
2+
typedef uint64_t obj_id_t;
3+
4+
class CacheManager {
5+
private:
6+
std::list<obj_id_t> lru_list;
7+
std::unordered_map<obj_id_t, std::list<obj_id_t>::iterator> map;
8+
9+
public:
10+
bool find(obj_id_t obj_id) {
11+
auto it = map.find(obj_id);
12+
if (it == map.end()) return false;
13+
lru_list.erase(it->second);
14+
lru_list.push_front(obj_id);
15+
map[obj_id] = lru_list.begin();
16+
return true;
17+
}
18+
19+
void insert(obj_id_t obj_id) {
20+
if (map.count(obj_id)) {
21+
lru_list.erase(map[obj_id]);
22+
}
23+
lru_list.push_front(obj_id);
24+
map[obj_id] = lru_list.begin();
25+
}
26+
27+
obj_id_t evict() {
28+
obj_id_t victim = lru_list.back();
29+
lru_list.pop_back();
30+
map.erase(victim);
31+
return victim;
32+
}
33+
};

libCacheSim/include/libCacheSim/evictionAlgo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ cache_t *CAR_init(const common_cache_params_t ccache_params, const char *cache_s
134134

135135
cache_t *ThreeLCache_init(const common_cache_params_t ccache_params, const char *cache_specific_params);
136136

137+
cache_t *FullCodeEvolve_init(const common_cache_params_t ccache_params, const char *cache_specific_params);
138+
137139
#ifdef ENABLE_LRB
138140
cache_t *LRB_init(const common_cache_params_t ccache_params, const char *cache_specific_params);
139141
#endif

0 commit comments

Comments
 (0)