|
34 | 34 | #include <string>
|
35 | 35 | #include <spdlog/spdlog.h>
|
36 | 36 | #include <tuple>
|
37 |
| -#include <picosha2.h> |
38 | 37 | #include <boost/interprocess/sync/file_lock.hpp>
|
39 | 38 | #include <boost/interprocess/sync/scoped_lock.hpp>
|
40 | 39 |
|
@@ -97,125 +96,11 @@ void llvm_bpf_jit_context::do_jit_compile()
|
97 | 96 | this->jit = std::move(jit);
|
98 | 97 | }
|
99 | 98 |
|
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 |
| - |
173 | 99 | ebpf_jit_fn llvm_bpf_jit_context::compile()
|
174 | 100 | {
|
175 | 101 | spin_lock_guard guard(compiling.get());
|
176 | 102 | 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(); |
219 | 104 | } else {
|
220 | 105 | SPDLOG_DEBUG("LLVM-JIT: already compiled");
|
221 | 106 | }
|
|
0 commit comments