Skip to content

Commit 4c5fb5a

Browse files
committed
add plugin_v2
1 parent 4950404 commit 4c5fb5a

11 files changed

Lines changed: 885 additions & 30 deletions

File tree

example/plugin_v2/CMakeLists.txt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
3+
# Set the project name
4+
project(plugin_lru_hooks)
5+
6+
# Set C++ standard
7+
set(CMAKE_CXX_STANDARD 17)
8+
set(CMAKE_CXX_STANDARD_REQUIRED On)
9+
set(CMAKE_CXX_EXTENSIONS Off)
10+
11+
# Enable position independent code for shared libraries
12+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
13+
14+
# Find required dependencies
15+
find_package(PkgConfig REQUIRED)
16+
pkg_check_modules(GLIB REQUIRED glib-2.0)
17+
message(STATUS "GLIB library: ${GLIB_LIBRARIES}")
18+
message(STATUS "GLIB include: ${GLIB_INCLUDE_DIRS}")
19+
20+
# Find libCacheSim
21+
find_library(LIBCACHESIM_LIBRARY
22+
NAMES CacheSim libCacheSim
23+
PATHS /usr/local/lib /usr/lib
24+
DOC "libCacheSim unified library"
25+
)
26+
27+
find_path(LIBCACHESIM_INCLUDE_DIR
28+
NAMES libCacheSim.h
29+
PATHS /usr/local/include /usr/include
30+
DOC "libCacheSim include directory"
31+
)
32+
33+
message(STATUS "libCacheSim library: ${LIBCACHESIM_LIBRARY}")
34+
message(STATUS "libCacheSim include: ${LIBCACHESIM_INCLUDE_DIR}")
35+
36+
if(NOT LIBCACHESIM_LIBRARY OR NOT LIBCACHESIM_INCLUDE_DIR)
37+
message(FATAL_ERROR "libCacheSim not found! Please install libCacheSim first.")
38+
endif()
39+
40+
# Add the shared library target
41+
add_library(plugin_lru_hooks SHARED plugin_lru.cpp)
42+
43+
# Set compiler flags
44+
target_compile_options(plugin_lru_hooks PRIVATE
45+
-Wall -Wextra
46+
-Wno-unused-variable -Wno-unused-function -Wno-unused-parameter
47+
)
48+
target_include_directories(plugin_lru_hooks PRIVATE
49+
${GLIB_INCLUDE_DIRS}
50+
${LIBCACHESIM_INCLUDE_DIR}
51+
)
52+
# Link libraries
53+
target_link_libraries(plugin_lru_hooks
54+
${GLIB_LIBRARIES}
55+
${LIBCACHESIM_LIBRARY}
56+
)
57+
58+
# Set library properties
59+
# set_target_properties(plugin_lru_hooks PROPERTIES
60+
# VERSION 1.0.0
61+
# SOVERSION 1
62+
# OUTPUT_NAME "plugin_lru_hooks"
63+
# )
64+
65+
# Optional: Add test executable if test file exists
66+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test_hooks_plugin.c")
67+
add_executable(test_hooks_plugin test_hooks_plugin.c)
68+
target_include_directories(test_hooks_plugin PRIVATE
69+
${LIBCACHESIM_INCLUDE_DIR}
70+
${GLIB_INCLUDE_DIRS}
71+
)
72+
target_link_libraries(test_hooks_plugin
73+
${GLIB_LIBRARIES}
74+
${LIBCACHESIM_LIBRARY}
75+
dl
76+
m
77+
)
78+
endif()

example/plugin_v2/README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Plugin LRU Hooks Example
2+
3+
This example demonstrates how to create a plugin for libCacheSim using the hook-based system implemented in `plugin_cache.c`.
4+
5+
## Files
6+
7+
- `plugin_lru_hooks.cpp` - The main LRU cache plugin implementation with hooks
8+
- `plugin_func.cpp` - Additional plugin functions
9+
- `test_hooks_plugin.c` - Test program for the plugin
10+
- `CMakeLists.txt` - Build configuration for creating a shared library
11+
12+
## Building
13+
14+
To compile the plugin into a shared library:
15+
16+
```bash
17+
mkdir build
18+
cd build
19+
cmake ..
20+
make
21+
```
22+
23+
This will create:
24+
- `libplugin_lru_hooks.so` - The shared library containing the plugin
25+
- `test_hooks_plugin` - A test executable (if test file exists)
26+
27+
## Plugin Interface
28+
29+
The plugin implements the following hook functions expected by libCacheSim's plugin system:
30+
31+
- `cache_init_hook()` - Initialize the cache data structure
32+
- `cache_hit_hook()` - Handle cache hits (move to head of LRU list)
33+
- `cache_miss_hook()` - Handle cache misses (insert new object)
34+
- `cache_eviction_hook()` - Evict least recently used object
35+
- `cache_remove_hook()` - Remove specific object from cache
36+
37+
## Usage
38+
39+
40+
```
41+
./bin/cachesim ../data/cloudPhysicsIO.vscsi vscsi lru,pluginCache 0.01,0.1 -e "plugin=/proj/cache-PG0/jason/libCacheSim/example/pluginv2/_build/libplugin_lru_hooks.so.1.0.0"
42+
```
43+
44+
## Dependencies
45+
46+
- libCacheSim headers
47+
- GLib (for basic data types)
48+
- C++17 compiler
49+
- CMake 3.12 or higher

