Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file: add file_system_space #2597

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/seastar/core/file-types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#ifndef SEASTAR_MODULE
#include <cstdint>
#include <fcntl.h>
#include <sys/stat.h>
#include <type_traits>
Expand Down Expand Up @@ -158,6 +159,18 @@ inline constexpr file_permissions operator&(file_permissions a, file_permissions
return file_permissions(std::underlying_type_t<file_permissions>(a) & std::underlying_type_t<file_permissions>(b));
}

/// seastar::space_info is equivalent to std::filesystem::space_info
/// with renamed members, to prevent a conflict with future::available()
struct space_info {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use std::filesystem::space_info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned it in a comment.
The compiler complains about it due to the available member conflicting with future::available. I can reproduce and quote the error

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can it conflict with future::available? It's in a different class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::uintmax_t capacity;
std::uintmax_t free_space;
std::uintmax_t available_space;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint64_t is sufficient, uintmax_t could be 128-but or whatever.


bool operator==(const space_info& o) const noexcept {
return capacity == o.capacity && free_space == o.free_space && available_space == o.available_space;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= default

};

/// @}

SEASTAR_MODULE_EXPORT_END
Expand Down
1 change: 1 addition & 0 deletions include/seastar/core/reactor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ public:
return file_accessible(pathname, access_flags::exists);
}
future<fs_type> file_system_at(std::string_view pathname) noexcept;
future<space_info> file_system_space(std::string_view pathname) noexcept;
future<struct statvfs> statvfs(std::string_view pathname) noexcept;
future<> remove_file(std::string_view pathname) noexcept;
future<> rename_file(std::string_view old_pathname, std::string_view new_pathname) noexcept;
Expand Down
5 changes: 5 additions & 0 deletions include/seastar/core/seastar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,11 @@ future<uint64_t> fs_avail(std::string_view name) noexcept;
future<uint64_t> fs_free(std::string_view name) noexcept;
/// @}

/// Return filesystem-wide stat where a file is located.
///
/// \param name name of the file to inspect
future<space_info> file_system_space(std::string_view name) noexcept;

namespace experimental {
/// \defgroup interprocess-module Interprocess Communication
///
Expand Down
20 changes: 20 additions & 0 deletions src/core/reactor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,26 @@ reactor::fstatfs(int fd) noexcept {
});
}

future<space_info>
reactor::file_system_space(std::string_view pathname) noexcept {
// Allocating memory for a filesystem::path can throw, hence the futurize_invoke
return futurize_invoke([this, pathname] {
return _thread_pool->submit<syscall_result_extra<std::filesystem::space_info>>([path = std::filesystem::path(pathname)] {
std::error_code ec;
auto si = std::filesystem::space(path, ec);
return wrap_syscall(ec.value(), si);
}).then([pathname = sstring(pathname)] (syscall_result_extra<std::filesystem::space_info> sr) {
sr.throw_fs_exception_if_error("file_system_space failed", pathname);
auto& fs_si = sr.extra;
return space_info{
.capacity = fs_si.capacity,
.free_space = fs_si.free,
.available_space = fs_si.available
};
});
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use coroutines in new code.


future<struct statvfs>
reactor::statvfs(std::string_view pathname) noexcept {
// Allocating memory for a sstring can throw, hence the futurize_invoke
Expand Down
4 changes: 4 additions & 0 deletions src/util/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ future<uint64_t> fs_free(std::string_view name) noexcept {
});
}

future<space_info> file_system_space(std::string_view name) noexcept {
return engine().file_system_space(name);
}

future<stat_data> file_stat(std::string_view name, follow_symlink follow) noexcept {
return engine().file_stat(name, follow);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/file_io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
* Copyright (C) 2014-2015 Cloudius Systems, Ltd.
*/

#include <filesystem>

#include <seastar/testing/random.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
#include <seastar/testing/test_runner.hh>

#include <seastar/core/reactor.hh>
#include <seastar/core/seastar.hh>
#include <seastar/core/semaphore.hh>
#include <seastar/core/condition-variable.hh>
Expand Down Expand Up @@ -947,3 +950,20 @@ SEASTAR_TEST_CASE(test_oversized_io_works) {
BOOST_REQUIRE((size_t)std::count_if(buf.get(), buf.get() + buf_size, [](auto x) { return x == 'a'; }) == buf_size);
});
}

SEASTAR_TEST_CASE(test_file_system_space) {
return tmp_dir::do_with_thread([] (tmp_dir& t) {
const auto& name = t.get_path().native();
struct statvfs st;
space_info si, si1;
do {
si = file_system_space(name).get();
st = engine().statvfs(name).get();
si1 = file_system_space(name).get();
} while (si1 != si);

BOOST_REQUIRE_EQUAL(st.f_blocks * st.f_frsize, si.capacity);
BOOST_REQUIRE_EQUAL(st.f_bfree * st.f_frsize, si.free_space);
BOOST_REQUIRE_EQUAL(st.f_bavail * st.f_frsize, si.available_space);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't guaranteed to pass. If a byte is added to the filesystem after assignment to si and removed after assignment to st, si == si1 && si != st.

There isn't a simple way to test this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without syncing the filesystem, it is auto-synced rarely (every 30 seconds?), so we shouldn't see these fluctuations in practice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who says space is updated on sync?

});
}
Loading