Skip to content

Commit

Permalink
Add functions for cache query/management.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluescarni committed Aug 19, 2023
1 parent 9c88480 commit 361fd7b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/heyoka/llvm_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ class HEYOKA_DLL_PUBLIC llvm_state
std::uintptr_t jit_lookup(const std::string &);

[[nodiscard]] llvm_state make_similar() const;

// Cache management.
static std::size_t get_memcache_size();
static std::size_t get_memcache_limit();
static void set_memcache_limit(std::size_t);
static void clear_memcache();
};

namespace detail
Expand Down
5 changes: 5 additions & 0 deletions src/llvm_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,11 @@ void llvm_state::save_impl(Archive &ar, unsigned) const
}
}

// NOTE: currently loading from an archive won't interact with the
// memory cache - that is, if the archive contains a compiled module
// not in the cache *before* loading, it won't have been inserted in the cache
// *after* loading. I don't think this is an issue at the moment, but if needed
// we can always implement the feature at a later stage.
template <typename Archive>
void llvm_state::load_impl(Archive &ar, unsigned version)
{
Expand Down
41 changes: 41 additions & 0 deletions src/llvm_state_mem_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <variant>

#include <boost/container_hash/hash.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/safe_numerics/safe_integer.hpp>
#include <boost/unordered_map.hpp>

Expand Down Expand Up @@ -192,6 +193,9 @@ void llvm_state_mem_cache_try_insert(std::string bc, unsigned opt_level, llvm_mc
const auto cur_size
= static_cast<std::size_t>(cur_val.opt_bc.size()) + cur_val.opt_ir.size() + cur_val.obj.size();

Check warning on line 194 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L194

Added line #L194 was not covered by tests

// NOTE: the next 4 lines cannot throw, which ensures that the
// cache cannot be left in an inconsistent state.

// Remove the last item in the queue.
lru_map.erase(cur_it);
lru_queue.pop_back();

Check warning on line 201 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L200-L201

Added lines #L200 - L201 were not covered by tests
Expand Down Expand Up @@ -236,4 +240,41 @@ void llvm_state_mem_cache_try_insert(std::string bc, unsigned opt_level, llvm_mc

} // namespace detail

std::size_t llvm_state::get_memcache_size()

Check warning on line 243 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L243

Added line #L243 was not covered by tests
{
// Lock down.
const std::lock_guard lock(detail::mem_cache_mutex);

Check warning on line 246 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L246

Added line #L246 was not covered by tests

return detail::mem_cache_size;
}

Check warning on line 249 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L248-L249

Added lines #L248 - L249 were not covered by tests

std::size_t llvm_state::get_memcache_limit()

Check warning on line 251 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L251

Added line #L251 was not covered by tests
{
// Lock down.
const std::lock_guard lock(detail::mem_cache_mutex);

Check warning on line 254 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L254

Added line #L254 was not covered by tests

return boost::numeric_cast<std::size_t>(detail::mem_cache_limit);
}

Check warning on line 257 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L256-L257

Added lines #L256 - L257 were not covered by tests

void llvm_state::set_memcache_limit(std::size_t new_limit)

Check warning on line 259 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L259

Added line #L259 was not covered by tests
{
// Lock down.
const std::lock_guard lock(detail::mem_cache_mutex);

Check warning on line 262 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L262

Added line #L262 was not covered by tests

detail::mem_cache_limit = boost::numeric_cast<std::uint64_t>(new_limit);
}

Check warning on line 265 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L264-L265

Added lines #L264 - L265 were not covered by tests

void llvm_state::clear_memcache()

Check warning on line 267 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L267

Added line #L267 was not covered by tests
{
// Lock down.
const std::lock_guard lock(detail::mem_cache_mutex);

Check warning on line 270 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L270

Added line #L270 was not covered by tests

// Sanity checks.
detail::llvm_state_mem_cache_sanity_checks();

Check warning on line 273 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L273

Added line #L273 was not covered by tests

detail::lru_map.clear();
detail::lru_queue.clear();
detail::mem_cache_size = 0;
}

Check warning on line 278 in src/llvm_state_mem_cache.cpp

View check run for this annotation

Codecov / codecov/patch

src/llvm_state_mem_cache.cpp#L275-L278

Added lines #L275 - L278 were not covered by tests

HEYOKA_END_NAMESPACE

0 comments on commit 361fd7b

Please sign in to comment.