Skip to content

Commit

Permalink
map handle caching: Add API to disable map handle caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
ned14 committed Sep 10, 2021
1 parent a9215c6 commit 7238591
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
34 changes: 30 additions & 4 deletions include/llfio/v2.0/detail/impl/map_handle.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ namespace detail
};
struct map_handle_cache_base_t
{
std::mutex lock;
mutable std::mutex lock;
size_t trie_count{0};
map_handle_cache_item_t *trie_children[8 * sizeof(size_t)];
bool trie_nobbledir{0};
bool trie_nobbledir{0}, disabled{false};
size_t bytes_in_cache{0}, hits{0}, misses{0};
};
static const size_t page_size_shift = [] { return QUICKCPPLIB_NAMESPACE::algorithm::bitwise_trie::detail::bitscanr(utils::page_size()); }();
Expand All @@ -72,6 +72,12 @@ namespace detail

~map_handle_cache_t() { trim_cache(std::chrono::steady_clock::now(), (size_t) -1); }

bool is_disabled() const noexcept
{
_lock_guard g(lock);
return _base::disabled;
}

using _base::size;
void *get(size_t bytes, size_t page_size)
{
Expand Down Expand Up @@ -157,6 +163,13 @@ namespace detail
ret.misses = _base::misses;
return ret;
}
bool set_cache_disabled(bool v)
{
_lock_guard g(lock);
bool ret = _base::disabled;
_base::disabled = v;
return ret;
}
};
extern inline QUICKCPPLIB_SYMBOL_EXPORT map_handle_cache_t &map_handle_cache()
{
Expand All @@ -171,12 +184,17 @@ result<map_handle> map_handle::_recycled_map(size_type bytes, section_handle::fl
{
return errc::argument_out_of_domain;
}
auto &c = detail::map_handle_cache();
if(c.is_disabled())
{
return _new_map(bytes, false, _flag);
}
result<map_handle> ret(map_handle(nullptr, _flag));
native_handle_type &nativeh = ret.value()._v;
OUTCOME_TRY(auto &&pagesize, detail::pagesize_from_flags(ret.value()._flag));
bytes = utils::round_up_to_page_size(bytes, pagesize);
LLFIO_LOG_FUNCTION_CALL(&ret);
void *addr = detail::map_handle_cache().get(bytes, pagesize);
void *addr = c.get(bytes, pagesize);
if(addr == nullptr)
{
return _new_map(bytes, false, _flag);
Expand Down Expand Up @@ -233,6 +251,10 @@ bool map_handle::_recycle_map() noexcept
{
LLFIO_LOG_FUNCTION_CALL(this);
auto &c = detail::map_handle_cache();
if(c.is_disabled())
{
return false;
}
#ifdef _WIN32
if(!win32_release_nonfile_allocations(_addr, _length, MEM_DECOMMIT))
{
Expand All @@ -254,7 +276,7 @@ bool map_handle::_recycle_map() noexcept
if(c.do_not_store_failed_count.load(std::memory_order_relaxed) < 10)
{
auto r = do_not_store({_addr, _length});
if(!r)
if(!r || r.assume_value().size() == 0)
{
c.do_not_store_failed_count.fetch_add(1, std::memory_order_relaxed);
}
Expand All @@ -275,5 +297,9 @@ map_handle::cache_statistics map_handle::trim_cache(std::chrono::steady_clock::t
return detail::map_handle_cache().trim_cache(older_than, max_items);
}

bool map_handle::set_cache_disabled(bool disabled) noexcept
{
return detail::map_handle_cache().set_cache_disabled(disabled);
}

LLFIO_V2_NAMESPACE_END
4 changes: 4 additions & 0 deletions include/llfio/v2.0/map_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,10 @@ class LLFIO_DECL map_handle : public lockable_io_handle
*/
static LLFIO_HEADERS_ONLY_MEMFUNC_SPEC cache_statistics trim_cache(std::chrono::steady_clock::time_point older_than = {},
size_t max_items = (size_t) -1) noexcept;
/*! Disable the map handle cache, returning its previous setting. Note that you may also
wish to explicitly trim the cache.
*/
static LLFIO_HEADERS_ONLY_MEMFUNC_SPEC bool set_cache_disabled(bool disabled) noexcept;

//! The memory section this handle is using
section_handle *section() const noexcept { return _section; }
Expand Down

0 comments on commit 7238591

Please sign in to comment.