MCPcopy Create free account
hub / github.com/1a1a11a/libCacheSim / runSimulation

Function runSimulation

libCacheSim-node/binding.cc:58–180  ·  view source on GitHub ↗

Main simulation function

Source from the content-addressed store, hash-verified

56
57// Main simulation function
58Napi::Value runSimulation(const Napi::CallbackInfo& info) {
59 Napi::Env env = info.Env();
60
61 // Check arguments
62 if (info.Length() < 3) {
63 Napi::TypeError::New(env, "Expected at least 3 arguments: tracePath, traceType, algorithm").ThrowAsJavaScriptException();
64 return env.Null();
65 }
66
67 if (!info[0].IsString() || !info[1].IsString() || !info[2].IsString()) {
68 Napi::TypeError::New(env, "First three arguments must be strings").ThrowAsJavaScriptException();
69 return env.Null();
70 }
71
72 std::string tracePath = info[0].As<Napi::String>().Utf8Value();
73 std::string traceType = info[1].As<Napi::String>().Utf8Value();
74 std::string algorithm = info[2].As<Napi::String>().Utf8Value();
75
76 // Check if file exists before trying to open it
77 if (!fileExists(tracePath)) {
78 Napi::Error::New(env, "Trace file does not exist: " + tracePath).ThrowAsJavaScriptException();
79 return env.Null();
80 }
81
82 // Parse optional cache size (default 1MB)
83 uint64_t cacheSize = 1024 * 1024; // 1MB default
84 if (info.Length() > 3 && info[3].IsString()) {
85 try {
86 cacheSize = parseCacheSize(info[3].As<Napi::String>().Utf8Value());
87 if (cacheSize == 0) {
88 Napi::Error::New(env, "Invalid cache size").ThrowAsJavaScriptException();
89 return env.Null();
90 }
91 } catch (const std::exception& e) {
92 Napi::Error::New(env, "Invalid cache size format").ThrowAsJavaScriptException();
93 return env.Null();
94 }
95 }
96
97 // Determine trace type enum
98 trace_type_e trace_type_enum;
99 std::string lowerTraceType = traceType;
100 std::transform(lowerTraceType.begin(), lowerTraceType.end(), lowerTraceType.begin(), ::tolower);
101
102 if (lowerTraceType == "vscsi") trace_type_enum = VSCSI_TRACE;
103 else if (lowerTraceType == "csv") trace_type_enum = CSV_TRACE;
104 else if (lowerTraceType == "txt" || lowerTraceType == "plain_txt") trace_type_enum = PLAIN_TXT_TRACE;
105 else if (lowerTraceType == "binary" || lowerTraceType == "bin") trace_type_enum = BIN_TRACE;
106 else if (lowerTraceType == "oracle") trace_type_enum = ORACLE_GENERAL_TRACE;
107 else {
108 Napi::Error::New(env, "Unsupported trace type. Supported: vscsi, csv, txt, binary, oracle").ThrowAsJavaScriptException();
109 return env.Null();
110 }
111
112 // Validate algorithm before creating cache
113 std::string lowerAlgo = algorithm;
114 std::transform(lowerAlgo.begin(), lowerAlgo.end(), lowerAlgo.begin(), ::tolower);
115 if (lowerAlgo != "lru" && lowerAlgo != "fifo" && lowerAlgo != "lfu" &&

Callers

nothing calls this directly

Calls 11

fileExistsFunction · 0.85
parseCacheSizeFunction · 0.85
open_traceFunction · 0.85
new_requestFunction · 0.85
close_traceFunction · 0.85
createCacheFunction · 0.85
free_requestFunction · 0.85
read_one_reqFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected