cuSBF
Loading...
Searching...
No Matches
fastx_host_memory.cuh
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6#include <filesystem>
7#include <string>
8#include <string_view>
9
11
12#if defined(__linux__)
13 #include <sys/stat.h>
14 #include <unistd.h>
15#endif
16
17namespace cusbf::detail {
18
20inline constexpr size_t kDefaultFastxHostRamSlackBytes = 4u << 30;
21
23 const std::string_view value = getenv_value("CUSBF_FASTX_HOST_RAM_SLACK_MB");
24 if (value.empty()) {
26 }
27
29 if (mebibytes == 0) {
31 }
32 return static_cast<size_t>(mebibytes) << 20;
33}
34
37#if defined(__linux__)
38 const long page_size = ::sysconf(_SC_PAGESIZE);
40 if (page_size > 0 && avail_pages > 0) {
41 return static_cast<size_t>(page_size) * static_cast<size_t>(avail_pages);
42 }
43#endif
44 return 0;
45}
46
50 if (const uint64_t mebibytes = parse_env_mebibytes(getenv_value("CUSBF_FASTX_MMAP_MAX_MB"));
51 mebibytes != 0) {
52 cap_bytes = mebibytes << 20;
53 }
54
56 if (available == 0) {
57 return cap_bytes;
58 }
59
60 const size_t slack = fastx_host_ram_slack_bytes();
61 const size_t ram_budget = available > slack ? available - slack : size_t{0};
62 return std::min(cap_bytes, static_cast<uint64_t>(ram_budget));
63}
64
66[[nodiscard]] inline bool fastx_file_fits_in_memory(const std::filesystem::path& path) {
67#if defined(__linux__)
68 const std::string path_string = path.string();
69 struct stat file_status{};
70 if (::stat(path_string.c_str(), &file_status) != 0 || file_status.st_size < 0) {
71 return false;
72 }
73 const auto file_bytes = static_cast<uint64_t>(file_status.st_size);
75#else
76 (void)path;
77 return false;
78#endif
79}
80
81} // namespace cusbf::detail
uint64_t fastx_memory_map_max_bytes()
Upper bound on file bytes that may be mmap'd (env cap and available RAM minus slack).
size_t fastx_host_ram_slack_bytes()
bool fastx_file_fits_in_memory(const std::filesystem::path &path)
True when uncompressed path size is within fastx_memory_map_max_bytes.
size_t query_available_host_bytes()
Available physical RAM (bytes) for mmap budgeting.
constexpr size_t kDefaultFastxHostRamSlackBytes
Default headroom left for the OS and other processes when sizing mmap.
uint64_t parse_env_mebibytes(std::string_view value)
Parses a decimal mebibyte count from value.
consteval bool separatorPositionAlwaysEncodesInvalid(char *input, uint64_t separatorPosition, uint64_t index)
Recursively tests whether placing the separator byte at any position in an input of valid bytes alway...
Definition Alphabet.cuh:37
std::string_view getenv_value(const char *env_name)
Reads env_name via getenv, or an empty view when unset.