Skip to content

Commit 0a3ac61

Browse files
authored
Vm: remove aot cache (eunomia-bpf#227)
1 parent 2d200c1 commit 0a3ac61

File tree

2 files changed

+1
-130
lines changed

2 files changed

+1
-130
lines changed

vm/llvm-jit/CMakeLists.txt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@ if(BPFTIME_ENABLE_UNIT_TESTING)
8787
add_subdirectory(unit-test)
8888
endif()
8989

90-
include(FetchContent)
91-
FetchContent_Declare(
92-
picosha2
93-
GIT_REPOSITORY https://github.com/okdshin/PicoSHA2
94-
GIT_TAG 27fcf69
95-
)
96-
FetchContent_MakeAvailable(picosha2)
97-
FetchContent_GetProperties(picosha2)
98-
99-
# get_target_property(picosha2_INCLUDE picosha2 INTERFACE_INCLUDE_DIRECTORIES)
100-
set(picosha2_INCLUDE ${picosha2_SOURCE_DIR})
101-
target_include_directories(vm-bpf PRIVATE ${picosha2_INCLUDE})
102-
add_dependencies(vm-bpf picosha2)
103-
10490
find_package(Boost REQUIRED)
10591

10692
target_include_directories(vm-bpf PRIVATE ${Boost_INCLUDE})

vm/llvm-jit/src/llvm/llvm_jit_context.cpp

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <string>
3535
#include <spdlog/spdlog.h>
3636
#include <tuple>
37-
#include <picosha2.h>
3837
#include <boost/interprocess/sync/file_lock.hpp>
3938
#include <boost/interprocess/sync/scoped_lock.hpp>
4039

@@ -97,125 +96,11 @@ void llvm_bpf_jit_context::do_jit_compile()
9796
this->jit = std::move(jit);
9897
}
9998

100-
static std::string hash_ebpf_program(const char *bytes, size_t size)
101-
{
102-
picosha2::hash256_one_by_one hasher;
103-
hasher.process(bytes, bytes + size);
104-
hasher.finish();
105-
std::vector<unsigned char> hash(picosha2::k_digest_size);
106-
hasher.get_hash_bytes(hash.begin(), hash.end());
107-
108-
return picosha2::get_hash_hex_string(hasher);
109-
}
110-
111-
static std::pair<std::filesystem::path, std::filesystem::path>
112-
ensure_aot_cache_dir_and_cache_file()
113-
{
114-
const char *home_dir = getenv("HOME");
115-
if (home_dir) {
116-
SPDLOG_DEBUG("Use `{}` as home directory", home_dir);
117-
} else {
118-
home_dir = ".";
119-
SPDLOG_DEBUG("Home dir not found, using working directory");
120-
}
121-
auto dir = std::filesystem::path(home_dir) / ".bpftime" / "aot-cache";
122-
if (!std::filesystem::exists(dir)) {
123-
std::error_code ec;
124-
if (!std::filesystem::create_directories(dir, ec)) {
125-
SPDLOG_CRITICAL(
126-
"Unable to create AOT cache directory: {}",
127-
ec.value());
128-
throw std::runtime_error("Unable to create aot cache");
129-
}
130-
}
131-
auto cache_lock = dir / "lock";
132-
struct stat st;
133-
int err;
134-
if (stat(cache_lock.c_str(), &st) == -1) {
135-
int err = errno;
136-
if (errno != ENOENT) {
137-
SPDLOG_CRITICAL(
138-
"Unable to detect the existence of {}, err={}",
139-
cache_lock.c_str(), errno);
140-
throw std::runtime_error("Failed to check");
141-
} else {
142-
std::ofstream ofs(cache_lock);
143-
}
144-
}
145-
146-
return { dir, cache_lock };
147-
}
148-
149-
static std::optional<std::vector<uint8_t> >
150-
load_aot_cache(const std::filesystem::path &path)
151-
{
152-
std::ifstream ifs(path, std::ios::binary | std::ios::ate);
153-
if (!ifs.is_open()) {
154-
SPDLOG_WARN(
155-
"LLVM-JIT: Unable to open aot cache file, fallback to jit");
156-
return {};
157-
} else {
158-
std::streamsize size = ifs.tellg();
159-
ifs.seekg(0, std::ios::beg);
160-
std::vector<uint8_t> buffer(size);
161-
162-
if (!ifs.read((char *)buffer.data(), size)) {
163-
SPDLOG_WARN(
164-
"LLVM-JIT: Unable to read aot cache, fallback to jit");
165-
return {};
166-
}
167-
SPDLOG_INFO("LLVM-JIT: {} bytes of aot cache loaded",
168-
buffer.size());
169-
return buffer;
170-
}
171-
}
172-
17399
ebpf_jit_fn llvm_bpf_jit_context::compile()
174100
{
175101
spin_lock_guard guard(compiling.get());
176102
if (!this->jit.has_value()) {
177-
if (getenv("BPFTIME_ENABLE_AOT")) {
178-
SPDLOG_DEBUG("LLVM-JIT: Entering AOT compilation");
179-
auto ebpf_prog_hash = hash_ebpf_program(
180-
(const char *)vm->insnsi, vm->num_insts * 8);
181-
SPDLOG_INFO("LLVM-JIT: SHA256 of ebpf program: {}",
182-
ebpf_prog_hash);
183-
auto [cache_dir, cache_lock] =
184-
ensure_aot_cache_dir_and_cache_file();
185-
186-
boost::interprocess::file_lock flock(
187-
cache_lock.c_str());
188-
boost::interprocess::scoped_lock<
189-
boost::interprocess::file_lock>
190-
_guard(flock);
191-
SPDLOG_DEBUG("LLVM_JIT: cache lock acquired");
192-
auto cache_file = cache_dir / ebpf_prog_hash;
193-
SPDLOG_DEBUG("LLVM-JIT: cache file is {}",
194-
cache_file.c_str());
195-
if (std::filesystem::exists(cache_file)) {
196-
SPDLOG_INFO(
197-
"LLVM-JIT: Try loading aot cache..");
198-
if (auto opt = load_aot_cache(cache_file);
199-
opt.has_value()) {
200-
this->load_aot_object(*opt);
201-
} else {
202-
SPDLOG_WARN(
203-
"Unable to load aot file, fallback to jit");
204-
do_jit_compile();
205-
}
206-
} else {
207-
SPDLOG_INFO("LLVM-JIT: Creating AOT cache..");
208-
auto cache = do_aot_compile();
209-
std::ofstream ofs(cache_file, std::ios::binary);
210-
ofs.write((const char *)cache.data(),
211-
cache.size());
212-
do_jit_compile();
213-
}
214-
215-
} else {
216-
SPDLOG_DEBUG("LLVM-JIT: AOT disabled, using JIT");
217-
do_jit_compile();
218-
}
103+
do_jit_compile();
219104
} else {
220105
SPDLOG_DEBUG("LLVM-JIT: already compiled");
221106
}

0 commit comments

Comments
 (0)