Skip to content

Commit

Permalink
Fix section_handle::flag::write_via_syscall, which broke if buffers w…
Browse files Browse the repository at this point in the history
…ere zero sized.
  • Loading branch information
ned14 committed Sep 24, 2021
1 parent 721503d commit a5e450e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmake/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(llfio_TESTS
"test/tests/map_handle_create_close/kernel_map_handle.cpp.hpp"
"test/tests/map_handle_create_close/runner.cpp"
"test/tests/mapped.cpp"
"test/tests/mapped_file_handle.cpp"
"test/tests/path_discovery.cpp"
"test/tests/path_view.cpp"
"test/tests/pipe_handle.cpp"
Expand Down
6 changes: 3 additions & 3 deletions include/llfio/revision.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Note the second line of this file must ALWAYS be the git SHA, third line ALWAYS the git SHA update time
#define LLFIO_PREVIOUS_COMMIT_REF 7238591f99132d8c0b223c144f21276e952c8e3f
#define LLFIO_PREVIOUS_COMMIT_DATE "2021-09-14 19:42:09 +00:00"
#define LLFIO_PREVIOUS_COMMIT_UNIQUE 7238591f
#define LLFIO_PREVIOUS_COMMIT_REF 721503d32fe35dbaa93bde0214ae8cd3799d14b8
#define LLFIO_PREVIOUS_COMMIT_DATE "2021-09-17 17:12:12 +00:00"
#define LLFIO_PREVIOUS_COMMIT_UNIQUE 721503d3
2 changes: 1 addition & 1 deletion include/llfio/v2.0/detail/impl/posix/io_handle.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ size_t io_handle::_do_max_buffers() const noexcept
v = 1;
#else
long r = sysconf(_SC_IOV_MAX);
if(r == -1)
if(r < 1)
{
#ifdef IOV_MAX
r = IOV_MAX;
Expand Down
7 changes: 6 additions & 1 deletion include/llfio/v2.0/mapped_file_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,19 @@ class LLFIO_DECL mapped_file_handle : public file_handle
assert(_mh.native_handle()._init == native_handle()._init);
if(!!(_sh.section_flags() & section_handle::flag::write_via_syscall))
{
const auto batch = max_buffers();
const auto batch = file_handle::_do_max_buffers();
io_request<const_buffers_type> thisreq(reqs);
LLFIO_DEADLINE_TO_SLEEP_INIT(d);
for(size_t n = 0; n < reqs.buffers.size();)
{
deadline nd;
LLFIO_DEADLINE_TO_PARTIAL_DEADLINE(nd, d);
thisreq.buffers = reqs.buffers.subspan(n, std::min(batch, reqs.buffers.size() - n));
if(thisreq.buffers.size() == 1 && thisreq.buffers.front().size() == 0)
{
n++;
continue;
}
OUTCOME_TRY(auto &&written, file_handle::_do_write(thisreq, nd));
if(written.empty())
{
Expand Down
82 changes: 82 additions & 0 deletions test/tests/mapped_file_handle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* Integration test kernel for whether the mapped file handle works
(C) 2021 Niall Douglas <http://www.nedproductions.biz/> (2 commits)
File Created: Sept 2021
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file Licence.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

#include "../test_kernel_decl.hpp"

static constexpr size_t DATA_SIZE = 1024 * 1024;

template <class T, class F> void runtest(T &v, F &&write)
{
namespace llfio = LLFIO_V2_NAMESPACE;
QUICKCPPLIB_NAMESPACE::algorithm::small_prng::small_prng rand;
std::vector<llfio::file_handle::const_buffer_type> buffers;
std::vector<char> bytes(4096);
for(char n = ' '; n < 'z'; n++)
{
memset(bytes.data(), n, bytes.size());
auto i = rand();
auto offset = i % DATA_SIZE;
auto bufferslen = (i >> 24);
buffers.resize(bufferslen);
size_t count = 0;
uint32_t b = 0;
for(; b < bufferslen && count < bytes.size() - 15; b++)
{
buffers[b] = llfio::file_handle::const_buffer_type((llfio::byte *) bytes.data() + count, rand() & 15);
count += buffers[b].size();
}
buffers.resize(b);
llfio::file_handle::io_request<llfio::file_handle::const_buffers_type> req(buffers, offset);
write(v, req);
}
};


static inline void TestMappedFileHandle()
{
namespace llfio = LLFIO_V2_NAMESPACE;
std::vector<llfio::byte> reference(DATA_SIZE);
runtest(reference, [](std::vector<llfio::byte> &cont, llfio::file_handle::io_request<llfio::file_handle::const_buffers_type> req) {
for(auto &b : req.buffers)
{
memcpy(cont.data() + req.offset, b.data(), b.size());
req.offset += b.size();
}
});
auto mf1 = llfio::mapped_file_handle::mapped_temp_inode(DATA_SIZE).value();
mf1.truncate(DATA_SIZE).value();
runtest(mf1, [](llfio::mapped_file_handle &cont, llfio::file_handle::io_request<llfio::file_handle::const_buffers_type> req) { cont.write(req).value(); });
BOOST_CHECK(0 == memcmp(mf1.address(), reference.data(), DATA_SIZE));
auto mf2 =
llfio::mapped_file_handle::mapped_temp_inode(DATA_SIZE, llfio::path_discovery::storage_backed_temporary_files_directory(), llfio::file_handle::mode::write,
llfio::file_handle::flag::none, llfio::section_handle::flag::write_via_syscall)
.value();
mf2.truncate(DATA_SIZE).value();
runtest(mf2, [](llfio::mapped_file_handle &cont, llfio::file_handle::io_request<llfio::file_handle::const_buffers_type> req) {
cont.write(req).value();
});
BOOST_CHECK(0 == memcmp(mf2.address(), reference.data(), DATA_SIZE));
}

KERNELTEST_TEST_KERNEL(integration, llfio, mapped_file_handle, cache, "Tests that the mapped_file_handle works as expected", TestMappedFileHandle())

0 comments on commit a5e450e

Please sign in to comment.