example/plugin_v2/plugin_lru.cpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
//
2+
// LRU Cache Plugin with Hooks for libCacheSim plugin_cache.c
3+
// This implements the hook-based plugin system that plugin_cache.c expects
4+
//
5+
6+
#include <glib.h>
7+
#include <libCacheSim.h>
8+
#include <stdbool.h>
9+
#include <stdint.h>
10+
#include <stdlib.h>
11+
12+
#include <unordered_map>
13+
14+
class StandaloneLRU {
15+
public:
16+
struct Node {
17+
obj_id_t obj_id;
18+
uint64_t obj_size;
19+
Node *prev;
20+
Node *next;
21+
22+
Node(obj_id_t k = 0, uint64_t obj_size = 0)
23+
: obj_id(k), obj_size(obj_size), prev(nullptr), next(nullptr) {}
24+
};
25+
26+
std::unordered_map<obj_id_t, Node *> cache_map;
27+
Node *head;
28+
Node *tail;
29+
30+
StandaloneLRU() {
31+
head = new Node();
32+
tail = new Node();
33+
head->next = tail;
34+
tail->prev = head;
35+
}
36+
37+
~StandaloneLRU() {
38+
while (head) {
39+
Node *temp = head;
40+
head = head->next;
41+
delete temp;
42+
}
43+
}
44+
45+
void add_to_head(Node *node) {
46+
node->prev = head;
47+
node->next = head->next;
48+
head->next->prev = node;
49+
head->next = node;
50+
}
51+
52+
void remove_node(Node *node) {
53+
node->prev->next = node->next;
54+
node->next->prev = node->prev;
55+
}
56+
57+
Node *remove_tail() {
58+
Node *last_node = tail->prev;
59+
remove_node(last_node);
60+
return last_node;
61+
}
62+
63+
void move_to_head(Node *node) {
64+
remove_node(node);
65+
add_to_head(node);
66+
}
67+
68+
void cache_hit(obj_id_t obj_id) {
69+
Node *node = cache_map[obj_id];
70+
move_to_head(node);
71+
}
72+
73+
void cache_miss(obj_id_t obj_id, uint64_t obj_size) {
74+
Node *new_node = new Node(obj_id, obj_size);
75+
cache_map[obj_id] = new_node;
76+
add_to_head(new_node);
77+
}
78+
79+
obj_id_t cache_eviction() {
80+
Node *node = remove_tail();
81+
obj_id_t evicted_id = node->obj_id;
82+
cache_map.erase(evicted_id);
83+
delete node;
84+
return evicted_id;
85+
}
86+
87+
void cache_remove(obj_id_t obj_id) {
88+
auto it = cache_map.find(obj_id);
89+
if (it == cache_map.end()) {
90+
return;
91+
}
92+
Node *node = it->second;
93+
remove_node(node);
94+
cache_map.erase(it);
95+
delete node;
96+
}
97+
};
98+
99+
// C interface for the plugin hooks
100+
extern "C" {
101+
102+
// implement the cache init hook
103+
void *cache_init_hook(const common_cache_params_t ccache_params) {
104+
// initialize the LRU cache
105+
StandaloneLRU *lru_cache = new StandaloneLRU();
106+
return lru_cache;
107+
}
108+
109+
// implement the cache hit hook
110+
void cache_hit_hook(void *data, const request_t *req) {
111+
// move object to the head of the list
112+
StandaloneLRU *lru_cache = (StandaloneLRU *)data;
113+
lru_cache->cache_hit(req->obj_id);
114+
}
115+
116+
// implement the cache miss hook
117+
void cache_miss_hook(void *data, const request_t *req) {
118+
// insert object into the cache
119+
StandaloneLRU *lru_cache = (StandaloneLRU *)data;
120+
lru_cache->cache_miss(req->obj_id, req->obj_size);
121+
}
122+
123+
// implement the cache eviction hook
124+
obj_id_t cache_eviction_hook(void *data, const request_t *req) {
125+
// evict the least recently used object
126+
StandaloneLRU *lru_cache = (StandaloneLRU *)data;
127+
return lru_cache->cache_eviction();
128+
}
129+
130+
// implement the cache remove hook
131+
void cache_remove_hook(void *data, const obj_id_t obj_id) {
132+
// remove object from the cache
133+
StandaloneLRU *lru_cache = (StandaloneLRU *)data;
134+
lru_cache->cache_remove(obj_id);
135+
}
136+
137+
} // extern "C"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <assert.h>
2+
#include <libCacheSim.h>
3+
#include <libgen.h>
4+
#include <stdbool.h>
5+
#include <stdint.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
#include "libCacheSim/cache.h"
11+
#include "libCacheSim/evictionAlgo.h"
12+
13+
static const char *plugin_lib_name = "libplugin_lru_hooks.so";
14+
15+
int main(int argc, char *argv[]) {
16+
common_cache_params_t cache_params = {
17+
.cache_size = 1000, // Small cache for easy testing
18+
.default_ttl = 0,
19+
.hashpower = 16,
20+
.consider_obj_metadata = false,
21+
};
22+
23+
// create a plugin LRU cache
24+
char *curr_bin_path = strdup(argv[0]);
25+
char *curr_dir = dirname(curr_bin_path);
26+
char *plugin_path = malloc(strlen(curr_dir) + strlen(plugin_lib_name) + 20);
27+
sprintf(plugin_path, "plugin_path=%s/%s", curr_dir, plugin_lib_name);
28+
printf("plugin_path: %s\n", plugin_path);
29+
cache_t *plugin_cache = pluginCache_init(cache_params, plugin_path);
30+
assert(plugin_cache != NULL);
31+
32+
request_t *req = new_request();
33+
assert(req != NULL);
34+
35+
// create a LRU cache
36+
cache_t *lru_cache = LRU_init(cache_params, NULL);
37+
assert(lru_cache != NULL);
38+
39+
// generate a 1000 random requests and compare the result of the plugin LRU
40+
// cache and the default LRU cache
41+
for (int i = 0; i < 1000; i++) {
42+
req->obj_id = rand() % 1000;
43+
req->obj_size = rand() % 10;
44+
req->clock_time = i;
45+
46+
bool hit1 = plugin_cache->get(plugin_cache, req);
47+
bool hit2 = lru_cache->get(lru_cache, req);
48+
assert(hit1 == hit2);
49+
}
50+
51+
// free the request
52+
free_request(req);
53+
plugin_cache->cache_free(plugin_cache);
54+
lru_cache->cache_free(lru_cache);
55+
printf("Plugin LRU cache and LRU cache are the same\n\n");
56+
return 0;
57+
}

