-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathbinding.cc
More file actions
307 lines (265 loc) · 9.44 KB
/
Copy pathbinding.cc
File metadata and controls
307 lines (265 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <napi.h>
#include <sys/stat.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include "libCacheSim.h"
// Helper function to check if file exists
bool fileExists(const std::string& filename) {
struct stat buffer;
return (stat(filename.c_str(), &buffer) == 0);
}
// Helper function to parse cache size string (e.g., "1mb", "1gb", "1024")
uint64_t parseCacheSize(const std::string& sizeStr) {
if (sizeStr.empty()) return 0;
std::string lower = sizeStr;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
// Extract number and unit
size_t pos = 0;
while (pos < lower.length() && (isdigit(lower[pos]) || lower[pos] == '.')) {
pos++;
}
double value = std::stod(lower.substr(0, pos));
std::string unit = lower.substr(pos);
uint64_t multiplier = 1;
if (unit == "kb" || unit == "k")
multiplier = 1024;
else if (unit == "mb" || unit == "m")
multiplier = 1024 * 1024;
else if (unit == "gb" || unit == "g")
multiplier = 1024 * 1024 * 1024;
else if (unit == "tb" || unit == "t")
multiplier = 1024ULL * 1024 * 1024 * 1024;
return (uint64_t)(value * multiplier);
}
// Helper function to get cache constructor by algorithm name
cache_t* createCache(const std::string& algo,
const common_cache_params_t& params) {
std::string lowerAlgo = algo;
std::transform(lowerAlgo.begin(), lowerAlgo.end(), lowerAlgo.begin(),
::tolower);
if (lowerAlgo == "lru")
return LRU_init(params, nullptr);
else if (lowerAlgo == "fifo")
return FIFO_init(params, nullptr);
else if (lowerAlgo == "lfu")
return LFU_init(params, nullptr);
else if (lowerAlgo == "arc")
return ARC_init(params, nullptr);
else if (lowerAlgo == "clock")
return Clock_init(params, nullptr);
else if (lowerAlgo == "s3fifo")
return S3FIFO_init(params, nullptr);
else if (lowerAlgo == "sieve")
return Sieve_init(params, nullptr);
return nullptr; // Unknown algorithm
}
// Main simulation function
Napi::Value runSimulation(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// Check arguments
if (info.Length() < 3) {
Napi::TypeError::New(
env, "Expected at least 3 arguments: tracePath, traceType, algorithm")
.ThrowAsJavaScriptException();
return env.Null();
}
if (!info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
Napi::TypeError::New(env, "First three arguments must be strings")
.ThrowAsJavaScriptException();
return env.Null();
}
std::string tracePath = info[0].As<Napi::String>().Utf8Value();
std::string traceType = info[1].As<Napi::String>().Utf8Value();
std::string algorithm = info[2].As<Napi::String>().Utf8Value();
// Check if file exists before trying to open it
if (!fileExists(tracePath)) {
Napi::Error::New(env, "Trace file does not exist: " + tracePath)
.ThrowAsJavaScriptException();
return env.Null();
}
// Parse optional cache size (default 1MB)
uint64_t cacheSize = 1024 * 1024; // 1MB default
if (info.Length() > 3 && info[3].IsString()) {
try {
cacheSize = parseCacheSize(info[3].As<Napi::String>().Utf8Value());
if (cacheSize == 0) {
Napi::Error::New(env, "Invalid cache size")
.ThrowAsJavaScriptException();
return env.Null();
}
} catch (const std::exception& e) {
Napi::Error::New(env, "Invalid cache size format")
.ThrowAsJavaScriptException();
return env.Null();
}
}
// Determine trace type enum
trace_type_e trace_type_enum;
std::string lowerTraceType = traceType;
std::transform(lowerTraceType.begin(), lowerTraceType.end(),
lowerTraceType.begin(), ::tolower);
if (lowerTraceType == "vscsi")
trace_type_enum = VSCSI_TRACE;
else if (lowerTraceType == "csv")
trace_type_enum = CSV_TRACE;
else if (lowerTraceType == "txt" || lowerTraceType == "plain_txt")
trace_type_enum = PLAIN_TXT_TRACE;
else if (lowerTraceType == "binary" || lowerTraceType == "bin")
trace_type_enum = BIN_TRACE;
else if (lowerTraceType == "oracle")
trace_type_enum = ORACLE_GENERAL_TRACE;
else {
Napi::Error::New(
env,
"Unsupported trace type. Supported: vscsi, csv, txt, binary, oracle")
.ThrowAsJavaScriptException();
return env.Null();
}
// Validate algorithm before creating cache
std::string lowerAlgo = algorithm;
std::transform(lowerAlgo.begin(), lowerAlgo.end(), lowerAlgo.begin(),
::tolower);
if (lowerAlgo != "lru" && lowerAlgo != "fifo" && lowerAlgo != "lfu" &&
lowerAlgo != "arc" && lowerAlgo != "clock" && lowerAlgo != "s3fifo" &&
lowerAlgo != "sieve") {
Napi::Error::New(env,
"Unsupported algorithm. Supported: lru, fifo, lfu, arc, "
"clock, s3fifo, sieve")
.ThrowAsJavaScriptException();
return env.Null();
}
// Open the trace file
reader_t* reader = open_trace(tracePath.c_str(), trace_type_enum, nullptr);
if (!reader) {
Napi::Error::New(env, "Failed to open trace file: " + tracePath)
.ThrowAsJavaScriptException();
return env.Null();
}
// Create a request container
request_t* req = new_request();
if (!req) {
close_trace(reader);
Napi::Error::New(env, "Failed to allocate request")
.ThrowAsJavaScriptException();
return env.Null();
}
// Initialize cache
common_cache_params_t cc_params = {.cache_size = cacheSize,
.default_ttl = 0,
.hashpower = 24,
.consider_obj_metadata = false};
cache_t* cache = createCache(algorithm, cc_params);
if (!cache) {
close_trace(reader);
free_request(req);
Napi::Error::New(env, "Failed to create cache with algorithm: " + algorithm)
.ThrowAsJavaScriptException();
return env.Null();
}
// Run simulation loop
uint64_t n_req = 0;
uint64_t n_miss = 0;
uint64_t n_hit = 0;
while (read_one_req(reader, req) == 0) {
bool hit = cache->get(cache, req);
if (hit)
n_hit++;
else
n_miss++;
n_req++;
}
// Cleanup
close_trace(reader);
free_request(req);
cache->cache_free(cache);
// Return simulation results as object
Napi::Object result = Napi::Object::New(env);
result.Set("totalRequests", Napi::Number::New(env, n_req));
result.Set("hits", Napi::Number::New(env, n_hit));
result.Set("misses", Napi::Number::New(env, n_miss));
result.Set("hitRatio",
Napi::Number::New(env, n_req > 0 ? (double)n_hit / n_req : 0.0));
result.Set("missRatio",
Napi::Number::New(env, n_req > 0 ? (double)n_miss / n_req : 0.0));
result.Set("algorithm", Napi::String::New(env, algorithm));
result.Set("cacheSize", Napi::Number::New(env, cacheSize));
return result;
}
// Simple simulation with hardcoded values (backward compatibility)
Napi::Value runSim(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
// Check if the default trace file exists
if (!fileExists("../data/cloudPhysicsIO.vscsi")) {
Napi::Error::New(
env, "Default trace file not found: ../data/cloudPhysicsIO.vscsi")
.ThrowAsJavaScriptException();
return env.Null();
}
// === Open the trace file ===
reader_t* reader = open_trace("../data/cloudPhysicsIO.vscsi", VSCSI_TRACE,
nullptr // No special initialization parameters
);
if (!reader) {
Napi::Error::New(env, "Failed to open trace").ThrowAsJavaScriptException();
return env.Null();
}
// === Create a request container ===
request_t* req = new_request();
if (!req) {
close_trace(reader);
Napi::Error::New(env, "Failed to allocate request")
.ThrowAsJavaScriptException();
return env.Null();
}
// === Initialize an LRU cache ===
common_cache_params_t cc_params = {.cache_size = 1024 * 1024, // 1MB
.default_ttl = 0,
.hashpower = 24,
.consider_obj_metadata = false};
cache_t* cache = LRU_init(cc_params, nullptr);
if (!cache) {
close_trace(reader);
free_request(req);
Napi::Error::New(env, "Failed to create cache")
.ThrowAsJavaScriptException();
return env.Null();
}
// === Run simulation loop ===
uint64_t n_req = 0;
uint64_t n_miss = 0;
uint64_t n_hit = 0;
while (read_one_req(reader, req) == 0) {
bool hit = cache->get(cache, req);
if (hit)
n_hit++;
else
n_miss++;
n_req++;
}
// === Cleanup ===
close_trace(reader);
free_request(req);
cache->cache_free(cache);
// === Return results as object ===
Napi::Object result = Napi::Object::New(env);
result.Set("totalRequests", Napi::Number::New(env, n_req));
result.Set("hits", Napi::Number::New(env, n_hit));
result.Set("misses", Napi::Number::New(env, n_miss));
result.Set("hitRatio",
Napi::Number::New(env, n_req > 0 ? (double)n_hit / n_req : 0.0));
result.Set("missRatio",
Napi::Number::New(env, n_req > 0 ? (double)n_miss / n_req : 0.0));
result.Set("algorithm", Napi::String::New(env, "lru"));
result.Set("cacheSize", Napi::Number::New(env, 1024 * 1024));
return result;
}
// Node.js addon initialization
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set("runSim", Napi::Function::New(env, runSim));
exports.Set("runSimulation", Napi::Function::New(env, runSimulation));
return exports;
}
NODE_API_MODULE(libcachesim_addon, Init)