file_utils.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <optional>
8 #include <string>
9 
10 #include <kvikio/shim/cufile_h_wrapper.hpp>
11 
12 namespace kvikio {
16 class FileWrapper {
17  private:
18  int _fd{-1};
19 
20  public:
29  FileWrapper(std::string const& file_path, std::string const& flags, bool o_direct, mode_t mode);
30 
34  FileWrapper() noexcept = default;
35 
36  ~FileWrapper() noexcept;
37  FileWrapper(FileWrapper const&) = delete;
38  FileWrapper& operator=(FileWrapper const&) = delete;
39  FileWrapper(FileWrapper&& o) noexcept;
40  FileWrapper& operator=(FileWrapper&& o) noexcept;
41 
50  void open(std::string const& file_path, std::string const& flags, bool o_direct, mode_t mode);
51 
57  bool opened() const noexcept;
58 
62  void close() noexcept;
63 
69  int fd() const noexcept;
70 };
71 
76  private:
77  CUfileHandle_t _handle{};
78  bool _registered{false};
79 
80  public:
81  CUFileHandleWrapper() noexcept = default;
82  ~CUFileHandleWrapper() noexcept;
84  CUFileHandleWrapper& operator=(CUFileHandleWrapper const&) = delete;
86  CUFileHandleWrapper& operator=(CUFileHandleWrapper&& o) noexcept;
87 
95  std::optional<CUfileError_t> register_handle(int fd) noexcept;
96 
102  bool registered() const noexcept;
103 
109  CUfileHandle_t handle() const noexcept;
110 
114  void unregister_handle() noexcept;
115 };
116 
127 int open_fd_parse_flags(std::string const& flags, bool o_direct);
128 
137 int open_fd(std::string const& file_path, std::string const& flags, bool o_direct, mode_t mode);
138 
144 [[nodiscard]] int open_flags(int fd);
145 
152 [[nodiscard]] std::size_t get_file_size(std::string const& file_path);
153 
160 [[nodiscard]] std::size_t get_file_size(int file_descriptor);
161 
169 std::pair<std::size_t, std::size_t> get_page_cache_info(std::string const& file_path);
170 
179 std::pair<std::size_t, std::size_t> get_page_cache_info(int fd);
180 
202 bool clear_page_cache(bool reclaim_dentries_and_inodes = true, bool clear_dirty_pages = true);
203 } // namespace kvikio
Class that provides RAII for the cuFile handle.
Definition: file_utils.hpp:75
bool registered() const noexcept
Check if the handle has been registered.
std::optional< CUfileError_t > register_handle(int fd) noexcept
Register the file handle given the file descriptor.
Class that provides RAII for file handling.
Definition: file_utils.hpp:16
void open(std::string const &file_path, std::string const &flags, bool o_direct, mode_t mode)
Open file using open(2)
bool opened() const noexcept
Check if the file has been opened.
void close() noexcept
Close the file if it is opened; do nothing otherwise.
int fd() const noexcept
Return the file descriptor.
FileWrapper() noexcept=default
Construct an empty file wrapper object without opening a file.
FileWrapper(std::string const &file_path, std::string const &flags, bool o_direct, mode_t mode)
Open file.
KvikIO namespace.
Definition: batch.hpp:16
int open_fd(std::string const &file_path, std::string const &flags, bool o_direct, mode_t mode)
Open file using open(2)
int open_fd_parse_flags(std::string const &flags, bool o_direct)
Parse open file flags given as a string and return oflags.
std::pair< std::size_t, std::size_t > get_page_cache_info(std::string const &file_path)
Obtain the page cache residency information for a given file.
bool clear_page_cache(bool reclaim_dentries_and_inodes=true, bool clear_dirty_pages=true)
Clear the page cache.
std::size_t get_file_size(std::string const &file_path)
Get file size from file descriptor fstat(3)
int open_flags(int fd)
Get the flags of the file descriptor (see open(2))