libCacheSim/bin/cachesim/cache_init.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ static inline cache_t *create_cache(const char *trace_path,
5656
{"lru", LRU_init},
5757
{"lru-prob", LRU_Prob_init},
5858
{"nop", nop_init},
59+
// plugin cache that allows user to implement custom cache
60+
{"pluginCache", pluginCache_init},
5961
{"qdlp", QDLP_init},
6062
{"random", Random_init},
6163
{"RandomLRU", RandomLRU_init},

libCacheSim/bin/cachesim/cli_parser.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
117117
case OPTION_EVICTION_PARAMS:
118118
arguments->eviction_params = strdup(arg);
119119
replace_char(arguments->eviction_params, ';', ',');
120-
replace_char(arguments->eviction_params, '_', '-');
120+
// replace_char(arguments->eviction_params, '_', '-');
121121
break;
122122
case OPTION_ADMISSION_ALGO:
123123
arguments->admission_algo = arg;
@@ -128,12 +128,12 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
128128
case OPTION_ADMISSION_PARAMS:
129129
arguments->admission_params = strdup(arg);
130130
replace_char(arguments->admission_params, ';', ',');
131-
replace_char(arguments->admission_params, '_', '-');
131+
// replace_char(arguments->admission_params, '_', '-');
132132
break;
133133
case OPTION_PREFETCH_PARAMS:
134134
arguments->prefetch_params = strdup(arg);
135135
replace_char(arguments->prefetch_params, ';', ',');
136-
replace_char(arguments->prefetch_params, '_', '-');
136+
// replace_char(arguments->prefetch_params, '_', '-');
137137
break;
138138
case OPTION_OUTPUT_PATH:
139139
strncpy(arguments->ofilepath, arg, OFILEPATH_LEN - 1);

libCacheSim/cache/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ set(eviction_sources_c
4545
eviction/LRUv0.c # an inefficient version but easier to understand
4646
eviction/MRU.c
4747
eviction/nop.c
48+
eviction/plugin_cache.c # plugin cache that allows user to implement custom cache
4849
eviction/QDLP.c
4950
eviction/Random.c
5051
eviction/RandomLRU.c

0 commit comments

Comments
 (0)