0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-16 15:00:51 +01:00

ircd::fs: Add a preliminary fincore(1)-like tool.

This commit is contained in:
Jason Volk 2019-04-20 16:59:08 -07:00
parent c4ace8cd8e
commit fbcd38fa17
2 changed files with 41 additions and 1 deletions

View file

@ -27,6 +27,9 @@ namespace ircd::fs
std::string read(const fd &, const read_opts & = read_opts_default);
std::string read(const string_view &path, const read_opts & = read_opts_default);
// Test whether bytes in the specified range are cached and should not block
bool fetched(const fd &, const size_t &, const read_opts & = read_opts_default);
// Prefetch bytes for subsequent read(); offset is given in opts.
size_t prefetch(const fd &, const size_t &, const read_opts & = read_opts_default);
}

View file

@ -484,7 +484,6 @@ ircd::fs::prefetch(const fd &fd,
const size_t &count,
const read_opts &opts)
{
assert(opts.op == op::READ);
static const size_t max_count
{
128_KiB
@ -504,6 +503,44 @@ ircd::fs::prefetch(const fd &fd,
return count;
}
bool
ircd::fs::fetched(const fd &fd,
const size_t &count,
const read_opts &opts)
{
assert(opts.offset % info::page_size == 0);
const size_t &map_size
{
count?: size(fd)
};
void *const &map
{
::mmap(nullptr, map_size, PROT_NONE, MAP_NONBLOCK | MAP_SHARED, int(fd), opts.offset)
};
if(unlikely(map == MAP_FAILED))
throw_system_error(errno);
const custom_ptr<void> map_ptr
{
map, [&map_size](void *const &map)
{
syscall(::munmap, map, map_size);
}
};
const size_t vec_size
{
std::max(((map_size + info::page_size - 1) / info::page_size) / 8, 1UL)
};
assert(vec_size > 0 && vec_size < map_size);
std::vector<uint64_t> vec(vec_size);
syscall(::mincore, map, map_size, reinterpret_cast<uint8_t *>(vec.data()));
return std::find(begin(vec), end(vec), 0UL) == end(vec);
}
std::string
ircd::fs::read(const string_view &path,
const read_opts &opts)